blob: c44d53626d69362a6cf34bf49ae0f494cb13f3c3 [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 Denoyelled68f8b52023-05-30 15:04:46 +020028static void qcs_free_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
Amaury Denoyelle4b167002022-12-12 09:59:50 +010029{
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;
Amaury Denoyelle217b0f42023-09-21 17:06:16 +020040
41 /* Reset DEM_FULL as buffer is released. This ensures mux is not woken
42 * up from rcv_buf stream callback when demux was previously blocked.
43 */
44 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle4b167002022-12-12 09:59:50 +010045}
46
47/* Free <qcs> instance. This function is reserved for internal usage : it must
48 * only be called on qcs alloc error or on connection shutdown. Else
Ilya Shipitsin07be66d2023-04-01 12:26:42 +020049 * qcs_destroy must be preferred to handle QUIC flow-control increase.
Amaury Denoyelle4b167002022-12-12 09:59:50 +010050 */
51static void qcs_free(struct qcs *qcs)
52{
53 struct qcc *qcc = qcs->qcc;
54
55 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn, qcs);
56
Amaury Denoyelle15337fd2022-12-20 14:47:16 +010057 /* Safe to use even if already removed from the list. */
58 LIST_DEL_INIT(&qcs->el_opening);
Amaury Denoyelle20f2a422023-01-03 14:39:24 +010059 LIST_DEL_INIT(&qcs->el_send);
Amaury Denoyelle4b167002022-12-12 09:59:50 +010060
61 /* Release stream endpoint descriptor. */
62 BUG_ON(qcs->sd && !se_fl_test(qcs->sd, SE_FL_ORPHAN));
63 sedesc_free(qcs->sd);
64
65 /* Release app-layer context. */
66 if (qcs->ctx && qcc->app_ops->detach)
67 qcc->app_ops->detach(qcs);
68
69 /* Release qc_stream_desc buffer from quic-conn layer. */
70 qc_stream_desc_release(qcs->stream);
71
72 /* Free Rx/Tx buffers. */
Amaury Denoyelled68f8b52023-05-30 15:04:46 +020073 qcs_free_ncbuf(qcs, &qcs->rx.ncbuf);
Amaury Denoyelle4b167002022-12-12 09:59:50 +010074 b_free(&qcs->tx.buf);
75
Amaury Denoyelle4b167002022-12-12 09:59:50 +010076 /* Remove qcs from qcc tree. */
77 eb64_delete(&qcs->by_id);
78
79 pool_free(pool_head_qcs, qcs);
80
81 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
82}
83
Amaury Denoyelledeed7772021-12-03 11:36:46 +010084/* Allocate a new QUIC streams with id <id> and type <type>. */
Amaury Denoyellea509ffb2022-07-04 15:50:33 +020085static struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +010086{
Amaury Denoyelledeed7772021-12-03 11:36:46 +010087 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010088
Amaury Denoyelle4f137572022-03-24 17:10:00 +010089 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
90
Amaury Denoyelledeed7772021-12-03 11:36:46 +010091 qcs = pool_alloc(pool_head_qcs);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +020092 if (!qcs) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +020093 TRACE_ERROR("alloc failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle17014a62022-04-27 15:09:27 +020094 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +020095 }
Amaury Denoyelle17014a62022-04-27 15:09:27 +020096
97 qcs->stream = NULL;
98 qcs->qcc = qcc;
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +020099 qcs->sd = NULL;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200100 qcs->flags = QC_SF_NONE;
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200101 qcs->st = QC_SS_IDLE;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200102 qcs->ctx = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100103
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200104 /* App callback attach may register the stream for http-request wait.
105 * These fields must be initialed before.
106 */
107 LIST_INIT(&qcs->el_opening);
Amaury Denoyelle20f2a422023-01-03 14:39:24 +0100108 LIST_INIT(&qcs->el_send);
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200109 qcs->start = TICK_ETERNITY;
110
Amaury Denoyelle4b167002022-12-12 09:59:50 +0100111 /* store transport layer stream descriptor in qcc tree */
112 qcs->id = qcs->by_id.key = id;
113 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
114
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100115 /* If stream is local, use peer remote-limit, or else the opposite. */
Amaury Denoyelle176174f2022-10-21 17:02:18 +0200116 if (quic_stream_is_bidi(id)) {
117 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
118 qcc->rfctl.msd_bidi_l;
119 }
120 else if (quic_stream_is_local(qcc, id)) {
121 qcs->tx.msd = qcc->rfctl.msd_uni_l;
122 }
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100123
Amaury Denoyelled323ab32023-10-09 16:15:20 +0200124 /* Properly set flow-control blocking if initial MSD is nul. */
125 if (!qcs->tx.msd)
126 qcs->flags |= QC_SF_BLK_SFCTL;
127
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200128 qcs->rx.ncbuf = NCBUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100129 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200130 qcs->rx.offset = qcs->rx.offset_max = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100131
Amaury Denoyelle176174f2022-10-21 17:02:18 +0200132 if (quic_stream_is_bidi(id)) {
133 qcs->rx.msd = quic_stream_is_local(qcc, id) ? qcc->lfctl.msd_bidi_l :
134 qcc->lfctl.msd_bidi_r;
135 }
136 else if (quic_stream_is_remote(qcc, id)) {
137 qcs->rx.msd = qcc->lfctl.msd_uni_r;
138 }
Amaury Denoyellea9773552022-05-16 14:38:25 +0200139 qcs->rx.msd_init = qcs->rx.msd;
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200140
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100141 qcs->tx.buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100142 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100143 qcs->tx.sent_offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100144
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100145 qcs->wait_event.tasklet = NULL;
146 qcs->wait_event.events = 0;
147 qcs->subs = NULL;
148
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200149 qcs->err = 0;
150
Amaury Denoyelle87386502023-10-11 17:32:04 +0200151 /* Allocate transport layer stream descriptor. Only needed for TX. */
152 if (!quic_stream_is_uni(id) || !quic_stream_is_remote(qcc, id)) {
153 struct quic_conn *qc = qcc->conn->handle.qc;
154 qcs->stream = qc_stream_desc_new(id, type, qcs, qc);
155 if (!qcs->stream) {
156 TRACE_ERROR("qc_stream_desc alloc failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
157 goto err;
158 }
159 }
160
Amaury Denoyelle3d550842023-01-24 17:42:21 +0100161 if (qcc->app_ops->attach && qcc->app_ops->attach(qcs, qcc->ctx)) {
162 TRACE_ERROR("app proto failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
163 goto err;
164 }
165
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100166 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100167 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100168 return qcs;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200169
170 err:
Amaury Denoyelle4b167002022-12-12 09:59:50 +0100171 qcs_free(qcs);
172 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200173 return NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100174}
175
Amaury Denoyelle3abeb572022-07-04 11:42:27 +0200176static forceinline struct stconn *qcs_sc(const struct qcs *qcs)
177{
178 return qcs->sd ? qcs->sd->sc : NULL;
179}
180
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200181/* Reset the <qcc> inactivity timeout for http-keep-alive timeout. */
182static forceinline void qcc_reset_idle_start(struct qcc *qcc)
183{
184 qcc->idle_start = now_ms;
185}
186
Amaury Denoyellec603de42022-07-25 11:21:46 +0200187/* Decrement <qcc> sc. */
188static forceinline void qcc_rm_sc(struct qcc *qcc)
189{
Amaury Denoyelle8d6d2462023-05-11 16:55:30 +0200190 BUG_ON(!qcc->nb_sc); /* Ensure sc count is always valid (ie >=0). */
Amaury Denoyellec603de42022-07-25 11:21:46 +0200191 --qcc->nb_sc;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200192
193 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
194 * refreshed after this on stream detach.
195 */
196 if (!qcc->nb_sc && !qcc->nb_hreq)
197 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200198}
199
200/* Decrement <qcc> hreq. */
201static forceinline void qcc_rm_hreq(struct qcc *qcc)
202{
Amaury Denoyelle8d6d2462023-05-11 16:55:30 +0200203 BUG_ON(!qcc->nb_hreq); /* Ensure http req count is always valid (ie >=0). */
Amaury Denoyellec603de42022-07-25 11:21:46 +0200204 --qcc->nb_hreq;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200205
206 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
207 * refreshed after this on I/O handler.
208 */
209 if (!qcc->nb_sc && !qcc->nb_hreq)
210 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200211}
212
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200213static inline int qcc_is_dead(const struct qcc *qcc)
214{
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200215 /* Maintain connection if stream endpoints are still active. */
216 if (qcc->nb_sc)
217 return 0;
218
219 /* Connection considered dead if either :
220 * - remote error detected at tranport level
221 * - error detected locally
222 * - MUX timeout expired or unset
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200223 */
Amaury Denoyelle5f67b172023-05-04 18:52:42 +0200224 if (qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL_DONE) ||
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200225 !qcc->task) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200226 return 1;
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200227 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200228
229 return 0;
230}
231
232/* Return true if the mux timeout should be armed. */
233static inline int qcc_may_expire(struct qcc *qcc)
234{
235 return !qcc->nb_sc;
236}
237
238/* Refresh the timeout on <qcc> if needed depending on its state. */
239static void qcc_refresh_timeout(struct qcc *qcc)
240{
241 const struct proxy *px = qcc->proxy;
242
243 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
244
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200245 if (!qcc->task) {
246 TRACE_DEVEL("already expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200247 goto leave;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200248 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200249
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200250 /* Check if upper layer is responsible of timeout management. */
251 if (!qcc_may_expire(qcc)) {
252 TRACE_DEVEL("not eligible for timeout", QMUX_EV_QCC_WAKE, qcc->conn);
253 qcc->task->expire = TICK_ETERNITY;
254 task_queue(qcc->task);
255 goto leave;
256 }
257
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200258 /* Frontend timeout management
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +0100259 * - shutdown done -> timeout client-fin
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200260 * - detached streams with data left to send -> default timeout
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200261 * - stream waiting on incomplete request or no stream yet activated -> timeout http-request
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200262 * - idle after stream processing -> timeout http-keep-alive
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +0100263 *
264 * If proxy stop-stop in progress, immediate or spread close will be
265 * processed if shutdown already one or connection is idle.
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200266 */
267 if (!conn_is_back(qcc->conn)) {
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +0100268 if (qcc->nb_hreq && !(qcc->flags & QC_CF_APP_SHUT)) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200269 TRACE_DEVEL("one or more requests still in progress", QMUX_EV_QCC_WAKE, qcc->conn);
270 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200271 task_queue(qcc->task);
272 goto leave;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200273 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200274
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +0100275 if ((!LIST_ISEMPTY(&qcc->opening_list) || unlikely(!qcc->largest_bidi_r)) &&
276 !(qcc->flags & QC_CF_APP_SHUT)) {
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200277 int timeout = px->timeout.httpreq;
278 struct qcs *qcs = NULL;
279 int base_time;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200280
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200281 /* Use start time of first stream waiting on HTTP or
282 * qcc idle if no stream not yet used.
283 */
284 if (likely(!LIST_ISEMPTY(&qcc->opening_list)))
285 qcs = LIST_ELEM(qcc->opening_list.n, struct qcs *, el_opening);
286 base_time = qcs ? qcs->start : qcc->idle_start;
287
288 TRACE_DEVEL("waiting on http request", QMUX_EV_QCC_WAKE, qcc->conn, qcs);
289 qcc->task->expire = tick_add_ifset(base_time, timeout);
290 }
291 else {
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +0100292 if (qcc->flags & QC_CF_APP_SHUT) {
293 TRACE_DEVEL("connection in closing", QMUX_EV_QCC_WAKE, qcc->conn);
294 qcc->task->expire = tick_add_ifset(now_ms,
295 qcc->shut_timeout);
296 }
297 else {
298 /* Use http-request timeout if keep-alive timeout not set */
299 int timeout = tick_isset(px->timeout.httpka) ?
300 px->timeout.httpka : px->timeout.httpreq;
301 TRACE_DEVEL("at least one request achieved but none currently in progress", QMUX_EV_QCC_WAKE, qcc->conn);
302 qcc->task->expire = tick_add_ifset(qcc->idle_start, timeout);
303 }
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +0100304
305 /* If proxy soft-stop in progress and connection is
306 * inactive, close the connection immediately. If a
307 * close-spread-time is configured, randomly spread the
308 * timer over a closing window.
309 */
310 if ((qcc->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) &&
311 !(global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)) {
312
313 /* Wake timeout task immediately if window already expired. */
314 int remaining_window = tick_isset(global.close_spread_end) ?
315 tick_remain(now_ms, global.close_spread_end) : 0;
316
317 TRACE_DEVEL("proxy disabled, prepare connection soft-stop", QMUX_EV_QCC_WAKE, qcc->conn);
318 if (remaining_window) {
319 /* We don't need to reset the expire if it would
320 * already happen before the close window end.
321 */
322 if (!tick_isset(qcc->task->expire) ||
323 tick_is_le(global.close_spread_end, qcc->task->expire)) {
324 /* Set an expire value shorter than the current value
325 * because the close spread window end comes earlier.
326 */
327 qcc->task->expire = tick_add(now_ms,
328 statistical_prng_range(remaining_window));
329 }
330 }
331 else {
332 /* We are past the soft close window end, wake the timeout
333 * task up immediately.
334 */
335 qcc->task->expire = now_ms;
336 task_wakeup(qcc->task, TASK_WOKEN_TIMER);
337 }
338 }
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200339 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200340 }
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200341
342 /* fallback to default timeout if frontend specific undefined or for
343 * backend connections.
344 */
345 if (!tick_isset(qcc->task->expire)) {
346 TRACE_DEVEL("fallback to default timeout", QMUX_EV_QCC_WAKE, qcc->conn);
347 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200348 }
349
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200350 task_queue(qcc->task);
351
352 leave:
353 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
354}
355
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200356/* Mark a stream as open if it was idle. This can be used on every
357 * successful emission/reception operation to update the stream state.
358 */
359static void qcs_idle_open(struct qcs *qcs)
360{
361 /* This operation must not be used if the stream is already closed. */
362 BUG_ON_HOT(qcs->st == QC_SS_CLO);
363
364 if (qcs->st == QC_SS_IDLE) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200365 TRACE_STATE("opening stream", QMUX_EV_QCS_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200366 qcs->st = QC_SS_OPEN;
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200367 }
368}
369
370/* Close the local channel of <qcs> instance. */
371static void qcs_close_local(struct qcs *qcs)
372{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200373 TRACE_STATE("closing stream locally", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
374
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200375 /* The stream must have already been opened. */
376 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
377
378 /* This operation cannot be used multiple times. */
379 BUG_ON_HOT(qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO);
380
381 if (quic_stream_is_bidi(qcs->id)) {
382 qcs->st = (qcs->st == QC_SS_HREM) ? QC_SS_CLO : QC_SS_HLOC;
Amaury Denoyelleafb7b9d2022-09-19 11:58:24 +0200383
384 if (qcs->flags & QC_SF_HREQ_RECV)
385 qcc_rm_hreq(qcs->qcc);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200386 }
387 else {
388 /* Only local uni streams are valid for this operation. */
389 BUG_ON_HOT(quic_stream_is_remote(qcs->qcc, qcs->id));
390 qcs->st = QC_SS_CLO;
391 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200392}
393
394/* Close the remote channel of <qcs> instance. */
395static void qcs_close_remote(struct qcs *qcs)
396{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200397 TRACE_STATE("closing stream remotely", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
398
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200399 /* The stream must have already been opened. */
400 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
401
402 /* This operation cannot be used multiple times. */
403 BUG_ON_HOT(qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO);
404
405 if (quic_stream_is_bidi(qcs->id)) {
406 qcs->st = (qcs->st == QC_SS_HLOC) ? QC_SS_CLO : QC_SS_HREM;
407 }
408 else {
409 /* Only remote uni streams are valid for this operation. */
410 BUG_ON_HOT(quic_stream_is_local(qcs->qcc, qcs->id));
411 qcs->st = QC_SS_CLO;
412 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200413}
414
415static int qcs_is_close_local(struct qcs *qcs)
416{
417 return qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO;
418}
419
Amaury Denoyelle6eb3c4b2022-12-09 16:26:03 +0100420static int qcs_is_close_remote(struct qcs *qcs)
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200421{
422 return qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO;
423}
424
Amaury Denoyelle0abde9d2023-05-11 16:52:17 +0200425/* Allocate if needed buffer <bptr> for stream <qcs>.
426 *
427 * Returns the buffer instance or NULL on allocation failure.
428 */
Amaury Denoyelled68f8b52023-05-30 15:04:46 +0200429struct buffer *qcs_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100430{
Amaury Denoyelle0abde9d2023-05-11 16:52:17 +0200431 return b_alloc(bptr);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100432}
433
Amaury Denoyelled00b3092023-05-11 17:00:54 +0200434/* Allocate if needed buffer <ncbuf> for stream <qcs>.
435 *
436 * Returns the buffer instance or NULL on allocation failure.
437 */
Amaury Denoyelled68f8b52023-05-30 15:04:46 +0200438static struct ncbuf *qcs_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200439{
440 struct buffer buf = BUF_NULL;
441
442 if (ncb_is_null(ncbuf)) {
Amaury Denoyelled00b3092023-05-11 17:00:54 +0200443 if (!b_alloc(&buf))
444 return NULL;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200445
446 *ncbuf = ncb_make(buf.area, buf.size, 0);
447 ncb_init(ncbuf, 0);
448 }
449
450 return ncbuf;
451}
452
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500453/* Notify an eventual subscriber on <qcs> or else wakeup up the stconn layer if
Amaury Denoyelle4561f842022-07-06 14:54:34 +0200454 * initialized.
455 */
456static void qcs_alert(struct qcs *qcs)
457{
458 if (qcs->subs) {
459 qcs_notify_recv(qcs);
460 qcs_notify_send(qcs);
461 }
462 else if (qcs_sc(qcs) && qcs->sd->sc->app_ops->wake) {
Amaury Denoyelle2d5c3f52023-05-11 13:41:41 +0200463 TRACE_POINT(QMUX_EV_STRM_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle4561f842022-07-06 14:54:34 +0200464 qcs->sd->sc->app_ops->wake(qcs->sd->sc);
465 }
466}
467
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100468int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
469{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100470 struct qcc *qcc = qcs->qcc;
471
472 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100473
474 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
475 BUG_ON(qcs->subs && qcs->subs != es);
476
477 es->events |= event_type;
478 qcs->subs = es;
479
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100480 if (event_type & SUB_RETRY_RECV)
481 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
482
483 if (event_type & SUB_RETRY_SEND)
484 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
485
486 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
487
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100488 return 0;
489}
490
491void qcs_notify_recv(struct qcs *qcs)
492{
493 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
Amaury Denoyelle2d5c3f52023-05-11 13:41:41 +0200494 TRACE_POINT(QMUX_EV_STRM_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100495 tasklet_wakeup(qcs->subs->tasklet);
496 qcs->subs->events &= ~SUB_RETRY_RECV;
497 if (!qcs->subs->events)
498 qcs->subs = NULL;
499 }
500}
501
502void qcs_notify_send(struct qcs *qcs)
503{
504 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
Amaury Denoyelle2d5c3f52023-05-11 13:41:41 +0200505 TRACE_POINT(QMUX_EV_STRM_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100506 tasklet_wakeup(qcs->subs->tasklet);
507 qcs->subs->events &= ~SUB_RETRY_SEND;
508 if (!qcs->subs->events)
509 qcs->subs = NULL;
510 }
511}
512
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200513/* A fatal error is detected locally for <qcc> connection. It should be closed
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200514 * with a CONNECTION_CLOSE using <err> code. Set <app> to true to indicate that
515 * the code must be considered as an application level error. This function
516 * must not be called more than once by connection.
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200517 */
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200518void qcc_set_error(struct qcc *qcc, int err, int app)
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200519{
520 /* This must not be called multiple times per connection. */
521 BUG_ON(qcc->flags & QC_CF_ERRL);
522
523 TRACE_STATE("connection on error", QMUX_EV_QCC_ERR, qcc->conn);
524
525 qcc->flags |= QC_CF_ERRL;
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200526 qcc->err = app ? quic_err_app(err) : quic_err_transport(err);
Amaury Denoyelleda24bcf2023-05-09 18:20:45 +0200527
528 /* TODO
Amaury Denoyelled68f8b52023-05-30 15:04:46 +0200529 * Ensure qcc_io_send() will be conducted to convert QC_CF_ERRL in
Amaury Denoyelleda24bcf2023-05-09 18:20:45 +0200530 * QC_CF_ERRL_DONE with CONNECTION_CLOSE frame emission. This may be
531 * unnecessary if we are currently in the MUX tasklet context, but it
532 * is too tedious too not forget a wakeup outside of this function for
533 * the moment.
534 */
535 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200536}
537
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200538/* Open a locally initiated stream for the connection <qcc>. Set <bidi> for a
539 * bidirectional stream, else an unidirectional stream is opened. The next
540 * available ID on the connection will be used according to the stream type.
541 *
542 * Returns the allocated stream instance or NULL on error.
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100543 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200544struct qcs *qcc_init_stream_local(struct qcc *qcc, int bidi)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100545{
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200546 struct qcs *qcs;
547 enum qcs_type type;
548 uint64_t *next;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100549
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200550 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
551
552 if (bidi) {
553 next = &qcc->next_bidi_l;
554 type = conn_is_back(qcc->conn) ? QCS_CLT_BIDI : QCS_SRV_BIDI;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100555 }
556 else {
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200557 next = &qcc->next_uni_l;
558 type = conn_is_back(qcc->conn) ? QCS_CLT_UNI : QCS_SRV_UNI;
559 }
560
561 /* TODO ensure that we won't overflow remote peer flow control limit on
562 * streams. Else, we should emit a STREAMS_BLOCKED frame.
563 */
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100564
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200565 qcs = qcs_new(qcc, *next, type);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200566 if (!qcs) {
567 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200568 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200569 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200570 }
Amaury Denoyellec055e302022-02-07 16:09:06 +0100571
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200572 TRACE_PROTO("opening local stream", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200573 *next += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100574
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200575 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200576 return qcs;
577}
578
579/* Open a remote initiated stream for the connection <qcc> with ID <id>. The
580 * caller is responsible to ensure that a stream with the same ID was not
581 * already opened. This function will also create all intermediaries streams
582 * with ID smaller than <id> not already opened before.
583 *
584 * Returns the allocated stream instance or NULL on error.
585 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200586static struct qcs *qcc_init_stream_remote(struct qcc *qcc, uint64_t id)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200587{
588 struct qcs *qcs = NULL;
589 enum qcs_type type;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200590 uint64_t *largest, max_id;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100591
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200592 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200593
Amaury Denoyelle8d6d2462023-05-11 16:55:30 +0200594 /* Function reserved to remote stream IDs. */
595 BUG_ON(quic_stream_is_local(qcc, id));
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100596
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200597 if (quic_stream_is_bidi(id)) {
598 largest = &qcc->largest_bidi_r;
599 type = conn_is_back(qcc->conn) ? QCS_SRV_BIDI : QCS_CLT_BIDI;
600 }
601 else {
602 largest = &qcc->largest_uni_r;
603 type = conn_is_back(qcc->conn) ? QCS_SRV_UNI : QCS_CLT_UNI;
604 }
605
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200606 /* RFC 9000 4.6. Controlling Concurrency
607 *
608 * An endpoint that receives a frame with a stream ID exceeding the
609 * limit it has sent MUST treat this as a connection error of type
610 * STREAM_LIMIT_ERROR
611 */
612 max_id = quic_stream_is_bidi(id) ? qcc->lfctl.ms_bidi * 4 :
613 qcc->lfctl.ms_uni * 4;
614 if (id >= max_id) {
615 TRACE_ERROR("flow control error", QMUX_EV_QCS_NEW|QMUX_EV_PROTO_ERR, qcc->conn);
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200616 qcc_set_error(qcc, QC_ERR_STREAM_LIMIT_ERROR, 0);
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200617 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200618 }
619
620 /* Only stream ID not already opened can be used. */
621 BUG_ON(id < *largest);
622
623 while (id >= *largest) {
Amaury Denoyellefd79ddb2022-08-16 11:13:45 +0200624 const char *str = *largest < id ? "initializing intermediary remote stream" :
625 "initializing remote stream";
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200626
627 qcs = qcs_new(qcc, *largest, type);
628 if (!qcs) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200629 TRACE_ERROR("stream fallocation failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200630 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200631 goto err;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100632 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200633
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200634 TRACE_PROTO(str, QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200635 *largest += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100636 }
637
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200638 out:
639 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle50742292022-03-29 14:57:19 +0200640 return qcs;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200641
642 err:
643 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
644 return NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200645}
646
Amaury Denoyelled68f8b52023-05-30 15:04:46 +0200647struct stconn *qcs_attach_sc(struct qcs *qcs, struct buffer *buf, char fin)
Amaury Denoyelle1a2faef2023-05-15 15:17:28 +0200648{
649 struct qcc *qcc = qcs->qcc;
650 struct session *sess = qcc->conn->owner;
651
652 qcs->sd = sedesc_new();
653 if (!qcs->sd)
654 return NULL;
655
656 qcs->sd->se = qcs;
657 qcs->sd->conn = qcc->conn;
658 se_fl_set(qcs->sd, SE_FL_T_MUX | SE_FL_ORPHAN | SE_FL_NOT_FIRST);
659 se_expect_no_data(qcs->sd);
660
661 /* TODO duplicated from mux_h2 */
662 sess->t_idle = ns_to_ms(now_ns - sess->accept_ts) - sess->t_handshake;
663
664 if (!sc_new_from_endp(qcs->sd, sess, buf))
665 return NULL;
666
667 /* QC_SF_HREQ_RECV must be set once for a stream. Else, nb_hreq counter
668 * will be incorrect for the connection.
669 */
670 BUG_ON_HOT(qcs->flags & QC_SF_HREQ_RECV);
671 qcs->flags |= QC_SF_HREQ_RECV;
672 ++qcc->nb_sc;
673 ++qcc->nb_hreq;
674
675 /* TODO duplicated from mux_h2 */
676 sess->accept_date = date;
677 sess->accept_ts = now_ns;
678 sess->t_handshake = 0;
679 sess->t_idle = 0;
680
681 /* A stream must have been registered for HTTP wait before attaching
682 * it to sedesc. See <qcs_wait_http_req> for more info.
683 */
684 BUG_ON_HOT(!LIST_INLIST(&qcs->el_opening));
685 LIST_DEL_INIT(&qcs->el_opening);
686
Amaury Denoyellebf86d892023-05-12 18:16:31 +0200687 if (fin) {
688 TRACE_STATE("report end-of-input", QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellebfddb422023-05-25 15:02:24 +0200689 se_fl_set(qcs->sd, SE_FL_EOI);
Amaury Denoyellebf86d892023-05-12 18:16:31 +0200690 }
691
Amaury Denoyelle1a2faef2023-05-15 15:17:28 +0200692 return qcs->sd->sc;
693}
694
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200695/* Use this function for a stream <id> which is not in <qcc> stream tree. It
696 * returns true if the associated stream is closed.
697 */
698static int qcc_stream_id_is_closed(struct qcc *qcc, uint64_t id)
699{
700 uint64_t *largest;
701
702 /* This function must only be used for stream not present in the stream tree. */
703 BUG_ON_HOT(eb64_lookup(&qcc->streams_by_id, id));
704
705 if (quic_stream_is_local(qcc, id)) {
706 largest = quic_stream_is_uni(id) ? &qcc->next_uni_l :
707 &qcc->next_bidi_l;
708 }
709 else {
710 largest = quic_stream_is_uni(id) ? &qcc->largest_uni_r :
711 &qcc->largest_bidi_r;
712 }
713
714 return id < *largest;
715}
716
717/* Retrieve the stream instance from <id> ID. This can be used when receiving
718 * STREAM, STREAM_DATA_BLOCKED, RESET_STREAM, MAX_STREAM_DATA or STOP_SENDING
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200719 * frames. Set to false <receive_only> or <send_only> if these particular types
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200720 * of streams are not allowed. If the stream instance is found, it is stored in
721 * <out>.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200722 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200723 * Returns 0 on success else non-zero. On error, a RESET_STREAM or a
724 * CONNECTION_CLOSE is automatically emitted. Beware that <out> may be NULL
725 * on success if the stream has already been closed.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200726 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200727int qcc_get_qcs(struct qcc *qcc, uint64_t id, int receive_only, int send_only,
728 struct qcs **out)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200729{
730 struct eb64_node *node;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200731
732 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200733 *out = NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200734
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200735 if (!receive_only && quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200736 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 +0200737 qcc_set_error(qcc, QC_ERR_STREAM_STATE_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200738 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200739 }
740
741 if (!send_only && quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200742 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 +0200743 qcc_set_error(qcc, QC_ERR_STREAM_STATE_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200744 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200745 }
746
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200747 /* Search the stream in the connection tree. */
748 node = eb64_lookup(&qcc->streams_by_id, id);
749 if (node) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200750 *out = eb64_entry(node, struct qcs, by_id);
751 TRACE_DEVEL("using stream from connection tree", QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200752 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200753 }
754
755 /* Check if stream is already closed. */
756 if (qcc_stream_id_is_closed(qcc, id)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200757 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200758 /* Consider this as a success even if <out> is left NULL. */
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200759 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200760 }
761
762 /* Create the stream. This is valid only for remote initiated one. A
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500763 * local stream must have already been explicitly created by the
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200764 * application protocol layer.
765 */
766 if (quic_stream_is_local(qcc, id)) {
767 /* RFC 9000 19.8. STREAM Frames
768 *
769 * An endpoint MUST terminate the connection with error
770 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
771 * initiated stream that has not yet been created, or for a send-only
772 * stream.
773 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200774 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 +0200775 qcc_set_error(qcc, QC_ERR_STREAM_STATE_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200776 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200777 }
778 else {
779 /* Remote stream not found - try to open it. */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200780 *out = qcc_init_stream_remote(qcc, id);
781 if (!*out) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200782 TRACE_ERROR("stream creation error", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200783 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200784 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200785 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100786
787 out:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200788 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200789 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200790
791 err:
792 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
793 return 1;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100794}
795
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200796/* Simple function to duplicate a buffer */
797static inline struct buffer qcs_b_dup(const struct ncbuf *b)
798{
799 return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0));
800}
801
Amaury Denoyelle36d4b5e2022-07-01 11:25:40 +0200802/* Remove <bytes> from <qcs> Rx buffer. Flow-control for received offsets may
803 * be allocated for the peer if needed.
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200804 */
805static void qcs_consume(struct qcs *qcs, uint64_t bytes)
806{
807 struct qcc *qcc = qcs->qcc;
808 struct quic_frame *frm;
809 struct ncbuf *buf = &qcs->rx.ncbuf;
810 enum ncb_ret ret;
811
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200812 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
813
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200814 ret = ncb_advance(buf, bytes);
815 if (ret) {
816 ABORT_NOW(); /* should not happens because removal only in data */
817 }
818
819 if (ncb_is_empty(buf))
Amaury Denoyelled68f8b52023-05-30 15:04:46 +0200820 qcs_free_ncbuf(qcs, buf);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200821
822 qcs->rx.offset += bytes;
Amaury Denoyellebb6296c2022-12-09 15:00:17 +0100823 /* Not necessary to emit a MAX_STREAM_DATA if all data received. */
824 if (qcs->flags & QC_SF_SIZE_KNOWN)
825 goto conn_fctl;
826
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200827 if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200828 TRACE_DATA("increase stream credit via MAX_STREAM_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +0100829 frm = qc_frm_alloc(QUIC_FT_MAX_STREAM_DATA);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +0100830 if (!frm) {
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200831 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +0100832 return;
833 }
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200834
835 qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init;
836
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200837 frm->max_stream_data.id = qcs->id;
838 frm->max_stream_data.max_stream_data = qcs->rx.msd;
839
840 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
841 tasklet_wakeup(qcc->wait_event.tasklet);
842 }
843
Amaury Denoyellebb6296c2022-12-09 15:00:17 +0100844 conn_fctl:
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200845 qcc->lfctl.offsets_consume += bytes;
846 if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200847 TRACE_DATA("increase conn credit via MAX_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +0100848 frm = qc_frm_alloc(QUIC_FT_MAX_DATA);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +0100849 if (!frm) {
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200850 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +0100851 return;
852 }
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200853
854 qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init;
855
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200856 frm->max_data.max_data = qcc->lfctl.md;
857
858 LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list);
859 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
860 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200861
862 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200863}
864
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200865/* Decode the content of STREAM frames already received on the stream instance
866 * <qcs>.
867 *
868 * Returns 0 on success else non-zero.
869 */
870static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
871{
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200872 struct buffer b;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200873 ssize_t ret;
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200874 int fin = 0;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200875
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200876 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
877
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200878 b = qcs_b_dup(&qcs->rx.ncbuf);
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200879
Amaury Denoyelled1310f82022-09-16 13:30:59 +0200880 /* Signal FIN to application if STREAM FIN received with all data. */
881 if (qcs_is_close_remote(qcs))
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200882 fin = 1;
883
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100884 if (!(qcs->flags & QC_SF_READ_ABORTED)) {
885 ret = qcc->app_ops->decode_qcs(qcs, &b, fin);
886 if (ret < 0) {
887 TRACE_ERROR("decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
888 goto err;
889 }
Amaury Denoyelle152beee2023-05-24 14:43:43 +0200890
891 if (qcs->flags & QC_SF_TO_RESET) {
892 if (qcs_sc(qcs) && !se_fl_test(qcs->sd, SE_FL_ERROR|SE_FL_ERR_PENDING)) {
893 se_fl_set_error(qcs->sd);
894 qcs_alert(qcs);
895 }
896 }
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100897 }
898 else {
899 TRACE_DATA("ignore read on stream", QMUX_EV_QCS_RECV, qcc->conn, qcs);
900 ret = b_data(&b);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200901 }
902
Amaury Denoyelle381d8132023-02-17 09:51:20 +0100903 if (ret)
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200904 qcs_consume(qcs, ret);
Amaury Denoyelle381d8132023-02-17 09:51:20 +0100905 if (ret || (!b_data(&b) && fin))
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200906 qcs_notify_recv(qcs);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200907
908 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200909 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200910
911 err:
912 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
913 return 1;
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200914}
915
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200916/* Prepare for the emission of RESET_STREAM on <qcs> with error code <err>. */
917void qcc_reset_stream(struct qcs *qcs, int err)
918{
919 struct qcc *qcc = qcs->qcc;
920
921 if ((qcs->flags & QC_SF_TO_RESET) || qcs_is_close_local(qcs))
922 return;
923
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200924 TRACE_STATE("reset stream", QMUX_EV_QCS_END, qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200925 qcs->flags |= QC_SF_TO_RESET;
926 qcs->err = err;
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +0100927
Amaury Denoyelle178fbff2023-03-22 11:17:59 +0100928 /* Remove prepared stream data from connection flow-control calcul. */
929 if (qcs->tx.offset > qcs->tx.sent_offset) {
930 const uint64_t diff = qcs->tx.offset - qcs->tx.sent_offset;
931 BUG_ON(qcc->tx.offsets - diff < qcc->tx.sent_offsets);
932 qcc->tx.offsets -= diff;
933 /* Reset qcs offset to prevent BUG_ON() on qcs_destroy(). */
934 qcs->tx.offset = qcs->tx.sent_offset;
935 }
936
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +0100937 qcc_send_stream(qcs, 1);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200938 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100939}
940
Amaury Denoyellef9b03262023-01-09 10:34:25 +0100941/* Register <qcs> stream for emission of STREAM, STOP_SENDING or RESET_STREAM.
942 * Set <urg> to 1 if stream content should be treated in priority compared to
943 * other streams.
944 */
945void qcc_send_stream(struct qcs *qcs, int urg)
Amaury Denoyelle20f2a422023-01-03 14:39:24 +0100946{
947 struct qcc *qcc = qcs->qcc;
948
949 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
950
951 /* Cannot send if already closed. */
952 BUG_ON(qcs_is_close_local(qcs));
953
Amaury Denoyellef9b03262023-01-09 10:34:25 +0100954 if (urg) {
955 LIST_DEL_INIT(&qcs->el_send);
956 LIST_INSERT(&qcc->send_list, &qcs->el_send);
957 }
958 else {
959 if (!LIST_INLIST(&qcs->el_send))
960 LIST_APPEND(&qcs->qcc->send_list, &qcs->el_send);
961 }
Amaury Denoyelle20f2a422023-01-03 14:39:24 +0100962
963 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
964}
965
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100966/* Prepare for the emission of STOP_SENDING on <qcs>. */
967void qcc_abort_stream_read(struct qcs *qcs)
968{
969 struct qcc *qcc = qcs->qcc;
970
971 TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn, qcs);
972
973 if ((qcs->flags & QC_SF_TO_STOP_SENDING) || qcs_is_close_remote(qcs))
974 goto end;
975
976 TRACE_STATE("abort stream read", QMUX_EV_QCS_END, qcc->conn, qcs);
977 qcs->flags |= (QC_SF_TO_STOP_SENDING|QC_SF_READ_ABORTED);
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +0100978
979 qcc_send_stream(qcs, 1);
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100980 tasklet_wakeup(qcc->wait_event.tasklet);
981
982 end:
983 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn, qcs);
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200984}
985
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200986/* Install the <app_ops> applicative layer of a QUIC connection on mux <qcc>.
987 * Returns 0 on success else non-zero.
988 */
989int qcc_install_app_ops(struct qcc *qcc, const struct qcc_app_ops *app_ops)
990{
991 TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn);
992
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +0100993 if (app_ops->init && !app_ops->init(qcc)) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200994 TRACE_ERROR("app ops init error", QMUX_EV_QCC_NEW, qcc->conn);
995 goto err;
996 }
997
998 TRACE_PROTO("application layer initialized", QMUX_EV_QCC_NEW, qcc->conn);
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +0100999 qcc->app_ops = app_ops;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001000
Amaury Denoyelle71fd0362023-01-24 17:35:37 +01001001 /* RFC 9114 7.2.4.2. Initialization
1002 *
1003 * Endpoints MUST NOT require any data to be
1004 * received from the peer prior to sending the SETTINGS frame;
1005 * settings MUST be sent as soon as the transport is ready to
1006 * send data.
1007 */
1008 if (qcc->app_ops->finalize) {
1009 if (qcc->app_ops->finalize(qcc->ctx)) {
1010 TRACE_ERROR("app ops finalize error", QMUX_EV_QCC_NEW, qcc->conn);
1011 goto err;
1012 }
1013 tasklet_wakeup(qcc->wait_event.tasklet);
1014 }
1015
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001016 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
1017 return 0;
1018
1019 err:
1020 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
1021 return 1;
1022}
1023
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001024/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
1025 * <data> with length <len> and represents the offset <offset>. <fin> is set if
1026 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001027 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +02001028 * Returns 0 on success else non-zero. On error, the received frame should not
1029 * be acknowledged.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001030 */
1031int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001032 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001033{
1034 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001035 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001036
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001037 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1038
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001039 if (qcc->flags & QC_CF_ERRL) {
1040 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001041 goto err;
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001042 }
1043
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +02001044 /* RFC 9000 19.8. STREAM Frames
1045 *
1046 * An endpoint MUST terminate the connection with error
1047 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
1048 * initiated stream that has not yet been created, or for a send-only
1049 * stream.
1050 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +02001051 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001052 TRACE_DATA("qcs retrieval error", QMUX_EV_QCC_RECV, qcc->conn);
1053 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +02001054 }
1055
1056 if (!qcs) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001057 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV, qcc->conn);
1058 goto out;
Amaury Denoyelle57161b72022-07-07 15:02:32 +02001059 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001060
Amaury Denoyellebf91e392022-07-04 10:02:04 +02001061 /* RFC 9000 4.5. Stream Final Size
1062 *
1063 * Once a final size for a stream is known, it cannot change. If a
1064 * RESET_STREAM or STREAM frame is received indicating a change in the
1065 * final size for the stream, an endpoint SHOULD respond with an error
1066 * of type FINAL_SIZE_ERROR; see Section 11 for details on error
1067 * handling.
1068 */
1069 if (qcs->flags & QC_SF_SIZE_KNOWN &&
1070 (offset + len > qcs->rx.offset_max || (fin && offset + len < qcs->rx.offset_max))) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +02001071 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 +02001072 qcc_set_error(qcc, QC_ERR_FINAL_SIZE_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001073 goto err;
Amaury Denoyellebf91e392022-07-04 10:02:04 +02001074 }
1075
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001076 if (qcs_is_close_remote(qcs)) {
1077 TRACE_DATA("skipping STREAM for remotely closed", QMUX_EV_QCC_RECV, qcc->conn);
1078 goto out;
1079 }
1080
Amaury Denoyellefa241932023-02-14 15:36:36 +01001081 if (offset + len < qcs->rx.offset ||
1082 (offset + len == qcs->rx.offset && (!fin || (qcs->flags & QC_SF_SIZE_KNOWN)))) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001083 TRACE_DATA("already received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1084 goto out;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001085 }
1086
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001087 TRACE_PROTO("receiving STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001088 qcs_idle_open(qcs);
1089
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001090 if (offset + len > qcs->rx.offset_max) {
1091 uint64_t diff = offset + len - qcs->rx.offset_max;
1092 qcs->rx.offset_max = offset + len;
1093 qcc->lfctl.offsets_recv += diff;
1094
1095 if (offset + len > qcs->rx.msd ||
1096 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
1097 /* RFC 9000 4.1. Data Flow Control
1098 *
1099 * A receiver MUST close the connection with an error
1100 * of type FLOW_CONTROL_ERROR if the sender violates
1101 * the advertised connection or stream data limits
1102 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +02001103 TRACE_ERROR("flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001104 qcc->conn, qcs);
Amaury Denoyelle58721f22023-05-09 18:01:09 +02001105 qcc_set_error(qcc, QC_ERR_FLOW_CONTROL_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001106 goto err;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001107 }
1108 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001109
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02001110 if (!qcs_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelled00b3092023-05-11 17:00:54 +02001111 TRACE_ERROR("receive ncbuf alloc failure", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1112 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
1113 goto err;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001114 }
1115
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001116 TRACE_DATA("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001117 if (offset < qcs->rx.offset) {
Frédéric Lécaillea18c3332022-07-04 09:54:58 +02001118 size_t diff = qcs->rx.offset - offset;
1119
1120 len -= diff;
1121 data += diff;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001122 offset = qcs->rx.offset;
1123 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001124
Amaury Denoyellefa241932023-02-14 15:36:36 +01001125 if (len) {
1126 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
1127 switch (ret) {
1128 case NCB_RET_OK:
1129 break;
1130
1131 case NCB_RET_DATA_REJ:
Amaury Denoyellecc3d7162022-05-20 15:14:57 +02001132 /* RFC 9000 2.2. Sending and Receiving Data
1133 *
1134 * An endpoint could receive data for a stream at the
1135 * same stream offset multiple times. Data that has
1136 * already been received can be discarded. The data at
1137 * a given offset MUST NOT change if it is sent
1138 * multiple times; an endpoint MAY treat receipt of
1139 * different data at the same offset within a stream as
1140 * a connection error of type PROTOCOL_VIOLATION.
1141 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +02001142 TRACE_ERROR("overlapping data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001143 qcc->conn, qcs);
Amaury Denoyelle58721f22023-05-09 18:01:09 +02001144 qcc_set_error(qcc, QC_ERR_PROTOCOL_VIOLATION, 0);
Amaury Denoyellefa241932023-02-14 15:36:36 +01001145 return 1;
1146
1147 case NCB_RET_GAP_SIZE:
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001148 TRACE_DATA("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
1149 qcc->conn, qcs);
Amaury Denoyellefa241932023-02-14 15:36:36 +01001150 return 1;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001151 }
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001152 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001153
1154 if (fin)
Amaury Denoyelle3f39b402022-07-01 16:11:03 +02001155 qcs->flags |= QC_SF_SIZE_KNOWN;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001156
Amaury Denoyelled1310f82022-09-16 13:30:59 +02001157 if (qcs->flags & QC_SF_SIZE_KNOWN &&
1158 qcs->rx.offset_max == qcs->rx.offset + ncb_data(&qcs->rx.ncbuf, 0)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001159 qcs_close_remote(qcs);
Amaury Denoyelled1310f82022-09-16 13:30:59 +02001160 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001161
Amaury Denoyellefa241932023-02-14 15:36:36 +01001162 if ((ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) || fin) {
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001163 qcc_decode_qcs(qcc, qcs);
Amaury Denoyelle418ba212022-08-02 15:57:16 +02001164 qcc_refresh_timeout(qcc);
1165 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001166
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001167 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001168 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001169 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001170
1171 err:
1172 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1173 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001174}
1175
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001176/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
1177 * the frame.
1178 *
1179 * Returns 0 on success else non-zero.
1180 */
1181int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
1182{
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001183 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1184
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001185 TRACE_PROTO("receiving MAX_DATA", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001186 if (qcc->rfctl.md < max) {
1187 qcc->rfctl.md = max;
Amaury Denoyelleb7143a82023-03-22 15:08:01 +01001188 TRACE_DATA("increase remote max-data", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001189
1190 if (qcc->flags & QC_CF_BLK_MFCTL) {
1191 qcc->flags &= ~QC_CF_BLK_MFCTL;
1192 tasklet_wakeup(qcc->wait_event.tasklet);
1193 }
1194 }
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001195
1196 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001197 return 0;
1198}
1199
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001200/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
1201 * field of the frame and <id> is the identifier of the QUIC stream.
1202 *
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001203 * Returns 0 on success else non-zero. On error, the received frame should not
1204 * be acknowledged.
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001205 */
1206int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
1207{
1208 struct qcs *qcs;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001209
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001210 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1211
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001212 if (qcc->flags & QC_CF_ERRL) {
1213 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001214 goto err;
1215 }
1216
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001217 /* RFC 9000 19.10. MAX_STREAM_DATA Frames
1218 *
1219 * Receiving a MAX_STREAM_DATA frame for a locally
1220 * initiated stream that has not yet been created MUST be treated as a
1221 * connection error of type STREAM_STATE_ERROR. An endpoint that
1222 * receives a MAX_STREAM_DATA frame for a receive-only stream MUST
1223 * terminate the connection with error STREAM_STATE_ERROR.
1224 */
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001225 if (qcc_get_qcs(qcc, id, 0, 1, &qcs))
1226 goto err;
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001227
1228 if (qcs) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001229 TRACE_PROTO("receiving MAX_STREAM_DATA", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001230 if (max > qcs->tx.msd) {
1231 qcs->tx.msd = max;
Amaury Denoyelleb7143a82023-03-22 15:08:01 +01001232 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 +01001233
1234 if (qcs->flags & QC_SF_BLK_SFCTL) {
1235 qcs->flags &= ~QC_SF_BLK_SFCTL;
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001236 /* TODO optim: only wakeup IO-CB if stream has data to sent. */
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001237 tasklet_wakeup(qcc->wait_event.tasklet);
1238 }
1239 }
1240 }
1241
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001242 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1243 qcc_refresh_timeout(qcc);
1244
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001245 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1246 return 0;
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001247
1248 err:
1249 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
1250 return 1;
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001251}
1252
1253/* Handle a new RESET_STREAM frame from stream ID <id> with error code <err>
1254 * and final stream size <final_size>.
1255 *
1256 * Returns 0 on success else non-zero. On error, the received frame should not
1257 * be acknowledged.
1258 */
1259int qcc_recv_reset_stream(struct qcc *qcc, uint64_t id, uint64_t err, uint64_t final_size)
1260{
1261 struct qcs *qcs;
1262
1263 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1264
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001265 if (qcc->flags & QC_CF_ERRL) {
1266 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001267 goto err;
1268 }
1269
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001270 /* RFC 9000 19.4. RESET_STREAM Frames
1271 *
1272 * An endpoint that receives a RESET_STREAM frame for a send-only stream
1273 * MUST terminate the connection with error STREAM_STATE_ERROR.
1274 */
1275 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
1276 TRACE_ERROR("RESET_STREAM for send-only stream received", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001277 goto err;
1278 }
1279
Amaury Denoyelle3cb78142023-05-15 11:31:20 +02001280 /* RFC 9000 3.2. Receiving Stream States
1281 *
1282 * A RESET_STREAM signal might be suppressed or withheld
1283 * if stream data is completely received and is buffered to be read by
1284 * the application. If the RESET_STREAM is suppressed, the receiving
1285 * part of the stream remains in "Data Recvd".
1286 */
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001287 if (!qcs || qcs_is_close_remote(qcs))
1288 goto out;
1289
1290 TRACE_PROTO("receiving RESET_STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1291 qcs_idle_open(qcs);
1292
Amaury Denoyelle3cb78142023-05-15 11:31:20 +02001293 /* Ensure stream closure is not forbidden by application protocol. */
Amaury Denoyellee269aeb2023-01-30 12:13:22 +01001294 if (qcc->app_ops->close) {
1295 if (qcc->app_ops->close(qcs, QCC_APP_OPS_CLOSE_SIDE_RD)) {
1296 TRACE_ERROR("closure rejected by app layer", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1297 goto out;
1298 }
1299 }
1300
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001301 if (qcs->rx.offset_max > final_size ||
1302 ((qcs->flags & QC_SF_SIZE_KNOWN) && qcs->rx.offset_max != final_size)) {
1303 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 +02001304 qcc_set_error(qcc, QC_ERR_FINAL_SIZE_ERROR, 0);
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001305 goto err;
1306 }
1307
Amaury Denoyelle3cb78142023-05-15 11:31:20 +02001308 /* RFC 9000 3.2. Receiving Stream States
1309 *
1310 * An
1311 * implementation MAY interrupt delivery of stream data, discard any
1312 * data that was not consumed, and signal the receipt of the
1313 * RESET_STREAM.
1314 */
1315 qcs->flags |= QC_SF_SIZE_KNOWN|QC_SF_RECV_RESET;
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001316 qcs_close_remote(qcs);
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02001317 qcs_free_ncbuf(qcs, &qcs->rx.ncbuf);
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001318
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001319 out:
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001320 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001321 return 0;
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001322
1323 err:
1324 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1325 return 1;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001326}
1327
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001328/* Handle a new STOP_SENDING frame for stream ID <id>. The error code should be
1329 * specified in <err>.
1330 *
1331 * Returns 0 on success else non-zero. On error, the received frame should not
1332 * be acknowledged.
1333 */
1334int qcc_recv_stop_sending(struct qcc *qcc, uint64_t id, uint64_t err)
1335{
1336 struct qcs *qcs;
1337
1338 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1339
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001340 if (qcc->flags & QC_CF_ERRL) {
1341 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001342 goto err;
1343 }
1344
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001345 /* RFC 9000 19.5. STOP_SENDING Frames
1346 *
1347 * Receiving a STOP_SENDING frame for a
1348 * locally initiated stream that has not yet been created MUST be
1349 * treated as a connection error of type STREAM_STATE_ERROR. An
1350 * endpoint that receives a STOP_SENDING frame for a receive-only stream
1351 * MUST terminate the connection with error STREAM_STATE_ERROR.
1352 */
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001353 if (qcc_get_qcs(qcc, id, 0, 1, &qcs))
1354 goto err;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001355
1356 if (!qcs)
1357 goto out;
1358
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001359 TRACE_PROTO("receiving STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelled7755372022-10-03 17:20:31 +02001360
1361 /* RFC 9000 3.5. Solicited State Transitions
1362 *
1363 * An endpoint is expected to send another STOP_SENDING frame if a
1364 * packet containing a previous STOP_SENDING is lost. However, once
1365 * either all stream data or a RESET_STREAM frame has been received for
1366 * the stream -- that is, the stream is in any state other than "Recv"
1367 * or "Size Known" -- sending a STOP_SENDING frame is unnecessary.
1368 */
1369
1370 /* TODO thanks to previous RFC clause, STOP_SENDING is ignored if current stream
1371 * has already been closed locally. This is useful to not emit multiple
1372 * RESET_STREAM for a single stream. This is functional if stream is
1373 * locally closed due to all data transmitted, but in this case the RFC
1374 * advices to use an explicit RESET_STREAM.
1375 */
1376 if (qcs_is_close_local(qcs)) {
1377 TRACE_STATE("ignoring STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1378 goto out;
1379 }
1380
Amaury Denoyelle96ca1b72022-08-09 17:36:38 +02001381 qcs_idle_open(qcs);
1382
Amaury Denoyelle87f87662023-01-30 12:12:43 +01001383 if (qcc->app_ops->close) {
1384 if (qcc->app_ops->close(qcs, QCC_APP_OPS_CLOSE_SIDE_WR)) {
1385 TRACE_ERROR("closure rejected by app layer", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1386 goto out;
1387 }
1388 }
1389
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001390 /* RFC 9000 3.5. Solicited State Transitions
1391 *
1392 * An endpoint that receives a STOP_SENDING frame
1393 * MUST send a RESET_STREAM frame if the stream is in the "Ready" or
1394 * "Send" state. If the stream is in the "Data Sent" state, the
1395 * endpoint MAY defer sending the RESET_STREAM frame until the packets
1396 * containing outstanding data are acknowledged or declared lost. If
1397 * any outstanding data is declared lost, the endpoint SHOULD send a
1398 * RESET_STREAM frame instead of retransmitting the data.
1399 *
1400 * An endpoint SHOULD copy the error code from the STOP_SENDING frame to
1401 * the RESET_STREAM frame it sends, but it can use any application error
1402 * code.
1403 */
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001404 qcc_reset_stream(qcs, err);
1405
Amaury Denoyelle152beee2023-05-24 14:43:43 +02001406 /* Report send error to stream-endpoint layer. */
1407 if (qcs_sc(qcs)) {
Amaury Denoyellebfddb422023-05-25 15:02:24 +02001408 /* If FIN already reached, future RESET_STREAMS will be ignored.
1409 * Manually set EOS in this case.
1410 */
1411 if (se_fl_test(qcs->sd, SE_FL_EOI))
1412 se_fl_set(qcs->sd, SE_FL_EOS);
1413
Amaury Denoyelle152beee2023-05-24 14:43:43 +02001414 se_fl_set_error(qcs->sd);
1415 qcs_alert(qcs);
1416 }
1417
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001418 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1419 qcc_refresh_timeout(qcc);
1420
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001421 out:
1422 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1423 return 0;
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001424
1425 err:
1426 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
1427 return 1;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001428}
1429
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001430/* Signal the closing of remote stream with id <id>. Flow-control for new
1431 * streams may be allocated for the peer if needed.
1432 */
1433static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +01001434{
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001435 struct quic_frame *frm;
1436
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001437 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn);
1438
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001439 if (quic_stream_is_bidi(id)) {
1440 ++qcc->lfctl.cl_bidi_r;
1441 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001442 TRACE_DATA("increase max stream limit with MAX_STREAMS_BIDI", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001443 frm = qc_frm_alloc(QUIC_FT_MAX_STREAMS_BIDI);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +01001444 if (!frm) {
Amaury Denoyelle58721f22023-05-09 18:01:09 +02001445 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +01001446 goto err;
1447 }
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001448
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001449 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
1450 qcc->lfctl.cl_bidi_r;
1451 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
1452 tasklet_wakeup(qcc->wait_event.tasklet);
1453
1454 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
1455 qcc->lfctl.cl_bidi_r = 0;
1456 }
1457 }
1458 else {
Amaury Denoyelle91077312022-12-22 18:56:09 +01001459 /* TODO unidirectional stream flow control with MAX_STREAMS_UNI
1460 * emission not implemented. It should be unnecessary for
1461 * HTTP/3 but may be required if other application protocols
1462 * are supported.
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02001463 */
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001464 }
1465
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001466 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
1467
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001468 return 0;
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +01001469
1470 err:
1471 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_END, qcc->conn);
1472 return 1;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001473}
1474
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05001475/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001476static void qcs_destroy(struct qcs *qcs)
1477{
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001478 struct qcc *qcc = qcs->qcc;
1479 struct connection *conn = qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001480 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001481
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001482 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001483
Amaury Denoyelle178fbff2023-03-22 11:17:59 +01001484 /* MUST not removed a stream with sending prepared data left. This is
1485 * to ensure consistency on connection flow-control calculation.
1486 */
1487 BUG_ON(qcs->tx.offset < qcs->tx.sent_offset);
1488
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001489 if (!(qcc->flags & QC_CF_ERRL)) {
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001490 if (quic_stream_is_remote(qcc, id))
1491 qcc_release_remote_stream(qcc, id);
1492 }
Amaury Denoyellec055e302022-02-07 16:09:06 +01001493
Amaury Denoyelledccbd732022-03-29 18:36:59 +02001494 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001495
1496 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001497}
1498
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001499/* Transfer as much as possible data on <qcs> from <in> to <out>. This is done
1500 * in respect with available flow-control at stream and connection level.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001501 *
Amaury Denoyelle0abde9d2023-05-11 16:52:17 +02001502 * Returns the total bytes of transferred data or a negative error code.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001503 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001504static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, struct buffer *in)
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001505{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001506 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001507 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001508 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001509
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001510 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001511
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02001512 if (!qcs_get_buf(qcs, out)) {
Amaury Denoyelle0abde9d2023-05-11 16:52:17 +02001513 TRACE_ERROR("buffer alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1514 goto err;
1515 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001516
1517 /*
1518 * QCS out buffer diagram
1519 * head left to_xfer
1520 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001521 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001522 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001523 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001524 * ^ ack-off ^ sent-off ^ off
1525 *
1526 * STREAM frame
1527 * ^ ^
1528 * |xxxxxxxxxxxxxxxxx|
1529 */
1530
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001531 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001532 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001533 BUG_ON_HOT(qcc->tx.offsets < qcc->tx.sent_offsets);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001534
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001535 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001536 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001537
1538 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
1539 /* do not exceed flow control limit */
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001540 if (qcs->tx.offset + to_xfer > qcs->tx.msd) {
1541 TRACE_DATA("do not exceed stream flow control", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001542 to_xfer = qcs->tx.msd - qcs->tx.offset;
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001543 }
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001544
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001545 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001546 /* do not overcome flow control limit on connection */
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001547 if (qcc->tx.offsets + to_xfer > qcc->rfctl.md) {
1548 TRACE_DATA("do not exceed conn flow control", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001549 to_xfer = qcc->rfctl.md - qcc->tx.offsets;
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001550 }
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001551
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001552 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001553 goto out;
1554
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001555 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001556
1557 out:
1558 {
1559 struct qcs_xfer_data_trace_arg arg = {
1560 .prep = b_data(out), .xfer = total,
1561 };
1562 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
1563 qcc->conn, qcs, &arg);
1564 }
1565
1566 return total;
Amaury Denoyelle0abde9d2023-05-11 16:52:17 +02001567
1568 err:
1569 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1570 return -1;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001571}
1572
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001573/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
1574 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001575 * stream frame. If <out> is NULL an empty STREAM frame is built : this may be
1576 * useful if FIN needs to be sent without any data left.
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001577 *
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001578 * Returns the payload length of the STREAM frame or a negative error code.
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001579 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001580static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
1581 struct list *frm_list)
1582{
1583 struct qcc *qcc = qcs->qcc;
1584 struct quic_frame *frm;
1585 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001586 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001587
1588 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1589
Amaury Denoyellea4569202022-04-15 17:29:25 +02001590 /* if ack_offset < buf_offset, it points to an older buffer. */
1591 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
1592 BUG_ON(qcs->tx.sent_offset < base_off);
1593
1594 head = qcs->tx.sent_offset - base_off;
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001595 total = out ? b_data(out) - head : 0;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001596 BUG_ON(total < 0);
1597
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001598 if (!total && !fin) {
1599 /* No need to send anything if total is NULL and no FIN to signal. */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001600 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1601 return 0;
1602 }
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001603 BUG_ON((!total && qcs->tx.sent_offset > qcs->tx.offset) ||
1604 (total && qcs->tx.sent_offset >= qcs->tx.offset));
Amaury Denoyellea4569202022-04-15 17:29:25 +02001605 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001606 BUG_ON(qcc->tx.sent_offsets + total > qcc->rfctl.md);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001607
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001608 TRACE_PROTO("sending STREAM frame", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001609 frm = qc_frm_alloc(QUIC_FT_STREAM_8);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001610 if (!frm) {
1611 TRACE_ERROR("frame alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001612 goto err;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001613 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001614
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001615 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001616 frm->stream.id = qcs->id;
Amaury Denoyelle1dac0182023-02-02 16:45:07 +01001617 frm->stream.offset.key = 0;
Amaury Denoyelleebfafc22023-03-07 18:07:08 +01001618 frm->stream.dup = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001619
Amaury Denoyelle42c5b752023-04-25 16:39:32 +02001620 if (total) {
1621 frm->stream.buf = out;
1622 frm->stream.data = (unsigned char *)b_peek(out, head);
1623 }
1624 else {
1625 /* Empty STREAM frame. */
1626 frm->stream.buf = NULL;
1627 frm->stream.data = NULL;
1628 }
1629
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +01001630 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001631 if (fin)
1632 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001633
1634 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001635 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001636 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001637 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001638
Amaury Denoyelle42c5b752023-04-25 16:39:32 +02001639 /* Always set length bit as we do not know if there is remaining frames
1640 * in the final packet after this STREAM.
1641 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001642 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
1643 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001644
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001645 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001646
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001647 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001648 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001649 struct qcs_build_stream_trace_arg arg = {
1650 .len = frm->stream.len, .fin = fin,
1651 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001652 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001653 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001654 qcc->conn, qcs, &arg);
1655 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001656
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001657 return total;
1658
1659 err:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001660 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001661 return -1;
1662}
1663
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001664/* Check after transferring data from qcs.tx.buf if FIN must be set on the next
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001665 * STREAM frame for <qcs>.
1666 *
1667 * Returns true if FIN must be set else false.
1668 */
1669static int qcs_stream_fin(struct qcs *qcs)
1670{
1671 return qcs->flags & QC_SF_FIN_STREAM && !b_data(&qcs->tx.buf);
1672}
1673
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001674/* Return true if <qcs> has data to send in new STREAM frames. */
1675static forceinline int qcs_need_sending(struct qcs *qcs)
1676{
1677 return b_data(&qcs->tx.buf) || qcs->tx.sent_offset < qcs->tx.offset ||
1678 qcs_stream_fin(qcs);
1679}
1680
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001681/* This function must be called by the upper layer to inform about the sending
1682 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
1683 * <offset>.
1684 */
1685void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
1686{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001687 struct qcc *qcc = qcs->qcc;
1688 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001689
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001690 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1691
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001692 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001693 BUG_ON(offset + data > qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001694
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001695 /* check if the STREAM frame has already been notified. It can happen
1696 * for retransmission.
1697 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001698 if (offset + data < qcs->tx.sent_offset) {
1699 TRACE_DEVEL("offset already notified", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1700 goto out;
1701 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001702
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001703 qcs_idle_open(qcs);
1704
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001705 diff = offset + data - qcs->tx.sent_offset;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001706 if (diff) {
1707 /* increase offset sum on connection */
1708 qcc->tx.sent_offsets += diff;
1709 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001710 if (qcc->tx.sent_offsets == qcc->rfctl.md) {
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001711 qcc->flags |= QC_CF_BLK_MFCTL;
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001712 TRACE_STATE("connection flow-control reached", QMUX_EV_QCS_SEND, qcc->conn);
1713 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001714
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001715 /* increase offset on stream */
1716 qcs->tx.sent_offset += diff;
1717 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
1718 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001719 if (qcs->tx.sent_offset == qcs->tx.msd) {
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001720 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001721 TRACE_STATE("stream flow-control reached", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1722 }
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001723
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01001724 /* If qcs.stream.buf is full, release it to the lower layer. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001725 if (qcs->tx.offset == qcs->tx.sent_offset &&
1726 b_full(&qcs->stream->buf->buf)) {
1727 qc_stream_buf_release(qcs->stream);
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001728 }
Amaury Denoyelle1bcb6952023-04-28 16:24:44 +02001729
1730 /* Add measurement for send rate. This is done at the MUX layer
1731 * to account only for STREAM frames without retransmission.
Amaury Denoyelle1bcb6952023-04-28 16:24:44 +02001732 */
Amaury Denoyellebc0adfa2023-04-28 16:46:11 +02001733 increment_send_rate(diff, 0);
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001734 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001735
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001736 if (qcs->tx.offset == qcs->tx.sent_offset && !b_data(&qcs->tx.buf)) {
1737 /* Remove stream from send_list if all was sent. */
1738 LIST_DEL_INIT(&qcs->el_send);
1739 TRACE_STATE("stream sent done", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1740
1741 if (qcs->flags & (QC_SF_FIN_STREAM|QC_SF_DETACH)) {
1742 /* Close stream locally. */
1743 qcs_close_local(qcs);
1744 /* Reset flag to not emit multiple FIN STREAM frames. */
1745 qcs->flags &= ~QC_SF_FIN_STREAM;
1746 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001747 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001748
1749 out:
1750 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001751}
1752
Amaury Denoyelleb35e32e2023-05-03 09:50:25 +02001753/* Returns true if subscribe set, false otherwise. */
1754static int qcc_subscribe_send(struct qcc *qcc)
1755{
1756 struct connection *conn = qcc->conn;
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02001757
1758 /* Do not subscribe if lower layer in error. */
1759 if (conn->flags & CO_FL_ERROR)
1760 return 0;
1761
Amaury Denoyelleb35e32e2023-05-03 09:50:25 +02001762 if (qcc->wait_event.events & SUB_RETRY_SEND)
1763 return 1;
1764
1765 TRACE_DEVEL("subscribe for send", QMUX_EV_QCC_SEND, qcc->conn);
1766 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &qcc->wait_event);
1767 return 1;
1768}
1769
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001770/* Wrapper for send on transport layer. Send a list of frames <frms> for the
1771 * connection <qcc>.
1772 *
1773 * Returns 0 if all data sent with success else non-zero.
1774 */
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02001775static int qcc_send_frames(struct qcc *qcc, struct list *frms)
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001776{
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001777 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
1778
1779 if (LIST_ISEMPTY(frms)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001780 TRACE_DEVEL("no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
1781 goto err;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001782 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +01001783
Amaury Denoyellecaa16542023-02-28 15:11:26 +01001784 if (!qc_send_mux(qcc->conn->handle.qc, frms)) {
Amaury Denoyelleb35e32e2023-05-03 09:50:25 +02001785 TRACE_DEVEL("error on sending", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelleb35e32e2023-05-03 09:50:25 +02001786 qcc_subscribe_send(qcc);
Amaury Denoyelle036cc5d2022-09-26 15:02:31 +02001787 goto err;
Amaury Denoyellecaa16542023-02-28 15:11:26 +01001788 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001789
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001790 /* If there is frames left at this stage, transport layer is blocked.
1791 * Subscribe on it to retry later.
1792 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001793 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelleb35e32e2023-05-03 09:50:25 +02001794 TRACE_DEVEL("remaining frames to send", QMUX_EV_QCC_SEND, qcc->conn);
1795 qcc_subscribe_send(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001796 goto err;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001797 }
1798
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001799 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001800 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001801
1802 err:
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02001803 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001804 return 1;
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001805}
1806
1807/* Emit a RESET_STREAM on <qcs>.
1808 *
1809 * Returns 0 if the frame has been successfully sent else non-zero.
1810 */
1811static int qcs_send_reset(struct qcs *qcs)
1812{
1813 struct list frms = LIST_HEAD_INIT(frms);
1814 struct quic_frame *frm;
1815
1816 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1817
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001818 frm = qc_frm_alloc(QUIC_FT_RESET_STREAM);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001819 if (!frm) {
1820 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001821 return 1;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001822 }
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001823
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001824 frm->reset_stream.id = qcs->id;
1825 frm->reset_stream.app_error_code = qcs->err;
1826 frm->reset_stream.final_size = qcs->tx.sent_offset;
1827
1828 LIST_APPEND(&frms, &frm->list);
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02001829 if (qcc_send_frames(qcs->qcc, &frms)) {
Amaury Denoyelle131f2d92023-05-09 14:10:55 +02001830 if (!LIST_ISEMPTY(&frms))
1831 qc_frm_free(&frm);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001832 TRACE_DEVEL("cannot send RESET_STREAM", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1833 return 1;
1834 }
1835
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001836 qcs_close_local(qcs);
1837 qcs->flags &= ~QC_SF_TO_RESET;
1838
1839 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001840 return 0;
1841}
1842
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001843/* Emit a STOP_SENDING on <qcs>.
1844 *
1845 * Returns 0 if the frame has been successfully sent else non-zero.
1846 */
1847static int qcs_send_stop_sending(struct qcs *qcs)
1848{
1849 struct list frms = LIST_HEAD_INIT(frms);
1850 struct quic_frame *frm;
1851 struct qcc *qcc = qcs->qcc;
1852
1853 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1854
1855 /* RFC 9000 3.3. Permitted Frame Types
1856 *
1857 * A
1858 * receiver MAY send a STOP_SENDING frame in any state where it has not
1859 * received a RESET_STREAM frame -- that is, states other than "Reset
1860 * Recvd" or "Reset Read". However, there is little value in sending a
1861 * STOP_SENDING frame in the "Data Recvd" state, as all stream data has
1862 * been received. A sender could receive either of these two types of
1863 * frames in any state as a result of delayed delivery of packets.¶
1864 */
1865 if (qcs_is_close_remote(qcs)) {
1866 TRACE_STATE("skip STOP_SENDING on remote already closed", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1867 goto done;
1868 }
1869
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001870 frm = qc_frm_alloc(QUIC_FT_STOP_SENDING);
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001871 if (!frm) {
1872 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1873 return 1;
1874 }
1875
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001876 frm->stop_sending.id = qcs->id;
1877 frm->stop_sending.app_error_code = qcs->err;
1878
1879 LIST_APPEND(&frms, &frm->list);
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02001880 if (qcc_send_frames(qcs->qcc, &frms)) {
Amaury Denoyelle131f2d92023-05-09 14:10:55 +02001881 if (!LIST_ISEMPTY(&frms))
1882 qc_frm_free(&frm);
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001883 TRACE_DEVEL("cannot send STOP_SENDING", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1884 return 1;
1885 }
1886
1887 done:
1888 qcs->flags &= ~QC_SF_TO_STOP_SENDING;
1889
1890 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1891 return 0;
1892}
1893
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02001894/* Used internally by qcc_io_send function. Proceed to send for <qcs>. This will
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001895 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001896 * is then generated and inserted in <frms> list.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001897 *
1898 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001899 * be null if out buffer cannot be allocated. On error a negative error code is
1900 * used.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001901 */
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02001902static int qcs_send(struct qcs *qcs, struct list *frms)
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001903{
1904 struct qcc *qcc = qcs->qcc;
1905 struct buffer *buf = &qcs->tx.buf;
1906 struct buffer *out = qc_stream_buf_get(qcs->stream);
Amaury Denoyelle6c501ed2023-05-12 16:19:32 +02001907 int xfer = 0, buf_avail;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001908 char fin = 0;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001909
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001910 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1911
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001912 /* Cannot send STREAM on remote unidirectional streams. */
1913 BUG_ON(quic_stream_is_uni(qcs->id) && quic_stream_is_remote(qcc, qcs->id));
1914
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001915 if (b_data(buf)) {
1916 /* Allocate <out> buffer if not already done. */
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001917 if (!out) {
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001918 if (qcc->flags & QC_CF_CONN_FULL)
1919 goto out;
1920
Amaury Denoyelle6c501ed2023-05-12 16:19:32 +02001921 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset,
1922 &buf_avail);
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001923 if (!out) {
Amaury Denoyelle1611a762023-05-15 13:56:46 +02001924 if (buf_avail) {
Amaury Denoyelle6c501ed2023-05-12 16:19:32 +02001925 TRACE_ERROR("stream desc alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1926 goto err;
1927 }
1928
Amaury Denoyelle1611a762023-05-15 13:56:46 +02001929 TRACE_STATE("hitting stream desc buffer limit", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001930 qcc->flags |= QC_CF_CONN_FULL;
1931 goto out;
1932 }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001933 }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001934
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001935 /* Transfer data from <buf> to <out>. */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001936 xfer = qcs_xfer_data(qcs, out, buf);
Amaury Denoyelle0abde9d2023-05-11 16:52:17 +02001937 if (xfer < 0)
1938 goto err;
1939
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001940 if (xfer > 0) {
1941 qcs_notify_send(qcs);
1942 qcs->flags &= ~QC_SF_BLK_MROOM;
1943 }
1944
1945 qcs->tx.offset += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001946 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001947 qcc->tx.offsets += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001948 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001949
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001950 /* out buffer cannot be emptied if qcs offsets differ. */
1951 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1952 }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001953
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001954 /* FIN is set if all incoming data were transferred. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001955 fin = qcs_stream_fin(qcs);
1956
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001957 /* Build a new STREAM frame with <out> buffer. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001958 if (qcs->tx.sent_offset != qcs->tx.offset || fin) {
Amaury Denoyelle04b22082023-05-03 09:50:39 +02001959 /* Skip STREAM frame allocation if already subscribed for send.
1960 * Happens on sendto transient error or network congestion.
1961 */
1962 if (qcc->wait_event.events & SUB_RETRY_SEND) {
1963 TRACE_DEVEL("already subscribed for sending",
1964 QMUX_EV_QCS_SEND, qcc->conn, qcs);
1965 goto err;
1966 }
1967
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001968 if (qcs_build_stream_frm(qcs, out, fin, frms) < 0)
1969 goto err;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001970 }
1971
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001972 out:
1973 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001974 return xfer;
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001975
1976 err:
1977 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1978 return -1;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001979}
1980
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001981/* Proceed to sending. Loop through all available streams for the <qcc>
1982 * instance and try to send as much as possible.
1983 *
1984 * Returns the total of bytes sent to the transport layer.
1985 */
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02001986static int qcc_io_send(struct qcc *qcc)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001987{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001988 struct list frms = LIST_HEAD_INIT(frms);
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001989 /* Temporary list for QCS on error. */
1990 struct list qcs_failed = LIST_HEAD_INIT(qcs_failed);
Amaury Denoyelle7c5591f2023-04-21 14:48:01 +02001991 struct qcs *qcs, *qcs_tmp, *first_qcs = NULL;
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001992 int ret, total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001993
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001994 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001995
Amaury Denoyelle04b22082023-05-03 09:50:39 +02001996 /* TODO if socket in transient error, sending should be temporarily
1997 * disabled for all frames. However, checking for send subscription is
1998 * not valid as this may be caused by a congestion error which only
1999 * apply for STREAM frames.
2000 */
2001
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002002 /* Check for transport error. */
2003 if (qcc->flags & QC_CF_ERR_CONN || qcc->conn->flags & CO_FL_ERROR) {
2004 TRACE_DEVEL("connection on error", QMUX_EV_QCC_SEND, qcc->conn);
2005 goto out;
2006 }
2007
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002008 /* Check for locally detected connection error. */
2009 if (qcc->flags & QC_CF_ERRL) {
2010 /* Prepare a CONNECTION_CLOSE if not already done. */
2011 if (!(qcc->flags & QC_CF_ERRL_DONE)) {
2012 TRACE_DATA("report a connection error", QMUX_EV_QCC_SEND|QMUX_EV_QCC_ERR, qcc->conn);
2013 quic_set_connection_close(qcc->conn->handle.qc, qcc->err);
2014 qcc->flags |= QC_CF_ERRL_DONE;
2015 }
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02002016 goto out;
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002017 }
2018
2019 if (qcc->conn->flags & CO_FL_SOCK_WR_SH) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002020 qcc->conn->flags |= CO_FL_ERROR;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002021 TRACE_DEVEL("connection on error", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02002022 goto out;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002023 }
2024
Amaury Denoyellec985cb12022-05-16 14:29:59 +02002025 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002026 if (qcc_send_frames(qcc, &qcc->lfctl.frms)) {
Amaury Denoyellec985cb12022-05-16 14:29:59 +02002027 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
2028 goto out;
2029 }
2030 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02002031
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01002032 if (qcc->flags & QC_CF_BLK_MFCTL)
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02002033 goto out;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01002034
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01002035 /* Send STREAM/STOP_SENDING/RESET_STREAM data for registered streams. */
2036 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_list, el_send) {
Amaury Denoyelle7c5591f2023-04-21 14:48:01 +02002037 /* Check if all QCS were processed. */
2038 if (qcs == first_qcs)
2039 break;
2040
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01002041 /* Stream must not be present in send_list if it has nothing to send. */
2042 BUG_ON(!(qcs->flags & (QC_SF_TO_STOP_SENDING|QC_SF_TO_RESET)) &&
2043 !qcs_need_sending(qcs));
Amaury Denoyellec6195d72022-05-23 11:39:14 +02002044
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01002045 /* Each STOP_SENDING/RESET_STREAM frame is sent individually to
2046 * guarantee its emission.
2047 *
2048 * TODO multiplex several frames in same datagram to optimize sending
2049 */
2050 if (qcs->flags & QC_SF_TO_STOP_SENDING) {
2051 if (qcs_send_stop_sending(qcs))
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02002052 goto sent_done;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01002053
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01002054 /* Remove stream from send_list if it had only STOP_SENDING
2055 * to send.
2056 */
2057 if (!(qcs->flags & QC_SF_TO_RESET) && !qcs_need_sending(qcs)) {
2058 LIST_DEL_INIT(&qcs->el_send);
2059 continue;
2060 }
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01002061 }
2062
Amaury Denoyelle843a1192022-07-04 11:44:38 +02002063 if (qcs->flags & QC_SF_TO_RESET) {
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01002064 if (qcs_send_reset(qcs))
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02002065 goto sent_done;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002066
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01002067 /* RFC 9000 3.3. Permitted Frame Types
2068 *
2069 * A sender MUST NOT send
2070 * a STREAM or STREAM_DATA_BLOCKED frame for a stream in the
2071 * "Reset Sent" state or any terminal state -- that is, after
2072 * sending a RESET_STREAM frame.
2073 */
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01002074 LIST_DEL_INIT(&qcs->el_send);
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02002075 continue;
2076 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02002077
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02002078 if (!(qcs->flags & QC_SF_BLK_SFCTL)) {
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002079 if ((ret = qcs_send(qcs, &frms)) < 0) {
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02002080 /* Temporarily remove QCS from send-list. */
2081 LIST_DEL_INIT(&qcs->el_send);
2082 LIST_APPEND(&qcs_failed, &qcs->el_send);
2083 continue;
2084 }
2085
2086 total += ret;
Amaury Denoyelle7c5591f2023-04-21 14:48:01 +02002087 if (ret) {
2088 /* Move QCS with some bytes transferred at the
2089 * end of send-list for next iterations.
2090 */
2091 LIST_DEL_INIT(&qcs->el_send);
2092 LIST_APPEND(&qcc->send_list, &qcs->el_send);
2093 /* Remember first moved QCS as checkpoint to interrupt loop */
2094 if (!first_qcs)
2095 first_qcs = qcs;
2096 }
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02002097 }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02002098 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02002099
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01002100 /* Retry sending until no frame to send, data rejected or connection
2101 * flow-control limit reached.
2102 */
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002103 while (qcc_send_frames(qcc, &frms) == 0 && !(qcc->flags & QC_CF_BLK_MFCTL)) {
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01002104 /* Reloop over <qcc.send_list>. Useful for streams which have
2105 * fulfilled their qc_stream_desc buf and have now release it.
2106 */
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02002107 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_list, el_send) {
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01002108 /* Only streams blocked on flow-control or waiting on a
2109 * new qc_stream_desc should be present in send_list as
2110 * long as transport layer can handle all data.
2111 */
2112 BUG_ON(qcs->stream->buf && !(qcs->flags & QC_SF_BLK_SFCTL));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02002113
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02002114 if (!(qcs->flags & QC_SF_BLK_SFCTL)) {
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002115 if ((ret = qcs_send(qcs, &frms)) < 0) {
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02002116 LIST_DEL_INIT(&qcs->el_send);
2117 LIST_APPEND(&qcs_failed, &qcs->el_send);
2118 continue;
2119 }
2120
2121 total += ret;
2122 }
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01002123 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +02002124 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02002125
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02002126 sent_done:
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02002127 /* Deallocate frames that the transport layer has rejected. */
2128 if (!LIST_ISEMPTY(&frms)) {
2129 struct quic_frame *frm, *frm2;
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01002130
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01002131 list_for_each_entry_safe(frm, frm2, &frms, list)
2132 qc_frm_free(&frm);
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02002133 }
2134
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02002135 /* Re-insert on-error QCS at the end of the send-list. */
2136 if (!LIST_ISEMPTY(&qcs_failed)) {
2137 list_for_each_entry_safe(qcs, qcs_tmp, &qcs_failed, el_send) {
2138 LIST_DEL_INIT(&qcs->el_send);
2139 LIST_APPEND(&qcc->send_list, &qcs->el_send);
2140 }
2141
2142 if (!(qcc->flags & QC_CF_BLK_MFCTL))
2143 tasklet_wakeup(qcc->wait_event.tasklet);
2144 }
2145
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02002146 out:
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002147 if (qcc->conn->flags & CO_FL_ERROR && !(qcc->flags & QC_CF_ERR_CONN)) {
2148 TRACE_ERROR("error reported by transport layer",
2149 QMUX_EV_QCC_SEND, qcc->conn);
2150 qcc->flags |= QC_CF_ERR_CONN;
2151 }
2152
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002153 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01002154 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002155}
2156
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002157/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
2158 * operation.
2159 *
2160 * Returns 0 on success else non-zero.
2161 */
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002162static int qcc_io_recv(struct qcc *qcc)
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002163{
2164 struct eb64_node *node;
2165 struct qcs *qcs;
2166
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002167 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02002168
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002169 if (qcc->flags & QC_CF_ERRL) {
2170 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002171 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02002172 return 0;
2173 }
2174
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002175 node = eb64_first(&qcc->streams_by_id);
2176 while (node) {
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002177 uint64_t id;
2178
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002179 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002180 id = qcs->id;
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002181
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002182 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002183 node = eb64_next(node);
2184 continue;
2185 }
2186
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002187 if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002188 node = eb64_next(node);
2189 continue;
2190 }
2191
2192 qcc_decode_qcs(qcc, qcs);
2193 node = eb64_next(node);
2194 }
2195
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002196 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002197 return 0;
2198}
2199
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002200
2201/* Release all streams which have their transfer operation achieved.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01002202 *
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002203 * Returns true if at least one stream is released.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01002204 */
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002205static int qcc_purge_streams(struct qcc *qcc)
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002206{
2207 struct eb64_node *node;
2208 int release = 0;
2209
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002210 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002211
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002212 node = eb64_first(&qcc->streams_by_id);
2213 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02002214 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002215 node = eb64_next(node);
2216
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002217 /* Release not attached closed streams. */
2218 if (qcs->st == QC_SS_CLO && !qcs_sc(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002219 TRACE_STATE("purging closed stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002220 qcs_destroy(qcs);
2221 release = 1;
2222 continue;
2223 }
2224
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002225 /* Release detached streams with empty buffer. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002226 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02002227 if (qcs_is_close_local(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002228 TRACE_STATE("purging detached stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002229 qcs_destroy(qcs);
2230 release = 1;
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002231 continue;
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002232 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002233 }
2234 }
2235
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002236 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002237 return release;
2238}
2239
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002240/* Execute application layer shutdown. If this operation is not defined, a
2241 * CONNECTION_CLOSE will be prepared as a fallback. This function is protected
2242 * against multiple invocation with the flag QC_CF_APP_SHUT.
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002243 */
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002244static void qcc_shutdown(struct qcc *qcc)
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002245{
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002246 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002247
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002248 if (qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL)) {
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002249 TRACE_DATA("connection on error", QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002250 goto out;
Amaury Denoyelle665817a2023-03-20 17:34:22 +01002251 }
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002252
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002253 if (qcc->flags & QC_CF_APP_SHUT)
2254 goto out;
2255
2256 TRACE_STATE("perform graceful shutdown", QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02002257 if (qcc->app_ops && qcc->app_ops->shutdown) {
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02002258 qcc->app_ops->shutdown(qcc->ctx);
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002259 qcc_io_send(qcc);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002260 }
2261 else {
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002262 qcc->err = quic_err_app(QC_ERR_NO_ERROR);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002263 }
2264
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002265 /* Register "no error" code at transport layer. Do not use
2266 * quic_set_connection_close() as retransmission may be performed to
2267 * finalized transfers. Do not overwrite quic-conn existing code if
2268 * already set.
2269 *
2270 * TODO implement a wrapper function for this in quic-conn module
2271 */
2272 if (!(qcc->conn->handle.qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
2273 qcc->conn->handle.qc->err = qcc->err;
2274
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002275 out:
2276 qcc->flags |= QC_CF_APP_SHUT;
2277 TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn);
2278}
2279
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002280/* Loop through all qcs from <qcc>. Report error on stream endpoint if
2281 * connection on error and wake them.
2282 */
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002283static int qcc_wake_some_streams(struct qcc *qcc)
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002284{
2285 struct qcs *qcs;
2286 struct eb64_node *node;
2287
2288 TRACE_POINT(QMUX_EV_QCC_WAKE, qcc->conn);
2289
2290 for (node = eb64_first(&qcc->streams_by_id); node;
2291 node = eb64_next(node)) {
2292 qcs = eb64_entry(node, struct qcs, by_id);
2293
2294 if (!qcs_sc(qcs))
2295 continue;
2296
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002297 if (qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL)) {
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002298 TRACE_POINT(QMUX_EV_QCC_WAKE, qcc->conn, qcs);
2299 se_fl_set_error(qcs->sd);
2300 qcs_alert(qcs);
2301 }
2302 }
2303
2304 return 0;
2305}
2306
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002307/* Conduct operations which should be made for <qcc> connection after
2308 * input/output. Most notably, closed streams are purged which may leave the
2309 * connection has ready to be released.
2310 *
2311 * Returns 1 if <qcc> must be released else 0.
2312 */
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002313static int qcc_io_process(struct qcc *qcc)
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002314{
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002315 qcc_purge_streams(qcc);
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002316
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +01002317 /* Check if a soft-stop is in progress.
2318 *
2319 * TODO this is relevant for frontend connections only.
2320 */
2321 if (unlikely(qcc->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
2322 int close = 1;
2323
2324 /* If using listener socket, soft-stop is not supported. The
2325 * connection must be closed immediately.
2326 */
2327 if (!qc_test_fd(qcc->conn->handle.qc)) {
2328 TRACE_DEVEL("proxy disabled with listener socket, closing connection", QMUX_EV_QCC_WAKE, qcc->conn);
2329 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002330 qcc_io_send(qcc);
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +01002331 goto out;
2332 }
2333
2334 TRACE_DEVEL("proxy disabled, prepare connection soft-stop", QMUX_EV_QCC_WAKE, qcc->conn);
2335
2336 /* If a close-spread-time option is set, we want to avoid
2337 * closing all the active HTTP3 connections at once so we add a
2338 * random factor that will spread the closing.
2339 */
2340 if (tick_isset(global.close_spread_end)) {
2341 int remaining_window = tick_remain(now_ms, global.close_spread_end);
2342 if (remaining_window) {
2343 /* This should increase the closing rate the
2344 * further along the window we are. */
2345 close = (remaining_window <= statistical_prng_range(global.close_spread_time));
2346 }
2347 }
2348 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE) {
2349 close = 0; /* let the client close his connection himself */
2350 }
2351
2352 if (close)
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002353 qcc_shutdown(qcc);
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +01002354 }
2355
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002356 /* Report error if set on stream endpoint layer. */
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002357 if (qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL))
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002358 qcc_wake_some_streams(qcc);
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002359
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +01002360 out:
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002361 if (qcc_is_dead(qcc))
2362 return 1;
2363
2364 return 0;
2365}
2366
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002367/* release function. This one should be called to free all resources allocated
2368 * to the mux.
2369 */
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002370static void qcc_release(struct qcc *qcc)
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002371{
2372 struct connection *conn = qcc->conn;
2373 struct eb64_node *node;
2374
2375 TRACE_ENTER(QMUX_EV_QCC_END, conn);
2376
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002377 qcc_shutdown(qcc);
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002378
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002379 if (qcc->task) {
2380 task_destroy(qcc->task);
2381 qcc->task = NULL;
2382 }
2383
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +02002384 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002385 if (conn && qcc->wait_event.events) {
2386 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
2387 qcc->wait_event.events,
2388 &qcc->wait_event);
2389 }
2390
2391 /* liberate remaining qcs instances */
2392 node = eb64_first(&qcc->streams_by_id);
2393 while (node) {
2394 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
2395 node = eb64_next(node);
2396 qcs_free(qcs);
2397 }
2398
2399 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
2400 struct quic_frame *frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01002401 qc_frm_free(&frm);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002402 }
2403
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02002404 if (qcc->app_ops && qcc->app_ops->release)
2405 qcc->app_ops->release(qcc->ctx);
2406 TRACE_PROTO("application layer released", QMUX_EV_QCC_END, conn);
2407
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002408 pool_free(pool_head_qcc, qcc);
2409
2410 if (conn) {
2411 LIST_DEL_INIT(&conn->stopping_list);
2412
2413 conn->handle.qc->conn = NULL;
2414 conn->mux = NULL;
2415 conn->ctx = NULL;
2416
2417 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
2418
2419 conn_stop_tracking(conn);
2420 conn_full_close(conn);
2421 if (conn->destroy_cb)
2422 conn->destroy_cb(conn);
2423 conn_free(conn);
2424 }
2425
2426 TRACE_LEAVE(QMUX_EV_QCC_END);
2427}
2428
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002429struct task *qcc_io_cb(struct task *t, void *ctx, unsigned int status)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002430{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02002431 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002432
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002433 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002434
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002435 qcc_io_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002436
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002437 qcc_io_recv(qcc);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002438
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002439 if (qcc_io_process(qcc)) {
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002440 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
2441 goto release;
2442 }
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002443
2444 qcc_refresh_timeout(qcc);
2445
Amaury Denoyelled3973852022-07-25 14:56:54 +02002446 end:
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002447 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
2448 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002449
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002450 release:
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002451 qcc_release(qcc);
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002452 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002453 return NULL;
2454}
2455
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002456static struct task *qcc_timeout_task(struct task *t, void *ctx, unsigned int state)
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002457{
2458 struct qcc *qcc = ctx;
2459 int expired = tick_is_expired(t->expire, now_ms);
2460
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002461 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002462
2463 if (qcc) {
2464 if (!expired) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002465 TRACE_DEVEL("not expired", QMUX_EV_QCC_WAKE, qcc->conn);
2466 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002467 }
2468
2469 if (!qcc_may_expire(qcc)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002470 TRACE_DEVEL("cannot expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002471 t->expire = TICK_ETERNITY;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002472 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002473 }
2474 }
2475
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002476 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01002477
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002478 if (!qcc) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002479 TRACE_DEVEL("no more qcc", QMUX_EV_QCC_WAKE);
2480 goto out;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002481 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01002482
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002483 qcc->task = NULL;
2484
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002485 /* TODO depending on the timeout condition, different shutdown mode
2486 * should be used. For http keep-alive or disabled proxy, a graceful
2487 * shutdown should occurs. For all other cases, an immediate close
2488 * seems legitimate.
2489 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002490 if (qcc_is_dead(qcc)) {
2491 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002492 qcc_release(qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002493 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002494
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002495 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002496 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002497 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002498
2499 requeue:
2500 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
2501 return t;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002502}
2503
Amaury Denoyelle0f61e4f2023-05-30 14:51:57 +02002504static int qmux_init(struct connection *conn, struct proxy *prx,
2505 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002506{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002507 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002508 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002509
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002510 TRACE_ENTER(QMUX_EV_QCC_NEW);
2511
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002512 qcc = pool_alloc(pool_head_qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002513 if (!qcc) {
2514 TRACE_ERROR("alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002515 goto fail_no_qcc;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002516 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002517
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002518 qcc->conn = conn;
2519 conn->ctx = qcc;
Amaury Denoyellec603de42022-07-25 11:21:46 +02002520 qcc->nb_hreq = qcc->nb_sc = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01002521 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002522
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002523 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002524
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002525 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002526
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002527 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02002528 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002529
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02002530 qcc->tx.sent_offsets = qcc->tx.offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002531
Amaury Denoyellec985cb12022-05-16 14:29:59 +02002532 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01002533 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02002534 qcc->lfctl.ms_uni = lparams->initial_max_streams_uni;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02002535 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
2536 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle176174f2022-10-21 17:02:18 +02002537 qcc->lfctl.msd_uni_r = lparams->initial_max_stream_data_uni;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01002538 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01002539
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02002540 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02002541 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02002542
Willy Tarreau784b8682022-04-11 14:18:10 +02002543 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01002544 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002545 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
2546 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle176174f2022-10-21 17:02:18 +02002547 qcc->rfctl.msd_uni_l = rparams->initial_max_stream_data_uni;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002548
Amaury Denoyellea509ffb2022-07-04 15:50:33 +02002549 if (conn_is_back(conn)) {
2550 qcc->next_bidi_l = 0x00;
2551 qcc->largest_bidi_r = 0x01;
2552 qcc->next_uni_l = 0x02;
2553 qcc->largest_uni_r = 0x03;
2554 }
2555 else {
2556 qcc->largest_bidi_r = 0x00;
2557 qcc->next_bidi_l = 0x01;
2558 qcc->largest_uni_r = 0x02;
2559 qcc->next_uni_l = 0x03;
2560 }
2561
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002562 qcc->wait_event.tasklet = tasklet_new();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002563 if (!qcc->wait_event.tasklet) {
2564 TRACE_ERROR("taslket alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002565 goto fail_no_tasklet;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002566 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002567
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01002568 LIST_INIT(&qcc->send_list);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02002569
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002570 qcc->wait_event.tasklet->process = qcc_io_cb;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002571 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01002572 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002573
Amaury Denoyelle07bf8f42022-07-22 16:16:03 +02002574 qcc->proxy = prx;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002575 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002576 qcc->task = NULL;
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +01002577 if (conn_is_back(qcc->conn)) {
2578 qcc->timeout = prx->timeout.server;
2579 qcc->shut_timeout = tick_isset(prx->timeout.serverfin) ?
2580 prx->timeout.serverfin : prx->timeout.server;
2581 }
2582 else {
2583 qcc->timeout = prx->timeout.client;
2584 qcc->shut_timeout = tick_isset(prx->timeout.clientfin) ?
2585 prx->timeout.clientfin : prx->timeout.client;
2586 }
2587
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002588 if (tick_isset(qcc->timeout)) {
2589 qcc->task = task_new_here();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002590 if (!qcc->task) {
2591 TRACE_ERROR("timeout task alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002592 goto fail_no_timeout_task;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002593 }
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002594 qcc->task->process = qcc_timeout_task;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002595 qcc->task->context = qcc;
2596 qcc->task->expire = tick_add(now_ms, qcc->timeout);
2597 }
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +02002598 qcc_reset_idle_start(qcc);
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02002599 LIST_INIT(&qcc->opening_list);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002600
Willy Tarreau784b8682022-04-11 14:18:10 +02002601 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +01002602
2603 if (qcc_install_app_ops(qcc, conn->handle.qc->app_ops)) {
Amaury Denoyelle8d44bfa2023-05-04 15:16:01 +02002604 TRACE_PROTO("Cannot install app layer", QMUX_EV_QCC_NEW|QMUX_EV_QCC_ERR, qcc->conn);
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +01002605 /* prepare a CONNECTION_CLOSE frame */
2606 quic_set_connection_close(conn->handle.qc, quic_err_transport(QC_ERR_APPLICATION_ERROR));
2607 goto fail_install_app_ops;
2608 }
2609
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01002610 if (qcc->app_ops == &h3_ops)
2611 proxy_inc_fe_cum_sess_ver_ctr(sess->listener, prx, 3);
2612
Amaury Denoyelleed820822023-04-19 17:58:39 +02002613 /* Register conn for idle front closing. This is done once everything is allocated. */
2614 if (!conn_is_back(conn))
2615 LIST_APPEND(&mux_stopping_data[tid].list, &conn->stopping_list);
2616
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002617 /* init read cycle */
2618 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002619
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002620 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002621 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002622
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +01002623 fail_install_app_ops:
2624 if (qcc->app_ops && qcc->app_ops->release)
2625 qcc->app_ops->release(qcc->ctx);
Amaury Denoyelleee65efb2023-05-12 16:29:48 +02002626 task_destroy(qcc->task);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002627 fail_no_timeout_task:
2628 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002629 fail_no_tasklet:
2630 pool_free(pool_head_qcc, qcc);
2631 fail_no_qcc:
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002632 TRACE_LEAVE(QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002633 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002634}
2635
Amaury Denoyelle0f61e4f2023-05-30 14:51:57 +02002636static void qmux_destroy(void *ctx)
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002637{
2638 struct qcc *qcc = ctx;
2639
2640 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002641 qcc_release(qcc);
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002642 TRACE_LEAVE(QMUX_EV_QCC_END);
2643}
2644
Amaury Denoyelle0f61e4f2023-05-30 14:51:57 +02002645static void qmux_strm_detach(struct sedesc *sd)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002646{
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002647 struct qcs *qcs = sd->se;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002648 struct qcc *qcc = qcs->qcc;
2649
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002650 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002651
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002652 /* TODO this BUG_ON_HOT() is not correct as the stconn layer may detach
2653 * from the stream even if it is not closed remotely at the QUIC layer.
2654 * This happens for example when a stream must be closed due to a
2655 * rejected request. To better handle these cases, it will be required
2656 * to implement shutr/shutw MUX operations. Once this is done, this
2657 * BUG_ON_HOT() statement can be adjusted.
2658 */
2659 //BUG_ON_HOT(!qcs_is_close_remote(qcs));
Amaury Denoyellec603de42022-07-25 11:21:46 +02002660
2661 qcc_rm_sc(qcc);
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002662
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002663 if (!qcs_is_close_local(qcs) &&
2664 !(qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL))) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002665 TRACE_STATE("remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002666 qcs->flags |= QC_SF_DETACH;
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002667 qcc_refresh_timeout(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002668
2669 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002670 return;
2671 }
2672
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002673 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01002674
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002675 if (qcc_is_dead(qcc)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002676 TRACE_STATE("killing dead connection", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002677 goto release;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002678 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002679 else if (qcc->task) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002680 TRACE_DEVEL("refreshing connection's timeout", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002681 qcc_refresh_timeout(qcc);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002682 }
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002683 else {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002684 TRACE_DEVEL("completed", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002685 }
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002686
2687 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002688 return;
2689
2690 release:
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002691 qcc_release(qcc);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002692 TRACE_LEAVE(QMUX_EV_STRM_END);
2693 return;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002694}
2695
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002696/* Called from the upper layer, to receive data */
Amaury Denoyelle0f61e4f2023-05-30 14:51:57 +02002697static size_t qmux_strm_rcv_buf(struct stconn *sc, struct buffer *buf,
2698 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002699{
Willy Tarreau3215e732022-05-27 10:09:11 +02002700 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle16494692023-05-15 11:35:45 +02002701 struct qcc *qcc = qcs->qcc;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002702 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002703 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002704
Amaury Denoyelle16494692023-05-15 11:35:45 +02002705 TRACE_ENTER(QMUX_EV_STRM_RECV, qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002706
Amaury Denoyelled80fbca2022-09-19 17:02:28 +02002707 ret = qcs_http_rcv_buf(qcs, buf, count, &fin);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002708
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002709 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002710 se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002711 }
2712 else {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002713 se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002714
Amaury Denoyellebf86d892023-05-12 18:16:31 +02002715 /* Set end-of-input when full message properly received. */
Christopher Faulet85eabfb2023-02-23 14:52:09 +01002716 if (fin) {
Amaury Denoyelle16494692023-05-15 11:35:45 +02002717 TRACE_STATE("report end-of-input", QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellebfddb422023-05-25 15:02:24 +02002718 se_fl_set(qcs->sd, SE_FL_EOI);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002719
Christopher Faulet85eabfb2023-02-23 14:52:09 +01002720 /* If request EOM is reported to the upper layer, it means the
2721 * QCS now expects data from the opposite side.
2722 */
2723 se_expect_data(qcs->sd);
2724 }
2725
Amaury Denoyelle3cb78142023-05-15 11:31:20 +02002726 /* Set end-of-stream on read closed. */
2727 if (qcs->flags & QC_SF_RECV_RESET ||
2728 qcc->conn->flags & CO_FL_SOCK_RD_SH) {
2729 TRACE_STATE("report end-of-stream", QMUX_EV_STRM_RECV, qcc->conn, qcs);
2730 se_fl_set(qcs->sd, SE_FL_EOS);
2731
2732 /* Set error if EOI not reached. This may happen on
2733 * RESET_STREAM reception or connection error.
2734 */
2735 if (!se_fl_test(qcs->sd, SE_FL_EOI)) {
2736 TRACE_STATE("report error on stream aborted", QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellebfddb422023-05-25 15:02:24 +02002737 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyelle3cb78142023-05-15 11:31:20 +02002738 }
2739 }
2740
Amaury Denoyelle16494692023-05-15 11:35:45 +02002741 if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING)) {
2742 TRACE_STATE("report error", QMUX_EV_STRM_RECV, qcc->conn, qcs);
2743 se_fl_set(qcs->sd, SE_FL_ERROR);
2744 }
2745
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002746 if (b_size(&qcs->rx.app_buf)) {
2747 b_free(&qcs->rx.app_buf);
2748 offer_buffers(NULL, 1);
2749 }
2750 }
2751
Amaury Denoyelleb8901d22023-05-03 15:30:04 +02002752 /* Restart demux if it was interrupted on full buffer. */
2753 if (ret && qcs->flags & QC_SF_DEM_FULL) {
Amaury Denoyelle217b0f42023-09-21 17:06:16 +02002754 /* Ensure DEM_FULL is only set if there is available data to
2755 * ensure we never do unnecessary wakeup here.
Amaury Denoyelleb8901d22023-05-03 15:30:04 +02002756 */
2757 BUG_ON(!ncb_data(&qcs->rx.ncbuf, 0));
2758
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002759 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle16494692023-05-15 11:35:45 +02002760 if (!(qcc->flags & QC_CF_ERRL))
2761 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002762 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002763
Amaury Denoyelle16494692023-05-15 11:35:45 +02002764 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002765
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002766 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002767}
2768
Amaury Denoyelle0f61e4f2023-05-30 14:51:57 +02002769static size_t qmux_strm_snd_buf(struct stconn *sc, struct buffer *buf,
2770 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002771{
Willy Tarreau3215e732022-05-27 10:09:11 +02002772 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002773 size_t ret = 0;
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002774 char fin;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002775
2776 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002777
Amaury Denoyelle3dc4e5a2022-09-13 16:49:21 +02002778 /* stream layer has been detached so no transfer must occur after. */
2779 BUG_ON_HOT(qcs->flags & QC_SF_DETACH);
2780
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002781 /* Report error if set on stream endpoint layer. */
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002782 if (qcs->qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL)) {
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002783 se_fl_set(qcs->sd, SE_FL_ERROR);
2784 TRACE_DEVEL("connection in error", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
2785 goto end;
2786 }
2787
Amaury Denoyelle843a1192022-07-04 11:44:38 +02002788 if (qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_RESET)) {
Amaury Denoyelle0ed617a2022-09-20 14:46:40 +02002789 ret = qcs_http_reset_buf(qcs, buf, count);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002790 goto end;
2791 }
2792
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002793 ret = qcs_http_snd_buf(qcs, buf, count, &fin);
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002794 if (fin) {
2795 TRACE_STATE("reached stream fin", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002796 qcs->flags |= QC_SF_FIN_STREAM;
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002797 }
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002798
Amaury Denoyelleab6cdec2023-01-10 10:41:41 +01002799 if (ret || fin) {
Amaury Denoyellef9b03262023-01-09 10:34:25 +01002800 qcc_send_stream(qcs, 0);
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002801 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
2802 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
2803 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002804
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002805 end:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002806 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
2807
2808 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002809}
2810
2811/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2812 * event subscriber <es> is not allowed to change from a previous call as long
2813 * as at least one event is still subscribed. The <event_type> must only be a
2814 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
2815 */
Amaury Denoyelle0f61e4f2023-05-30 14:51:57 +02002816static int qmux_strm_subscribe(struct stconn *sc, int event_type,
2817 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002818{
Willy Tarreau3215e732022-05-27 10:09:11 +02002819 return qcs_subscribe(__sc_mux_strm(sc), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002820}
2821
2822/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
2823 * The <es> pointer is not allowed to differ from the one passed to the
2824 * subscribe() call. It always returns zero.
2825 */
Amaury Denoyelle0f61e4f2023-05-30 14:51:57 +02002826static int qmux_strm_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002827{
Willy Tarreau3215e732022-05-27 10:09:11 +02002828 struct qcs *qcs = __sc_mux_strm(sc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002829
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002830 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
2831 BUG_ON(qcs->subs && qcs->subs != es);
2832
2833 es->events &= ~event_type;
2834 if (!es->events)
2835 qcs->subs = NULL;
2836
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002837 return 0;
2838}
2839
Amaury Denoyelle0f61e4f2023-05-30 14:51:57 +02002840static int qmux_wake(struct connection *conn)
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002841{
2842 struct qcc *qcc = conn->ctx;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002843
2844 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002845
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002846 if (qcc_io_process(qcc)) {
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002847 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002848 goto release;
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002849 }
2850
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002851 qcc_wake_some_streams(qcc);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002852
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002853 qcc_refresh_timeout(qcc);
2854
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002855 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002856 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002857
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002858 release:
Amaury Denoyelled68f8b52023-05-30 15:04:46 +02002859 qcc_release(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002860 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002861 return 1;
2862}
2863
Amaury Denoyelle0f61e4f2023-05-30 14:51:57 +02002864static void qmux_strm_shutw(struct stconn *sc, enum co_shw_mode mode)
Amaury Denoyellea473f192022-12-21 10:21:58 +01002865{
2866 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002867 struct qcc *qcc = qcs->qcc;
Amaury Denoyellea473f192022-12-21 10:21:58 +01002868
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002869 TRACE_ENTER(QMUX_EV_STRM_SHUT, qcc->conn, qcs);
Amaury Denoyellea473f192022-12-21 10:21:58 +01002870
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002871 /* Early closure reported if QC_SF_FIN_STREAM not yet set. */
Amaury Denoyellea473f192022-12-21 10:21:58 +01002872 if (!qcs_is_close_local(qcs) &&
2873 !(qcs->flags & (QC_SF_FIN_STREAM|QC_SF_TO_RESET))) {
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002874
2875 if (qcs->flags & QC_SF_UNKNOWN_PL_LENGTH) {
2876 /* Close stream with a FIN STREAM frame. */
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002877 if (!(qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL))) {
Amaury Denoyelle3fd40932023-05-10 10:41:47 +02002878 TRACE_STATE("set FIN STREAM",
2879 QMUX_EV_STRM_SHUT, qcc->conn, qcs);
2880 qcs->flags |= QC_SF_FIN_STREAM;
2881 qcc_send_stream(qcs, 0);
2882 }
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002883 }
2884 else {
2885 /* RESET_STREAM necessary. */
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002886 if (!(qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL)))
Amaury Denoyelle3fd40932023-05-10 10:41:47 +02002887 qcc_reset_stream(qcs, 0);
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002888 se_fl_set_error(qcs->sd);
2889 }
2890
2891 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyellea473f192022-12-21 10:21:58 +01002892 }
2893
Amaury Denoyelled4af0412023-05-03 18:17:19 +02002894 out:
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002895 TRACE_LEAVE(QMUX_EV_STRM_SHUT, qcc->conn, qcs);
Amaury Denoyellea473f192022-12-21 10:21:58 +01002896}
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002897
Willy Tarreaub4a4fee2022-09-02 16:00:40 +02002898/* for debugging with CLI's "show sess" command. May emit multiple lines, each
2899 * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single
2900 * line is used. Each field starts with a space so it's safe to print it after
2901 * existing fields.
2902 */
Amaury Denoyelle0f61e4f2023-05-30 14:51:57 +02002903static int qmux_strm_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx)
Willy Tarreaub4a4fee2022-09-02 16:00:40 +02002904{
2905 struct qcs *qcs = sd->se;
2906 struct qcc *qcc;
2907 int ret = 0;
2908
2909 if (!qcs)
2910 return ret;
2911
2912 chunk_appendf(msg, " qcs=%p .flg=%#x .id=%llu .st=%s .ctx=%p, .err=%#llx",
2913 qcs, qcs->flags, (ull)qcs->id, qcs_st_to_str(qcs->st), qcs->ctx, (ull)qcs->err);
2914
2915 if (pfx)
2916 chunk_appendf(msg, "\n%s", pfx);
2917
2918 qcc = qcs->qcc;
2919 chunk_appendf(msg, " qcc=%p .flg=%#x .nbsc=%llu .nbhreq=%llu, .task=%p",
2920 qcc, qcc->flags, (ull)qcc->nb_sc, (ull)qcc->nb_hreq, qcc->task);
2921 return ret;
2922}
2923
2924
Amaury Denoyelle0f61e4f2023-05-30 14:51:57 +02002925static const struct mux_ops qmux_ops = {
2926 .init = qmux_init,
2927 .destroy = qmux_destroy,
2928 .detach = qmux_strm_detach,
2929 .rcv_buf = qmux_strm_rcv_buf,
2930 .snd_buf = qmux_strm_snd_buf,
2931 .subscribe = qmux_strm_subscribe,
2932 .unsubscribe = qmux_strm_unsubscribe,
2933 .wake = qmux_wake,
2934 .shutw = qmux_strm_shutw,
2935 .show_sd = qmux_strm_show_sd,
Willy Tarreaub5821e12022-04-26 11:54:08 +02002936 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02002937 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002938};
2939
2940static struct mux_proto_list mux_proto_quic =
Amaury Denoyelle0f61e4f2023-05-30 14:51:57 +02002941 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qmux_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002942
2943INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);