blob: 6746fc87eebcd1f613a861e243e4adb3bef8509c [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 Denoyelledd4fbfb2022-03-24 16:09:16 +010022#include <haproxy/trace.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010023
Amaury Denoyelledeed7772021-12-03 11:36:46 +010024DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010025DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
26
Amaury Denoyelle4b167002022-12-12 09:59:50 +010027static void qc_free_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
28{
29 struct buffer buf;
30
31 if (ncb_is_null(ncbuf))
32 return;
33
34 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
35 b_free(&buf);
36 offer_buffers(NULL, 1);
37
38 *ncbuf = NCBUF_NULL;
39}
40
41/* Free <qcs> instance. This function is reserved for internal usage : it must
42 * only be called on qcs alloc error or on connection shutdown. Else
Ilya Shipitsin07be66d2023-04-01 12:26:42 +020043 * qcs_destroy must be preferred to handle QUIC flow-control increase.
Amaury Denoyelle4b167002022-12-12 09:59:50 +010044 */
45static void qcs_free(struct qcs *qcs)
46{
47 struct qcc *qcc = qcs->qcc;
48
49 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn, qcs);
50
Amaury Denoyelle15337fd2022-12-20 14:47:16 +010051 /* Safe to use even if already removed from the list. */
52 LIST_DEL_INIT(&qcs->el_opening);
Amaury Denoyelle20f2a422023-01-03 14:39:24 +010053 LIST_DEL_INIT(&qcs->el_send);
Amaury Denoyelle4b167002022-12-12 09:59:50 +010054
55 /* Release stream endpoint descriptor. */
56 BUG_ON(qcs->sd && !se_fl_test(qcs->sd, SE_FL_ORPHAN));
57 sedesc_free(qcs->sd);
58
59 /* Release app-layer context. */
60 if (qcs->ctx && qcc->app_ops->detach)
61 qcc->app_ops->detach(qcs);
62
63 /* Release qc_stream_desc buffer from quic-conn layer. */
64 qc_stream_desc_release(qcs->stream);
65
66 /* Free Rx/Tx buffers. */
67 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
68 b_free(&qcs->tx.buf);
69
70 BUG_ON(!qcc->strms[qcs_id_type(qcs->id)].nb_streams);
71 --qcc->strms[qcs_id_type(qcs->id)].nb_streams;
72
73 /* Remove qcs from qcc tree. */
74 eb64_delete(&qcs->by_id);
75
76 pool_free(pool_head_qcs, qcs);
77
78 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
79}
80
Amaury Denoyelledeed7772021-12-03 11:36:46 +010081/* Allocate a new QUIC streams with id <id> and type <type>. */
Amaury Denoyellea509ffb2022-07-04 15:50:33 +020082static struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +010083{
Amaury Denoyelledeed7772021-12-03 11:36:46 +010084 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010085
Amaury Denoyelle4f137572022-03-24 17:10:00 +010086 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
87
Amaury Denoyelledeed7772021-12-03 11:36:46 +010088 qcs = pool_alloc(pool_head_qcs);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +020089 if (!qcs) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +020090 TRACE_ERROR("alloc failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle17014a62022-04-27 15:09:27 +020091 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +020092 }
Amaury Denoyelle17014a62022-04-27 15:09:27 +020093
94 qcs->stream = NULL;
95 qcs->qcc = qcc;
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +020096 qcs->sd = NULL;
Amaury Denoyelle17014a62022-04-27 15:09:27 +020097 qcs->flags = QC_SF_NONE;
Amaury Denoyelle38e60062022-07-01 16:48:42 +020098 qcs->st = QC_SS_IDLE;
Amaury Denoyelle47447af2022-04-27 15:17:11 +020099 qcs->ctx = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100100
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200101 /* App callback attach may register the stream for http-request wait.
102 * These fields must be initialed before.
103 */
104 LIST_INIT(&qcs->el_opening);
Amaury Denoyelle20f2a422023-01-03 14:39:24 +0100105 LIST_INIT(&qcs->el_send);
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200106 qcs->start = TICK_ETERNITY;
107
Amaury Denoyelle4b167002022-12-12 09:59:50 +0100108 /* store transport layer stream descriptor in qcc tree */
109 qcs->id = qcs->by_id.key = id;
110 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
111
112 qcc->strms[type].nb_streams++;
113
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200114 /* Allocate transport layer stream descriptor. Only needed for TX. */
115 if (!quic_stream_is_uni(id) || !quic_stream_is_remote(qcc, id)) {
116 struct quic_conn *qc = qcc->conn->handle.qc;
117 qcs->stream = qc_stream_desc_new(id, type, qcs, qc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200118 if (!qcs->stream) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200119 TRACE_ERROR("qc_stream_desc alloc failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200120 goto err;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200121 }
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200122 }
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200123
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100124 /* If stream is local, use peer remote-limit, or else the opposite. */
Amaury Denoyelle176174f2022-10-21 17:02:18 +0200125 if (quic_stream_is_bidi(id)) {
126 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
127 qcc->rfctl.msd_bidi_l;
128 }
129 else if (quic_stream_is_local(qcc, id)) {
130 qcs->tx.msd = qcc->rfctl.msd_uni_l;
131 }
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100132
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200133 qcs->rx.ncbuf = NCBUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100134 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200135 qcs->rx.offset = qcs->rx.offset_max = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100136
Amaury Denoyelle176174f2022-10-21 17:02:18 +0200137 if (quic_stream_is_bidi(id)) {
138 qcs->rx.msd = quic_stream_is_local(qcc, id) ? qcc->lfctl.msd_bidi_l :
139 qcc->lfctl.msd_bidi_r;
140 }
141 else if (quic_stream_is_remote(qcc, id)) {
142 qcs->rx.msd = qcc->lfctl.msd_uni_r;
143 }
Amaury Denoyellea9773552022-05-16 14:38:25 +0200144 qcs->rx.msd_init = qcs->rx.msd;
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200145
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100146 qcs->tx.buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100147 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100148 qcs->tx.sent_offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100149
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100150 qcs->wait_event.tasklet = NULL;
151 qcs->wait_event.events = 0;
152 qcs->subs = NULL;
153
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200154 qcs->err = 0;
155
Amaury Denoyelle3d550842023-01-24 17:42:21 +0100156 if (qcc->app_ops->attach && qcc->app_ops->attach(qcs, qcc->ctx)) {
157 TRACE_ERROR("app proto failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
158 goto err;
159 }
160
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100161 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100162 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100163 return qcs;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200164
165 err:
Amaury Denoyelle4b167002022-12-12 09:59:50 +0100166 qcs_free(qcs);
167 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200168 return NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100169}
170
Amaury Denoyelle3abeb572022-07-04 11:42:27 +0200171static forceinline struct stconn *qcs_sc(const struct qcs *qcs)
172{
173 return qcs->sd ? qcs->sd->sc : NULL;
174}
175
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200176/* Reset the <qcc> inactivity timeout for http-keep-alive timeout. */
177static forceinline void qcc_reset_idle_start(struct qcc *qcc)
178{
179 qcc->idle_start = now_ms;
180}
181
Amaury Denoyellec603de42022-07-25 11:21:46 +0200182/* Decrement <qcc> sc. */
183static forceinline void qcc_rm_sc(struct qcc *qcc)
184{
185 BUG_ON_HOT(!qcc->nb_sc);
186 --qcc->nb_sc;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200187
188 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
189 * refreshed after this on stream detach.
190 */
191 if (!qcc->nb_sc && !qcc->nb_hreq)
192 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200193}
194
195/* Decrement <qcc> hreq. */
196static forceinline void qcc_rm_hreq(struct qcc *qcc)
197{
198 BUG_ON_HOT(!qcc->nb_hreq);
199 --qcc->nb_hreq;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200200
201 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
202 * refreshed after this on I/O handler.
203 */
204 if (!qcc->nb_sc && !qcc->nb_hreq)
205 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200206}
207
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200208static inline int qcc_is_dead(const struct qcc *qcc)
209{
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200210 /* Maintain connection if stream endpoints are still active. */
211 if (qcc->nb_sc)
212 return 0;
213
214 /* Connection considered dead if either :
215 * - remote error detected at tranport level
216 * - error detected locally
217 * - MUX timeout expired or unset
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200218 */
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200219 if (qcc->conn->flags & CO_FL_ERROR || qcc->flags & QC_CF_ERRL_DONE ||
220 !qcc->task) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200221 return 1;
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200222 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200223
224 return 0;
225}
226
227/* Return true if the mux timeout should be armed. */
228static inline int qcc_may_expire(struct qcc *qcc)
229{
230 return !qcc->nb_sc;
231}
232
233/* Refresh the timeout on <qcc> if needed depending on its state. */
234static void qcc_refresh_timeout(struct qcc *qcc)
235{
236 const struct proxy *px = qcc->proxy;
237
238 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
239
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200240 if (!qcc->task) {
241 TRACE_DEVEL("already expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200242 goto leave;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200243 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200244
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200245 /* Check if upper layer is responsible of timeout management. */
246 if (!qcc_may_expire(qcc)) {
247 TRACE_DEVEL("not eligible for timeout", QMUX_EV_QCC_WAKE, qcc->conn);
248 qcc->task->expire = TICK_ETERNITY;
249 task_queue(qcc->task);
250 goto leave;
251 }
252
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200253 /* Frontend timeout management
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +0100254 * - shutdown done -> timeout client-fin
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200255 * - detached streams with data left to send -> default timeout
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200256 * - stream waiting on incomplete request or no stream yet activated -> timeout http-request
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200257 * - idle after stream processing -> timeout http-keep-alive
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +0100258 *
259 * If proxy stop-stop in progress, immediate or spread close will be
260 * processed if shutdown already one or connection is idle.
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200261 */
262 if (!conn_is_back(qcc->conn)) {
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +0100263 if (qcc->nb_hreq && !(qcc->flags & QC_CF_APP_SHUT)) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200264 TRACE_DEVEL("one or more requests still in progress", QMUX_EV_QCC_WAKE, qcc->conn);
265 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200266 task_queue(qcc->task);
267 goto leave;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200268 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200269
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +0100270 if ((!LIST_ISEMPTY(&qcc->opening_list) || unlikely(!qcc->largest_bidi_r)) &&
271 !(qcc->flags & QC_CF_APP_SHUT)) {
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200272 int timeout = px->timeout.httpreq;
273 struct qcs *qcs = NULL;
274 int base_time;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200275
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200276 /* Use start time of first stream waiting on HTTP or
277 * qcc idle if no stream not yet used.
278 */
279 if (likely(!LIST_ISEMPTY(&qcc->opening_list)))
280 qcs = LIST_ELEM(qcc->opening_list.n, struct qcs *, el_opening);
281 base_time = qcs ? qcs->start : qcc->idle_start;
282
283 TRACE_DEVEL("waiting on http request", QMUX_EV_QCC_WAKE, qcc->conn, qcs);
284 qcc->task->expire = tick_add_ifset(base_time, timeout);
285 }
286 else {
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +0100287 if (qcc->flags & QC_CF_APP_SHUT) {
288 TRACE_DEVEL("connection in closing", QMUX_EV_QCC_WAKE, qcc->conn);
289 qcc->task->expire = tick_add_ifset(now_ms,
290 qcc->shut_timeout);
291 }
292 else {
293 /* Use http-request timeout if keep-alive timeout not set */
294 int timeout = tick_isset(px->timeout.httpka) ?
295 px->timeout.httpka : px->timeout.httpreq;
296 TRACE_DEVEL("at least one request achieved but none currently in progress", QMUX_EV_QCC_WAKE, qcc->conn);
297 qcc->task->expire = tick_add_ifset(qcc->idle_start, timeout);
298 }
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +0100299
300 /* If proxy soft-stop in progress and connection is
301 * inactive, close the connection immediately. If a
302 * close-spread-time is configured, randomly spread the
303 * timer over a closing window.
304 */
305 if ((qcc->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) &&
306 !(global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)) {
307
308 /* Wake timeout task immediately if window already expired. */
309 int remaining_window = tick_isset(global.close_spread_end) ?
310 tick_remain(now_ms, global.close_spread_end) : 0;
311
312 TRACE_DEVEL("proxy disabled, prepare connection soft-stop", QMUX_EV_QCC_WAKE, qcc->conn);
313 if (remaining_window) {
314 /* We don't need to reset the expire if it would
315 * already happen before the close window end.
316 */
317 if (!tick_isset(qcc->task->expire) ||
318 tick_is_le(global.close_spread_end, qcc->task->expire)) {
319 /* Set an expire value shorter than the current value
320 * because the close spread window end comes earlier.
321 */
322 qcc->task->expire = tick_add(now_ms,
323 statistical_prng_range(remaining_window));
324 }
325 }
326 else {
327 /* We are past the soft close window end, wake the timeout
328 * task up immediately.
329 */
330 qcc->task->expire = now_ms;
331 task_wakeup(qcc->task, TASK_WOKEN_TIMER);
332 }
333 }
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200334 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200335 }
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200336
337 /* fallback to default timeout if frontend specific undefined or for
338 * backend connections.
339 */
340 if (!tick_isset(qcc->task->expire)) {
341 TRACE_DEVEL("fallback to default timeout", QMUX_EV_QCC_WAKE, qcc->conn);
342 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200343 }
344
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200345 task_queue(qcc->task);
346
347 leave:
348 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
349}
350
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200351/* Mark a stream as open if it was idle. This can be used on every
352 * successful emission/reception operation to update the stream state.
353 */
354static void qcs_idle_open(struct qcs *qcs)
355{
356 /* This operation must not be used if the stream is already closed. */
357 BUG_ON_HOT(qcs->st == QC_SS_CLO);
358
359 if (qcs->st == QC_SS_IDLE) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200360 TRACE_STATE("opening stream", QMUX_EV_QCS_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200361 qcs->st = QC_SS_OPEN;
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200362 }
363}
364
365/* Close the local channel of <qcs> instance. */
366static void qcs_close_local(struct qcs *qcs)
367{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200368 TRACE_STATE("closing stream locally", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
369
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200370 /* The stream must have already been opened. */
371 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
372
373 /* This operation cannot be used multiple times. */
374 BUG_ON_HOT(qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO);
375
376 if (quic_stream_is_bidi(qcs->id)) {
377 qcs->st = (qcs->st == QC_SS_HREM) ? QC_SS_CLO : QC_SS_HLOC;
Amaury Denoyelleafb7b9d2022-09-19 11:58:24 +0200378
379 if (qcs->flags & QC_SF_HREQ_RECV)
380 qcc_rm_hreq(qcs->qcc);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200381 }
382 else {
383 /* Only local uni streams are valid for this operation. */
384 BUG_ON_HOT(quic_stream_is_remote(qcs->qcc, qcs->id));
385 qcs->st = QC_SS_CLO;
386 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200387}
388
389/* Close the remote channel of <qcs> instance. */
390static void qcs_close_remote(struct qcs *qcs)
391{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200392 TRACE_STATE("closing stream remotely", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
393
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200394 /* The stream must have already been opened. */
395 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
396
397 /* This operation cannot be used multiple times. */
398 BUG_ON_HOT(qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO);
399
400 if (quic_stream_is_bidi(qcs->id)) {
401 qcs->st = (qcs->st == QC_SS_HLOC) ? QC_SS_CLO : QC_SS_HREM;
402 }
403 else {
404 /* Only remote uni streams are valid for this operation. */
405 BUG_ON_HOT(quic_stream_is_local(qcs->qcc, qcs->id));
406 qcs->st = QC_SS_CLO;
407 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200408}
409
410static int qcs_is_close_local(struct qcs *qcs)
411{
412 return qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO;
413}
414
Amaury Denoyelle6eb3c4b2022-12-09 16:26:03 +0100415static int qcs_is_close_remote(struct qcs *qcs)
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200416{
417 return qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO;
418}
419
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100420struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100421{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100422 struct buffer *buf = b_alloc(bptr);
423 BUG_ON(!buf);
424 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100425}
426
Amaury Denoyellea441ec92022-07-04 15:48:57 +0200427static struct ncbuf *qc_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200428{
429 struct buffer buf = BUF_NULL;
430
431 if (ncb_is_null(ncbuf)) {
432 b_alloc(&buf);
433 BUG_ON(b_is_null(&buf));
434
435 *ncbuf = ncb_make(buf.area, buf.size, 0);
436 ncb_init(ncbuf, 0);
437 }
438
439 return ncbuf;
440}
441
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500442/* Notify an eventual subscriber on <qcs> or else wakeup up the stconn layer if
Amaury Denoyelle4561f842022-07-06 14:54:34 +0200443 * initialized.
444 */
445static void qcs_alert(struct qcs *qcs)
446{
447 if (qcs->subs) {
448 qcs_notify_recv(qcs);
449 qcs_notify_send(qcs);
450 }
451 else if (qcs_sc(qcs) && qcs->sd->sc->app_ops->wake) {
452 qcs->sd->sc->app_ops->wake(qcs->sd->sc);
453 }
454}
455
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100456int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
457{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100458 struct qcc *qcc = qcs->qcc;
459
460 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100461
462 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
463 BUG_ON(qcs->subs && qcs->subs != es);
464
465 es->events |= event_type;
466 qcs->subs = es;
467
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100468 if (event_type & SUB_RETRY_RECV)
469 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
470
471 if (event_type & SUB_RETRY_SEND)
472 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
473
474 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
475
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100476 return 0;
477}
478
479void qcs_notify_recv(struct qcs *qcs)
480{
481 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
482 tasklet_wakeup(qcs->subs->tasklet);
483 qcs->subs->events &= ~SUB_RETRY_RECV;
484 if (!qcs->subs->events)
485 qcs->subs = NULL;
486 }
487}
488
489void qcs_notify_send(struct qcs *qcs)
490{
491 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
492 tasklet_wakeup(qcs->subs->tasklet);
493 qcs->subs->events &= ~SUB_RETRY_SEND;
494 if (!qcs->subs->events)
495 qcs->subs = NULL;
496 }
497}
498
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200499/* A fatal error is detected locally for <qcc> connection. It should be closed
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200500 * with a CONNECTION_CLOSE using <err> code. Set <app> to true to indicate that
501 * the code must be considered as an application level error. This function
502 * must not be called more than once by connection.
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200503 */
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200504void qcc_set_error(struct qcc *qcc, int err, int app)
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200505{
506 /* This must not be called multiple times per connection. */
507 BUG_ON(qcc->flags & QC_CF_ERRL);
508
509 TRACE_STATE("connection on error", QMUX_EV_QCC_ERR, qcc->conn);
510
511 qcc->flags |= QC_CF_ERRL;
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200512 qcc->err = app ? quic_err_app(err) : quic_err_transport(err);
Amaury Denoyelleda24bcf2023-05-09 18:20:45 +0200513
514 /* TODO
515 * Ensure qc_send() will be conducted to convert QC_CF_ERRL in
516 * QC_CF_ERRL_DONE with CONNECTION_CLOSE frame emission. This may be
517 * unnecessary if we are currently in the MUX tasklet context, but it
518 * is too tedious too not forget a wakeup outside of this function for
519 * the moment.
520 */
521 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200522}
523
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200524/* Open a locally initiated stream for the connection <qcc>. Set <bidi> for a
525 * bidirectional stream, else an unidirectional stream is opened. The next
526 * available ID on the connection will be used according to the stream type.
527 *
528 * Returns the allocated stream instance or NULL on error.
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100529 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200530struct qcs *qcc_init_stream_local(struct qcc *qcc, int bidi)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100531{
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200532 struct qcs *qcs;
533 enum qcs_type type;
534 uint64_t *next;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100535
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200536 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
537
538 if (bidi) {
539 next = &qcc->next_bidi_l;
540 type = conn_is_back(qcc->conn) ? QCS_CLT_BIDI : QCS_SRV_BIDI;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100541 }
542 else {
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200543 next = &qcc->next_uni_l;
544 type = conn_is_back(qcc->conn) ? QCS_CLT_UNI : QCS_SRV_UNI;
545 }
546
547 /* TODO ensure that we won't overflow remote peer flow control limit on
548 * streams. Else, we should emit a STREAMS_BLOCKED frame.
549 */
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100550
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200551 qcs = qcs_new(qcc, *next, type);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200552 if (!qcs) {
553 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200554 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200555 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200556 }
Amaury Denoyellec055e302022-02-07 16:09:06 +0100557
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200558 TRACE_PROTO("opening local stream", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200559 *next += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100560
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200561 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200562 return qcs;
563}
564
565/* Open a remote initiated stream for the connection <qcc> with ID <id>. The
566 * caller is responsible to ensure that a stream with the same ID was not
567 * already opened. This function will also create all intermediaries streams
568 * with ID smaller than <id> not already opened before.
569 *
570 * Returns the allocated stream instance or NULL on error.
571 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200572static struct qcs *qcc_init_stream_remote(struct qcc *qcc, uint64_t id)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200573{
574 struct qcs *qcs = NULL;
575 enum qcs_type type;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200576 uint64_t *largest, max_id;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100577
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200578 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200579
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200580 BUG_ON_HOT(quic_stream_is_local(qcc, id));
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100581
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200582 if (quic_stream_is_bidi(id)) {
583 largest = &qcc->largest_bidi_r;
584 type = conn_is_back(qcc->conn) ? QCS_SRV_BIDI : QCS_CLT_BIDI;
585 }
586 else {
587 largest = &qcc->largest_uni_r;
588 type = conn_is_back(qcc->conn) ? QCS_SRV_UNI : QCS_CLT_UNI;
589 }
590
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200591 /* RFC 9000 4.6. Controlling Concurrency
592 *
593 * An endpoint that receives a frame with a stream ID exceeding the
594 * limit it has sent MUST treat this as a connection error of type
595 * STREAM_LIMIT_ERROR
596 */
597 max_id = quic_stream_is_bidi(id) ? qcc->lfctl.ms_bidi * 4 :
598 qcc->lfctl.ms_uni * 4;
599 if (id >= max_id) {
600 TRACE_ERROR("flow control error", QMUX_EV_QCS_NEW|QMUX_EV_PROTO_ERR, qcc->conn);
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200601 qcc_set_error(qcc, QC_ERR_STREAM_LIMIT_ERROR, 0);
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200602 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200603 }
604
605 /* Only stream ID not already opened can be used. */
606 BUG_ON(id < *largest);
607
608 while (id >= *largest) {
Amaury Denoyellefd79ddb2022-08-16 11:13:45 +0200609 const char *str = *largest < id ? "initializing intermediary remote stream" :
610 "initializing remote stream";
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200611
612 qcs = qcs_new(qcc, *largest, type);
613 if (!qcs) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200614 TRACE_ERROR("stream fallocation failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200615 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200616 goto err;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100617 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200618
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200619 TRACE_PROTO(str, QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200620 *largest += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100621 }
622
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200623 out:
624 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle50742292022-03-29 14:57:19 +0200625 return qcs;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200626
627 err:
628 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
629 return NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200630}
631
632/* Use this function for a stream <id> which is not in <qcc> stream tree. It
633 * returns true if the associated stream is closed.
634 */
635static int qcc_stream_id_is_closed(struct qcc *qcc, uint64_t id)
636{
637 uint64_t *largest;
638
639 /* This function must only be used for stream not present in the stream tree. */
640 BUG_ON_HOT(eb64_lookup(&qcc->streams_by_id, id));
641
642 if (quic_stream_is_local(qcc, id)) {
643 largest = quic_stream_is_uni(id) ? &qcc->next_uni_l :
644 &qcc->next_bidi_l;
645 }
646 else {
647 largest = quic_stream_is_uni(id) ? &qcc->largest_uni_r :
648 &qcc->largest_bidi_r;
649 }
650
651 return id < *largest;
652}
653
654/* Retrieve the stream instance from <id> ID. This can be used when receiving
655 * STREAM, STREAM_DATA_BLOCKED, RESET_STREAM, MAX_STREAM_DATA or STOP_SENDING
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200656 * frames. Set to false <receive_only> or <send_only> if these particular types
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200657 * of streams are not allowed. If the stream instance is found, it is stored in
658 * <out>.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200659 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200660 * Returns 0 on success else non-zero. On error, a RESET_STREAM or a
661 * CONNECTION_CLOSE is automatically emitted. Beware that <out> may be NULL
662 * on success if the stream has already been closed.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200663 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200664int qcc_get_qcs(struct qcc *qcc, uint64_t id, int receive_only, int send_only,
665 struct qcs **out)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200666{
667 struct eb64_node *node;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200668
669 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200670 *out = NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200671
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200672 if (!receive_only && quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200673 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 +0200674 qcc_set_error(qcc, QC_ERR_STREAM_STATE_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200675 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200676 }
677
678 if (!send_only && quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200679 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 +0200680 qcc_set_error(qcc, QC_ERR_STREAM_STATE_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200681 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200682 }
683
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200684 /* Search the stream in the connection tree. */
685 node = eb64_lookup(&qcc->streams_by_id, id);
686 if (node) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200687 *out = eb64_entry(node, struct qcs, by_id);
688 TRACE_DEVEL("using stream from connection tree", QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200689 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200690 }
691
692 /* Check if stream is already closed. */
693 if (qcc_stream_id_is_closed(qcc, id)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200694 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200695 /* Consider this as a success even if <out> is left NULL. */
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200696 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200697 }
698
699 /* Create the stream. This is valid only for remote initiated one. A
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500700 * local stream must have already been explicitly created by the
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200701 * application protocol layer.
702 */
703 if (quic_stream_is_local(qcc, id)) {
704 /* RFC 9000 19.8. STREAM Frames
705 *
706 * An endpoint MUST terminate the connection with error
707 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
708 * initiated stream that has not yet been created, or for a send-only
709 * stream.
710 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200711 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 +0200712 qcc_set_error(qcc, QC_ERR_STREAM_STATE_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200713 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200714 }
715 else {
716 /* Remote stream not found - try to open it. */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200717 *out = qcc_init_stream_remote(qcc, id);
718 if (!*out) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200719 TRACE_ERROR("stream creation error", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200720 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200721 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200722 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100723
724 out:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200725 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200726 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200727
728 err:
729 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
730 return 1;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100731}
732
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200733/* Simple function to duplicate a buffer */
734static inline struct buffer qcs_b_dup(const struct ncbuf *b)
735{
736 return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0));
737}
738
Amaury Denoyelle36d4b5e2022-07-01 11:25:40 +0200739/* Remove <bytes> from <qcs> Rx buffer. Flow-control for received offsets may
740 * be allocated for the peer if needed.
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200741 */
742static void qcs_consume(struct qcs *qcs, uint64_t bytes)
743{
744 struct qcc *qcc = qcs->qcc;
745 struct quic_frame *frm;
746 struct ncbuf *buf = &qcs->rx.ncbuf;
747 enum ncb_ret ret;
748
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200749 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
750
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200751 ret = ncb_advance(buf, bytes);
752 if (ret) {
753 ABORT_NOW(); /* should not happens because removal only in data */
754 }
755
756 if (ncb_is_empty(buf))
757 qc_free_ncbuf(qcs, buf);
758
759 qcs->rx.offset += bytes;
Amaury Denoyellebb6296c2022-12-09 15:00:17 +0100760 /* Not necessary to emit a MAX_STREAM_DATA if all data received. */
761 if (qcs->flags & QC_SF_SIZE_KNOWN)
762 goto conn_fctl;
763
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200764 if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200765 TRACE_DATA("increase stream credit via MAX_STREAM_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +0100766 frm = qc_frm_alloc(QUIC_FT_MAX_STREAM_DATA);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +0100767 if (!frm) {
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200768 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +0100769 return;
770 }
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200771
772 qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init;
773
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200774 frm->max_stream_data.id = qcs->id;
775 frm->max_stream_data.max_stream_data = qcs->rx.msd;
776
777 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
778 tasklet_wakeup(qcc->wait_event.tasklet);
779 }
780
Amaury Denoyellebb6296c2022-12-09 15:00:17 +0100781 conn_fctl:
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200782 qcc->lfctl.offsets_consume += bytes;
783 if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200784 TRACE_DATA("increase conn credit via MAX_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +0100785 frm = qc_frm_alloc(QUIC_FT_MAX_DATA);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +0100786 if (!frm) {
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200787 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +0100788 return;
789 }
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200790
791 qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init;
792
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200793 frm->max_data.max_data = qcc->lfctl.md;
794
795 LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list);
796 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
797 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200798
799 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200800}
801
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200802/* Decode the content of STREAM frames already received on the stream instance
803 * <qcs>.
804 *
805 * Returns 0 on success else non-zero.
806 */
807static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
808{
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200809 struct buffer b;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200810 ssize_t ret;
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200811 int fin = 0;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200812
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200813 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
814
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200815 b = qcs_b_dup(&qcs->rx.ncbuf);
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200816
Amaury Denoyelled1310f82022-09-16 13:30:59 +0200817 /* Signal FIN to application if STREAM FIN received with all data. */
818 if (qcs_is_close_remote(qcs))
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200819 fin = 1;
820
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100821 if (!(qcs->flags & QC_SF_READ_ABORTED)) {
822 ret = qcc->app_ops->decode_qcs(qcs, &b, fin);
823 if (ret < 0) {
824 TRACE_ERROR("decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
825 goto err;
826 }
827 }
828 else {
829 TRACE_DATA("ignore read on stream", QMUX_EV_QCS_RECV, qcc->conn, qcs);
830 ret = b_data(&b);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200831 }
832
Amaury Denoyelle381d8132023-02-17 09:51:20 +0100833 if (ret)
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200834 qcs_consume(qcs, ret);
Amaury Denoyelle381d8132023-02-17 09:51:20 +0100835 if (ret || (!b_data(&b) && fin))
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200836 qcs_notify_recv(qcs);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200837
838 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200839 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200840
841 err:
842 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
843 return 1;
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200844}
845
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200846/* Prepare for the emission of RESET_STREAM on <qcs> with error code <err>. */
847void qcc_reset_stream(struct qcs *qcs, int err)
848{
849 struct qcc *qcc = qcs->qcc;
850
851 if ((qcs->flags & QC_SF_TO_RESET) || qcs_is_close_local(qcs))
852 return;
853
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200854 TRACE_STATE("reset stream", QMUX_EV_QCS_END, qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200855 qcs->flags |= QC_SF_TO_RESET;
856 qcs->err = err;
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +0100857
Amaury Denoyelle178fbff2023-03-22 11:17:59 +0100858 /* Remove prepared stream data from connection flow-control calcul. */
859 if (qcs->tx.offset > qcs->tx.sent_offset) {
860 const uint64_t diff = qcs->tx.offset - qcs->tx.sent_offset;
861 BUG_ON(qcc->tx.offsets - diff < qcc->tx.sent_offsets);
862 qcc->tx.offsets -= diff;
863 /* Reset qcs offset to prevent BUG_ON() on qcs_destroy(). */
864 qcs->tx.offset = qcs->tx.sent_offset;
865 }
866
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +0100867 qcc_send_stream(qcs, 1);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200868 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100869}
870
Amaury Denoyellef9b03262023-01-09 10:34:25 +0100871/* Register <qcs> stream for emission of STREAM, STOP_SENDING or RESET_STREAM.
872 * Set <urg> to 1 if stream content should be treated in priority compared to
873 * other streams.
874 */
875void qcc_send_stream(struct qcs *qcs, int urg)
Amaury Denoyelle20f2a422023-01-03 14:39:24 +0100876{
877 struct qcc *qcc = qcs->qcc;
878
879 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
880
881 /* Cannot send if already closed. */
882 BUG_ON(qcs_is_close_local(qcs));
883
Amaury Denoyellef9b03262023-01-09 10:34:25 +0100884 if (urg) {
885 LIST_DEL_INIT(&qcs->el_send);
886 LIST_INSERT(&qcc->send_list, &qcs->el_send);
887 }
888 else {
889 if (!LIST_INLIST(&qcs->el_send))
890 LIST_APPEND(&qcs->qcc->send_list, &qcs->el_send);
891 }
Amaury Denoyelle20f2a422023-01-03 14:39:24 +0100892
893 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
894}
895
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100896/* Prepare for the emission of STOP_SENDING on <qcs>. */
897void qcc_abort_stream_read(struct qcs *qcs)
898{
899 struct qcc *qcc = qcs->qcc;
900
901 TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn, qcs);
902
903 if ((qcs->flags & QC_SF_TO_STOP_SENDING) || qcs_is_close_remote(qcs))
904 goto end;
905
906 TRACE_STATE("abort stream read", QMUX_EV_QCS_END, qcc->conn, qcs);
907 qcs->flags |= (QC_SF_TO_STOP_SENDING|QC_SF_READ_ABORTED);
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +0100908
909 qcc_send_stream(qcs, 1);
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100910 tasklet_wakeup(qcc->wait_event.tasklet);
911
912 end:
913 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn, qcs);
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200914}
915
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200916/* Install the <app_ops> applicative layer of a QUIC connection on mux <qcc>.
917 * Returns 0 on success else non-zero.
918 */
919int qcc_install_app_ops(struct qcc *qcc, const struct qcc_app_ops *app_ops)
920{
921 TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn);
922
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +0100923 if (app_ops->init && !app_ops->init(qcc)) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200924 TRACE_ERROR("app ops init error", QMUX_EV_QCC_NEW, qcc->conn);
925 goto err;
926 }
927
928 TRACE_PROTO("application layer initialized", QMUX_EV_QCC_NEW, qcc->conn);
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +0100929 qcc->app_ops = app_ops;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200930
Amaury Denoyelle71fd0362023-01-24 17:35:37 +0100931 /* RFC 9114 7.2.4.2. Initialization
932 *
933 * Endpoints MUST NOT require any data to be
934 * received from the peer prior to sending the SETTINGS frame;
935 * settings MUST be sent as soon as the transport is ready to
936 * send data.
937 */
938 if (qcc->app_ops->finalize) {
939 if (qcc->app_ops->finalize(qcc->ctx)) {
940 TRACE_ERROR("app ops finalize error", QMUX_EV_QCC_NEW, qcc->conn);
941 goto err;
942 }
943 tasklet_wakeup(qcc->wait_event.tasklet);
944 }
945
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200946 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
947 return 0;
948
949 err:
950 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
951 return 1;
952}
953
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200954/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
955 * <data> with length <len> and represents the offset <offset>. <fin> is set if
956 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100957 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200958 * Returns 0 on success else non-zero. On error, the received frame should not
959 * be acknowledged.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100960 */
961int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200962 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100963{
964 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200965 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100966
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100967 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
968
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200969 if (qcc->flags & QC_CF_ERRL) {
970 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200971 goto err;
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200972 }
973
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200974 /* RFC 9000 19.8. STREAM Frames
975 *
976 * An endpoint MUST terminate the connection with error
977 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
978 * initiated stream that has not yet been created, or for a send-only
979 * stream.
980 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200981 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200982 TRACE_DATA("qcs retrieval error", QMUX_EV_QCC_RECV, qcc->conn);
983 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200984 }
985
986 if (!qcs) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200987 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV, qcc->conn);
988 goto out;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200989 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100990
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200991 /* RFC 9000 4.5. Stream Final Size
992 *
993 * Once a final size for a stream is known, it cannot change. If a
994 * RESET_STREAM or STREAM frame is received indicating a change in the
995 * final size for the stream, an endpoint SHOULD respond with an error
996 * of type FINAL_SIZE_ERROR; see Section 11 for details on error
997 * handling.
998 */
999 if (qcs->flags & QC_SF_SIZE_KNOWN &&
1000 (offset + len > qcs->rx.offset_max || (fin && offset + len < qcs->rx.offset_max))) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +02001001 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 +02001002 qcc_set_error(qcc, QC_ERR_FINAL_SIZE_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001003 goto err;
Amaury Denoyellebf91e392022-07-04 10:02:04 +02001004 }
1005
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001006 if (qcs_is_close_remote(qcs)) {
1007 TRACE_DATA("skipping STREAM for remotely closed", QMUX_EV_QCC_RECV, qcc->conn);
1008 goto out;
1009 }
1010
Amaury Denoyellefa241932023-02-14 15:36:36 +01001011 if (offset + len < qcs->rx.offset ||
1012 (offset + len == qcs->rx.offset && (!fin || (qcs->flags & QC_SF_SIZE_KNOWN)))) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001013 TRACE_DATA("already received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1014 goto out;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001015 }
1016
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001017 TRACE_PROTO("receiving STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001018 qcs_idle_open(qcs);
1019
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001020 if (offset + len > qcs->rx.offset_max) {
1021 uint64_t diff = offset + len - qcs->rx.offset_max;
1022 qcs->rx.offset_max = offset + len;
1023 qcc->lfctl.offsets_recv += diff;
1024
1025 if (offset + len > qcs->rx.msd ||
1026 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
1027 /* RFC 9000 4.1. Data Flow Control
1028 *
1029 * A receiver MUST close the connection with an error
1030 * of type FLOW_CONTROL_ERROR if the sender violates
1031 * the advertised connection or stream data limits
1032 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +02001033 TRACE_ERROR("flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001034 qcc->conn, qcs);
Amaury Denoyelle58721f22023-05-09 18:01:09 +02001035 qcc_set_error(qcc, QC_ERR_FLOW_CONTROL_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001036 goto err;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001037 }
1038 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001039
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001040 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001041 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001042 ABORT_NOW();
1043 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001044 }
1045
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001046 TRACE_DATA("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001047 if (offset < qcs->rx.offset) {
Frédéric Lécaillea18c3332022-07-04 09:54:58 +02001048 size_t diff = qcs->rx.offset - offset;
1049
1050 len -= diff;
1051 data += diff;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001052 offset = qcs->rx.offset;
1053 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001054
Amaury Denoyellefa241932023-02-14 15:36:36 +01001055 if (len) {
1056 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
1057 switch (ret) {
1058 case NCB_RET_OK:
1059 break;
1060
1061 case NCB_RET_DATA_REJ:
Amaury Denoyellecc3d7162022-05-20 15:14:57 +02001062 /* RFC 9000 2.2. Sending and Receiving Data
1063 *
1064 * An endpoint could receive data for a stream at the
1065 * same stream offset multiple times. Data that has
1066 * already been received can be discarded. The data at
1067 * a given offset MUST NOT change if it is sent
1068 * multiple times; an endpoint MAY treat receipt of
1069 * different data at the same offset within a stream as
1070 * a connection error of type PROTOCOL_VIOLATION.
1071 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +02001072 TRACE_ERROR("overlapping data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001073 qcc->conn, qcs);
Amaury Denoyelle58721f22023-05-09 18:01:09 +02001074 qcc_set_error(qcc, QC_ERR_PROTOCOL_VIOLATION, 0);
Amaury Denoyellefa241932023-02-14 15:36:36 +01001075 return 1;
1076
1077 case NCB_RET_GAP_SIZE:
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001078 TRACE_DATA("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
1079 qcc->conn, qcs);
Amaury Denoyellefa241932023-02-14 15:36:36 +01001080 return 1;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001081 }
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001082 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001083
1084 if (fin)
Amaury Denoyelle3f39b402022-07-01 16:11:03 +02001085 qcs->flags |= QC_SF_SIZE_KNOWN;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001086
Amaury Denoyelled1310f82022-09-16 13:30:59 +02001087 if (qcs->flags & QC_SF_SIZE_KNOWN &&
1088 qcs->rx.offset_max == qcs->rx.offset + ncb_data(&qcs->rx.ncbuf, 0)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001089 qcs_close_remote(qcs);
Amaury Denoyelled1310f82022-09-16 13:30:59 +02001090 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001091
Amaury Denoyellefa241932023-02-14 15:36:36 +01001092 if ((ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) || fin) {
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001093 qcc_decode_qcs(qcc, qcs);
Amaury Denoyelle418ba212022-08-02 15:57:16 +02001094 qcc_refresh_timeout(qcc);
1095 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001096
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001097 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001098 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001099 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001100
1101 err:
1102 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1103 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001104}
1105
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001106/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
1107 * the frame.
1108 *
1109 * Returns 0 on success else non-zero.
1110 */
1111int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
1112{
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001113 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1114
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001115 TRACE_PROTO("receiving MAX_DATA", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001116 if (qcc->rfctl.md < max) {
1117 qcc->rfctl.md = max;
Amaury Denoyelleb7143a82023-03-22 15:08:01 +01001118 TRACE_DATA("increase remote max-data", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001119
1120 if (qcc->flags & QC_CF_BLK_MFCTL) {
1121 qcc->flags &= ~QC_CF_BLK_MFCTL;
1122 tasklet_wakeup(qcc->wait_event.tasklet);
1123 }
1124 }
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001125
1126 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001127 return 0;
1128}
1129
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001130/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
1131 * field of the frame and <id> is the identifier of the QUIC stream.
1132 *
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001133 * Returns 0 on success else non-zero. On error, the received frame should not
1134 * be acknowledged.
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001135 */
1136int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
1137{
1138 struct qcs *qcs;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001139
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001140 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1141
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001142 if (qcc->flags & QC_CF_ERRL) {
1143 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001144 goto err;
1145 }
1146
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001147 /* RFC 9000 19.10. MAX_STREAM_DATA Frames
1148 *
1149 * Receiving a MAX_STREAM_DATA frame for a locally
1150 * initiated stream that has not yet been created MUST be treated as a
1151 * connection error of type STREAM_STATE_ERROR. An endpoint that
1152 * receives a MAX_STREAM_DATA frame for a receive-only stream MUST
1153 * terminate the connection with error STREAM_STATE_ERROR.
1154 */
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001155 if (qcc_get_qcs(qcc, id, 0, 1, &qcs))
1156 goto err;
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001157
1158 if (qcs) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001159 TRACE_PROTO("receiving MAX_STREAM_DATA", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001160 if (max > qcs->tx.msd) {
1161 qcs->tx.msd = max;
Amaury Denoyelleb7143a82023-03-22 15:08:01 +01001162 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 +01001163
1164 if (qcs->flags & QC_SF_BLK_SFCTL) {
1165 qcs->flags &= ~QC_SF_BLK_SFCTL;
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001166 /* TODO optim: only wakeup IO-CB if stream has data to sent. */
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001167 tasklet_wakeup(qcc->wait_event.tasklet);
1168 }
1169 }
1170 }
1171
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001172 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1173 qcc_refresh_timeout(qcc);
1174
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001175 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1176 return 0;
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001177
1178 err:
1179 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
1180 return 1;
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001181}
1182
1183/* Handle a new RESET_STREAM frame from stream ID <id> with error code <err>
1184 * and final stream size <final_size>.
1185 *
1186 * Returns 0 on success else non-zero. On error, the received frame should not
1187 * be acknowledged.
1188 */
1189int qcc_recv_reset_stream(struct qcc *qcc, uint64_t id, uint64_t err, uint64_t final_size)
1190{
1191 struct qcs *qcs;
1192
1193 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1194
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001195 if (qcc->flags & QC_CF_ERRL) {
1196 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001197 goto err;
1198 }
1199
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001200 /* RFC 9000 19.4. RESET_STREAM Frames
1201 *
1202 * An endpoint that receives a RESET_STREAM frame for a send-only stream
1203 * MUST terminate the connection with error STREAM_STATE_ERROR.
1204 */
1205 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
1206 TRACE_ERROR("RESET_STREAM for send-only stream received", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle58721f22023-05-09 18:01:09 +02001207 qcc_set_error(qcc, QC_ERR_STREAM_STATE_ERROR, 0);
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001208 goto err;
1209 }
1210
1211 if (!qcs || qcs_is_close_remote(qcs))
1212 goto out;
1213
1214 TRACE_PROTO("receiving RESET_STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1215 qcs_idle_open(qcs);
1216
Amaury Denoyellee269aeb2023-01-30 12:13:22 +01001217 if (qcc->app_ops->close) {
1218 if (qcc->app_ops->close(qcs, QCC_APP_OPS_CLOSE_SIDE_RD)) {
1219 TRACE_ERROR("closure rejected by app layer", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1220 goto out;
1221 }
1222 }
1223
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001224 if (qcs->rx.offset_max > final_size ||
1225 ((qcs->flags & QC_SF_SIZE_KNOWN) && qcs->rx.offset_max != final_size)) {
1226 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 +02001227 qcc_set_error(qcc, QC_ERR_FINAL_SIZE_ERROR, 0);
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001228 goto err;
1229 }
1230
1231 qcs->flags |= QC_SF_SIZE_KNOWN;
1232 qcs_close_remote(qcs);
1233 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
1234
1235 if (qcs_sc(qcs)) {
1236 se_fl_set_error(qcs->sd);
1237 qcs_alert(qcs);
1238 }
1239
1240 out:
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001241 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001242 return 0;
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001243
1244 err:
1245 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1246 return 1;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001247}
1248
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001249/* Handle a new STOP_SENDING frame for stream ID <id>. The error code should be
1250 * specified in <err>.
1251 *
1252 * Returns 0 on success else non-zero. On error, the received frame should not
1253 * be acknowledged.
1254 */
1255int qcc_recv_stop_sending(struct qcc *qcc, uint64_t id, uint64_t err)
1256{
1257 struct qcs *qcs;
1258
1259 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1260
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001261 if (qcc->flags & QC_CF_ERRL) {
1262 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001263 goto err;
1264 }
1265
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001266 /* RFC 9000 19.5. STOP_SENDING Frames
1267 *
1268 * Receiving a STOP_SENDING frame for a
1269 * locally initiated stream that has not yet been created MUST be
1270 * treated as a connection error of type STREAM_STATE_ERROR. An
1271 * endpoint that receives a STOP_SENDING frame for a receive-only stream
1272 * MUST terminate the connection with error STREAM_STATE_ERROR.
1273 */
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001274 if (qcc_get_qcs(qcc, id, 0, 1, &qcs))
1275 goto err;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001276
1277 if (!qcs)
1278 goto out;
1279
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001280 TRACE_PROTO("receiving STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelled7755372022-10-03 17:20:31 +02001281
1282 /* RFC 9000 3.5. Solicited State Transitions
1283 *
1284 * An endpoint is expected to send another STOP_SENDING frame if a
1285 * packet containing a previous STOP_SENDING is lost. However, once
1286 * either all stream data or a RESET_STREAM frame has been received for
1287 * the stream -- that is, the stream is in any state other than "Recv"
1288 * or "Size Known" -- sending a STOP_SENDING frame is unnecessary.
1289 */
1290
1291 /* TODO thanks to previous RFC clause, STOP_SENDING is ignored if current stream
1292 * has already been closed locally. This is useful to not emit multiple
1293 * RESET_STREAM for a single stream. This is functional if stream is
1294 * locally closed due to all data transmitted, but in this case the RFC
1295 * advices to use an explicit RESET_STREAM.
1296 */
1297 if (qcs_is_close_local(qcs)) {
1298 TRACE_STATE("ignoring STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1299 goto out;
1300 }
1301
Amaury Denoyelle96ca1b72022-08-09 17:36:38 +02001302 qcs_idle_open(qcs);
1303
Amaury Denoyelle87f87662023-01-30 12:12:43 +01001304 if (qcc->app_ops->close) {
1305 if (qcc->app_ops->close(qcs, QCC_APP_OPS_CLOSE_SIDE_WR)) {
1306 TRACE_ERROR("closure rejected by app layer", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1307 goto out;
1308 }
1309 }
1310
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001311 /* RFC 9000 3.5. Solicited State Transitions
1312 *
1313 * An endpoint that receives a STOP_SENDING frame
1314 * MUST send a RESET_STREAM frame if the stream is in the "Ready" or
1315 * "Send" state. If the stream is in the "Data Sent" state, the
1316 * endpoint MAY defer sending the RESET_STREAM frame until the packets
1317 * containing outstanding data are acknowledged or declared lost. If
1318 * any outstanding data is declared lost, the endpoint SHOULD send a
1319 * RESET_STREAM frame instead of retransmitting the data.
1320 *
1321 * An endpoint SHOULD copy the error code from the STOP_SENDING frame to
1322 * the RESET_STREAM frame it sends, but it can use any application error
1323 * code.
1324 */
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001325 qcc_reset_stream(qcs, err);
1326
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001327 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1328 qcc_refresh_timeout(qcc);
1329
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001330 out:
1331 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1332 return 0;
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001333
1334 err:
1335 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
1336 return 1;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001337}
1338
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001339/* Signal the closing of remote stream with id <id>. Flow-control for new
1340 * streams may be allocated for the peer if needed.
1341 */
1342static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +01001343{
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001344 struct quic_frame *frm;
1345
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001346 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn);
1347
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001348 if (quic_stream_is_bidi(id)) {
1349 ++qcc->lfctl.cl_bidi_r;
1350 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001351 TRACE_DATA("increase max stream limit with MAX_STREAMS_BIDI", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001352 frm = qc_frm_alloc(QUIC_FT_MAX_STREAMS_BIDI);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +01001353 if (!frm) {
Amaury Denoyelle58721f22023-05-09 18:01:09 +02001354 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +01001355 goto err;
1356 }
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001357
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001358 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
1359 qcc->lfctl.cl_bidi_r;
1360 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
1361 tasklet_wakeup(qcc->wait_event.tasklet);
1362
1363 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
1364 qcc->lfctl.cl_bidi_r = 0;
1365 }
1366 }
1367 else {
Amaury Denoyelle91077312022-12-22 18:56:09 +01001368 /* TODO unidirectional stream flow control with MAX_STREAMS_UNI
1369 * emission not implemented. It should be unnecessary for
1370 * HTTP/3 but may be required if other application protocols
1371 * are supported.
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02001372 */
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001373 }
1374
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001375 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
1376
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001377 return 0;
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +01001378
1379 err:
1380 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_END, qcc->conn);
1381 return 1;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001382}
1383
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05001384/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001385static void qcs_destroy(struct qcs *qcs)
1386{
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001387 struct qcc *qcc = qcs->qcc;
1388 struct connection *conn = qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001389 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001390
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001391 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001392
Amaury Denoyelle178fbff2023-03-22 11:17:59 +01001393 /* MUST not removed a stream with sending prepared data left. This is
1394 * to ensure consistency on connection flow-control calculation.
1395 */
1396 BUG_ON(qcs->tx.offset < qcs->tx.sent_offset);
1397
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001398 if (!(qcc->flags & QC_CF_ERRL)) {
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001399 if (quic_stream_is_remote(qcc, id))
1400 qcc_release_remote_stream(qcc, id);
1401 }
Amaury Denoyellec055e302022-02-07 16:09:06 +01001402
Amaury Denoyelledccbd732022-03-29 18:36:59 +02001403 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001404
1405 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001406}
1407
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001408/* Transfer as much as possible data on <qcs> from <in> to <out>. This is done
1409 * in respect with available flow-control at stream and connection level.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001410 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001411 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001412 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001413static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, struct buffer *in)
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001414{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001415 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001416 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001417 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001418
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001419 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001420
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001421 qc_get_buf(qcs, out);
1422
1423 /*
1424 * QCS out buffer diagram
1425 * head left to_xfer
1426 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001427 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001428 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001429 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001430 * ^ ack-off ^ sent-off ^ off
1431 *
1432 * STREAM frame
1433 * ^ ^
1434 * |xxxxxxxxxxxxxxxxx|
1435 */
1436
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001437 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001438 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001439 BUG_ON_HOT(qcc->tx.offsets < qcc->tx.sent_offsets);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001440
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001441 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001442 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001443
1444 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
1445 /* do not exceed flow control limit */
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001446 if (qcs->tx.offset + to_xfer > qcs->tx.msd) {
1447 TRACE_DATA("do not exceed stream flow control", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001448 to_xfer = qcs->tx.msd - qcs->tx.offset;
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001449 }
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001450
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001451 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001452 /* do not overcome flow control limit on connection */
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001453 if (qcc->tx.offsets + to_xfer > qcc->rfctl.md) {
1454 TRACE_DATA("do not exceed conn flow control", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001455 to_xfer = qcc->rfctl.md - qcc->tx.offsets;
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001456 }
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001457
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001458 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001459 goto out;
1460
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001461 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001462
1463 out:
1464 {
1465 struct qcs_xfer_data_trace_arg arg = {
1466 .prep = b_data(out), .xfer = total,
1467 };
1468 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
1469 qcc->conn, qcs, &arg);
1470 }
1471
1472 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001473}
1474
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001475/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
1476 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001477 * stream frame. If <out> is NULL an empty STREAM frame is built : this may be
1478 * useful if FIN needs to be sent without any data left.
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001479 *
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001480 * Returns the payload length of the STREAM frame or a negative error code.
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001481 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001482static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
1483 struct list *frm_list)
1484{
1485 struct qcc *qcc = qcs->qcc;
1486 struct quic_frame *frm;
1487 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001488 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001489
1490 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1491
Amaury Denoyellea4569202022-04-15 17:29:25 +02001492 /* if ack_offset < buf_offset, it points to an older buffer. */
1493 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
1494 BUG_ON(qcs->tx.sent_offset < base_off);
1495
1496 head = qcs->tx.sent_offset - base_off;
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001497 total = out ? b_data(out) - head : 0;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001498 BUG_ON(total < 0);
1499
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001500 if (!total && !fin) {
1501 /* No need to send anything if total is NULL and no FIN to signal. */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001502 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1503 return 0;
1504 }
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001505 BUG_ON((!total && qcs->tx.sent_offset > qcs->tx.offset) ||
1506 (total && qcs->tx.sent_offset >= qcs->tx.offset));
Amaury Denoyellea4569202022-04-15 17:29:25 +02001507 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001508 BUG_ON(qcc->tx.sent_offsets + total > qcc->rfctl.md);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001509
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001510 TRACE_PROTO("sending STREAM frame", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001511 frm = qc_frm_alloc(QUIC_FT_STREAM_8);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001512 if (!frm) {
1513 TRACE_ERROR("frame alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001514 goto err;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001515 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001516
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001517 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001518 frm->stream.id = qcs->id;
Amaury Denoyelle1dac0182023-02-02 16:45:07 +01001519 frm->stream.offset.key = 0;
Amaury Denoyelleebfafc22023-03-07 18:07:08 +01001520 frm->stream.dup = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001521
Amaury Denoyelle42c5b752023-04-25 16:39:32 +02001522 if (total) {
1523 frm->stream.buf = out;
1524 frm->stream.data = (unsigned char *)b_peek(out, head);
1525 }
1526 else {
1527 /* Empty STREAM frame. */
1528 frm->stream.buf = NULL;
1529 frm->stream.data = NULL;
1530 }
1531
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +01001532 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001533 if (fin)
1534 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001535
1536 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001537 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001538 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001539 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001540
Amaury Denoyelle42c5b752023-04-25 16:39:32 +02001541 /* Always set length bit as we do not know if there is remaining frames
1542 * in the final packet after this STREAM.
1543 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001544 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
1545 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001546
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001547 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001548
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001549 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001550 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001551 struct qcs_build_stream_trace_arg arg = {
1552 .len = frm->stream.len, .fin = fin,
1553 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001554 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001555 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001556 qcc->conn, qcs, &arg);
1557 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001558
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001559 return total;
1560
1561 err:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001562 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001563 return -1;
1564}
1565
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001566/* Check after transferring data from qcs.tx.buf if FIN must be set on the next
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001567 * STREAM frame for <qcs>.
1568 *
1569 * Returns true if FIN must be set else false.
1570 */
1571static int qcs_stream_fin(struct qcs *qcs)
1572{
1573 return qcs->flags & QC_SF_FIN_STREAM && !b_data(&qcs->tx.buf);
1574}
1575
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001576/* Return true if <qcs> has data to send in new STREAM frames. */
1577static forceinline int qcs_need_sending(struct qcs *qcs)
1578{
1579 return b_data(&qcs->tx.buf) || qcs->tx.sent_offset < qcs->tx.offset ||
1580 qcs_stream_fin(qcs);
1581}
1582
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001583/* This function must be called by the upper layer to inform about the sending
1584 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
1585 * <offset>.
1586 */
1587void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
1588{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001589 struct qcc *qcc = qcs->qcc;
1590 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001591
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001592 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1593
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001594 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001595 BUG_ON(offset + data > qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001596
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001597 /* check if the STREAM frame has already been notified. It can happen
1598 * for retransmission.
1599 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001600 if (offset + data < qcs->tx.sent_offset) {
1601 TRACE_DEVEL("offset already notified", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1602 goto out;
1603 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001604
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001605 qcs_idle_open(qcs);
1606
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001607 diff = offset + data - qcs->tx.sent_offset;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001608 if (diff) {
1609 /* increase offset sum on connection */
1610 qcc->tx.sent_offsets += diff;
1611 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001612 if (qcc->tx.sent_offsets == qcc->rfctl.md) {
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001613 qcc->flags |= QC_CF_BLK_MFCTL;
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001614 TRACE_STATE("connection flow-control reached", QMUX_EV_QCS_SEND, qcc->conn);
1615 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001616
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001617 /* increase offset on stream */
1618 qcs->tx.sent_offset += diff;
1619 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
1620 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001621 if (qcs->tx.sent_offset == qcs->tx.msd) {
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001622 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001623 TRACE_STATE("stream flow-control reached", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1624 }
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001625
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01001626 /* If qcs.stream.buf is full, release it to the lower layer. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001627 if (qcs->tx.offset == qcs->tx.sent_offset &&
1628 b_full(&qcs->stream->buf->buf)) {
1629 qc_stream_buf_release(qcs->stream);
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001630 }
Amaury Denoyelle1bcb6952023-04-28 16:24:44 +02001631
1632 /* Add measurement for send rate. This is done at the MUX layer
1633 * to account only for STREAM frames without retransmission.
Amaury Denoyelle1bcb6952023-04-28 16:24:44 +02001634 */
Amaury Denoyellebc0adfa2023-04-28 16:46:11 +02001635 increment_send_rate(diff, 0);
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001636 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001637
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001638 if (qcs->tx.offset == qcs->tx.sent_offset && !b_data(&qcs->tx.buf)) {
1639 /* Remove stream from send_list if all was sent. */
1640 LIST_DEL_INIT(&qcs->el_send);
1641 TRACE_STATE("stream sent done", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1642
1643 if (qcs->flags & (QC_SF_FIN_STREAM|QC_SF_DETACH)) {
1644 /* Close stream locally. */
1645 qcs_close_local(qcs);
1646 /* Reset flag to not emit multiple FIN STREAM frames. */
1647 qcs->flags &= ~QC_SF_FIN_STREAM;
1648 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001649 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001650
1651 out:
1652 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001653}
1654
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001655/* Wrapper for send on transport layer. Send a list of frames <frms> for the
1656 * connection <qcc>.
1657 *
1658 * Returns 0 if all data sent with success else non-zero.
1659 */
1660static int qc_send_frames(struct qcc *qcc, struct list *frms)
1661{
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001662 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
1663
1664 if (LIST_ISEMPTY(frms)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001665 TRACE_DEVEL("no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
1666 goto err;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001667 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +01001668
Amaury Denoyellecaa16542023-02-28 15:11:26 +01001669 if (!qc_send_mux(qcc->conn->handle.qc, frms)) {
1670 /* TODO should subscribe only for a transient send error */
1671 TRACE_DEVEL("error on send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
1672 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1673 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyelle036cc5d2022-09-26 15:02:31 +02001674 goto err;
Amaury Denoyellecaa16542023-02-28 15:11:26 +01001675 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001676
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001677 /* If there is frames left at this stage, transport layer is blocked.
1678 * Subscribe on it to retry later.
1679 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001680 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001681 TRACE_DEVEL("remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001682 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1683 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001684 goto err;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001685 }
1686
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001687 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001688 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001689
1690 err:
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001691 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001692 return 1;
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001693}
1694
1695/* Emit a RESET_STREAM on <qcs>.
1696 *
1697 * Returns 0 if the frame has been successfully sent else non-zero.
1698 */
1699static int qcs_send_reset(struct qcs *qcs)
1700{
1701 struct list frms = LIST_HEAD_INIT(frms);
1702 struct quic_frame *frm;
1703
1704 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1705
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001706 frm = qc_frm_alloc(QUIC_FT_RESET_STREAM);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001707 if (!frm) {
1708 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001709 return 1;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001710 }
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001711
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001712 frm->reset_stream.id = qcs->id;
1713 frm->reset_stream.app_error_code = qcs->err;
1714 frm->reset_stream.final_size = qcs->tx.sent_offset;
1715
1716 LIST_APPEND(&frms, &frm->list);
1717 if (qc_send_frames(qcs->qcc, &frms)) {
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001718 qc_frm_free(&frm);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001719 TRACE_DEVEL("cannot send RESET_STREAM", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1720 return 1;
1721 }
1722
1723 if (qcs_sc(qcs)) {
1724 se_fl_set_error(qcs->sd);
1725 qcs_alert(qcs);
1726 }
1727
1728 qcs_close_local(qcs);
1729 qcs->flags &= ~QC_SF_TO_RESET;
1730
1731 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001732 return 0;
1733}
1734
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001735/* Emit a STOP_SENDING on <qcs>.
1736 *
1737 * Returns 0 if the frame has been successfully sent else non-zero.
1738 */
1739static int qcs_send_stop_sending(struct qcs *qcs)
1740{
1741 struct list frms = LIST_HEAD_INIT(frms);
1742 struct quic_frame *frm;
1743 struct qcc *qcc = qcs->qcc;
1744
1745 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1746
1747 /* RFC 9000 3.3. Permitted Frame Types
1748 *
1749 * A
1750 * receiver MAY send a STOP_SENDING frame in any state where it has not
1751 * received a RESET_STREAM frame -- that is, states other than "Reset
1752 * Recvd" or "Reset Read". However, there is little value in sending a
1753 * STOP_SENDING frame in the "Data Recvd" state, as all stream data has
1754 * been received. A sender could receive either of these two types of
1755 * frames in any state as a result of delayed delivery of packets.¶
1756 */
1757 if (qcs_is_close_remote(qcs)) {
1758 TRACE_STATE("skip STOP_SENDING on remote already closed", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1759 goto done;
1760 }
1761
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001762 frm = qc_frm_alloc(QUIC_FT_STOP_SENDING);
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001763 if (!frm) {
1764 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1765 return 1;
1766 }
1767
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001768 frm->stop_sending.id = qcs->id;
1769 frm->stop_sending.app_error_code = qcs->err;
1770
1771 LIST_APPEND(&frms, &frm->list);
1772 if (qc_send_frames(qcs->qcc, &frms)) {
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001773 qc_frm_free(&frm);
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001774 TRACE_DEVEL("cannot send STOP_SENDING", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1775 return 1;
1776 }
1777
1778 done:
1779 qcs->flags &= ~QC_SF_TO_STOP_SENDING;
1780
1781 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1782 return 0;
1783}
1784
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001785/* Used internally by qc_send function. Proceed to send for <qcs>. This will
1786 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001787 * is then generated and inserted in <frms> list.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001788 *
1789 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001790 * be null if out buffer cannot be allocated. On error a negative error code is
1791 * used.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001792 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001793static int _qc_send_qcs(struct qcs *qcs, struct list *frms)
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001794{
1795 struct qcc *qcc = qcs->qcc;
1796 struct buffer *buf = &qcs->tx.buf;
1797 struct buffer *out = qc_stream_buf_get(qcs->stream);
1798 int xfer = 0;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001799 char fin = 0;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001800
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001801 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1802
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001803 /* Cannot send STREAM on remote unidirectional streams. */
1804 BUG_ON(quic_stream_is_uni(qcs->id) && quic_stream_is_remote(qcc, qcs->id));
1805
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001806 if (b_data(buf)) {
1807 /* Allocate <out> buffer if not already done. */
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001808 if (!out) {
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001809 if (qcc->flags & QC_CF_CONN_FULL)
1810 goto out;
1811
1812 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
1813 if (!out) {
1814 TRACE_STATE("cannot allocate stream desc buffer", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1815 qcc->flags |= QC_CF_CONN_FULL;
1816 goto out;
1817 }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001818 }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001819
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001820 /* Transfer data from <buf> to <out>. */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001821 xfer = qcs_xfer_data(qcs, out, buf);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001822 if (xfer > 0) {
1823 qcs_notify_send(qcs);
1824 qcs->flags &= ~QC_SF_BLK_MROOM;
1825 }
1826
1827 qcs->tx.offset += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001828 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001829 qcc->tx.offsets += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001830 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001831
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001832 /* out buffer cannot be emptied if qcs offsets differ. */
1833 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1834 }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001835
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001836 /* FIN is set if all incoming data were transferred. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001837 fin = qcs_stream_fin(qcs);
1838
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001839 /* Build a new STREAM frame with <out> buffer. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001840 if (qcs->tx.sent_offset != qcs->tx.offset || fin) {
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001841 if (qcs_build_stream_frm(qcs, out, fin, frms) < 0)
1842 goto err;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001843 }
1844
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001845 out:
1846 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001847 return xfer;
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001848
1849 err:
1850 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1851 return -1;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001852}
1853
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001854/* Proceed to sending. Loop through all available streams for the <qcc>
1855 * instance and try to send as much as possible.
1856 *
1857 * Returns the total of bytes sent to the transport layer.
1858 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001859static int qc_send(struct qcc *qcc)
1860{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001861 struct list frms = LIST_HEAD_INIT(frms);
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001862 /* Temporary list for QCS on error. */
1863 struct list qcs_failed = LIST_HEAD_INIT(qcs_failed);
Amaury Denoyelle7c5591f2023-04-21 14:48:01 +02001864 struct qcs *qcs, *qcs_tmp, *first_qcs = NULL;
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001865 int ret, total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001866
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001867 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001868
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001869 /* Check for locally detected connection error. */
1870 if (qcc->flags & QC_CF_ERRL) {
1871 /* Prepare a CONNECTION_CLOSE if not already done. */
1872 if (!(qcc->flags & QC_CF_ERRL_DONE)) {
1873 TRACE_DATA("report a connection error", QMUX_EV_QCC_SEND|QMUX_EV_QCC_ERR, qcc->conn);
1874 quic_set_connection_close(qcc->conn->handle.qc, qcc->err);
1875 qcc->flags |= QC_CF_ERRL_DONE;
1876 }
1877 TRACE_DEVEL("connection on error", QMUX_EV_QCC_SEND, qcc->conn);
1878 goto err;
1879 }
1880
1881 if (qcc->conn->flags & CO_FL_SOCK_WR_SH) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001882 qcc->conn->flags |= CO_FL_ERROR;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001883 TRACE_DEVEL("connection on error", QMUX_EV_QCC_SEND, qcc->conn);
1884 goto err;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001885 }
1886
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001887 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1888 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
1889 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
1890 goto out;
1891 }
1892 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02001893
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001894 if (qcc->flags & QC_CF_BLK_MFCTL)
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001895 goto err;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001896
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001897 /* Send STREAM/STOP_SENDING/RESET_STREAM data for registered streams. */
1898 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_list, el_send) {
Amaury Denoyelle7c5591f2023-04-21 14:48:01 +02001899 /* Check if all QCS were processed. */
1900 if (qcs == first_qcs)
1901 break;
1902
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001903 /* Stream must not be present in send_list if it has nothing to send. */
1904 BUG_ON(!(qcs->flags & (QC_SF_TO_STOP_SENDING|QC_SF_TO_RESET)) &&
1905 !qcs_need_sending(qcs));
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001906
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001907 /* Each STOP_SENDING/RESET_STREAM frame is sent individually to
1908 * guarantee its emission.
1909 *
1910 * TODO multiplex several frames in same datagram to optimize sending
1911 */
1912 if (qcs->flags & QC_SF_TO_STOP_SENDING) {
1913 if (qcs_send_stop_sending(qcs))
1914 goto out;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001915
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001916 /* Remove stream from send_list if it had only STOP_SENDING
1917 * to send.
1918 */
1919 if (!(qcs->flags & QC_SF_TO_RESET) && !qcs_need_sending(qcs)) {
1920 LIST_DEL_INIT(&qcs->el_send);
1921 continue;
1922 }
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001923 }
1924
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001925 if (qcs->flags & QC_SF_TO_RESET) {
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001926 if (qcs_send_reset(qcs))
1927 goto out;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001928
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001929 /* RFC 9000 3.3. Permitted Frame Types
1930 *
1931 * A sender MUST NOT send
1932 * a STREAM or STREAM_DATA_BLOCKED frame for a stream in the
1933 * "Reset Sent" state or any terminal state -- that is, after
1934 * sending a RESET_STREAM frame.
1935 */
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001936 LIST_DEL_INIT(&qcs->el_send);
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001937 continue;
1938 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001939
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001940 if (!(qcs->flags & QC_SF_BLK_SFCTL)) {
1941 if ((ret = _qc_send_qcs(qcs, &frms)) < 0) {
1942 /* Temporarily remove QCS from send-list. */
1943 LIST_DEL_INIT(&qcs->el_send);
1944 LIST_APPEND(&qcs_failed, &qcs->el_send);
1945 continue;
1946 }
1947
1948 total += ret;
Amaury Denoyelle7c5591f2023-04-21 14:48:01 +02001949 if (ret) {
1950 /* Move QCS with some bytes transferred at the
1951 * end of send-list for next iterations.
1952 */
1953 LIST_DEL_INIT(&qcs->el_send);
1954 LIST_APPEND(&qcc->send_list, &qcs->el_send);
1955 /* Remember first moved QCS as checkpoint to interrupt loop */
1956 if (!first_qcs)
1957 first_qcs = qcs;
1958 }
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001959 }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001960 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001961
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01001962 /* Retry sending until no frame to send, data rejected or connection
1963 * flow-control limit reached.
1964 */
1965 while (qc_send_frames(qcc, &frms) == 0 && !(qcc->flags & QC_CF_BLK_MFCTL)) {
1966 /* Reloop over <qcc.send_list>. Useful for streams which have
1967 * fulfilled their qc_stream_desc buf and have now release it.
1968 */
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001969 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_list, el_send) {
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01001970 /* Only streams blocked on flow-control or waiting on a
1971 * new qc_stream_desc should be present in send_list as
1972 * long as transport layer can handle all data.
1973 */
1974 BUG_ON(qcs->stream->buf && !(qcs->flags & QC_SF_BLK_SFCTL));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001975
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001976 if (!(qcs->flags & QC_SF_BLK_SFCTL)) {
1977 if ((ret = _qc_send_qcs(qcs, &frms)) < 0) {
1978 LIST_DEL_INIT(&qcs->el_send);
1979 LIST_APPEND(&qcs_failed, &qcs->el_send);
1980 continue;
1981 }
1982
1983 total += ret;
1984 }
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01001985 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001986 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001987
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001988 out:
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02001989 /* Deallocate frames that the transport layer has rejected. */
1990 if (!LIST_ISEMPTY(&frms)) {
1991 struct quic_frame *frm, *frm2;
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001992
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001993 list_for_each_entry_safe(frm, frm2, &frms, list)
1994 qc_frm_free(&frm);
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02001995 }
1996
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001997 /* Re-insert on-error QCS at the end of the send-list. */
1998 if (!LIST_ISEMPTY(&qcs_failed)) {
1999 list_for_each_entry_safe(qcs, qcs_tmp, &qcs_failed, el_send) {
2000 LIST_DEL_INIT(&qcs->el_send);
2001 LIST_APPEND(&qcc->send_list, &qcs->el_send);
2002 }
2003
2004 if (!(qcc->flags & QC_CF_BLK_MFCTL))
2005 tasklet_wakeup(qcc->wait_event.tasklet);
2006 }
2007
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002008 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01002009 return total;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002010
2011 err:
2012 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
2013 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002014}
2015
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002016/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
2017 * operation.
2018 *
2019 * Returns 0 on success else non-zero.
2020 */
2021static int qc_recv(struct qcc *qcc)
2022{
2023 struct eb64_node *node;
2024 struct qcs *qcs;
2025
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002026 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02002027
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002028 if (qcc->flags & QC_CF_ERRL) {
2029 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002030 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02002031 return 0;
2032 }
2033
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002034 node = eb64_first(&qcc->streams_by_id);
2035 while (node) {
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002036 uint64_t id;
2037
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002038 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002039 id = qcs->id;
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002040
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002041 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002042 node = eb64_next(node);
2043 continue;
2044 }
2045
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002046 if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002047 node = eb64_next(node);
2048 continue;
2049 }
2050
2051 qcc_decode_qcs(qcc, qcs);
2052 node = eb64_next(node);
2053 }
2054
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002055 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002056 return 0;
2057}
2058
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002059
2060/* Release all streams which have their transfer operation achieved.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01002061 *
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002062 * Returns true if at least one stream is released.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01002063 */
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002064static int qc_purge_streams(struct qcc *qcc)
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002065{
2066 struct eb64_node *node;
2067 int release = 0;
2068
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002069 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002070
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002071 node = eb64_first(&qcc->streams_by_id);
2072 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02002073 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002074 node = eb64_next(node);
2075
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002076 /* Release not attached closed streams. */
2077 if (qcs->st == QC_SS_CLO && !qcs_sc(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002078 TRACE_STATE("purging closed stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002079 qcs_destroy(qcs);
2080 release = 1;
2081 continue;
2082 }
2083
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002084 /* Release detached streams with empty buffer. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002085 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02002086 if (qcs_is_close_local(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002087 TRACE_STATE("purging detached stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002088 qcs_destroy(qcs);
2089 release = 1;
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002090 continue;
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002091 }
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002092
2093 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
2094 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002095 }
2096 }
2097
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002098 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002099 return release;
2100}
2101
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002102/* Execute application layer shutdown. If this operation is not defined, a
2103 * CONNECTION_CLOSE will be prepared as a fallback. This function is protected
2104 * against multiple invocation with the flag QC_CF_APP_SHUT.
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002105 */
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002106static void qc_shutdown(struct qcc *qcc)
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002107{
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002108 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002109
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002110 if (qcc->flags & QC_CF_ERRL) {
2111 TRACE_DATA("connection on error", QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002112 goto out;
Amaury Denoyelle665817a2023-03-20 17:34:22 +01002113 }
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002114
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002115 if (qcc->flags & QC_CF_APP_SHUT)
2116 goto out;
2117
2118 TRACE_STATE("perform graceful shutdown", QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02002119 if (qcc->app_ops && qcc->app_ops->shutdown) {
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02002120 qcc->app_ops->shutdown(qcc->ctx);
Amaury Denoyellea154dc02022-06-13 17:09:01 +02002121 qc_send(qcc);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002122 }
2123 else {
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002124 qcc->err = quic_err_app(QC_ERR_NO_ERROR);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002125 }
2126
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002127 /* Register "no error" code at transport layer. Do not use
2128 * quic_set_connection_close() as retransmission may be performed to
2129 * finalized transfers. Do not overwrite quic-conn existing code if
2130 * already set.
2131 *
2132 * TODO implement a wrapper function for this in quic-conn module
2133 */
2134 if (!(qcc->conn->handle.qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
2135 qcc->conn->handle.qc->err = qcc->err;
2136
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002137 out:
2138 qcc->flags |= QC_CF_APP_SHUT;
2139 TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn);
2140}
2141
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002142/* Loop through all qcs from <qcc>. Report error on stream endpoint if
2143 * connection on error and wake them.
2144 */
2145static int qc_wake_some_streams(struct qcc *qcc)
2146{
2147 struct qcs *qcs;
2148 struct eb64_node *node;
2149
2150 TRACE_POINT(QMUX_EV_QCC_WAKE, qcc->conn);
2151
2152 for (node = eb64_first(&qcc->streams_by_id); node;
2153 node = eb64_next(node)) {
2154 qcs = eb64_entry(node, struct qcs, by_id);
2155
2156 if (!qcs_sc(qcs))
2157 continue;
2158
2159 if (qcc->conn->flags & CO_FL_ERROR || qcc->flags & QC_CF_ERRL) {
2160 TRACE_POINT(QMUX_EV_QCC_WAKE, qcc->conn, qcs);
2161 se_fl_set_error(qcs->sd);
2162 qcs_alert(qcs);
2163 }
2164 }
2165
2166 return 0;
2167}
2168
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002169/* Conduct operations which should be made for <qcc> connection after
2170 * input/output. Most notably, closed streams are purged which may leave the
2171 * connection has ready to be released.
2172 *
2173 * Returns 1 if <qcc> must be released else 0.
2174 */
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002175static int qc_process(struct qcc *qcc)
2176{
2177 qc_purge_streams(qcc);
2178
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +01002179 /* Check if a soft-stop is in progress.
2180 *
2181 * TODO this is relevant for frontend connections only.
2182 */
2183 if (unlikely(qcc->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
2184 int close = 1;
2185
2186 /* If using listener socket, soft-stop is not supported. The
2187 * connection must be closed immediately.
2188 */
2189 if (!qc_test_fd(qcc->conn->handle.qc)) {
2190 TRACE_DEVEL("proxy disabled with listener socket, closing connection", QMUX_EV_QCC_WAKE, qcc->conn);
2191 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
2192 qc_send(qcc);
2193 goto out;
2194 }
2195
2196 TRACE_DEVEL("proxy disabled, prepare connection soft-stop", QMUX_EV_QCC_WAKE, qcc->conn);
2197
2198 /* If a close-spread-time option is set, we want to avoid
2199 * closing all the active HTTP3 connections at once so we add a
2200 * random factor that will spread the closing.
2201 */
2202 if (tick_isset(global.close_spread_end)) {
2203 int remaining_window = tick_remain(now_ms, global.close_spread_end);
2204 if (remaining_window) {
2205 /* This should increase the closing rate the
2206 * further along the window we are. */
2207 close = (remaining_window <= statistical_prng_range(global.close_spread_time));
2208 }
2209 }
2210 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE) {
2211 close = 0; /* let the client close his connection himself */
2212 }
2213
2214 if (close)
2215 qc_shutdown(qcc);
2216 }
2217
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002218 /* Report error if set on stream endpoint layer. */
2219 if (qcc->flags & QC_CF_ERRL)
2220 qc_wake_some_streams(qcc);
2221
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +01002222 out:
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002223 if (qcc_is_dead(qcc))
2224 return 1;
2225
2226 return 0;
2227}
2228
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002229/* release function. This one should be called to free all resources allocated
2230 * to the mux.
2231 */
2232static void qc_release(struct qcc *qcc)
2233{
2234 struct connection *conn = qcc->conn;
2235 struct eb64_node *node;
2236
2237 TRACE_ENTER(QMUX_EV_QCC_END, conn);
2238
2239 qc_shutdown(qcc);
2240
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002241 if (qcc->task) {
2242 task_destroy(qcc->task);
2243 qcc->task = NULL;
2244 }
2245
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +02002246 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002247 if (conn && qcc->wait_event.events) {
2248 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
2249 qcc->wait_event.events,
2250 &qcc->wait_event);
2251 }
2252
2253 /* liberate remaining qcs instances */
2254 node = eb64_first(&qcc->streams_by_id);
2255 while (node) {
2256 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
2257 node = eb64_next(node);
2258 qcs_free(qcs);
2259 }
2260
2261 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
2262 struct quic_frame *frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01002263 qc_frm_free(&frm);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002264 }
2265
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02002266 if (qcc->app_ops && qcc->app_ops->release)
2267 qcc->app_ops->release(qcc->ctx);
2268 TRACE_PROTO("application layer released", QMUX_EV_QCC_END, conn);
2269
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002270 pool_free(pool_head_qcc, qcc);
2271
2272 if (conn) {
2273 LIST_DEL_INIT(&conn->stopping_list);
2274
2275 conn->handle.qc->conn = NULL;
2276 conn->mux = NULL;
2277 conn->ctx = NULL;
2278
2279 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
2280
2281 conn_stop_tracking(conn);
2282 conn_full_close(conn);
2283 if (conn->destroy_cb)
2284 conn->destroy_cb(conn);
2285 conn_free(conn);
2286 }
2287
2288 TRACE_LEAVE(QMUX_EV_QCC_END);
2289}
2290
Willy Tarreau41e701e2022-09-08 15:12:59 +02002291struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002292{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02002293 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002294
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002295 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002296
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002297 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002298
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002299 qc_recv(qcc);
2300
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002301 if (qc_process(qcc)) {
2302 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
2303 goto release;
2304 }
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002305
2306 qcc_refresh_timeout(qcc);
2307
Amaury Denoyelled3973852022-07-25 14:56:54 +02002308 end:
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002309 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
2310 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002311
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002312 release:
2313 qc_release(qcc);
2314 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002315 return NULL;
2316}
2317
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002318static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
2319{
2320 struct qcc *qcc = ctx;
2321 int expired = tick_is_expired(t->expire, now_ms);
2322
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002323 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002324
2325 if (qcc) {
2326 if (!expired) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002327 TRACE_DEVEL("not expired", QMUX_EV_QCC_WAKE, qcc->conn);
2328 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002329 }
2330
2331 if (!qcc_may_expire(qcc)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002332 TRACE_DEVEL("cannot expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002333 t->expire = TICK_ETERNITY;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002334 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002335 }
2336 }
2337
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002338 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01002339
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002340 if (!qcc) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002341 TRACE_DEVEL("no more qcc", QMUX_EV_QCC_WAKE);
2342 goto out;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002343 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01002344
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002345 qcc->task = NULL;
2346
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002347 /* TODO depending on the timeout condition, different shutdown mode
2348 * should be used. For http keep-alive or disabled proxy, a graceful
2349 * shutdown should occurs. For all other cases, an immediate close
2350 * seems legitimate.
2351 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002352 if (qcc_is_dead(qcc)) {
2353 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002354 qc_release(qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002355 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002356
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002357 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002358 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002359 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002360
2361 requeue:
2362 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
2363 return t;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002364}
2365
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002366static int qc_init(struct connection *conn, struct proxy *prx,
2367 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002368{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002369 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002370 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002371
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002372 TRACE_ENTER(QMUX_EV_QCC_NEW);
2373
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002374 qcc = pool_alloc(pool_head_qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002375 if (!qcc) {
2376 TRACE_ERROR("alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002377 goto fail_no_qcc;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002378 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002379
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002380 qcc->conn = conn;
2381 conn->ctx = qcc;
Amaury Denoyellec603de42022-07-25 11:21:46 +02002382 qcc->nb_hreq = qcc->nb_sc = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01002383 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002384
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002385 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002386
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002387 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002388
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002389 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02002390 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002391
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002392 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02002393 qcc->tx.sent_offsets = qcc->tx.offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002394
2395 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002396 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002397 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002398 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002399 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002400
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002401 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002402 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002403 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002404 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002405
2406 /* Server initiated streams must respect the server flow control. */
2407 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002408 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002409 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002410 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
2411
2412 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002413 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002414 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002415 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002416
Amaury Denoyellec985cb12022-05-16 14:29:59 +02002417 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01002418 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02002419 qcc->lfctl.ms_uni = lparams->initial_max_streams_uni;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02002420 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
2421 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle176174f2022-10-21 17:02:18 +02002422 qcc->lfctl.msd_uni_r = lparams->initial_max_stream_data_uni;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01002423 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01002424
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02002425 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02002426 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02002427
Willy Tarreau784b8682022-04-11 14:18:10 +02002428 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01002429 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002430 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
2431 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle176174f2022-10-21 17:02:18 +02002432 qcc->rfctl.msd_uni_l = rparams->initial_max_stream_data_uni;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002433
Amaury Denoyellea509ffb2022-07-04 15:50:33 +02002434 if (conn_is_back(conn)) {
2435 qcc->next_bidi_l = 0x00;
2436 qcc->largest_bidi_r = 0x01;
2437 qcc->next_uni_l = 0x02;
2438 qcc->largest_uni_r = 0x03;
2439 }
2440 else {
2441 qcc->largest_bidi_r = 0x00;
2442 qcc->next_bidi_l = 0x01;
2443 qcc->largest_uni_r = 0x02;
2444 qcc->next_uni_l = 0x03;
2445 }
2446
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002447 qcc->wait_event.tasklet = tasklet_new();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002448 if (!qcc->wait_event.tasklet) {
2449 TRACE_ERROR("taslket alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002450 goto fail_no_tasklet;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002451 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002452
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01002453 LIST_INIT(&qcc->send_list);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02002454
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002455 qcc->wait_event.tasklet->process = qc_io_cb;
2456 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01002457 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002458
Amaury Denoyelle07bf8f42022-07-22 16:16:03 +02002459 qcc->proxy = prx;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002460 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002461 qcc->task = NULL;
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +01002462 if (conn_is_back(qcc->conn)) {
2463 qcc->timeout = prx->timeout.server;
2464 qcc->shut_timeout = tick_isset(prx->timeout.serverfin) ?
2465 prx->timeout.serverfin : prx->timeout.server;
2466 }
2467 else {
2468 qcc->timeout = prx->timeout.client;
2469 qcc->shut_timeout = tick_isset(prx->timeout.clientfin) ?
2470 prx->timeout.clientfin : prx->timeout.client;
2471 }
2472
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002473 if (tick_isset(qcc->timeout)) {
2474 qcc->task = task_new_here();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002475 if (!qcc->task) {
2476 TRACE_ERROR("timeout task alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002477 goto fail_no_timeout_task;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002478 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002479 qcc->task->process = qc_timeout_task;
2480 qcc->task->context = qcc;
2481 qcc->task->expire = tick_add(now_ms, qcc->timeout);
2482 }
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +02002483 qcc_reset_idle_start(qcc);
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02002484 LIST_INIT(&qcc->opening_list);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002485
Willy Tarreau784b8682022-04-11 14:18:10 +02002486 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +01002487
2488 if (qcc_install_app_ops(qcc, conn->handle.qc->app_ops)) {
Amaury Denoyelle8d44bfa2023-05-04 15:16:01 +02002489 TRACE_PROTO("Cannot install app layer", QMUX_EV_QCC_NEW|QMUX_EV_QCC_ERR, qcc->conn);
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +01002490 /* prepare a CONNECTION_CLOSE frame */
2491 quic_set_connection_close(conn->handle.qc, quic_err_transport(QC_ERR_APPLICATION_ERROR));
2492 goto fail_install_app_ops;
2493 }
2494
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01002495 if (qcc->app_ops == &h3_ops)
2496 proxy_inc_fe_cum_sess_ver_ctr(sess->listener, prx, 3);
2497
Amaury Denoyelleed820822023-04-19 17:58:39 +02002498 /* Register conn for idle front closing. This is done once everything is allocated. */
2499 if (!conn_is_back(conn))
2500 LIST_APPEND(&mux_stopping_data[tid].list, &conn->stopping_list);
2501
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002502 /* init read cycle */
2503 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002504
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002505 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002506 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002507
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +01002508 fail_install_app_ops:
2509 if (qcc->app_ops && qcc->app_ops->release)
2510 qcc->app_ops->release(qcc->ctx);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002511 fail_no_timeout_task:
2512 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002513 fail_no_tasklet:
2514 pool_free(pool_head_qcc, qcc);
2515 fail_no_qcc:
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002516 TRACE_LEAVE(QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002517 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002518}
2519
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002520static void qc_destroy(void *ctx)
2521{
2522 struct qcc *qcc = ctx;
2523
2524 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
2525 qc_release(qcc);
2526 TRACE_LEAVE(QMUX_EV_QCC_END);
2527}
2528
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002529static void qc_detach(struct sedesc *sd)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002530{
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002531 struct qcs *qcs = sd->se;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002532 struct qcc *qcc = qcs->qcc;
2533
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002534 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002535
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002536 /* TODO this BUG_ON_HOT() is not correct as the stconn layer may detach
2537 * from the stream even if it is not closed remotely at the QUIC layer.
2538 * This happens for example when a stream must be closed due to a
2539 * rejected request. To better handle these cases, it will be required
2540 * to implement shutr/shutw MUX operations. Once this is done, this
2541 * BUG_ON_HOT() statement can be adjusted.
2542 */
2543 //BUG_ON_HOT(!qcs_is_close_remote(qcs));
Amaury Denoyellec603de42022-07-25 11:21:46 +02002544
2545 qcc_rm_sc(qcc);
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002546
Amaury Denoyelled4af0412023-05-03 18:17:19 +02002547 if (!qcs_is_close_local(qcs) && !(qcc->conn->flags & CO_FL_ERROR) &&
2548 !(qcc->flags & QC_CF_ERRL)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002549 TRACE_STATE("remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002550 qcs->flags |= QC_SF_DETACH;
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002551 qcc_refresh_timeout(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002552
2553 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002554 return;
2555 }
2556
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002557 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01002558
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002559 if (qcc_is_dead(qcc)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002560 TRACE_STATE("killing dead connection", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002561 goto release;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002562 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002563 else if (qcc->task) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002564 TRACE_DEVEL("refreshing connection's timeout", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002565 qcc_refresh_timeout(qcc);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002566 }
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002567 else {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002568 TRACE_DEVEL("completed", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002569 }
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002570
2571 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002572 return;
2573
2574 release:
2575 qc_release(qcc);
2576 TRACE_LEAVE(QMUX_EV_STRM_END);
2577 return;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002578}
2579
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002580/* Called from the upper layer, to receive data */
Amaury Denoyelleb7ce79c2022-11-24 10:51:19 +01002581static size_t qc_recv_buf(struct stconn *sc, struct buffer *buf,
2582 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002583{
Willy Tarreau3215e732022-05-27 10:09:11 +02002584 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002585 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002586 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002587
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002588 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002589
Amaury Denoyelled80fbca2022-09-19 17:02:28 +02002590 ret = qcs_http_rcv_buf(qcs, buf, count, &fin);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002591
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002592 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002593 se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002594 }
2595 else {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002596 se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
2597 if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING))
2598 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002599
Amaury Denoyelle72a78e82022-07-29 15:34:12 +02002600 /* Set end-of-input if FIN received and all data extracted. */
Christopher Faulet85eabfb2023-02-23 14:52:09 +01002601 if (fin) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002602 se_fl_set(qcs->sd, SE_FL_EOI);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002603
Christopher Faulet85eabfb2023-02-23 14:52:09 +01002604 /* If request EOM is reported to the upper layer, it means the
2605 * QCS now expects data from the opposite side.
2606 */
2607 se_expect_data(qcs->sd);
2608 }
2609
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002610 if (b_size(&qcs->rx.app_buf)) {
2611 b_free(&qcs->rx.app_buf);
2612 offer_buffers(NULL, 1);
2613 }
2614 }
2615
Amaury Denoyelleb8901d22023-05-03 15:30:04 +02002616 /* Restart demux if it was interrupted on full buffer. */
2617 if (ret && qcs->flags & QC_SF_DEM_FULL) {
2618 /* There must be data left for demux if it was interrupted on
2619 * full buffer. If this assumption is incorrect wakeup is not
2620 * necessary.
2621 */
2622 BUG_ON(!ncb_data(&qcs->rx.ncbuf, 0));
2623
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002624 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelled4af0412023-05-03 18:17:19 +02002625 if (!(qcs->qcc->flags & QC_CF_ERRL))
2626 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002627 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002628
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002629 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
2630
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002631 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002632}
2633
Amaury Denoyelleb7ce79c2022-11-24 10:51:19 +01002634static size_t qc_send_buf(struct stconn *sc, struct buffer *buf,
2635 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002636{
Willy Tarreau3215e732022-05-27 10:09:11 +02002637 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002638 size_t ret = 0;
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002639 char fin;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002640
2641 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002642
Amaury Denoyelle3dc4e5a2022-09-13 16:49:21 +02002643 /* stream layer has been detached so no transfer must occur after. */
2644 BUG_ON_HOT(qcs->flags & QC_SF_DETACH);
2645
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002646 /* Report error if set on stream endpoint layer. */
2647 if (qcs->qcc->flags & QC_CF_ERRL) {
2648 se_fl_set(qcs->sd, SE_FL_ERROR);
2649 TRACE_DEVEL("connection in error", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
2650 goto end;
2651 }
2652
Amaury Denoyelle843a1192022-07-04 11:44:38 +02002653 if (qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_RESET)) {
Amaury Denoyelle0ed617a2022-09-20 14:46:40 +02002654 ret = qcs_http_reset_buf(qcs, buf, count);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002655 goto end;
2656 }
2657
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002658 ret = qcs_http_snd_buf(qcs, buf, count, &fin);
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002659 if (fin) {
2660 TRACE_STATE("reached stream fin", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002661 qcs->flags |= QC_SF_FIN_STREAM;
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002662 }
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002663
Amaury Denoyelleab6cdec2023-01-10 10:41:41 +01002664 if (ret || fin) {
Amaury Denoyellef9b03262023-01-09 10:34:25 +01002665 qcc_send_stream(qcs, 0);
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002666 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
2667 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
2668 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002669
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002670 end:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002671 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
2672
2673 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002674}
2675
2676/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2677 * event subscriber <es> is not allowed to change from a previous call as long
2678 * as at least one event is still subscribed. The <event_type> must only be a
2679 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
2680 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002681static int qc_subscribe(struct stconn *sc, int event_type,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002682 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002683{
Willy Tarreau3215e732022-05-27 10:09:11 +02002684 return qcs_subscribe(__sc_mux_strm(sc), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002685}
2686
2687/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
2688 * The <es> pointer is not allowed to differ from the one passed to the
2689 * subscribe() call. It always returns zero.
2690 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002691static int qc_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002692{
Willy Tarreau3215e732022-05-27 10:09:11 +02002693 struct qcs *qcs = __sc_mux_strm(sc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002694
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002695 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
2696 BUG_ON(qcs->subs && qcs->subs != es);
2697
2698 es->events &= ~event_type;
2699 if (!es->events)
2700 qcs->subs = NULL;
2701
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002702 return 0;
2703}
2704
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002705static int qc_wake(struct connection *conn)
2706{
2707 struct qcc *qcc = conn->ctx;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002708
2709 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002710
Willy Tarreau784b8682022-04-11 14:18:10 +02002711 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002712 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
2713
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002714 qc_send(qcc);
2715
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002716 if (qc_process(qcc)) {
2717 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002718 goto release;
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002719 }
2720
2721 qc_wake_some_streams(qcc);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002722
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002723 qcc_refresh_timeout(qcc);
2724
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002725 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002726 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002727
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002728 release:
2729 qc_release(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002730 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002731 return 1;
2732}
2733
Amaury Denoyellea473f192022-12-21 10:21:58 +01002734static void qc_shutw(struct stconn *sc, enum co_shw_mode mode)
2735{
2736 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002737 struct qcc *qcc = qcs->qcc;
Amaury Denoyellea473f192022-12-21 10:21:58 +01002738
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002739 TRACE_ENTER(QMUX_EV_STRM_SHUT, qcc->conn, qcs);
Amaury Denoyellea473f192022-12-21 10:21:58 +01002740
Amaury Denoyelled4af0412023-05-03 18:17:19 +02002741 if (qcc->flags & QC_CF_ERRL) {
2742 TRACE_DEVEL("connection on error", QMUX_EV_QCC_END, qcc->conn);
2743 goto out;
2744 }
2745
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002746 /* Early closure reported if QC_SF_FIN_STREAM not yet set. */
Amaury Denoyellea473f192022-12-21 10:21:58 +01002747 if (!qcs_is_close_local(qcs) &&
2748 !(qcs->flags & (QC_SF_FIN_STREAM|QC_SF_TO_RESET))) {
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002749
2750 if (qcs->flags & QC_SF_UNKNOWN_PL_LENGTH) {
2751 /* Close stream with a FIN STREAM frame. */
2752 TRACE_STATE("set FIN STREAM", QMUX_EV_STRM_SHUT, qcc->conn, qcs);
2753 qcs->flags |= QC_SF_FIN_STREAM;
2754 qcc_send_stream(qcs, 0);
2755 }
2756 else {
2757 /* RESET_STREAM necessary. */
2758 qcc_reset_stream(qcs, 0);
2759 se_fl_set_error(qcs->sd);
2760 }
2761
2762 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyellea473f192022-12-21 10:21:58 +01002763 }
2764
Amaury Denoyelled4af0412023-05-03 18:17:19 +02002765 out:
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002766 TRACE_LEAVE(QMUX_EV_STRM_SHUT, qcc->conn, qcs);
Amaury Denoyellea473f192022-12-21 10:21:58 +01002767}
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002768
Willy Tarreaub4a4fee2022-09-02 16:00:40 +02002769/* for debugging with CLI's "show sess" command. May emit multiple lines, each
2770 * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single
2771 * line is used. Each field starts with a space so it's safe to print it after
2772 * existing fields.
2773 */
2774static int qc_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx)
2775{
2776 struct qcs *qcs = sd->se;
2777 struct qcc *qcc;
2778 int ret = 0;
2779
2780 if (!qcs)
2781 return ret;
2782
2783 chunk_appendf(msg, " qcs=%p .flg=%#x .id=%llu .st=%s .ctx=%p, .err=%#llx",
2784 qcs, qcs->flags, (ull)qcs->id, qcs_st_to_str(qcs->st), qcs->ctx, (ull)qcs->err);
2785
2786 if (pfx)
2787 chunk_appendf(msg, "\n%s", pfx);
2788
2789 qcc = qcs->qcc;
2790 chunk_appendf(msg, " qcc=%p .flg=%#x .nbsc=%llu .nbhreq=%llu, .task=%p",
2791 qcc, qcc->flags, (ull)qcc->nb_sc, (ull)qcc->nb_hreq, qcc->task);
2792 return ret;
2793}
2794
2795
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002796static const struct mux_ops qc_ops = {
2797 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002798 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002799 .detach = qc_detach,
Amaury Denoyelleb7ce79c2022-11-24 10:51:19 +01002800 .rcv_buf = qc_recv_buf,
2801 .snd_buf = qc_send_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002802 .subscribe = qc_subscribe,
2803 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002804 .wake = qc_wake,
Amaury Denoyellea473f192022-12-21 10:21:58 +01002805 .shutw = qc_shutw,
Willy Tarreaub4a4fee2022-09-02 16:00:40 +02002806 .show_sd = qc_show_sd,
Willy Tarreaub5821e12022-04-26 11:54:08 +02002807 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02002808 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002809};
2810
2811static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002812 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002813
2814INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);