1 #include <haproxy/mux_quic.h>
2 #include <haproxy/mux_quic_priv.h>
4 #include <import/eb64tree.h>
6 #include <haproxy/api.h>
7 #include <haproxy/buf.h>
8 #include <haproxy/chunk.h>
9 #include <haproxy/connection.h>
10 #include <haproxy/dynbuf.h>
11 #include <haproxy/global-t.h>
12 #include <haproxy/h3.h>
13 #include <haproxy/list.h>
14 #include <haproxy/ncbuf.h>
15 #include <haproxy/pool.h>
16 #include <haproxy/proxy.h>
17 #include <haproxy/qcm_http.h>
18 #include <haproxy/qcm_qmux.h>
19 #include <haproxy/qcm_trace.h>
20 #include <haproxy/quic_conn.h>
21 #include <haproxy/quic_enc.h>
22 #include <haproxy/quic_fctl.h>
23 #include <haproxy/quic_frame.h>
24 #include <haproxy/quic_pacing.h>
25 #include <haproxy/quic_sock.h>
26 #include <haproxy/quic_stream.h>
27 #include <haproxy/quic_stats.h>
28 #include <haproxy/quic_tp-t.h>
29 #include <haproxy/quic_tune.h>
30 #include <haproxy/quic_tx.h>
31 #include <haproxy/quic_utils.h>
32 #include <haproxy/session.h>
33 #include <haproxy/ssl_sock-t.h>
34 #include <haproxy/stconn.h>
35 #include <haproxy/time.h>
36 #include <haproxy/trace.h>
37 #include <haproxy/xprt_qmux.h>
38 #include <haproxy/xref.h>
40 DECLARE_TYPED_POOL(pool_head_qcc, "qcc", struct qcc);
41 DECLARE_TYPED_POOL(pool_head_qcs, "qcs", struct qcs);
42 DECLARE_STATIC_TYPED_POOL(pool_head_qc_stream_rxbuf, "qc_stream_rxbuf", struct qc_stream_rxbuf);
44 static void qcm_ctrl_send(struct qc_stream_desc *, uint64_t data, uint64_t offset);
45 static void qcm_ctrl_room(struct qc_stream_desc *, uint64_t room);
47 static void qcc_release(struct qcc *qcc);
48 static int qcc_app_init(struct qcc *qcc);
49 static void qcc_app_shutdown(struct qcc *qcc);
51 /* Returns true if pacing should be used for <conn> connection. */
52 static int qcc_is_pacing_active(const struct connection *conn)
54 return conn_is_quic(conn) && quic_tune_conn_test(QUIC_TUNE_FB_TX_PACING, conn);
57 /* Free <rxbuf> instance and its inner data storage attached to <qcs> stream. */
58 static void qcs_free_rxbuf(struct qcs *qcs, struct qc_stream_rxbuf *rxbuf)
64 if (!ncb_is_null(ncbuf)) {
65 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
67 offer_buffers(NULL, 1);
69 rxbuf->ncb = NCBUF_NULL;
71 /* Reset DEM_FULL as buffer is released. This ensures mux is not woken
72 * up from rcv_buf stream callback when demux was previously blocked.
74 qcs->flags &= ~QC_SF_DEM_FULL;
76 eb64_delete(&rxbuf->off_node);
77 pool_free(pool_head_qc_stream_rxbuf, rxbuf);
78 bdata_ctr_bdec(&qcs->rx.data);
81 /* Free <qcs> instance. This function is reserved for internal usage : it must
82 * only be called on qcs alloc error or on connection shutdown. Else
83 * qcs_destroy must be preferred to handle QUIC flow-control increase.
85 static void qcs_free(struct qcs *qcs)
87 struct qcc *qcc = qcs->qcc;
88 struct qc_stream_rxbuf *b;
90 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn, qcs);
91 TRACE_STATE("releasing QUIC stream", QMUX_EV_QCS_END, qcc->conn, qcs);
93 /* Safe to use even if already removed from the list. */
94 LIST_DEL_INIT(&qcs->el_opening);
95 LIST_DEL_INIT(&qcs->el_recv);
96 LIST_DEL_INIT(&qcs->el_send);
97 LIST_DEL_INIT(&qcs->el_fctl);
98 LIST_DEL_INIT(&qcs->el_buf);
100 /* Release stream endpoint descriptor. */
101 BUG_ON(qcs->sd && !se_fl_test(qcs->sd, SE_FL_ORPHAN));
102 sedesc_free(qcs->sd);
105 if (qcs->flags & QC_SF_HREQ_RECV) {
106 BUG_ON(!qcc->nb_hreq);
110 /* Release app-layer context. */
111 if (qcs->ctx && qcc->app_ops->detach)
112 qcc->app_ops->detach(qcs);
114 /* Release qc_stream_desc buffer from quic-conn layer. */
115 if (conn_is_quic(qcc->conn) && qcs->tx.stream) {
116 qc_stream_desc_sub_send(qcs->tx.stream, NULL);
117 qc_stream_desc_release(qcs->tx.stream, qcs->tx.fc.off_real, qcc);
119 else if (!conn_is_quic(qcc->conn)) {
120 b_free(&qcs->tx.qmux_buf);
123 /* Free Rx buffer. */
124 while (!eb_is_empty(&qcs->rx.bufs)) {
125 b = container_of(eb64_first(&qcs->rx.bufs),
126 struct qc_stream_rxbuf, off_node);
127 qcs_free_rxbuf(qcs, b);
130 if (b_size(&qcs->rx.app_buf)) {
131 b_free(&qcs->rx.app_buf);
132 offer_buffers(NULL, 1);
135 /* Remove qcs from qcc tree. */
136 eb64_delete(&qcs->by_id);
138 pool_free(pool_head_qcs, qcs);
140 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
143 /* Allocate a new QUIC streams with id <id> and type <type>. */
144 static struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
148 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
150 qcs = pool_alloc(pool_head_qcs);
152 TRACE_ERROR("alloc failure", QMUX_EV_QCS_NEW, qcc->conn);
156 qcs->tx.stream = NULL;
160 qcs->flags = QC_SF_NONE;
161 qcs->st = QC_SS_IDLE;
164 /* App callback attach may register the stream for http-request wait.
165 * These fields must be initialized before.
167 LIST_INIT(&qcs->el_opening);
168 LIST_INIT(&qcs->el_recv);
169 LIST_INIT(&qcs->el_send);
170 LIST_INIT(&qcs->el_fctl);
171 LIST_INIT(&qcs->el_buf);
172 qcs->start = TICK_ETERNITY;
174 /* store transport layer stream descriptor in qcc tree */
175 qcs->id = qcs->by_id.key = id;
176 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
178 /* Different limits can be set by the peer for local and remote bidi streams. */
179 if (quic_stream_is_bidi(id)) {
180 qfctl_init(&qcs->tx.fc, quic_stream_is_local(qcc, id) ?
181 qcc->rfctl.msd_bidi_r : qcc->rfctl.msd_bidi_l);
183 else if (quic_stream_is_local(qcc, id)) {
184 qfctl_init(&qcs->tx.fc, qcc->rfctl.msd_uni_l);
187 qfctl_init(&qcs->tx.fc, 0);
190 qcs->tx.msd_frm = NULL;
192 qcs->rx.bufs = EB_ROOT_UNIQUE;
193 qcs->rx.app_buf = BUF_NULL;
194 qcs->rx.offset = qcs->rx.offset_max = 0;
196 if (quic_stream_is_bidi(id)) {
197 qcs->rx.msd = quic_stream_is_local(qcc, id) ? qcc->lfctl.msd_bidi_l :
198 qcc->lfctl.msd_bidi_r;
200 else if (quic_stream_is_remote(qcc, id)) {
201 qcs->rx.msd = qcc->lfctl.msd_uni_r;
203 qcs->rx.msd_base = 0;
204 bdata_ctr_init(&qcs->rx.data);
206 qcs->wait_event.tasklet = NULL;
207 qcs->wait_event.events = 0;
210 qcs->rs_err = qcs->ss_err = 0;
212 /* Reset all timers and start base one. */
213 tot_time_reset(&qcs->timer.base);
214 tot_time_reset(&qcs->timer.buf);
215 tot_time_reset(&qcs->timer.fctl);
216 tot_time_start(&qcs->timer.base);
218 /* Allocate transport layer stream descriptor. Only needed for TX. */
219 if (!quic_stream_is_uni(id) || !quic_stream_is_remote(qcc, id)) {
220 if (conn_is_quic(qcc->conn)) {
221 struct quic_conn *qc = qcc->conn->handle.qc;
222 qcs->tx.stream = qc_stream_desc_new(id, type, qcs, qc);
223 if (!qcs->tx.stream) {
224 TRACE_ERROR("qc_stream_desc alloc failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
228 qc_stream_desc_sub_send(qcs->tx.stream, qcm_ctrl_send);
229 qc_stream_desc_sub_room(qcs->tx.stream, qcm_ctrl_room);
232 qcs->tx.qmux_buf = BUF_NULL;
236 /* If app layer is closed, reset immediately a new stream if proto does
237 * not implement graceful shutdown. Else proto is responsible to either
238 * accept or reject the new stream via its attach() operation.
240 if (qcc->app_st >= QCC_APP_ST_SHUT && !qcc->app_ops->shutdown) {
241 qcc_abort_stream_read(qcs, 0);
242 qcc_reset_stream(qcs, 0, 0);
245 else if (qcc->app_ops->attach && qcc->app_ops->attach(qcs, qcc->ctx)) {
246 TRACE_ERROR("app proto failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
251 TRACE_STATE("created new QUIC stream", QMUX_EV_QCS_NEW, qcc->conn, qcs);
252 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
257 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
261 static forceinline struct stconn *qcs_sc(const struct qcs *qcs)
263 return qcs->sd ? qcs->sd->sc : NULL;
266 /* Reset the <qcc> inactivity timeout for http-keep-alive timeout. */
267 static forceinline void qcc_reset_idle_start(struct qcc *qcc)
269 qcc->idle_start = now_ms;
272 /* Return true if the mux timeout should be armed. */
273 static inline int qcc_may_expire(struct qcc *qcc)
278 /* Decrement <qcc> sc. */
279 static forceinline void qcc_rm_sc(struct qcc *qcc)
281 BUG_ON(!qcc->nb_sc); /* Ensure sc count is always valid (ie >=0). */
285 /* Checks if <qcc> connection can be used to attach new streams on it or if
286 * reuse is definitely blocked. This is based on constant parameters such as a
287 * connection error or timeout, the server max-reuse limit or if the peer has
288 * requested a graceful shutdown. Flow control is not taken into account here
289 * as it can be adjusted dynamically over the connection lifetime.
291 * Returns a boolean value indicating if reuse is possible.
293 static int qcc_be_is_reusable(const struct qcc *qcc)
295 const struct server *srv = __objt_server(qcc->conn->target);
297 /* Connection on error or already on timeout. */
298 if (qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL) || !qcc->task)
301 /* Shutdown initiated by the peer - in HTTP/3 this corresponds to a GOAWAY frame received. */
302 if (qcc->flags & QC_CF_CONN_SHUT)
305 if (srv->max_reuse >= 0 && qcc->tot_sc > srv->max_reuse)
308 /* Ensure we do not exceed the maximum usable stream ID. */
309 if (qcc->next_bidi_l > QCS_ID_MAX_STRM_CL_BIDI)
315 /* Indicates if a connection is idle and cannot be used anymore. If true the
316 * connection should be released as soon as possible.
318 static inline int qcc_is_dead(const struct qcc *qcc)
320 /* Maintain connection if there is still request streams active. */
324 if (!conn_is_back(qcc->conn)) {
325 /* FE conn considered dead if either :
326 * - transport or local error reported
327 * - MUX timeout expired
330 return qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL_DONE) ||
331 !qcc->task || qcc->app_st == QCC_APP_ST_SHUT;
334 /* BE conn considered dead if reuse is definitively blocked.
335 * Checks similar conditions as FE side and more.
337 return !qcc_be_is_reusable(qcc);
341 /* Refresh the timeout on <qcc> if needed depending on its state. */
342 static void qcc_refresh_timeout(struct qcc *qcc)
344 const struct proxy *px = qcc->proxy;
346 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
349 TRACE_DEVEL("already expired", QMUX_EV_QCC_WAKE, qcc->conn);
353 /* Check if upper layer is responsible of timeout management. */
354 if (!qcc_may_expire(qcc)) {
355 TRACE_DEVEL("not eligible for timeout", QMUX_EV_QCC_WAKE, qcc->conn);
356 qcc->task->expire = TICK_ETERNITY;
357 task_queue(qcc->task);
361 /* Timeout management
362 * - detached streams with data left to send -> default timeout
363 * - shutdown done -> timeout client-fin
364 * - (FE only) stream waiting on incomplete request or no stream yet activated -> timeout http-request
365 * - (FE only) idle after stream processing -> timeout http-keep-alive
367 * If proxy stop-stop in progress, immediate or spread close will be
368 * processed if shutdown already one or connection is idle.
370 if (!LIST_ISEMPTY(&qcc->send_list) || !LIST_ISEMPTY(&qcc->tx.frms)) {
371 TRACE_DEVEL("pending output data", QMUX_EV_QCC_WAKE, qcc->conn);
372 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
374 else if (qcc->app_st >= QCC_APP_ST_SHUT) {
375 TRACE_DEVEL("connection in closing", QMUX_EV_QCC_WAKE, qcc->conn);
376 qcc->task->expire = tick_add_ifset(now_ms,
379 else if (!conn_is_back(qcc->conn)) {
380 if (!LIST_ISEMPTY(&qcc->opening_list) || unlikely(!qcc->largest_bidi_r)) {
381 int timeout = px->timeout.httpreq;
382 struct qcs *qcs = NULL;
385 /* Use start time of first stream waiting on HTTP or
386 * qcc idle if no stream not yet used.
388 if (likely(!LIST_ISEMPTY(&qcc->opening_list)))
389 qcs = LIST_ELEM(qcc->opening_list.n, struct qcs *, el_opening);
390 base_time = qcs ? qcs->start : qcc->idle_start;
392 TRACE_DEVEL("waiting on http request", QMUX_EV_QCC_WAKE, qcc->conn, qcs);
393 qcc->task->expire = tick_add_ifset(base_time, timeout);
396 /* Use http-request timeout if keep-alive timeout not set */
397 int timeout = tick_isset(px->timeout.httpka) ?
398 px->timeout.httpka : px->timeout.httpreq;
399 TRACE_DEVEL("at least one request achieved but none currently in progress", QMUX_EV_QCC_WAKE, qcc->conn);
400 qcc->task->expire = tick_add_ifset(qcc->idle_start, timeout);
403 /* Fallback to client timeout if specific value not set. */
404 if (!tick_isset(qcc->task->expire)) {
405 TRACE_DEVEL("fallback to default timeout", QMUX_EV_QCC_WAKE, qcc->conn);
406 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
410 /* If proxy soft-stop in progress and connection is inactive,
411 * close the connection immediately. If a close-spread-time is
412 * configured, randomly spread the timer over a closing window.
414 if ((qcc->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) &&
415 !(global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)) {
417 /* Wake timeout task immediately if window already expired. */
418 int remaining_window = tick_isset(global.close_spread_end) ?
419 tick_remain(now_ms, global.close_spread_end) : 0;
421 TRACE_DEVEL("proxy disabled, prepare connection soft-stop", QMUX_EV_QCC_WAKE, qcc->conn);
422 if (remaining_window) {
423 /* We don't need to reset the expire if it would
424 * already happen before the close window end.
426 if (!tick_isset(qcc->task->expire) ||
427 tick_is_le(global.close_spread_end, qcc->task->expire)) {
428 /* Set an expire value shorter than the current value
429 * because the close spread window end comes earlier.
431 qcc->task->expire = tick_add(now_ms,
432 statistical_prng_range(remaining_window));
436 /* We are past the soft close window end, wake the timeout
437 * task up immediately.
439 qcc->task->expire = tick_add(now_ms, 0);
440 task_wakeup(qcc->task, TASK_WOKEN_TIMER);
444 task_queue(qcc->task);
447 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
450 /* Mark a stream as open if it was idle. This can be used on every
451 * successful emission/reception operation to update the stream state.
453 void qcs_idle_open(struct qcs *qcs)
455 /* This operation must not be used if the stream is already closed. */
456 BUG_ON_HOT(qcs->st == QC_SS_CLO);
458 if (qcs->st == QC_SS_IDLE) {
459 TRACE_STATE("opening stream", QMUX_EV_QCS_NEW, qcs->qcc->conn, qcs);
460 qcs->st = QC_SS_OPEN;
464 /* Close the local channel of <qcs> instance. */
465 void qcs_close_local(struct qcs *qcs)
467 TRACE_STATE("closing stream locally", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
469 /* The stream must have already been opened. */
470 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
472 /* This operation cannot be used multiple times. */
473 BUG_ON_HOT(qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO);
475 if (quic_stream_is_bidi(qcs->id)) {
476 qcs->st = (qcs->st == QC_SS_HREM) ? QC_SS_CLO : QC_SS_HLOC;
479 /* Only local uni streams are valid for this operation. */
480 BUG_ON_HOT(quic_stream_is_remote(qcs->qcc, qcs->id));
485 /* Returns true if <qcs> can be purged. */
486 int qcs_is_completed(struct qcs *qcs)
488 /* A stream is completed if fully closed and stconn released, or simply
489 * detached and everything already sent.
491 return (qcs->st == QC_SS_CLO && !qcs_sc(qcs)) ||
492 (qcs_is_close_local(qcs) && (qcs->flags & QC_SF_DETACH) &&
493 !(qcs->flags & QC_SF_TO_STOP_SENDING));
496 /* Close the remote channel of <qcs> instance. */
497 static void qcs_close_remote(struct qcs *qcs)
499 TRACE_STATE("closing stream remotely", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
501 /* The stream must have already been opened. */
502 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
504 /* This operation cannot be used multiple times. */
505 BUG_ON_HOT(qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO);
507 /* Ignore remote closure if QCS is already completed and thus already
508 * scheduled for purging. This is possible as a detached QCS may be
509 * considered as terminated even if only locally closed. This is
510 * mandatory to avoid a double insert in purge list. This approach also
511 * preserves QCS state on completion for better diagnostics.
513 if (qcs_is_completed(qcs)) {
514 TRACE_STATE("ignore close remote on completed stream", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
515 COUNT_IF(!(qcs->flags & QC_SF_DETACH), "QCS reported as completed prior remote closure but not detached.");
516 COUNT_IF(!LIST_INLIST(&qcs->el_send), "QCS reported as completed not in purge list");
520 if (quic_stream_is_bidi(qcs->id)) {
521 qcs->st = (qcs->st == QC_SS_HLOC) ? QC_SS_CLO : QC_SS_HREM;
524 /* Only remote uni streams are valid for this operation. */
525 BUG_ON_HOT(quic_stream_is_local(qcs->qcc, qcs->id));
529 /* Cancel STOP_SENDING emission as it is now unneeded. */
530 if (qcs->st == QC_SS_CLO && (qcs->flags & QC_SF_TO_STOP_SENDING)) {
531 qcs->flags &= ~QC_SF_TO_STOP_SENDING;
532 /* Remove from send_list. Necessary to ensure BUG_ON() below is not triggered. */
533 LIST_DEL_INIT(&qcs->el_send);
536 if (qcs_is_completed(qcs)) {
537 BUG_ON(LIST_INLIST(&qcs->el_send));
538 TRACE_STATE("add stream in purg_list", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
539 LIST_APPEND(&qcs->qcc->purg_list, &qcs->el_send);
543 int qcs_is_close_local(struct qcs *qcs)
545 return qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO;
548 int qcs_is_close_remote(struct qcs *qcs)
550 return qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO;
553 /* Allocate if needed buffer <ncbuf> for stream <qcs>.
555 * Returns the buffer instance or NULL on allocation failure.
557 static struct ncbuf *qcs_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
559 struct buffer buf = BUF_NULL;
561 if (ncb_is_null(ncbuf)) {
562 if (!b_alloc(&buf, DB_MUX_RX))
565 *ncbuf = ncb_make(buf.area, buf.size, 0);
572 /* Notify the stconn layer if initialized with TASK_WOKEN_MSG state and
573 * eventually TASK_WOKEN_IO.
575 static void qcs_alert(struct qcs *qcs)
577 unsigned int state = TASK_WOKEN_MSG;
579 TRACE_POINT(QMUX_EV_STRM_WAKE, qcs->qcc->conn, qcs);
584 qcs->subs->events = 0;
586 state |= TASK_WOKEN_IO;
588 tasklet_wakeup(qcs_sc(qcs)->wait_event.tasklet, state);
591 int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
593 struct qcc *qcc = qcs->qcc;
595 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
597 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
598 BUG_ON(qcs->subs && qcs->subs != es);
600 es->events |= event_type;
603 if (event_type & SUB_RETRY_RECV)
604 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
606 if (event_type & SUB_RETRY_SEND)
607 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
609 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
614 void qcs_notify_recv(struct qcs *qcs)
616 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
617 TRACE_POINT(QMUX_EV_STRM_WAKE, qcs->qcc->conn, qcs);
618 tasklet_wakeup(qcs->subs->tasklet, TASK_WOKEN_IO);
619 qcs->subs->events &= ~SUB_RETRY_RECV;
620 if (!qcs->subs->events)
625 void qcs_notify_send(struct qcs *qcs)
627 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
628 TRACE_POINT(QMUX_EV_STRM_WAKE, qcs->qcc->conn, qcs);
629 tasklet_wakeup(qcs->subs->tasklet, TASK_WOKEN_IO);
630 qcs->subs->events &= ~SUB_RETRY_SEND;
631 if (!qcs->subs->events)
636 const struct buffer *qcs_tx_buf_const(const struct qcs *qcs)
638 return conn_is_quic(qcs->qcc->conn) ?
639 qc_stream_buf_get(qcs->tx.stream) : &qcs->tx.qmux_buf;
642 struct buffer *qcs_tx_buf(struct qcs *qcs)
644 return conn_is_quic(qcs->qcc->conn) ?
645 qc_stream_buf_get(qcs->tx.stream) : &qcs->tx.qmux_buf;
648 /* Returns total number of bytes not already sent to quic-conn layer. */
649 uint64_t qcs_prep_bytes(const struct qcs *qcs)
651 const struct buffer *out = qcs_tx_buf_const(qcs);
652 uint64_t diff, base_off;
657 if (conn_is_quic(qcs->qcc->conn)) {
658 /* if ack_offset < buf_offset, it points to an older buffer. */
659 base_off = MAX(qcs->tx.stream->buf_offset, qcs->tx.stream->ack_offset);
660 diff = qcs->tx.fc.off_real - base_off;
661 return b_data(out) - diff;
668 /* Used as a callback for qc_stream_desc layer to notify about emission of a
669 * STREAM frame of <data> length starting at <offset>.
671 static void qcm_ctrl_send(struct qc_stream_desc *stream, uint64_t data, uint64_t offset)
673 struct qcs *qcs = stream->ctx;
674 struct qcc *qcc = qcs->qcc;
677 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
679 /* Real off MUST always be the greatest offset sent. */
680 BUG_ON(offset > qcs->tx.fc.off_real);
682 /* Check if the STREAM frame has already been notified. An empty FIN
683 * frame must not be considered retransmitted.
685 if (data && offset + data <= qcs->tx.fc.off_real) {
686 TRACE_DEVEL("offset already notified", QMUX_EV_QCS_SEND, qcc->conn, qcs);
690 /* An empty STREAM frame is only used to notify FIN. A retransmitted
691 * empty FIN cannot be notified as QCS will be unsubscribed first.
693 BUG_ON(!data && !(qcs->flags & QC_SF_FIN_STREAM));
697 diff = offset + data - qcs->tx.fc.off_real;
699 struct quic_fctl *fc_conn = &qcc->tx.fc;
700 struct quic_fctl *fc_strm = &qcs->tx.fc;
702 /* Ensure real offset never exceeds soft value. */
703 BUG_ON(fc_conn->off_real + diff > fc_conn->off_soft);
704 BUG_ON(fc_strm->off_real + diff > fc_strm->off_soft);
706 /* increase offset sum on connection */
707 if (qfctl_rinc(fc_conn, diff)) {
708 TRACE_STATE("connection flow-control reached",
709 QMUX_EV_QCS_SEND, qcc->conn);
712 /* increase offset on stream */
713 if (qfctl_rinc(fc_strm, diff)) {
714 TRACE_STATE("stream flow-control reached",
715 QMUX_EV_QCS_SEND, qcc->conn, qcs);
717 /* Release buffer if everything sent and buf is full or stream is waiting for room. */
718 if (!qcs_prep_bytes(qcs) &&
719 (b_full(&qcs->tx.stream->buf->buf) || qcs->flags & QC_SF_BLK_MROOM)) {
720 qc_stream_buf_release(qcs->tx.stream);
721 qcs->flags &= ~QC_SF_BLK_MROOM;
722 qcs_notify_send(qcs);
725 /* Add measurement for send rate. This is done at the MUX layer
726 * to account only for STREAM frames without retransmission.
728 increment_send_rate(diff, 0);
731 if (!qc_stream_buf_get(qcs->tx.stream) || !qcs_prep_bytes(qcs)) {
732 /* Remove stream from send_list if all was sent. */
733 LIST_DEL_INIT(&qcs->el_send);
734 TRACE_STATE("stream sent done", QMUX_EV_QCS_SEND, qcc->conn, qcs);
736 /* Everything sent, close the stream if no new data will come.
737 * This is the case if either FIN has been sent or upper layer
740 * This is also performed if peer has resetted its input as no
741 * more transfer should occur on this QCS instance.
742 * TODO this assumption may be invalid depending on QUIC stream
743 * type - app proto should be responsible to deal with RS.
745 if ((qcs->flags & (QC_SF_FIN_STREAM|QC_SF_DETACH)) ||
746 (!qcs_sc(qcs) && (qcs->flags & QC_SF_RECV_RESET))) {
747 /* Close stream locally. */
748 qcs_close_local(qcs);
750 if (qcs->flags & QC_SF_FIN_STREAM) {
751 qcs->tx.stream->flags |= QC_SD_FL_WAIT_FOR_FIN;
752 /* Reset flag to not emit multiple FIN STREAM frames. */
753 qcs->flags &= ~QC_SF_FIN_STREAM;
756 /* Unsubscribe from streamdesc when everything sent. */
757 qc_stream_desc_sub_send(qcs->tx.stream, NULL);
759 if (qcs_is_completed(qcs)) {
760 TRACE_STATE("add stream in purg_list", QMUX_EV_QCS_SEND, qcc->conn, qcs);
761 LIST_APPEND(&qcc->purg_list, &qcs->el_send);
767 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
770 /* Returns true if <qcc> buffer window does not have room for a new buffer. */
771 static inline int qcc_bufwnd_full(const struct qcc *qcc)
773 const struct quic_conn *qc = qcc->conn->handle.qc;
774 return qcc->tx.buf_in_flight >= qc->path->cwnd;
777 static void qcm_ctrl_room(struct qc_stream_desc *stream, uint64_t room)
779 /* Context is different for active and released streams. */
780 struct qcc *qcc = !(stream->flags & QC_SD_FL_RELEASE) ?
781 ((struct qcs *)stream->ctx)->qcc : stream->ctx;
782 qcc_notify_buf(qcc, room);
785 /* Report that one or several stream-desc buffers have been released for <qcc>
786 * connection. <free_size> represent the sum of freed buffers sizes. May also
787 * be used to notify about congestion window increase, in which case
788 * <free_size> can be nul.
790 void qcc_notify_buf(struct qcc *qcc, uint64_t free_size)
794 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
796 /* Cannot have a negative buf_in_flight counter */
797 BUG_ON(qcc->tx.buf_in_flight < free_size);
798 qcc->tx.buf_in_flight -= free_size;
800 if (qcc_bufwnd_full(qcc))
803 if (qcc->flags & QC_CF_CONN_FULL) {
804 TRACE_STATE("buf window now available", QMUX_EV_QCC_WAKE, qcc->conn);
805 qcc->flags &= ~QC_CF_CONN_FULL;
808 /* TODO an optimization would be to only wake up a limited count of QCS
809 * instances based on <free_size>. But it may not work if a woken QCS
810 * is in error and does not try to allocate a buffer, leaving the
811 * unwoken QCS indefinitely in the buflist.
813 while (!LIST_ISEMPTY(&qcc->buf_wait_list)) {
814 qcs = LIST_ELEM(qcc->buf_wait_list.n, struct qcs *, el_buf);
815 LIST_DEL_INIT(&qcs->el_buf);
816 tot_time_stop(&qcs->timer.buf);
817 qcs_notify_send(qcs);
820 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
823 /* A fatal error is detected locally for <qcc> connection. It should be closed
824 * with a CONNECTION_CLOSE using <err> code. Set <app> to true to indicate that
825 * the code must be considered as an application level error. This function
826 * must not be called more than once by connection. If <tevt> is non null, it
827 * is used as a connection level termination event code.
829 void qcc_set_error(struct qcc *qcc, int err, int app, int tevt)
831 /* This must not be called multiple times per connection. */
832 BUG_ON(qcc->flags & QC_CF_ERRL);
834 TRACE_STATE("connection on error", QMUX_EV_QCC_ERR, qcc->conn);
836 qcc->flags |= QC_CF_ERRL;
837 qcc->err = app ? quic_err_app(err) : quic_err_transport(err);
840 qcc_report_term_evt(qcc, tevt);
843 * Ensure qcc_io_send() will be conducted to convert QC_CF_ERRL in
844 * QC_CF_ERRL_DONE with CONNECTION_CLOSE frame emission. This may be
845 * unnecessary if we are currently in the MUX tasklet context, but it
846 * is too tedious too not forget a wakeup outside of this function for
849 tasklet_wakeup(qcc->wait_event.tasklet);
852 void qcc_report_term_evt(struct qcc *qcc, enum muxc_term_event_type type)
854 enum term_event_loc loc = tevt_loc_muxc;
856 if (qcc->flags & QC_CF_IS_BACK)
858 qcc->term_evts_log = tevt_report_event(qcc->term_evts_log, loc, type);
861 /* Increment glitch counter for <qcc> connection by <inc> steps. If configured
862 * threshold reached, close the connection with an error code.
864 int _qcc_report_glitch(struct qcc *qcc, int inc)
866 const int max = QUIC_TUNE_FB_CONN_GET(sec_glitches_threshold, qcc->conn);
868 qcc->glitches += inc;
869 if (max && qcc->glitches >= max && !(qcc->flags & QC_CF_ERRL) &&
870 (th_ctx->idle_pct <= global.tune.glitch_kill_maxidle)) {
871 if (qcc->app_ops->report_susp) {
872 qcc->app_ops->report_susp(qcc->ctx);
873 qcc_set_error(qcc, qcc->err.code, 1,
874 muxc_tevt_type_graceful_shut);
877 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0,
878 muxc_tevt_type_internal_err);
886 /* Returns the number of streams which can still be opened until flow-control limit. */
887 int qcc_fctl_avail_streams(const struct qcc *qcc, int bidi)
890 const uint64_t next = qcc->next_bidi_l / 4;
891 BUG_ON(qcc->rfctl.ms_bidi < next);
892 return qcc->rfctl.ms_bidi - next;
895 const uint64_t next = qcc->next_uni_l / 4;
896 BUG_ON(qcc->rfctl.ms_uni < next);
897 return qcc->rfctl.ms_uni - next;
901 /* Retrieves the maximum number of bidirectional remote streams that the peer
902 * will be allowed to use during <conn> connection lifetime. This is guaranteed
903 * to be a positive integer.
905 static uint64_t qcc_max_strm_bidi_remote(const struct connection *conn)
907 /* On FE side, streams may be limited by stream.max-total configuration. */
908 if (!conn_is_back(conn) && quic_tune.fe.stream_max_total)
909 return quic_tune.fe.stream_max_total;
910 return (uint64_t)1 << 60;
913 /* Open a locally initiated stream for the connection <qcc>. Set <bidi> for a
914 * bidirectional stream, else an unidirectional stream is opened. The next
915 * available ID on the connection will be used according to the stream type.
917 * Returns the allocated stream instance or NULL on error.
919 struct qcs *qcc_init_stream_local(struct qcc *qcc, int bidi)
925 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
928 /* Caller must ensure that max-streams peer flow-control won't be violated. */
929 BUG_ON(qcc->rfctl.ms_bidi * 4 < qcc->next_bidi_l);
930 next = &qcc->next_bidi_l;
931 type = conn_is_back(qcc->conn) ? QCS_CLT_BIDI : QCS_SRV_BIDI;
934 BUG_ON(qcc->rfctl.ms_uni * 4 < qcc->next_uni_l);
935 next = &qcc->next_uni_l;
936 type = conn_is_back(qcc->conn) ? QCS_CLT_UNI : QCS_SRV_UNI;
939 qcs = qcs_new(qcc, *next, type);
941 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0,
942 muxc_tevt_type_internal_err);
943 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_NEW, qcc->conn);
947 TRACE_PROTO("opening local stream", QMUX_EV_QCS_NEW, qcc->conn, qcs);
948 /* TODO emit STREAMS_BLOCKED if cannot create future streams. */
951 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
955 /* Open a remote initiated stream for the connection <qcc> with ID <id>. The
956 * caller is responsible to ensure that a stream with the same ID was not
957 * already opened. This function will also create all intermediaries streams
958 * with ID smaller than <id> not already opened before.
960 * Returns the allocated stream instance or NULL on error.
962 static struct qcs *qcc_init_stream_remote(struct qcc *qcc, uint64_t id)
964 struct qcs *qcs = NULL;
966 uint64_t *largest, max_id;
968 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
970 /* Function reserved to remote stream IDs. */
971 BUG_ON(quic_stream_is_local(qcc, id));
973 if (quic_stream_is_bidi(id)) {
974 largest = &qcc->largest_bidi_r;
975 type = conn_is_back(qcc->conn) ? QCS_SRV_BIDI : QCS_CLT_BIDI;
978 largest = &qcc->largest_uni_r;
979 type = conn_is_back(qcc->conn) ? QCS_SRV_UNI : QCS_CLT_UNI;
982 /* RFC 9000 4.6. Controlling Concurrency
984 * An endpoint that receives a frame with a stream ID exceeding the
985 * limit it has sent MUST treat this as a connection error of type
988 max_id = quic_stream_is_bidi(id) ? qcc->lfctl.ms_bidi * 4 :
989 qcc->lfctl.ms_uni * 4;
991 TRACE_ERROR("flow control error", QMUX_EV_QCS_NEW|QMUX_EV_PROTO_ERR, qcc->conn);
992 qcc_set_error(qcc, QC_ERR_STREAM_LIMIT_ERROR, 0,
993 muxc_tevt_type_proto_err);
997 /* Only stream ID not already opened can be used. */
998 BUG_ON(id < *largest);
1000 /* MAX_STREAMS emission must not allowed too big stream ID. */
1001 BUG_ON(*largest > QUIC_VARINT_8_BYTE_MAX);
1003 while (id >= *largest) {
1004 const char *str = *largest < id ? "initializing intermediary remote stream" :
1005 "initializing remote stream";
1007 qcs = qcs_new(qcc, *largest, type);
1009 TRACE_ERROR("stream fallocation failure", QMUX_EV_QCS_NEW, qcc->conn);
1010 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0,
1011 muxc_tevt_type_internal_err);
1015 TRACE_PROTO(str, QMUX_EV_QCS_NEW, qcc->conn, qcs);
1020 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
1024 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
1028 /* Mark <qcs> as reserved for metadata transfer. As such, future txbuf
1029 * allocation won't be accounted against connection limit.
1031 void qcs_send_metadata(struct qcs *qcs)
1033 if (conn_is_quic(qcs->qcc->conn)) {
1034 /* Reserved for stream with Tx capability. */
1035 BUG_ON(!qcs->tx.stream);
1036 /* Cannot use if some data already transferred for this stream. */
1037 BUG_ON(qcs->tx.stream->ack_offset || !eb_is_empty(&qcs->tx.stream->buf_tree));
1039 qc_stream_desc_sub_room(qcs->tx.stream, NULL);
1042 qcs->flags |= QC_SF_TXBUB_OOB;
1045 /* Instantiate a stream and its associated stconn and sedesc. This is necessary
1046 * to transfer data after a new request reception. <buf> can be used to forward
1047 * the first received request data. <fin> must be set if the whole request is
1050 * This function is only used on frontend side.
1052 * Note that if <qcs> is already fully closed, nothing is instantiated. This is
1053 * useful if a RESET_STREAM was already emitted in response to a STOP_SENDING.
1055 * Returns 0 on success else a negative error code. If <qcs> is already fully
1056 * closed and nothing is performed, it is considered as a success case.
1058 int qcs_attach_sc(struct qcs *qcs, struct buffer *buf, char fin)
1060 struct qcc *qcc = qcs->qcc;
1061 struct session *sess = qcc->conn->owner;
1063 TRACE_ENTER(QMUX_EV_STRM_RECV, qcc->conn, qcs);
1065 if (qcs->st == QC_SS_CLO) {
1066 TRACE_STATE("skip attach on already closed stream", QMUX_EV_STRM_RECV, qcc->conn, qcs);
1070 /* TODO duplicated from mux_h2 */
1071 sess->t_idle = ns_to_ms(now_ns - sess->accept_ts) - sess->t_handshake;
1073 /* first create the stream endpoint descriptor */
1074 qcs->sd = sedesc_new();
1076 TRACE_DEVEL("leaving on error", QMUX_EV_STRM_RECV, qcc->conn, qcs);
1080 qcs->sd->conn = qcc->conn;
1081 se_fl_set(qcs->sd, SE_FL_T_MUX | SE_FL_ORPHAN | SE_FL_NOT_FIRST);
1082 se_expect_no_data(qcs->sd);
1084 if (!(global.tune.no_zero_copy_fwd & NO_ZERO_COPY_FWD_QUIC_SND))
1085 se_fl_set(qcs->sd, SE_FL_MAY_FASTFWD_CONS);
1087 if (!sc_new_from_endp(qcs->sd, sess, buf)) {
1088 TRACE_DEVEL("leaving on error", QMUX_EV_STRM_RECV, qcc->conn, qcs);
1092 /* QCS must be identified as request stream prior to stconn instantiation. */
1093 BUG_ON(!(qcs->flags & QC_SF_HREQ_RECV));
1097 /* TODO duplicated from mux_h2 */
1098 sess->accept_date = date;
1099 sess->accept_ts = now_ns;
1100 sess->t_handshake = 0;
1103 /* A stream must have been registered for HTTP wait before attaching
1104 * it to sedesc. See <qcs_wait_http_req> for more info.
1106 BUG_ON_HOT(!LIST_INLIST(&qcs->el_opening));
1107 LIST_DEL_INIT(&qcs->el_opening);
1109 /* rcv_buf may be skipped if request is wholly received on attach.
1110 * Ensure that similar flags are set for FIN both on rcv_buf and here.
1113 TRACE_STATE("report end-of-input", QMUX_EV_STRM_RECV, qcc->conn, qcs);
1114 se_fl_set(qcs->sd, SE_FL_EOI);
1115 se_expect_data(qcs->sd);
1118 /* A QCS can be already locally closed before stream layer
1119 * instantiation. This notably happens if STOP_SENDING was the first
1120 * frame received for this instance. In this case, an error is
1121 * immediately to the stream layer to prevent transmission.
1123 * TODO it could be better to not instantiate at all the stream layer.
1124 * However, extra care is required to ensure QCS instance is released.
1126 if (unlikely(qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_RESET))) {
1127 TRACE_STATE("report early error", QMUX_EV_STRM_RECV, qcc->conn, qcs);
1128 se_fl_set_error(qcs->sd);
1131 /* Graceful shutdown is initiated as soon as max stream is reached. */
1132 if (qcs->id == (qcc_max_strm_bidi_remote(qcc->conn) - 1) * 4) {
1133 TRACE_STATE("initiate shutdown as max remote bidi stream reached", QMUX_EV_STRM_RECV, qcc->conn, qcs);
1134 qcc_app_shutdown(qcc);
1138 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcc->conn, qcs);
1142 /* Use this function for a stream <id> which is not in <qcc> stream tree. It
1143 * returns true if the associated stream is closed.
1145 static int qcc_stream_id_is_closed(struct qcc *qcc, uint64_t id)
1149 /* This function must only be used for stream not present in the stream tree. */
1150 BUG_ON_HOT(eb64_lookup(&qcc->streams_by_id, id));
1152 if (quic_stream_is_local(qcc, id)) {
1153 largest = quic_stream_is_uni(id) ? &qcc->next_uni_l :
1157 largest = quic_stream_is_uni(id) ? &qcc->largest_uni_r :
1158 &qcc->largest_bidi_r;
1161 return id < *largest;
1164 /* Retrieve the stream instance from <id> ID. This can be used when receiving
1165 * STREAM, STREAM_DATA_BLOCKED, RESET_STREAM, MAX_STREAM_DATA or STOP_SENDING
1166 * frames. Set to false <receive_only> or <send_only> if these particular types
1167 * of streams are not allowed. If the stream instance is found, it is stored in
1170 * Returns 0 on success else non-zero. On error, a RESET_STREAM or a
1171 * CONNECTION_CLOSE is automatically emitted. Beware that <out> may be NULL
1172 * on success if the stream has already been closed.
1174 int qcc_get_qcs(struct qcc *qcc, uint64_t id, int receive_only, int send_only,
1177 struct eb64_node *node;
1179 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1182 if (!receive_only && quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
1183 TRACE_ERROR("receive-only stream not allowed", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id);
1184 qcc_set_error(qcc, QC_ERR_STREAM_STATE_ERROR, 0,
1185 muxc_tevt_type_proto_err);
1189 if (!send_only && quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
1190 TRACE_ERROR("send-only stream not allowed", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id);
1191 qcc_set_error(qcc, QC_ERR_STREAM_STATE_ERROR, 0,
1192 muxc_tevt_type_proto_err);
1196 /* Search the stream in the connection tree. */
1197 node = eb64_lookup(&qcc->streams_by_id, id);
1199 *out = eb64_entry(node, struct qcs, by_id);
1200 TRACE_DEVEL("using stream from connection tree", QMUX_EV_QCC_RECV, qcc->conn, *out);
1204 /* Check if stream is already closed. */
1205 if (qcc_stream_id_is_closed(qcc, id)) {
1206 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
1207 /* Consider this as a success even if <out> is left NULL. */
1211 /* Create the stream. This is valid only for remote initiated one. A
1212 * local stream must have already been explicitly created by the
1213 * application protocol layer.
1215 if (quic_stream_is_local(qcc, id)) {
1216 /* RFC 9000 19.8. STREAM Frames
1218 * An endpoint MUST terminate the connection with error
1219 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
1220 * initiated stream that has not yet been created, or for a send-only
1223 TRACE_ERROR("locally initiated stream not yet created", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id);
1224 qcc_set_error(qcc, QC_ERR_STREAM_STATE_ERROR, 0,
1225 muxc_tevt_type_proto_err);
1229 /* Remote stream not found - try to open it. */
1230 *out = qcc_init_stream_remote(qcc, id);
1232 TRACE_ERROR("stream creation error", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
1238 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn, *out);
1242 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1246 /* Convert <b> out-of-order storage into a contiguous buffer. */
1247 static inline struct buffer qcs_b_dup(const struct qc_stream_rxbuf *b)
1250 const struct ncbuf *ncb = &b->ncb;
1251 return b_make(ncb_orig(ncb), ncb->size, ncb->head, ncb_data(ncb, 0));
1258 /* Transfer data into <qcs> stream <rxbuf> current Rx buffer from its directly
1259 * following buffer. This is useful when parsing was interrupted due to partial
1260 * data. If following buffer does not exists, nothing is done.
1262 * Returns 0 if data transfer was performed.
1264 static int qcs_transfer_rx_data(struct qcs *qcs, struct qc_stream_rxbuf *rxbuf)
1266 struct qc_stream_rxbuf *rxbuf_next;
1267 struct eb64_node *next;
1268 struct buffer b, b_next;
1269 enum ncb_ret ncb_ret;
1273 BUG_ON(ncb_is_full(&rxbuf->ncb));
1275 next = eb64_next(&rxbuf->off_node);
1279 rxbuf_next = container_of(next, struct qc_stream_rxbuf, off_node);
1280 if (rxbuf_next->off_node.key == rxbuf->off_end &&
1281 ncb_data(&rxbuf_next->ncb, 0)) {
1282 eb64_delete(&rxbuf->off_node);
1285 b = qcs_b_dup(rxbuf);
1286 b_next = qcs_b_dup(rxbuf_next);
1287 to_copy = MIN(b_data(&b_next), ncb_size(&rxbuf->ncb) - b_data(&b));
1289 ncb_ret = ncb_add(&rxbuf->ncb, ncb_data(&rxbuf->ncb, 0),
1290 b_head(&b_next), to_copy, NCB_ADD_COMPARE);
1291 BUG_ON(ncb_ret != NCB_RET_OK);
1293 ncb_ret = ncb_advance(&rxbuf_next->ncb, to_copy);
1294 BUG_ON(ncb_ret != NCB_RET_OK);
1296 rxbuf->off_node.key = qcs->rx.offset;
1297 rxbuf->off_end = qcs->rx.offset + b_data(&b) + to_copy;
1298 eb64_insert(&qcs->rx.bufs, &rxbuf->off_node);
1300 /* Increment next rxbuf offset. This must not exceed off_end. */
1301 rxbuf_next->off_node.key += to_copy;
1302 BUG_ON(rxbuf_next->off_node.key > rxbuf_next->off_end);
1303 /* Now reinsert next rxbuf unless it has been completely truncated. */
1304 if (rxbuf_next->off_node.key < rxbuf_next->off_end) {
1305 eb64_insert(&qcs->rx.bufs, &rxbuf_next->off_node);
1309 offer_buffers(NULL, 1);
1310 pool_free(pool_head_qc_stream_rxbuf, rxbuf_next);
1311 bdata_ctr_bdec(&qcs->rx.data);
1321 /* Returns the Rx buffer instance for <qcs> stream read offset. May be NULL if
1322 * not already allocated.
1324 static struct qc_stream_rxbuf *qcs_get_curr_rxbuf(struct qcs *qcs)
1326 struct eb64_node *node;
1327 struct qc_stream_rxbuf *buf;
1329 node = eb64_first(&qcs->rx.bufs);
1333 buf = container_of(node, struct qc_stream_rxbuf, off_node);
1334 if (qcs->rx.offset < buf->off_node.key) {
1335 /* first buffer allocated for a future offset */
1339 /* Ensures obsolete buffer are not kept inside QCS */
1340 BUG_ON(buf->off_end < qcs->rx.offset);
1344 /* Returns the amount of data readable at <qcs> stream on current buffer. Note
1345 * that this does account for hypothetical contiguous data divided on other
1346 * Rx buffers instances.
1348 static ncb_sz_t qcs_rx_avail_data(struct qcs *qcs)
1350 struct qc_stream_rxbuf *b = qcs_get_curr_rxbuf(qcs);
1351 return b ? ncb_data(&b->ncb, 0) : 0;
1354 /* Account for <bytes> in <qcc> connection flow-control. If needed, a MAX_DATA
1355 * frame is prepared for emission.
1357 static void qcc_account_for_conn_fctl(struct qcc *qcc, uint64_t bytes)
1359 struct quic_frame *frm;
1361 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1363 qcc->lfctl.offsets_consume += bytes;
1364 if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) {
1365 TRACE_DATA("increase conn credit via MAX_DATA", QMUX_EV_QCS_RECV, qcc->conn);
1366 frm = qc_frm_alloc(QUIC_FT_MAX_DATA);
1368 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0,
1369 muxc_tevt_type_internal_err);
1373 qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init;
1374 frm->max_data.max_data = qcc->lfctl.md;
1376 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
1377 tasklet_wakeup(qcc->wait_event.tasklet);
1381 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1384 /* Remove <bytes> from <buf> current Rx buffer of <qcs> stream. Flow-control
1385 * for received offsets may be allocated for the peer if needed.
1387 static void qcs_consume(struct qcs *qcs, uint64_t bytes, struct qc_stream_rxbuf *buf)
1389 struct qcc *qcc = qcs->qcc;
1390 struct quic_frame *frm;
1392 uint64_t diff, inc = 0;
1394 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
1396 /* <buf> must be current QCS Rx buffer. */
1397 BUG_ON_HOT(buf->off_node.key > qcs->rx.offset ||
1398 qcs->rx.offset >= buf->off_end);
1400 ret = ncb_advance(&buf->ncb, bytes);
1402 ABORT_NOW(); /* should not happens because removal only in data */
1405 qcs->rx.offset += bytes;
1406 /* QCS Rx offset must only point directly up to the next buffer. */
1407 BUG_ON_HOT(qcs->rx.offset > buf->off_end);
1409 /* Not necessary to emit a MAX_STREAM_DATA if all data received. */
1410 if (qcs->flags & QC_SF_SIZE_KNOWN)
1413 /* Check if a MAX_STREAM_DATA frame should be emitted, determined by
1414 * the consumed capacity. If no more than 2 Rx buffers can be allocated
1415 * per QCS, the limit is set to half the capacity. Else, the limit is
1416 * set to match bufsize.
1418 if (qcs->rx.msd - qcs->rx.msd_base < qcm_stream_rx_bufsz() * 2) {
1419 if ((qcs->rx.offset - qcs->rx.msd_base) * 2 >= qcs->rx.msd - qcs->rx.msd_base)
1420 inc = qcs->rx.offset - qcs->rx.msd_base;
1423 diff = qcs->rx.offset - qcs->rx.msd_base;
1424 while (diff >= qcm_stream_rx_bufsz()) {
1425 inc += qcm_stream_rx_bufsz();
1426 diff -= qcm_stream_rx_bufsz();
1431 frm = qcs->tx.msd_frm;
1433 frm = qc_frm_alloc(QUIC_FT_MAX_STREAM_DATA);
1435 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0,
1436 muxc_tevt_type_internal_err);
1440 frm->max_stream_data.id = qcs->id;
1441 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
1442 qcs->tx.msd_frm = frm;
1445 TRACE_DATA("increase stream credit via MAX_STREAM_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
1447 qcs->rx.msd_base += inc;
1449 frm->max_stream_data.max_stream_data = qcs->rx.msd;
1451 tasklet_wakeup(qcc->wait_event.tasklet);
1455 qcc_account_for_conn_fctl(qcc, bytes);
1457 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
1460 /* Decode the content of STREAM frames already received on the stream instance
1461 * <qcs> from the <qcc> connection.
1463 * It is safe to remove <qcs> from <qcc> recv_list after decoding is done. Even
1464 * if an error is returned, caller should consider that no further Rx
1465 * processing can be performed for the stream, until new bytes are available.
1467 * Returns the result of app_ops rcv_buf callback, which is the number of bytes
1468 * successfully transcoded, or a negative error code. If no error occurred but
1469 * decoding cannot proceed due to missing data, the return value is 0. The
1470 * value 0 may also be returned when dealing with a standalone FIN signal.
1472 static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
1474 struct qc_stream_rxbuf *rxbuf;
1478 int prev_glitches = qcc->glitches;
1480 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
1482 if (qcc->flags & QC_CF_ERRL) {
1483 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
1489 rxbuf = qcs_get_curr_rxbuf(qcs);
1490 b = qcs_b_dup(rxbuf);
1492 /* Signal FIN to application if STREAM FIN received with all data. */
1493 if (qcs_is_close_remote(qcs))
1496 if (!(qcs->flags & QC_SF_READ_ABORTED)) {
1497 ret = qcc->app_ops->rcv_buf(qcs, &b, fin);
1499 if (qcc->glitches != prev_glitches && !(qcc->flags & QC_CF_IS_BACK))
1500 session_add_glitch_ctr(qcc->conn->owner, qcc->glitches - prev_glitches);
1503 TRACE_ERROR("decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
1507 /* App layer cannot decode due to partial data, which is stored
1508 * at a Rx buffer boundary. Try to realign data if possible and
1511 if (!ret && rxbuf && !(qcs->flags & QC_SF_DEM_FULL) &&
1512 qcs->rx.offset + ncb_data(&rxbuf->ncb, 0) == rxbuf->off_end) {
1513 if (!qcs_transfer_rx_data(qcs, rxbuf)) {
1514 TRACE_DEVEL("restart parsing after data realignment", QMUX_EV_QCS_RECV, qcc->conn, qcs);
1519 if (qcs->flags & QC_SF_TO_RESET) {
1520 if (qcs_sc(qcs) && !se_fl_test(qcs->sd, SE_FL_ERROR|SE_FL_ERR_PENDING)) {
1521 se_fl_set_error(qcs->sd);
1527 TRACE_DATA("ignore read on stream", QMUX_EV_QCS_RECV, qcc->conn, qcs);
1533 qcs_consume(qcs, ret, rxbuf);
1535 if (ncb_is_empty(&rxbuf->ncb)) {
1536 qcs_free_rxbuf(qcs, rxbuf);
1538 /* Close QCS remotely if only one Rx buffer remains and
1539 * all data with FIN already stored in it. This is
1540 * necessary to be performed before app_ops rcv_buf to
1541 * ensure FIN is correctly signalled.
1543 if (qcs->flags & QC_SF_SIZE_KNOWN && !eb_is_empty(&qcs->rx.bufs)) {
1544 const ncb_sz_t avail = qcs_rx_avail_data(qcs);
1545 if (qcs->rx.offset + avail == qcs->rx.offset_max)
1546 qcs_close_remote(qcs);
1551 if (ret || (!b_data(&b) && fin))
1552 qcs_notify_recv(qcs);
1554 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
1558 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
1562 /* Allocate if needed and retrieve <qcs> stream buffer for data reception.
1564 * Returns buffer pointer. May be NULL on allocation failure.
1566 struct buffer *qcc_get_stream_rxbuf(struct qcs *qcs)
1568 return b_alloc(&qcs->rx.app_buf, DB_MUX_RX);
1571 /* Allocate if needed and retrieve <qcs> stream buffer for data emission.
1573 * <err> is an output argument which is useful to differentiate the failure
1574 * cause when the buffer cannot be allocated. It is set to 0 if the connection
1575 * buffer window is full. For fatal errors, its value is non-zero.
1577 * Streams reserved for application protocol metadata transfer are not subject
1578 * to the buffer limit per connection. Hence, for them only a memory error
1579 * can prevent a buffer allocation.
1581 * Returns buffer pointer. May be NULL on allocation failure, in which case
1582 * <err> will refer to the cause.
1584 struct buffer *qcc_get_stream_txbuf(struct qcs *qcs, int *err, int small)
1586 struct qcc *qcc = qcs->qcc;
1587 struct buffer *out = qcs_tx_buf(qcs);
1589 /* Stream must not try to reallocate a buffer if currently waiting for one. */
1590 BUG_ON(LIST_INLIST(&qcs->el_buf));
1594 if (conn_is_quic(qcc->conn) && !out) {
1595 if (likely(!(qcs->flags & QC_SF_TXBUB_OOB))) {
1596 if ((qcc->flags & QC_CF_CONN_FULL)) {
1597 LIST_APPEND(&qcc->buf_wait_list, &qcs->el_buf);
1598 tot_time_start(&qcs->timer.buf);
1602 if (qcc_bufwnd_full(qcc)) {
1603 TRACE_STATE("no more room", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1604 LIST_APPEND(&qcc->buf_wait_list, &qcs->el_buf);
1605 tot_time_start(&qcs->timer.buf);
1606 qcc->flags |= QC_CF_CONN_FULL;
1611 out = qc_stream_buf_alloc(qcs->tx.stream, qcs->tx.fc.off_real, small);
1613 TRACE_ERROR("stream desc alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1618 if (likely(!(qcs->flags & QC_SF_TXBUB_OOB)))
1619 qcc->tx.buf_in_flight += b_size(out);
1621 else if (!conn_is_quic(qcc->conn)) {
1622 if ((small && !b_alloc_small(out)) ||
1623 (!small && !b_alloc(out, DB_MUX_TX))) {
1624 TRACE_ERROR("QCS tx buf alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1634 /* Reallocate <qcs> stream buffer to convert a small buffer to a bigger one.
1635 * Contrary to standard allocation, this function will never stop due to a full
1636 * buffer window. The smaller buffer is released first which guarantees that the
1637 * buffer window has room left.
1639 * Returns buffer pointer or NULL on allocation failure.
1641 struct buffer *qcc_realloc_stream_txbuf(struct qcs *qcs)
1643 struct qcc *qcc = qcs->qcc;
1644 struct buffer *out = qcs_tx_buf(qcs);
1646 /* Stream must not try to reallocate a buffer if currently waiting for one. */
1647 BUG_ON(LIST_INLIST(&qcs->el_buf));
1649 if (conn_is_quic(qcc->conn)) {
1650 if (likely(!(qcs->flags & QC_SF_TXBUB_OOB))) {
1651 /* Reduce buffer window. As such there is always some space
1652 * left for a new buffer allocation.
1654 BUG_ON(qcc->tx.buf_in_flight < b_size(out));
1655 qcc->tx.buf_in_flight -= b_size(out);
1658 out = qc_stream_buf_realloc(qcs->tx.stream);
1660 TRACE_ERROR("buffer alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1664 if (likely(!(qcs->flags & QC_SF_TXBUB_OOB)))
1665 qcc->tx.buf_in_flight += b_size(out);
1669 if (!b_alloc(out, DB_MUX_TX)) {
1670 TRACE_ERROR("QCS tx buf alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1676 return out && b_size(out) ? out : NULL;
1679 /* Try to realign <out> buffer for <qcs> stream. This is done only if there is
1680 * no data waiting for ACK.
1682 * Returns 0 if realign was performed else non-zero.
1684 int qcc_realign_stream_txbuf(const struct qcs *qcs, struct buffer *out)
1686 if (qcs_prep_bytes(qcs) == b_data(out)) {
1687 b_slow_realign(out, trash.area, b_data(out));
1694 /* Release the current <qcs> Tx buffer. This is useful if space left is not
1695 * enough anymore. A new instance can then be allocated to continue sending.
1697 * This operation fails if there is not yet sent bytes in the buffer. In this
1698 * case, stream layer should interrupt sending until further notification.
1700 * Returns 0 if buffer is released and a new one can be allocated or non-zero
1701 * if there is still remaining data.
1703 int qcc_release_stream_txbuf(struct qcs *qcs)
1705 const uint64_t bytes = qcs_prep_bytes(qcs);
1707 /* Cannot release buffer if prepared data is not fully sent. */
1709 qcs->flags |= QC_SF_BLK_MROOM;
1713 qc_stream_buf_release(qcs->tx.stream);
1717 /* Returns true if stream layer can proceed to emission via <qcs>. */
1718 int qcc_stream_can_send(const struct qcs *qcs)
1720 return !(qcs->flags & QC_SF_BLK_MROOM) && !LIST_INLIST(&qcs->el_buf);
1723 /* Wakes up every streams of <qcc> which are currently waiting for sending but
1724 * are blocked on connection flow control.
1726 static void qcc_notify_fctl(struct qcc *qcc)
1730 while (!LIST_ISEMPTY(&qcc->fctl_list)) {
1731 qcs = LIST_ELEM(qcc->fctl_list.n, struct qcs *, el_fctl);
1732 LIST_DEL_INIT(&qcs->el_fctl);
1733 tot_time_stop(&qcs->timer.fctl);
1734 qcs_notify_send(qcs);
1738 /* Free <qcc> STREAM frames in Tx list. */
1739 static void qcc_clear_frms(struct qcc *qcc)
1741 TRACE_STATE("resetting STREAM frames list", QMUX_EV_QCC_SEND, qcc->conn);
1742 while (!LIST_ISEMPTY(&qcc->tx.frms)) {
1743 struct quic_frame *frm = LIST_ELEM(qcc->tx.frms.n, struct quic_frame *, list);
1744 qc_frm_free(conn_is_quic(qcc->conn) ? qcc->conn->handle.qc : NULL, &frm);
1748 /* Register <qcs> stream for emission of STREAM, STOP_SENDING or RESET_STREAM.
1749 * Set <urg> to true if stream should be emitted in priority. This is useful
1750 * when sending STOP_SENDING or RESET_STREAM, or for emission on an application
1753 static void _qcc_send_stream(struct qcs *qcs, int urg)
1755 struct qcc *qcc = qcs->qcc;
1757 qcc_clear_frms(qcc);
1760 /* qcc_emit_rs_ss() relies on reset/aborted streams in send_list front. */
1761 BUG_ON(!(qcs->flags & (QC_SF_TO_RESET|QC_SF_TO_STOP_SENDING|QC_SF_TXBUB_OOB)));
1763 LIST_DEL_INIT(&qcs->el_send);
1764 LIST_INSERT(&qcc->send_list, &qcs->el_send);
1767 if (!LIST_INLIST(&qcs->el_send))
1768 LIST_APPEND(&qcs->qcc->send_list, &qcs->el_send);
1772 /* Prepare for the emission of RESET_STREAM on <qcs> with error code <err>. If
1773 * <tevt> is non null, it is used as a stream level termination event code.
1775 void qcc_reset_stream(struct qcs *qcs, int err, int tevt)
1777 struct qcc *qcc = qcs->qcc;
1778 const uint64_t diff = qcs_prep_bytes(qcs);
1780 if ((qcs->flags & QC_SF_TO_RESET) || qcs_is_close_local(qcs))
1783 /* TODO if QCS waiting for buffer, it could be removed from
1784 * <qcc.buf_wait_list> if sending is closed now.
1787 TRACE_STATE("reset stream", QMUX_EV_QCS_END, qcc->conn, qcs);
1788 qcs->flags |= QC_SF_TO_RESET;
1791 /* On BE side, a QCS may be resetted before any data emission. */
1792 if (conn_is_back(qcs->qcc->conn))
1796 const int soft_blocked = qfctl_sblocked(&qcc->tx.fc);
1798 /* Soft offset cannot be inferior to real one. */
1799 BUG_ON(qcc->tx.fc.off_soft - diff < qcc->tx.fc.off_real);
1801 /* Subtract to conn flow control data amount prepared on stream not yet sent. */
1802 qcc->tx.fc.off_soft -= diff;
1803 if (soft_blocked && !qfctl_sblocked(&qcc->tx.fc))
1804 qcc_notify_fctl(qcc);
1806 /* Reset QCS soft off to prevent BUG_ON() on qcs_destroy(). */
1807 qcs->tx.fc.off_soft = qcs->tx.fc.off_real;
1810 /* Report send error to stream-endpoint layer. */
1812 se_fl_set_error(qcs->sd);
1814 se_report_term_evt(qcs->sd, tevt);
1818 _qcc_send_stream(qcs, 1);
1819 tasklet_wakeup(qcc->wait_event.tasklet);
1822 /* Register <qcs> stream for STREAM emission. Set <urg> to 1 if stream content
1823 * should be treated in priority compared to other streams. <count> must
1824 * contains the size of the frame payload, used for flow control accounting.
1826 void qcc_send_stream(struct qcs *qcs, int urg, int count)
1828 struct qcc *qcc = qcs->qcc;
1830 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1832 /* Cannot send STREAM if already closed. */
1833 BUG_ON(qcs_is_close_local(qcs));
1835 _qcc_send_stream(qcs, urg);
1838 qfctl_sinc(&qcc->tx.fc, count);
1839 qfctl_sinc(&qcs->tx.fc, count);
1840 if (conn_is_quic(qcc->conn))
1841 bdata_ctr_add(&qcs->tx.stream->data, count);
1844 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1847 /* Prepare for the emission of STOP_SENDING on <qcs>. */
1848 void qcc_abort_stream_read(struct qcs *qcs, int err)
1850 struct qcc *qcc = qcs->qcc;
1852 TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn, qcs);
1854 if ((qcs->flags & QC_SF_READ_ABORTED) || qcs_is_close_remote(qcs))
1857 TRACE_STATE("abort stream read", QMUX_EV_QCS_END, qcc->conn, qcs);
1858 qcs->flags |= (QC_SF_TO_STOP_SENDING|QC_SF_READ_ABORTED);
1861 _qcc_send_stream(qcs, 1);
1862 tasklet_wakeup(qcc->wait_event.tasklet);
1865 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn, qcs);
1868 /* Install the applicative layer of a QUIC connection on mux <qcc>.
1869 * Returns 0 on success else non-zero.
1871 int qcc_install_app_ops(struct qcc *qcc)
1873 struct connection *conn = qcc->conn;
1874 const struct qcc_app_ops *app_ops;
1879 TRACE_ENTER(QMUX_EV_QCC_NEW, conn);
1881 if (!conn_get_alpn(conn, &alpn, &alpn_len)) {
1882 if (!conn_is_back(conn))
1885 srv = __objt_server(conn->target);
1886 HA_RWLOCK_RDLOCK(SERVER_LOCK, &srv->path_params.param_lock);
1887 alpn = srv->path_params.nego_alpn;
1888 HA_RWLOCK_RDUNLOCK(SERVER_LOCK, &srv->path_params.param_lock);
1892 alpn_len = strlen(srv->path_params.nego_alpn);
1895 app_ops = quic_alpn_to_app_ops(alpn, alpn_len);
1899 if (app_ops->init && !app_ops->init(qcc)) {
1900 TRACE_ERROR("application layer install error", QMUX_EV_QCC_NEW, conn);
1904 TRACE_PROTO("application layer installed", QMUX_EV_QCC_NEW, conn);
1905 qcc->app_ops = app_ops;
1907 TRACE_LEAVE(QMUX_EV_QCC_NEW, conn);
1911 TRACE_LEAVE(QMUX_EV_QCC_NEW, conn);
1915 /* Retrieves the Rx buffer instance usable to store STREAM data starting at
1916 * <offset>. It is dynamically allocated if not already instantiated. <len>
1917 * must contains the size of the STREAM frame. It may be reduced by the
1918 * function if data is too large relative to the buffer starting offset.
1919 * Another buffer instance should be allocated to store the remaining data.
1921 * Returns the buffer instance or NULL in case of error.
1923 static struct qc_stream_rxbuf *qcs_get_rxbuf(struct qcs *qcs, uint64_t offset,
1926 struct qcc *qcc = qcs->qcc;
1927 struct eb64_node *node;
1928 struct qc_stream_rxbuf *buf;
1929 struct ncbuf *ncbuf;
1931 TRACE_ENTER(QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
1933 node = eb64_lookup_le(&qcs->rx.bufs, offset);
1935 buf = container_of(node, struct qc_stream_rxbuf, off_node);
1937 if (!node || offset >= buf->off_end) {
1938 const uint64_t aligned_off = offset - (offset % qcm_stream_rx_bufsz());
1940 TRACE_DEVEL("allocating a new entry", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
1941 buf = pool_alloc(pool_head_qc_stream_rxbuf);
1943 TRACE_ERROR("qcs rxbuf alloc error", QMUX_EV_QCC_RECV, qcc->conn, qcs);
1947 buf->ncb = NCBUF_NULL;
1948 buf->off_node.key = aligned_off;
1949 buf->off_end = aligned_off + qcm_stream_rx_bufsz();
1950 eb64_insert(&qcs->rx.bufs, &buf->off_node);
1951 bdata_ctr_binc(&qcs->rx.data);
1955 if (!qcs_get_ncbuf(qcs, ncbuf) || ncb_is_null(ncbuf)) {
1956 TRACE_ERROR("receive ncbuf alloc failure", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1960 if (offset + *len > buf->off_end)
1961 *len = buf->off_end - offset;
1963 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
1967 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
1971 /* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
1972 * <data> with length <len> and represents the offset <offset>. <fin> is set if
1973 * the QUIC frame FIN bit is set.
1975 * Returns 0 on success else non-zero. On error, the received frame should not
1978 int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
1979 char fin, char *data)
1981 const int fin_standalone = (!len && fin);
1983 enum ncb_ret ncb_ret;
1987 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1989 if (qcc->flags & QC_CF_ERRL) {
1990 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
1994 /* RFC 9000 19.8. STREAM Frames
1996 * An endpoint MUST terminate the connection with error
1997 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
1998 * initiated stream that has not yet been created, or for a send-only
2001 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
2002 TRACE_DATA("qcs retrieval error", QMUX_EV_QCC_RECV, qcc->conn);
2007 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV, qcc->conn);
2011 /* RFC 9000 4.5. Stream Final Size
2013 * Once a final size for a stream is known, it cannot change. If a
2014 * RESET_STREAM or STREAM frame is received indicating a change in the
2015 * final size for the stream, an endpoint SHOULD respond with an error
2016 * of type FINAL_SIZE_ERROR; see Section 11 for details on error
2019 if (((qcs->flags & QC_SF_SIZE_KNOWN) && offset + len > qcs->rx.offset_max) ||
2020 (fin && offset + len < qcs->rx.offset_max)) {
2021 TRACE_ERROR("final size error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR, qcc->conn, qcs);
2022 qcc_set_error(qcc, QC_ERR_FINAL_SIZE_ERROR, 0,
2023 muxc_tevt_type_proto_err);
2027 if (qcs_is_close_remote(qcs)) {
2028 TRACE_DATA("skipping STREAM for remotely closed", QMUX_EV_QCC_RECV, qcc->conn);
2032 if (offset + len < qcs->rx.offset ||
2033 (offset + len == qcs->rx.offset && (!fin || (qcs->flags & QC_SF_SIZE_KNOWN)))) {
2034 TRACE_DATA("already received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
2038 TRACE_PROTO("receiving STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
2041 if (offset + len > qcs->rx.offset_max) {
2042 uint64_t diff = offset + len - qcs->rx.offset_max;
2043 qcs->rx.offset_max = offset + len;
2044 qcc->lfctl.offsets_recv += diff;
2046 if (offset + len > qcs->rx.msd ||
2047 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
2048 /* RFC 9000 4.1. Data Flow Control
2050 * A receiver MUST close the connection with an error
2051 * of type FLOW_CONTROL_ERROR if the sender violates
2052 * the advertised connection or stream data limits
2054 TRACE_ERROR("flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
2056 qcc_set_error(qcc, QC_ERR_FLOW_CONTROL_ERROR, 0,
2057 muxc_tevt_type_proto_err);
2062 TRACE_DATA("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
2063 if (offset < qcs->rx.offset) {
2064 size_t diff = qcs->rx.offset - offset;
2068 offset = qcs->rx.offset;
2071 if (len && (qcc->flags & QC_CF_WAIT_HS)) {
2072 if (!(qcc->conn->flags & CO_FL_EARLY_DATA)) {
2073 /* Ensure 'Early-data: 1' will be set on the request. */
2074 TRACE_PROTO("received early data", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
2075 qcc->conn->flags |= CO_FL_EARLY_DATA;
2081 struct qc_stream_rxbuf *buf;
2083 struct quic_counters *prx_counters;
2086 buf = qcs_get_rxbuf(qcs, offset, &len);
2088 TRACE_ERROR("rxbuf alloc failure", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
2089 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0,
2090 muxc_tevt_type_internal_err);
2094 /* For oldest buffer, ncb_advance() may already have been performed. */
2095 ncb_off = offset - MAX(qcs->rx.offset, buf->off_node.key);
2097 ncb_ret = ncb_add(&buf->ncb, ncb_off, data, len, NCB_ADD_COMPARE);
2102 case NCB_RET_DATA_REJ:
2103 /* RFC 9000 2.2. Sending and Receiving Data
2105 * An endpoint could receive data for a stream at the
2106 * same stream offset multiple times. Data that has
2107 * already been received can be discarded. The data at
2108 * a given offset MUST NOT change if it is sent
2109 * multiple times; an endpoint MAY treat receipt of
2110 * different data at the same offset within a stream as
2111 * a connection error of type PROTOCOL_VIOLATION.
2113 TRACE_ERROR("overlapping data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
2115 qcc_set_error(qcc, QC_ERR_PROTOCOL_VIOLATION, 0,
2116 muxc_tevt_type_proto_err);
2119 case NCB_RET_GAP_SIZE:
2120 TRACE_DATA("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
2123 prx_counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, &quic_stats_module);
2124 HA_ATOMIC_INC(&prx_counters->ncbuf_gap_limit);
2135 qcs->flags |= QC_SF_SIZE_KNOWN;
2137 if (qcs->flags & QC_SF_SIZE_KNOWN &&
2138 qcs->rx.offset_max == qcs->rx.offset + qcs_rx_avail_data(qcs)) {
2139 qcs_close_remote(qcs);
2142 while ((qcs_rx_avail_data(qcs) && !(qcs->flags & QC_SF_DEM_FULL)) ||
2143 unlikely(fin_standalone && qcs_is_close_remote(qcs))) {
2145 ret = qcc_decode_qcs(qcc, qcs);
2146 LIST_DEL_INIT(&qcs->el_recv);
2147 qcc_refresh_timeout(qcc);
2152 BUG_ON_HOT(fin_standalone); /* On fin_standalone <ret> should be NULL, which ensures no infinite loop. */
2155 /* Ensure that an idle backend conn is freed if it cannot open new
2156 * stream. This is only performed for QUIC which directly calls
2157 * qcc_recv() and is not compatible with QMux. For the latter, dead
2158 * connection should still be detected after recv via qcc_io_process().
2160 if (conn_is_quic(qcc->conn) && conn_is_back(qcc->conn) && qcc_is_dead(qcc)) {
2161 TRACE_STATE("releasing dead connection after STREAM decoding", QMUX_EV_QCC_RECV, qcc->conn);
2166 /* Wakeup tasklet if QCS inserted in purge list. May happen after remote closure. */
2167 if (qcs_is_completed(qcs))
2168 tasklet_wakeup(qcc->wait_event.tasklet);
2171 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
2175 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
2179 /* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
2182 * Returns 0 on success else non-zero.
2184 int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
2186 const int blocked_soft = qfctl_sblocked(&qcc->tx.fc);
2187 int unblock_soft = 0, unblock_real = 0;
2189 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
2191 TRACE_PROTO("receiving MAX_DATA", QMUX_EV_QCC_RECV, qcc->conn);
2192 if (qfctl_set_max(&qcc->tx.fc, max, &unblock_soft, &unblock_real)) {
2193 TRACE_DATA("increase remote max-data", QMUX_EV_QCC_RECV, qcc->conn);
2196 tasklet_wakeup(qcc->wait_event.tasklet);
2199 qcc_notify_fctl(qcc);
2201 /* Refresh frms list only if this would result in newer data :
2202 * a. flow-control is not real blocked
2203 * b. soft off was equal or greater than previous limit
2205 if (!qfctl_rblocked(&qcc->tx.fc) && blocked_soft)
2206 qcc_clear_frms(qcc);
2209 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
2213 /* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
2214 * field of the frame and <id> is the identifier of the QUIC stream.
2216 * Returns 0 on success else non-zero. On error, the received frame should not
2219 int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
2223 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
2225 if (qcc->flags & QC_CF_ERRL) {
2226 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
2230 /* RFC 9000 19.10. MAX_STREAM_DATA Frames
2232 * Receiving a MAX_STREAM_DATA frame for a locally
2233 * initiated stream that has not yet been created MUST be treated as a
2234 * connection error of type STREAM_STATE_ERROR. An endpoint that
2235 * receives a MAX_STREAM_DATA frame for a receive-only stream MUST
2236 * terminate the connection with error STREAM_STATE_ERROR.
2238 if (qcc_get_qcs(qcc, id, 0, 1, &qcs))
2242 const int blocked_soft = qfctl_sblocked(&qcs->tx.fc);
2243 int unblock_soft = 0, unblock_real = 0;
2245 TRACE_PROTO("receiving MAX_STREAM_DATA", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
2246 if (qfctl_set_max(&qcs->tx.fc, max, &unblock_soft, &unblock_real)) {
2247 TRACE_DATA("increase remote max-stream-data", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
2249 /* TODO optim: only wakeup IO-CB if stream has data to sent. */
2250 tasklet_wakeup(qcc->wait_event.tasklet);
2254 tot_time_stop(&qcs->timer.fctl);
2255 qcs_notify_send(qcs);
2258 /* Same refresh condition as qcc_recv_max_data(). */
2259 if (!qfctl_rblocked(&qcs->tx.fc) && blocked_soft)
2260 qcc_clear_frms(qcc);
2264 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
2265 qcc_refresh_timeout(qcc);
2267 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
2271 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
2275 /* Handle a MAX_STREAMS frame. <max> must contains the cumulative number of
2276 * streams that can be opened. <bidi> is a boolean set if this refers to
2277 * bidirectional streams.
2279 * Returns 0 on success else non-zero. On error, the received frame should not
2282 int qcc_recv_max_streams(struct qcc *qcc, uint64_t max, int bidi)
2284 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
2286 if (qcc->flags & QC_CF_ERRL) {
2287 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
2291 /* RFC 9000 19.11. MAX_STREAMS Frames
2293 * This value cannot exceed 2^60, as it is not possible to
2294 * encode stream IDs larger than 2^62-1. Receipt of a frame that
2295 * permits opening of a stream larger than this limit MUST be treated
2296 * as a connection error of type FRAME_ENCODING_ERROR.
2298 if (max > QUIC_VARINT_8_BYTE_MAX) {
2299 TRACE_ERROR("invalid MAX_STREAMS value", QMUX_EV_QCC_RECV, qcc->conn);
2300 qcc_set_error(qcc, QC_ERR_FRAME_ENCODING_ERROR, 0,
2301 muxc_tevt_type_proto_err);
2305 TRACE_PROTO("receiving MAX_STREAMS", QMUX_EV_QCC_RECV, qcc->conn);
2307 if (max > qcc->rfctl.ms_bidi) {
2308 TRACE_DATA("increase remote max-streams-bidi", QMUX_EV_QCC_RECV, qcc->conn);
2309 qcc->rfctl.ms_bidi = max;
2313 /* TODO no extra unidirectional streams open after connection
2314 * startup, so uni MAX_STREAMS flow-control is not necessary
2319 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
2323 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
2327 /* Handle a new RESET_STREAM frame from stream ID <id> with error code <err>
2328 * and final stream size <final_size>.
2330 * Returns 0 on success else non-zero. On error, the received frame should not
2333 int qcc_recv_reset_stream(struct qcc *qcc, uint64_t id, uint64_t err, uint64_t final_size)
2336 struct qc_stream_rxbuf *b;
2337 int prev_glitches = qcc->glitches;
2338 uint64_t to_consume;
2340 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
2342 if (qcc->flags & QC_CF_ERRL) {
2343 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
2347 /* RFC 9000 19.4. RESET_STREAM Frames
2349 * An endpoint that receives a RESET_STREAM frame for a send-only stream
2350 * MUST terminate the connection with error STREAM_STATE_ERROR.
2352 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
2353 TRACE_ERROR("RESET_STREAM for send-only stream received", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
2357 /* RFC 9000 3.2. Receiving Stream States
2359 * A RESET_STREAM signal might be suppressed or withheld
2360 * if stream data is completely received and is buffered to be read by
2361 * the application. If the RESET_STREAM is suppressed, the receiving
2362 * part of the stream remains in "Data Recvd".
2364 if (!qcs || qcs_is_close_remote(qcs))
2367 TRACE_PROTO("receiving RESET_STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
2370 /* Ensure stream closure is not forbidden by application protocol. */
2371 if (qcc->app_ops->close) {
2372 if (qcc->app_ops->close(qcs, QCC_APP_OPS_CLOSE_SIDE_RD)) {
2373 TRACE_ERROR("closure rejected by app layer", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
2378 if (qcs->rx.offset_max > final_size ||
2379 ((qcs->flags & QC_SF_SIZE_KNOWN) && qcs->rx.offset_max != final_size)) {
2380 TRACE_ERROR("final size error on RESET_STREAM",
2381 QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR, qcc->conn, qcs);
2382 qcc_set_error(qcc, QC_ERR_FINAL_SIZE_ERROR, 0,
2383 muxc_tevt_type_proto_err);
2387 /* Discarded data to account for in connection flow control. */
2388 to_consume = final_size - qcs->rx.offset;
2390 /* Ensure final size respects the current stream and connection flow control limits. */
2391 if (final_size > qcs->rx.msd ||
2392 qcc->lfctl.offsets_recv + to_consume > qcc->lfctl.md) {
2393 TRACE_ERROR("flow control violation on RESET_STREAM final size",
2394 QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR, qcc->conn, qcs);
2395 qcc_set_error(qcc, QC_ERR_FLOW_CONTROL_ERROR, 0,
2396 muxc_tevt_type_proto_err);
2400 qcc->lfctl.offsets_recv += to_consume;
2402 qcs->flags |= QC_SF_SIZE_KNOWN|QC_SF_RECV_RESET;
2403 qcs_close_remote(qcs);
2404 /* Wakeup tasklet if QCS inserted in purge list. May happen after remote closure. */
2405 if (qcs_is_completed(qcs))
2406 tasklet_wakeup(qcc->wait_event.tasklet);
2408 /* RFC 9000 3.2. Receiving Stream States
2411 * implementation MAY interrupt delivery of stream data, discard any
2412 * data that was not consumed, and signal the receipt of the
2415 while (!eb_is_empty(&qcs->rx.bufs)) {
2416 b = container_of(eb64_first(&qcs->rx.bufs),
2417 struct qc_stream_rxbuf, off_node);
2418 qcs_free_rxbuf(qcs, b);
2421 qcc_account_for_conn_fctl(qcc, to_consume);
2422 /* Ensure final size checks coherence for future STREAM frames. */
2423 qcs->rx.offset_max = final_size;
2425 /* Remove stream from recv_list if present. */
2426 LIST_DEL_INIT(&qcs->el_recv);
2428 /* No more data will be received after peer reset. QCS can be purged
2429 * if there is nothing to sent on our side.
2431 if (!qcs_sc(qcs) && !LIST_INLIST(&qcs->el_send)) {
2432 qcs_close_local(qcs);
2433 TRACE_STATE("add stream in purg_list", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
2434 LIST_APPEND(&qcs->qcc->purg_list, &qcs->el_send);
2435 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
2438 /* Wake-up upper stream layer to notify about reset on read. */
2443 if (qcc->glitches != prev_glitches && !(qcc->flags & QC_CF_IS_BACK))
2444 session_add_glitch_ctr(qcc->conn->owner, qcc->glitches - prev_glitches);
2446 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
2450 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
2454 /* Handle a new STOP_SENDING frame for stream ID <id>. The error code should be
2455 * specified in <err>.
2457 * Returns 0 on success else non-zero. On error, the received frame should not
2460 int qcc_recv_stop_sending(struct qcc *qcc, uint64_t id, uint64_t err)
2463 int prev_glitches = qcc->glitches;
2465 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
2467 if (qcc->flags & QC_CF_ERRL) {
2468 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
2472 /* RFC 9000 19.5. STOP_SENDING Frames
2474 * Receiving a STOP_SENDING frame for a
2475 * locally initiated stream that has not yet been created MUST be
2476 * treated as a connection error of type STREAM_STATE_ERROR. An
2477 * endpoint that receives a STOP_SENDING frame for a receive-only stream
2478 * MUST terminate the connection with error STREAM_STATE_ERROR.
2480 if (qcc_get_qcs(qcc, id, 0, 1, &qcs))
2486 TRACE_PROTO("receiving STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
2488 /* RFC 9000 3.5. Solicited State Transitions
2490 * An endpoint is expected to send another STOP_SENDING frame if a
2491 * packet containing a previous STOP_SENDING is lost. However, once
2492 * either all stream data or a RESET_STREAM frame has been received for
2493 * the stream -- that is, the stream is in any state other than "Recv"
2494 * or "Size Known" -- sending a STOP_SENDING frame is unnecessary.
2497 /* TODO thanks to previous RFC clause, STOP_SENDING is ignored if current stream
2498 * has already been closed locally. This is useful to not emit multiple
2499 * RESET_STREAM for a single stream. This is functional if stream is
2500 * locally closed due to all data transmitted, but in this case the RFC
2501 * advises to use an explicit RESET_STREAM.
2503 if (qcs_is_close_local(qcs)) {
2504 TRACE_STATE("ignoring STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
2510 if (qcc->app_ops->close) {
2511 if (qcc->app_ops->close(qcs, QCC_APP_OPS_CLOSE_SIDE_WR)) {
2512 TRACE_ERROR("closure rejected by app layer", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
2518 /* Manually set EOS if FIN already reached as futures RESET_STREAM will be ignored in this case. */
2519 if (qcs_sc(qcs) && se_fl_test(qcs->sd, SE_FL_EOI)) {
2520 se_fl_set(qcs->sd, SE_FL_EOS);
2521 se_report_term_evt(qcs->sd, (qcc->flags & QC_CF_ERR_CONN ? se_tevt_type_rcv_err : se_tevt_type_eos));
2525 se_report_term_evt(qcs->sd, se_tevt_type_rst_rcvd);
2528 /* If not defined yet, set abort info for the sedesc */
2529 if (!qcs->sd->abort_info.info) {
2530 qcs->sd->abort_info.info = (SE_ABRT_SRC_MUX_QUIC << SE_ABRT_SRC_SHIFT);
2531 qcs->sd->abort_info.code = err;
2535 /* RFC 9000 3.5. Solicited State Transitions
2537 * An endpoint that receives a STOP_SENDING frame
2538 * MUST send a RESET_STREAM frame if the stream is in the "Ready" or
2539 * "Send" state. If the stream is in the "Data Sent" state, the
2540 * endpoint MAY defer sending the RESET_STREAM frame until the packets
2541 * containing outstanding data are acknowledged or declared lost. If
2542 * any outstanding data is declared lost, the endpoint SHOULD send a
2543 * RESET_STREAM frame instead of retransmitting the data.
2545 * An endpoint SHOULD copy the error code from the STOP_SENDING frame to
2546 * the RESET_STREAM frame it sends, but it can use any application error
2549 qcc_reset_stream(qcs, err, 0);
2551 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
2552 qcc_refresh_timeout(qcc);
2555 if (qcc->glitches != prev_glitches && !(qcc->flags & QC_CF_IS_BACK))
2556 session_add_glitch_ctr(qcc->conn->owner, qcc->glitches - prev_glitches);
2558 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
2562 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
2566 /* Signal the closing of remote stream with id <id>. Flow-control for new
2567 * streams may be allocated for the peer if needed.
2569 static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
2571 struct quic_frame *frm;
2572 uint64_t conn_max, rem, non_extra, inc;
2574 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn);
2576 if (quic_stream_is_bidi(id)) {
2577 const uint64_t max = qcc_max_strm_bidi_remote(qcc->conn);
2578 /* The peer must not have been authorized to open a stream outside of this range. */
2579 BUG_ON(qcc->lfctl.ms_bidi > max);
2580 if (qcc->lfctl.ms_bidi == max) {
2581 TRACE_DATA("maximum streams value reached", QMUX_EV_QCC_SEND, qcc->conn);
2585 ++qcc->lfctl.cl_bidi_r;
2586 /* MAX_STREAMS needed if closed streams value more than twice
2587 * the initial window or reaching the stream ID limit.
2589 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_rel / 2 ||
2590 qcc->lfctl.cl_bidi_r + qcc->lfctl.ms_bidi == max) {
2592 BUG_ON(qcc->lfctl.ms_bidi_rel < qcc->lfctl.cl_bidi_r);
2593 rem = qcc->lfctl.ms_bidi_rel - qcc->lfctl.cl_bidi_r;
2594 /* if every streams are closed, decrement extra stream accounting by 1 */
2595 non_extra = !rem ? 1 : 0;
2597 if (!(qcc->flags & QC_CF_IS_BACK) && global.tune.streams_elasticity) {
2598 /* If stream elasticity is active, first decrement closed from extra streams. */
2599 if (qcc->lfctl.ms_bidi_rel > 1) {
2600 _HA_ATOMIC_SUB(&tg_ctx->committed_extra_streams,
2601 qcc->lfctl.cl_bidi_r - non_extra);
2604 /* Now calculate the available streams. */
2605 conn_max = conn_calc_max_streams(qcc->lfctl.ms_bidi_init);
2606 if (conn_max <= rem) {
2607 /* More streams already consumed than currently allowed,
2608 * keep the current flow control limit.
2610 qcc->lfctl.ms_bidi_rel = rem;
2611 qcc->lfctl.cl_bidi_r = 0;
2615 /* Update flow control limit up to the allowed elasticity limit. */
2616 inc = conn_max - rem;
2617 _HA_ATOMIC_ADD(&tg_ctx->committed_extra_streams, inc - non_extra);
2618 qcc->lfctl.ms_bidi_rel = rem + inc;
2621 /* Stream elasticity not active, flow control increase remains static. */
2622 inc = qcc->lfctl.cl_bidi_r;
2625 TRACE_DATA("increase max stream limit with MAX_STREAMS_BIDI", QMUX_EV_QCC_SEND, qcc->conn);
2626 frm = qc_frm_alloc(QUIC_FT_MAX_STREAMS_BIDI);
2628 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0,
2629 muxc_tevt_type_internal_err);
2633 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi + inc;
2634 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
2635 tasklet_wakeup(qcc->wait_event.tasklet);
2637 qcc->lfctl.ms_bidi += inc;
2638 qcc->lfctl.cl_bidi_r = 0;
2642 /* TODO unidirectional stream flow control with MAX_STREAMS_UNI
2643 * emission not implemented. It should be unnecessary for
2644 * HTTP/3 but may be required if other application protocols
2650 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
2654 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_END, qcc->conn);
2658 /* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
2659 static void qcs_destroy(struct qcs *qcs)
2661 struct qcc *qcc = qcs->qcc;
2662 struct connection *conn = qcc->conn;
2663 const uint64_t id = qcs->id;
2665 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
2667 if (!(qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL))) {
2668 /* MUST not removed a stream with sending prepared data left. This is
2669 * to ensure consistency on connection flow-control calculation.
2671 BUG_ON(qcs->tx.fc.off_soft != qcs->tx.fc.off_real);
2673 if (quic_stream_is_remote(qcc, id))
2674 qcc_release_remote_stream(qcc, id);
2677 /* If some data are not yet consumed, account them in conn flow
2678 * control. On frontend, this may happen when FE stream is released
2679 * prior to request completion.
2681 * TODO QCS should not be released if not closed remotely to guarantee
2682 * the connection flow control consistency
2684 if (!qcs_is_close_remote(qcs) && qcs->rx.offset_max > qcs->rx.offset) {
2685 COUNT_IF(1, "QCS is released while not remotely closed. "
2686 "This may cause miscalculation in conn flow control.");
2687 qcc_account_for_conn_fctl(qcc, qcs->rx.offset_max - qcs->rx.offset);
2692 /* Rearm http-keep-alive timeout when last request stream is freed. */
2693 if (!conn_is_back(qcc->conn) && qcc_may_expire(qcc) && !qcc->nb_hreq) {
2694 qcc_reset_idle_start(qcc);
2695 qcc_refresh_timeout(qcc);
2698 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
2701 /* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
2702 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
2703 * stream frame. If <out> is NULL an empty STREAM frame is built : this may be
2704 * useful if FIN needs to be sent without any data left. Frame length will be
2705 * truncated if greater than <fc_conn_wnd>. This allows to prepare several
2706 * frames in a loop while respecting connection flow control window.
2708 * Returns the payload length of the STREAM frame or a negative error code.
2710 static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
2711 struct list *frm_list, uint64_t window_conn)
2713 struct qcc *qcc = qcs->qcc;
2714 struct quic_frame *frm;
2715 const uint64_t window_stream = qfctl_rcap(&qcs->tx.fc);
2716 const uint64_t bytes = qcs_prep_bytes(qcs);
2719 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
2721 /* This must only be called if there is data left, or at least a standalone FIN. */
2722 BUG_ON((!out || !b_data(out)) && !fin);
2726 /* do not exceed stream flow control limit */
2727 if (total > window_stream) {
2728 TRACE_DATA("do not exceed stream flow control", QMUX_EV_QCS_SEND, qcc->conn, qcs);
2729 total = window_stream;
2732 /* do not exceed connection flow control limit */
2733 if (total > window_conn) {
2734 TRACE_DATA("do not exceed conn flow control", QMUX_EV_QCS_SEND, qcc->conn, qcs);
2735 total = window_conn;
2738 /* Reset FIN if bytes to send is capped by flow control. */
2742 if (!total && !fin) {
2743 /* No need to send anything if total is NULL and no FIN to signal. */
2744 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
2748 TRACE_PROTO("sending STREAM frame", QMUX_EV_QCS_SEND, qcc->conn, qcs);
2749 frm = qc_frm_alloc(QUIC_FT_STREAM_8);
2751 TRACE_ERROR("frame alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
2755 frm->stream.stream =
2756 conn_is_quic(qcc->conn) ? (void *)qcs->tx.stream : (void *)qcs;
2757 frm->stream.id = qcs->id;
2758 frm->stream.offset = 0;
2759 frm->stream.dup = 0;
2762 frm->stream.buf = out;
2763 frm->stream.data = (unsigned char *)b_peek(out, b_data(out) - bytes);
2766 /* Empty STREAM frame. */
2767 frm->stream.buf = NULL;
2768 frm->stream.data = NULL;
2771 /* FIN is positioned only when the buffer has been totally emptied. */
2773 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
2775 if (qcs->tx.fc.off_real) {
2776 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
2777 frm->stream.offset = qcs->tx.fc.off_real;
2780 /* Always set length bit as we do not know if there is remaining frames
2781 * in the final packet after this STREAM.
2783 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
2784 frm->stream.len = total;
2786 LIST_APPEND(frm_list, &frm->list);
2790 struct qcs_build_stream_trace_arg arg = {
2791 .len = frm->stream.len, .fin = fin,
2792 .offset = frm->stream.offset,
2794 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
2795 qcc->conn, qcs, &arg);
2801 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
2805 /* Returns true if subscribe set, false otherwise. */
2806 static int qcc_subscribe_send(struct qcc *qcc)
2808 struct connection *conn = qcc->conn;
2810 /* Do not subscribe if lower layer in error. */
2811 if (conn->flags & CO_FL_ERROR)
2814 if (qcc->wait_event.events & SUB_RETRY_SEND)
2817 TRACE_DEVEL("subscribe for send", QMUX_EV_QCC_SEND, qcc->conn);
2818 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &qcc->wait_event);
2822 static int qcc_quic_send_frames(struct qcc *qcc, struct list *frms, int stream)
2824 enum quic_tx_err ret;
2825 struct quic_pacer *pacer = NULL;
2827 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
2829 if (stream && qcc_is_pacing_active(qcc->conn))
2830 pacer = &qcc->tx.pacer;
2832 ret = qc_send_mux(qcc->conn->handle.qc, frms, pacer);
2833 if (ret == QUIC_TX_ERR_FATAL) {
2834 TRACE_DEVEL("error on sending", QMUX_EV_QCC_SEND, qcc->conn);
2835 qcc_subscribe_send(qcc);
2839 /* If there is frames left at this stage, transport layer is blocked.
2840 * Subscribe on it to retry later.
2842 if (!LIST_ISEMPTY(frms) && ret != QUIC_TX_ERR_PACING) {
2843 TRACE_DEVEL("remaining frames to send", QMUX_EV_QCC_SEND, qcc->conn);
2844 qcc_subscribe_send(qcc);
2848 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
2849 return ret == QUIC_TX_ERR_PACING ? 1 : 0;
2852 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
2856 /* Wrapper for send on transport layer. Send a list of frames <frms> for the
2859 * Returns 0 if all data sent with success. On fatal error, a negative error
2860 * code is returned. A positive 1 is used if emission should be paced.
2862 static int qcc_send_frames(struct qcc *qcc, struct list *frms, int stream)
2864 if (LIST_ISEMPTY(frms)) {
2865 TRACE_DEVEL("leaving on no frame to send", QMUX_EV_QCC_SEND, qcc->conn);
2869 return conn_is_quic(qcc->conn) ? qcc_quic_send_frames(qcc, frms, stream) :
2870 qcc_qmux_send_frames(qcc, frms);
2873 /* Emit a RESET_STREAM on <qcs>.
2875 * Returns 0 if the frame has been successfully sent else non-zero.
2877 static int qcs_send_reset(struct qcs *qcs)
2879 struct list frms = LIST_HEAD_INIT(frms);
2880 struct quic_frame *frm;
2882 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
2884 frm = qc_frm_alloc(QUIC_FT_RESET_STREAM);
2886 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
2890 frm->reset_stream.id = qcs->id;
2891 frm->reset_stream.app_error_code = qcs->rs_err;
2892 frm->reset_stream.final_size = qcs->tx.fc.off_real;
2894 LIST_APPEND(&frms, &frm->list);
2895 if (qcc_send_frames(qcs->qcc, &frms, 0)) {
2896 if (!LIST_ISEMPTY(&frms))
2897 qc_frm_free(conn_is_quic(qcs->qcc->conn) ? qcs->qcc->conn->handle.qc : NULL, &frm);
2898 TRACE_DEVEL("cannot send RESET_STREAM", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
2902 qcs_close_local(qcs);
2903 qcs->flags &= ~QC_SF_TO_RESET;
2905 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
2909 /* Emit a STOP_SENDING on <qcs>.
2911 * Returns 0 if the frame has been successfully sent else non-zero.
2913 static int qcs_send_stop_sending(struct qcs *qcs)
2915 struct list frms = LIST_HEAD_INIT(frms);
2916 struct quic_frame *frm;
2917 struct qcc *qcc = qcs->qcc;
2919 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
2921 /* RFC 9000 3.3. Permitted Frame Types
2924 * receiver MAY send a STOP_SENDING frame in any state where it has not
2925 * received a RESET_STREAM frame -- that is, states other than "Reset
2926 * Recvd" or "Reset Read". However, there is little value in sending a
2927 * STOP_SENDING frame in the "Data Recvd" state, as all stream data has
2928 * been received. A sender could receive either of these two types of
2929 * frames in any state as a result of delayed delivery of packets.¶
2931 if (qcs_is_close_remote(qcs)) {
2932 TRACE_STATE("skip STOP_SENDING on remote already closed", QMUX_EV_QCS_SEND, qcc->conn, qcs);
2936 frm = qc_frm_alloc(QUIC_FT_STOP_SENDING);
2938 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
2942 frm->stop_sending.id = qcs->id;
2943 frm->stop_sending.app_error_code = qcs->ss_err;
2945 LIST_APPEND(&frms, &frm->list);
2946 if (qcc_send_frames(qcs->qcc, &frms, 0)) {
2947 if (!LIST_ISEMPTY(&frms))
2948 qc_frm_free(conn_is_quic(qcc->conn) ? qcc->conn->handle.qc : NULL, &frm);
2949 TRACE_DEVEL("cannot send STOP_SENDING", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
2954 qcs->flags &= ~QC_SF_TO_STOP_SENDING;
2956 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
2960 /* Used internally by qcc_io_send function. Proceed to send for <qcs>. A STREAM
2961 * frame is generated pointing to QCS stream descriptor content and inserted in
2962 * <frms> list. Frame length will be truncated if greater than <window_conn>.
2963 * This allows to prepare several frames in a loop while respecting connection
2964 * flow control window.
2966 * Returns the payload length of the STREAM frame or a negative error code.
2968 static int qcs_send(struct qcs *qcs, struct list *frms, uint64_t window_conn)
2970 struct qcc *qcc = qcs->qcc;
2971 struct buffer *out = qcs_tx_buf(qcs);
2973 const char fin = qcs->flags & QC_SF_FIN_STREAM;
2975 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
2977 /* Cannot send STREAM on remote unidirectional streams. */
2978 BUG_ON(quic_stream_is_uni(qcs->id) && quic_stream_is_remote(qcc, qcs->id));
2980 /* This function must not be called if there is nothing to send. */
2981 BUG_ON(!fin && !qcs_prep_bytes(qcs));
2983 /* Skip STREAM frame allocation if already subscribed for send.
2984 * Happens on sendto transient error or network congestion.
2986 if (qcc->wait_event.events & SUB_RETRY_SEND) {
2987 TRACE_DEVEL("already subscribed for sending",
2988 QMUX_EV_QCS_SEND, qcc->conn, qcs);
2992 /* Build a new STREAM frame with <out> buffer. */
2993 flen = qcs_build_stream_frm(qcs, out, fin, frms, window_conn);
2998 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
3002 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
3006 /* Emit prepared flow-control related frames from <qcc> list.
3008 * Returns 0 on success else non zero.
3010 static int qcc_emit_fctl(struct qcc *qcc)
3012 struct list single_frm = LIST_HEAD_INIT(single_frm);
3013 struct quic_frame *frm;
3014 struct eb64_node *node;
3018 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
3020 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
3021 /* Each frame is sent individually in a single element list. */
3022 frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list);
3023 id = frm->type == QUIC_FT_MAX_STREAM_DATA ?
3024 frm->max_stream_data.id : -1;
3026 LIST_DELETE(&frm->list);
3027 LIST_APPEND(&single_frm, &frm->list);
3029 if (qcc_send_frames(qcc, &single_frm, 0)) {
3030 if (!LIST_ISEMPTY(&single_frm)) {
3031 /* replace frame that cannot be sent in qcc list. */
3032 LIST_DELETE(&frm->list);
3033 LIST_INSERT(&qcc->lfctl.frms, &frm->list);
3038 BUG_ON_HOT(!LIST_ISEMPTY(&single_frm));
3040 /* If frame is MAX_STREAM_DATA, reset corresponding QCS msd_frm pointer. */
3042 node = eb64_lookup(&qcc->streams_by_id, id);
3044 qcs = eb64_entry(node, struct qcs, by_id);
3045 qcs->tx.msd_frm = NULL;
3050 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
3054 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
3058 /* Send RESET_STREAM/STOP_SENDING for streams in <qcc> send_list if requested.
3059 * Each frame is encoded and emitted separately for now. If a frame cannot be
3060 * sent, send_list looping is interrupted.
3062 * Returns 0 on success else non-zero.
3064 static int qcc_emit_rs_ss(struct qcc *qcc)
3066 struct qcs *qcs, *qcs_tmp;
3068 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
3070 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_list, el_send) {
3071 /* Stream must not be present in send_list if it has nothing to send. */
3072 BUG_ON(!(qcs->flags & (QC_SF_FIN_STREAM|QC_SF_TO_STOP_SENDING|QC_SF_TO_RESET)) &&
3073 ((conn_is_quic(qcc->conn) && !qcs->tx.stream) || !qcs_prep_bytes(qcs)));
3075 /* Interrupt looping for the first stream where no RS nor SS is
3076 * necessary and is not use for "metadata" transfer. These
3077 * streams are always in front of the send_list.
3079 if (!(qcs->flags & (QC_SF_TO_STOP_SENDING|QC_SF_TO_RESET|QC_SF_TXBUB_OOB)))
3082 TRACE_DATA("prepare for RS/SS transfer", QMUX_EV_QCC_SEND, qcc->conn, qcs);
3084 /* Each RS and SS frame is sent individually. Necessary to
3085 * ensure it has been emitted as there is no transport callback
3088 * TODO multiplex frames to optimize sending. However, it may
3089 * not be advisable to mix different streams in the same dgram
3090 * to avoid interdependency in case of loss.
3093 if (qcs->flags & QC_SF_TO_STOP_SENDING) {
3094 if (qcs_send_stop_sending(qcs))
3097 /* Remove stream from send_list if only SS was necessary. */
3098 if (!(qcs->flags & (QC_SF_FIN_STREAM|QC_SF_TO_RESET)) &&
3099 ((conn_is_quic(qcc->conn) && !qcs->tx.stream) || !qcs_prep_bytes(qcs))) {
3100 LIST_DEL_INIT(&qcs->el_send);
3102 if (qcs_is_completed(qcs)) {
3103 TRACE_STATE("add stream in purg_list", QMUX_EV_QCC_SEND|QMUX_EV_QCS_SEND, qcc->conn, qcs);
3104 LIST_DEL_INIT(&qcs->el_send);
3105 LIST_APPEND(&qcc->purg_list, &qcs->el_send);
3111 if (qcs->flags & QC_SF_TO_RESET) {
3112 if (qcs_send_reset(qcs))
3115 /* RFC 9000 3.3. Permitted Frame Types
3117 * A sender MUST NOT send
3118 * a STREAM or STREAM_DATA_BLOCKED frame for a stream in the
3119 * "Reset Sent" state or any terminal state -- that is, after
3120 * sending a RESET_STREAM frame.
3122 LIST_DEL_INIT(&qcs->el_send);
3123 if (qcs_is_completed(qcs)) {
3124 TRACE_STATE("add stream in purg_list", QMUX_EV_QCC_SEND|QMUX_EV_QCS_SEND, qcc->conn, qcs);
3125 LIST_APPEND(&qcc->purg_list, &qcs->el_send);
3131 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
3135 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
3139 /* Encode STREAM frames into <qcc> tx frms for streams registered into
3140 * send_list. On each error, related stream is removed from send_list and
3141 * inserted into <qcs_failed> list.
3143 * This functions also serves to emit RESET_STREAM and STOP_SENDING frames. In
3144 * this case, frame is emitted immediately without using <qcc> tx frms. If an
3145 * error occurred during this step, this is considered as fatal. Tx frms is
3146 * cleared and 0 is returned.
3148 * Returns the sum of encoded payload STREAM frames length. Note that 0 can be
3149 * returned either if no frame was built or only empty payload frames were
3152 static int qcc_build_frms(struct qcc *qcc, struct list *qcs_failed)
3154 struct list *frms = &qcc->tx.frms;
3155 struct qcs *qcs, *qcs_tmp, *first_qcs = NULL;
3156 uint64_t window_conn = qfctl_rcap(&qcc->tx.fc);
3157 int ret = 0, total = 0;
3159 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
3161 /* Frames list must first be cleared via qcc_clear_frms(). */
3162 BUG_ON(!LIST_ISEMPTY(&qcc->tx.frms));
3164 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_list, el_send) {
3165 /* Check if all QCS were processed. */
3166 if (qcs == first_qcs)
3169 TRACE_DATA("prepare for data transfer", QMUX_EV_QCC_SEND, qcc->conn, qcs);
3171 /* Streams with RS/SS must be handled via qcc_emit_rs_ss(). */
3172 BUG_ON(qcs->flags & (QC_SF_TO_STOP_SENDING|QC_SF_TO_RESET));
3173 /* Stream must not be present in send_list if it has nothing to send. */
3174 BUG_ON(!(qcs->flags & QC_SF_FIN_STREAM) && ((conn_is_quic(qcc->conn) && !qcs->tx.stream) || !qcs_prep_bytes(qcs)));
3176 /* Total sent bytes must not exceed connection window. */
3177 BUG_ON(total > window_conn);
3179 if (!qfctl_rblocked(&qcc->tx.fc) &&
3180 !qfctl_rblocked(&qcs->tx.fc) && window_conn > total) {
3181 if ((ret = qcs_send(qcs, frms, window_conn - total)) < 0) {
3182 /* Temporarily remove QCS from send-list. */
3183 LIST_DEL_INIT(&qcs->el_send);
3184 LIST_APPEND(qcs_failed, &qcs->el_send);
3190 /* Move QCS with some bytes transferred at the
3191 * end of send-list for next iterations.
3193 LIST_DEL_INIT(&qcs->el_send);
3194 LIST_APPEND(&qcc->send_list, &qcs->el_send);
3195 /* Remember first moved QCS as checkpoint to interrupt loop */
3202 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
3206 /* Schedule <qcc> after emission was interrupted on pacing. */
3207 static void qcc_wakeup_pacing(struct qcc *qcc)
3209 /* Sleep to be able to reemit at least a single packet */
3210 const int inter = qcc->tx.pacer.cc->algo->pacing_inter(qcc->tx.pacer.cc);
3211 /* Convert nano to milliseconds rounded up, with 1ms as minimal value. */
3212 const int expire = MAX((inter + 999999) / 1000000, 1);
3213 qcc->pacing_task->expire = tick_add_ifset(now_ms, MS_TO_TICKS(expire));
3214 ++qcc->tx.paced_sent_ctr;
3217 /* Proceed to sending. Loop through all available streams for the <qcc>
3218 * instance and try to send as much as possible.
3220 * Returns the total of bytes sent to the transport layer.
3222 static int qcc_io_send(struct qcc *qcc)
3224 struct list *frms = &qcc->tx.frms;
3225 /* Temporary list for QCS on error. */
3226 struct list qcs_failed = LIST_HEAD_INIT(qcs_failed);
3227 struct qcs *qcs, *qcs_tmp;
3228 uint64_t window_conn = qfctl_rcap(&qcc->tx.fc);
3229 int ret = 0, total = 0, resent;
3231 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
3233 if (qcc_is_pacing_active(qcc->conn)) {
3234 /* Always reset pacing_task timer to prevent unnecessary execution. */
3235 qcc->pacing_task->expire = TICK_ETERNITY;
3238 /* TODO if socket in transient error, sending should be temporarily
3239 * disabled for all frames. However, checking for send subscription is
3240 * not valid as this may be caused by a congestion error which only
3241 * apply for STREAM frames.
3244 /* Check for transport error. */
3245 if (qcc->flags & QC_CF_ERR_CONN || qcc->conn->flags & CO_FL_ERROR) {
3246 TRACE_DEVEL("connection on error", QMUX_EV_QCC_SEND, qcc->conn);
3250 /* Check for locally detected connection error. */
3251 if (qcc->flags & QC_CF_ERRL) {
3252 /* Prepare a CONNECTION_CLOSE if not already done. */
3253 if (!(qcc->flags & QC_CF_ERRL_DONE)) {
3254 TRACE_DATA("report a connection error", QMUX_EV_QCC_SEND|QMUX_EV_QCC_ERR, qcc->conn);
3255 /* TODO implement a QMux alternative */
3256 if (conn_is_quic(qcc->conn))
3257 quic_set_connection_close(qcc->conn->handle.qc, qcc->err);
3258 qcc->flags |= QC_CF_ERRL_DONE;
3263 if (qcc->conn->flags & CO_FL_SOCK_WR_SH) {
3264 qcc->conn->flags |= CO_FL_ERROR;
3265 TRACE_DEVEL("connection on error", QMUX_EV_QCC_SEND, qcc->conn);
3269 if (qcc->app_st < QCC_APP_ST_INIT) {
3270 if (qcc_app_init(qcc))
3274 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
3275 if (qcc_emit_fctl(qcc)) {
3276 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
3281 if (qcc_emit_rs_ss(qcc)) {
3282 TRACE_DEVEL("emission interrupted on STOP_SENDING/RESET_STREAM send error", QMUX_EV_QCC_SEND, qcc->conn);
3286 /* Encode new STREAM frames if list has been previously cleared. */
3287 if (LIST_ISEMPTY(frms) && !LIST_ISEMPTY(&qcc->send_list)) {
3288 total = qcc_build_frms(qcc, &qcs_failed);
3289 if (LIST_ISEMPTY(frms))
3293 if (!LIST_ISEMPTY(frms) && qcc_is_pacing_active(qcc->conn)) {
3294 if (!quic_pacing_reload(&qcc->tx.pacer)) {
3295 qcc_wakeup_pacing(qcc);
3301 /* Retry sending until no frame to send, data rejected or connection
3302 * flow-control limit reached.
3304 while ((ret = qcc_send_frames(qcc, frms, 1)) == 0 && !qfctl_rblocked(&qcc->tx.fc)) {
3305 /* TODO should this check also be performed for QUIC ? */
3306 if (!conn_is_quic(qcc->conn) && (qcc->conn->flags & CO_FL_ERROR)) {
3307 TRACE_DEVEL("connection on error", QMUX_EV_QCC_SEND, qcc->conn);
3311 window_conn = qfctl_rcap(&qcc->tx.fc);
3314 /* Reloop over <qcc.send_list>. Useful for streams which have
3315 * fulfilled their qc_stream_desc buf and have now release it.
3317 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_list, el_send) {
3318 /* Only streams blocked on flow-control or waiting on a
3319 * new qc_stream_desc should be present in send_list as
3320 * long as transport layer can handle all data.
3322 BUG_ON((!conn_is_quic(qcc->conn) || qcs->tx.stream->buf) &&
3323 !qfctl_rblocked(&qcs->tx.fc));
3325 /* Total sent bytes must not exceed connection window. */
3326 BUG_ON(resent > window_conn);
3328 if (!qfctl_rblocked(&qcs->tx.fc) && window_conn > resent) {
3329 if ((ret = qcs_send(qcs, frms, window_conn - resent)) < 0) {
3330 LIST_DEL_INIT(&qcs->el_send);
3331 LIST_APPEND(&qcs_failed, &qcs->el_send);
3342 /* qcc_send_frames cannot return 1 if pacing not used. */
3343 BUG_ON(!qcc_is_pacing_active(qcc->conn));
3344 qcc_wakeup_pacing(qcc);
3348 /* Re-insert on-error QCS at the end of the send-list. */
3349 if (!LIST_ISEMPTY(&qcs_failed)) {
3350 list_for_each_entry_safe(qcs, qcs_tmp, &qcs_failed, el_send) {
3351 LIST_DEL_INIT(&qcs->el_send);
3352 LIST_APPEND(&qcc->send_list, &qcs->el_send);
3355 if (!qfctl_rblocked(&qcc->tx.fc))
3356 tasklet_wakeup(qcc->wait_event.tasklet);
3359 if (qcc->conn->flags & CO_FL_ERROR && !(qcc->flags & QC_CF_ERR_CONN)) {
3360 TRACE_ERROR("error reported by transport layer",
3361 QMUX_EV_QCC_SEND, qcc->conn);
3362 qcc->flags |= QC_CF_ERR_CONN;
3365 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
3369 /* Detects QUIC handshake completion. Any SE_FL_WAIT_FOR_HS streams are woken
3370 * up if wait-for-handshake is active.
3372 static void qcc_wait_for_hs(struct qcc *qcc)
3374 struct connection *conn = qcc->conn;
3375 struct quic_conn *qc = conn->handle.qc;
3376 struct eb64_node *node;
3379 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
3381 if (qc->state >= QUIC_HS_ST_COMPLETE) {
3382 if (conn->flags & CO_FL_EARLY_SSL_HS) {
3383 TRACE_STATE("mark early data as ready", QMUX_EV_QCC_WAKE, conn);
3384 conn->flags &= ~CO_FL_EARLY_SSL_HS;
3387 /* wake-up any stream blocked on early data transfer */
3388 node = eb64_first(&qcc->streams_by_id);
3390 qcs = container_of(node, struct qcs, by_id);
3391 if (qcs_sc(qcs) && se_fl_test(qcs->sd, SE_FL_WAIT_FOR_HS))
3392 qcs_notify_recv(qcs);
3393 node = eb64_next(node);
3396 qcc->flags &= ~QC_CF_WAIT_HS;
3399 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
3402 /* Proceed on receiving. Loop on streams subscribed in recv_list and performed
3403 * STREAM frames decoding upon them.
3405 * Returns the number of newly transcoded bytes.
3407 static int qcc_io_recv(struct qcc *qcc)
3412 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
3414 if (qcc->flags & QC_CF_ERRL) {
3415 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
3416 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
3420 if ((qcc->flags & QC_CF_WAIT_HS) && !(qcc->wait_event.events & SUB_RETRY_RECV))
3421 qcc_wait_for_hs(qcc);
3423 if (!conn_is_quic(qcc->conn)) {
3424 if (!(qcc->wait_event.events & SUB_RETRY_RECV))
3429 while (!LIST_ISEMPTY(&qcc->recv_list)) {
3430 qcs = LIST_ELEM(qcc->recv_list.n, struct qcs *, el_recv);
3431 /* No need to add an uni local stream in recv_list. */
3432 BUG_ON(quic_stream_is_uni(qcs->id) && quic_stream_is_local(qcc, qcs->id));
3434 while (qcs_rx_avail_data(qcs) && !(qcs->flags & QC_SF_DEM_FULL)) {
3435 ret = qcc_decode_qcs(qcc, qcs);
3437 LIST_DEL_INIT(&qcs->el_recv);
3438 /* Interrupt all receive if connection on error. */
3439 if (qcc->flags & QC_CF_ERRL)
3441 /* Decode next entry if stream on error. */
3448 /* Always remove QCS from recv_list to prevent infinite loop.
3449 * This is performed even if inner loop was not executed : QCS
3450 * has nothing to do in recv_list if no avail Rx data or demux
3451 * is blocked. Next decoding will be performed on new data read
3452 * unless demux is blocked. In this case QCS will be reinserted
3453 * in recv_list on unblocking to execute decode here again.
3455 LIST_DEL_INIT(&qcs->el_recv);
3459 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
3463 /* Calculate the number of bidirectional streams which can still be opened for
3464 * <conn> connection. This depends on flow-control set by the peer.
3466 * Returns the value which is a positive integer or 0 if no new stream
3467 * currently available.
3469 static int qcm_avail_streams(struct connection *conn)
3471 const struct server *srv = __objt_server(conn->target);
3472 struct qcc *qcc = conn->ctx;
3475 if (!qcc_be_is_reusable(qcc))
3478 ret = qcc_fctl_avail_streams(qcc, 1);
3480 /* Enforce stream_max_concurrent limit even if peer allows more streams. */
3481 if (ret > quic_tune.be.stream_max_concurrent - qcc->nb_hreq)
3482 ret = quic_tune.be.stream_max_concurrent - qcc->nb_hreq;
3484 /* Now cap return value if reaching max-reuse server or maximum stream
3485 * ID. qcc_be_is_reusable() already detected if one of these has been
3488 if (ret > 1 && srv->max_reuse >= 0 && qcc->tot_sc == srv->max_reuse)
3490 else if (ret > 1 && unlikely(qcc->next_bidi_l == QCS_ID_MAX_STRM_CL_BIDI))
3496 /* Returns the number of streams currently attached into <conn> connection.
3497 * Used to determine if a connection can be considered as idle or not.
3499 static int qcm_used_streams(struct connection *conn)
3501 struct qcc *qcc = conn->ctx;
3505 /* Release all streams which have their transfer operation achieved. */
3506 static void qcc_purge_streams(struct qcc *qcc)
3510 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
3512 while (!LIST_ISEMPTY(&qcc->purg_list)) {
3513 qcs = LIST_ELEM(qcc->purg_list.n, struct qcs *, el_send);
3515 TRACE_STATE("purging stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
3516 BUG_ON_HOT(!qcs_is_completed(qcs));
3520 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
3523 /* Loop through all qcs from <qcc> and wake their associated data layer if
3524 * still active. Also report error on it if connection is already in error.
3526 static void qcc_wake_streams(struct qcc *qcc)
3529 struct eb64_node *node;
3531 TRACE_POINT(QMUX_EV_QCC_WAKE, qcc->conn);
3533 for (node = eb64_first(&qcc->streams_by_id); node;
3534 node = eb64_next(node)) {
3535 qcs = eb64_entry(node, struct qcs, by_id);
3540 if (qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL)) {
3541 TRACE_POINT(QMUX_EV_QCC_WAKE, qcc->conn, qcs);
3542 se_fl_set_error(qcs->sd);
3549 /* Conduct operations which should be made for <qcc> connection after
3550 * input/output. Most notably, closed streams are purged which may leave the
3551 * connection has ready to be released.
3553 * Returns 1 if <qcc> must be released else 0.
3555 static int qcc_io_process(struct qcc *qcc)
3557 if (!LIST_ISEMPTY(&qcc->purg_list))
3558 qcc_purge_streams(qcc);
3560 /* Check if a soft-stop is in progress.
3562 * TODO this is relevant for frontend connections only.
3564 if (unlikely(qcc->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
3567 /* If using listener socket, soft-stop is not supported. The
3568 * connection must be closed immediately.
3570 if (conn_is_quic(qcc->conn) && !qc_test_fd(qcc->conn->handle.qc)) {
3571 TRACE_DEVEL("proxy disabled with listener socket, closing connection", QMUX_EV_QCC_WAKE, qcc->conn);
3572 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
3577 TRACE_DEVEL("proxy disabled, prepare connection soft-stop", QMUX_EV_QCC_WAKE, qcc->conn);
3579 /* If a close-spread-time option is set, we want to avoid
3580 * closing all the active HTTP3 connections at once so we add a
3581 * random factor that will spread the closing.
3583 if (tick_isset(global.close_spread_end)) {
3584 int remaining_window = tick_remain(now_ms, global.close_spread_end);
3585 if (remaining_window) {
3586 /* This should increase the closing rate the
3587 * further along the window we are. */
3588 close = (remaining_window <= statistical_prng_range(global.close_spread_time));
3591 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE) {
3592 close = 0; /* let the client close his connection himself */
3596 qcc_app_shutdown(qcc);
3599 /* Report error if set on stream endpoint layer. */
3600 if (qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL))
3601 qcc_wake_streams(qcc);
3604 if (qcc_is_dead(qcc))
3610 /* Conduct I/O operations to finalize <qcc> app layer initialization. Note that
3611 * <qcc> app state may remain NULL even on success, if only a transient
3612 * blocking was encountered. Finalize operation can be retried later.
3614 * Returns 0 on success else non-zero.
3616 static int qcc_app_init(struct qcc *qcc)
3620 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
3622 if (qcc->app_ops->finalize) {
3623 ret = qcc->app_ops->finalize(qcc->ctx);
3625 TRACE_ERROR("app ops finalize error", QMUX_EV_QCC_NEW, qcc->conn);
3630 TRACE_STATE("cannot finalize app ops yet", QMUX_EV_QCC_NEW, qcc->conn);
3635 qcc->app_st = QCC_APP_ST_INIT;
3638 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
3642 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
3646 /* Execute application layer shutdown. If this operation is not defined, a
3647 * CONNECTION_CLOSE will be prepared as a fallback. This function is protected
3648 * against multiple invocations thanks to <qcc> application state context.
3650 static void qcc_app_shutdown(struct qcc *qcc)
3652 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
3654 if (qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL)) {
3655 TRACE_DATA("connection on error", QMUX_EV_QCC_END, qcc->conn);
3659 if (qcc->app_st >= QCC_APP_ST_SHUT)
3662 if (qcc->app_st < QCC_APP_ST_INIT) {
3663 if (qcc_app_init(qcc))
3667 TRACE_STATE("perform graceful shutdown", QMUX_EV_QCC_END, qcc->conn);
3668 qcc_report_term_evt(qcc, muxc_tevt_type_graceful_shut);
3669 if (qcc->app_ops && qcc->app_ops->shutdown) {
3670 qcc->app_ops->shutdown(qcc->ctx);
3674 qcc->err = quic_err_transport(QC_ERR_NO_ERROR);
3677 if (conn_is_quic(qcc->conn)) {
3678 /* Register "no error" code at transport layer. Do not use
3679 * quic_set_connection_close() as retransmission may be performed to
3680 * finalized transfers. Do not overwrite quic-conn existing code if
3683 * TODO implement a wrapper function for this in quic-conn module
3685 if (!(qcc->conn->handle.qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
3686 qcc->conn->handle.qc->err = qcc->err;
3689 /* A connection is not reusable if app layer is closed. */
3690 if (qcc->flags & QC_CF_IS_BACK) {
3691 if (qcc->conn->flags & CO_FL_LIST_MASK)
3692 conn_delete_from_tree(qcc->conn, tid);
3693 else if (qcc->conn->flags & CO_FL_SESS_IDLE)
3694 session_unown_conn(qcc->conn->owner, qcc->conn);
3698 qcc->app_st = QCC_APP_ST_SHUT;
3699 TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn);
3702 /* Free all resources allocated for <qcc> connection. */
3703 static void qcc_release(struct qcc *qcc)
3705 struct connection *conn = qcc->conn;
3706 struct eb64_node *node;
3707 struct quic_conn *qc;
3709 TRACE_ENTER(QMUX_EV_QCC_END, conn);
3711 task_destroy(qcc->pacing_task);
3714 task_destroy(qcc->task);
3718 /* free remaining qcs instances */
3719 node = eb64_first(&qcc->streams_by_id);
3721 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
3722 node = eb64_next(node);
3726 if (conn && conn_is_quic(conn)) {
3727 qc = conn->handle.qc;
3729 /* unsubscribe from all remaining qc_stream_desc */
3730 node = eb64_first(&qc->streams_by_id);
3732 struct qc_stream_desc *stream = eb64_entry(node, struct qc_stream_desc, by_id);
3733 qc_stream_desc_sub_room(stream, NULL);
3734 node = eb64_next(node);
3737 /* register streams IDs so that quic-conn layer can ignore already closed streams. */
3738 if (!conn_is_back(conn)) {
3739 qc->rx.stream_max_uni = qcc->largest_uni_r;
3740 qc->rx.stream_max_bidi = qcc->largest_bidi_r;
3744 tasklet_free(qcc->wait_event.tasklet);
3746 qcc_report_term_evt(qcc, muxc_tevt_type_shutw);
3747 if (qcc->wait_event.events) {
3748 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
3749 qcc->wait_event.events,
3754 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
3755 struct quic_frame *frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list);
3756 qc_frm_free(conn_is_quic(qcc->conn) ? qcc->conn->handle.qc : NULL, &frm);
3759 qcc_clear_frms(qcc);
3762 if (qcc->app_ops->release)
3763 qcc->app_ops->release(qcc->ctx);
3764 if (conn && !conn_is_back(conn) && conn_is_quic(conn) && conn->handle.qc)
3765 conn->handle.qc->strm_reject = qcc->app_ops->strm_reject;
3767 TRACE_PROTO("application layer released", QMUX_EV_QCC_END, conn);
3769 if (!(qcc->flags & QC_CF_IS_BACK) && global.tune.streams_elasticity &&
3770 qcc->lfctl.ms_bidi_rel > 1) {
3771 _HA_ATOMIC_SUB(&tg_ctx->committed_extra_streams,
3772 qcc->lfctl.ms_bidi_rel - 1);
3775 if (conn && !conn_is_quic(conn)) {
3776 b_free(&qcc->rx.qmux_buf);
3777 b_free(&qcc->tx.qmux_buf);
3778 offer_buffers(NULL, 2);
3781 TRACE_USER("release mux", QMUX_EV_QCC_END, conn);
3782 pool_free(pool_head_qcc, qcc);
3788 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
3790 conn_stop_tracking(conn);
3791 conn_full_close(conn);
3792 if (conn->destroy_cb)
3793 conn->destroy_cb(conn);
3797 TRACE_LEAVE(QMUX_EV_QCC_END);
3800 struct task *qcc_io_cb(struct task *t, void *ctx, unsigned int state)
3802 struct qcc *qcc = ctx;
3803 struct connection *conn;
3804 int total = 0, conn_in_list;
3806 if (state & TASK_F_USR1) {
3807 /* the tasklet was idling on an idle connection, it might have
3808 * been stolen, let's be careful!
3810 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3811 if (t->context == NULL) {
3812 /* The connection has been taken over by another thread,
3813 * we're no longer responsible for it, so just free the
3814 * tasklet, and do nothing.
3816 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3817 tasklet_free((struct tasklet *)t);
3819 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
3823 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
3825 /* Remove the connection from the list, to be sure nobody attempts
3826 * to use it while we handle the I/O events
3828 if (LIST_INLIST(&conn->idle_list)) {
3830 BUG_ON(!(conn->flags & CO_FL_LIST_MASK));
3831 conn_delete_from_tree(conn, tid);
3833 else if (LIST_INLIST(&conn->sess_el)) {
3835 BUG_ON(!(conn->flags & CO_FL_SESS_IDLE));
3836 /* session_detach_idle_conn cannot fail thanks to LIST_INLIST() above test */
3837 session_detach_idle_conn(conn->owner, conn);
3843 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3845 /* we're certain the connection was not in an idle list */
3847 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
3851 if (!(qcc->wait_event.events & SUB_RETRY_SEND))
3852 total += qcc_io_send(qcc);
3854 total += qcc_io_recv(qcc);
3856 if (qcc_io_process(qcc)) {
3857 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, conn);
3862 qcc_refresh_timeout(qcc);
3864 /* Trigger pacing task is emission should be retried after some delay. */
3865 if (qcc_is_pacing_active(conn)) {
3866 if (tick_isset(qcc->pacing_task->expire))
3867 task_queue(qcc->pacing_task);
3871 struct server *srv = __objt_server(conn->target);
3873 if (srv->cur_admin & SRV_ADMF_MAINT) {
3874 /* Do not store an idle conn if server in maintenance. */
3878 if (conn->flags & CO_FL_SESS_IDLE) {
3879 if (!session_reinsert_idle_conn(conn->owner, conn)) {
3880 /* session add conn failure */
3885 srv_add_idle(srv, conn, (conn->flags & CO_FL_LIST_MASK) == CO_FL_SAFE_LIST);
3888 /* Do not access conn without protection as soon as it is reinserted in idle list. */
3892 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
3897 qcc_app_shutdown(qcc);
3900 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
3905 static struct task *qcc_pacing_task(struct task *t, void *ctx, unsigned int state)
3907 struct qcc *qcc = ctx;
3908 int expired = tick_is_expired(t->expire, now_ms);
3910 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
3913 if (!tick_isset(t->expire))
3914 TRACE_DEVEL("cancelled pacing task", QMUX_EV_QCC_WAKE, qcc->conn);
3918 /* Reschedule I/O immediately. */
3919 tasklet_wakeup_after(NULL, qcc->wait_event.tasklet);
3922 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
3926 static struct task *qcc_timeout_task(struct task *t, void *ctx, unsigned int state)
3928 struct qcc *qcc = ctx;
3929 int expired = tick_is_expired(t->expire, now_ms);
3931 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
3934 /* Make sure nobody stole the connection from us */
3935 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3937 /* Somebody already stole the connection from us, so we should
3938 * not free it, we just have to free the task.
3942 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3947 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3948 TRACE_DEVEL("not expired", QMUX_EV_QCC_WAKE, qcc->conn);
3952 if (!qcc_may_expire(qcc)) {
3953 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3954 TRACE_DEVEL("cannot expired", QMUX_EV_QCC_WAKE, qcc->conn);
3955 t->expire = TICK_ETERNITY;
3959 /* We're about to destroy the connection, so make sure nobody
3960 * attempts to steal it from us.
3962 if (qcc->conn->flags & CO_FL_LIST_MASK)
3963 conn_delete_from_tree(qcc->conn, tid);
3964 else if (qcc->conn->flags & CO_FL_SESS_IDLE)
3965 session_unown_conn(qcc->conn->owner, qcc->conn);
3967 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3973 TRACE_DEVEL("no more qcc", QMUX_EV_QCC_WAKE);
3977 /* Mark timeout as triggered by setting task to NULL. */
3980 /* TODO depending on the timeout condition, different shutdown mode
3981 * should be used. For http keep-alive or disabled proxy, a graceful
3982 * shutdown should occurs. For all other cases, an immediate close
3985 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
3986 qcc_app_shutdown(qcc);
3990 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
3994 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
3998 /* Minimal initialization of <qcc> members to use qcc_release() safely. */
3999 static void _qcc_init(struct qcc *qcc)
4002 qcc->pacing_task = NULL;
4004 qcc->wait_event.tasklet = NULL;
4005 qcc->app_ops = NULL;
4006 qcc->streams_by_id = EB_ROOT_UNIQUE;
4007 LIST_INIT(&qcc->lfctl.frms);
4008 LIST_INIT(&qcc->tx.frms);
4011 static int qcm_init(struct connection *conn, struct proxy *prx,
4012 struct session *sess, struct buffer *input)
4015 const struct quic_transport_params *lparams, *rparams;
4016 void *conn_ctx = conn->ctx;
4018 TRACE_ENTER(QMUX_EV_QCC_NEW);
4020 qcc = pool_alloc(pool_head_qcc);
4022 TRACE_ERROR("alloc failure", QMUX_EV_QCC_NEW);
4028 qcc->nb_hreq = qcc->nb_sc = qcc->tot_sc = 0;
4029 qcc->flags = conn_is_back(conn) ? QC_CF_IS_BACK : 0;
4030 qcc->app_st = QCC_APP_ST_NULL;
4032 qcc->term_evts_log = 0;
4033 qcc->err = quic_err_transport(QC_ERR_NO_ERROR);
4035 if (conn_is_quic(conn)) {
4036 /* Server parameters, params used for RX flow control. */
4037 lparams = &conn->handle.qc->rx.params;
4039 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = qcc->lfctl.ms_bidi_rel = lparams->initial_max_streams_bidi;
4040 qcc->lfctl.ms_uni = lparams->initial_max_streams_uni;
4041 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
4042 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
4043 qcc->lfctl.msd_uni_r = lparams->initial_max_stream_data_uni;
4044 qcc->lfctl.cl_bidi_r = 0;
4046 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
4047 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
4049 rparams = &conn->handle.qc->tx.params;
4050 qfctl_init(&qcc->tx.fc, rparams->initial_max_data);
4051 qcc->rfctl.ms_uni = rparams->initial_max_streams_uni;
4052 qcc->rfctl.ms_bidi = rparams->initial_max_streams_bidi;
4053 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
4054 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
4055 qcc->rfctl.msd_uni_l = rparams->initial_max_stream_data_uni;
4058 rparams = xprt_qmux_rparams(conn->xprt_ctx);
4059 qfctl_init(&qcc->tx.fc, rparams->initial_max_data);
4061 qcc->rfctl.ms_uni = rparams->initial_max_streams_uni;
4062 qcc->rfctl.ms_bidi = rparams->initial_max_streams_bidi;
4063 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
4064 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
4065 qcc->rfctl.msd_uni_l = rparams->initial_max_stream_data_uni;
4067 lparams = xprt_qmux_lparams(conn->xprt_ctx);
4068 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = qcc->lfctl.ms_bidi_rel= lparams->initial_max_streams_bidi;
4069 qcc->lfctl.ms_uni = lparams->initial_max_streams_uni;
4070 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
4071 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
4072 qcc->lfctl.msd_uni_r = lparams->initial_max_stream_data_uni;
4073 qcc->lfctl.cl_bidi_r = 0;
4075 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
4076 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
4079 qcc->tx.buf_in_flight = 0;
4081 if (qcc_is_pacing_active(conn)) {
4082 quic_pacing_init(&qcc->tx.pacer, &conn->handle.qc->path->cc);
4083 qcc->tx.paced_sent_ctr = 0;
4085 /* Initialize pacing_task. */
4086 qcc->pacing_task = task_new_here();
4087 if (!qcc->pacing_task) {
4088 TRACE_ERROR("pacing task alloc failure", QMUX_EV_QCC_NEW);
4091 qcc->pacing_task->process = qcc_pacing_task;
4092 qcc->pacing_task->context = qcc;
4093 qcc->pacing_task->expire = TICK_ETERNITY;
4094 qcc->pacing_task->state |= TASK_F_WANTS_TIME;
4097 if (!conn_is_quic(conn)) {
4098 qcc->tx.qmux_buf = BUF_NULL;
4099 qcc->rx.qmux_buf = BUF_NULL;
4101 /* Rx buffer is transferred from xprt layer - necessary if too many data were read */
4102 qcc->rx.rlen = xprt_qmux_xfer_rxbuf(conn->xprt_ctx, &qcc->rx.qmux_buf);
4103 /* Cannot have a non empty record with an empty buffer. */
4104 BUG_ON(qcc->rx.rlen && !b_data(&qcc->rx.qmux_buf));
4107 if (conn_is_back(conn)) {
4108 qcc->next_bidi_l = 0x00;
4109 qcc->largest_bidi_r = 0x01;
4110 qcc->next_uni_l = 0x02;
4111 qcc->largest_uni_r = 0x03;
4114 qcc->largest_bidi_r = 0x00;
4115 qcc->next_bidi_l = 0x01;
4116 qcc->largest_uni_r = 0x02;
4117 qcc->next_uni_l = 0x03;
4119 if (global.tune.streams_elasticity && qcc->lfctl.ms_bidi_init > 1) {
4120 _HA_ATOMIC_ADD(&tg_ctx->committed_extra_streams,
4121 qcc->lfctl.ms_bidi_init - 1);
4125 qcc->wait_event.tasklet = tasklet_new();
4126 if (!qcc->wait_event.tasklet) {
4127 TRACE_ERROR("tasklet alloc failure", QMUX_EV_QCC_NEW);
4131 LIST_INIT(&qcc->recv_list);
4132 LIST_INIT(&qcc->send_list);
4133 LIST_INIT(&qcc->fctl_list);
4134 LIST_INIT(&qcc->buf_wait_list);
4135 LIST_INIT(&qcc->purg_list);
4137 qcc->wait_event.tasklet->process = qcc_io_cb;
4138 qcc->wait_event.tasklet->context = qcc;
4139 qcc->wait_event.tasklet->state |= TASK_F_WANTS_TIME;
4140 qcc->wait_event.events = 0;
4143 /* haproxy timeouts */
4144 if (conn_is_back(conn)) {
4145 qcc->timeout = prx->timeout.server;
4146 qcc->shut_timeout = tick_isset(prx->timeout.serverfin) ?
4147 prx->timeout.serverfin : prx->timeout.server;
4150 qcc->timeout = prx->timeout.client;
4151 qcc->shut_timeout = tick_isset(prx->timeout.clientfin) ?
4152 prx->timeout.clientfin : prx->timeout.client;
4155 /* Always allocate task even if timeout is unset. In MUX code, if task
4156 * is NULL, it indicates that a timeout has stroke earlier.
4158 qcc->task = task_new_here();
4160 TRACE_ERROR("timeout task alloc failure", QMUX_EV_QCC_NEW);
4163 qcc->task->process = qcc_timeout_task;
4164 qcc->task->context = qcc;
4165 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
4167 qcc_reset_idle_start(qcc);
4168 LIST_INIT(&qcc->opening_list);
4170 if (conn_is_quic(conn)) {
4171 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
4174 /* Wakeup MUX immediately if data copied from XPRT layer. */
4175 if (unlikely(b_data(&qcc->rx.qmux_buf)))
4176 tasklet_wakeup(qcc->wait_event.tasklet);
4179 /* Register conn as app_ops may use it. */
4182 if (qcc_install_app_ops(qcc)) {
4183 TRACE_PROTO("Cannot install app layer", QMUX_EV_QCC_NEW|QMUX_EV_QCC_ERR, conn);
4187 if (qcc->app_ops == &h3_ops && !conn_is_back(conn))
4188 proxy_inc_fe_cum_sess_ver_ctr(sess->listener, prx, 3);
4190 if (!conn_is_back(conn)) {
4191 /* Register conn for idle front closing. */
4192 LIST_APPEND(&mux_stopping_data[tid].list, &conn->stopping_list);
4194 if (tick_isset(qcc->task->expire)) {
4195 /* Activate client timeout until the first bytes of data are received. */
4196 TRACE_DEVEL("activate default timeout", QMUX_EV_QCC_NEW, conn);
4197 task_queue(qcc->task);
4200 /* init read cycle */
4201 tasklet_wakeup(qcc->wait_event.tasklet);
4203 if (conn_is_quic(conn)) {
4204 /* MUX is initialized before QUIC handshake completion if early data
4205 * received. Flag connection to delay stream processing if
4206 * wait-for-handshake is active.
4208 if (conn->handle.qc->state < QUIC_HS_ST_COMPLETE) {
4209 if (!(conn->flags & CO_FL_EARLY_SSL_HS)) {
4210 TRACE_STATE("flag connection with early data", QMUX_EV_QCC_WAKE, conn);
4211 conn->flags |= CO_FL_EARLY_SSL_HS;
4212 /* subscribe for handshake completion */
4213 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV,
4215 qcc->flags |= QC_CF_WAIT_HS;
4221 /* Initiate backend side transfer by creating the first
4222 * bidirectional stream. MUX will then be woken up on QUIC
4223 * handshake completion so that stream layer can start the
4227 struct stconn *sc = conn_ctx;
4229 /* TODO how to properly handle initial value of max-bidi-stream set to 0 ? */
4230 if (!qcc_fctl_avail_streams(qcc, 1)) {
4231 TRACE_ERROR("peer does not allow any bidi stream, not yet supported",
4232 QMUX_EV_QCC_NEW|QMUX_EV_QCC_ERR, qcc->conn);
4236 qcs = qcc_init_stream_local(qcc, 1);
4238 TRACE_PROTO("Cannot allocate a new locally initiated stream",
4239 QMUX_EV_QCC_NEW|QMUX_EV_QCC_ERR, conn);
4243 if (sc_attach_mux(sc, qcs, conn)) {
4244 TRACE_ERROR("Cannot attach backend first init stream",
4245 QMUX_EV_QCC_NEW|QMUX_EV_QCC_ERR, conn);
4251 qcs->sd = sc->sedesc;
4256 TRACE_USER("init mux", QMUX_EV_QCC_NEW, conn);
4257 TRACE_LEAVE(QMUX_EV_QCC_NEW, conn);
4261 if (conn_is_quic(conn)) {
4262 /* Prepare CONNECTION_CLOSE, using INTERNAL_ERROR as fallback code if unset. */
4263 if (!(conn->handle.qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)) {
4264 struct quic_err err = qcc && qcc->err.code ?
4265 qcc->err : quic_err_transport(QC_ERR_INTERNAL_ERROR);
4266 quic_set_connection_close(conn->handle.qc, err);
4271 /* In case of MUX init failure, session will ensure connection is freed. */
4277 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_NEW, qcc ? conn : NULL);
4281 static void qcm_destroy(void *ctx)
4283 struct qcc *qcc = ctx;
4285 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
4287 TRACE_LEAVE(QMUX_EV_QCC_END);
4290 static int qcm_strm_attach(struct connection *conn, struct sedesc *sd, struct session *sess)
4293 struct qcc *qcc = conn->ctx;
4295 TRACE_ENTER(QMUX_EV_QCS_NEW, conn);
4297 /* Flow control limit on bidi streams should already have
4298 * been checked by a prior qcm_avail_streams() invocation.
4300 BUG_ON(!qcc_fctl_avail_streams(qcc, 1));
4302 /* Connection should not be reused if already on error/closed. */
4303 BUG_ON(qcc->flags & QC_CF_ERRL || qcc->app_st >= QCC_APP_ST_SHUT);
4305 qcs = qcc_init_stream_local(qcc, 1);
4307 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_NEW, qcc->conn);
4311 if (sc_attach_mux(sd->sc, qcs, conn)) {
4312 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_NEW, qcc->conn);
4318 qcs->sd = sd->sc->sedesc;
4322 /* the connection is not idle anymore, let's mark this */
4323 HA_ATOMIC_AND(&qcc->wait_event.tasklet->state, ~TASK_F_USR1);
4324 xprt_set_used(qcc->conn, qcc->conn->xprt, qcc->conn->xprt_ctx);
4326 TRACE_LEAVE(QMUX_EV_QCS_NEW, conn);
4330 static void qcm_strm_detach(struct sedesc *sd)
4332 struct qcs *qcs = sd->se;
4333 struct qcc *qcc = qcs->qcc;
4334 struct connection *conn = qcc->conn;
4335 struct session *sess = qcs->sess;
4337 TRACE_ENTER(QMUX_EV_STRM_END, conn, qcs);
4339 /* TODO this BUG_ON_HOT() is not correct as the stconn layer may detach
4340 * from the stream even if it is not closed remotely at the QUIC layer.
4341 * This happens for example when a stream must be closed due to a
4342 * rejected request. To better handle these cases, it will be required
4343 * to implement shutr/shutw MUX operations. Once this is done, this
4344 * BUG_ON_HOT() statement can be adjusted.
4346 //BUG_ON_HOT(!qcs_is_close_remote(qcs));
4350 /* Prevent QCS free if there is remaining data to send. */
4351 if ((!qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_STOP_SENDING)) &&
4352 !(qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL))) {
4353 TRACE_STATE("remaining data, detaching qcs", QMUX_EV_STRM_END, conn, qcs);
4354 qcs->flags |= QC_SF_DETACH;
4355 qcc_refresh_timeout(qcc);
4357 /* TODO should detached QCS be ignored to consider conn as idle ? */
4358 COUNT_IF_HOT(conn_is_back(conn),
4359 "QCS in detached state on BE side, may lower reuse rate");
4361 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn, qcs);
4367 /* Backend connection can be reused unless it is already on error/closed. */
4368 if ((qcc->flags & QC_CF_IS_BACK) && !qcc_is_dead(qcc) &&
4369 qcc->app_st == QCC_APP_ST_INIT) {
4370 if (conn->flags & CO_FL_PRIVATE) {
4371 TRACE_DEVEL("handle private connection reuse", QMUX_EV_STRM_END, conn);
4373 /* Ensure conn is attached into session. Most of the times
4374 * this is already done during connect so this is a no-op.
4376 session_add_conn(sess, conn);
4379 if (!LIST_INLIST(&conn->sess_el)) {
4380 /* Session insertion above has failed and connection is idle, remove it. */
4384 /* mark that the tasklet may lose its context to another thread and
4385 * that the handler needs to check it under the idle conns lock.
4387 HA_ATOMIC_OR(&qcc->wait_event.tasklet->state, TASK_F_USR1);
4388 xprt_set_idle(qcc->conn, qcc->conn->xprt, qcc->conn->xprt_ctx);
4390 /* Ensure session can keep a new idle connection. */
4391 if (session_check_idle_conn(sess, conn)) {
4392 TRACE_DEVEL("idle conn rejected by session", QMUX_EV_STRM_END, conn);
4395 /* At this point, the connection is inserted into
4396 * session list and marked as idle, so it may already
4397 * have been purged from another thread.
4405 TRACE_DEVEL("prepare for idle connection reuse", QMUX_EV_STRM_END, conn);
4407 /* mark that the tasklet may lose its context to another thread and
4408 * that the handler needs to check it under the idle conns lock.
4410 HA_ATOMIC_OR(&qcc->wait_event.tasklet->state, TASK_F_USR1);
4411 xprt_set_idle(qcc->conn, qcc->conn->xprt, qcc->conn->xprt_ctx);
4413 if (!srv_add_to_idle_list(objt_server(conn->target), conn, 1)) {
4414 /* Idle conn insert failure, gracefully close the connection. */
4415 TRACE_DEVEL("idle connection cannot be kept on the server", QMUX_EV_STRM_END, conn);
4419 /* At this point, the connection has been added to the
4420 * server idle list, so another thread may already have
4421 * hijacked it, so we can't do anything with it.
4426 else if (!ceb_intree(&conn->hash_node.node) &&
4427 qcm_avail_streams(conn) &&
4428 objt_server(conn->target)) {
4429 TRACE_DEVEL("mark connection as available for reuse", QMUX_EV_STRM_END, conn);
4430 srv_add_to_avail_list(__objt_server(conn->target), conn);
4435 if (qcc_is_dead(qcc)) {
4436 TRACE_STATE("killing dead connection", QMUX_EV_STRM_END, conn);
4440 TRACE_DEVEL("refreshing connection's timeout", QMUX_EV_STRM_END, conn);
4441 qcc_refresh_timeout(qcc);
4445 TRACE_LEAVE(QMUX_EV_STRM_END, conn);
4449 qcc_app_shutdown(qcc);
4451 TRACE_LEAVE(QMUX_EV_STRM_END);
4455 /* Called from the upper layer, to receive data */
4456 static size_t qcm_strm_rcv_buf(struct stconn *sc, struct buffer *buf,
4457 size_t count, int flags)
4459 struct qcs *qcs = __sc_mux_strm(sc);
4460 struct qcc *qcc = qcs->qcc;
4464 TRACE_ENTER(QMUX_EV_STRM_RECV, qcc->conn, qcs);
4466 ret = qcs_http_rcv_buf(qcs, buf, count, &fin);
4468 if (b_data(&qcs->rx.app_buf)) {
4469 se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
4472 se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
4474 /* Set end-of-input when full message properly received. */
4476 TRACE_STATE("report end-of-input", QMUX_EV_STRM_RECV, qcc->conn, qcs);
4477 se_fl_set(qcs->sd, SE_FL_EOI);
4479 if (!(qcc->flags & QC_CF_IS_BACK)) {
4480 /* If request EOM is reported to the upper layer, it means the
4481 * QCS now expects data from the opposite side.
4483 se_expect_data(qcs->sd);
4487 /* Set end-of-stream on read closed. */
4488 if (qcs->flags & QC_SF_RECV_RESET ||
4489 qcc->conn->flags & CO_FL_SOCK_RD_SH) {
4490 TRACE_STATE("report end-of-stream", QMUX_EV_STRM_RECV, qcc->conn, qcs);
4491 se_fl_set(qcs->sd, SE_FL_EOS);
4493 /* Set error if EOI not reached. This may happen on
4494 * RESET_STREAM reception or connection error.
4496 if (!se_fl_test(qcs->sd, SE_FL_EOI)) {
4497 TRACE_STATE("report error on stream aborted", QMUX_EV_STRM_RECV, qcc->conn, qcs);
4498 se_fl_set(qcs->sd, SE_FL_ERROR);
4499 se_report_term_evt(qcs->sd, (qcc->flags & QC_CF_ERR_CONN ? se_tevt_type_truncated_rcv_err : se_tevt_type_truncated_eos));
4502 se_report_term_evt(qcs->sd, (qcc->flags & QC_CF_ERR_CONN ? se_tevt_type_rcv_err : se_tevt_type_eos));
4506 if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING)) {
4507 TRACE_STATE("report error", QMUX_EV_STRM_RECV, qcc->conn, qcs);
4508 se_fl_set(qcs->sd, SE_FL_ERROR);
4511 if (b_size(&qcs->rx.app_buf)) {
4512 b_free(&qcs->rx.app_buf);
4513 offer_buffers(NULL, 1);
4517 /* Restart demux if it was interrupted on full buffer. */
4518 if (ret && qcs->flags & QC_SF_DEM_FULL) {
4519 /* Ensure DEM_FULL is only set if there is available data to
4520 * ensure we never do unnecessary wakeup here.
4522 BUG_ON(!qcs_rx_avail_data(qcs));
4524 qcs->flags &= ~QC_SF_DEM_FULL;
4525 if (!(qcc->flags & QC_CF_ERRL)) {
4526 LIST_APPEND(&qcc->recv_list, &qcs->el_recv);
4527 tasklet_wakeup(qcc->wait_event.tasklet);
4531 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcc->conn, qcs);
4536 static size_t qcm_strm_snd_buf(struct stconn *sc, struct buffer *buf,
4537 size_t count, int flags)
4539 struct qcs *qcs = __sc_mux_strm(sc);
4540 const size_t old_data = qcs_prep_bytes(qcs);
4544 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4546 /* stream layer has been detached so no transfer must occur after. */
4547 BUG_ON_HOT(qcs->flags & QC_SF_DETACH);
4549 /* Report error if set on stream endpoint layer. */
4550 if (qcs->qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL)) {
4551 se_fl_set(qcs->sd, SE_FL_ERROR);
4552 TRACE_DEVEL("connection in error", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4556 /* Cannot emit data after FIN/RESET_STREAM, drain extra payload. */
4557 if (qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_RESET)) {
4559 ret = qcs_http_reset_buf(qcs, buf, count);
4563 if (LIST_INLIST(&qcs->el_buf)) {
4564 TRACE_DEVEL("leaving on no buf avail", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4568 if (qfctl_sblocked(&qcs->qcc->tx.fc)) {
4569 TRACE_DEVEL("leaving on connection flow control",
4570 QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4571 if (!LIST_INLIST(&qcs->el_fctl)) {
4572 TRACE_DEVEL("append to fctl-list",
4573 QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4574 LIST_APPEND(&qcs->qcc->fctl_list, &qcs->el_fctl);
4575 tot_time_start(&qcs->timer.fctl);
4580 if (qfctl_sblocked(&qcs->tx.fc)) {
4581 TRACE_DEVEL("leaving on flow-control reached",
4582 QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4583 tot_time_start(&qcs->timer.fctl);
4587 ret = qcs_http_snd_buf(qcs, buf, count, &fin);
4589 TRACE_STATE("reached stream fin", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4590 qcs->flags |= QC_SF_FIN_STREAM;
4594 const size_t data = qcs_prep_bytes(qcs) - old_data;
4596 qcc_send_stream(qcs, 0, data);
4598 /* Wake up MUX to emit newly transferred data. */
4599 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
4600 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
4604 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4610 static size_t qcm_strm_nego_ff(struct stconn *sc, struct buffer *input,
4611 size_t count, unsigned int flags)
4613 struct qcs *qcs = __sc_mux_strm(sc);
4616 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4618 /* stream layer has been detached so no transfer must occur after. */
4619 BUG_ON_HOT(qcs->flags & QC_SF_DETACH);
4621 if (!qcs->qcc->app_ops->nego_ff || !qcs->qcc->app_ops->done_ff) {
4622 /* Fast forwarding is not supported by the QUIC application layer */
4623 qcs->sd->iobuf.flags |= IOBUF_FL_NO_FF;
4627 if (qcs->qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL)) {
4628 /* Disable fast-forward if connection is on error. Eventually,
4629 * error will be reported to stream-conn if snd_buf is invoked.
4631 TRACE_DEVEL("connection in error", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4632 qcs->sd->iobuf.flags |= IOBUF_FL_NO_FF;
4636 if (qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_RESET)) {
4637 /* Cannot emit any new data if stream already closed. Data
4638 * draining will be performed via snd_buf.
4640 TRACE_DEVEL("stream already closed", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4641 qcs->sd->iobuf.flags |= IOBUF_FL_NO_FF;
4645 if (LIST_INLIST(&qcs->el_buf)) {
4646 TRACE_DEVEL("leaving on no buf avail", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4647 qcs->sd->iobuf.flags |= IOBUF_FL_FF_BLOCKED;
4651 if (qfctl_sblocked(&qcs->qcc->tx.fc)) {
4652 TRACE_DEVEL("leaving on connection flow control", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4653 if (!LIST_INLIST(&qcs->el_fctl)) {
4654 TRACE_DEVEL("append to fctl-list", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4655 LIST_APPEND(&qcs->qcc->fctl_list, &qcs->el_fctl);
4657 qcs->sd->iobuf.flags |= IOBUF_FL_FF_BLOCKED;
4661 if (qfctl_sblocked(&qcs->tx.fc)) {
4662 TRACE_DEVEL("leaving on flow-control reached", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4663 qcs->sd->iobuf.flags |= IOBUF_FL_FF_BLOCKED;
4667 /* Alawys disable splicing */
4668 qcs->sd->iobuf.flags |= IOBUF_FL_NO_SPLICING;
4670 ret = qcs->qcc->app_ops->nego_ff(qcs, count);
4674 /* forward remaining input data */
4675 if (b_data(input)) {
4678 if (xfer > b_data(input))
4679 xfer = b_data(input);
4680 b_add(qcs->sd->iobuf.buf, qcs->sd->iobuf.offset);
4681 qcs->sd->iobuf.data = b_xfer(qcs->sd->iobuf.buf, input, xfer);
4682 b_sub(qcs->sd->iobuf.buf, qcs->sd->iobuf.offset);
4684 /* Cannot forward more data, wait for room */
4685 if (b_data(input)) {
4690 ret -= qcs->sd->iobuf.data;
4693 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4697 static size_t qcm_strm_done_ff(struct stconn *sc)
4699 struct qcs *qcs = __sc_mux_strm(sc);
4700 struct qcc *qcc = qcs->qcc;
4701 struct sedesc *sd = qcs->sd;
4702 size_t total = 0, data = sd->iobuf.data;
4704 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4706 if (sd->iobuf.flags & IOBUF_FL_EOI) {
4707 TRACE_STATE("reached stream fin", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4708 qcs->flags |= QC_SF_FIN_STREAM;
4711 if (!(qcs->flags & QC_SF_FIN_STREAM) && !sd->iobuf.data) {
4712 TRACE_STATE("no data sent", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4714 /* There is nothing to forward and the SD was blocked after a
4715 * successful nego by the producer. We can try to release the
4716 * TXBUF to retry. In this case, the TX buf MUST exist.
4718 if ((qcs->sd->iobuf.flags & IOBUF_FL_FF_WANT_ROOM) && !qcc_release_stream_txbuf(qcs))
4719 qcs->sd->iobuf.flags &= ~(IOBUF_FL_FF_BLOCKED|IOBUF_FL_FF_WANT_ROOM);
4723 total = qcs->qcc->app_ops->done_ff(qcs);
4724 if (total || qcs->flags & QC_SF_FIN_STREAM)
4725 qcc_send_stream(qcs, 0, total);
4727 /* Reset stconn iobuf information. */
4728 qcs->sd->iobuf.buf = NULL;
4729 qcs->sd->iobuf.offset = 0;
4730 qcs->sd->iobuf.data = 0;
4732 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
4733 tasklet_wakeup(qcc->wait_event.tasklet);
4736 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4740 static int qcm_strm_resume_ff(struct stconn *sc, unsigned int flags)
4745 /* Called from the upper layer, to subscribe <es> to events <event_type>. The
4746 * event subscriber <es> is not allowed to change from a previous call as long
4747 * as at least one event is still subscribed. The <event_type> must only be a
4748 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4750 static int qcm_strm_subscribe(struct stconn *sc, int event_type,
4751 struct wait_event *es)
4753 return qcs_subscribe(__sc_mux_strm(sc), event_type, es);
4756 /* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4757 * The <es> pointer is not allowed to differ from the one passed to the
4758 * subscribe() call. It always returns zero.
4760 static int qcm_strm_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
4762 struct qcs *qcs = __sc_mux_strm(sc);
4764 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
4765 BUG_ON(qcs->subs && qcs->subs != es);
4767 es->events &= ~event_type;
4774 static int qcm_wake(struct connection *conn)
4776 struct qcc *qcc = conn->ctx;
4778 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
4780 if (qcc_io_process(qcc)) {
4781 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
4785 /* Wake all streams, unless an error is set as qcc_io_process() has
4786 * already woken them in this case.
4788 if (!(qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL)))
4789 qcc_wake_streams(qcc);
4791 qcc_refresh_timeout(qcc);
4793 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
4797 qcc_app_shutdown(qcc);
4799 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
4803 static void qcm_strm_shut(struct stconn *sc, unsigned int mode, struct se_abort_info *reason)
4805 struct qcs *qcs = __sc_mux_strm(sc);
4806 struct qcc *qcc = qcs->qcc;
4807 enum qcc_app_ops_lclose_mode lclo_mode;
4809 if (!(mode & (SE_SHW_SILENT|SE_SHW_NORMAL)))
4812 TRACE_ENTER(QMUX_EV_STRM_SHUT, qcc->conn, qcs);
4814 /* Not implemented. */
4815 BUG_ON(mode & SE_SHR_DRAIN);
4817 /* Early closure reported if QC_SF_FIN_STREAM not yet set. */
4818 if ((mode & (SE_SHW_SILENT|SE_SHW_NORMAL)) &&
4819 !qcs_is_close_local(qcs) &&
4820 !(qcs->flags & (QC_SF_FIN_STREAM|QC_SF_TO_RESET))) {
4822 if (qcs->flags & QC_SF_UNKNOWN_PL_LENGTH)
4823 lclo_mode = QCC_APP_OPS_LCLO_MODE_NORMAL;
4824 else if (se_fl_test(qcs->sd, SE_FL_KILL_CONN))
4825 lclo_mode = QCC_APP_OPS_LCLO_MODE_KILL_CONN;
4827 lclo_mode = QCC_APP_OPS_LCLO_MODE_ABORT;
4829 qcc->app_ops->lclose(qcs, lclo_mode);
4830 tasklet_wakeup(qcc->wait_event.tasklet);
4833 if ((mode & SE_SHR_RESET) &&
4834 !qcs_is_close_remote(qcs) && !(qcs->flags & (QC_SF_READ_ABORTED))) {
4835 qcc->app_ops->lclose(qcs, QCC_APP_OPS_LCLO_MODE_READ);
4839 TRACE_LEAVE(QMUX_EV_STRM_SHUT, qcc->conn, qcs);
4842 static int qcm_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
4844 struct qcc *qcc = conn->ctx;
4847 case MUX_CTL_EXIT_STATUS:
4848 return MUX_ES_UNKNOWN;
4850 case MUX_CTL_GET_GLITCHES:
4851 return qcc->glitches;
4853 case MUX_CTL_GET_NBSTRM:
4854 return qcc->nb_hreq;
4856 case MUX_CTL_GET_MAXSTRM:
4857 return qcc->lfctl.ms_bidi_rel;
4860 return qcc->term_evts_log;
4867 static int qcm_sctl(struct stconn *sc, enum mux_sctl_type mux_sctl, void *output)
4870 const struct qcs *qcs = __sc_mux_strm(sc);
4871 const struct qcc *qcc = qcs->qcc;
4872 const struct connection *conn = qcc->conn;
4873 union mux_sctl_dbg_str_ctx *dbg_ctx;
4879 *((int64_t *)output) = qcs->id;
4882 case MUX_SCTL_DBG_STR:
4884 buf = get_trash_chunk();
4886 if (dbg_ctx->arg.debug_flags & MUX_SCTL_DBG_STR_L_MUXS)
4887 qcm_dump_qcs_info(buf, qcs);
4889 if (dbg_ctx->arg.debug_flags & MUX_SCTL_DBG_STR_L_MUXC)
4890 qcm_dump_qcc_info(buf, qcc);
4892 if (dbg_ctx->arg.debug_flags & MUX_SCTL_DBG_STR_L_CONN) {
4893 chunk_appendf(buf, " conn.flg=%#08x conn.err_code=%u conn.evts=%s",
4894 conn->flags, conn->err_code,
4895 tevt_evts2str(conn->term_evts_log));
4898 if (dbg_ctx->arg.debug_flags & MUX_SCTL_DBG_STR_L_XPRT)
4899 conn->xprt->dump_info(buf, conn);
4901 dbg_ctx->ret.buf = *buf;
4904 case MUX_SCTL_TEVTS:
4905 return qcs->sd->term_evts_log;
4912 /* for debugging with CLI's "show sess" command. May emit multiple lines, each
4913 * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single
4914 * line is used. Each field starts with a space so it's safe to print it after
4917 static int qcm_strm_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx)
4919 struct qcs *qcs = sd->se;
4926 chunk_appendf(msg, " qcs=%p .flg=%#x .id=%llu .st=%s .ctx=%p, .err=%#llx/%#llx",
4927 qcs, qcs->flags, (ull)qcs->id, qcs_st_to_str(qcs->st), qcs->ctx,
4928 (ull)qcs->rs_err, (ull)qcs->ss_err);
4931 chunk_appendf(msg, "\n%s", pfx);
4934 chunk_appendf(msg, " qcc=%p .flg=%#x .nbsc=%llu .nbhreq=%llu, .task=%p",
4935 qcc, qcc->flags, (ull)qcc->nb_sc, (ull)qcc->nb_hreq, qcc->task);
4940 static const struct mux_ops quic_ops = {
4942 .destroy = qcm_destroy,
4943 .detach = qcm_strm_detach,
4944 .rcv_buf = qcm_strm_rcv_buf,
4945 .snd_buf = qcm_strm_snd_buf,
4946 .nego_fastfwd = qcm_strm_nego_ff,
4947 .done_fastfwd = qcm_strm_done_ff,
4948 .resume_fastfwd = qcm_strm_resume_ff,
4949 .subscribe = qcm_strm_subscribe,
4950 .unsubscribe = qcm_strm_unsubscribe,
4952 .avail_streams = qcm_avail_streams,
4953 .used_streams = qcm_used_streams,
4954 .takeover = NULL, /* QUIC takeover support not implemented yet */
4955 .attach = qcm_strm_attach,
4956 .shut = qcm_strm_shut,
4959 .show_sd = qcm_strm_show_sd,
4960 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
4964 void qcc_show_quic(struct qcc *qcc)
4966 const struct quic_conn *qc = qcc->conn->handle.qc;
4967 struct eb64_node *node;
4969 chunk_appendf(&trash, " qcc=0x%p flags=0x%x sc=%llu hreq=%llu bwnd=%llu/%llu\n",
4970 qcc, qcc->flags, (ullong)qcc->nb_sc, (ullong)qcc->nb_hreq,
4971 (ullong)qcc->tx.buf_in_flight, (ullong)qc->path->cwnd);
4973 if (qcc_is_pacing_active(qcc->conn)) {
4974 chunk_appendf(&trash, " pacing int_sent=%d last_sent=%d\n",
4975 qcc->tx.paced_sent_ctr,
4976 qcc->tx.pacer.last_sent);
4979 node = eb64_first(&qcc->streams_by_id);
4981 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
4982 chunk_appendf(&trash, " qcs=0x%p id=%llu flags=0x%x st=%s",
4983 qcs, (ullong)qcs->id, qcs->flags,
4984 qcs_st_to_str(qcs->st));
4986 if (!quic_stream_is_uni(qcs->id) || !quic_stream_is_local(qcc, qcs->id)) {
4987 chunk_appendf(&trash, " rxb=%u(%u)", qcs->rx.data.bcnt, qcs->rx.data.bmax);
4988 chunk_appendf(&trash, " rxoff=%llu", (ullong)qcs->rx.offset);
4991 if (!quic_stream_is_uni(qcs->id) || !quic_stream_is_remote(qcc, qcs->id)) {
4993 bdata_ctr_print(&trash, &qcs->tx.stream->data, "txb=");
4994 chunk_appendf(&trash, " txoff=%llu(%llu) msd=%llu",
4995 (ullong)qcs->tx.fc.off_real,
4996 (ullong)qcs->tx.fc.off_soft - (ullong)qcs->tx.fc.off_real,
4997 (ullong)qcs->tx.fc.limit);
4999 chunk_appendf(&trash, "\n");
5000 node = eb64_next(node);
5004 static struct mux_proto_list mux_proto_quic =
5005 { .mux_proto = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &quic_ops };
5007 INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);
5009 static const struct mux_ops qmux_ops = {
5011 .destroy = qcm_destroy,
5012 .detach = qcm_strm_detach,
5013 .rcv_buf = qcm_strm_rcv_buf,
5014 .snd_buf = qcm_strm_snd_buf,
5015 .nego_fastfwd = qcm_strm_nego_ff,
5016 .done_fastfwd = qcm_strm_done_ff,
5017 .resume_fastfwd = qcm_strm_resume_ff,
5018 .subscribe = qcm_strm_subscribe,
5019 .unsubscribe = qcm_strm_unsubscribe,
5021 .avail_streams = qcm_avail_streams,
5022 .used_streams = qcm_used_streams,
5023 .takeover = NULL, /* QUIC takeover support not implemented yet */
5024 .attach = qcm_strm_attach,
5025 .shut = qcm_strm_shut,
5028 .show_sd = qcm_strm_show_sd,
5029 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_EXPERIMENTAL,
5033 static struct mux_proto_list mux_proto_qmux =
5034 { .mux_proto = IST("qmux"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &qmux_ops,
5035 .alpn = "\002h3", .init_xprt = XPRT_QMUX };
5037 INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_qmux);