blob: 3cc24108d146229c1c26119741a7b2e6517e0f27 [file] [log] [blame]
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001#include <haproxy/mux_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002
Amaury Denoyelleeb01f592021-10-07 16:44:05 +02003#include <import/eb64tree.h>
4
Frédéric Lécailledfbae762021-02-18 09:59:01 +01005#include <haproxy/api.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01006#include <haproxy/connection.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +01007#include <haproxy/dynbuf.h>
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01008#include <haproxy/h3.h>
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02009#include <haproxy/list.h>
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +020010#include <haproxy/ncbuf.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +010011#include <haproxy/pool.h>
Frédéric Lécaille9969adb2023-01-18 11:52:21 +010012#include <haproxy/proxy.h>
Amaury Denoyelled80fbca2022-09-19 17:02:28 +020013#include <haproxy/qmux_http.h>
Amaury Denoyelle36d50bf2022-09-19 16:12:38 +020014#include <haproxy/qmux_trace.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020015#include <haproxy/quic_conn.h>
Amaury Denoyelle40c24f12023-01-27 17:47:49 +010016#include <haproxy/quic_frame.h>
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +010017#include <haproxy/quic_sock.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020018#include <haproxy/quic_stream.h>
Frédéric Lécaille748ece62022-05-21 23:58:40 +020019#include <haproxy/quic_tp-t.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020020#include <haproxy/ssl_sock-t.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020021#include <haproxy/stconn.h>
Amaury Denoyelle1a2faef2023-05-15 15:17:28 +020022#include <haproxy/time.h>
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010023#include <haproxy/trace.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010024
Amaury Denoyelledeed7772021-12-03 11:36:46 +010025DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010026DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
27
Amaury Denoyelle4b167002022-12-12 09:59:50 +010028static void qc_free_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
29{
30 struct buffer buf;
31
32 if (ncb_is_null(ncbuf))
33 return;
34
35 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
36 b_free(&buf);
37 offer_buffers(NULL, 1);
38
39 *ncbuf = NCBUF_NULL;
40}
41
42/* Free <qcs> instance. This function is reserved for internal usage : it must
43 * only be called on qcs alloc error or on connection shutdown. Else
Ilya Shipitsin07be66d2023-04-01 12:26:42 +020044 * qcs_destroy must be preferred to handle QUIC flow-control increase.
Amaury Denoyelle4b167002022-12-12 09:59:50 +010045 */
46static void qcs_free(struct qcs *qcs)
47{
48 struct qcc *qcc = qcs->qcc;
49
50 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn, qcs);
51
Amaury Denoyelle15337fd2022-12-20 14:47:16 +010052 /* Safe to use even if already removed from the list. */
53 LIST_DEL_INIT(&qcs->el_opening);
Amaury Denoyelle20f2a422023-01-03 14:39:24 +010054 LIST_DEL_INIT(&qcs->el_send);
Amaury Denoyelle4b167002022-12-12 09:59:50 +010055
56 /* Release stream endpoint descriptor. */
57 BUG_ON(qcs->sd && !se_fl_test(qcs->sd, SE_FL_ORPHAN));
58 sedesc_free(qcs->sd);
59
60 /* Release app-layer context. */
61 if (qcs->ctx && qcc->app_ops->detach)
62 qcc->app_ops->detach(qcs);
63
64 /* Release qc_stream_desc buffer from quic-conn layer. */
65 qc_stream_desc_release(qcs->stream);
66
67 /* Free Rx/Tx buffers. */
68 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
69 b_free(&qcs->tx.buf);
70
71 BUG_ON(!qcc->strms[qcs_id_type(qcs->id)].nb_streams);
72 --qcc->strms[qcs_id_type(qcs->id)].nb_streams;
73
74 /* Remove qcs from qcc tree. */
75 eb64_delete(&qcs->by_id);
76
77 pool_free(pool_head_qcs, qcs);
78
79 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
80}
81
Amaury Denoyelledeed7772021-12-03 11:36:46 +010082/* Allocate a new QUIC streams with id <id> and type <type>. */
Amaury Denoyellea509ffb2022-07-04 15:50:33 +020083static struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +010084{
Amaury Denoyelledeed7772021-12-03 11:36:46 +010085 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010086
Amaury Denoyelle4f137572022-03-24 17:10:00 +010087 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
88
Amaury Denoyelledeed7772021-12-03 11:36:46 +010089 qcs = pool_alloc(pool_head_qcs);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +020090 if (!qcs) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +020091 TRACE_ERROR("alloc failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle17014a62022-04-27 15:09:27 +020092 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +020093 }
Amaury Denoyelle17014a62022-04-27 15:09:27 +020094
95 qcs->stream = NULL;
96 qcs->qcc = qcc;
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +020097 qcs->sd = NULL;
Amaury Denoyelle17014a62022-04-27 15:09:27 +020098 qcs->flags = QC_SF_NONE;
Amaury Denoyelle38e60062022-07-01 16:48:42 +020099 qcs->st = QC_SS_IDLE;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200100 qcs->ctx = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100101
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200102 /* App callback attach may register the stream for http-request wait.
103 * These fields must be initialed before.
104 */
105 LIST_INIT(&qcs->el_opening);
Amaury Denoyelle20f2a422023-01-03 14:39:24 +0100106 LIST_INIT(&qcs->el_send);
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200107 qcs->start = TICK_ETERNITY;
108
Amaury Denoyelle4b167002022-12-12 09:59:50 +0100109 /* store transport layer stream descriptor in qcc tree */
110 qcs->id = qcs->by_id.key = id;
111 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
112
113 qcc->strms[type].nb_streams++;
114
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200115 /* Allocate transport layer stream descriptor. Only needed for TX. */
116 if (!quic_stream_is_uni(id) || !quic_stream_is_remote(qcc, id)) {
117 struct quic_conn *qc = qcc->conn->handle.qc;
118 qcs->stream = qc_stream_desc_new(id, type, qcs, qc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200119 if (!qcs->stream) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200120 TRACE_ERROR("qc_stream_desc alloc failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200121 goto err;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200122 }
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200123 }
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200124
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100125 /* If stream is local, use peer remote-limit, or else the opposite. */
Amaury Denoyelle176174f2022-10-21 17:02:18 +0200126 if (quic_stream_is_bidi(id)) {
127 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
128 qcc->rfctl.msd_bidi_l;
129 }
130 else if (quic_stream_is_local(qcc, id)) {
131 qcs->tx.msd = qcc->rfctl.msd_uni_l;
132 }
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100133
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200134 qcs->rx.ncbuf = NCBUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100135 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200136 qcs->rx.offset = qcs->rx.offset_max = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100137
Amaury Denoyelle176174f2022-10-21 17:02:18 +0200138 if (quic_stream_is_bidi(id)) {
139 qcs->rx.msd = quic_stream_is_local(qcc, id) ? qcc->lfctl.msd_bidi_l :
140 qcc->lfctl.msd_bidi_r;
141 }
142 else if (quic_stream_is_remote(qcc, id)) {
143 qcs->rx.msd = qcc->lfctl.msd_uni_r;
144 }
Amaury Denoyellea9773552022-05-16 14:38:25 +0200145 qcs->rx.msd_init = qcs->rx.msd;
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200146
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100147 qcs->tx.buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100148 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100149 qcs->tx.sent_offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100150
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100151 qcs->wait_event.tasklet = NULL;
152 qcs->wait_event.events = 0;
153 qcs->subs = NULL;
154
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200155 qcs->err = 0;
156
Amaury Denoyelle3d550842023-01-24 17:42:21 +0100157 if (qcc->app_ops->attach && qcc->app_ops->attach(qcs, qcc->ctx)) {
158 TRACE_ERROR("app proto failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
159 goto err;
160 }
161
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100162 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100163 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100164 return qcs;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200165
166 err:
Amaury Denoyelle4b167002022-12-12 09:59:50 +0100167 qcs_free(qcs);
168 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200169 return NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100170}
171
Amaury Denoyelle3abeb572022-07-04 11:42:27 +0200172static forceinline struct stconn *qcs_sc(const struct qcs *qcs)
173{
174 return qcs->sd ? qcs->sd->sc : NULL;
175}
176
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200177/* Reset the <qcc> inactivity timeout for http-keep-alive timeout. */
178static forceinline void qcc_reset_idle_start(struct qcc *qcc)
179{
180 qcc->idle_start = now_ms;
181}
182
Amaury Denoyellec603de42022-07-25 11:21:46 +0200183/* Decrement <qcc> sc. */
184static forceinline void qcc_rm_sc(struct qcc *qcc)
185{
186 BUG_ON_HOT(!qcc->nb_sc);
187 --qcc->nb_sc;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200188
189 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
190 * refreshed after this on stream detach.
191 */
192 if (!qcc->nb_sc && !qcc->nb_hreq)
193 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200194}
195
196/* Decrement <qcc> hreq. */
197static forceinline void qcc_rm_hreq(struct qcc *qcc)
198{
199 BUG_ON_HOT(!qcc->nb_hreq);
200 --qcc->nb_hreq;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200201
202 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
203 * refreshed after this on I/O handler.
204 */
205 if (!qcc->nb_sc && !qcc->nb_hreq)
206 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200207}
208
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200209static inline int qcc_is_dead(const struct qcc *qcc)
210{
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200211 /* Maintain connection if stream endpoints are still active. */
212 if (qcc->nb_sc)
213 return 0;
214
215 /* Connection considered dead if either :
216 * - remote error detected at tranport level
217 * - error detected locally
218 * - MUX timeout expired or unset
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200219 */
Amaury Denoyelle5f67b172023-05-04 18:52:42 +0200220 if (qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL_DONE) ||
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200221 !qcc->task) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200222 return 1;
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200223 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200224
225 return 0;
226}
227
228/* Return true if the mux timeout should be armed. */
229static inline int qcc_may_expire(struct qcc *qcc)
230{
231 return !qcc->nb_sc;
232}
233
234/* Refresh the timeout on <qcc> if needed depending on its state. */
235static void qcc_refresh_timeout(struct qcc *qcc)
236{
237 const struct proxy *px = qcc->proxy;
238
239 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
240
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200241 if (!qcc->task) {
242 TRACE_DEVEL("already expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200243 goto leave;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200244 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200245
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200246 /* Check if upper layer is responsible of timeout management. */
247 if (!qcc_may_expire(qcc)) {
248 TRACE_DEVEL("not eligible for timeout", QMUX_EV_QCC_WAKE, qcc->conn);
249 qcc->task->expire = TICK_ETERNITY;
250 task_queue(qcc->task);
251 goto leave;
252 }
253
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200254 /* Frontend timeout management
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +0100255 * - shutdown done -> timeout client-fin
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200256 * - detached streams with data left to send -> default timeout
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200257 * - stream waiting on incomplete request or no stream yet activated -> timeout http-request
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200258 * - idle after stream processing -> timeout http-keep-alive
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +0100259 *
260 * If proxy stop-stop in progress, immediate or spread close will be
261 * processed if shutdown already one or connection is idle.
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200262 */
263 if (!conn_is_back(qcc->conn)) {
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +0100264 if (qcc->nb_hreq && !(qcc->flags & QC_CF_APP_SHUT)) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200265 TRACE_DEVEL("one or more requests still in progress", QMUX_EV_QCC_WAKE, qcc->conn);
266 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200267 task_queue(qcc->task);
268 goto leave;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200269 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200270
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +0100271 if ((!LIST_ISEMPTY(&qcc->opening_list) || unlikely(!qcc->largest_bidi_r)) &&
272 !(qcc->flags & QC_CF_APP_SHUT)) {
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200273 int timeout = px->timeout.httpreq;
274 struct qcs *qcs = NULL;
275 int base_time;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200276
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200277 /* Use start time of first stream waiting on HTTP or
278 * qcc idle if no stream not yet used.
279 */
280 if (likely(!LIST_ISEMPTY(&qcc->opening_list)))
281 qcs = LIST_ELEM(qcc->opening_list.n, struct qcs *, el_opening);
282 base_time = qcs ? qcs->start : qcc->idle_start;
283
284 TRACE_DEVEL("waiting on http request", QMUX_EV_QCC_WAKE, qcc->conn, qcs);
285 qcc->task->expire = tick_add_ifset(base_time, timeout);
286 }
287 else {
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +0100288 if (qcc->flags & QC_CF_APP_SHUT) {
289 TRACE_DEVEL("connection in closing", QMUX_EV_QCC_WAKE, qcc->conn);
290 qcc->task->expire = tick_add_ifset(now_ms,
291 qcc->shut_timeout);
292 }
293 else {
294 /* Use http-request timeout if keep-alive timeout not set */
295 int timeout = tick_isset(px->timeout.httpka) ?
296 px->timeout.httpka : px->timeout.httpreq;
297 TRACE_DEVEL("at least one request achieved but none currently in progress", QMUX_EV_QCC_WAKE, qcc->conn);
298 qcc->task->expire = tick_add_ifset(qcc->idle_start, timeout);
299 }
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +0100300
301 /* If proxy soft-stop in progress and connection is
302 * inactive, close the connection immediately. If a
303 * close-spread-time is configured, randomly spread the
304 * timer over a closing window.
305 */
306 if ((qcc->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) &&
307 !(global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)) {
308
309 /* Wake timeout task immediately if window already expired. */
310 int remaining_window = tick_isset(global.close_spread_end) ?
311 tick_remain(now_ms, global.close_spread_end) : 0;
312
313 TRACE_DEVEL("proxy disabled, prepare connection soft-stop", QMUX_EV_QCC_WAKE, qcc->conn);
314 if (remaining_window) {
315 /* We don't need to reset the expire if it would
316 * already happen before the close window end.
317 */
318 if (!tick_isset(qcc->task->expire) ||
319 tick_is_le(global.close_spread_end, qcc->task->expire)) {
320 /* Set an expire value shorter than the current value
321 * because the close spread window end comes earlier.
322 */
323 qcc->task->expire = tick_add(now_ms,
324 statistical_prng_range(remaining_window));
325 }
326 }
327 else {
328 /* We are past the soft close window end, wake the timeout
329 * task up immediately.
330 */
331 qcc->task->expire = now_ms;
332 task_wakeup(qcc->task, TASK_WOKEN_TIMER);
333 }
334 }
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200335 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200336 }
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200337
338 /* fallback to default timeout if frontend specific undefined or for
339 * backend connections.
340 */
341 if (!tick_isset(qcc->task->expire)) {
342 TRACE_DEVEL("fallback to default timeout", QMUX_EV_QCC_WAKE, qcc->conn);
343 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200344 }
345
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200346 task_queue(qcc->task);
347
348 leave:
349 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
350}
351
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200352/* Mark a stream as open if it was idle. This can be used on every
353 * successful emission/reception operation to update the stream state.
354 */
355static void qcs_idle_open(struct qcs *qcs)
356{
357 /* This operation must not be used if the stream is already closed. */
358 BUG_ON_HOT(qcs->st == QC_SS_CLO);
359
360 if (qcs->st == QC_SS_IDLE) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200361 TRACE_STATE("opening stream", QMUX_EV_QCS_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200362 qcs->st = QC_SS_OPEN;
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200363 }
364}
365
366/* Close the local channel of <qcs> instance. */
367static void qcs_close_local(struct qcs *qcs)
368{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200369 TRACE_STATE("closing stream locally", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
370
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200371 /* The stream must have already been opened. */
372 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
373
374 /* This operation cannot be used multiple times. */
375 BUG_ON_HOT(qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO);
376
377 if (quic_stream_is_bidi(qcs->id)) {
378 qcs->st = (qcs->st == QC_SS_HREM) ? QC_SS_CLO : QC_SS_HLOC;
Amaury Denoyelleafb7b9d2022-09-19 11:58:24 +0200379
380 if (qcs->flags & QC_SF_HREQ_RECV)
381 qcc_rm_hreq(qcs->qcc);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200382 }
383 else {
384 /* Only local uni streams are valid for this operation. */
385 BUG_ON_HOT(quic_stream_is_remote(qcs->qcc, qcs->id));
386 qcs->st = QC_SS_CLO;
387 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200388}
389
390/* Close the remote channel of <qcs> instance. */
391static void qcs_close_remote(struct qcs *qcs)
392{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200393 TRACE_STATE("closing stream remotely", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
394
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200395 /* The stream must have already been opened. */
396 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
397
398 /* This operation cannot be used multiple times. */
399 BUG_ON_HOT(qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO);
400
401 if (quic_stream_is_bidi(qcs->id)) {
402 qcs->st = (qcs->st == QC_SS_HLOC) ? QC_SS_CLO : QC_SS_HREM;
403 }
404 else {
405 /* Only remote uni streams are valid for this operation. */
406 BUG_ON_HOT(quic_stream_is_local(qcs->qcc, qcs->id));
407 qcs->st = QC_SS_CLO;
408 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200409}
410
411static int qcs_is_close_local(struct qcs *qcs)
412{
413 return qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO;
414}
415
Amaury Denoyelle6eb3c4b2022-12-09 16:26:03 +0100416static int qcs_is_close_remote(struct qcs *qcs)
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200417{
418 return qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO;
419}
420
Amaury Denoyelle0abde9d2023-05-11 16:52:17 +0200421/* Allocate if needed buffer <bptr> for stream <qcs>.
422 *
423 * Returns the buffer instance or NULL on allocation failure.
424 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100425struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100426{
Amaury Denoyelle0abde9d2023-05-11 16:52:17 +0200427 return b_alloc(bptr);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100428}
429
Amaury Denoyelled00b3092023-05-11 17:00:54 +0200430/* Allocate if needed buffer <ncbuf> for stream <qcs>.
431 *
432 * Returns the buffer instance or NULL on allocation failure.
433 */
Amaury Denoyellea441ec92022-07-04 15:48:57 +0200434static struct ncbuf *qc_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200435{
436 struct buffer buf = BUF_NULL;
437
438 if (ncb_is_null(ncbuf)) {
Amaury Denoyelled00b3092023-05-11 17:00:54 +0200439 if (!b_alloc(&buf))
440 return NULL;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200441
442 *ncbuf = ncb_make(buf.area, buf.size, 0);
443 ncb_init(ncbuf, 0);
444 }
445
446 return ncbuf;
447}
448
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500449/* Notify an eventual subscriber on <qcs> or else wakeup up the stconn layer if
Amaury Denoyelle4561f842022-07-06 14:54:34 +0200450 * initialized.
451 */
452static void qcs_alert(struct qcs *qcs)
453{
454 if (qcs->subs) {
455 qcs_notify_recv(qcs);
456 qcs_notify_send(qcs);
457 }
458 else if (qcs_sc(qcs) && qcs->sd->sc->app_ops->wake) {
Amaury Denoyelle2d5c3f52023-05-11 13:41:41 +0200459 TRACE_POINT(QMUX_EV_STRM_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle4561f842022-07-06 14:54:34 +0200460 qcs->sd->sc->app_ops->wake(qcs->sd->sc);
461 }
462}
463
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100464int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
465{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100466 struct qcc *qcc = qcs->qcc;
467
468 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100469
470 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
471 BUG_ON(qcs->subs && qcs->subs != es);
472
473 es->events |= event_type;
474 qcs->subs = es;
475
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100476 if (event_type & SUB_RETRY_RECV)
477 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
478
479 if (event_type & SUB_RETRY_SEND)
480 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
481
482 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
483
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100484 return 0;
485}
486
487void qcs_notify_recv(struct qcs *qcs)
488{
489 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
Amaury Denoyelle2d5c3f52023-05-11 13:41:41 +0200490 TRACE_POINT(QMUX_EV_STRM_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100491 tasklet_wakeup(qcs->subs->tasklet);
492 qcs->subs->events &= ~SUB_RETRY_RECV;
493 if (!qcs->subs->events)
494 qcs->subs = NULL;
495 }
496}
497
498void qcs_notify_send(struct qcs *qcs)
499{
500 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
Amaury Denoyelle2d5c3f52023-05-11 13:41:41 +0200501 TRACE_POINT(QMUX_EV_STRM_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100502 tasklet_wakeup(qcs->subs->tasklet);
503 qcs->subs->events &= ~SUB_RETRY_SEND;
504 if (!qcs->subs->events)
505 qcs->subs = NULL;
506 }
507}
508
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200509/* A fatal error is detected locally for <qcc> connection. It should be closed
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200510 * with a CONNECTION_CLOSE using <err> code. Set <app> to true to indicate that
511 * the code must be considered as an application level error. This function
512 * must not be called more than once by connection.
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200513 */
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200514void qcc_set_error(struct qcc *qcc, int err, int app)
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200515{
516 /* This must not be called multiple times per connection. */
517 BUG_ON(qcc->flags & QC_CF_ERRL);
518
519 TRACE_STATE("connection on error", QMUX_EV_QCC_ERR, qcc->conn);
520
521 qcc->flags |= QC_CF_ERRL;
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200522 qcc->err = app ? quic_err_app(err) : quic_err_transport(err);
Amaury Denoyelleda24bcf2023-05-09 18:20:45 +0200523
524 /* TODO
525 * Ensure qc_send() will be conducted to convert QC_CF_ERRL in
526 * QC_CF_ERRL_DONE with CONNECTION_CLOSE frame emission. This may be
527 * unnecessary if we are currently in the MUX tasklet context, but it
528 * is too tedious too not forget a wakeup outside of this function for
529 * the moment.
530 */
531 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyelle51f116d2023-05-04 15:49:02 +0200532}
533
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200534/* Open a locally initiated stream for the connection <qcc>. Set <bidi> for a
535 * bidirectional stream, else an unidirectional stream is opened. The next
536 * available ID on the connection will be used according to the stream type.
537 *
538 * Returns the allocated stream instance or NULL on error.
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100539 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200540struct qcs *qcc_init_stream_local(struct qcc *qcc, int bidi)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100541{
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200542 struct qcs *qcs;
543 enum qcs_type type;
544 uint64_t *next;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100545
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200546 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
547
548 if (bidi) {
549 next = &qcc->next_bidi_l;
550 type = conn_is_back(qcc->conn) ? QCS_CLT_BIDI : QCS_SRV_BIDI;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100551 }
552 else {
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200553 next = &qcc->next_uni_l;
554 type = conn_is_back(qcc->conn) ? QCS_CLT_UNI : QCS_SRV_UNI;
555 }
556
557 /* TODO ensure that we won't overflow remote peer flow control limit on
558 * streams. Else, we should emit a STREAMS_BLOCKED frame.
559 */
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100560
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200561 qcs = qcs_new(qcc, *next, type);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200562 if (!qcs) {
563 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200564 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200565 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200566 }
Amaury Denoyellec055e302022-02-07 16:09:06 +0100567
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200568 TRACE_PROTO("opening local stream", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200569 *next += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100570
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200571 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200572 return qcs;
573}
574
575/* Open a remote initiated stream for the connection <qcc> with ID <id>. The
576 * caller is responsible to ensure that a stream with the same ID was not
577 * already opened. This function will also create all intermediaries streams
578 * with ID smaller than <id> not already opened before.
579 *
580 * Returns the allocated stream instance or NULL on error.
581 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200582static struct qcs *qcc_init_stream_remote(struct qcc *qcc, uint64_t id)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200583{
584 struct qcs *qcs = NULL;
585 enum qcs_type type;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200586 uint64_t *largest, max_id;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100587
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200588 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200589
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200590 BUG_ON_HOT(quic_stream_is_local(qcc, id));
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100591
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200592 if (quic_stream_is_bidi(id)) {
593 largest = &qcc->largest_bidi_r;
594 type = conn_is_back(qcc->conn) ? QCS_SRV_BIDI : QCS_CLT_BIDI;
595 }
596 else {
597 largest = &qcc->largest_uni_r;
598 type = conn_is_back(qcc->conn) ? QCS_SRV_UNI : QCS_CLT_UNI;
599 }
600
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200601 /* RFC 9000 4.6. Controlling Concurrency
602 *
603 * An endpoint that receives a frame with a stream ID exceeding the
604 * limit it has sent MUST treat this as a connection error of type
605 * STREAM_LIMIT_ERROR
606 */
607 max_id = quic_stream_is_bidi(id) ? qcc->lfctl.ms_bidi * 4 :
608 qcc->lfctl.ms_uni * 4;
609 if (id >= max_id) {
610 TRACE_ERROR("flow control error", QMUX_EV_QCS_NEW|QMUX_EV_PROTO_ERR, qcc->conn);
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200611 qcc_set_error(qcc, QC_ERR_STREAM_LIMIT_ERROR, 0);
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200612 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200613 }
614
615 /* Only stream ID not already opened can be used. */
616 BUG_ON(id < *largest);
617
618 while (id >= *largest) {
Amaury Denoyellefd79ddb2022-08-16 11:13:45 +0200619 const char *str = *largest < id ? "initializing intermediary remote stream" :
620 "initializing remote stream";
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200621
622 qcs = qcs_new(qcc, *largest, type);
623 if (!qcs) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200624 TRACE_ERROR("stream fallocation failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200625 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200626 goto err;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100627 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200628
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200629 TRACE_PROTO(str, QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200630 *largest += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100631 }
632
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200633 out:
634 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle50742292022-03-29 14:57:19 +0200635 return qcs;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200636
637 err:
638 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
639 return NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200640}
641
Amaury Denoyelle1a2faef2023-05-15 15:17:28 +0200642struct stconn *qc_attach_sc(struct qcs *qcs, struct buffer *buf)
643{
644 struct qcc *qcc = qcs->qcc;
645 struct session *sess = qcc->conn->owner;
646
647 qcs->sd = sedesc_new();
648 if (!qcs->sd)
649 return NULL;
650
651 qcs->sd->se = qcs;
652 qcs->sd->conn = qcc->conn;
653 se_fl_set(qcs->sd, SE_FL_T_MUX | SE_FL_ORPHAN | SE_FL_NOT_FIRST);
654 se_expect_no_data(qcs->sd);
655
656 /* TODO duplicated from mux_h2 */
657 sess->t_idle = ns_to_ms(now_ns - sess->accept_ts) - sess->t_handshake;
658
659 if (!sc_new_from_endp(qcs->sd, sess, buf))
660 return NULL;
661
662 /* QC_SF_HREQ_RECV must be set once for a stream. Else, nb_hreq counter
663 * will be incorrect for the connection.
664 */
665 BUG_ON_HOT(qcs->flags & QC_SF_HREQ_RECV);
666 qcs->flags |= QC_SF_HREQ_RECV;
667 ++qcc->nb_sc;
668 ++qcc->nb_hreq;
669
670 /* TODO duplicated from mux_h2 */
671 sess->accept_date = date;
672 sess->accept_ts = now_ns;
673 sess->t_handshake = 0;
674 sess->t_idle = 0;
675
676 /* A stream must have been registered for HTTP wait before attaching
677 * it to sedesc. See <qcs_wait_http_req> for more info.
678 */
679 BUG_ON_HOT(!LIST_INLIST(&qcs->el_opening));
680 LIST_DEL_INIT(&qcs->el_opening);
681
682 return qcs->sd->sc;
683}
684
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200685/* Use this function for a stream <id> which is not in <qcc> stream tree. It
686 * returns true if the associated stream is closed.
687 */
688static int qcc_stream_id_is_closed(struct qcc *qcc, uint64_t id)
689{
690 uint64_t *largest;
691
692 /* This function must only be used for stream not present in the stream tree. */
693 BUG_ON_HOT(eb64_lookup(&qcc->streams_by_id, id));
694
695 if (quic_stream_is_local(qcc, id)) {
696 largest = quic_stream_is_uni(id) ? &qcc->next_uni_l :
697 &qcc->next_bidi_l;
698 }
699 else {
700 largest = quic_stream_is_uni(id) ? &qcc->largest_uni_r :
701 &qcc->largest_bidi_r;
702 }
703
704 return id < *largest;
705}
706
707/* Retrieve the stream instance from <id> ID. This can be used when receiving
708 * STREAM, STREAM_DATA_BLOCKED, RESET_STREAM, MAX_STREAM_DATA or STOP_SENDING
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200709 * frames. Set to false <receive_only> or <send_only> if these particular types
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200710 * of streams are not allowed. If the stream instance is found, it is stored in
711 * <out>.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200712 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200713 * Returns 0 on success else non-zero. On error, a RESET_STREAM or a
714 * CONNECTION_CLOSE is automatically emitted. Beware that <out> may be NULL
715 * on success if the stream has already been closed.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200716 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200717int qcc_get_qcs(struct qcc *qcc, uint64_t id, int receive_only, int send_only,
718 struct qcs **out)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200719{
720 struct eb64_node *node;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200721
722 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200723 *out = NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200724
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200725 if (!receive_only && quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200726 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 +0200727 qcc_set_error(qcc, QC_ERR_STREAM_STATE_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200728 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200729 }
730
731 if (!send_only && quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200732 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 +0200733 qcc_set_error(qcc, QC_ERR_STREAM_STATE_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200734 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200735 }
736
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200737 /* Search the stream in the connection tree. */
738 node = eb64_lookup(&qcc->streams_by_id, id);
739 if (node) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200740 *out = eb64_entry(node, struct qcs, by_id);
741 TRACE_DEVEL("using stream from connection tree", QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200742 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200743 }
744
745 /* Check if stream is already closed. */
746 if (qcc_stream_id_is_closed(qcc, id)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200747 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200748 /* Consider this as a success even if <out> is left NULL. */
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200749 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200750 }
751
752 /* Create the stream. This is valid only for remote initiated one. A
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500753 * local stream must have already been explicitly created by the
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200754 * application protocol layer.
755 */
756 if (quic_stream_is_local(qcc, id)) {
757 /* RFC 9000 19.8. STREAM Frames
758 *
759 * An endpoint MUST terminate the connection with error
760 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
761 * initiated stream that has not yet been created, or for a send-only
762 * stream.
763 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200764 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 +0200765 qcc_set_error(qcc, QC_ERR_STREAM_STATE_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200766 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200767 }
768 else {
769 /* Remote stream not found - try to open it. */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200770 *out = qcc_init_stream_remote(qcc, id);
771 if (!*out) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200772 TRACE_ERROR("stream creation error", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200773 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200774 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200775 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100776
777 out:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200778 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200779 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200780
781 err:
782 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
783 return 1;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100784}
785
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200786/* Simple function to duplicate a buffer */
787static inline struct buffer qcs_b_dup(const struct ncbuf *b)
788{
789 return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0));
790}
791
Amaury Denoyelle36d4b5e2022-07-01 11:25:40 +0200792/* Remove <bytes> from <qcs> Rx buffer. Flow-control for received offsets may
793 * be allocated for the peer if needed.
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200794 */
795static void qcs_consume(struct qcs *qcs, uint64_t bytes)
796{
797 struct qcc *qcc = qcs->qcc;
798 struct quic_frame *frm;
799 struct ncbuf *buf = &qcs->rx.ncbuf;
800 enum ncb_ret ret;
801
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200802 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
803
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200804 ret = ncb_advance(buf, bytes);
805 if (ret) {
806 ABORT_NOW(); /* should not happens because removal only in data */
807 }
808
809 if (ncb_is_empty(buf))
810 qc_free_ncbuf(qcs, buf);
811
812 qcs->rx.offset += bytes;
Amaury Denoyellebb6296c2022-12-09 15:00:17 +0100813 /* Not necessary to emit a MAX_STREAM_DATA if all data received. */
814 if (qcs->flags & QC_SF_SIZE_KNOWN)
815 goto conn_fctl;
816
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200817 if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200818 TRACE_DATA("increase stream credit via MAX_STREAM_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +0100819 frm = qc_frm_alloc(QUIC_FT_MAX_STREAM_DATA);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +0100820 if (!frm) {
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200821 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +0100822 return;
823 }
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200824
825 qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init;
826
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200827 frm->max_stream_data.id = qcs->id;
828 frm->max_stream_data.max_stream_data = qcs->rx.msd;
829
830 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
831 tasklet_wakeup(qcc->wait_event.tasklet);
832 }
833
Amaury Denoyellebb6296c2022-12-09 15:00:17 +0100834 conn_fctl:
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200835 qcc->lfctl.offsets_consume += bytes;
836 if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200837 TRACE_DATA("increase conn credit via MAX_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +0100838 frm = qc_frm_alloc(QUIC_FT_MAX_DATA);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +0100839 if (!frm) {
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200840 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +0100841 return;
842 }
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200843
844 qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init;
845
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200846 frm->max_data.max_data = qcc->lfctl.md;
847
848 LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list);
849 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
850 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200851
852 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200853}
854
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200855/* Decode the content of STREAM frames already received on the stream instance
856 * <qcs>.
857 *
858 * Returns 0 on success else non-zero.
859 */
860static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
861{
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200862 struct buffer b;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200863 ssize_t ret;
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200864 int fin = 0;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200865
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200866 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
867
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200868 b = qcs_b_dup(&qcs->rx.ncbuf);
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200869
Amaury Denoyelled1310f82022-09-16 13:30:59 +0200870 /* Signal FIN to application if STREAM FIN received with all data. */
871 if (qcs_is_close_remote(qcs))
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200872 fin = 1;
873
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100874 if (!(qcs->flags & QC_SF_READ_ABORTED)) {
875 ret = qcc->app_ops->decode_qcs(qcs, &b, fin);
876 if (ret < 0) {
877 TRACE_ERROR("decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
878 goto err;
879 }
880 }
881 else {
882 TRACE_DATA("ignore read on stream", QMUX_EV_QCS_RECV, qcc->conn, qcs);
883 ret = b_data(&b);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200884 }
885
Amaury Denoyelle381d8132023-02-17 09:51:20 +0100886 if (ret)
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200887 qcs_consume(qcs, ret);
Amaury Denoyelle381d8132023-02-17 09:51:20 +0100888 if (ret || (!b_data(&b) && fin))
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200889 qcs_notify_recv(qcs);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200890
891 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200892 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200893
894 err:
895 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
896 return 1;
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200897}
898
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200899/* Prepare for the emission of RESET_STREAM on <qcs> with error code <err>. */
900void qcc_reset_stream(struct qcs *qcs, int err)
901{
902 struct qcc *qcc = qcs->qcc;
903
904 if ((qcs->flags & QC_SF_TO_RESET) || qcs_is_close_local(qcs))
905 return;
906
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200907 TRACE_STATE("reset stream", QMUX_EV_QCS_END, qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200908 qcs->flags |= QC_SF_TO_RESET;
909 qcs->err = err;
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +0100910
Amaury Denoyelle178fbff2023-03-22 11:17:59 +0100911 /* Remove prepared stream data from connection flow-control calcul. */
912 if (qcs->tx.offset > qcs->tx.sent_offset) {
913 const uint64_t diff = qcs->tx.offset - qcs->tx.sent_offset;
914 BUG_ON(qcc->tx.offsets - diff < qcc->tx.sent_offsets);
915 qcc->tx.offsets -= diff;
916 /* Reset qcs offset to prevent BUG_ON() on qcs_destroy(). */
917 qcs->tx.offset = qcs->tx.sent_offset;
918 }
919
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +0100920 qcc_send_stream(qcs, 1);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200921 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100922}
923
Amaury Denoyellef9b03262023-01-09 10:34:25 +0100924/* Register <qcs> stream for emission of STREAM, STOP_SENDING or RESET_STREAM.
925 * Set <urg> to 1 if stream content should be treated in priority compared to
926 * other streams.
927 */
928void qcc_send_stream(struct qcs *qcs, int urg)
Amaury Denoyelle20f2a422023-01-03 14:39:24 +0100929{
930 struct qcc *qcc = qcs->qcc;
931
932 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
933
934 /* Cannot send if already closed. */
935 BUG_ON(qcs_is_close_local(qcs));
936
Amaury Denoyellef9b03262023-01-09 10:34:25 +0100937 if (urg) {
938 LIST_DEL_INIT(&qcs->el_send);
939 LIST_INSERT(&qcc->send_list, &qcs->el_send);
940 }
941 else {
942 if (!LIST_INLIST(&qcs->el_send))
943 LIST_APPEND(&qcs->qcc->send_list, &qcs->el_send);
944 }
Amaury Denoyelle20f2a422023-01-03 14:39:24 +0100945
946 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
947}
948
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100949/* Prepare for the emission of STOP_SENDING on <qcs>. */
950void qcc_abort_stream_read(struct qcs *qcs)
951{
952 struct qcc *qcc = qcs->qcc;
953
954 TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn, qcs);
955
956 if ((qcs->flags & QC_SF_TO_STOP_SENDING) || qcs_is_close_remote(qcs))
957 goto end;
958
959 TRACE_STATE("abort stream read", QMUX_EV_QCS_END, qcc->conn, qcs);
960 qcs->flags |= (QC_SF_TO_STOP_SENDING|QC_SF_READ_ABORTED);
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +0100961
962 qcc_send_stream(qcs, 1);
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100963 tasklet_wakeup(qcc->wait_event.tasklet);
964
965 end:
966 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn, qcs);
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200967}
968
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200969/* Install the <app_ops> applicative layer of a QUIC connection on mux <qcc>.
970 * Returns 0 on success else non-zero.
971 */
972int qcc_install_app_ops(struct qcc *qcc, const struct qcc_app_ops *app_ops)
973{
974 TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn);
975
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +0100976 if (app_ops->init && !app_ops->init(qcc)) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200977 TRACE_ERROR("app ops init error", QMUX_EV_QCC_NEW, qcc->conn);
978 goto err;
979 }
980
981 TRACE_PROTO("application layer initialized", QMUX_EV_QCC_NEW, qcc->conn);
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +0100982 qcc->app_ops = app_ops;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200983
Amaury Denoyelle71fd0362023-01-24 17:35:37 +0100984 /* RFC 9114 7.2.4.2. Initialization
985 *
986 * Endpoints MUST NOT require any data to be
987 * received from the peer prior to sending the SETTINGS frame;
988 * settings MUST be sent as soon as the transport is ready to
989 * send data.
990 */
991 if (qcc->app_ops->finalize) {
992 if (qcc->app_ops->finalize(qcc->ctx)) {
993 TRACE_ERROR("app ops finalize error", QMUX_EV_QCC_NEW, qcc->conn);
994 goto err;
995 }
996 tasklet_wakeup(qcc->wait_event.tasklet);
997 }
998
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200999 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
1000 return 0;
1001
1002 err:
1003 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
1004 return 1;
1005}
1006
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001007/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
1008 * <data> with length <len> and represents the offset <offset>. <fin> is set if
1009 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001010 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +02001011 * Returns 0 on success else non-zero. On error, the received frame should not
1012 * be acknowledged.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001013 */
1014int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001015 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001016{
1017 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001018 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001019
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001020 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1021
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001022 if (qcc->flags & QC_CF_ERRL) {
1023 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001024 goto err;
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001025 }
1026
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +02001027 /* RFC 9000 19.8. STREAM Frames
1028 *
1029 * An endpoint MUST terminate the connection with error
1030 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
1031 * initiated stream that has not yet been created, or for a send-only
1032 * stream.
1033 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +02001034 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001035 TRACE_DATA("qcs retrieval error", QMUX_EV_QCC_RECV, qcc->conn);
1036 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +02001037 }
1038
1039 if (!qcs) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001040 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV, qcc->conn);
1041 goto out;
Amaury Denoyelle57161b72022-07-07 15:02:32 +02001042 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001043
Amaury Denoyellebf91e392022-07-04 10:02:04 +02001044 /* RFC 9000 4.5. Stream Final Size
1045 *
1046 * Once a final size for a stream is known, it cannot change. If a
1047 * RESET_STREAM or STREAM frame is received indicating a change in the
1048 * final size for the stream, an endpoint SHOULD respond with an error
1049 * of type FINAL_SIZE_ERROR; see Section 11 for details on error
1050 * handling.
1051 */
1052 if (qcs->flags & QC_SF_SIZE_KNOWN &&
1053 (offset + len > qcs->rx.offset_max || (fin && offset + len < qcs->rx.offset_max))) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +02001054 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 +02001055 qcc_set_error(qcc, QC_ERR_FINAL_SIZE_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001056 goto err;
Amaury Denoyellebf91e392022-07-04 10:02:04 +02001057 }
1058
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001059 if (qcs_is_close_remote(qcs)) {
1060 TRACE_DATA("skipping STREAM for remotely closed", QMUX_EV_QCC_RECV, qcc->conn);
1061 goto out;
1062 }
1063
Amaury Denoyellefa241932023-02-14 15:36:36 +01001064 if (offset + len < qcs->rx.offset ||
1065 (offset + len == qcs->rx.offset && (!fin || (qcs->flags & QC_SF_SIZE_KNOWN)))) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001066 TRACE_DATA("already received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1067 goto out;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001068 }
1069
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001070 TRACE_PROTO("receiving STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001071 qcs_idle_open(qcs);
1072
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001073 if (offset + len > qcs->rx.offset_max) {
1074 uint64_t diff = offset + len - qcs->rx.offset_max;
1075 qcs->rx.offset_max = offset + len;
1076 qcc->lfctl.offsets_recv += diff;
1077
1078 if (offset + len > qcs->rx.msd ||
1079 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
1080 /* RFC 9000 4.1. Data Flow Control
1081 *
1082 * A receiver MUST close the connection with an error
1083 * of type FLOW_CONTROL_ERROR if the sender violates
1084 * the advertised connection or stream data limits
1085 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +02001086 TRACE_ERROR("flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001087 qcc->conn, qcs);
Amaury Denoyelle58721f22023-05-09 18:01:09 +02001088 qcc_set_error(qcc, QC_ERR_FLOW_CONTROL_ERROR, 0);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001089 goto err;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001090 }
1091 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001092
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001093 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelled00b3092023-05-11 17:00:54 +02001094 TRACE_ERROR("receive ncbuf alloc failure", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1095 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
1096 goto err;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001097 }
1098
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001099 TRACE_DATA("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001100 if (offset < qcs->rx.offset) {
Frédéric Lécaillea18c3332022-07-04 09:54:58 +02001101 size_t diff = qcs->rx.offset - offset;
1102
1103 len -= diff;
1104 data += diff;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001105 offset = qcs->rx.offset;
1106 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001107
Amaury Denoyellefa241932023-02-14 15:36:36 +01001108 if (len) {
1109 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
1110 switch (ret) {
1111 case NCB_RET_OK:
1112 break;
1113
1114 case NCB_RET_DATA_REJ:
Amaury Denoyellecc3d7162022-05-20 15:14:57 +02001115 /* RFC 9000 2.2. Sending and Receiving Data
1116 *
1117 * An endpoint could receive data for a stream at the
1118 * same stream offset multiple times. Data that has
1119 * already been received can be discarded. The data at
1120 * a given offset MUST NOT change if it is sent
1121 * multiple times; an endpoint MAY treat receipt of
1122 * different data at the same offset within a stream as
1123 * a connection error of type PROTOCOL_VIOLATION.
1124 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +02001125 TRACE_ERROR("overlapping data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001126 qcc->conn, qcs);
Amaury Denoyelle58721f22023-05-09 18:01:09 +02001127 qcc_set_error(qcc, QC_ERR_PROTOCOL_VIOLATION, 0);
Amaury Denoyellefa241932023-02-14 15:36:36 +01001128 return 1;
1129
1130 case NCB_RET_GAP_SIZE:
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001131 TRACE_DATA("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
1132 qcc->conn, qcs);
Amaury Denoyellefa241932023-02-14 15:36:36 +01001133 return 1;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001134 }
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001135 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001136
1137 if (fin)
Amaury Denoyelle3f39b402022-07-01 16:11:03 +02001138 qcs->flags |= QC_SF_SIZE_KNOWN;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001139
Amaury Denoyelled1310f82022-09-16 13:30:59 +02001140 if (qcs->flags & QC_SF_SIZE_KNOWN &&
1141 qcs->rx.offset_max == qcs->rx.offset + ncb_data(&qcs->rx.ncbuf, 0)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001142 qcs_close_remote(qcs);
Amaury Denoyelled1310f82022-09-16 13:30:59 +02001143 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001144
Amaury Denoyellefa241932023-02-14 15:36:36 +01001145 if ((ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) || fin) {
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001146 qcc_decode_qcs(qcc, qcs);
Amaury Denoyelle418ba212022-08-02 15:57:16 +02001147 qcc_refresh_timeout(qcc);
1148 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001149
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001150 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001151 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001152 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001153
1154 err:
1155 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1156 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001157}
1158
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001159/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
1160 * the frame.
1161 *
1162 * Returns 0 on success else non-zero.
1163 */
1164int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
1165{
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001166 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1167
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001168 TRACE_PROTO("receiving MAX_DATA", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001169 if (qcc->rfctl.md < max) {
1170 qcc->rfctl.md = max;
Amaury Denoyelleb7143a82023-03-22 15:08:01 +01001171 TRACE_DATA("increase remote max-data", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001172
1173 if (qcc->flags & QC_CF_BLK_MFCTL) {
1174 qcc->flags &= ~QC_CF_BLK_MFCTL;
1175 tasklet_wakeup(qcc->wait_event.tasklet);
1176 }
1177 }
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001178
1179 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001180 return 0;
1181}
1182
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001183/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
1184 * field of the frame and <id> is the identifier of the QUIC stream.
1185 *
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001186 * Returns 0 on success else non-zero. On error, the received frame should not
1187 * be acknowledged.
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001188 */
1189int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
1190{
1191 struct qcs *qcs;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001192
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001193 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 Denoyelleb68559a2022-07-06 15:45:20 +02001200 /* RFC 9000 19.10. MAX_STREAM_DATA Frames
1201 *
1202 * Receiving a MAX_STREAM_DATA frame for a locally
1203 * initiated stream that has not yet been created MUST be treated as a
1204 * connection error of type STREAM_STATE_ERROR. An endpoint that
1205 * receives a MAX_STREAM_DATA frame for a receive-only stream MUST
1206 * terminate the connection with error STREAM_STATE_ERROR.
1207 */
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001208 if (qcc_get_qcs(qcc, id, 0, 1, &qcs))
1209 goto err;
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001210
1211 if (qcs) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001212 TRACE_PROTO("receiving MAX_STREAM_DATA", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001213 if (max > qcs->tx.msd) {
1214 qcs->tx.msd = max;
Amaury Denoyelleb7143a82023-03-22 15:08:01 +01001215 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 +01001216
1217 if (qcs->flags & QC_SF_BLK_SFCTL) {
1218 qcs->flags &= ~QC_SF_BLK_SFCTL;
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001219 /* TODO optim: only wakeup IO-CB if stream has data to sent. */
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001220 tasklet_wakeup(qcc->wait_event.tasklet);
1221 }
1222 }
1223 }
1224
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001225 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1226 qcc_refresh_timeout(qcc);
1227
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001228 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1229 return 0;
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001230
1231 err:
1232 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
1233 return 1;
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001234}
1235
1236/* Handle a new RESET_STREAM frame from stream ID <id> with error code <err>
1237 * and final stream size <final_size>.
1238 *
1239 * Returns 0 on success else non-zero. On error, the received frame should not
1240 * be acknowledged.
1241 */
1242int qcc_recv_reset_stream(struct qcc *qcc, uint64_t id, uint64_t err, uint64_t final_size)
1243{
1244 struct qcs *qcs;
1245
1246 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1247
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001248 if (qcc->flags & QC_CF_ERRL) {
1249 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001250 goto err;
1251 }
1252
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001253 /* RFC 9000 19.4. RESET_STREAM Frames
1254 *
1255 * An endpoint that receives a RESET_STREAM frame for a send-only stream
1256 * MUST terminate the connection with error STREAM_STATE_ERROR.
1257 */
1258 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
1259 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 +02001260 qcc_set_error(qcc, QC_ERR_STREAM_STATE_ERROR, 0);
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001261 goto err;
1262 }
1263
Amaury Denoyelle3cb78142023-05-15 11:31:20 +02001264 /* RFC 9000 3.2. Receiving Stream States
1265 *
1266 * A RESET_STREAM signal might be suppressed or withheld
1267 * if stream data is completely received and is buffered to be read by
1268 * the application. If the RESET_STREAM is suppressed, the receiving
1269 * part of the stream remains in "Data Recvd".
1270 */
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001271 if (!qcs || qcs_is_close_remote(qcs))
1272 goto out;
1273
1274 TRACE_PROTO("receiving RESET_STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1275 qcs_idle_open(qcs);
1276
Amaury Denoyelle3cb78142023-05-15 11:31:20 +02001277 /* Ensure stream closure is not forbidden by application protocol. */
Amaury Denoyellee269aeb2023-01-30 12:13:22 +01001278 if (qcc->app_ops->close) {
1279 if (qcc->app_ops->close(qcs, QCC_APP_OPS_CLOSE_SIDE_RD)) {
1280 TRACE_ERROR("closure rejected by app layer", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1281 goto out;
1282 }
1283 }
1284
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001285 if (qcs->rx.offset_max > final_size ||
1286 ((qcs->flags & QC_SF_SIZE_KNOWN) && qcs->rx.offset_max != final_size)) {
1287 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 +02001288 qcc_set_error(qcc, QC_ERR_FINAL_SIZE_ERROR, 0);
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001289 goto err;
1290 }
1291
Amaury Denoyelle3cb78142023-05-15 11:31:20 +02001292 /* RFC 9000 3.2. Receiving Stream States
1293 *
1294 * An
1295 * implementation MAY interrupt delivery of stream data, discard any
1296 * data that was not consumed, and signal the receipt of the
1297 * RESET_STREAM.
1298 */
1299 qcs->flags |= QC_SF_SIZE_KNOWN|QC_SF_RECV_RESET;
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001300 qcs_close_remote(qcs);
1301 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
1302
1303 if (qcs_sc(qcs)) {
1304 se_fl_set_error(qcs->sd);
1305 qcs_alert(qcs);
1306 }
1307
1308 out:
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001309 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001310 return 0;
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001311
1312 err:
1313 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1314 return 1;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001315}
1316
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001317/* Handle a new STOP_SENDING frame for stream ID <id>. The error code should be
1318 * specified in <err>.
1319 *
1320 * Returns 0 on success else non-zero. On error, the received frame should not
1321 * be acknowledged.
1322 */
1323int qcc_recv_stop_sending(struct qcc *qcc, uint64_t id, uint64_t err)
1324{
1325 struct qcs *qcs;
1326
1327 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1328
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001329 if (qcc->flags & QC_CF_ERRL) {
1330 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001331 goto err;
1332 }
1333
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001334 /* RFC 9000 19.5. STOP_SENDING Frames
1335 *
1336 * Receiving a STOP_SENDING frame for a
1337 * locally initiated stream that has not yet been created MUST be
1338 * treated as a connection error of type STREAM_STATE_ERROR. An
1339 * endpoint that receives a STOP_SENDING frame for a receive-only stream
1340 * MUST terminate the connection with error STREAM_STATE_ERROR.
1341 */
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001342 if (qcc_get_qcs(qcc, id, 0, 1, &qcs))
1343 goto err;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001344
1345 if (!qcs)
1346 goto out;
1347
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001348 TRACE_PROTO("receiving STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelled7755372022-10-03 17:20:31 +02001349
1350 /* RFC 9000 3.5. Solicited State Transitions
1351 *
1352 * An endpoint is expected to send another STOP_SENDING frame if a
1353 * packet containing a previous STOP_SENDING is lost. However, once
1354 * either all stream data or a RESET_STREAM frame has been received for
1355 * the stream -- that is, the stream is in any state other than "Recv"
1356 * or "Size Known" -- sending a STOP_SENDING frame is unnecessary.
1357 */
1358
1359 /* TODO thanks to previous RFC clause, STOP_SENDING is ignored if current stream
1360 * has already been closed locally. This is useful to not emit multiple
1361 * RESET_STREAM for a single stream. This is functional if stream is
1362 * locally closed due to all data transmitted, but in this case the RFC
1363 * advices to use an explicit RESET_STREAM.
1364 */
1365 if (qcs_is_close_local(qcs)) {
1366 TRACE_STATE("ignoring STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1367 goto out;
1368 }
1369
Amaury Denoyelle96ca1b72022-08-09 17:36:38 +02001370 qcs_idle_open(qcs);
1371
Amaury Denoyelle87f87662023-01-30 12:12:43 +01001372 if (qcc->app_ops->close) {
1373 if (qcc->app_ops->close(qcs, QCC_APP_OPS_CLOSE_SIDE_WR)) {
1374 TRACE_ERROR("closure rejected by app layer", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1375 goto out;
1376 }
1377 }
1378
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001379 /* RFC 9000 3.5. Solicited State Transitions
1380 *
1381 * An endpoint that receives a STOP_SENDING frame
1382 * MUST send a RESET_STREAM frame if the stream is in the "Ready" or
1383 * "Send" state. If the stream is in the "Data Sent" state, the
1384 * endpoint MAY defer sending the RESET_STREAM frame until the packets
1385 * containing outstanding data are acknowledged or declared lost. If
1386 * any outstanding data is declared lost, the endpoint SHOULD send a
1387 * RESET_STREAM frame instead of retransmitting the data.
1388 *
1389 * An endpoint SHOULD copy the error code from the STOP_SENDING frame to
1390 * the RESET_STREAM frame it sends, but it can use any application error
1391 * code.
1392 */
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001393 qcc_reset_stream(qcs, err);
1394
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001395 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1396 qcc_refresh_timeout(qcc);
1397
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001398 out:
1399 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1400 return 0;
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001401
1402 err:
1403 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
1404 return 1;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001405}
1406
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001407/* Signal the closing of remote stream with id <id>. Flow-control for new
1408 * streams may be allocated for the peer if needed.
1409 */
1410static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +01001411{
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001412 struct quic_frame *frm;
1413
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001414 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn);
1415
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001416 if (quic_stream_is_bidi(id)) {
1417 ++qcc->lfctl.cl_bidi_r;
1418 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001419 TRACE_DATA("increase max stream limit with MAX_STREAMS_BIDI", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001420 frm = qc_frm_alloc(QUIC_FT_MAX_STREAMS_BIDI);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +01001421 if (!frm) {
Amaury Denoyelle58721f22023-05-09 18:01:09 +02001422 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +01001423 goto err;
1424 }
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001425
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001426 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
1427 qcc->lfctl.cl_bidi_r;
1428 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
1429 tasklet_wakeup(qcc->wait_event.tasklet);
1430
1431 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
1432 qcc->lfctl.cl_bidi_r = 0;
1433 }
1434 }
1435 else {
Amaury Denoyelle91077312022-12-22 18:56:09 +01001436 /* TODO unidirectional stream flow control with MAX_STREAMS_UNI
1437 * emission not implemented. It should be unnecessary for
1438 * HTTP/3 but may be required if other application protocols
1439 * are supported.
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02001440 */
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001441 }
1442
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001443 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
1444
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001445 return 0;
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +01001446
1447 err:
1448 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_END, qcc->conn);
1449 return 1;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001450}
1451
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05001452/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001453static void qcs_destroy(struct qcs *qcs)
1454{
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001455 struct qcc *qcc = qcs->qcc;
1456 struct connection *conn = qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001457 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001458
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001459 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001460
Amaury Denoyelle178fbff2023-03-22 11:17:59 +01001461 /* MUST not removed a stream with sending prepared data left. This is
1462 * to ensure consistency on connection flow-control calculation.
1463 */
1464 BUG_ON(qcs->tx.offset < qcs->tx.sent_offset);
1465
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001466 if (!(qcc->flags & QC_CF_ERRL)) {
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001467 if (quic_stream_is_remote(qcc, id))
1468 qcc_release_remote_stream(qcc, id);
1469 }
Amaury Denoyellec055e302022-02-07 16:09:06 +01001470
Amaury Denoyelledccbd732022-03-29 18:36:59 +02001471 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001472
1473 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001474}
1475
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001476/* Transfer as much as possible data on <qcs> from <in> to <out>. This is done
1477 * in respect with available flow-control at stream and connection level.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001478 *
Amaury Denoyelle0abde9d2023-05-11 16:52:17 +02001479 * Returns the total bytes of transferred data or a negative error code.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001480 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001481static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, struct buffer *in)
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001482{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001483 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001484 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001485 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001486
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001487 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001488
Amaury Denoyelle0abde9d2023-05-11 16:52:17 +02001489 if (!qc_get_buf(qcs, out)) {
1490 TRACE_ERROR("buffer alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1491 goto err;
1492 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001493
1494 /*
1495 * QCS out buffer diagram
1496 * head left to_xfer
1497 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001498 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001499 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001500 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001501 * ^ ack-off ^ sent-off ^ off
1502 *
1503 * STREAM frame
1504 * ^ ^
1505 * |xxxxxxxxxxxxxxxxx|
1506 */
1507
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001508 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001509 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001510 BUG_ON_HOT(qcc->tx.offsets < qcc->tx.sent_offsets);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001511
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001512 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001513 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001514
1515 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
1516 /* do not exceed flow control limit */
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001517 if (qcs->tx.offset + to_xfer > qcs->tx.msd) {
1518 TRACE_DATA("do not exceed stream flow control", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001519 to_xfer = qcs->tx.msd - qcs->tx.offset;
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001520 }
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001521
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001522 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001523 /* do not overcome flow control limit on connection */
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001524 if (qcc->tx.offsets + to_xfer > qcc->rfctl.md) {
1525 TRACE_DATA("do not exceed conn flow control", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001526 to_xfer = qcc->rfctl.md - qcc->tx.offsets;
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001527 }
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001528
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001529 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001530 goto out;
1531
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001532 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001533
1534 out:
1535 {
1536 struct qcs_xfer_data_trace_arg arg = {
1537 .prep = b_data(out), .xfer = total,
1538 };
1539 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
1540 qcc->conn, qcs, &arg);
1541 }
1542
1543 return total;
Amaury Denoyelle0abde9d2023-05-11 16:52:17 +02001544
1545 err:
1546 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1547 return -1;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001548}
1549
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001550/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
1551 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001552 * stream frame. If <out> is NULL an empty STREAM frame is built : this may be
1553 * useful if FIN needs to be sent without any data left.
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001554 *
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001555 * Returns the payload length of the STREAM frame or a negative error code.
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001556 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001557static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
1558 struct list *frm_list)
1559{
1560 struct qcc *qcc = qcs->qcc;
1561 struct quic_frame *frm;
1562 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001563 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001564
1565 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1566
Amaury Denoyellea4569202022-04-15 17:29:25 +02001567 /* if ack_offset < buf_offset, it points to an older buffer. */
1568 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
1569 BUG_ON(qcs->tx.sent_offset < base_off);
1570
1571 head = qcs->tx.sent_offset - base_off;
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001572 total = out ? b_data(out) - head : 0;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001573 BUG_ON(total < 0);
1574
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001575 if (!total && !fin) {
1576 /* No need to send anything if total is NULL and no FIN to signal. */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001577 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1578 return 0;
1579 }
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001580 BUG_ON((!total && qcs->tx.sent_offset > qcs->tx.offset) ||
1581 (total && qcs->tx.sent_offset >= qcs->tx.offset));
Amaury Denoyellea4569202022-04-15 17:29:25 +02001582 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001583 BUG_ON(qcc->tx.sent_offsets + total > qcc->rfctl.md);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001584
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001585 TRACE_PROTO("sending STREAM frame", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001586 frm = qc_frm_alloc(QUIC_FT_STREAM_8);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001587 if (!frm) {
1588 TRACE_ERROR("frame alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001589 goto err;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001590 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001591
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001592 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001593 frm->stream.id = qcs->id;
Amaury Denoyelle1dac0182023-02-02 16:45:07 +01001594 frm->stream.offset.key = 0;
Amaury Denoyelleebfafc22023-03-07 18:07:08 +01001595 frm->stream.dup = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001596
Amaury Denoyelle42c5b752023-04-25 16:39:32 +02001597 if (total) {
1598 frm->stream.buf = out;
1599 frm->stream.data = (unsigned char *)b_peek(out, head);
1600 }
1601 else {
1602 /* Empty STREAM frame. */
1603 frm->stream.buf = NULL;
1604 frm->stream.data = NULL;
1605 }
1606
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +01001607 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001608 if (fin)
1609 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001610
1611 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001612 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001613 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001614 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001615
Amaury Denoyelle42c5b752023-04-25 16:39:32 +02001616 /* Always set length bit as we do not know if there is remaining frames
1617 * in the final packet after this STREAM.
1618 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001619 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
1620 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001621
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001622 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001623
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001624 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001625 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001626 struct qcs_build_stream_trace_arg arg = {
1627 .len = frm->stream.len, .fin = fin,
1628 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001629 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001630 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001631 qcc->conn, qcs, &arg);
1632 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001633
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001634 return total;
1635
1636 err:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001637 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001638 return -1;
1639}
1640
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001641/* Check after transferring data from qcs.tx.buf if FIN must be set on the next
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001642 * STREAM frame for <qcs>.
1643 *
1644 * Returns true if FIN must be set else false.
1645 */
1646static int qcs_stream_fin(struct qcs *qcs)
1647{
1648 return qcs->flags & QC_SF_FIN_STREAM && !b_data(&qcs->tx.buf);
1649}
1650
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001651/* Return true if <qcs> has data to send in new STREAM frames. */
1652static forceinline int qcs_need_sending(struct qcs *qcs)
1653{
1654 return b_data(&qcs->tx.buf) || qcs->tx.sent_offset < qcs->tx.offset ||
1655 qcs_stream_fin(qcs);
1656}
1657
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001658/* This function must be called by the upper layer to inform about the sending
1659 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
1660 * <offset>.
1661 */
1662void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
1663{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001664 struct qcc *qcc = qcs->qcc;
1665 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001666
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001667 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1668
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001669 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001670 BUG_ON(offset + data > qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001671
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001672 /* check if the STREAM frame has already been notified. It can happen
1673 * for retransmission.
1674 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001675 if (offset + data < qcs->tx.sent_offset) {
1676 TRACE_DEVEL("offset already notified", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1677 goto out;
1678 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001679
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001680 qcs_idle_open(qcs);
1681
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001682 diff = offset + data - qcs->tx.sent_offset;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001683 if (diff) {
1684 /* increase offset sum on connection */
1685 qcc->tx.sent_offsets += diff;
1686 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001687 if (qcc->tx.sent_offsets == qcc->rfctl.md) {
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001688 qcc->flags |= QC_CF_BLK_MFCTL;
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001689 TRACE_STATE("connection flow-control reached", QMUX_EV_QCS_SEND, qcc->conn);
1690 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001691
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001692 /* increase offset on stream */
1693 qcs->tx.sent_offset += diff;
1694 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
1695 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001696 if (qcs->tx.sent_offset == qcs->tx.msd) {
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001697 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001698 TRACE_STATE("stream flow-control reached", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1699 }
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001700
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01001701 /* If qcs.stream.buf is full, release it to the lower layer. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001702 if (qcs->tx.offset == qcs->tx.sent_offset &&
1703 b_full(&qcs->stream->buf->buf)) {
1704 qc_stream_buf_release(qcs->stream);
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001705 }
Amaury Denoyelle1bcb6952023-04-28 16:24:44 +02001706
1707 /* Add measurement for send rate. This is done at the MUX layer
1708 * to account only for STREAM frames without retransmission.
Amaury Denoyelle1bcb6952023-04-28 16:24:44 +02001709 */
Amaury Denoyellebc0adfa2023-04-28 16:46:11 +02001710 increment_send_rate(diff, 0);
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001711 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001712
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001713 if (qcs->tx.offset == qcs->tx.sent_offset && !b_data(&qcs->tx.buf)) {
1714 /* Remove stream from send_list if all was sent. */
1715 LIST_DEL_INIT(&qcs->el_send);
1716 TRACE_STATE("stream sent done", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1717
1718 if (qcs->flags & (QC_SF_FIN_STREAM|QC_SF_DETACH)) {
1719 /* Close stream locally. */
1720 qcs_close_local(qcs);
1721 /* Reset flag to not emit multiple FIN STREAM frames. */
1722 qcs->flags &= ~QC_SF_FIN_STREAM;
1723 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001724 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001725
1726 out:
1727 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001728}
1729
Amaury Denoyelleb35e32e2023-05-03 09:50:25 +02001730/* Returns true if subscribe set, false otherwise. */
1731static int qcc_subscribe_send(struct qcc *qcc)
1732{
1733 struct connection *conn = qcc->conn;
Amaury Denoyelleb2e31d32023-05-10 11:57:40 +02001734
1735 /* Do not subscribe if lower layer in error. */
1736 if (conn->flags & CO_FL_ERROR)
1737 return 0;
1738
Amaury Denoyelleb35e32e2023-05-03 09:50:25 +02001739 if (qcc->wait_event.events & SUB_RETRY_SEND)
1740 return 1;
1741
1742 TRACE_DEVEL("subscribe for send", QMUX_EV_QCC_SEND, qcc->conn);
1743 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &qcc->wait_event);
1744 return 1;
1745}
1746
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001747/* Wrapper for send on transport layer. Send a list of frames <frms> for the
1748 * connection <qcc>.
1749 *
1750 * Returns 0 if all data sent with success else non-zero.
1751 */
1752static int qc_send_frames(struct qcc *qcc, struct list *frms)
1753{
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001754 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
1755
1756 if (LIST_ISEMPTY(frms)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001757 TRACE_DEVEL("no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
1758 goto err;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001759 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +01001760
Amaury Denoyellecaa16542023-02-28 15:11:26 +01001761 if (!qc_send_mux(qcc->conn->handle.qc, frms)) {
Amaury Denoyelleb35e32e2023-05-03 09:50:25 +02001762 TRACE_DEVEL("error on sending", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelleb35e32e2023-05-03 09:50:25 +02001763 qcc_subscribe_send(qcc);
Amaury Denoyelle036cc5d2022-09-26 15:02:31 +02001764 goto err;
Amaury Denoyellecaa16542023-02-28 15:11:26 +01001765 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001766
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001767 /* If there is frames left at this stage, transport layer is blocked.
1768 * Subscribe on it to retry later.
1769 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001770 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelleb35e32e2023-05-03 09:50:25 +02001771 TRACE_DEVEL("remaining frames to send", QMUX_EV_QCC_SEND, qcc->conn);
1772 qcc_subscribe_send(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001773 goto err;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001774 }
1775
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001776 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001777 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001778
1779 err:
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02001780 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001781 return 1;
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001782}
1783
1784/* Emit a RESET_STREAM on <qcs>.
1785 *
1786 * Returns 0 if the frame has been successfully sent else non-zero.
1787 */
1788static int qcs_send_reset(struct qcs *qcs)
1789{
1790 struct list frms = LIST_HEAD_INIT(frms);
1791 struct quic_frame *frm;
1792
1793 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1794
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001795 frm = qc_frm_alloc(QUIC_FT_RESET_STREAM);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001796 if (!frm) {
1797 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001798 return 1;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001799 }
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001800
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001801 frm->reset_stream.id = qcs->id;
1802 frm->reset_stream.app_error_code = qcs->err;
1803 frm->reset_stream.final_size = qcs->tx.sent_offset;
1804
1805 LIST_APPEND(&frms, &frm->list);
1806 if (qc_send_frames(qcs->qcc, &frms)) {
Amaury Denoyelle131f2d92023-05-09 14:10:55 +02001807 if (!LIST_ISEMPTY(&frms))
1808 qc_frm_free(&frm);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001809 TRACE_DEVEL("cannot send RESET_STREAM", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1810 return 1;
1811 }
1812
1813 if (qcs_sc(qcs)) {
1814 se_fl_set_error(qcs->sd);
1815 qcs_alert(qcs);
1816 }
1817
1818 qcs_close_local(qcs);
1819 qcs->flags &= ~QC_SF_TO_RESET;
1820
1821 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001822 return 0;
1823}
1824
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001825/* Emit a STOP_SENDING on <qcs>.
1826 *
1827 * Returns 0 if the frame has been successfully sent else non-zero.
1828 */
1829static int qcs_send_stop_sending(struct qcs *qcs)
1830{
1831 struct list frms = LIST_HEAD_INIT(frms);
1832 struct quic_frame *frm;
1833 struct qcc *qcc = qcs->qcc;
1834
1835 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1836
1837 /* RFC 9000 3.3. Permitted Frame Types
1838 *
1839 * A
1840 * receiver MAY send a STOP_SENDING frame in any state where it has not
1841 * received a RESET_STREAM frame -- that is, states other than "Reset
1842 * Recvd" or "Reset Read". However, there is little value in sending a
1843 * STOP_SENDING frame in the "Data Recvd" state, as all stream data has
1844 * been received. A sender could receive either of these two types of
1845 * frames in any state as a result of delayed delivery of packets.¶
1846 */
1847 if (qcs_is_close_remote(qcs)) {
1848 TRACE_STATE("skip STOP_SENDING on remote already closed", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1849 goto done;
1850 }
1851
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001852 frm = qc_frm_alloc(QUIC_FT_STOP_SENDING);
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001853 if (!frm) {
1854 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1855 return 1;
1856 }
1857
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001858 frm->stop_sending.id = qcs->id;
1859 frm->stop_sending.app_error_code = qcs->err;
1860
1861 LIST_APPEND(&frms, &frm->list);
1862 if (qc_send_frames(qcs->qcc, &frms)) {
Amaury Denoyelle131f2d92023-05-09 14:10:55 +02001863 if (!LIST_ISEMPTY(&frms))
1864 qc_frm_free(&frm);
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001865 TRACE_DEVEL("cannot send STOP_SENDING", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1866 return 1;
1867 }
1868
1869 done:
1870 qcs->flags &= ~QC_SF_TO_STOP_SENDING;
1871
1872 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1873 return 0;
1874}
1875
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001876/* Used internally by qc_send function. Proceed to send for <qcs>. This will
1877 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001878 * is then generated and inserted in <frms> list.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001879 *
1880 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001881 * be null if out buffer cannot be allocated. On error a negative error code is
1882 * used.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001883 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001884static int _qc_send_qcs(struct qcs *qcs, struct list *frms)
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001885{
1886 struct qcc *qcc = qcs->qcc;
1887 struct buffer *buf = &qcs->tx.buf;
1888 struct buffer *out = qc_stream_buf_get(qcs->stream);
Amaury Denoyelle6c501ed2023-05-12 16:19:32 +02001889 int xfer = 0, buf_avail;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001890 char fin = 0;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001891
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001892 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1893
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001894 /* Cannot send STREAM on remote unidirectional streams. */
1895 BUG_ON(quic_stream_is_uni(qcs->id) && quic_stream_is_remote(qcc, qcs->id));
1896
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001897 if (b_data(buf)) {
1898 /* Allocate <out> buffer if not already done. */
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001899 if (!out) {
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001900 if (qcc->flags & QC_CF_CONN_FULL)
1901 goto out;
1902
Amaury Denoyelle6c501ed2023-05-12 16:19:32 +02001903 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset,
1904 &buf_avail);
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001905 if (!out) {
Amaury Denoyelle1611a762023-05-15 13:56:46 +02001906 if (buf_avail) {
Amaury Denoyelle6c501ed2023-05-12 16:19:32 +02001907 TRACE_ERROR("stream desc alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1908 goto err;
1909 }
1910
Amaury Denoyelle1611a762023-05-15 13:56:46 +02001911 TRACE_STATE("hitting stream desc buffer limit", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001912 qcc->flags |= QC_CF_CONN_FULL;
1913 goto out;
1914 }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001915 }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001916
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001917 /* Transfer data from <buf> to <out>. */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001918 xfer = qcs_xfer_data(qcs, out, buf);
Amaury Denoyelle0abde9d2023-05-11 16:52:17 +02001919 if (xfer < 0)
1920 goto err;
1921
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001922 if (xfer > 0) {
1923 qcs_notify_send(qcs);
1924 qcs->flags &= ~QC_SF_BLK_MROOM;
1925 }
1926
1927 qcs->tx.offset += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001928 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001929 qcc->tx.offsets += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001930 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001931
Amaury Denoyellea57ab0f2023-04-26 11:38:11 +02001932 /* out buffer cannot be emptied if qcs offsets differ. */
1933 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1934 }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001935
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001936 /* FIN is set if all incoming data were transferred. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001937 fin = qcs_stream_fin(qcs);
1938
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001939 /* Build a new STREAM frame with <out> buffer. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001940 if (qcs->tx.sent_offset != qcs->tx.offset || fin) {
Amaury Denoyelle04b22082023-05-03 09:50:39 +02001941 /* Skip STREAM frame allocation if already subscribed for send.
1942 * Happens on sendto transient error or network congestion.
1943 */
1944 if (qcc->wait_event.events & SUB_RETRY_SEND) {
1945 TRACE_DEVEL("already subscribed for sending",
1946 QMUX_EV_QCS_SEND, qcc->conn, qcs);
1947 goto err;
1948 }
1949
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001950 if (qcs_build_stream_frm(qcs, out, fin, frms) < 0)
1951 goto err;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001952 }
1953
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001954 out:
1955 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001956 return xfer;
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001957
1958 err:
1959 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1960 return -1;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001961}
1962
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001963/* Proceed to sending. Loop through all available streams for the <qcc>
1964 * instance and try to send as much as possible.
1965 *
1966 * Returns the total of bytes sent to the transport layer.
1967 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001968static int qc_send(struct qcc *qcc)
1969{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001970 struct list frms = LIST_HEAD_INIT(frms);
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001971 /* Temporary list for QCS on error. */
1972 struct list qcs_failed = LIST_HEAD_INIT(qcs_failed);
Amaury Denoyelle7c5591f2023-04-21 14:48:01 +02001973 struct qcs *qcs, *qcs_tmp, *first_qcs = NULL;
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02001974 int ret, total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001975
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001976 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001977
Amaury Denoyelle04b22082023-05-03 09:50:39 +02001978 /* TODO if socket in transient error, sending should be temporarily
1979 * disabled for all frames. However, checking for send subscription is
1980 * not valid as this may be caused by a congestion error which only
1981 * apply for STREAM frames.
1982 */
1983
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02001984 /* Check for transport error. */
1985 if (qcc->flags & QC_CF_ERR_CONN || qcc->conn->flags & CO_FL_ERROR) {
1986 TRACE_DEVEL("connection on error", QMUX_EV_QCC_SEND, qcc->conn);
1987 goto out;
1988 }
1989
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001990 /* Check for locally detected connection error. */
1991 if (qcc->flags & QC_CF_ERRL) {
1992 /* Prepare a CONNECTION_CLOSE if not already done. */
1993 if (!(qcc->flags & QC_CF_ERRL_DONE)) {
1994 TRACE_DATA("report a connection error", QMUX_EV_QCC_SEND|QMUX_EV_QCC_ERR, qcc->conn);
1995 quic_set_connection_close(qcc->conn->handle.qc, qcc->err);
1996 qcc->flags |= QC_CF_ERRL_DONE;
1997 }
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02001998 goto out;
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02001999 }
2000
2001 if (qcc->conn->flags & CO_FL_SOCK_WR_SH) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002002 qcc->conn->flags |= CO_FL_ERROR;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002003 TRACE_DEVEL("connection on error", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02002004 goto out;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002005 }
2006
Amaury Denoyellec985cb12022-05-16 14:29:59 +02002007 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
2008 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
2009 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
2010 goto out;
2011 }
2012 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02002013
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01002014 if (qcc->flags & QC_CF_BLK_MFCTL)
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02002015 goto out;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01002016
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01002017 /* Send STREAM/STOP_SENDING/RESET_STREAM data for registered streams. */
2018 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_list, el_send) {
Amaury Denoyelle7c5591f2023-04-21 14:48:01 +02002019 /* Check if all QCS were processed. */
2020 if (qcs == first_qcs)
2021 break;
2022
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01002023 /* Stream must not be present in send_list if it has nothing to send. */
2024 BUG_ON(!(qcs->flags & (QC_SF_TO_STOP_SENDING|QC_SF_TO_RESET)) &&
2025 !qcs_need_sending(qcs));
Amaury Denoyellec6195d72022-05-23 11:39:14 +02002026
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01002027 /* Each STOP_SENDING/RESET_STREAM frame is sent individually to
2028 * guarantee its emission.
2029 *
2030 * TODO multiplex several frames in same datagram to optimize sending
2031 */
2032 if (qcs->flags & QC_SF_TO_STOP_SENDING) {
2033 if (qcs_send_stop_sending(qcs))
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02002034 goto sent_done;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01002035
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01002036 /* Remove stream from send_list if it had only STOP_SENDING
2037 * to send.
2038 */
2039 if (!(qcs->flags & QC_SF_TO_RESET) && !qcs_need_sending(qcs)) {
2040 LIST_DEL_INIT(&qcs->el_send);
2041 continue;
2042 }
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01002043 }
2044
Amaury Denoyelle843a1192022-07-04 11:44:38 +02002045 if (qcs->flags & QC_SF_TO_RESET) {
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01002046 if (qcs_send_reset(qcs))
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02002047 goto sent_done;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002048
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01002049 /* RFC 9000 3.3. Permitted Frame Types
2050 *
2051 * A sender MUST NOT send
2052 * a STREAM or STREAM_DATA_BLOCKED frame for a stream in the
2053 * "Reset Sent" state or any terminal state -- that is, after
2054 * sending a RESET_STREAM frame.
2055 */
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01002056 LIST_DEL_INIT(&qcs->el_send);
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02002057 continue;
2058 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02002059
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02002060 if (!(qcs->flags & QC_SF_BLK_SFCTL)) {
2061 if ((ret = _qc_send_qcs(qcs, &frms)) < 0) {
2062 /* Temporarily remove QCS from send-list. */
2063 LIST_DEL_INIT(&qcs->el_send);
2064 LIST_APPEND(&qcs_failed, &qcs->el_send);
2065 continue;
2066 }
2067
2068 total += ret;
Amaury Denoyelle7c5591f2023-04-21 14:48:01 +02002069 if (ret) {
2070 /* Move QCS with some bytes transferred at the
2071 * end of send-list for next iterations.
2072 */
2073 LIST_DEL_INIT(&qcs->el_send);
2074 LIST_APPEND(&qcc->send_list, &qcs->el_send);
2075 /* Remember first moved QCS as checkpoint to interrupt loop */
2076 if (!first_qcs)
2077 first_qcs = qcs;
2078 }
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02002079 }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02002080 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02002081
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01002082 /* Retry sending until no frame to send, data rejected or connection
2083 * flow-control limit reached.
2084 */
2085 while (qc_send_frames(qcc, &frms) == 0 && !(qcc->flags & QC_CF_BLK_MFCTL)) {
2086 /* Reloop over <qcc.send_list>. Useful for streams which have
2087 * fulfilled their qc_stream_desc buf and have now release it.
2088 */
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02002089 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_list, el_send) {
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01002090 /* Only streams blocked on flow-control or waiting on a
2091 * new qc_stream_desc should be present in send_list as
2092 * long as transport layer can handle all data.
2093 */
2094 BUG_ON(qcs->stream->buf && !(qcs->flags & QC_SF_BLK_SFCTL));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02002095
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02002096 if (!(qcs->flags & QC_SF_BLK_SFCTL)) {
2097 if ((ret = _qc_send_qcs(qcs, &frms)) < 0) {
2098 LIST_DEL_INIT(&qcs->el_send);
2099 LIST_APPEND(&qcs_failed, &qcs->el_send);
2100 continue;
2101 }
2102
2103 total += ret;
2104 }
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01002105 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +02002106 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02002107
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02002108 sent_done:
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02002109 /* Deallocate frames that the transport layer has rejected. */
2110 if (!LIST_ISEMPTY(&frms)) {
2111 struct quic_frame *frm, *frm2;
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01002112
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01002113 list_for_each_entry_safe(frm, frm2, &frms, list)
2114 qc_frm_free(&frm);
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02002115 }
2116
Amaury Denoyelle93d2ebe2023-04-19 11:42:24 +02002117 /* Re-insert on-error QCS at the end of the send-list. */
2118 if (!LIST_ISEMPTY(&qcs_failed)) {
2119 list_for_each_entry_safe(qcs, qcs_tmp, &qcs_failed, el_send) {
2120 LIST_DEL_INIT(&qcs->el_send);
2121 LIST_APPEND(&qcc->send_list, &qcs->el_send);
2122 }
2123
2124 if (!(qcc->flags & QC_CF_BLK_MFCTL))
2125 tasklet_wakeup(qcc->wait_event.tasklet);
2126 }
2127
Amaury Denoyelle2ad41b82023-05-10 11:59:10 +02002128 out:
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002129 if (qcc->conn->flags & CO_FL_ERROR && !(qcc->flags & QC_CF_ERR_CONN)) {
2130 TRACE_ERROR("error reported by transport layer",
2131 QMUX_EV_QCC_SEND, qcc->conn);
2132 qcc->flags |= QC_CF_ERR_CONN;
2133 }
2134
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002135 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01002136 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002137}
2138
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002139/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
2140 * operation.
2141 *
2142 * Returns 0 on success else non-zero.
2143 */
2144static int qc_recv(struct qcc *qcc)
2145{
2146 struct eb64_node *node;
2147 struct qcs *qcs;
2148
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002149 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02002150
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002151 if (qcc->flags & QC_CF_ERRL) {
2152 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002153 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02002154 return 0;
2155 }
2156
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002157 node = eb64_first(&qcc->streams_by_id);
2158 while (node) {
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002159 uint64_t id;
2160
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002161 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002162 id = qcs->id;
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002163
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002164 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002165 node = eb64_next(node);
2166 continue;
2167 }
2168
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002169 if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002170 node = eb64_next(node);
2171 continue;
2172 }
2173
2174 qcc_decode_qcs(qcc, qcs);
2175 node = eb64_next(node);
2176 }
2177
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002178 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002179 return 0;
2180}
2181
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002182
2183/* Release all streams which have their transfer operation achieved.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01002184 *
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002185 * Returns true if at least one stream is released.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01002186 */
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002187static int qc_purge_streams(struct qcc *qcc)
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002188{
2189 struct eb64_node *node;
2190 int release = 0;
2191
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002192 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002193
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002194 node = eb64_first(&qcc->streams_by_id);
2195 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02002196 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002197 node = eb64_next(node);
2198
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002199 /* Release not attached closed streams. */
2200 if (qcs->st == QC_SS_CLO && !qcs_sc(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002201 TRACE_STATE("purging closed stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002202 qcs_destroy(qcs);
2203 release = 1;
2204 continue;
2205 }
2206
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002207 /* Release detached streams with empty buffer. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002208 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02002209 if (qcs_is_close_local(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002210 TRACE_STATE("purging detached stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002211 qcs_destroy(qcs);
2212 release = 1;
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002213 continue;
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002214 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002215 }
2216 }
2217
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002218 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002219 return release;
2220}
2221
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002222/* Execute application layer shutdown. If this operation is not defined, a
2223 * CONNECTION_CLOSE will be prepared as a fallback. This function is protected
2224 * against multiple invocation with the flag QC_CF_APP_SHUT.
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002225 */
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002226static void qc_shutdown(struct qcc *qcc)
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002227{
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002228 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002229
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002230 if (qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL)) {
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002231 TRACE_DATA("connection on error", QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002232 goto out;
Amaury Denoyelle665817a2023-03-20 17:34:22 +01002233 }
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002234
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002235 if (qcc->flags & QC_CF_APP_SHUT)
2236 goto out;
2237
2238 TRACE_STATE("perform graceful shutdown", QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02002239 if (qcc->app_ops && qcc->app_ops->shutdown) {
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02002240 qcc->app_ops->shutdown(qcc->ctx);
Amaury Denoyellea154dc02022-06-13 17:09:01 +02002241 qc_send(qcc);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002242 }
2243 else {
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002244 qcc->err = quic_err_app(QC_ERR_NO_ERROR);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002245 }
2246
Amaury Denoyelle51f116d2023-05-04 15:49:02 +02002247 /* Register "no error" code at transport layer. Do not use
2248 * quic_set_connection_close() as retransmission may be performed to
2249 * finalized transfers. Do not overwrite quic-conn existing code if
2250 * already set.
2251 *
2252 * TODO implement a wrapper function for this in quic-conn module
2253 */
2254 if (!(qcc->conn->handle.qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
2255 qcc->conn->handle.qc->err = qcc->err;
2256
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002257 out:
2258 qcc->flags |= QC_CF_APP_SHUT;
2259 TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn);
2260}
2261
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002262/* Loop through all qcs from <qcc>. Report error on stream endpoint if
2263 * connection on error and wake them.
2264 */
2265static int qc_wake_some_streams(struct qcc *qcc)
2266{
2267 struct qcs *qcs;
2268 struct eb64_node *node;
2269
2270 TRACE_POINT(QMUX_EV_QCC_WAKE, qcc->conn);
2271
2272 for (node = eb64_first(&qcc->streams_by_id); node;
2273 node = eb64_next(node)) {
2274 qcs = eb64_entry(node, struct qcs, by_id);
2275
2276 if (!qcs_sc(qcs))
2277 continue;
2278
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002279 if (qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL)) {
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002280 TRACE_POINT(QMUX_EV_QCC_WAKE, qcc->conn, qcs);
2281 se_fl_set_error(qcs->sd);
2282 qcs_alert(qcs);
2283 }
2284 }
2285
2286 return 0;
2287}
2288
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002289/* Conduct operations which should be made for <qcc> connection after
2290 * input/output. Most notably, closed streams are purged which may leave the
2291 * connection has ready to be released.
2292 *
2293 * Returns 1 if <qcc> must be released else 0.
2294 */
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002295static int qc_process(struct qcc *qcc)
2296{
2297 qc_purge_streams(qcc);
2298
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +01002299 /* Check if a soft-stop is in progress.
2300 *
2301 * TODO this is relevant for frontend connections only.
2302 */
2303 if (unlikely(qcc->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
2304 int close = 1;
2305
2306 /* If using listener socket, soft-stop is not supported. The
2307 * connection must be closed immediately.
2308 */
2309 if (!qc_test_fd(qcc->conn->handle.qc)) {
2310 TRACE_DEVEL("proxy disabled with listener socket, closing connection", QMUX_EV_QCC_WAKE, qcc->conn);
2311 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
2312 qc_send(qcc);
2313 goto out;
2314 }
2315
2316 TRACE_DEVEL("proxy disabled, prepare connection soft-stop", QMUX_EV_QCC_WAKE, qcc->conn);
2317
2318 /* If a close-spread-time option is set, we want to avoid
2319 * closing all the active HTTP3 connections at once so we add a
2320 * random factor that will spread the closing.
2321 */
2322 if (tick_isset(global.close_spread_end)) {
2323 int remaining_window = tick_remain(now_ms, global.close_spread_end);
2324 if (remaining_window) {
2325 /* This should increase the closing rate the
2326 * further along the window we are. */
2327 close = (remaining_window <= statistical_prng_range(global.close_spread_time));
2328 }
2329 }
2330 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE) {
2331 close = 0; /* let the client close his connection himself */
2332 }
2333
2334 if (close)
2335 qc_shutdown(qcc);
2336 }
2337
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002338 /* Report error if set on stream endpoint layer. */
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002339 if (qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL))
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002340 qc_wake_some_streams(qcc);
2341
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +01002342 out:
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002343 if (qcc_is_dead(qcc))
2344 return 1;
2345
2346 return 0;
2347}
2348
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002349/* release function. This one should be called to free all resources allocated
2350 * to the mux.
2351 */
2352static void qc_release(struct qcc *qcc)
2353{
2354 struct connection *conn = qcc->conn;
2355 struct eb64_node *node;
2356
2357 TRACE_ENTER(QMUX_EV_QCC_END, conn);
2358
2359 qc_shutdown(qcc);
2360
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002361 if (qcc->task) {
2362 task_destroy(qcc->task);
2363 qcc->task = NULL;
2364 }
2365
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +02002366 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002367 if (conn && qcc->wait_event.events) {
2368 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
2369 qcc->wait_event.events,
2370 &qcc->wait_event);
2371 }
2372
2373 /* liberate remaining qcs instances */
2374 node = eb64_first(&qcc->streams_by_id);
2375 while (node) {
2376 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
2377 node = eb64_next(node);
2378 qcs_free(qcs);
2379 }
2380
2381 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
2382 struct quic_frame *frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01002383 qc_frm_free(&frm);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002384 }
2385
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02002386 if (qcc->app_ops && qcc->app_ops->release)
2387 qcc->app_ops->release(qcc->ctx);
2388 TRACE_PROTO("application layer released", QMUX_EV_QCC_END, conn);
2389
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002390 pool_free(pool_head_qcc, qcc);
2391
2392 if (conn) {
2393 LIST_DEL_INIT(&conn->stopping_list);
2394
2395 conn->handle.qc->conn = NULL;
2396 conn->mux = NULL;
2397 conn->ctx = NULL;
2398
2399 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
2400
2401 conn_stop_tracking(conn);
2402 conn_full_close(conn);
2403 if (conn->destroy_cb)
2404 conn->destroy_cb(conn);
2405 conn_free(conn);
2406 }
2407
2408 TRACE_LEAVE(QMUX_EV_QCC_END);
2409}
2410
Willy Tarreau41e701e2022-09-08 15:12:59 +02002411struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002412{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02002413 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002414
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002415 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002416
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002417 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002418
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002419 qc_recv(qcc);
2420
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002421 if (qc_process(qcc)) {
2422 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
2423 goto release;
2424 }
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002425
2426 qcc_refresh_timeout(qcc);
2427
Amaury Denoyelled3973852022-07-25 14:56:54 +02002428 end:
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002429 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
2430 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002431
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002432 release:
2433 qc_release(qcc);
2434 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002435 return NULL;
2436}
2437
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002438static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
2439{
2440 struct qcc *qcc = ctx;
2441 int expired = tick_is_expired(t->expire, now_ms);
2442
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002443 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002444
2445 if (qcc) {
2446 if (!expired) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002447 TRACE_DEVEL("not expired", QMUX_EV_QCC_WAKE, qcc->conn);
2448 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002449 }
2450
2451 if (!qcc_may_expire(qcc)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002452 TRACE_DEVEL("cannot expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002453 t->expire = TICK_ETERNITY;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002454 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002455 }
2456 }
2457
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002458 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01002459
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002460 if (!qcc) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002461 TRACE_DEVEL("no more qcc", QMUX_EV_QCC_WAKE);
2462 goto out;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002463 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01002464
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002465 qcc->task = NULL;
2466
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002467 /* TODO depending on the timeout condition, different shutdown mode
2468 * should be used. For http keep-alive or disabled proxy, a graceful
2469 * shutdown should occurs. For all other cases, an immediate close
2470 * seems legitimate.
2471 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002472 if (qcc_is_dead(qcc)) {
2473 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002474 qc_release(qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002475 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002476
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002477 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002478 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002479 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002480
2481 requeue:
2482 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
2483 return t;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002484}
2485
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002486static int qc_init(struct connection *conn, struct proxy *prx,
2487 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002488{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002489 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002490 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002491
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002492 TRACE_ENTER(QMUX_EV_QCC_NEW);
2493
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002494 qcc = pool_alloc(pool_head_qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002495 if (!qcc) {
2496 TRACE_ERROR("alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002497 goto fail_no_qcc;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002498 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002499
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002500 qcc->conn = conn;
2501 conn->ctx = qcc;
Amaury Denoyellec603de42022-07-25 11:21:46 +02002502 qcc->nb_hreq = qcc->nb_sc = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01002503 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002504
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002505 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002506
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002507 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002508
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002509 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02002510 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002511
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002512 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02002513 qcc->tx.sent_offsets = qcc->tx.offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002514
2515 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002516 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002517 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002518 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002519 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002520
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002521 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002522 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002523 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002524 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002525
2526 /* Server initiated streams must respect the server flow control. */
2527 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002528 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002529 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002530 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
2531
2532 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002533 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002534 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002535 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002536
Amaury Denoyellec985cb12022-05-16 14:29:59 +02002537 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01002538 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02002539 qcc->lfctl.ms_uni = lparams->initial_max_streams_uni;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02002540 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
2541 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle176174f2022-10-21 17:02:18 +02002542 qcc->lfctl.msd_uni_r = lparams->initial_max_stream_data_uni;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01002543 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01002544
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02002545 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02002546 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02002547
Willy Tarreau784b8682022-04-11 14:18:10 +02002548 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01002549 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002550 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
2551 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle176174f2022-10-21 17:02:18 +02002552 qcc->rfctl.msd_uni_l = rparams->initial_max_stream_data_uni;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002553
Amaury Denoyellea509ffb2022-07-04 15:50:33 +02002554 if (conn_is_back(conn)) {
2555 qcc->next_bidi_l = 0x00;
2556 qcc->largest_bidi_r = 0x01;
2557 qcc->next_uni_l = 0x02;
2558 qcc->largest_uni_r = 0x03;
2559 }
2560 else {
2561 qcc->largest_bidi_r = 0x00;
2562 qcc->next_bidi_l = 0x01;
2563 qcc->largest_uni_r = 0x02;
2564 qcc->next_uni_l = 0x03;
2565 }
2566
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002567 qcc->wait_event.tasklet = tasklet_new();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002568 if (!qcc->wait_event.tasklet) {
2569 TRACE_ERROR("taslket alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002570 goto fail_no_tasklet;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002571 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002572
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01002573 LIST_INIT(&qcc->send_list);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02002574
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002575 qcc->wait_event.tasklet->process = qc_io_cb;
2576 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01002577 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002578
Amaury Denoyelle07bf8f42022-07-22 16:16:03 +02002579 qcc->proxy = prx;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002580 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002581 qcc->task = NULL;
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +01002582 if (conn_is_back(qcc->conn)) {
2583 qcc->timeout = prx->timeout.server;
2584 qcc->shut_timeout = tick_isset(prx->timeout.serverfin) ?
2585 prx->timeout.serverfin : prx->timeout.server;
2586 }
2587 else {
2588 qcc->timeout = prx->timeout.client;
2589 qcc->shut_timeout = tick_isset(prx->timeout.clientfin) ?
2590 prx->timeout.clientfin : prx->timeout.client;
2591 }
2592
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002593 if (tick_isset(qcc->timeout)) {
2594 qcc->task = task_new_here();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002595 if (!qcc->task) {
2596 TRACE_ERROR("timeout task alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002597 goto fail_no_timeout_task;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002598 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002599 qcc->task->process = qc_timeout_task;
2600 qcc->task->context = qcc;
2601 qcc->task->expire = tick_add(now_ms, qcc->timeout);
2602 }
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +02002603 qcc_reset_idle_start(qcc);
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02002604 LIST_INIT(&qcc->opening_list);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002605
Willy Tarreau784b8682022-04-11 14:18:10 +02002606 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +01002607
2608 if (qcc_install_app_ops(qcc, conn->handle.qc->app_ops)) {
Amaury Denoyelle8d44bfa2023-05-04 15:16:01 +02002609 TRACE_PROTO("Cannot install app layer", QMUX_EV_QCC_NEW|QMUX_EV_QCC_ERR, qcc->conn);
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +01002610 /* prepare a CONNECTION_CLOSE frame */
2611 quic_set_connection_close(conn->handle.qc, quic_err_transport(QC_ERR_APPLICATION_ERROR));
2612 goto fail_install_app_ops;
2613 }
2614
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01002615 if (qcc->app_ops == &h3_ops)
2616 proxy_inc_fe_cum_sess_ver_ctr(sess->listener, prx, 3);
2617
Amaury Denoyelleed820822023-04-19 17:58:39 +02002618 /* Register conn for idle front closing. This is done once everything is allocated. */
2619 if (!conn_is_back(conn))
2620 LIST_APPEND(&mux_stopping_data[tid].list, &conn->stopping_list);
2621
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002622 /* init read cycle */
2623 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002624
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002625 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002626 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002627
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +01002628 fail_install_app_ops:
2629 if (qcc->app_ops && qcc->app_ops->release)
2630 qcc->app_ops->release(qcc->ctx);
Amaury Denoyelleee65efb2023-05-12 16:29:48 +02002631 task_destroy(qcc->task);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002632 fail_no_timeout_task:
2633 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002634 fail_no_tasklet:
2635 pool_free(pool_head_qcc, qcc);
2636 fail_no_qcc:
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002637 TRACE_LEAVE(QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002638 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002639}
2640
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002641static void qc_destroy(void *ctx)
2642{
2643 struct qcc *qcc = ctx;
2644
2645 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
2646 qc_release(qcc);
2647 TRACE_LEAVE(QMUX_EV_QCC_END);
2648}
2649
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002650static void qc_detach(struct sedesc *sd)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002651{
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002652 struct qcs *qcs = sd->se;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002653 struct qcc *qcc = qcs->qcc;
2654
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002655 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002656
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002657 /* TODO this BUG_ON_HOT() is not correct as the stconn layer may detach
2658 * from the stream even if it is not closed remotely at the QUIC layer.
2659 * This happens for example when a stream must be closed due to a
2660 * rejected request. To better handle these cases, it will be required
2661 * to implement shutr/shutw MUX operations. Once this is done, this
2662 * BUG_ON_HOT() statement can be adjusted.
2663 */
2664 //BUG_ON_HOT(!qcs_is_close_remote(qcs));
Amaury Denoyellec603de42022-07-25 11:21:46 +02002665
2666 qcc_rm_sc(qcc);
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002667
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002668 if (!qcs_is_close_local(qcs) &&
2669 !(qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL))) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002670 TRACE_STATE("remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002671 qcs->flags |= QC_SF_DETACH;
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002672 qcc_refresh_timeout(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002673
2674 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002675 return;
2676 }
2677
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002678 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01002679
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002680 if (qcc_is_dead(qcc)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002681 TRACE_STATE("killing dead connection", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002682 goto release;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002683 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002684 else if (qcc->task) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002685 TRACE_DEVEL("refreshing connection's timeout", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002686 qcc_refresh_timeout(qcc);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002687 }
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002688 else {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002689 TRACE_DEVEL("completed", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002690 }
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002691
2692 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002693 return;
2694
2695 release:
2696 qc_release(qcc);
2697 TRACE_LEAVE(QMUX_EV_STRM_END);
2698 return;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002699}
2700
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002701/* Called from the upper layer, to receive data */
Amaury Denoyelleb7ce79c2022-11-24 10:51:19 +01002702static size_t qc_recv_buf(struct stconn *sc, struct buffer *buf,
2703 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002704{
Willy Tarreau3215e732022-05-27 10:09:11 +02002705 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle16494692023-05-15 11:35:45 +02002706 struct qcc *qcc = qcs->qcc;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002707 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002708 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002709
Amaury Denoyelle16494692023-05-15 11:35:45 +02002710 TRACE_ENTER(QMUX_EV_STRM_RECV, qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002711
Amaury Denoyelled80fbca2022-09-19 17:02:28 +02002712 ret = qcs_http_rcv_buf(qcs, buf, count, &fin);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002713
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002714 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002715 se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002716 }
2717 else {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002718 se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002719
Amaury Denoyelle72a78e82022-07-29 15:34:12 +02002720 /* Set end-of-input if FIN received and all data extracted. */
Christopher Faulet85eabfb2023-02-23 14:52:09 +01002721 if (fin) {
Amaury Denoyelle16494692023-05-15 11:35:45 +02002722 TRACE_STATE("report end-of-input", QMUX_EV_STRM_RECV, qcc->conn, qcs);
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002723 se_fl_set(qcs->sd, SE_FL_EOI);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002724
Christopher Faulet85eabfb2023-02-23 14:52:09 +01002725 /* If request EOM is reported to the upper layer, it means the
2726 * QCS now expects data from the opposite side.
2727 */
2728 se_expect_data(qcs->sd);
2729 }
2730
Amaury Denoyelle3cb78142023-05-15 11:31:20 +02002731 /* Set end-of-stream on read closed. */
2732 if (qcs->flags & QC_SF_RECV_RESET ||
2733 qcc->conn->flags & CO_FL_SOCK_RD_SH) {
2734 TRACE_STATE("report end-of-stream", QMUX_EV_STRM_RECV, qcc->conn, qcs);
2735 se_fl_set(qcs->sd, SE_FL_EOS);
2736
2737 /* Set error if EOI not reached. This may happen on
2738 * RESET_STREAM reception or connection error.
2739 */
2740 if (!se_fl_test(qcs->sd, SE_FL_EOI)) {
2741 TRACE_STATE("report error on stream aborted", QMUX_EV_STRM_RECV, qcc->conn, qcs);
2742 se_fl_set(qcs->sd, SE_FL_EOS | SE_FL_ERROR);
2743 }
2744 }
2745
Amaury Denoyelle16494692023-05-15 11:35:45 +02002746 if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING)) {
2747 TRACE_STATE("report error", QMUX_EV_STRM_RECV, qcc->conn, qcs);
2748 se_fl_set(qcs->sd, SE_FL_ERROR);
2749 }
2750
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002751 if (b_size(&qcs->rx.app_buf)) {
2752 b_free(&qcs->rx.app_buf);
2753 offer_buffers(NULL, 1);
2754 }
2755 }
2756
Amaury Denoyelleb8901d22023-05-03 15:30:04 +02002757 /* Restart demux if it was interrupted on full buffer. */
2758 if (ret && qcs->flags & QC_SF_DEM_FULL) {
2759 /* There must be data left for demux if it was interrupted on
2760 * full buffer. If this assumption is incorrect wakeup is not
2761 * necessary.
2762 */
2763 BUG_ON(!ncb_data(&qcs->rx.ncbuf, 0));
2764
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002765 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle16494692023-05-15 11:35:45 +02002766 if (!(qcc->flags & QC_CF_ERRL))
2767 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002768 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002769
Amaury Denoyelle16494692023-05-15 11:35:45 +02002770 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002771
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002772 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002773}
2774
Amaury Denoyelleb7ce79c2022-11-24 10:51:19 +01002775static size_t qc_send_buf(struct stconn *sc, struct buffer *buf,
2776 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002777{
Willy Tarreau3215e732022-05-27 10:09:11 +02002778 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002779 size_t ret = 0;
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002780 char fin;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002781
2782 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002783
Amaury Denoyelle3dc4e5a2022-09-13 16:49:21 +02002784 /* stream layer has been detached so no transfer must occur after. */
2785 BUG_ON_HOT(qcs->flags & QC_SF_DETACH);
2786
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002787 /* Report error if set on stream endpoint layer. */
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002788 if (qcs->qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL)) {
Amaury Denoyelle35542ce2023-05-03 18:16:40 +02002789 se_fl_set(qcs->sd, SE_FL_ERROR);
2790 TRACE_DEVEL("connection in error", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
2791 goto end;
2792 }
2793
Amaury Denoyelle843a1192022-07-04 11:44:38 +02002794 if (qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_RESET)) {
Amaury Denoyelle0ed617a2022-09-20 14:46:40 +02002795 ret = qcs_http_reset_buf(qcs, buf, count);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002796 goto end;
2797 }
2798
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002799 ret = qcs_http_snd_buf(qcs, buf, count, &fin);
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002800 if (fin) {
2801 TRACE_STATE("reached stream fin", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002802 qcs->flags |= QC_SF_FIN_STREAM;
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002803 }
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002804
Amaury Denoyelleab6cdec2023-01-10 10:41:41 +01002805 if (ret || fin) {
Amaury Denoyellef9b03262023-01-09 10:34:25 +01002806 qcc_send_stream(qcs, 0);
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002807 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
2808 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
2809 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002810
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002811 end:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002812 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
2813
2814 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002815}
2816
2817/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2818 * event subscriber <es> is not allowed to change from a previous call as long
2819 * as at least one event is still subscribed. The <event_type> must only be a
2820 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
2821 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002822static int qc_subscribe(struct stconn *sc, int event_type,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002823 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002824{
Willy Tarreau3215e732022-05-27 10:09:11 +02002825 return qcs_subscribe(__sc_mux_strm(sc), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002826}
2827
2828/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
2829 * The <es> pointer is not allowed to differ from the one passed to the
2830 * subscribe() call. It always returns zero.
2831 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002832static int qc_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002833{
Willy Tarreau3215e732022-05-27 10:09:11 +02002834 struct qcs *qcs = __sc_mux_strm(sc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002835
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002836 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
2837 BUG_ON(qcs->subs && qcs->subs != es);
2838
2839 es->events &= ~event_type;
2840 if (!es->events)
2841 qcs->subs = NULL;
2842
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002843 return 0;
2844}
2845
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002846static int qc_wake(struct connection *conn)
2847{
2848 struct qcc *qcc = conn->ctx;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002849
2850 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002851
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002852 if (qc_process(qcc)) {
2853 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002854 goto release;
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002855 }
2856
2857 qc_wake_some_streams(qcc);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002858
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002859 qcc_refresh_timeout(qcc);
2860
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002861 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002862 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002863
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002864 release:
2865 qc_release(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002866 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002867 return 1;
2868}
2869
Amaury Denoyellea473f192022-12-21 10:21:58 +01002870static void qc_shutw(struct stconn *sc, enum co_shw_mode mode)
2871{
2872 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002873 struct qcc *qcc = qcs->qcc;
Amaury Denoyellea473f192022-12-21 10:21:58 +01002874
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002875 TRACE_ENTER(QMUX_EV_STRM_SHUT, qcc->conn, qcs);
Amaury Denoyellea473f192022-12-21 10:21:58 +01002876
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002877 /* Early closure reported if QC_SF_FIN_STREAM not yet set. */
Amaury Denoyellea473f192022-12-21 10:21:58 +01002878 if (!qcs_is_close_local(qcs) &&
2879 !(qcs->flags & (QC_SF_FIN_STREAM|QC_SF_TO_RESET))) {
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002880
2881 if (qcs->flags & QC_SF_UNKNOWN_PL_LENGTH) {
2882 /* Close stream with a FIN STREAM frame. */
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002883 if (!(qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL))) {
Amaury Denoyelle3fd40932023-05-10 10:41:47 +02002884 TRACE_STATE("set FIN STREAM",
2885 QMUX_EV_STRM_SHUT, qcc->conn, qcs);
2886 qcs->flags |= QC_SF_FIN_STREAM;
2887 qcc_send_stream(qcs, 0);
2888 }
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002889 }
2890 else {
2891 /* RESET_STREAM necessary. */
Amaury Denoyelle5f67b172023-05-04 18:52:42 +02002892 if (!(qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL)))
Amaury Denoyelle3fd40932023-05-10 10:41:47 +02002893 qcc_reset_stream(qcs, 0);
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002894 se_fl_set_error(qcs->sd);
2895 }
2896
2897 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyellea473f192022-12-21 10:21:58 +01002898 }
2899
Amaury Denoyelled4af0412023-05-03 18:17:19 +02002900 out:
Amaury Denoyelle24962dd2023-04-24 17:50:23 +02002901 TRACE_LEAVE(QMUX_EV_STRM_SHUT, qcc->conn, qcs);
Amaury Denoyellea473f192022-12-21 10:21:58 +01002902}
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002903
Willy Tarreaub4a4fee2022-09-02 16:00:40 +02002904/* for debugging with CLI's "show sess" command. May emit multiple lines, each
2905 * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single
2906 * line is used. Each field starts with a space so it's safe to print it after
2907 * existing fields.
2908 */
2909static int qc_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx)
2910{
2911 struct qcs *qcs = sd->se;
2912 struct qcc *qcc;
2913 int ret = 0;
2914
2915 if (!qcs)
2916 return ret;
2917
2918 chunk_appendf(msg, " qcs=%p .flg=%#x .id=%llu .st=%s .ctx=%p, .err=%#llx",
2919 qcs, qcs->flags, (ull)qcs->id, qcs_st_to_str(qcs->st), qcs->ctx, (ull)qcs->err);
2920
2921 if (pfx)
2922 chunk_appendf(msg, "\n%s", pfx);
2923
2924 qcc = qcs->qcc;
2925 chunk_appendf(msg, " qcc=%p .flg=%#x .nbsc=%llu .nbhreq=%llu, .task=%p",
2926 qcc, qcc->flags, (ull)qcc->nb_sc, (ull)qcc->nb_hreq, qcc->task);
2927 return ret;
2928}
2929
2930
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002931static const struct mux_ops qc_ops = {
2932 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002933 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002934 .detach = qc_detach,
Amaury Denoyelleb7ce79c2022-11-24 10:51:19 +01002935 .rcv_buf = qc_recv_buf,
2936 .snd_buf = qc_send_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002937 .subscribe = qc_subscribe,
2938 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002939 .wake = qc_wake,
Amaury Denoyellea473f192022-12-21 10:21:58 +01002940 .shutw = qc_shutw,
Willy Tarreaub4a4fee2022-09-02 16:00:40 +02002941 .show_sd = qc_show_sd,
Willy Tarreaub5821e12022-04-26 11:54:08 +02002942 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02002943 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002944};
2945
2946static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002947 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002948
2949INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);