blob: 291f4b3bbb633fed387429a4383b4eda7a0482e0 [file] [log] [blame]
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001#include <haproxy/mux_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002
Amaury Denoyelleeb01f592021-10-07 16:44:05 +02003#include <import/eb64tree.h>
4
Frédéric Lécailledfbae762021-02-18 09:59:01 +01005#include <haproxy/api.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01006#include <haproxy/connection.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +01007#include <haproxy/dynbuf.h>
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01008#include <haproxy/h3.h>
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02009#include <haproxy/list.h>
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +020010#include <haproxy/ncbuf.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +010011#include <haproxy/pool.h>
Frédéric Lécaille9969adb2023-01-18 11:52:21 +010012#include <haproxy/proxy.h>
Amaury Denoyelled80fbca2022-09-19 17:02:28 +020013#include <haproxy/qmux_http.h>
Amaury Denoyelle36d50bf2022-09-19 16:12:38 +020014#include <haproxy/qmux_trace.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020015#include <haproxy/quic_conn.h>
Amaury Denoyelle40c24f12023-01-27 17:47:49 +010016#include <haproxy/quic_frame.h>
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +010017#include <haproxy/quic_sock.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020018#include <haproxy/quic_stream.h>
Frédéric Lécaille748ece62022-05-21 23:58:40 +020019#include <haproxy/quic_tp-t.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020020#include <haproxy/ssl_sock-t.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020021#include <haproxy/stconn.h>
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010022#include <haproxy/trace.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010023
Amaury Denoyelledeed7772021-12-03 11:36:46 +010024DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010025DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
26
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +020027/* Emit a CONNECTION_CLOSE with error <err>. This will interrupt all future
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +020028 * send/receive operations.
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +020029 */
30static void qcc_emit_cc(struct qcc *qcc, int err)
31{
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +020032 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
33
Amaury Denoyellee2213df2023-03-09 10:14:28 +010034 /* This function must not be called multiple times. */
35 BUG_ON(qcc->flags & QC_CF_CC_EMIT);
36
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +020037 TRACE_STATE("set CONNECTION_CLOSE on quic-conn", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle57e6db72022-07-13 15:07:56 +020038 quic_set_connection_close(qcc->conn->handle.qc, quic_err_transport(err));
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +020039 qcc->flags |= QC_CF_CC_EMIT;
40 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +020041
42 TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +020043}
44
Amaury Denoyelle4b167002022-12-12 09:59:50 +010045static void qc_free_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
46{
47 struct buffer buf;
48
49 if (ncb_is_null(ncbuf))
50 return;
51
52 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
53 b_free(&buf);
54 offer_buffers(NULL, 1);
55
56 *ncbuf = NCBUF_NULL;
57}
58
59/* Free <qcs> instance. This function is reserved for internal usage : it must
60 * only be called on qcs alloc error or on connection shutdown. Else
Ilya Shipitsin07be66d2023-04-01 12:26:42 +020061 * qcs_destroy must be preferred to handle QUIC flow-control increase.
Amaury Denoyelle4b167002022-12-12 09:59:50 +010062 */
63static void qcs_free(struct qcs *qcs)
64{
65 struct qcc *qcc = qcs->qcc;
66
67 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn, qcs);
68
Amaury Denoyelle15337fd2022-12-20 14:47:16 +010069 /* Safe to use even if already removed from the list. */
70 LIST_DEL_INIT(&qcs->el_opening);
Amaury Denoyelle20f2a422023-01-03 14:39:24 +010071 LIST_DEL_INIT(&qcs->el_send);
Amaury Denoyelle4b167002022-12-12 09:59:50 +010072
73 /* Release stream endpoint descriptor. */
74 BUG_ON(qcs->sd && !se_fl_test(qcs->sd, SE_FL_ORPHAN));
75 sedesc_free(qcs->sd);
76
77 /* Release app-layer context. */
78 if (qcs->ctx && qcc->app_ops->detach)
79 qcc->app_ops->detach(qcs);
80
81 /* Release qc_stream_desc buffer from quic-conn layer. */
82 qc_stream_desc_release(qcs->stream);
83
84 /* Free Rx/Tx buffers. */
85 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
86 b_free(&qcs->tx.buf);
87
88 BUG_ON(!qcc->strms[qcs_id_type(qcs->id)].nb_streams);
89 --qcc->strms[qcs_id_type(qcs->id)].nb_streams;
90
91 /* Remove qcs from qcc tree. */
92 eb64_delete(&qcs->by_id);
93
94 pool_free(pool_head_qcs, qcs);
95
96 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
97}
98
Amaury Denoyelledeed7772021-12-03 11:36:46 +010099/* Allocate a new QUIC streams with id <id> and type <type>. */
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200100static struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100101{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100102 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100103
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100104 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
105
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100106 qcs = pool_alloc(pool_head_qcs);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200107 if (!qcs) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200108 TRACE_ERROR("alloc failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200109 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200110 }
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200111
112 qcs->stream = NULL;
113 qcs->qcc = qcc;
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +0200114 qcs->sd = NULL;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200115 qcs->flags = QC_SF_NONE;
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200116 qcs->st = QC_SS_IDLE;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200117 qcs->ctx = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100118
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200119 /* App callback attach may register the stream for http-request wait.
120 * These fields must be initialed before.
121 */
122 LIST_INIT(&qcs->el_opening);
Amaury Denoyelle20f2a422023-01-03 14:39:24 +0100123 LIST_INIT(&qcs->el_send);
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200124 qcs->start = TICK_ETERNITY;
125
Amaury Denoyelle4b167002022-12-12 09:59:50 +0100126 /* store transport layer stream descriptor in qcc tree */
127 qcs->id = qcs->by_id.key = id;
128 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
129
130 qcc->strms[type].nb_streams++;
131
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200132 /* Allocate transport layer stream descriptor. Only needed for TX. */
133 if (!quic_stream_is_uni(id) || !quic_stream_is_remote(qcc, id)) {
134 struct quic_conn *qc = qcc->conn->handle.qc;
135 qcs->stream = qc_stream_desc_new(id, type, qcs, qc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200136 if (!qcs->stream) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200137 TRACE_ERROR("qc_stream_desc alloc failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200138 goto err;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200139 }
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200140 }
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200141
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100142 /* If stream is local, use peer remote-limit, or else the opposite. */
Amaury Denoyelle176174f2022-10-21 17:02:18 +0200143 if (quic_stream_is_bidi(id)) {
144 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
145 qcc->rfctl.msd_bidi_l;
146 }
147 else if (quic_stream_is_local(qcc, id)) {
148 qcs->tx.msd = qcc->rfctl.msd_uni_l;
149 }
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100150
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200151 qcs->rx.ncbuf = NCBUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100152 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200153 qcs->rx.offset = qcs->rx.offset_max = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100154
Amaury Denoyelle176174f2022-10-21 17:02:18 +0200155 if (quic_stream_is_bidi(id)) {
156 qcs->rx.msd = quic_stream_is_local(qcc, id) ? qcc->lfctl.msd_bidi_l :
157 qcc->lfctl.msd_bidi_r;
158 }
159 else if (quic_stream_is_remote(qcc, id)) {
160 qcs->rx.msd = qcc->lfctl.msd_uni_r;
161 }
Amaury Denoyellea9773552022-05-16 14:38:25 +0200162 qcs->rx.msd_init = qcs->rx.msd;
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200163
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100164 qcs->tx.buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100165 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100166 qcs->tx.sent_offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100167
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100168 qcs->wait_event.tasklet = NULL;
169 qcs->wait_event.events = 0;
170 qcs->subs = NULL;
171
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200172 qcs->err = 0;
173
Amaury Denoyelle3d550842023-01-24 17:42:21 +0100174 if (qcc->app_ops->attach && qcc->app_ops->attach(qcs, qcc->ctx)) {
175 TRACE_ERROR("app proto failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
176 goto err;
177 }
178
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100179 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100180 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100181 return qcs;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200182
183 err:
Amaury Denoyelle4b167002022-12-12 09:59:50 +0100184 qcs_free(qcs);
185 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200186 return NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100187}
188
Amaury Denoyelle3abeb572022-07-04 11:42:27 +0200189static forceinline struct stconn *qcs_sc(const struct qcs *qcs)
190{
191 return qcs->sd ? qcs->sd->sc : NULL;
192}
193
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200194/* Reset the <qcc> inactivity timeout for http-keep-alive timeout. */
195static forceinline void qcc_reset_idle_start(struct qcc *qcc)
196{
197 qcc->idle_start = now_ms;
198}
199
Amaury Denoyellec603de42022-07-25 11:21:46 +0200200/* Decrement <qcc> sc. */
201static forceinline void qcc_rm_sc(struct qcc *qcc)
202{
203 BUG_ON_HOT(!qcc->nb_sc);
204 --qcc->nb_sc;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200205
206 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
207 * refreshed after this on stream detach.
208 */
209 if (!qcc->nb_sc && !qcc->nb_hreq)
210 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200211}
212
213/* Decrement <qcc> hreq. */
214static forceinline void qcc_rm_hreq(struct qcc *qcc)
215{
216 BUG_ON_HOT(!qcc->nb_hreq);
217 --qcc->nb_hreq;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200218
219 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
220 * refreshed after this on I/O handler.
221 */
222 if (!qcc->nb_sc && !qcc->nb_hreq)
223 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200224}
225
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200226static inline int qcc_is_dead(const struct qcc *qcc)
227{
228 /* Mux connection is considered dead if :
229 * - all stream-desc are detached AND
230 * = connection is on error OR
231 * = mux timeout has already fired or is unset
232 */
233 if (!qcc->nb_sc && ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task))
234 return 1;
235
236 return 0;
237}
238
239/* Return true if the mux timeout should be armed. */
240static inline int qcc_may_expire(struct qcc *qcc)
241{
242 return !qcc->nb_sc;
243}
244
245/* Refresh the timeout on <qcc> if needed depending on its state. */
246static void qcc_refresh_timeout(struct qcc *qcc)
247{
248 const struct proxy *px = qcc->proxy;
249
250 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
251
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200252 if (!qcc->task) {
253 TRACE_DEVEL("already expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200254 goto leave;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200255 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200256
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200257 /* Check if upper layer is responsible of timeout management. */
258 if (!qcc_may_expire(qcc)) {
259 TRACE_DEVEL("not eligible for timeout", QMUX_EV_QCC_WAKE, qcc->conn);
260 qcc->task->expire = TICK_ETERNITY;
261 task_queue(qcc->task);
262 goto leave;
263 }
264
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200265 /* Frontend timeout management
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +0100266 * - shutdown done -> timeout client-fin
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200267 * - detached streams with data left to send -> default timeout
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200268 * - stream waiting on incomplete request or no stream yet activated -> timeout http-request
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200269 * - idle after stream processing -> timeout http-keep-alive
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +0100270 *
271 * If proxy stop-stop in progress, immediate or spread close will be
272 * processed if shutdown already one or connection is idle.
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200273 */
274 if (!conn_is_back(qcc->conn)) {
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +0100275 if (qcc->nb_hreq && !(qcc->flags & QC_CF_APP_SHUT)) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200276 TRACE_DEVEL("one or more requests still in progress", QMUX_EV_QCC_WAKE, qcc->conn);
277 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200278 task_queue(qcc->task);
279 goto leave;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200280 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200281
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +0100282 if ((!LIST_ISEMPTY(&qcc->opening_list) || unlikely(!qcc->largest_bidi_r)) &&
283 !(qcc->flags & QC_CF_APP_SHUT)) {
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200284 int timeout = px->timeout.httpreq;
285 struct qcs *qcs = NULL;
286 int base_time;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200287
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200288 /* Use start time of first stream waiting on HTTP or
289 * qcc idle if no stream not yet used.
290 */
291 if (likely(!LIST_ISEMPTY(&qcc->opening_list)))
292 qcs = LIST_ELEM(qcc->opening_list.n, struct qcs *, el_opening);
293 base_time = qcs ? qcs->start : qcc->idle_start;
294
295 TRACE_DEVEL("waiting on http request", QMUX_EV_QCC_WAKE, qcc->conn, qcs);
296 qcc->task->expire = tick_add_ifset(base_time, timeout);
297 }
298 else {
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +0100299 if (qcc->flags & QC_CF_APP_SHUT) {
300 TRACE_DEVEL("connection in closing", QMUX_EV_QCC_WAKE, qcc->conn);
301 qcc->task->expire = tick_add_ifset(now_ms,
302 qcc->shut_timeout);
303 }
304 else {
305 /* Use http-request timeout if keep-alive timeout not set */
306 int timeout = tick_isset(px->timeout.httpka) ?
307 px->timeout.httpka : px->timeout.httpreq;
308 TRACE_DEVEL("at least one request achieved but none currently in progress", QMUX_EV_QCC_WAKE, qcc->conn);
309 qcc->task->expire = tick_add_ifset(qcc->idle_start, timeout);
310 }
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +0100311
312 /* If proxy soft-stop in progress and connection is
313 * inactive, close the connection immediately. If a
314 * close-spread-time is configured, randomly spread the
315 * timer over a closing window.
316 */
317 if ((qcc->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) &&
318 !(global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)) {
319
320 /* Wake timeout task immediately if window already expired. */
321 int remaining_window = tick_isset(global.close_spread_end) ?
322 tick_remain(now_ms, global.close_spread_end) : 0;
323
324 TRACE_DEVEL("proxy disabled, prepare connection soft-stop", QMUX_EV_QCC_WAKE, qcc->conn);
325 if (remaining_window) {
326 /* We don't need to reset the expire if it would
327 * already happen before the close window end.
328 */
329 if (!tick_isset(qcc->task->expire) ||
330 tick_is_le(global.close_spread_end, qcc->task->expire)) {
331 /* Set an expire value shorter than the current value
332 * because the close spread window end comes earlier.
333 */
334 qcc->task->expire = tick_add(now_ms,
335 statistical_prng_range(remaining_window));
336 }
337 }
338 else {
339 /* We are past the soft close window end, wake the timeout
340 * task up immediately.
341 */
342 qcc->task->expire = now_ms;
343 task_wakeup(qcc->task, TASK_WOKEN_TIMER);
344 }
345 }
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200346 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200347 }
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200348
349 /* fallback to default timeout if frontend specific undefined or for
350 * backend connections.
351 */
352 if (!tick_isset(qcc->task->expire)) {
353 TRACE_DEVEL("fallback to default timeout", QMUX_EV_QCC_WAKE, qcc->conn);
354 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200355 }
356
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200357 task_queue(qcc->task);
358
359 leave:
360 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
361}
362
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200363/* Mark a stream as open if it was idle. This can be used on every
364 * successful emission/reception operation to update the stream state.
365 */
366static void qcs_idle_open(struct qcs *qcs)
367{
368 /* This operation must not be used if the stream is already closed. */
369 BUG_ON_HOT(qcs->st == QC_SS_CLO);
370
371 if (qcs->st == QC_SS_IDLE) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200372 TRACE_STATE("opening stream", QMUX_EV_QCS_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200373 qcs->st = QC_SS_OPEN;
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200374 }
375}
376
377/* Close the local channel of <qcs> instance. */
378static void qcs_close_local(struct qcs *qcs)
379{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200380 TRACE_STATE("closing stream locally", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
381
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200382 /* The stream must have already been opened. */
383 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
384
385 /* This operation cannot be used multiple times. */
386 BUG_ON_HOT(qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO);
387
388 if (quic_stream_is_bidi(qcs->id)) {
389 qcs->st = (qcs->st == QC_SS_HREM) ? QC_SS_CLO : QC_SS_HLOC;
Amaury Denoyelleafb7b9d2022-09-19 11:58:24 +0200390
391 if (qcs->flags & QC_SF_HREQ_RECV)
392 qcc_rm_hreq(qcs->qcc);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200393 }
394 else {
395 /* Only local uni streams are valid for this operation. */
396 BUG_ON_HOT(quic_stream_is_remote(qcs->qcc, qcs->id));
397 qcs->st = QC_SS_CLO;
398 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200399}
400
401/* Close the remote channel of <qcs> instance. */
402static void qcs_close_remote(struct qcs *qcs)
403{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200404 TRACE_STATE("closing stream remotely", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
405
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200406 /* The stream must have already been opened. */
407 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
408
409 /* This operation cannot be used multiple times. */
410 BUG_ON_HOT(qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO);
411
412 if (quic_stream_is_bidi(qcs->id)) {
413 qcs->st = (qcs->st == QC_SS_HLOC) ? QC_SS_CLO : QC_SS_HREM;
414 }
415 else {
416 /* Only remote uni streams are valid for this operation. */
417 BUG_ON_HOT(quic_stream_is_local(qcs->qcc, qcs->id));
418 qcs->st = QC_SS_CLO;
419 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200420}
421
422static int qcs_is_close_local(struct qcs *qcs)
423{
424 return qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO;
425}
426
Amaury Denoyelle6eb3c4b2022-12-09 16:26:03 +0100427static int qcs_is_close_remote(struct qcs *qcs)
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200428{
429 return qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO;
430}
431
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100432struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100433{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100434 struct buffer *buf = b_alloc(bptr);
435 BUG_ON(!buf);
436 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100437}
438
Amaury Denoyellea441ec92022-07-04 15:48:57 +0200439static struct ncbuf *qc_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200440{
441 struct buffer buf = BUF_NULL;
442
443 if (ncb_is_null(ncbuf)) {
444 b_alloc(&buf);
445 BUG_ON(b_is_null(&buf));
446
447 *ncbuf = ncb_make(buf.area, buf.size, 0);
448 ncb_init(ncbuf, 0);
449 }
450
451 return ncbuf;
452}
453
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500454/* Notify an eventual subscriber on <qcs> or else wakeup up the stconn layer if
Amaury Denoyelle4561f842022-07-06 14:54:34 +0200455 * initialized.
456 */
457static void qcs_alert(struct qcs *qcs)
458{
459 if (qcs->subs) {
460 qcs_notify_recv(qcs);
461 qcs_notify_send(qcs);
462 }
463 else if (qcs_sc(qcs) && qcs->sd->sc->app_ops->wake) {
464 qcs->sd->sc->app_ops->wake(qcs->sd->sc);
465 }
466}
467
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100468int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
469{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100470 struct qcc *qcc = qcs->qcc;
471
472 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100473
474 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
475 BUG_ON(qcs->subs && qcs->subs != es);
476
477 es->events |= event_type;
478 qcs->subs = es;
479
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100480 if (event_type & SUB_RETRY_RECV)
481 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
482
483 if (event_type & SUB_RETRY_SEND)
484 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
485
486 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
487
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100488 return 0;
489}
490
491void qcs_notify_recv(struct qcs *qcs)
492{
493 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
494 tasklet_wakeup(qcs->subs->tasklet);
495 qcs->subs->events &= ~SUB_RETRY_RECV;
496 if (!qcs->subs->events)
497 qcs->subs = NULL;
498 }
499}
500
501void qcs_notify_send(struct qcs *qcs)
502{
503 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
504 tasklet_wakeup(qcs->subs->tasklet);
505 qcs->subs->events &= ~SUB_RETRY_SEND;
506 if (!qcs->subs->events)
507 qcs->subs = NULL;
508 }
509}
510
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200511/* Open a locally initiated stream for the connection <qcc>. Set <bidi> for a
512 * bidirectional stream, else an unidirectional stream is opened. The next
513 * available ID on the connection will be used according to the stream type.
514 *
515 * Returns the allocated stream instance or NULL on error.
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100516 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200517struct qcs *qcc_init_stream_local(struct qcc *qcc, int bidi)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100518{
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200519 struct qcs *qcs;
520 enum qcs_type type;
521 uint64_t *next;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100522
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200523 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
524
525 if (bidi) {
526 next = &qcc->next_bidi_l;
527 type = conn_is_back(qcc->conn) ? QCS_CLT_BIDI : QCS_SRV_BIDI;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100528 }
529 else {
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200530 next = &qcc->next_uni_l;
531 type = conn_is_back(qcc->conn) ? QCS_CLT_UNI : QCS_SRV_UNI;
532 }
533
534 /* TODO ensure that we won't overflow remote peer flow control limit on
535 * streams. Else, we should emit a STREAMS_BLOCKED frame.
536 */
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100537
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200538 qcs = qcs_new(qcc, *next, type);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200539 if (!qcs) {
540 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyellec0c6b6d2023-03-09 10:16:09 +0100541 qcc_emit_cc(qcc, QC_ERR_INTERNAL_ERROR);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200542 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200543 }
Amaury Denoyellec055e302022-02-07 16:09:06 +0100544
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200545 TRACE_PROTO("opening local stream", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200546 *next += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100547
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200548 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200549 return qcs;
550}
551
552/* Open a remote initiated stream for the connection <qcc> with ID <id>. The
553 * caller is responsible to ensure that a stream with the same ID was not
554 * already opened. This function will also create all intermediaries streams
555 * with ID smaller than <id> not already opened before.
556 *
557 * Returns the allocated stream instance or NULL on error.
558 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200559static struct qcs *qcc_init_stream_remote(struct qcc *qcc, uint64_t id)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200560{
561 struct qcs *qcs = NULL;
562 enum qcs_type type;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200563 uint64_t *largest, max_id;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100564
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200565 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200566
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200567 BUG_ON_HOT(quic_stream_is_local(qcc, id));
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100568
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200569 if (quic_stream_is_bidi(id)) {
570 largest = &qcc->largest_bidi_r;
571 type = conn_is_back(qcc->conn) ? QCS_SRV_BIDI : QCS_CLT_BIDI;
572 }
573 else {
574 largest = &qcc->largest_uni_r;
575 type = conn_is_back(qcc->conn) ? QCS_SRV_UNI : QCS_CLT_UNI;
576 }
577
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200578 /* RFC 9000 4.6. Controlling Concurrency
579 *
580 * An endpoint that receives a frame with a stream ID exceeding the
581 * limit it has sent MUST treat this as a connection error of type
582 * STREAM_LIMIT_ERROR
583 */
584 max_id = quic_stream_is_bidi(id) ? qcc->lfctl.ms_bidi * 4 :
585 qcc->lfctl.ms_uni * 4;
586 if (id >= max_id) {
587 TRACE_ERROR("flow control error", QMUX_EV_QCS_NEW|QMUX_EV_PROTO_ERR, qcc->conn);
588 qcc_emit_cc(qcc, QC_ERR_STREAM_LIMIT_ERROR);
589 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200590 }
591
592 /* Only stream ID not already opened can be used. */
593 BUG_ON(id < *largest);
594
595 while (id >= *largest) {
Amaury Denoyellefd79ddb2022-08-16 11:13:45 +0200596 const char *str = *largest < id ? "initializing intermediary remote stream" :
597 "initializing remote stream";
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200598
599 qcs = qcs_new(qcc, *largest, type);
600 if (!qcs) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200601 TRACE_ERROR("stream fallocation failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyellec0c6b6d2023-03-09 10:16:09 +0100602 qcc_emit_cc(qcc, QC_ERR_INTERNAL_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200603 goto err;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100604 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200605
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200606 TRACE_PROTO(str, QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200607 *largest += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100608 }
609
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200610 out:
611 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle50742292022-03-29 14:57:19 +0200612 return qcs;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200613
614 err:
615 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
616 return NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200617}
618
619/* Use this function for a stream <id> which is not in <qcc> stream tree. It
620 * returns true if the associated stream is closed.
621 */
622static int qcc_stream_id_is_closed(struct qcc *qcc, uint64_t id)
623{
624 uint64_t *largest;
625
626 /* This function must only be used for stream not present in the stream tree. */
627 BUG_ON_HOT(eb64_lookup(&qcc->streams_by_id, id));
628
629 if (quic_stream_is_local(qcc, id)) {
630 largest = quic_stream_is_uni(id) ? &qcc->next_uni_l :
631 &qcc->next_bidi_l;
632 }
633 else {
634 largest = quic_stream_is_uni(id) ? &qcc->largest_uni_r :
635 &qcc->largest_bidi_r;
636 }
637
638 return id < *largest;
639}
640
641/* Retrieve the stream instance from <id> ID. This can be used when receiving
642 * STREAM, STREAM_DATA_BLOCKED, RESET_STREAM, MAX_STREAM_DATA or STOP_SENDING
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200643 * frames. Set to false <receive_only> or <send_only> if these particular types
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200644 * of streams are not allowed. If the stream instance is found, it is stored in
645 * <out>.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200646 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200647 * Returns 0 on success else non-zero. On error, a RESET_STREAM or a
648 * CONNECTION_CLOSE is automatically emitted. Beware that <out> may be NULL
649 * on success if the stream has already been closed.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200650 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200651int qcc_get_qcs(struct qcc *qcc, uint64_t id, int receive_only, int send_only,
652 struct qcs **out)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200653{
654 struct eb64_node *node;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200655
656 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200657 *out = NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200658
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200659 if (!receive_only && quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200660 TRACE_ERROR("receive-only stream not allowed", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id);
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200661 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200662 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200663 }
664
665 if (!send_only && quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200666 TRACE_ERROR("send-only stream not allowed", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id);
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200667 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200668 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200669 }
670
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200671 /* Search the stream in the connection tree. */
672 node = eb64_lookup(&qcc->streams_by_id, id);
673 if (node) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200674 *out = eb64_entry(node, struct qcs, by_id);
675 TRACE_DEVEL("using stream from connection tree", QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200676 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200677 }
678
679 /* Check if stream is already closed. */
680 if (qcc_stream_id_is_closed(qcc, id)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200681 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200682 /* Consider this as a success even if <out> is left NULL. */
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200683 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200684 }
685
686 /* Create the stream. This is valid only for remote initiated one. A
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500687 * local stream must have already been explicitly created by the
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200688 * application protocol layer.
689 */
690 if (quic_stream_is_local(qcc, id)) {
691 /* RFC 9000 19.8. STREAM Frames
692 *
693 * An endpoint MUST terminate the connection with error
694 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
695 * initiated stream that has not yet been created, or for a send-only
696 * stream.
697 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200698 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 Denoyellea509ffb2022-07-04 15:50:33 +0200699 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200700 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200701 }
702 else {
703 /* Remote stream not found - try to open it. */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200704 *out = qcc_init_stream_remote(qcc, id);
705 if (!*out) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200706 TRACE_ERROR("stream creation error", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200707 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200708 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200709 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100710
711 out:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200712 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200713 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200714
715 err:
716 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
717 return 1;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100718}
719
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200720/* Simple function to duplicate a buffer */
721static inline struct buffer qcs_b_dup(const struct ncbuf *b)
722{
723 return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0));
724}
725
Amaury Denoyelle36d4b5e2022-07-01 11:25:40 +0200726/* Remove <bytes> from <qcs> Rx buffer. Flow-control for received offsets may
727 * be allocated for the peer if needed.
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200728 */
729static void qcs_consume(struct qcs *qcs, uint64_t bytes)
730{
731 struct qcc *qcc = qcs->qcc;
732 struct quic_frame *frm;
733 struct ncbuf *buf = &qcs->rx.ncbuf;
734 enum ncb_ret ret;
735
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200736 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
737
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200738 ret = ncb_advance(buf, bytes);
739 if (ret) {
740 ABORT_NOW(); /* should not happens because removal only in data */
741 }
742
743 if (ncb_is_empty(buf))
744 qc_free_ncbuf(qcs, buf);
745
746 qcs->rx.offset += bytes;
Amaury Denoyellebb6296c2022-12-09 15:00:17 +0100747 /* Not necessary to emit a MAX_STREAM_DATA if all data received. */
748 if (qcs->flags & QC_SF_SIZE_KNOWN)
749 goto conn_fctl;
750
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200751 if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200752 TRACE_DATA("increase stream credit via MAX_STREAM_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +0100753 frm = qc_frm_alloc(QUIC_FT_MAX_STREAM_DATA);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +0100754 if (!frm) {
755 qcc_emit_cc(qcc, QC_ERR_INTERNAL_ERROR);
756 return;
757 }
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200758
759 qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init;
760
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200761 frm->max_stream_data.id = qcs->id;
762 frm->max_stream_data.max_stream_data = qcs->rx.msd;
763
764 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
765 tasklet_wakeup(qcc->wait_event.tasklet);
766 }
767
Amaury Denoyellebb6296c2022-12-09 15:00:17 +0100768 conn_fctl:
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200769 qcc->lfctl.offsets_consume += bytes;
770 if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200771 TRACE_DATA("increase conn credit via MAX_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +0100772 frm = qc_frm_alloc(QUIC_FT_MAX_DATA);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +0100773 if (!frm) {
774 qcc_emit_cc(qcc, QC_ERR_INTERNAL_ERROR);
775 return;
776 }
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200777
778 qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init;
779
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200780 frm->max_data.max_data = qcc->lfctl.md;
781
782 LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list);
783 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
784 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200785
786 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200787}
788
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200789/* Decode the content of STREAM frames already received on the stream instance
790 * <qcs>.
791 *
792 * Returns 0 on success else non-zero.
793 */
794static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
795{
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200796 struct buffer b;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200797 ssize_t ret;
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200798 int fin = 0;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200799
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200800 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
801
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200802 b = qcs_b_dup(&qcs->rx.ncbuf);
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200803
Amaury Denoyelled1310f82022-09-16 13:30:59 +0200804 /* Signal FIN to application if STREAM FIN received with all data. */
805 if (qcs_is_close_remote(qcs))
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200806 fin = 1;
807
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100808 if (!(qcs->flags & QC_SF_READ_ABORTED)) {
809 ret = qcc->app_ops->decode_qcs(qcs, &b, fin);
810 if (ret < 0) {
811 TRACE_ERROR("decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
812 goto err;
813 }
814 }
815 else {
816 TRACE_DATA("ignore read on stream", QMUX_EV_QCS_RECV, qcc->conn, qcs);
817 ret = b_data(&b);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200818 }
819
Amaury Denoyelle381d8132023-02-17 09:51:20 +0100820 if (ret)
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200821 qcs_consume(qcs, ret);
Amaury Denoyelle381d8132023-02-17 09:51:20 +0100822 if (ret || (!b_data(&b) && fin))
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200823 qcs_notify_recv(qcs);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200824
825 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200826 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200827
828 err:
829 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
830 return 1;
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200831}
832
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200833/* Emit a CONNECTION_CLOSE_APP with error <err>. Reserved for application error
Amaury Denoyelled666d742022-07-13 15:15:58 +0200834 * code. To close the connection right away, set <immediate> : this is useful
835 * when dealing with a connection fatal error. Else a graceful shutdown will be
836 * conducted : the error-code is only registered. The lower layer is
837 * responsible to close the connection when deemed suitable. Note that in this
838 * case the error code might be overwritten if an immediate close is requested
839 * in the interval.
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200840 */
Amaury Denoyelled666d742022-07-13 15:15:58 +0200841void qcc_emit_cc_app(struct qcc *qcc, int err, int immediate)
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200842{
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200843 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
844
Amaury Denoyellee2213df2023-03-09 10:14:28 +0100845 /* This function must not be called multiple times after immediate is set. */
846 BUG_ON(qcc->flags & QC_CF_CC_EMIT);
847
Amaury Denoyelled666d742022-07-13 15:15:58 +0200848 if (immediate) {
849 quic_set_connection_close(qcc->conn->handle.qc, quic_err_app(err));
850 qcc->flags |= QC_CF_CC_EMIT;
851 tasklet_wakeup(qcc->wait_event.tasklet);
852 }
853 else {
854 /* Only register the error code for graceful shutdown. */
855 qcc->conn->handle.qc->err = quic_err_app(err);
856 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200857
858 TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200859}
860
861/* Prepare for the emission of RESET_STREAM on <qcs> with error code <err>. */
862void qcc_reset_stream(struct qcs *qcs, int err)
863{
864 struct qcc *qcc = qcs->qcc;
865
866 if ((qcs->flags & QC_SF_TO_RESET) || qcs_is_close_local(qcs))
867 return;
868
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200869 TRACE_STATE("reset stream", QMUX_EV_QCS_END, qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200870 qcs->flags |= QC_SF_TO_RESET;
871 qcs->err = err;
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +0100872
Amaury Denoyelle178fbff2023-03-22 11:17:59 +0100873 /* Remove prepared stream data from connection flow-control calcul. */
874 if (qcs->tx.offset > qcs->tx.sent_offset) {
875 const uint64_t diff = qcs->tx.offset - qcs->tx.sent_offset;
876 BUG_ON(qcc->tx.offsets - diff < qcc->tx.sent_offsets);
877 qcc->tx.offsets -= diff;
878 /* Reset qcs offset to prevent BUG_ON() on qcs_destroy(). */
879 qcs->tx.offset = qcs->tx.sent_offset;
880 }
881
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +0100882 qcc_send_stream(qcs, 1);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200883 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100884}
885
Amaury Denoyellef9b03262023-01-09 10:34:25 +0100886/* Register <qcs> stream for emission of STREAM, STOP_SENDING or RESET_STREAM.
887 * Set <urg> to 1 if stream content should be treated in priority compared to
888 * other streams.
889 */
890void qcc_send_stream(struct qcs *qcs, int urg)
Amaury Denoyelle20f2a422023-01-03 14:39:24 +0100891{
892 struct qcc *qcc = qcs->qcc;
893
894 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
895
896 /* Cannot send if already closed. */
897 BUG_ON(qcs_is_close_local(qcs));
898
Amaury Denoyellef9b03262023-01-09 10:34:25 +0100899 if (urg) {
900 LIST_DEL_INIT(&qcs->el_send);
901 LIST_INSERT(&qcc->send_list, &qcs->el_send);
902 }
903 else {
904 if (!LIST_INLIST(&qcs->el_send))
905 LIST_APPEND(&qcs->qcc->send_list, &qcs->el_send);
906 }
Amaury Denoyelle20f2a422023-01-03 14:39:24 +0100907
908 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
909}
910
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100911/* Prepare for the emission of STOP_SENDING on <qcs>. */
912void qcc_abort_stream_read(struct qcs *qcs)
913{
914 struct qcc *qcc = qcs->qcc;
915
916 TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn, qcs);
917
918 if ((qcs->flags & QC_SF_TO_STOP_SENDING) || qcs_is_close_remote(qcs))
919 goto end;
920
921 TRACE_STATE("abort stream read", QMUX_EV_QCS_END, qcc->conn, qcs);
922 qcs->flags |= (QC_SF_TO_STOP_SENDING|QC_SF_READ_ABORTED);
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +0100923
924 qcc_send_stream(qcs, 1);
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100925 tasklet_wakeup(qcc->wait_event.tasklet);
926
927 end:
928 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn, qcs);
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200929}
930
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200931/* Install the <app_ops> applicative layer of a QUIC connection on mux <qcc>.
932 * Returns 0 on success else non-zero.
933 */
934int qcc_install_app_ops(struct qcc *qcc, const struct qcc_app_ops *app_ops)
935{
936 TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn);
937
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +0100938 if (app_ops->init && !app_ops->init(qcc)) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200939 TRACE_ERROR("app ops init error", QMUX_EV_QCC_NEW, qcc->conn);
940 goto err;
941 }
942
943 TRACE_PROTO("application layer initialized", QMUX_EV_QCC_NEW, qcc->conn);
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +0100944 qcc->app_ops = app_ops;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200945
Amaury Denoyelle71fd0362023-01-24 17:35:37 +0100946 /* RFC 9114 7.2.4.2. Initialization
947 *
948 * Endpoints MUST NOT require any data to be
949 * received from the peer prior to sending the SETTINGS frame;
950 * settings MUST be sent as soon as the transport is ready to
951 * send data.
952 */
953 if (qcc->app_ops->finalize) {
954 if (qcc->app_ops->finalize(qcc->ctx)) {
955 TRACE_ERROR("app ops finalize error", QMUX_EV_QCC_NEW, qcc->conn);
956 goto err;
957 }
958 tasklet_wakeup(qcc->wait_event.tasklet);
959 }
960
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200961 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
962 return 0;
963
964 err:
965 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
966 return 1;
967}
968
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200969/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
970 * <data> with length <len> and represents the offset <offset>. <fin> is set if
971 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100972 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200973 * Returns 0 on success else non-zero. On error, the received frame should not
974 * be acknowledged.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100975 */
976int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200977 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100978{
979 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200980 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100981
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100982 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
983
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200984 if (qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200985 TRACE_DATA("connection closed", QMUX_EV_QCC_RECV, qcc->conn);
986 goto err;
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200987 }
988
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200989 /* RFC 9000 19.8. STREAM Frames
990 *
991 * An endpoint MUST terminate the connection with error
992 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
993 * initiated stream that has not yet been created, or for a send-only
994 * stream.
995 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200996 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200997 TRACE_DATA("qcs retrieval error", QMUX_EV_QCC_RECV, qcc->conn);
998 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200999 }
1000
1001 if (!qcs) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001002 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV, qcc->conn);
1003 goto out;
Amaury Denoyelle57161b72022-07-07 15:02:32 +02001004 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001005
Amaury Denoyellebf91e392022-07-04 10:02:04 +02001006 /* RFC 9000 4.5. Stream Final Size
1007 *
1008 * Once a final size for a stream is known, it cannot change. If a
1009 * RESET_STREAM or STREAM frame is received indicating a change in the
1010 * final size for the stream, an endpoint SHOULD respond with an error
1011 * of type FINAL_SIZE_ERROR; see Section 11 for details on error
1012 * handling.
1013 */
1014 if (qcs->flags & QC_SF_SIZE_KNOWN &&
1015 (offset + len > qcs->rx.offset_max || (fin && offset + len < qcs->rx.offset_max))) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +02001016 TRACE_ERROR("final size error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR, qcc->conn, qcs);
Amaury Denoyellebf91e392022-07-04 10:02:04 +02001017 qcc_emit_cc(qcc, QC_ERR_FINAL_SIZE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001018 goto err;
Amaury Denoyellebf91e392022-07-04 10:02:04 +02001019 }
1020
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001021 if (qcs_is_close_remote(qcs)) {
1022 TRACE_DATA("skipping STREAM for remotely closed", QMUX_EV_QCC_RECV, qcc->conn);
1023 goto out;
1024 }
1025
Amaury Denoyellefa241932023-02-14 15:36:36 +01001026 if (offset + len < qcs->rx.offset ||
1027 (offset + len == qcs->rx.offset && (!fin || (qcs->flags & QC_SF_SIZE_KNOWN)))) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001028 TRACE_DATA("already received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1029 goto out;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001030 }
1031
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001032 TRACE_PROTO("receiving STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001033 qcs_idle_open(qcs);
1034
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001035 if (offset + len > qcs->rx.offset_max) {
1036 uint64_t diff = offset + len - qcs->rx.offset_max;
1037 qcs->rx.offset_max = offset + len;
1038 qcc->lfctl.offsets_recv += diff;
1039
1040 if (offset + len > qcs->rx.msd ||
1041 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
1042 /* RFC 9000 4.1. Data Flow Control
1043 *
1044 * A receiver MUST close the connection with an error
1045 * of type FLOW_CONTROL_ERROR if the sender violates
1046 * the advertised connection or stream data limits
1047 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +02001048 TRACE_ERROR("flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001049 qcc->conn, qcs);
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001050 qcc_emit_cc(qcc, QC_ERR_FLOW_CONTROL_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001051 goto err;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001052 }
1053 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001054
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001055 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001056 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001057 ABORT_NOW();
1058 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001059 }
1060
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001061 TRACE_DATA("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001062 if (offset < qcs->rx.offset) {
Frédéric Lécaillea18c3332022-07-04 09:54:58 +02001063 size_t diff = qcs->rx.offset - offset;
1064
1065 len -= diff;
1066 data += diff;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001067 offset = qcs->rx.offset;
1068 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001069
Amaury Denoyellefa241932023-02-14 15:36:36 +01001070 if (len) {
1071 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
1072 switch (ret) {
1073 case NCB_RET_OK:
1074 break;
1075
1076 case NCB_RET_DATA_REJ:
Amaury Denoyellecc3d7162022-05-20 15:14:57 +02001077 /* RFC 9000 2.2. Sending and Receiving Data
1078 *
1079 * An endpoint could receive data for a stream at the
1080 * same stream offset multiple times. Data that has
1081 * already been received can be discarded. The data at
1082 * a given offset MUST NOT change if it is sent
1083 * multiple times; an endpoint MAY treat receipt of
1084 * different data at the same offset within a stream as
1085 * a connection error of type PROTOCOL_VIOLATION.
1086 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +02001087 TRACE_ERROR("overlapping data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001088 qcc->conn, qcs);
Amaury Denoyellecc3d7162022-05-20 15:14:57 +02001089 qcc_emit_cc(qcc, QC_ERR_PROTOCOL_VIOLATION);
Amaury Denoyellefa241932023-02-14 15:36:36 +01001090 return 1;
1091
1092 case NCB_RET_GAP_SIZE:
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001093 TRACE_DATA("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
1094 qcc->conn, qcs);
Amaury Denoyellefa241932023-02-14 15:36:36 +01001095 return 1;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001096 }
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001097 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001098
1099 if (fin)
Amaury Denoyelle3f39b402022-07-01 16:11:03 +02001100 qcs->flags |= QC_SF_SIZE_KNOWN;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001101
Amaury Denoyelled1310f82022-09-16 13:30:59 +02001102 if (qcs->flags & QC_SF_SIZE_KNOWN &&
1103 qcs->rx.offset_max == qcs->rx.offset + ncb_data(&qcs->rx.ncbuf, 0)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001104 qcs_close_remote(qcs);
Amaury Denoyelled1310f82022-09-16 13:30:59 +02001105 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001106
Amaury Denoyellefa241932023-02-14 15:36:36 +01001107 if ((ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) || fin) {
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001108 qcc_decode_qcs(qcc, qcs);
Amaury Denoyelle418ba212022-08-02 15:57:16 +02001109 qcc_refresh_timeout(qcc);
1110 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001111
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001112 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001113 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001114 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001115
1116 err:
1117 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1118 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001119}
1120
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001121/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
1122 * the frame.
1123 *
1124 * Returns 0 on success else non-zero.
1125 */
1126int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
1127{
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001128 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1129
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001130 TRACE_PROTO("receiving MAX_DATA", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001131 if (qcc->rfctl.md < max) {
1132 qcc->rfctl.md = max;
Amaury Denoyelleb7143a82023-03-22 15:08:01 +01001133 TRACE_DATA("increase remote max-data", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001134
1135 if (qcc->flags & QC_CF_BLK_MFCTL) {
1136 qcc->flags &= ~QC_CF_BLK_MFCTL;
1137 tasklet_wakeup(qcc->wait_event.tasklet);
1138 }
1139 }
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001140
1141 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001142 return 0;
1143}
1144
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001145/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
1146 * field of the frame and <id> is the identifier of the QUIC stream.
1147 *
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001148 * Returns 0 on success else non-zero. On error, the received frame should not
1149 * be acknowledged.
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001150 */
1151int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
1152{
1153 struct qcs *qcs;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001154
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001155 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1156
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001157 if (qcc->flags & QC_CF_CC_EMIT) {
1158 TRACE_DATA("connection closed", QMUX_EV_QCC_RECV, qcc->conn);
1159 goto err;
1160 }
1161
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001162 /* RFC 9000 19.10. MAX_STREAM_DATA Frames
1163 *
1164 * Receiving a MAX_STREAM_DATA frame for a locally
1165 * initiated stream that has not yet been created MUST be treated as a
1166 * connection error of type STREAM_STATE_ERROR. An endpoint that
1167 * receives a MAX_STREAM_DATA frame for a receive-only stream MUST
1168 * terminate the connection with error STREAM_STATE_ERROR.
1169 */
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001170 if (qcc_get_qcs(qcc, id, 0, 1, &qcs))
1171 goto err;
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001172
1173 if (qcs) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001174 TRACE_PROTO("receiving MAX_STREAM_DATA", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001175 if (max > qcs->tx.msd) {
1176 qcs->tx.msd = max;
Amaury Denoyelleb7143a82023-03-22 15:08:01 +01001177 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 +01001178
1179 if (qcs->flags & QC_SF_BLK_SFCTL) {
1180 qcs->flags &= ~QC_SF_BLK_SFCTL;
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001181 /* TODO optim: only wakeup IO-CB if stream has data to sent. */
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001182 tasklet_wakeup(qcc->wait_event.tasklet);
1183 }
1184 }
1185 }
1186
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001187 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1188 qcc_refresh_timeout(qcc);
1189
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001190 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1191 return 0;
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001192
1193 err:
1194 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
1195 return 1;
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001196}
1197
1198/* Handle a new RESET_STREAM frame from stream ID <id> with error code <err>
1199 * and final stream size <final_size>.
1200 *
1201 * Returns 0 on success else non-zero. On error, the received frame should not
1202 * be acknowledged.
1203 */
1204int qcc_recv_reset_stream(struct qcc *qcc, uint64_t id, uint64_t err, uint64_t final_size)
1205{
1206 struct qcs *qcs;
1207
1208 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1209
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001210 if (qcc->flags & QC_CF_CC_EMIT) {
1211 TRACE_DATA("connection closed", QMUX_EV_QCC_RECV, qcc->conn);
1212 goto err;
1213 }
1214
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001215 /* RFC 9000 19.4. RESET_STREAM Frames
1216 *
1217 * An endpoint that receives a RESET_STREAM frame for a send-only stream
1218 * MUST terminate the connection with error STREAM_STATE_ERROR.
1219 */
1220 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
1221 TRACE_ERROR("RESET_STREAM for send-only stream received", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1222 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
1223 goto err;
1224 }
1225
1226 if (!qcs || qcs_is_close_remote(qcs))
1227 goto out;
1228
1229 TRACE_PROTO("receiving RESET_STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1230 qcs_idle_open(qcs);
1231
Amaury Denoyellee269aeb2023-01-30 12:13:22 +01001232 if (qcc->app_ops->close) {
1233 if (qcc->app_ops->close(qcs, QCC_APP_OPS_CLOSE_SIDE_RD)) {
1234 TRACE_ERROR("closure rejected by app layer", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1235 goto out;
1236 }
1237 }
1238
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001239 if (qcs->rx.offset_max > final_size ||
1240 ((qcs->flags & QC_SF_SIZE_KNOWN) && qcs->rx.offset_max != final_size)) {
1241 TRACE_ERROR("final size error on RESET_STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1242 qcc_emit_cc(qcc, QC_ERR_FINAL_SIZE_ERROR);
1243 goto err;
1244 }
1245
1246 qcs->flags |= QC_SF_SIZE_KNOWN;
1247 qcs_close_remote(qcs);
1248 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
1249
1250 if (qcs_sc(qcs)) {
1251 se_fl_set_error(qcs->sd);
1252 qcs_alert(qcs);
1253 }
1254
1255 out:
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001256 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001257 return 0;
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001258
1259 err:
1260 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1261 return 1;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001262}
1263
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001264/* Handle a new STOP_SENDING frame for stream ID <id>. The error code should be
1265 * specified in <err>.
1266 *
1267 * Returns 0 on success else non-zero. On error, the received frame should not
1268 * be acknowledged.
1269 */
1270int qcc_recv_stop_sending(struct qcc *qcc, uint64_t id, uint64_t err)
1271{
1272 struct qcs *qcs;
1273
1274 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1275
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001276 if (qcc->flags & QC_CF_CC_EMIT) {
1277 TRACE_DATA("connection closed", QMUX_EV_QCC_RECV, qcc->conn);
1278 goto err;
1279 }
1280
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001281 /* RFC 9000 19.5. STOP_SENDING Frames
1282 *
1283 * Receiving a STOP_SENDING frame for a
1284 * locally initiated stream that has not yet been created MUST be
1285 * treated as a connection error of type STREAM_STATE_ERROR. An
1286 * endpoint that receives a STOP_SENDING frame for a receive-only stream
1287 * MUST terminate the connection with error STREAM_STATE_ERROR.
1288 */
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001289 if (qcc_get_qcs(qcc, id, 0, 1, &qcs))
1290 goto err;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001291
1292 if (!qcs)
1293 goto out;
1294
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001295 TRACE_PROTO("receiving STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelled7755372022-10-03 17:20:31 +02001296
1297 /* RFC 9000 3.5. Solicited State Transitions
1298 *
1299 * An endpoint is expected to send another STOP_SENDING frame if a
1300 * packet containing a previous STOP_SENDING is lost. However, once
1301 * either all stream data or a RESET_STREAM frame has been received for
1302 * the stream -- that is, the stream is in any state other than "Recv"
1303 * or "Size Known" -- sending a STOP_SENDING frame is unnecessary.
1304 */
1305
1306 /* TODO thanks to previous RFC clause, STOP_SENDING is ignored if current stream
1307 * has already been closed locally. This is useful to not emit multiple
1308 * RESET_STREAM for a single stream. This is functional if stream is
1309 * locally closed due to all data transmitted, but in this case the RFC
1310 * advices to use an explicit RESET_STREAM.
1311 */
1312 if (qcs_is_close_local(qcs)) {
1313 TRACE_STATE("ignoring STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1314 goto out;
1315 }
1316
Amaury Denoyelle96ca1b72022-08-09 17:36:38 +02001317 qcs_idle_open(qcs);
1318
Amaury Denoyelle87f87662023-01-30 12:12:43 +01001319 if (qcc->app_ops->close) {
1320 if (qcc->app_ops->close(qcs, QCC_APP_OPS_CLOSE_SIDE_WR)) {
1321 TRACE_ERROR("closure rejected by app layer", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1322 goto out;
1323 }
1324 }
1325
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001326 /* RFC 9000 3.5. Solicited State Transitions
1327 *
1328 * An endpoint that receives a STOP_SENDING frame
1329 * MUST send a RESET_STREAM frame if the stream is in the "Ready" or
1330 * "Send" state. If the stream is in the "Data Sent" state, the
1331 * endpoint MAY defer sending the RESET_STREAM frame until the packets
1332 * containing outstanding data are acknowledged or declared lost. If
1333 * any outstanding data is declared lost, the endpoint SHOULD send a
1334 * RESET_STREAM frame instead of retransmitting the data.
1335 *
1336 * An endpoint SHOULD copy the error code from the STOP_SENDING frame to
1337 * the RESET_STREAM frame it sends, but it can use any application error
1338 * code.
1339 */
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001340 qcc_reset_stream(qcs, err);
1341
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001342 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1343 qcc_refresh_timeout(qcc);
1344
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001345 out:
1346 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1347 return 0;
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001348
1349 err:
1350 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
1351 return 1;
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001352}
1353
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001354/* Signal the closing of remote stream with id <id>. Flow-control for new
1355 * streams may be allocated for the peer if needed.
1356 */
1357static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +01001358{
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001359 struct quic_frame *frm;
1360
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001361 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn);
1362
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001363 if (quic_stream_is_bidi(id)) {
1364 ++qcc->lfctl.cl_bidi_r;
1365 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001366 TRACE_DATA("increase max stream limit with MAX_STREAMS_BIDI", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001367 frm = qc_frm_alloc(QUIC_FT_MAX_STREAMS_BIDI);
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +01001368 if (!frm) {
1369 qcc_emit_cc(qcc, QC_ERR_INTERNAL_ERROR);
1370 goto err;
1371 }
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001372
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001373 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
1374 qcc->lfctl.cl_bidi_r;
1375 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
1376 tasklet_wakeup(qcc->wait_event.tasklet);
1377
1378 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
1379 qcc->lfctl.cl_bidi_r = 0;
1380 }
1381 }
1382 else {
Amaury Denoyelle91077312022-12-22 18:56:09 +01001383 /* TODO unidirectional stream flow control with MAX_STREAMS_UNI
1384 * emission not implemented. It should be unnecessary for
1385 * HTTP/3 but may be required if other application protocols
1386 * are supported.
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02001387 */
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001388 }
1389
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001390 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
1391
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001392 return 0;
Amaury Denoyelleabbb5ad2023-03-09 10:16:38 +01001393
1394 err:
1395 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_END, qcc->conn);
1396 return 1;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001397}
1398
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05001399/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001400static void qcs_destroy(struct qcs *qcs)
1401{
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001402 struct qcc *qcc = qcs->qcc;
1403 struct connection *conn = qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001404 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001405
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001406 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001407
Amaury Denoyelle178fbff2023-03-22 11:17:59 +01001408 /* MUST not removed a stream with sending prepared data left. This is
1409 * to ensure consistency on connection flow-control calculation.
1410 */
1411 BUG_ON(qcs->tx.offset < qcs->tx.sent_offset);
1412
Amaury Denoyelleb47310d2023-03-09 15:49:48 +01001413 if (!(qcc->flags & QC_CF_CC_EMIT)) {
1414 if (quic_stream_is_remote(qcc, id))
1415 qcc_release_remote_stream(qcc, id);
1416 }
Amaury Denoyellec055e302022-02-07 16:09:06 +01001417
Amaury Denoyelledccbd732022-03-29 18:36:59 +02001418 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001419
1420 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001421}
1422
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001423/* Transfer as much as possible data on <qcs> from <in> to <out>. This is done
1424 * in respect with available flow-control at stream and connection level.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001425 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001426 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001427 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001428static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, struct buffer *in)
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001429{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001430 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001431 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001432 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001433
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001434 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001435
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001436 qc_get_buf(qcs, out);
1437
1438 /*
1439 * QCS out buffer diagram
1440 * head left to_xfer
1441 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001442 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001443 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001444 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001445 * ^ ack-off ^ sent-off ^ off
1446 *
1447 * STREAM frame
1448 * ^ ^
1449 * |xxxxxxxxxxxxxxxxx|
1450 */
1451
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001452 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001453 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001454 BUG_ON_HOT(qcc->tx.offsets < qcc->tx.sent_offsets);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001455
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001456 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001457 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001458
1459 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
1460 /* do not exceed flow control limit */
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001461 if (qcs->tx.offset + to_xfer > qcs->tx.msd) {
1462 TRACE_DATA("do not exceed stream flow control", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001463 to_xfer = qcs->tx.msd - qcs->tx.offset;
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001464 }
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001465
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001466 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001467 /* do not overcome flow control limit on connection */
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001468 if (qcc->tx.offsets + to_xfer > qcc->rfctl.md) {
1469 TRACE_DATA("do not exceed conn flow control", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001470 to_xfer = qcc->rfctl.md - qcc->tx.offsets;
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001471 }
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001472
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001473 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001474 goto out;
1475
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001476 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001477
1478 out:
1479 {
1480 struct qcs_xfer_data_trace_arg arg = {
1481 .prep = b_data(out), .xfer = total,
1482 };
1483 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
1484 qcc->conn, qcs, &arg);
1485 }
1486
1487 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001488}
1489
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001490/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
1491 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
1492 * stream frame.
1493 *
1494 * Returns the length of the STREAM frame or a negative error code.
1495 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001496static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
1497 struct list *frm_list)
1498{
1499 struct qcc *qcc = qcs->qcc;
1500 struct quic_frame *frm;
1501 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001502 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001503
1504 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1505
Amaury Denoyellea4569202022-04-15 17:29:25 +02001506 /* if ack_offset < buf_offset, it points to an older buffer. */
1507 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
1508 BUG_ON(qcs->tx.sent_offset < base_off);
1509
1510 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001511 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001512 BUG_ON(total < 0);
1513
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001514 if (!total && !fin) {
1515 /* No need to send anything if total is NULL and no FIN to signal. */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001516 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1517 return 0;
1518 }
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001519 BUG_ON((!total && qcs->tx.sent_offset > qcs->tx.offset) ||
1520 (total && qcs->tx.sent_offset >= qcs->tx.offset));
Amaury Denoyellea4569202022-04-15 17:29:25 +02001521 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001522 BUG_ON(qcc->tx.sent_offsets + total > qcc->rfctl.md);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001523
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001524 TRACE_PROTO("sending STREAM frame", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001525 frm = qc_frm_alloc(QUIC_FT_STREAM_8);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001526 if (!frm) {
1527 TRACE_ERROR("frame alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001528 goto err;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001529 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001530
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001531 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001532 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001533 frm->stream.buf = out;
1534 frm->stream.data = (unsigned char *)b_peek(out, head);
Amaury Denoyelle1dac0182023-02-02 16:45:07 +01001535 frm->stream.offset.key = 0;
Amaury Denoyelleebfafc22023-03-07 18:07:08 +01001536 frm->stream.dup = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001537
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +01001538 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001539 if (fin)
1540 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001541
1542 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001543 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001544 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001545 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001546
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001547 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
1548 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001549
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001550 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001551
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001552 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001553 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001554 struct qcs_build_stream_trace_arg arg = {
1555 .len = frm->stream.len, .fin = fin,
1556 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001557 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001558 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001559 qcc->conn, qcs, &arg);
1560 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001561
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001562 return total;
1563
1564 err:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001565 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001566 return -1;
1567}
1568
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001569/* Check after transferring data from qcs.tx.buf if FIN must be set on the next
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001570 * STREAM frame for <qcs>.
1571 *
1572 * Returns true if FIN must be set else false.
1573 */
1574static int qcs_stream_fin(struct qcs *qcs)
1575{
1576 return qcs->flags & QC_SF_FIN_STREAM && !b_data(&qcs->tx.buf);
1577}
1578
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001579/* Return true if <qcs> has data to send in new STREAM frames. */
1580static forceinline int qcs_need_sending(struct qcs *qcs)
1581{
1582 return b_data(&qcs->tx.buf) || qcs->tx.sent_offset < qcs->tx.offset ||
1583 qcs_stream_fin(qcs);
1584}
1585
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001586/* This function must be called by the upper layer to inform about the sending
1587 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
1588 * <offset>.
1589 */
1590void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
1591{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001592 struct qcc *qcc = qcs->qcc;
1593 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001594
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001595 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1596
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001597 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001598 BUG_ON(offset + data > qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001599
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001600 /* check if the STREAM frame has already been notified. It can happen
1601 * for retransmission.
1602 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001603 if (offset + data < qcs->tx.sent_offset) {
1604 TRACE_DEVEL("offset already notified", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1605 goto out;
1606 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001607
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001608 qcs_idle_open(qcs);
1609
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001610 diff = offset + data - qcs->tx.sent_offset;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001611 if (diff) {
1612 /* increase offset sum on connection */
1613 qcc->tx.sent_offsets += diff;
1614 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001615 if (qcc->tx.sent_offsets == qcc->rfctl.md) {
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001616 qcc->flags |= QC_CF_BLK_MFCTL;
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001617 TRACE_STATE("connection flow-control reached", QMUX_EV_QCS_SEND, qcc->conn);
1618 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001619
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001620 /* increase offset on stream */
1621 qcs->tx.sent_offset += diff;
1622 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
1623 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001624 if (qcs->tx.sent_offset == qcs->tx.msd) {
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001625 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001626 TRACE_STATE("stream flow-control reached", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1627 }
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001628
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01001629 /* If qcs.stream.buf is full, release it to the lower layer. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001630 if (qcs->tx.offset == qcs->tx.sent_offset &&
1631 b_full(&qcs->stream->buf->buf)) {
1632 qc_stream_buf_release(qcs->stream);
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001633 }
1634 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001635
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001636 if (qcs->tx.offset == qcs->tx.sent_offset && !b_data(&qcs->tx.buf)) {
1637 /* Remove stream from send_list if all was sent. */
1638 LIST_DEL_INIT(&qcs->el_send);
1639 TRACE_STATE("stream sent done", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1640
1641 if (qcs->flags & (QC_SF_FIN_STREAM|QC_SF_DETACH)) {
1642 /* Close stream locally. */
1643 qcs_close_local(qcs);
1644 /* Reset flag to not emit multiple FIN STREAM frames. */
1645 qcs->flags &= ~QC_SF_FIN_STREAM;
1646 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001647 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001648
1649 out:
1650 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001651}
1652
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001653/* Wrapper for send on transport layer. Send a list of frames <frms> for the
1654 * connection <qcc>.
1655 *
1656 * Returns 0 if all data sent with success else non-zero.
1657 */
1658static int qc_send_frames(struct qcc *qcc, struct list *frms)
1659{
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001660 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
1661
1662 if (LIST_ISEMPTY(frms)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001663 TRACE_DEVEL("no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
1664 goto err;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001665 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +01001666
Amaury Denoyellecaa16542023-02-28 15:11:26 +01001667 if (!qc_send_mux(qcc->conn->handle.qc, frms)) {
1668 /* TODO should subscribe only for a transient send error */
1669 TRACE_DEVEL("error on send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
1670 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1671 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyelle036cc5d2022-09-26 15:02:31 +02001672 goto err;
Amaury Denoyellecaa16542023-02-28 15:11:26 +01001673 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001674
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001675 /* If there is frames left at this stage, transport layer is blocked.
1676 * Subscribe on it to retry later.
1677 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001678 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001679 TRACE_DEVEL("remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001680 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1681 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001682 goto err;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001683 }
1684
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001685 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001686 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001687
1688 err:
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001689 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001690 return 1;
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001691}
1692
1693/* Emit a RESET_STREAM on <qcs>.
1694 *
1695 * Returns 0 if the frame has been successfully sent else non-zero.
1696 */
1697static int qcs_send_reset(struct qcs *qcs)
1698{
1699 struct list frms = LIST_HEAD_INIT(frms);
1700 struct quic_frame *frm;
1701
1702 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1703
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001704 frm = qc_frm_alloc(QUIC_FT_RESET_STREAM);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001705 if (!frm) {
1706 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001707 return 1;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001708 }
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001709
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001710 frm->reset_stream.id = qcs->id;
1711 frm->reset_stream.app_error_code = qcs->err;
1712 frm->reset_stream.final_size = qcs->tx.sent_offset;
1713
1714 LIST_APPEND(&frms, &frm->list);
1715 if (qc_send_frames(qcs->qcc, &frms)) {
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001716 qc_frm_free(&frm);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001717 TRACE_DEVEL("cannot send RESET_STREAM", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1718 return 1;
1719 }
1720
1721 if (qcs_sc(qcs)) {
1722 se_fl_set_error(qcs->sd);
1723 qcs_alert(qcs);
1724 }
1725
1726 qcs_close_local(qcs);
1727 qcs->flags &= ~QC_SF_TO_RESET;
1728
1729 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001730 return 0;
1731}
1732
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001733/* Emit a STOP_SENDING on <qcs>.
1734 *
1735 * Returns 0 if the frame has been successfully sent else non-zero.
1736 */
1737static int qcs_send_stop_sending(struct qcs *qcs)
1738{
1739 struct list frms = LIST_HEAD_INIT(frms);
1740 struct quic_frame *frm;
1741 struct qcc *qcc = qcs->qcc;
1742
1743 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1744
1745 /* RFC 9000 3.3. Permitted Frame Types
1746 *
1747 * A
1748 * receiver MAY send a STOP_SENDING frame in any state where it has not
1749 * received a RESET_STREAM frame -- that is, states other than "Reset
1750 * Recvd" or "Reset Read". However, there is little value in sending a
1751 * STOP_SENDING frame in the "Data Recvd" state, as all stream data has
1752 * been received. A sender could receive either of these two types of
1753 * frames in any state as a result of delayed delivery of packets.¶
1754 */
1755 if (qcs_is_close_remote(qcs)) {
1756 TRACE_STATE("skip STOP_SENDING on remote already closed", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1757 goto done;
1758 }
1759
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001760 frm = qc_frm_alloc(QUIC_FT_STOP_SENDING);
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001761 if (!frm) {
1762 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1763 return 1;
1764 }
1765
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001766 frm->stop_sending.id = qcs->id;
1767 frm->stop_sending.app_error_code = qcs->err;
1768
1769 LIST_APPEND(&frms, &frm->list);
1770 if (qc_send_frames(qcs->qcc, &frms)) {
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001771 qc_frm_free(&frm);
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001772 TRACE_DEVEL("cannot send STOP_SENDING", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1773 return 1;
1774 }
1775
1776 done:
1777 qcs->flags &= ~QC_SF_TO_STOP_SENDING;
1778
1779 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1780 return 0;
1781}
1782
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001783/* Used internally by qc_send function. Proceed to send for <qcs>. This will
1784 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001785 * is then generated and inserted in <frms> list.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001786 *
1787 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
1788 * be null if out buffer cannot be allocated.
1789 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001790static int _qc_send_qcs(struct qcs *qcs, struct list *frms)
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001791{
1792 struct qcc *qcc = qcs->qcc;
1793 struct buffer *buf = &qcs->tx.buf;
1794 struct buffer *out = qc_stream_buf_get(qcs->stream);
1795 int xfer = 0;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001796 char fin = 0;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001797
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001798 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1799
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001800 /* Cannot send STREAM on remote unidirectional streams. */
1801 BUG_ON(quic_stream_is_uni(qcs->id) && quic_stream_is_remote(qcc, qcs->id));
1802
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001803 /* Allocate <out> buffer if necessary. */
1804 if (!out) {
1805 if (qcc->flags & QC_CF_CONN_FULL)
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001806 goto out;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001807
1808 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
1809 if (!out) {
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001810 TRACE_STATE("cannot allocate stream desc buffer", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001811 qcc->flags |= QC_CF_CONN_FULL;
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001812 goto out;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001813 }
1814 }
1815
1816 /* Transfer data from <buf> to <out>. */
1817 if (b_data(buf)) {
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001818 xfer = qcs_xfer_data(qcs, out, buf);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001819 if (xfer > 0) {
1820 qcs_notify_send(qcs);
1821 qcs->flags &= ~QC_SF_BLK_MROOM;
1822 }
1823
1824 qcs->tx.offset += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001825 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001826 qcc->tx.offsets += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001827 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001828 }
1829
1830 /* out buffer cannot be emptied if qcs offsets differ. */
1831 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1832
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001833 /* FIN is set if all incoming data were transferred. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001834 fin = qcs_stream_fin(qcs);
1835
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001836 /* Build a new STREAM frame with <out> buffer. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001837 if (qcs->tx.sent_offset != qcs->tx.offset || fin) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001838 int ret;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001839 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +02001840 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001841 }
1842
Amaury Denoyelle1ec78ff2023-03-22 11:58:32 +01001843 out:
1844 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001845 return xfer;
1846}
1847
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001848/* Proceed to sending. Loop through all available streams for the <qcc>
1849 * instance and try to send as much as possible.
1850 *
1851 * Returns the total of bytes sent to the transport layer.
1852 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001853static int qc_send(struct qcc *qcc)
1854{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001855 struct list frms = LIST_HEAD_INIT(frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001856 struct qcs *qcs, *qcs_tmp;
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01001857 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001858
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001859 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001860
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001861 if (qcc->conn->flags & CO_FL_SOCK_WR_SH || qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001862 qcc->conn->flags |= CO_FL_ERROR;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001863 TRACE_DEVEL("connection on error", QMUX_EV_QCC_SEND, qcc->conn);
1864 goto err;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001865 }
1866
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001867 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1868 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
1869 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
1870 goto out;
1871 }
1872 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02001873
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001874 if (qcc->flags & QC_CF_BLK_MFCTL)
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001875 goto err;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001876
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001877 /* Send STREAM/STOP_SENDING/RESET_STREAM data for registered streams. */
1878 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_list, el_send) {
1879 /* Stream must not be present in send_list if it has nothing to send. */
1880 BUG_ON(!(qcs->flags & (QC_SF_TO_STOP_SENDING|QC_SF_TO_RESET)) &&
1881 !qcs_need_sending(qcs));
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001882
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001883 /* Each STOP_SENDING/RESET_STREAM frame is sent individually to
1884 * guarantee its emission.
1885 *
1886 * TODO multiplex several frames in same datagram to optimize sending
1887 */
1888 if (qcs->flags & QC_SF_TO_STOP_SENDING) {
1889 if (qcs_send_stop_sending(qcs))
1890 goto out;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001891
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001892 /* Remove stream from send_list if it had only STOP_SENDING
1893 * to send.
1894 */
1895 if (!(qcs->flags & QC_SF_TO_RESET) && !qcs_need_sending(qcs)) {
1896 LIST_DEL_INIT(&qcs->el_send);
1897 continue;
1898 }
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001899 }
1900
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001901 if (qcs->flags & QC_SF_TO_RESET) {
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001902 if (qcs_send_reset(qcs))
1903 goto out;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001904
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001905 /* RFC 9000 3.3. Permitted Frame Types
1906 *
1907 * A sender MUST NOT send
1908 * a STREAM or STREAM_DATA_BLOCKED frame for a stream in the
1909 * "Reset Sent" state or any terminal state -- that is, after
1910 * sending a RESET_STREAM frame.
1911 */
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001912 LIST_DEL_INIT(&qcs->el_send);
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001913 continue;
1914 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001915
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001916 if (!(qcs->flags & QC_SF_BLK_SFCTL))
1917 total += _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001918 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001919
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01001920 /* Retry sending until no frame to send, data rejected or connection
1921 * flow-control limit reached.
1922 */
1923 while (qc_send_frames(qcc, &frms) == 0 && !(qcc->flags & QC_CF_BLK_MFCTL)) {
1924 /* Reloop over <qcc.send_list>. Useful for streams which have
1925 * fulfilled their qc_stream_desc buf and have now release it.
1926 */
1927 list_for_each_entry(qcs, &qcc->send_list, el_send) {
1928 /* Only streams blocked on flow-control or waiting on a
1929 * new qc_stream_desc should be present in send_list as
1930 * long as transport layer can handle all data.
1931 */
1932 BUG_ON(qcs->stream->buf && !(qcs->flags & QC_SF_BLK_SFCTL));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001933
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01001934 if (!(qcs->flags & QC_SF_BLK_SFCTL))
1935 total += _qc_send_qcs(qcs, &frms);
1936 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001937 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001938
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001939 out:
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02001940 /* Deallocate frames that the transport layer has rejected. */
1941 if (!LIST_ISEMPTY(&frms)) {
1942 struct quic_frame *frm, *frm2;
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001943
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001944 list_for_each_entry_safe(frm, frm2, &frms, list)
1945 qc_frm_free(&frm);
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02001946 }
1947
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001948 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001949 return total;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001950
1951 err:
1952 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
1953 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001954}
1955
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001956/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1957 * operation.
1958 *
1959 * Returns 0 on success else non-zero.
1960 */
1961static int qc_recv(struct qcc *qcc)
1962{
1963 struct eb64_node *node;
1964 struct qcs *qcs;
1965
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001966 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001967
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001968 if (qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001969 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001970 return 0;
1971 }
1972
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001973 node = eb64_first(&qcc->streams_by_id);
1974 while (node) {
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001975 uint64_t id;
1976
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001977 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001978 id = qcs->id;
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001979
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001980 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001981 node = eb64_next(node);
1982 continue;
1983 }
1984
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001985 if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001986 node = eb64_next(node);
1987 continue;
1988 }
1989
1990 qcc_decode_qcs(qcc, qcs);
1991 node = eb64_next(node);
1992 }
1993
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001994 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001995 return 0;
1996}
1997
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001998
1999/* Release all streams which have their transfer operation achieved.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01002000 *
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002001 * Returns true if at least one stream is released.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01002002 */
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002003static int qc_purge_streams(struct qcc *qcc)
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002004{
2005 struct eb64_node *node;
2006 int release = 0;
2007
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002008 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002009
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002010 node = eb64_first(&qcc->streams_by_id);
2011 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02002012 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002013 node = eb64_next(node);
2014
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002015 /* Release not attached closed streams. */
2016 if (qcs->st == QC_SS_CLO && !qcs_sc(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002017 TRACE_STATE("purging closed stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002018 qcs_destroy(qcs);
2019 release = 1;
2020 continue;
2021 }
2022
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002023 /* Release detached streams with empty buffer. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002024 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02002025 if (qcs_is_close_local(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002026 TRACE_STATE("purging detached stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002027 qcs_destroy(qcs);
2028 release = 1;
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002029 continue;
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002030 }
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002031
2032 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
2033 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002034 }
2035 }
2036
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002037 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002038 return release;
2039}
2040
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002041/* Execute application layer shutdown. If this operation is not defined, a
2042 * CONNECTION_CLOSE will be prepared as a fallback. This function is protected
2043 * against multiple invocation with the flag QC_CF_APP_SHUT.
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002044 */
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002045static void qc_shutdown(struct qcc *qcc)
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002046{
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002047 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002048
Amaury Denoyelle665817a2023-03-20 17:34:22 +01002049 if (qcc->flags & (QC_CF_APP_SHUT|QC_CF_CC_EMIT)) {
2050 TRACE_DATA("connection closed", QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002051 goto out;
Amaury Denoyelle665817a2023-03-20 17:34:22 +01002052 }
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002053
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02002054 if (qcc->app_ops && qcc->app_ops->shutdown) {
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02002055 qcc->app_ops->shutdown(qcc->ctx);
Amaury Denoyellea154dc02022-06-13 17:09:01 +02002056 qc_send(qcc);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002057 }
2058 else {
2059 qcc_emit_cc_app(qcc, QC_ERR_NO_ERROR, 0);
2060 }
2061
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002062 out:
2063 qcc->flags |= QC_CF_APP_SHUT;
2064 TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn);
2065}
2066
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002067/* Conduct operations which should be made for <qcc> connection after
2068 * input/output. Most notably, closed streams are purged which may leave the
2069 * connection has ready to be released.
2070 *
2071 * Returns 1 if <qcc> must be released else 0.
2072 */
2073
2074static int qc_process(struct qcc *qcc)
2075{
2076 qc_purge_streams(qcc);
2077
Amaury Denoyelleb3aa07c2023-01-24 18:20:28 +01002078 /* Check if a soft-stop is in progress.
2079 *
2080 * TODO this is relevant for frontend connections only.
2081 */
2082 if (unlikely(qcc->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
2083 int close = 1;
2084
2085 /* If using listener socket, soft-stop is not supported. The
2086 * connection must be closed immediately.
2087 */
2088 if (!qc_test_fd(qcc->conn->handle.qc)) {
2089 TRACE_DEVEL("proxy disabled with listener socket, closing connection", QMUX_EV_QCC_WAKE, qcc->conn);
2090 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
2091 qc_send(qcc);
2092 goto out;
2093 }
2094
2095 TRACE_DEVEL("proxy disabled, prepare connection soft-stop", QMUX_EV_QCC_WAKE, qcc->conn);
2096
2097 /* If a close-spread-time option is set, we want to avoid
2098 * closing all the active HTTP3 connections at once so we add a
2099 * random factor that will spread the closing.
2100 */
2101 if (tick_isset(global.close_spread_end)) {
2102 int remaining_window = tick_remain(now_ms, global.close_spread_end);
2103 if (remaining_window) {
2104 /* This should increase the closing rate the
2105 * further along the window we are. */
2106 close = (remaining_window <= statistical_prng_range(global.close_spread_time));
2107 }
2108 }
2109 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE) {
2110 close = 0; /* let the client close his connection himself */
2111 }
2112
2113 if (close)
2114 qc_shutdown(qcc);
2115 }
2116
2117 out:
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002118 if (qcc_is_dead(qcc))
2119 return 1;
2120
2121 return 0;
2122}
2123
Amaury Denoyelleb30247b2023-01-24 18:18:23 +01002124/* release function. This one should be called to free all resources allocated
2125 * to the mux.
2126 */
2127static void qc_release(struct qcc *qcc)
2128{
2129 struct connection *conn = qcc->conn;
2130 struct eb64_node *node;
2131
2132 TRACE_ENTER(QMUX_EV_QCC_END, conn);
2133
2134 qc_shutdown(qcc);
2135
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002136 if (qcc->task) {
2137 task_destroy(qcc->task);
2138 qcc->task = NULL;
2139 }
2140
2141 if (qcc->wait_event.tasklet)
2142 tasklet_free(qcc->wait_event.tasklet);
2143 if (conn && qcc->wait_event.events) {
2144 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
2145 qcc->wait_event.events,
2146 &qcc->wait_event);
2147 }
2148
2149 /* liberate remaining qcs instances */
2150 node = eb64_first(&qcc->streams_by_id);
2151 while (node) {
2152 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
2153 node = eb64_next(node);
2154 qcs_free(qcs);
2155 }
2156
2157 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
2158 struct quic_frame *frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01002159 qc_frm_free(&frm);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002160 }
2161
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02002162 if (qcc->app_ops && qcc->app_ops->release)
2163 qcc->app_ops->release(qcc->ctx);
2164 TRACE_PROTO("application layer released", QMUX_EV_QCC_END, conn);
2165
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02002166 pool_free(pool_head_qcc, qcc);
2167
2168 if (conn) {
2169 LIST_DEL_INIT(&conn->stopping_list);
2170
2171 conn->handle.qc->conn = NULL;
2172 conn->mux = NULL;
2173 conn->ctx = NULL;
2174
2175 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
2176
2177 conn_stop_tracking(conn);
2178 conn_full_close(conn);
2179 if (conn->destroy_cb)
2180 conn->destroy_cb(conn);
2181 conn_free(conn);
2182 }
2183
2184 TRACE_LEAVE(QMUX_EV_QCC_END);
2185}
2186
Willy Tarreau41e701e2022-09-08 15:12:59 +02002187struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002188{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02002189 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002190
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002191 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002192
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002193 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002194
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002195 qc_recv(qcc);
2196
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002197 if (qc_process(qcc)) {
2198 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
2199 goto release;
2200 }
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002201
2202 qcc_refresh_timeout(qcc);
2203
Amaury Denoyelled3973852022-07-25 14:56:54 +02002204 end:
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002205 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
2206 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002207
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002208 release:
2209 qc_release(qcc);
2210 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002211 return NULL;
2212}
2213
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002214static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
2215{
2216 struct qcc *qcc = ctx;
2217 int expired = tick_is_expired(t->expire, now_ms);
2218
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002219 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002220
2221 if (qcc) {
2222 if (!expired) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002223 TRACE_DEVEL("not expired", QMUX_EV_QCC_WAKE, qcc->conn);
2224 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002225 }
2226
2227 if (!qcc_may_expire(qcc)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002228 TRACE_DEVEL("cannot expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002229 t->expire = TICK_ETERNITY;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002230 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002231 }
2232 }
2233
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002234 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01002235
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002236 if (!qcc) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002237 TRACE_DEVEL("no more qcc", QMUX_EV_QCC_WAKE);
2238 goto out;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002239 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01002240
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002241 qcc->task = NULL;
2242
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002243 /* TODO depending on the timeout condition, different shutdown mode
2244 * should be used. For http keep-alive or disabled proxy, a graceful
2245 * shutdown should occurs. For all other cases, an immediate close
2246 * seems legitimate.
2247 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002248 if (qcc_is_dead(qcc)) {
2249 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002250 qc_release(qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002251 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002252
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002253 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002254 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002255 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002256
2257 requeue:
2258 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
2259 return t;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002260}
2261
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002262static int qc_init(struct connection *conn, struct proxy *prx,
2263 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002264{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002265 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002266 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002267
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002268 TRACE_ENTER(QMUX_EV_QCC_NEW);
2269
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002270 qcc = pool_alloc(pool_head_qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002271 if (!qcc) {
2272 TRACE_ERROR("alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002273 goto fail_no_qcc;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002274 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002275
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002276 qcc->conn = conn;
2277 conn->ctx = qcc;
Amaury Denoyellec603de42022-07-25 11:21:46 +02002278 qcc->nb_hreq = qcc->nb_sc = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01002279 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002280
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002281 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002282
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002283 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002284
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002285 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02002286 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002287
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002288 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02002289 qcc->tx.sent_offsets = qcc->tx.offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002290
2291 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002292 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002293 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002294 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002295 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002296
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002297 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002298 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002299 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002300 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002301
2302 /* Server initiated streams must respect the server flow control. */
2303 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002304 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002305 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002306 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
2307
2308 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002309 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002310 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002311 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002312
Amaury Denoyellec985cb12022-05-16 14:29:59 +02002313 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01002314 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02002315 qcc->lfctl.ms_uni = lparams->initial_max_streams_uni;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02002316 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
2317 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle176174f2022-10-21 17:02:18 +02002318 qcc->lfctl.msd_uni_r = lparams->initial_max_stream_data_uni;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01002319 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01002320
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02002321 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02002322 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02002323
Willy Tarreau784b8682022-04-11 14:18:10 +02002324 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01002325 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002326 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
2327 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle176174f2022-10-21 17:02:18 +02002328 qcc->rfctl.msd_uni_l = rparams->initial_max_stream_data_uni;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002329
Amaury Denoyellea509ffb2022-07-04 15:50:33 +02002330 if (conn_is_back(conn)) {
2331 qcc->next_bidi_l = 0x00;
2332 qcc->largest_bidi_r = 0x01;
2333 qcc->next_uni_l = 0x02;
2334 qcc->largest_uni_r = 0x03;
2335 }
2336 else {
2337 qcc->largest_bidi_r = 0x00;
2338 qcc->next_bidi_l = 0x01;
2339 qcc->largest_uni_r = 0x02;
2340 qcc->next_uni_l = 0x03;
2341 }
2342
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002343 qcc->wait_event.tasklet = tasklet_new();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002344 if (!qcc->wait_event.tasklet) {
2345 TRACE_ERROR("taslket alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002346 goto fail_no_tasklet;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002347 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002348
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01002349 LIST_INIT(&qcc->send_list);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02002350
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002351 qcc->wait_event.tasklet->process = qc_io_cb;
2352 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01002353 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002354
Amaury Denoyelle07bf8f42022-07-22 16:16:03 +02002355 qcc->proxy = prx;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002356 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002357 qcc->task = NULL;
Amaury Denoyelleeb7d3202023-02-08 15:55:24 +01002358 if (conn_is_back(qcc->conn)) {
2359 qcc->timeout = prx->timeout.server;
2360 qcc->shut_timeout = tick_isset(prx->timeout.serverfin) ?
2361 prx->timeout.serverfin : prx->timeout.server;
2362 }
2363 else {
2364 qcc->timeout = prx->timeout.client;
2365 qcc->shut_timeout = tick_isset(prx->timeout.clientfin) ?
2366 prx->timeout.clientfin : prx->timeout.client;
2367 }
2368
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002369 if (tick_isset(qcc->timeout)) {
2370 qcc->task = task_new_here();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002371 if (!qcc->task) {
2372 TRACE_ERROR("timeout task alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002373 goto fail_no_timeout_task;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002374 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002375 qcc->task->process = qc_timeout_task;
2376 qcc->task->context = qcc;
2377 qcc->task->expire = tick_add(now_ms, qcc->timeout);
2378 }
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +02002379 qcc_reset_idle_start(qcc);
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02002380 LIST_INIT(&qcc->opening_list);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002381
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002382 if (!conn_is_back(conn)) {
2383 if (!LIST_INLIST(&conn->stopping_list)) {
2384 LIST_APPEND(&mux_stopping_data[tid].list,
2385 &conn->stopping_list);
2386 }
2387 }
2388
Willy Tarreau784b8682022-04-11 14:18:10 +02002389 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +01002390
2391 if (qcc_install_app_ops(qcc, conn->handle.qc->app_ops)) {
2392 TRACE_PROTO("Cannot install app layer", QMUX_EV_QCC_NEW, qcc->conn);
2393 /* prepare a CONNECTION_CLOSE frame */
2394 quic_set_connection_close(conn->handle.qc, quic_err_transport(QC_ERR_APPLICATION_ERROR));
2395 goto fail_install_app_ops;
2396 }
2397
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01002398 if (qcc->app_ops == &h3_ops)
2399 proxy_inc_fe_cum_sess_ver_ctr(sess->listener, prx, 3);
2400
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002401 /* init read cycle */
2402 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002403
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002404 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002405 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002406
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +01002407 fail_install_app_ops:
2408 if (qcc->app_ops && qcc->app_ops->release)
2409 qcc->app_ops->release(qcc->ctx);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002410 fail_no_timeout_task:
2411 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002412 fail_no_tasklet:
2413 pool_free(pool_head_qcc, qcc);
2414 fail_no_qcc:
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002415 TRACE_LEAVE(QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002416 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002417}
2418
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002419static void qc_destroy(void *ctx)
2420{
2421 struct qcc *qcc = ctx;
2422
2423 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
2424 qc_release(qcc);
2425 TRACE_LEAVE(QMUX_EV_QCC_END);
2426}
2427
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002428static void qc_detach(struct sedesc *sd)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002429{
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002430 struct qcs *qcs = sd->se;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002431 struct qcc *qcc = qcs->qcc;
2432
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002433 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002434
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002435 /* TODO this BUG_ON_HOT() is not correct as the stconn layer may detach
2436 * from the stream even if it is not closed remotely at the QUIC layer.
2437 * This happens for example when a stream must be closed due to a
2438 * rejected request. To better handle these cases, it will be required
2439 * to implement shutr/shutw MUX operations. Once this is done, this
2440 * BUG_ON_HOT() statement can be adjusted.
2441 */
2442 //BUG_ON_HOT(!qcs_is_close_remote(qcs));
Amaury Denoyellec603de42022-07-25 11:21:46 +02002443
2444 qcc_rm_sc(qcc);
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002445
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02002446 if (!qcs_is_close_local(qcs) && !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002447 TRACE_STATE("remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002448 qcs->flags |= QC_SF_DETACH;
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002449 qcc_refresh_timeout(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002450
2451 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002452 return;
2453 }
2454
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002455 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01002456
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002457 if (qcc_is_dead(qcc)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002458 TRACE_STATE("killing dead connection", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002459 goto release;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002460 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002461 else if (qcc->task) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002462 TRACE_DEVEL("refreshing connection's timeout", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002463 qcc_refresh_timeout(qcc);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002464 }
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002465 else {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002466 TRACE_DEVEL("completed", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002467 }
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002468
2469 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002470 return;
2471
2472 release:
2473 qc_release(qcc);
2474 TRACE_LEAVE(QMUX_EV_STRM_END);
2475 return;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002476}
2477
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002478/* Called from the upper layer, to receive data */
Amaury Denoyelleb7ce79c2022-11-24 10:51:19 +01002479static size_t qc_recv_buf(struct stconn *sc, struct buffer *buf,
2480 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002481{
Willy Tarreau3215e732022-05-27 10:09:11 +02002482 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002483 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002484 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002485
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002486 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002487
Amaury Denoyelled80fbca2022-09-19 17:02:28 +02002488 ret = qcs_http_rcv_buf(qcs, buf, count, &fin);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002489
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002490 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002491 se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002492 }
2493 else {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002494 se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
2495 if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING))
2496 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002497
Amaury Denoyelle72a78e82022-07-29 15:34:12 +02002498 /* Set end-of-input if FIN received and all data extracted. */
Christopher Faulet85eabfb2023-02-23 14:52:09 +01002499 if (fin) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002500 se_fl_set(qcs->sd, SE_FL_EOI);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002501
Christopher Faulet85eabfb2023-02-23 14:52:09 +01002502 /* If request EOM is reported to the upper layer, it means the
2503 * QCS now expects data from the opposite side.
2504 */
2505 se_expect_data(qcs->sd);
2506 }
2507
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002508 if (b_size(&qcs->rx.app_buf)) {
2509 b_free(&qcs->rx.app_buf);
2510 offer_buffers(NULL, 1);
2511 }
2512 }
2513
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002514 if (ret) {
2515 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002516 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002517 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002518
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002519 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
2520
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002521 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002522}
2523
Amaury Denoyelleb7ce79c2022-11-24 10:51:19 +01002524static size_t qc_send_buf(struct stconn *sc, struct buffer *buf,
2525 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002526{
Willy Tarreau3215e732022-05-27 10:09:11 +02002527 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002528 size_t ret;
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002529 char fin;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002530
2531 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002532
Amaury Denoyelle3dc4e5a2022-09-13 16:49:21 +02002533 /* stream layer has been detached so no transfer must occur after. */
2534 BUG_ON_HOT(qcs->flags & QC_SF_DETACH);
2535
Amaury Denoyelle843a1192022-07-04 11:44:38 +02002536 if (qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_RESET)) {
Amaury Denoyelle0ed617a2022-09-20 14:46:40 +02002537 ret = qcs_http_reset_buf(qcs, buf, count);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002538 goto end;
2539 }
2540
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002541 ret = qcs_http_snd_buf(qcs, buf, count, &fin);
2542 if (fin)
2543 qcs->flags |= QC_SF_FIN_STREAM;
2544
Amaury Denoyelleab6cdec2023-01-10 10:41:41 +01002545 if (ret || fin) {
Amaury Denoyellef9b03262023-01-09 10:34:25 +01002546 qcc_send_stream(qcs, 0);
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002547 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
2548 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
2549 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002550
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002551 end:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002552 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
2553
2554 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002555}
2556
2557/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2558 * event subscriber <es> is not allowed to change from a previous call as long
2559 * as at least one event is still subscribed. The <event_type> must only be a
2560 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
2561 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002562static int qc_subscribe(struct stconn *sc, int event_type,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002563 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002564{
Willy Tarreau3215e732022-05-27 10:09:11 +02002565 return qcs_subscribe(__sc_mux_strm(sc), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002566}
2567
2568/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
2569 * The <es> pointer is not allowed to differ from the one passed to the
2570 * subscribe() call. It always returns zero.
2571 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002572static int qc_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002573{
Willy Tarreau3215e732022-05-27 10:09:11 +02002574 struct qcs *qcs = __sc_mux_strm(sc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002575
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002576 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
2577 BUG_ON(qcs->subs && qcs->subs != es);
2578
2579 es->events &= ~event_type;
2580 if (!es->events)
2581 qcs->subs = NULL;
2582
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002583 return 0;
2584}
2585
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002586/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Willy Tarreau4596fe22022-05-17 19:07:51 +02002587 * report SE_FL_ERR_PENDING|SE_FL_ERROR on the attached stream connectors and
2588 * wake them.
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002589 */
2590static int qc_wake_some_streams(struct qcc *qcc)
2591{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002592 struct qcs *qcs;
2593 struct eb64_node *node;
2594
2595 for (node = eb64_first(&qcc->streams_by_id); node;
2596 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02002597 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002598
Amaury Denoyelle3abeb572022-07-04 11:42:27 +02002599 if (!qcs_sc(qcs))
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002600 continue;
2601
2602 if (qcc->conn->flags & CO_FL_ERROR) {
Willy Tarreau35c4dd02023-01-17 16:25:29 +01002603 se_fl_set_error(qcs->sd);
Amaury Denoyelle4561f842022-07-06 14:54:34 +02002604 qcs_alert(qcs);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002605 }
2606 }
2607
2608 return 0;
2609}
2610
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002611static int qc_wake(struct connection *conn)
2612{
2613 struct qcc *qcc = conn->ctx;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002614
2615 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002616
Willy Tarreau784b8682022-04-11 14:18:10 +02002617 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002618 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
2619
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002620 qc_send(qcc);
2621
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002622 if (qc_process(qcc)) {
2623 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002624 goto release;
Amaury Denoyelle14dbb842023-01-24 18:19:47 +01002625 }
2626
2627 qc_wake_some_streams(qcc);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002628
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002629 qcc_refresh_timeout(qcc);
2630
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002631 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002632 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002633
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002634 release:
2635 qc_release(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002636 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002637 return 1;
2638}
2639
Amaury Denoyellea473f192022-12-21 10:21:58 +01002640static void qc_shutw(struct stconn *sc, enum co_shw_mode mode)
2641{
2642 struct qcs *qcs = __sc_mux_strm(sc);
2643
2644 TRACE_ENTER(QMUX_EV_STRM_SHUT, qcs->qcc->conn, qcs);
2645
2646 /* If QC_SF_FIN_STREAM is not set and stream is not closed locally, it
2647 * means that upper layer reported an early closure. A RESET_STREAM is
2648 * necessary if not already scheduled.
2649 */
2650
2651 if (!qcs_is_close_local(qcs) &&
2652 !(qcs->flags & (QC_SF_FIN_STREAM|QC_SF_TO_RESET))) {
2653 qcc_reset_stream(qcs, 0);
2654 se_fl_set_error(qcs->sd);
2655 }
2656
2657 TRACE_LEAVE(QMUX_EV_STRM_SHUT, qcs->qcc->conn, qcs);
2658}
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002659
Willy Tarreaub4a4fee2022-09-02 16:00:40 +02002660/* for debugging with CLI's "show sess" command. May emit multiple lines, each
2661 * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single
2662 * line is used. Each field starts with a space so it's safe to print it after
2663 * existing fields.
2664 */
2665static int qc_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx)
2666{
2667 struct qcs *qcs = sd->se;
2668 struct qcc *qcc;
2669 int ret = 0;
2670
2671 if (!qcs)
2672 return ret;
2673
2674 chunk_appendf(msg, " qcs=%p .flg=%#x .id=%llu .st=%s .ctx=%p, .err=%#llx",
2675 qcs, qcs->flags, (ull)qcs->id, qcs_st_to_str(qcs->st), qcs->ctx, (ull)qcs->err);
2676
2677 if (pfx)
2678 chunk_appendf(msg, "\n%s", pfx);
2679
2680 qcc = qcs->qcc;
2681 chunk_appendf(msg, " qcc=%p .flg=%#x .nbsc=%llu .nbhreq=%llu, .task=%p",
2682 qcc, qcc->flags, (ull)qcc->nb_sc, (ull)qcc->nb_hreq, qcc->task);
2683 return ret;
2684}
2685
2686
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002687static const struct mux_ops qc_ops = {
2688 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002689 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002690 .detach = qc_detach,
Amaury Denoyelleb7ce79c2022-11-24 10:51:19 +01002691 .rcv_buf = qc_recv_buf,
2692 .snd_buf = qc_send_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002693 .subscribe = qc_subscribe,
2694 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002695 .wake = qc_wake,
Amaury Denoyellea473f192022-12-21 10:21:58 +01002696 .shutw = qc_shutw,
Willy Tarreaub4a4fee2022-09-02 16:00:40 +02002697 .show_sd = qc_show_sd,
Willy Tarreaub5821e12022-04-26 11:54:08 +02002698 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02002699 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002700};
2701
2702static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002703 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002704
2705INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);