]> git.kaiwu.me - haproxy.git/blob
de443a568
[haproxy.git] /
1 #include <haproxy/mux_quic.h>
2 #include <haproxy/mux_quic_priv.h>
3
4 #include <import/eb64tree.h>
5
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>
39
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);
43
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);
46
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);
50
51 /* Returns true if pacing should be used for <conn> connection. */
52 static int qcc_is_pacing_active(const struct connection *conn)
53 {
54 return conn_is_quic(conn) && quic_tune_conn_test(QUIC_TUNE_FB_TX_PACING, conn);
55 }
56
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)
59 {
60 struct ncbuf *ncbuf;
61 struct buffer buf;
62
63 ncbuf = &rxbuf->ncb;
64 if (!ncb_is_null(ncbuf)) {
65 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
66 b_free(&buf);
67 offer_buffers(NULL, 1);
68 }
69 rxbuf->ncb = NCBUF_NULL;
70
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.
73 */
74 qcs->flags &= ~QC_SF_DEM_FULL;
75
76 eb64_delete(&rxbuf->off_node);
77 pool_free(pool_head_qc_stream_rxbuf, rxbuf);
78 bdata_ctr_bdec(&qcs->rx.data);
79 }
80
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.
84 */
85 static void qcs_free(struct qcs *qcs)
86 {
87 struct qcc *qcc = qcs->qcc;
88 struct qc_stream_rxbuf *b;
89
90 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn, qcs);
91 TRACE_STATE("releasing QUIC stream", QMUX_EV_QCS_END, qcc->conn, qcs);
92
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);
99
100 /* Release stream endpoint descriptor. */
101 BUG_ON(qcs->sd && !se_fl_test(qcs->sd, SE_FL_ORPHAN));
102 sedesc_free(qcs->sd);
103 qcs->sd = NULL;
104
105 if (qcs->flags & QC_SF_HREQ_RECV) {
106 BUG_ON(!qcc->nb_hreq);
107 --qcc->nb_hreq;
108 }
109
110 /* Release app-layer context. */
111 if (qcs->ctx && qcc->app_ops->detach)
112 qcc->app_ops->detach(qcs);
113
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);
118 }
119 else if (!conn_is_quic(qcc->conn)) {
120 b_free(&qcs->tx.qmux_buf);
121 }
122
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);
128 }
129
130 if (b_size(&qcs->rx.app_buf)) {
131 b_free(&qcs->rx.app_buf);
132 offer_buffers(NULL, 1);
133 }
134
135 /* Remove qcs from qcc tree. */
136 eb64_delete(&qcs->by_id);
137
138 pool_free(pool_head_qcs, qcs);
139
140 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
141 }
142
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)
145 {
146 struct qcs *qcs;
147
148 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
149
150 qcs = pool_alloc(pool_head_qcs);
151 if (!qcs) {
152 TRACE_ERROR("alloc failure", QMUX_EV_QCS_NEW, qcc->conn);
153 return NULL;
154 }
155
156 qcs->tx.stream = NULL;
157 qcs->qcc = qcc;
158 qcs->sess = NULL;
159 qcs->sd = NULL;
160 qcs->flags = QC_SF_NONE;
161 qcs->st = QC_SS_IDLE;
162 qcs->ctx = NULL;
163
164 /* App callback attach may register the stream for http-request wait.
165 * These fields must be initialized before.
166 */
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;
173
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);
177
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);
182 }
183 else if (quic_stream_is_local(qcc, id)) {
184 qfctl_init(&qcs->tx.fc, qcc->rfctl.msd_uni_l);
185 }
186 else {
187 qfctl_init(&qcs->tx.fc, 0);
188 }
189
190 qcs->tx.msd_frm = NULL;
191
192 qcs->rx.bufs = EB_ROOT_UNIQUE;
193 qcs->rx.app_buf = BUF_NULL;
194 qcs->rx.offset = qcs->rx.offset_max = 0;
195
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;
199 }
200 else if (quic_stream_is_remote(qcc, id)) {
201 qcs->rx.msd = qcc->lfctl.msd_uni_r;
202 }
203 qcs->rx.msd_base = 0;
204 bdata_ctr_init(&qcs->rx.data);
205
206 qcs->wait_event.tasklet = NULL;
207 qcs->wait_event.events = 0;
208 qcs->subs = NULL;
209
210 qcs->rs_err = qcs->ss_err = 0;
211
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);
217
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);
225 goto err;
226 }
227
228 qc_stream_desc_sub_send(qcs->tx.stream, qcm_ctrl_send);
229 qc_stream_desc_sub_room(qcs->tx.stream, qcm_ctrl_room);
230 }
231 else {
232 qcs->tx.qmux_buf = BUF_NULL;
233 }
234 }
235
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.
239 */
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);
243 goto out;
244 }
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);
247 goto err;
248 }
249
250 out:
251 TRACE_STATE("created new QUIC stream", QMUX_EV_QCS_NEW, qcc->conn, qcs);
252 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
253 return qcs;
254
255 err:
256 qcs_free(qcs);
257 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
258 return NULL;
259 }
260
261 static forceinline struct stconn *qcs_sc(const struct qcs *qcs)
262 {
263 return qcs->sd ? qcs->sd->sc : NULL;
264 }
265
266 /* Reset the <qcc> inactivity timeout for http-keep-alive timeout. */
267 static forceinline void qcc_reset_idle_start(struct qcc *qcc)
268 {
269 qcc->idle_start = now_ms;
270 }
271
272 /* Return true if the mux timeout should be armed. */
273 static inline int qcc_may_expire(struct qcc *qcc)
274 {
275 return !qcc->nb_sc;
276 }
277
278 /* Decrement <qcc> sc. */
279 static forceinline void qcc_rm_sc(struct qcc *qcc)
280 {
281 BUG_ON(!qcc->nb_sc); /* Ensure sc count is always valid (ie >=0). */
282 --qcc->nb_sc;
283 }
284
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.
290 *
291 * Returns a boolean value indicating if reuse is possible.
292 */
293 static int qcc_be_is_reusable(const struct qcc *qcc)
294 {
295 const struct server *srv = __objt_server(qcc->conn->target);
296
297 /* Connection on error or already on timeout. */
298 if (qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL) || !qcc->task)
299 return 0;
300
301 /* Shutdown initiated by the peer - in HTTP/3 this corresponds to a GOAWAY frame received. */
302 if (qcc->flags & QC_CF_CONN_SHUT)
303 return 0;
304
305 if (srv->max_reuse >= 0 && qcc->tot_sc > srv->max_reuse)
306 return 0;
307
308 /* Ensure we do not exceed the maximum usable stream ID. */
309 if (qcc->next_bidi_l > QCS_ID_MAX_STRM_CL_BIDI)
310 return 0;
311
312 return 1;
313 }
314
315 /* Indicates if a connection is idle and cannot be used anymore. If true the
316 * connection should be released as soon as possible.
317 */
318 static inline int qcc_is_dead(const struct qcc *qcc)
319 {
320 /* Maintain connection if there is still request streams active. */
321 if (qcc->nb_hreq)
322 return 0;
323
324 if (!conn_is_back(qcc->conn)) {
325 /* FE conn considered dead if either :
326 * - transport or local error reported
327 * - MUX timeout expired
328 * - app layer shut
329 */
330 return qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL_DONE) ||
331 !qcc->task || qcc->app_st == QCC_APP_ST_SHUT;
332 }
333 else {
334 /* BE conn considered dead if reuse is definitively blocked.
335 * Checks similar conditions as FE side and more.
336 */
337 return !qcc_be_is_reusable(qcc);
338 }
339 }
340
341 /* Refresh the timeout on <qcc> if needed depending on its state. */
342 static void qcc_refresh_timeout(struct qcc *qcc)
343 {
344 const struct proxy *px = qcc->proxy;
345
346 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
347
348 if (!qcc->task) {
349 TRACE_DEVEL("already expired", QMUX_EV_QCC_WAKE, qcc->conn);
350 goto leave;
351 }
352
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);
358 goto leave;
359 }
360
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
366 *
367 * If proxy stop-stop in progress, immediate or spread close will be
368 * processed if shutdown already one or connection is idle.
369 */
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);
373 }
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,
377 qcc->shut_timeout);
378 }
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;
383 int base_time;
384
385 /* Use start time of first stream waiting on HTTP or
386 * qcc idle if no stream not yet used.
387 */
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;
391
392 TRACE_DEVEL("waiting on http request", QMUX_EV_QCC_WAKE, qcc->conn, qcs);
393 qcc->task->expire = tick_add_ifset(base_time, timeout);
394 }
395 else {
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);
401 }
402
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);
407 }
408 }
409
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.
413 */
414 if ((qcc->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) &&
415 !(global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE)) {
416
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;
420
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.
425 */
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.
430 */
431 qcc->task->expire = tick_add(now_ms,
432 statistical_prng_range(remaining_window));
433 }
434 }
435 else {
436 /* We are past the soft close window end, wake the timeout
437 * task up immediately.
438 */
439 qcc->task->expire = tick_add(now_ms, 0);
440 task_wakeup(qcc->task, TASK_WOKEN_TIMER);
441 }
442 }
443
444 task_queue(qcc->task);
445
446 leave:
447 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
448 }
449
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.
452 */
453 void qcs_idle_open(struct qcs *qcs)
454 {
455 /* This operation must not be used if the stream is already closed. */
456 BUG_ON_HOT(qcs->st == QC_SS_CLO);
457
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;
461 }
462 }
463
464 /* Close the local channel of <qcs> instance. */
465 void qcs_close_local(struct qcs *qcs)
466 {
467 TRACE_STATE("closing stream locally", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
468
469 /* The stream must have already been opened. */
470 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
471
472 /* This operation cannot be used multiple times. */
473 BUG_ON_HOT(qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO);
474
475 if (quic_stream_is_bidi(qcs->id)) {
476 qcs->st = (qcs->st == QC_SS_HREM) ? QC_SS_CLO : QC_SS_HLOC;
477 }
478 else {
479 /* Only local uni streams are valid for this operation. */
480 BUG_ON_HOT(quic_stream_is_remote(qcs->qcc, qcs->id));
481 qcs->st = QC_SS_CLO;
482 }
483 }
484
485 /* Returns true if <qcs> can be purged. */
486 int qcs_is_completed(struct qcs *qcs)
487 {
488 /* A stream is completed if fully closed and stconn released, or simply
489 * detached and everything already sent.
490 */
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));
494 }
495
496 /* Close the remote channel of <qcs> instance. */
497 static void qcs_close_remote(struct qcs *qcs)
498 {
499 TRACE_STATE("closing stream remotely", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
500
501 /* The stream must have already been opened. */
502 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
503
504 /* This operation cannot be used multiple times. */
505 BUG_ON_HOT(qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO);
506
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.
512 */
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");
517 return;
518 }
519
520 if (quic_stream_is_bidi(qcs->id)) {
521 qcs->st = (qcs->st == QC_SS_HLOC) ? QC_SS_CLO : QC_SS_HREM;
522 }
523 else {
524 /* Only remote uni streams are valid for this operation. */
525 BUG_ON_HOT(quic_stream_is_local(qcs->qcc, qcs->id));
526 qcs->st = QC_SS_CLO;
527 }
528
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);
534 }
535
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);
540 }
541 }
542
543 int qcs_is_close_local(struct qcs *qcs)
544 {
545 return qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO;
546 }
547
548 int qcs_is_close_remote(struct qcs *qcs)
549 {
550 return qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO;
551 }
552
553 /* Allocate if needed buffer <ncbuf> for stream <qcs>.
554 *
555 * Returns the buffer instance or NULL on allocation failure.
556 */
557 static struct ncbuf *qcs_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
558 {
559 struct buffer buf = BUF_NULL;
560
561 if (ncb_is_null(ncbuf)) {
562 if (!b_alloc(&buf, DB_MUX_RX))
563 return NULL;
564
565 *ncbuf = ncb_make(buf.area, buf.size, 0);
566 ncb_init(ncbuf, 0);
567 }
568
569 return ncbuf;
570 }
571
572 /* Notify the stconn layer if initialized with TASK_WOKEN_MSG state and
573 * eventually TASK_WOKEN_IO.
574 */
575 static void qcs_alert(struct qcs *qcs)
576 {
577 unsigned int state = TASK_WOKEN_MSG;
578
579 TRACE_POINT(QMUX_EV_STRM_WAKE, qcs->qcc->conn, qcs);
580 if (!qcs_sc(qcs))
581 return;
582
583 if (qcs->subs) {
584 qcs->subs->events = 0;
585 qcs->subs = NULL;
586 state |= TASK_WOKEN_IO;
587 }
588 tasklet_wakeup(qcs_sc(qcs)->wait_event.tasklet, state);
589 }
590
591 int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
592 {
593 struct qcc *qcc = qcs->qcc;
594
595 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
596
597 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
598 BUG_ON(qcs->subs && qcs->subs != es);
599
600 es->events |= event_type;
601 qcs->subs = es;
602
603 if (event_type & SUB_RETRY_RECV)
604 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
605
606 if (event_type & SUB_RETRY_SEND)
607 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
608
609 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
610
611 return 0;
612 }
613
614 void qcs_notify_recv(struct qcs *qcs)
615 {
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)
621 qcs->subs = NULL;
622 }
623 }
624
625 void qcs_notify_send(struct qcs *qcs)
626 {
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)
632 qcs->subs = NULL;
633 }
634 }
635
636 const struct buffer *qcs_tx_buf_const(const struct qcs *qcs)
637 {
638 return conn_is_quic(qcs->qcc->conn) ?
639 qc_stream_buf_get(qcs->tx.stream) : &qcs->tx.qmux_buf;
640 }
641
642 struct buffer *qcs_tx_buf(struct qcs *qcs)
643 {
644 return conn_is_quic(qcs->qcc->conn) ?
645 qc_stream_buf_get(qcs->tx.stream) : &qcs->tx.qmux_buf;
646 }
647
648 /* Returns total number of bytes not already sent to quic-conn layer. */
649 uint64_t qcs_prep_bytes(const struct qcs *qcs)
650 {
651 const struct buffer *out = qcs_tx_buf_const(qcs);
652 uint64_t diff, base_off;
653
654 if (!out)
655 return 0;
656
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;
662 }
663 else {
664 return b_data(out);
665 }
666 }
667
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>.
670 */
671 static void qcm_ctrl_send(struct qc_stream_desc *stream, uint64_t data, uint64_t offset)
672 {
673 struct qcs *qcs = stream->ctx;
674 struct qcc *qcc = qcs->qcc;
675 uint64_t diff;
676
677 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
678
679 /* Real off MUST always be the greatest offset sent. */
680 BUG_ON(offset > qcs->tx.fc.off_real);
681
682 /* Check if the STREAM frame has already been notified. An empty FIN
683 * frame must not be considered retransmitted.
684 */
685 if (data && offset + data <= qcs->tx.fc.off_real) {
686 TRACE_DEVEL("offset already notified", QMUX_EV_QCS_SEND, qcc->conn, qcs);
687 goto out;
688 }
689
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.
692 */
693 BUG_ON(!data && !(qcs->flags & QC_SF_FIN_STREAM));
694
695 qcs_idle_open(qcs);
696
697 diff = offset + data - qcs->tx.fc.off_real;
698 if (diff) {
699 struct quic_fctl *fc_conn = &qcc->tx.fc;
700 struct quic_fctl *fc_strm = &qcs->tx.fc;
701
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);
705
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);
710 }
711
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);
716 }
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);
723 }
724
725 /* Add measurement for send rate. This is done at the MUX layer
726 * to account only for STREAM frames without retransmission.
727 */
728 increment_send_rate(diff, 0);
729 }
730
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);
735
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
738 * is detached.
739 *
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.
744 */
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);
749
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;
754 }
755
756 /* Unsubscribe from streamdesc when everything sent. */
757 qc_stream_desc_sub_send(qcs->tx.stream, NULL);
758
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);
762 }
763 }
764 }
765
766 out:
767 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
768 }
769
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)
772 {
773 const struct quic_conn *qc = qcc->conn->handle.qc;
774 return qcc->tx.buf_in_flight >= qc->path->cwnd;
775 }
776
777 static void qcm_ctrl_room(struct qc_stream_desc *stream, uint64_t room)
778 {
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);
783 }
784
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.
789 */
790 void qcc_notify_buf(struct qcc *qcc, uint64_t free_size)
791 {
792 struct qcs *qcs;
793
794 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
795
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;
799
800 if (qcc_bufwnd_full(qcc))
801 return;
802
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;
806 }
807
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.
812 */
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);
818 }
819
820 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
821 }
822
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.
828 */
829 void qcc_set_error(struct qcc *qcc, int err, int app, int tevt)
830 {
831 /* This must not be called multiple times per connection. */
832 BUG_ON(qcc->flags & QC_CF_ERRL);
833
834 TRACE_STATE("connection on error", QMUX_EV_QCC_ERR, qcc->conn);
835
836 qcc->flags |= QC_CF_ERRL;
837 qcc->err = app ? quic_err_app(err) : quic_err_transport(err);
838
839 if (tevt)
840 qcc_report_term_evt(qcc, tevt);
841
842 /* TODO
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
847 * the moment.
848 */
849 tasklet_wakeup(qcc->wait_event.tasklet);
850 }
851
852 void qcc_report_term_evt(struct qcc *qcc, enum muxc_term_event_type type)
853 {
854 enum term_event_loc loc = tevt_loc_muxc;
855
856 if (qcc->flags & QC_CF_IS_BACK)
857 loc += 8;
858 qcc->term_evts_log = tevt_report_event(qcc->term_evts_log, loc, type);
859 }
860
861 /* Increment glitch counter for <qcc> connection by <inc> steps. If configured
862 * threshold reached, close the connection with an error code.
863 */
864 int _qcc_report_glitch(struct qcc *qcc, int inc)
865 {
866 const int max = QUIC_TUNE_FB_CONN_GET(sec_glitches_threshold, qcc->conn);
867
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);
875 }
876 else {
877 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0,
878 muxc_tevt_type_internal_err);
879 }
880 return 1;
881 }
882
883 return 0;
884 }
885
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)
888 {
889 if (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;
893 }
894 else {
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;
898 }
899 }
900
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.
904 */
905 static uint64_t qcc_max_strm_bidi_remote(const struct connection *conn)
906 {
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;
911 }
912
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.
916 *
917 * Returns the allocated stream instance or NULL on error.
918 */
919 struct qcs *qcc_init_stream_local(struct qcc *qcc, int bidi)
920 {
921 struct qcs *qcs;
922 enum qcs_type type;
923 uint64_t *next;
924
925 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
926
927 if (bidi) {
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;
932 }
933 else {
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;
937 }
938
939 qcs = qcs_new(qcc, *next, type);
940 if (!qcs) {
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);
944 return NULL;
945 }
946
947 TRACE_PROTO("opening local stream", QMUX_EV_QCS_NEW, qcc->conn, qcs);
948 /* TODO emit STREAMS_BLOCKED if cannot create future streams. */
949 *next += 4;
950
951 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
952 return qcs;
953 }
954
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.
959 *
960 * Returns the allocated stream instance or NULL on error.
961 */
962 static struct qcs *qcc_init_stream_remote(struct qcc *qcc, uint64_t id)
963 {
964 struct qcs *qcs = NULL;
965 enum qcs_type type;
966 uint64_t *largest, max_id;
967
968 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
969
970 /* Function reserved to remote stream IDs. */
971 BUG_ON(quic_stream_is_local(qcc, id));
972
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;
976 }
977 else {
978 largest = &qcc->largest_uni_r;
979 type = conn_is_back(qcc->conn) ? QCS_SRV_UNI : QCS_CLT_UNI;
980 }
981
982 /* RFC 9000 4.6. Controlling Concurrency
983 *
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
986 * STREAM_LIMIT_ERROR
987 */
988 max_id = quic_stream_is_bidi(id) ? qcc->lfctl.ms_bidi * 4 :
989 qcc->lfctl.ms_uni * 4;
990 if (id >= max_id) {
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);
994 goto err;
995 }
996
997 /* Only stream ID not already opened can be used. */
998 BUG_ON(id < *largest);
999
1000 /* MAX_STREAMS emission must not allowed too big stream ID. */
1001 BUG_ON(*largest > QUIC_VARINT_8_BYTE_MAX);
1002
1003 while (id >= *largest) {
1004 const char *str = *largest < id ? "initializing intermediary remote stream" :
1005 "initializing remote stream";
1006
1007 qcs = qcs_new(qcc, *largest, type);
1008 if (!qcs) {
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);
1012 goto err;
1013 }
1014
1015 TRACE_PROTO(str, QMUX_EV_QCS_NEW, qcc->conn, qcs);
1016 *largest += 4;
1017 }
1018
1019 out:
1020 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
1021 return qcs;
1022
1023 err:
1024 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
1025 return NULL;
1026 }
1027
1028 /* Mark <qcs> as reserved for metadata transfer. As such, future txbuf
1029 * allocation won't be accounted against connection limit.
1030 */
1031 void qcs_send_metadata(struct qcs *qcs)
1032 {
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));
1038
1039 qc_stream_desc_sub_room(qcs->tx.stream, NULL);
1040 }
1041
1042 qcs->flags |= QC_SF_TXBUB_OOB;
1043 }
1044
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
1048 * already received.
1049 *
1050 * This function is only used on frontend side.
1051 *
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.
1054 *
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.
1057 */
1058 int qcs_attach_sc(struct qcs *qcs, struct buffer *buf, char fin)
1059 {
1060 struct qcc *qcc = qcs->qcc;
1061 struct session *sess = qcc->conn->owner;
1062
1063 TRACE_ENTER(QMUX_EV_STRM_RECV, qcc->conn, qcs);
1064
1065 if (qcs->st == QC_SS_CLO) {
1066 TRACE_STATE("skip attach on already closed stream", QMUX_EV_STRM_RECV, qcc->conn, qcs);
1067 goto out;
1068 }
1069
1070 /* TODO duplicated from mux_h2 */
1071 sess->t_idle = ns_to_ms(now_ns - sess->accept_ts) - sess->t_handshake;
1072
1073 /* first create the stream endpoint descriptor */
1074 qcs->sd = sedesc_new();
1075 if (!qcs->sd) {
1076 TRACE_DEVEL("leaving on error", QMUX_EV_STRM_RECV, qcc->conn, qcs);
1077 return -1;
1078 }
1079 qcs->sd->se = 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);
1083
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);
1086
1087 if (!sc_new_from_endp(qcs->sd, sess, buf)) {
1088 TRACE_DEVEL("leaving on error", QMUX_EV_STRM_RECV, qcc->conn, qcs);
1089 return -1;
1090 }
1091
1092 /* QCS must be identified as request stream prior to stconn instantiation. */
1093 BUG_ON(!(qcs->flags & QC_SF_HREQ_RECV));
1094 ++qcc->nb_sc;
1095 ++qcc->tot_sc;
1096
1097 /* TODO duplicated from mux_h2 */
1098 sess->accept_date = date;
1099 sess->accept_ts = now_ns;
1100 sess->t_handshake = 0;
1101 sess->t_idle = 0;
1102
1103 /* A stream must have been registered for HTTP wait before attaching
1104 * it to sedesc. See <qcs_wait_http_req> for more info.
1105 */
1106 BUG_ON_HOT(!LIST_INLIST(&qcs->el_opening));
1107 LIST_DEL_INIT(&qcs->el_opening);
1108
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.
1111 */
1112 if (fin) {
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);
1116 }
1117
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.
1122 *
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.
1125 */
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);
1129 }
1130
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);
1135 }
1136
1137 out:
1138 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcc->conn, qcs);
1139 return 0;
1140 }
1141
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.
1144 */
1145 static int qcc_stream_id_is_closed(struct qcc *qcc, uint64_t id)
1146 {
1147 uint64_t *largest;
1148
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));
1151
1152 if (quic_stream_is_local(qcc, id)) {
1153 largest = quic_stream_is_uni(id) ? &qcc->next_uni_l :
1154 &qcc->next_bidi_l;
1155 }
1156 else {
1157 largest = quic_stream_is_uni(id) ? &qcc->largest_uni_r :
1158 &qcc->largest_bidi_r;
1159 }
1160
1161 return id < *largest;
1162 }
1163
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
1168 * <out>.
1169 *
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.
1173 */
1174 int qcc_get_qcs(struct qcc *qcc, uint64_t id, int receive_only, int send_only,
1175 struct qcs **out)
1176 {
1177 struct eb64_node *node;
1178
1179 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1180 *out = NULL;
1181
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);
1186 goto err;
1187 }
1188
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);
1193 goto err;
1194 }
1195
1196 /* Search the stream in the connection tree. */
1197 node = eb64_lookup(&qcc->streams_by_id, id);
1198 if (node) {
1199 *out = eb64_entry(node, struct qcs, by_id);
1200 TRACE_DEVEL("using stream from connection tree", QMUX_EV_QCC_RECV, qcc->conn, *out);
1201 goto out;
1202 }
1203
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. */
1208 goto out;
1209 }
1210
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.
1214 */
1215 if (quic_stream_is_local(qcc, id)) {
1216 /* RFC 9000 19.8. STREAM Frames
1217 *
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
1221 * stream.
1222 */
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);
1226 goto err;
1227 }
1228 else {
1229 /* Remote stream not found - try to open it. */
1230 *out = qcc_init_stream_remote(qcc, id);
1231 if (!*out) {
1232 TRACE_ERROR("stream creation error", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
1233 goto err;
1234 }
1235 }
1236
1237 out:
1238 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn, *out);
1239 return 0;
1240
1241 err:
1242 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1243 return 1;
1244 }
1245
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)
1248 {
1249 if (b) {
1250 const struct ncbuf *ncb = &b->ncb;
1251 return b_make(ncb_orig(ncb), ncb->size, ncb->head, ncb_data(ncb, 0));
1252 }
1253 else {
1254 return BUF_NULL;
1255 }
1256 }
1257
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.
1261 *
1262 * Returns 0 if data transfer was performed.
1263 */
1264 static int qcs_transfer_rx_data(struct qcs *qcs, struct qc_stream_rxbuf *rxbuf)
1265 {
1266 struct qc_stream_rxbuf *rxbuf_next;
1267 struct eb64_node *next;
1268 struct buffer b, b_next;
1269 enum ncb_ret ncb_ret;
1270 size_t to_copy;
1271 int ret = 1;
1272
1273 BUG_ON(ncb_is_full(&rxbuf->ncb));
1274
1275 next = eb64_next(&rxbuf->off_node);
1276 if (!next)
1277 goto out;
1278
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);
1283 eb64_delete(next);
1284
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));
1288
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);
1292
1293 ncb_ret = ncb_advance(&rxbuf_next->ncb, to_copy);
1294 BUG_ON(ncb_ret != NCB_RET_OK);
1295
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);
1299
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);
1306 }
1307 else {
1308 b_free(&b_next);
1309 offer_buffers(NULL, 1);
1310 pool_free(pool_head_qc_stream_rxbuf, rxbuf_next);
1311 bdata_ctr_bdec(&qcs->rx.data);
1312 }
1313
1314 ret = 0;
1315 }
1316
1317 out:
1318 return ret;
1319 }
1320
1321 /* Returns the Rx buffer instance for <qcs> stream read offset. May be NULL if
1322 * not already allocated.
1323 */
1324 static struct qc_stream_rxbuf *qcs_get_curr_rxbuf(struct qcs *qcs)
1325 {
1326 struct eb64_node *node;
1327 struct qc_stream_rxbuf *buf;
1328
1329 node = eb64_first(&qcs->rx.bufs);
1330 if (!node)
1331 return NULL;
1332
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 */
1336 return NULL;
1337 }
1338
1339 /* Ensures obsolete buffer are not kept inside QCS */
1340 BUG_ON(buf->off_end < qcs->rx.offset);
1341 return buf;
1342 }
1343
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.
1347 */
1348 static ncb_sz_t qcs_rx_avail_data(struct qcs *qcs)
1349 {
1350 struct qc_stream_rxbuf *b = qcs_get_curr_rxbuf(qcs);
1351 return b ? ncb_data(&b->ncb, 0) : 0;
1352 }
1353
1354 /* Account for <bytes> in <qcc> connection flow-control. If needed, a MAX_DATA
1355 * frame is prepared for emission.
1356 */
1357 static void qcc_account_for_conn_fctl(struct qcc *qcc, uint64_t bytes)
1358 {
1359 struct quic_frame *frm;
1360
1361 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1362
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);
1367 if (!frm) {
1368 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0,
1369 muxc_tevt_type_internal_err);
1370 goto out;
1371 }
1372
1373 qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init;
1374 frm->max_data.max_data = qcc->lfctl.md;
1375
1376 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
1377 tasklet_wakeup(qcc->wait_event.tasklet);
1378 }
1379
1380 out:
1381 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1382 }
1383
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.
1386 */
1387 static void qcs_consume(struct qcs *qcs, uint64_t bytes, struct qc_stream_rxbuf *buf)
1388 {
1389 struct qcc *qcc = qcs->qcc;
1390 struct quic_frame *frm;
1391 enum ncb_ret ret;
1392 uint64_t diff, inc = 0;
1393
1394 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
1395
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);
1399
1400 ret = ncb_advance(&buf->ncb, bytes);
1401 if (ret) {
1402 ABORT_NOW(); /* should not happens because removal only in data */
1403 }
1404
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);
1408
1409 /* Not necessary to emit a MAX_STREAM_DATA if all data received. */
1410 if (qcs->flags & QC_SF_SIZE_KNOWN)
1411 goto conn_fctl;
1412
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.
1417 */
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;
1421 }
1422 else {
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();
1427 }
1428 }
1429
1430 if (inc) {
1431 frm = qcs->tx.msd_frm;
1432 if (!frm) {
1433 frm = qc_frm_alloc(QUIC_FT_MAX_STREAM_DATA);
1434 if (!frm) {
1435 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0,
1436 muxc_tevt_type_internal_err);
1437 return;
1438 }
1439
1440 frm->max_stream_data.id = qcs->id;
1441 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
1442 qcs->tx.msd_frm = frm;
1443 }
1444
1445 TRACE_DATA("increase stream credit via MAX_STREAM_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
1446 qcs->rx.msd += inc;
1447 qcs->rx.msd_base += inc;
1448
1449 frm->max_stream_data.max_stream_data = qcs->rx.msd;
1450
1451 tasklet_wakeup(qcc->wait_event.tasklet);
1452 }
1453
1454 conn_fctl:
1455 qcc_account_for_conn_fctl(qcc, bytes);
1456
1457 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
1458 }
1459
1460 /* Decode the content of STREAM frames already received on the stream instance
1461 * <qcs> from the <qcc> connection.
1462 *
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.
1466 *
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.
1471 */
1472 static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
1473 {
1474 struct qc_stream_rxbuf *rxbuf;
1475 struct buffer b;
1476 ssize_t ret;
1477 int fin = 0;
1478 int prev_glitches = qcc->glitches;
1479
1480 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
1481
1482 if (qcc->flags & QC_CF_ERRL) {
1483 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
1484 ret = -1;
1485 goto err;
1486 }
1487
1488 restart:
1489 rxbuf = qcs_get_curr_rxbuf(qcs);
1490 b = qcs_b_dup(rxbuf);
1491
1492 /* Signal FIN to application if STREAM FIN received with all data. */
1493 if (qcs_is_close_remote(qcs))
1494 fin = 1;
1495
1496 if (!(qcs->flags & QC_SF_READ_ABORTED)) {
1497 ret = qcc->app_ops->rcv_buf(qcs, &b, fin);
1498
1499 if (qcc->glitches != prev_glitches && !(qcc->flags & QC_CF_IS_BACK))
1500 session_add_glitch_ctr(qcc->conn->owner, qcc->glitches - prev_glitches);
1501
1502 if (ret < 0) {
1503 TRACE_ERROR("decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
1504 goto err;
1505 }
1506
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
1509 * restart decoding.
1510 */
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);
1515 goto restart;
1516 }
1517 }
1518
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);
1522 qcs_alert(qcs);
1523 }
1524 }
1525 }
1526 else {
1527 TRACE_DATA("ignore read on stream", QMUX_EV_QCS_RECV, qcc->conn, qcs);
1528 ret = b_data(&b);
1529 }
1530
1531 if (rxbuf) {
1532 if (ret)
1533 qcs_consume(qcs, ret, rxbuf);
1534
1535 if (ncb_is_empty(&rxbuf->ncb)) {
1536 qcs_free_rxbuf(qcs, rxbuf);
1537
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.
1542 */
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);
1547 }
1548 }
1549 }
1550
1551 if (ret || (!b_data(&b) && fin))
1552 qcs_notify_recv(qcs);
1553
1554 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
1555 return ret;
1556
1557 err:
1558 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
1559 return ret;
1560 }
1561
1562 /* Allocate if needed and retrieve <qcs> stream buffer for data reception.
1563 *
1564 * Returns buffer pointer. May be NULL on allocation failure.
1565 */
1566 struct buffer *qcc_get_stream_rxbuf(struct qcs *qcs)
1567 {
1568 return b_alloc(&qcs->rx.app_buf, DB_MUX_RX);
1569 }
1570
1571 /* Allocate if needed and retrieve <qcs> stream buffer for data emission.
1572 *
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.
1576 *
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.
1580 *
1581 * Returns buffer pointer. May be NULL on allocation failure, in which case
1582 * <err> will refer to the cause.
1583 */
1584 struct buffer *qcc_get_stream_txbuf(struct qcs *qcs, int *err, int small)
1585 {
1586 struct qcc *qcc = qcs->qcc;
1587 struct buffer *out = qcs_tx_buf(qcs);
1588
1589 /* Stream must not try to reallocate a buffer if currently waiting for one. */
1590 BUG_ON(LIST_INLIST(&qcs->el_buf));
1591
1592 *err = 0;
1593
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);
1599 goto out;
1600 }
1601
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;
1607 goto out;
1608 }
1609 }
1610
1611 out = qc_stream_buf_alloc(qcs->tx.stream, qcs->tx.fc.off_real, small);
1612 if (!out) {
1613 TRACE_ERROR("stream desc alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1614 *err = 1;
1615 goto out;
1616 }
1617
1618 if (likely(!(qcs->flags & QC_SF_TXBUB_OOB)))
1619 qcc->tx.buf_in_flight += b_size(out);
1620 }
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);
1625 *err = 1;
1626 goto out;
1627 }
1628 }
1629
1630 out:
1631 return out;
1632 }
1633
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.
1638 *
1639 * Returns buffer pointer or NULL on allocation failure.
1640 */
1641 struct buffer *qcc_realloc_stream_txbuf(struct qcs *qcs)
1642 {
1643 struct qcc *qcc = qcs->qcc;
1644 struct buffer *out = qcs_tx_buf(qcs);
1645
1646 /* Stream must not try to reallocate a buffer if currently waiting for one. */
1647 BUG_ON(LIST_INLIST(&qcs->el_buf));
1648
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.
1653 */
1654 BUG_ON(qcc->tx.buf_in_flight < b_size(out));
1655 qcc->tx.buf_in_flight -= b_size(out);
1656 }
1657
1658 out = qc_stream_buf_realloc(qcs->tx.stream);
1659 if (!out) {
1660 TRACE_ERROR("buffer alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1661 goto out;
1662 }
1663
1664 if (likely(!(qcs->flags & QC_SF_TXBUB_OOB)))
1665 qcc->tx.buf_in_flight += b_size(out);
1666 }
1667 else {
1668 b_free(out);
1669 if (!b_alloc(out, DB_MUX_TX)) {
1670 TRACE_ERROR("QCS tx buf alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1671 goto out;
1672 }
1673 }
1674
1675 out:
1676 return out && b_size(out) ? out : NULL;
1677 }
1678
1679 /* Try to realign <out> buffer for <qcs> stream. This is done only if there is
1680 * no data waiting for ACK.
1681 *
1682 * Returns 0 if realign was performed else non-zero.
1683 */
1684 int qcc_realign_stream_txbuf(const struct qcs *qcs, struct buffer *out)
1685 {
1686 if (qcs_prep_bytes(qcs) == b_data(out)) {
1687 b_slow_realign(out, trash.area, b_data(out));
1688 return 0;
1689 }
1690
1691 return 1;
1692 }
1693
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.
1696 *
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.
1699 *
1700 * Returns 0 if buffer is released and a new one can be allocated or non-zero
1701 * if there is still remaining data.
1702 */
1703 int qcc_release_stream_txbuf(struct qcs *qcs)
1704 {
1705 const uint64_t bytes = qcs_prep_bytes(qcs);
1706
1707 /* Cannot release buffer if prepared data is not fully sent. */
1708 if (bytes) {
1709 qcs->flags |= QC_SF_BLK_MROOM;
1710 return 1;
1711 }
1712
1713 qc_stream_buf_release(qcs->tx.stream);
1714 return 0;
1715 }
1716
1717 /* Returns true if stream layer can proceed to emission via <qcs>. */
1718 int qcc_stream_can_send(const struct qcs *qcs)
1719 {
1720 return !(qcs->flags & QC_SF_BLK_MROOM) && !LIST_INLIST(&qcs->el_buf);
1721 }
1722
1723 /* Wakes up every streams of <qcc> which are currently waiting for sending but
1724 * are blocked on connection flow control.
1725 */
1726 static void qcc_notify_fctl(struct qcc *qcc)
1727 {
1728 struct qcs *qcs;
1729
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);
1735 }
1736 }
1737
1738 /* Free <qcc> STREAM frames in Tx list. */
1739 static void qcc_clear_frms(struct qcc *qcc)
1740 {
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);
1745 }
1746 }
1747
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
1751 * control stream.
1752 */
1753 static void _qcc_send_stream(struct qcs *qcs, int urg)
1754 {
1755 struct qcc *qcc = qcs->qcc;
1756
1757 qcc_clear_frms(qcc);
1758
1759 if (urg) {
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)));
1762
1763 LIST_DEL_INIT(&qcs->el_send);
1764 LIST_INSERT(&qcc->send_list, &qcs->el_send);
1765 }
1766 else {
1767 if (!LIST_INLIST(&qcs->el_send))
1768 LIST_APPEND(&qcs->qcc->send_list, &qcs->el_send);
1769 }
1770 }
1771
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.
1774 */
1775 void qcc_reset_stream(struct qcs *qcs, int err, int tevt)
1776 {
1777 struct qcc *qcc = qcs->qcc;
1778 const uint64_t diff = qcs_prep_bytes(qcs);
1779
1780 if ((qcs->flags & QC_SF_TO_RESET) || qcs_is_close_local(qcs))
1781 return;
1782
1783 /* TODO if QCS waiting for buffer, it could be removed from
1784 * <qcc.buf_wait_list> if sending is closed now.
1785 */
1786
1787 TRACE_STATE("reset stream", QMUX_EV_QCS_END, qcc->conn, qcs);
1788 qcs->flags |= QC_SF_TO_RESET;
1789 qcs->rs_err = err;
1790
1791 /* On BE side, a QCS may be resetted before any data emission. */
1792 if (conn_is_back(qcs->qcc->conn))
1793 qcs_idle_open(qcs);
1794
1795 if (diff) {
1796 const int soft_blocked = qfctl_sblocked(&qcc->tx.fc);
1797
1798 /* Soft offset cannot be inferior to real one. */
1799 BUG_ON(qcc->tx.fc.off_soft - diff < qcc->tx.fc.off_real);
1800
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);
1805
1806 /* Reset QCS soft off to prevent BUG_ON() on qcs_destroy(). */
1807 qcs->tx.fc.off_soft = qcs->tx.fc.off_real;
1808 }
1809
1810 /* Report send error to stream-endpoint layer. */
1811 if (qcs_sc(qcs)) {
1812 se_fl_set_error(qcs->sd);
1813 if (tevt)
1814 se_report_term_evt(qcs->sd, tevt);
1815 qcs_alert(qcs);
1816 }
1817
1818 _qcc_send_stream(qcs, 1);
1819 tasklet_wakeup(qcc->wait_event.tasklet);
1820 }
1821
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.
1825 */
1826 void qcc_send_stream(struct qcs *qcs, int urg, int count)
1827 {
1828 struct qcc *qcc = qcs->qcc;
1829
1830 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1831
1832 /* Cannot send STREAM if already closed. */
1833 BUG_ON(qcs_is_close_local(qcs));
1834
1835 _qcc_send_stream(qcs, urg);
1836
1837 if (count) {
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);
1842 }
1843
1844 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1845 }
1846
1847 /* Prepare for the emission of STOP_SENDING on <qcs>. */
1848 void qcc_abort_stream_read(struct qcs *qcs, int err)
1849 {
1850 struct qcc *qcc = qcs->qcc;
1851
1852 TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn, qcs);
1853
1854 if ((qcs->flags & QC_SF_READ_ABORTED) || qcs_is_close_remote(qcs))
1855 goto end;
1856
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);
1859 qcs->ss_err = err;
1860
1861 _qcc_send_stream(qcs, 1);
1862 tasklet_wakeup(qcc->wait_event.tasklet);
1863
1864 end:
1865 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn, qcs);
1866 }
1867
1868 /* Install the applicative layer of a QUIC connection on mux <qcc>.
1869 * Returns 0 on success else non-zero.
1870 */
1871 int qcc_install_app_ops(struct qcc *qcc)
1872 {
1873 struct connection *conn = qcc->conn;
1874 const struct qcc_app_ops *app_ops;
1875 struct server *srv;
1876 const char *alpn;
1877 int alpn_len;
1878
1879 TRACE_ENTER(QMUX_EV_QCC_NEW, conn);
1880
1881 if (!conn_get_alpn(conn, &alpn, &alpn_len)) {
1882 if (!conn_is_back(conn))
1883 goto err;
1884
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);
1889
1890 if (!alpn)
1891 goto err;
1892 alpn_len = strlen(srv->path_params.nego_alpn);
1893 }
1894
1895 app_ops = quic_alpn_to_app_ops(alpn, alpn_len);
1896 if (!app_ops)
1897 goto err;
1898
1899 if (app_ops->init && !app_ops->init(qcc)) {
1900 TRACE_ERROR("application layer install error", QMUX_EV_QCC_NEW, conn);
1901 goto err;
1902 }
1903
1904 TRACE_PROTO("application layer installed", QMUX_EV_QCC_NEW, conn);
1905 qcc->app_ops = app_ops;
1906
1907 TRACE_LEAVE(QMUX_EV_QCC_NEW, conn);
1908 return 0;
1909
1910 err:
1911 TRACE_LEAVE(QMUX_EV_QCC_NEW, conn);
1912 return 1;
1913 }
1914
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.
1920 *
1921 * Returns the buffer instance or NULL in case of error.
1922 */
1923 static struct qc_stream_rxbuf *qcs_get_rxbuf(struct qcs *qcs, uint64_t offset,
1924 uint64_t *len)
1925 {
1926 struct qcc *qcc = qcs->qcc;
1927 struct eb64_node *node;
1928 struct qc_stream_rxbuf *buf;
1929 struct ncbuf *ncbuf;
1930
1931 TRACE_ENTER(QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
1932
1933 node = eb64_lookup_le(&qcs->rx.bufs, offset);
1934 if (node)
1935 buf = container_of(node, struct qc_stream_rxbuf, off_node);
1936
1937 if (!node || offset >= buf->off_end) {
1938 const uint64_t aligned_off = offset - (offset % qcm_stream_rx_bufsz());
1939
1940 TRACE_DEVEL("allocating a new entry", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
1941 buf = pool_alloc(pool_head_qc_stream_rxbuf);
1942 if (!buf) {
1943 TRACE_ERROR("qcs rxbuf alloc error", QMUX_EV_QCC_RECV, qcc->conn, qcs);
1944 goto err;
1945 }
1946
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);
1952 }
1953
1954 ncbuf = &buf->ncb;
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);
1957 goto err;
1958 }
1959
1960 if (offset + *len > buf->off_end)
1961 *len = buf->off_end - offset;
1962
1963 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
1964 return buf;
1965
1966 err:
1967 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
1968 return NULL;
1969 }
1970
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.
1974 *
1975 * Returns 0 on success else non-zero. On error, the received frame should not
1976 * be acknowledged.
1977 */
1978 int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
1979 char fin, char *data)
1980 {
1981 const int fin_standalone = (!len && fin);
1982 struct qcs *qcs;
1983 enum ncb_ret ncb_ret;
1984 uint64_t left;
1985 int ret;
1986
1987 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1988
1989 if (qcc->flags & QC_CF_ERRL) {
1990 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
1991 goto err;
1992 }
1993
1994 /* RFC 9000 19.8. STREAM Frames
1995 *
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
1999 * stream.
2000 */
2001 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
2002 TRACE_DATA("qcs retrieval error", QMUX_EV_QCC_RECV, qcc->conn);
2003 goto err;
2004 }
2005
2006 if (!qcs) {
2007 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV, qcc->conn);
2008 goto out;
2009 }
2010
2011 /* RFC 9000 4.5. Stream Final Size
2012 *
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
2017 * handling.
2018 */
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);
2024 goto err;
2025 }
2026
2027 if (qcs_is_close_remote(qcs)) {
2028 TRACE_DATA("skipping STREAM for remotely closed", QMUX_EV_QCC_RECV, qcc->conn);
2029 goto out;
2030 }
2031
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);
2035 goto out;
2036 }
2037
2038 TRACE_PROTO("receiving STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
2039 qcs_idle_open(qcs);
2040
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;
2045
2046 if (offset + len > qcs->rx.msd ||
2047 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
2048 /* RFC 9000 4.1. Data Flow Control
2049 *
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
2053 */
2054 TRACE_ERROR("flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
2055 qcc->conn, qcs);
2056 qcc_set_error(qcc, QC_ERR_FLOW_CONTROL_ERROR, 0,
2057 muxc_tevt_type_proto_err);
2058 goto err;
2059 }
2060 }
2061
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;
2065
2066 len -= diff;
2067 data += diff;
2068 offset = qcs->rx.offset;
2069 }
2070
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;
2076 }
2077 }
2078
2079 left = len;
2080 while (left) {
2081 struct qc_stream_rxbuf *buf;
2082 struct proxy *px;
2083 struct quic_counters *prx_counters;
2084 ncb_sz_t ncb_off;
2085
2086 buf = qcs_get_rxbuf(qcs, offset, &len);
2087 if (!buf) {
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);
2091 goto err;
2092 }
2093
2094 /* For oldest buffer, ncb_advance() may already have been performed. */
2095 ncb_off = offset - MAX(qcs->rx.offset, buf->off_node.key);
2096
2097 ncb_ret = ncb_add(&buf->ncb, ncb_off, data, len, NCB_ADD_COMPARE);
2098 switch (ncb_ret) {
2099 case NCB_RET_OK:
2100 break;
2101
2102 case NCB_RET_DATA_REJ:
2103 /* RFC 9000 2.2. Sending and Receiving Data
2104 *
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.
2112 */
2113 TRACE_ERROR("overlapping data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
2114 qcc->conn, qcs);
2115 qcc_set_error(qcc, QC_ERR_PROTOCOL_VIOLATION, 0,
2116 muxc_tevt_type_proto_err);
2117 return 1;
2118
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,
2121 qcc->conn, qcs);
2122 px = qcc->proxy;
2123 prx_counters = EXTRA_COUNTERS_GET(px->extra_counters_fe, &quic_stats_module);
2124 HA_ATOMIC_INC(&prx_counters->ncbuf_gap_limit);
2125 return 1;
2126 }
2127
2128 offset += len;
2129 data += len;
2130 left -= len;
2131 len = left;
2132 }
2133
2134 if (fin)
2135 qcs->flags |= QC_SF_SIZE_KNOWN;
2136
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);
2140 }
2141
2142 while ((qcs_rx_avail_data(qcs) && !(qcs->flags & QC_SF_DEM_FULL)) ||
2143 unlikely(fin_standalone && qcs_is_close_remote(qcs))) {
2144
2145 ret = qcc_decode_qcs(qcc, qcs);
2146 LIST_DEL_INIT(&qcs->el_recv);
2147 qcc_refresh_timeout(qcc);
2148
2149 if (ret <= 0)
2150 break;
2151
2152 BUG_ON_HOT(fin_standalone); /* On fin_standalone <ret> should be NULL, which ensures no infinite loop. */
2153 }
2154
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().
2159 */
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);
2162 qcc_release(qcc);
2163 return 0;
2164 }
2165
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);
2169
2170 out:
2171 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
2172 return 0;
2173
2174 err:
2175 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
2176 return 1;
2177 }
2178
2179 /* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
2180 * the frame.
2181 *
2182 * Returns 0 on success else non-zero.
2183 */
2184 int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
2185 {
2186 const int blocked_soft = qfctl_sblocked(&qcc->tx.fc);
2187 int unblock_soft = 0, unblock_real = 0;
2188
2189 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
2190
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);
2194
2195 if (unblock_real)
2196 tasklet_wakeup(qcc->wait_event.tasklet);
2197
2198 if (unblock_soft)
2199 qcc_notify_fctl(qcc);
2200
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
2204 */
2205 if (!qfctl_rblocked(&qcc->tx.fc) && blocked_soft)
2206 qcc_clear_frms(qcc);
2207 }
2208
2209 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
2210 return 0;
2211 }
2212
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.
2215 *
2216 * Returns 0 on success else non-zero. On error, the received frame should not
2217 * be acknowledged.
2218 */
2219 int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
2220 {
2221 struct qcs *qcs;
2222
2223 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
2224
2225 if (qcc->flags & QC_CF_ERRL) {
2226 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
2227 goto err;
2228 }
2229
2230 /* RFC 9000 19.10. MAX_STREAM_DATA Frames
2231 *
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.
2237 */
2238 if (qcc_get_qcs(qcc, id, 0, 1, &qcs))
2239 goto err;
2240
2241 if (qcs) {
2242 const int blocked_soft = qfctl_sblocked(&qcs->tx.fc);
2243 int unblock_soft = 0, unblock_real = 0;
2244
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);
2248 if (unblock_real) {
2249 /* TODO optim: only wakeup IO-CB if stream has data to sent. */
2250 tasklet_wakeup(qcc->wait_event.tasklet);
2251 }
2252
2253 if (unblock_soft) {
2254 tot_time_stop(&qcs->timer.fctl);
2255 qcs_notify_send(qcs);
2256 }
2257
2258 /* Same refresh condition as qcc_recv_max_data(). */
2259 if (!qfctl_rblocked(&qcs->tx.fc) && blocked_soft)
2260 qcc_clear_frms(qcc);
2261 }
2262 }
2263
2264 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
2265 qcc_refresh_timeout(qcc);
2266
2267 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
2268 return 0;
2269
2270 err:
2271 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
2272 return 1;
2273 }
2274
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.
2278 *
2279 * Returns 0 on success else non-zero. On error, the received frame should not
2280 * be acknowledged.
2281 */
2282 int qcc_recv_max_streams(struct qcc *qcc, uint64_t max, int bidi)
2283 {
2284 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
2285
2286 if (qcc->flags & QC_CF_ERRL) {
2287 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
2288 goto err;
2289 }
2290
2291 /* RFC 9000 19.11. MAX_STREAMS Frames
2292 *
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.
2297 */
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);
2302 goto err;
2303 }
2304
2305 TRACE_PROTO("receiving MAX_STREAMS", QMUX_EV_QCC_RECV, qcc->conn);
2306 if (bidi) {
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;
2310 }
2311 }
2312 else {
2313 /* TODO no extra unidirectional streams open after connection
2314 * startup, so uni MAX_STREAMS flow-control is not necessary
2315 * for now.
2316 */
2317 }
2318
2319 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
2320 return 0;
2321
2322 err:
2323 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
2324 return 1;
2325 }
2326
2327 /* Handle a new RESET_STREAM frame from stream ID <id> with error code <err>
2328 * and final stream size <final_size>.
2329 *
2330 * Returns 0 on success else non-zero. On error, the received frame should not
2331 * be acknowledged.
2332 */
2333 int qcc_recv_reset_stream(struct qcc *qcc, uint64_t id, uint64_t err, uint64_t final_size)
2334 {
2335 struct qcs *qcs;
2336 struct qc_stream_rxbuf *b;
2337 int prev_glitches = qcc->glitches;
2338 uint64_t to_consume;
2339
2340 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
2341
2342 if (qcc->flags & QC_CF_ERRL) {
2343 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
2344 goto err;
2345 }
2346
2347 /* RFC 9000 19.4. RESET_STREAM Frames
2348 *
2349 * An endpoint that receives a RESET_STREAM frame for a send-only stream
2350 * MUST terminate the connection with error STREAM_STATE_ERROR.
2351 */
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);
2354 goto err;
2355 }
2356
2357 /* RFC 9000 3.2. Receiving Stream States
2358 *
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".
2363 */
2364 if (!qcs || qcs_is_close_remote(qcs))
2365 goto out;
2366
2367 TRACE_PROTO("receiving RESET_STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
2368 qcs_idle_open(qcs);
2369
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);
2374 goto out;
2375 }
2376 }
2377
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);
2384 goto err;
2385 }
2386
2387 /* Discarded data to account for in connection flow control. */
2388 to_consume = final_size - qcs->rx.offset;
2389
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);
2397 goto err;
2398 }
2399
2400 qcc->lfctl.offsets_recv += to_consume;
2401
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);
2407
2408 /* RFC 9000 3.2. Receiving Stream States
2409 *
2410 * An
2411 * implementation MAY interrupt delivery of stream data, discard any
2412 * data that was not consumed, and signal the receipt of the
2413 * RESET_STREAM.
2414 */
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);
2419 }
2420
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;
2424
2425 /* Remove stream from recv_list if present. */
2426 LIST_DEL_INIT(&qcs->el_recv);
2427
2428 /* No more data will be received after peer reset. QCS can be purged
2429 * if there is nothing to sent on our side.
2430 */
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);
2436 }
2437 else {
2438 /* Wake-up upper stream layer to notify about reset on read. */
2439 qcs_alert(qcs);
2440 }
2441
2442 out:
2443 if (qcc->glitches != prev_glitches && !(qcc->flags & QC_CF_IS_BACK))
2444 session_add_glitch_ctr(qcc->conn->owner, qcc->glitches - prev_glitches);
2445
2446 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
2447 return 0;
2448
2449 err:
2450 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
2451 return 1;
2452 }
2453
2454 /* Handle a new STOP_SENDING frame for stream ID <id>. The error code should be
2455 * specified in <err>.
2456 *
2457 * Returns 0 on success else non-zero. On error, the received frame should not
2458 * be acknowledged.
2459 */
2460 int qcc_recv_stop_sending(struct qcc *qcc, uint64_t id, uint64_t err)
2461 {
2462 struct qcs *qcs;
2463 int prev_glitches = qcc->glitches;
2464
2465 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
2466
2467 if (qcc->flags & QC_CF_ERRL) {
2468 TRACE_DATA("connection on error", QMUX_EV_QCC_RECV, qcc->conn);
2469 goto err;
2470 }
2471
2472 /* RFC 9000 19.5. STOP_SENDING Frames
2473 *
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.
2479 */
2480 if (qcc_get_qcs(qcc, id, 0, 1, &qcs))
2481 goto err;
2482
2483 if (!qcs)
2484 goto out;
2485
2486 TRACE_PROTO("receiving STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
2487
2488 /* RFC 9000 3.5. Solicited State Transitions
2489 *
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.
2495 */
2496
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.
2502 */
2503 if (qcs_is_close_local(qcs)) {
2504 TRACE_STATE("ignoring STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
2505 goto out;
2506 }
2507
2508 qcs_idle_open(qcs);
2509
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);
2513 goto out;
2514 }
2515 }
2516
2517 if (qcs->sd) {
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));
2522 qcs_alert(qcs);
2523 }
2524 else {
2525 se_report_term_evt(qcs->sd, se_tevt_type_rst_rcvd);
2526 }
2527
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;
2532 }
2533 }
2534
2535 /* RFC 9000 3.5. Solicited State Transitions
2536 *
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.
2544 *
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
2547 * code.
2548 */
2549 qcc_reset_stream(qcs, err, 0);
2550
2551 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
2552 qcc_refresh_timeout(qcc);
2553
2554 out:
2555 if (qcc->glitches != prev_glitches && !(qcc->flags & QC_CF_IS_BACK))
2556 session_add_glitch_ctr(qcc->conn->owner, qcc->glitches - prev_glitches);
2557
2558 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
2559 return 0;
2560
2561 err:
2562 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
2563 return 1;
2564 }
2565
2566 /* Signal the closing of remote stream with id <id>. Flow-control for new
2567 * streams may be allocated for the peer if needed.
2568 */
2569 static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
2570 {
2571 struct quic_frame *frm;
2572 uint64_t conn_max, rem, non_extra, inc;
2573
2574 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn);
2575
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);
2582 goto out;
2583 }
2584
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.
2588 */
2589 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_rel / 2 ||
2590 qcc->lfctl.cl_bidi_r + qcc->lfctl.ms_bidi == max) {
2591
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;
2596
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);
2602 }
2603
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.
2609 */
2610 qcc->lfctl.ms_bidi_rel = rem;
2611 qcc->lfctl.cl_bidi_r = 0;
2612 goto out;
2613 }
2614
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;
2619 }
2620 else {
2621 /* Stream elasticity not active, flow control increase remains static. */
2622 inc = qcc->lfctl.cl_bidi_r;
2623 }
2624
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);
2627 if (!frm) {
2628 qcc_set_error(qcc, QC_ERR_INTERNAL_ERROR, 0,
2629 muxc_tevt_type_internal_err);
2630 goto err;
2631 }
2632
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);
2636
2637 qcc->lfctl.ms_bidi += inc;
2638 qcc->lfctl.cl_bidi_r = 0;
2639 }
2640 }
2641 else {
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
2645 * are supported.
2646 */
2647 }
2648
2649 out:
2650 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
2651 return 0;
2652
2653 err:
2654 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_END, qcc->conn);
2655 return 1;
2656 }
2657
2658 /* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
2659 static void qcs_destroy(struct qcs *qcs)
2660 {
2661 struct qcc *qcc = qcs->qcc;
2662 struct connection *conn = qcc->conn;
2663 const uint64_t id = qcs->id;
2664
2665 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
2666
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.
2670 */
2671 BUG_ON(qcs->tx.fc.off_soft != qcs->tx.fc.off_real);
2672
2673 if (quic_stream_is_remote(qcc, id))
2674 qcc_release_remote_stream(qcc, id);
2675 }
2676
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.
2680 *
2681 * TODO QCS should not be released if not closed remotely to guarantee
2682 * the connection flow control consistency
2683 */
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);
2688 }
2689
2690 qcs_free(qcs);
2691
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);
2696 }
2697
2698 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
2699 }
2700
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.
2707 *
2708 * Returns the payload length of the STREAM frame or a negative error code.
2709 */
2710 static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
2711 struct list *frm_list, uint64_t window_conn)
2712 {
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);
2717 uint64_t total;
2718
2719 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
2720
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);
2723
2724 total = bytes;
2725
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;
2730 }
2731
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;
2736 }
2737
2738 /* Reset FIN if bytes to send is capped by flow control. */
2739 if (total < bytes)
2740 fin = 0;
2741
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);
2745 return 0;
2746 }
2747
2748 TRACE_PROTO("sending STREAM frame", QMUX_EV_QCS_SEND, qcc->conn, qcs);
2749 frm = qc_frm_alloc(QUIC_FT_STREAM_8);
2750 if (!frm) {
2751 TRACE_ERROR("frame alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
2752 goto err;
2753 }
2754
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;
2760
2761 if (total) {
2762 frm->stream.buf = out;
2763 frm->stream.data = (unsigned char *)b_peek(out, b_data(out) - bytes);
2764 }
2765 else {
2766 /* Empty STREAM frame. */
2767 frm->stream.buf = NULL;
2768 frm->stream.data = NULL;
2769 }
2770
2771 /* FIN is positioned only when the buffer has been totally emptied. */
2772 if (fin)
2773 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
2774
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;
2778 }
2779
2780 /* Always set length bit as we do not know if there is remaining frames
2781 * in the final packet after this STREAM.
2782 */
2783 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
2784 frm->stream.len = total;
2785
2786 LIST_APPEND(frm_list, &frm->list);
2787
2788 out:
2789 {
2790 struct qcs_build_stream_trace_arg arg = {
2791 .len = frm->stream.len, .fin = fin,
2792 .offset = frm->stream.offset,
2793 };
2794 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
2795 qcc->conn, qcs, &arg);
2796 }
2797
2798 return total;
2799
2800 err:
2801 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
2802 return -1;
2803 }
2804
2805 /* Returns true if subscribe set, false otherwise. */
2806 static int qcc_subscribe_send(struct qcc *qcc)
2807 {
2808 struct connection *conn = qcc->conn;
2809
2810 /* Do not subscribe if lower layer in error. */
2811 if (conn->flags & CO_FL_ERROR)
2812 return 0;
2813
2814 if (qcc->wait_event.events & SUB_RETRY_SEND)
2815 return 1;
2816
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);
2819 return 1;
2820 }
2821
2822 static int qcc_quic_send_frames(struct qcc *qcc, struct list *frms, int stream)
2823 {
2824 enum quic_tx_err ret;
2825 struct quic_pacer *pacer = NULL;
2826
2827 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
2828
2829 if (stream && qcc_is_pacing_active(qcc->conn))
2830 pacer = &qcc->tx.pacer;
2831
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);
2836 goto err;
2837 }
2838
2839 /* If there is frames left at this stage, transport layer is blocked.
2840 * Subscribe on it to retry later.
2841 */
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);
2845 goto err;
2846 }
2847
2848 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
2849 return ret == QUIC_TX_ERR_PACING ? 1 : 0;
2850
2851 err:
2852 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
2853 return -1;
2854 }
2855
2856 /* Wrapper for send on transport layer. Send a list of frames <frms> for the
2857 * connection <qcc>.
2858 *
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.
2861 */
2862 static int qcc_send_frames(struct qcc *qcc, struct list *frms, int stream)
2863 {
2864 if (LIST_ISEMPTY(frms)) {
2865 TRACE_DEVEL("leaving on no frame to send", QMUX_EV_QCC_SEND, qcc->conn);
2866 return -1;
2867 }
2868
2869 return conn_is_quic(qcc->conn) ? qcc_quic_send_frames(qcc, frms, stream) :
2870 qcc_qmux_send_frames(qcc, frms);
2871 }
2872
2873 /* Emit a RESET_STREAM on <qcs>.
2874 *
2875 * Returns 0 if the frame has been successfully sent else non-zero.
2876 */
2877 static int qcs_send_reset(struct qcs *qcs)
2878 {
2879 struct list frms = LIST_HEAD_INIT(frms);
2880 struct quic_frame *frm;
2881
2882 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
2883
2884 frm = qc_frm_alloc(QUIC_FT_RESET_STREAM);
2885 if (!frm) {
2886 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
2887 return 1;
2888 }
2889
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;
2893
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);
2899 return 1;
2900 }
2901
2902 qcs_close_local(qcs);
2903 qcs->flags &= ~QC_SF_TO_RESET;
2904
2905 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
2906 return 0;
2907 }
2908
2909 /* Emit a STOP_SENDING on <qcs>.
2910 *
2911 * Returns 0 if the frame has been successfully sent else non-zero.
2912 */
2913 static int qcs_send_stop_sending(struct qcs *qcs)
2914 {
2915 struct list frms = LIST_HEAD_INIT(frms);
2916 struct quic_frame *frm;
2917 struct qcc *qcc = qcs->qcc;
2918
2919 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
2920
2921 /* RFC 9000 3.3. Permitted Frame Types
2922 *
2923 * A
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.¶
2930 */
2931 if (qcs_is_close_remote(qcs)) {
2932 TRACE_STATE("skip STOP_SENDING on remote already closed", QMUX_EV_QCS_SEND, qcc->conn, qcs);
2933 goto done;
2934 }
2935
2936 frm = qc_frm_alloc(QUIC_FT_STOP_SENDING);
2937 if (!frm) {
2938 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
2939 return 1;
2940 }
2941
2942 frm->stop_sending.id = qcs->id;
2943 frm->stop_sending.app_error_code = qcs->ss_err;
2944
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);
2950 return 1;
2951 }
2952
2953 done:
2954 qcs->flags &= ~QC_SF_TO_STOP_SENDING;
2955
2956 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
2957 return 0;
2958 }
2959
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.
2965 *
2966 * Returns the payload length of the STREAM frame or a negative error code.
2967 */
2968 static int qcs_send(struct qcs *qcs, struct list *frms, uint64_t window_conn)
2969 {
2970 struct qcc *qcc = qcs->qcc;
2971 struct buffer *out = qcs_tx_buf(qcs);
2972 int flen = 0;
2973 const char fin = qcs->flags & QC_SF_FIN_STREAM;
2974
2975 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
2976
2977 /* Cannot send STREAM on remote unidirectional streams. */
2978 BUG_ON(quic_stream_is_uni(qcs->id) && quic_stream_is_remote(qcc, qcs->id));
2979
2980 /* This function must not be called if there is nothing to send. */
2981 BUG_ON(!fin && !qcs_prep_bytes(qcs));
2982
2983 /* Skip STREAM frame allocation if already subscribed for send.
2984 * Happens on sendto transient error or network congestion.
2985 */
2986 if (qcc->wait_event.events & SUB_RETRY_SEND) {
2987 TRACE_DEVEL("already subscribed for sending",
2988 QMUX_EV_QCS_SEND, qcc->conn, qcs);
2989 goto err;
2990 }
2991
2992 /* Build a new STREAM frame with <out> buffer. */
2993 flen = qcs_build_stream_frm(qcs, out, fin, frms, window_conn);
2994 if (flen < 0)
2995 goto err;
2996
2997 out:
2998 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
2999 return flen;
3000
3001 err:
3002 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
3003 return -1;
3004 }
3005
3006 /* Emit prepared flow-control related frames from <qcc> list.
3007 *
3008 * Returns 0 on success else non zero.
3009 */
3010 static int qcc_emit_fctl(struct qcc *qcc)
3011 {
3012 struct list single_frm = LIST_HEAD_INIT(single_frm);
3013 struct quic_frame *frm;
3014 struct eb64_node *node;
3015 struct qcs *qcs;
3016 int64_t id;
3017
3018 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
3019
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;
3025
3026 LIST_DELETE(&frm->list);
3027 LIST_APPEND(&single_frm, &frm->list);
3028
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);
3034 }
3035 goto err;
3036 }
3037
3038 BUG_ON_HOT(!LIST_ISEMPTY(&single_frm));
3039
3040 /* If frame is MAX_STREAM_DATA, reset corresponding QCS msd_frm pointer. */
3041 if (id >= 0) {
3042 node = eb64_lookup(&qcc->streams_by_id, id);
3043 if (node) {
3044 qcs = eb64_entry(node, struct qcs, by_id);
3045 qcs->tx.msd_frm = NULL;
3046 }
3047 }
3048 }
3049
3050 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
3051 return 0;
3052
3053 err:
3054 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
3055 return 1;
3056 }
3057
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.
3061 *
3062 * Returns 0 on success else non-zero.
3063 */
3064 static int qcc_emit_rs_ss(struct qcc *qcc)
3065 {
3066 struct qcs *qcs, *qcs_tmp;
3067
3068 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
3069
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)));
3074
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.
3078 */
3079 if (!(qcs->flags & (QC_SF_TO_STOP_SENDING|QC_SF_TO_RESET|QC_SF_TXBUB_OOB)))
3080 break;
3081
3082 TRACE_DATA("prepare for RS/SS transfer", QMUX_EV_QCC_SEND, qcc->conn, qcs);
3083
3084 /* Each RS and SS frame is sent individually. Necessary to
3085 * ensure it has been emitted as there is no transport callback
3086 * for now.
3087 *
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.
3091 */
3092
3093 if (qcs->flags & QC_SF_TO_STOP_SENDING) {
3094 if (qcs_send_stop_sending(qcs))
3095 goto err;
3096
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);
3101
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);
3106 }
3107 continue;
3108 }
3109 }
3110
3111 if (qcs->flags & QC_SF_TO_RESET) {
3112 if (qcs_send_reset(qcs))
3113 goto err;
3114
3115 /* RFC 9000 3.3. Permitted Frame Types
3116 *
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.
3121 */
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);
3126 }
3127 continue;
3128 }
3129 }
3130
3131 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
3132 return 0;
3133
3134 err:
3135 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
3136 return 1;
3137 }
3138
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.
3142 *
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.
3147 *
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
3150 * encoded.
3151 */
3152 static int qcc_build_frms(struct qcc *qcc, struct list *qcs_failed)
3153 {
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;
3158
3159 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
3160
3161 /* Frames list must first be cleared via qcc_clear_frms(). */
3162 BUG_ON(!LIST_ISEMPTY(&qcc->tx.frms));
3163
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)
3167 break;
3168
3169 TRACE_DATA("prepare for data transfer", QMUX_EV_QCC_SEND, qcc->conn, qcs);
3170
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)));
3175
3176 /* Total sent bytes must not exceed connection window. */
3177 BUG_ON(total > window_conn);
3178
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);
3185 continue;
3186 }
3187
3188 total += ret;
3189 if (ret) {
3190 /* Move QCS with some bytes transferred at the
3191 * end of send-list for next iterations.
3192 */
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 */
3196 if (!first_qcs)
3197 first_qcs = qcs;
3198 }
3199 }
3200 }
3201
3202 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
3203 return total;
3204 }
3205
3206 /* Schedule <qcc> after emission was interrupted on pacing. */
3207 static void qcc_wakeup_pacing(struct qcc *qcc)
3208 {
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;
3215 }
3216
3217 /* Proceed to sending. Loop through all available streams for the <qcc>
3218 * instance and try to send as much as possible.
3219 *
3220 * Returns the total of bytes sent to the transport layer.
3221 */
3222 static int qcc_io_send(struct qcc *qcc)
3223 {
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;
3230
3231 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
3232
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;
3236 }
3237
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.
3242 */
3243
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);
3247 goto out;
3248 }
3249
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;
3259 }
3260 goto out;
3261 }
3262
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);
3266 goto out;
3267 }
3268
3269 if (qcc->app_st < QCC_APP_ST_INIT) {
3270 if (qcc_app_init(qcc))
3271 goto out;
3272 }
3273
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);
3277 goto out;
3278 }
3279 }
3280
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);
3283 goto out;
3284 }
3285
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))
3290 goto out;
3291 }
3292
3293 if (!LIST_ISEMPTY(frms) && qcc_is_pacing_active(qcc->conn)) {
3294 if (!quic_pacing_reload(&qcc->tx.pacer)) {
3295 qcc_wakeup_pacing(qcc);
3296 total = 0;
3297 goto out;
3298 }
3299 }
3300
3301 /* Retry sending until no frame to send, data rejected or connection
3302 * flow-control limit reached.
3303 */
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);
3308 goto out;
3309 }
3310
3311 window_conn = qfctl_rcap(&qcc->tx.fc);
3312 resent = 0;
3313
3314 /* Reloop over <qcc.send_list>. Useful for streams which have
3315 * fulfilled their qc_stream_desc buf and have now release it.
3316 */
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.
3321 */
3322 BUG_ON((!conn_is_quic(qcc->conn) || qcs->tx.stream->buf) &&
3323 !qfctl_rblocked(&qcs->tx.fc));
3324
3325 /* Total sent bytes must not exceed connection window. */
3326 BUG_ON(resent > window_conn);
3327
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);
3332 continue;
3333 }
3334
3335 total += ret;
3336 resent += ret;
3337 }
3338 }
3339 }
3340
3341 if (ret == 1) {
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);
3345 }
3346
3347 out:
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);
3353 }
3354
3355 if (!qfctl_rblocked(&qcc->tx.fc))
3356 tasklet_wakeup(qcc->wait_event.tasklet);
3357 }
3358
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;
3363 }
3364
3365 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
3366 return total;
3367 }
3368
3369 /* Detects QUIC handshake completion. Any SE_FL_WAIT_FOR_HS streams are woken
3370 * up if wait-for-handshake is active.
3371 */
3372 static void qcc_wait_for_hs(struct qcc *qcc)
3373 {
3374 struct connection *conn = qcc->conn;
3375 struct quic_conn *qc = conn->handle.qc;
3376 struct eb64_node *node;
3377 struct qcs *qcs;
3378
3379 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
3380
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;
3385 }
3386
3387 /* wake-up any stream blocked on early data transfer */
3388 node = eb64_first(&qcc->streams_by_id);
3389 while (node) {
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);
3394 }
3395
3396 qcc->flags &= ~QC_CF_WAIT_HS;
3397 }
3398
3399 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
3400 }
3401
3402 /* Proceed on receiving. Loop on streams subscribed in recv_list and performed
3403 * STREAM frames decoding upon them.
3404 *
3405 * Returns the number of newly transcoded bytes.
3406 */
3407 static int qcc_io_recv(struct qcc *qcc)
3408 {
3409 struct qcs *qcs;
3410 int total = 0, ret;
3411
3412 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
3413
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);
3417 return 0;
3418 }
3419
3420 if ((qcc->flags & QC_CF_WAIT_HS) && !(qcc->wait_event.events & SUB_RETRY_RECV))
3421 qcc_wait_for_hs(qcc);
3422
3423 if (!conn_is_quic(qcc->conn)) {
3424 if (!(qcc->wait_event.events & SUB_RETRY_RECV))
3425 qcc_qmux_recv(qcc);
3426 }
3427
3428 next_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));
3433
3434 while (qcs_rx_avail_data(qcs) && !(qcs->flags & QC_SF_DEM_FULL)) {
3435 ret = qcc_decode_qcs(qcc, qcs);
3436 if (ret <= 0) {
3437 LIST_DEL_INIT(&qcs->el_recv);
3438 /* Interrupt all receive if connection on error. */
3439 if (qcc->flags & QC_CF_ERRL)
3440 goto done;
3441 /* Decode next entry if stream on error. */
3442 goto next_recv;
3443 }
3444
3445 total += ret;
3446 }
3447
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.
3454 */
3455 LIST_DEL_INIT(&qcs->el_recv);
3456 }
3457
3458 done:
3459 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
3460 return total;
3461 }
3462
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.
3465 *
3466 * Returns the value which is a positive integer or 0 if no new stream
3467 * currently available.
3468 */
3469 static int qcm_avail_streams(struct connection *conn)
3470 {
3471 const struct server *srv = __objt_server(conn->target);
3472 struct qcc *qcc = conn->ctx;
3473 int ret;
3474
3475 if (!qcc_be_is_reusable(qcc))
3476 return 0;
3477
3478 ret = qcc_fctl_avail_streams(qcc, 1);
3479
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;
3483
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
3486 * exceeded.
3487 */
3488 if (ret > 1 && srv->max_reuse >= 0 && qcc->tot_sc == srv->max_reuse)
3489 ret = 1;
3490 else if (ret > 1 && unlikely(qcc->next_bidi_l == QCS_ID_MAX_STRM_CL_BIDI))
3491 ret = 1;
3492
3493 return ret;
3494 }
3495
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.
3498 */
3499 static int qcm_used_streams(struct connection *conn)
3500 {
3501 struct qcc *qcc = conn->ctx;
3502 return qcc->nb_sc;
3503 }
3504
3505 /* Release all streams which have their transfer operation achieved. */
3506 static void qcc_purge_streams(struct qcc *qcc)
3507 {
3508 struct qcs *qcs;
3509
3510 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
3511
3512 while (!LIST_ISEMPTY(&qcc->purg_list)) {
3513 qcs = LIST_ELEM(qcc->purg_list.n, struct qcs *, el_send);
3514
3515 TRACE_STATE("purging stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
3516 BUG_ON_HOT(!qcs_is_completed(qcs));
3517 qcs_destroy(qcs);
3518 }
3519
3520 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
3521 }
3522
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.
3525 */
3526 static void qcc_wake_streams(struct qcc *qcc)
3527 {
3528 struct qcs *qcs;
3529 struct eb64_node *node;
3530
3531 TRACE_POINT(QMUX_EV_QCC_WAKE, qcc->conn);
3532
3533 for (node = eb64_first(&qcc->streams_by_id); node;
3534 node = eb64_next(node)) {
3535 qcs = eb64_entry(node, struct qcs, by_id);
3536
3537 if (!qcs_sc(qcs))
3538 continue;
3539
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);
3543 }
3544
3545 qcs_alert(qcs);
3546 }
3547 }
3548
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.
3552 *
3553 * Returns 1 if <qcc> must be released else 0.
3554 */
3555 static int qcc_io_process(struct qcc *qcc)
3556 {
3557 if (!LIST_ISEMPTY(&qcc->purg_list))
3558 qcc_purge_streams(qcc);
3559
3560 /* Check if a soft-stop is in progress.
3561 *
3562 * TODO this is relevant for frontend connections only.
3563 */
3564 if (unlikely(qcc->proxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
3565 int close = 1;
3566
3567 /* If using listener socket, soft-stop is not supported. The
3568 * connection must be closed immediately.
3569 */
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);
3573 qcc_io_send(qcc);
3574 goto out;
3575 }
3576
3577 TRACE_DEVEL("proxy disabled, prepare connection soft-stop", QMUX_EV_QCC_WAKE, qcc->conn);
3578
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.
3582 */
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));
3589 }
3590 }
3591 else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE) {
3592 close = 0; /* let the client close his connection himself */
3593 }
3594
3595 if (close)
3596 qcc_app_shutdown(qcc);
3597 }
3598
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);
3602
3603 out:
3604 if (qcc_is_dead(qcc))
3605 return 1;
3606
3607 return 0;
3608 }
3609
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.
3613 *
3614 * Returns 0 on success else non-zero.
3615 */
3616 static int qcc_app_init(struct qcc *qcc)
3617 {
3618 int ret;
3619
3620 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
3621
3622 if (qcc->app_ops->finalize) {
3623 ret = qcc->app_ops->finalize(qcc->ctx);
3624 if (ret < 0) {
3625 TRACE_ERROR("app ops finalize error", QMUX_EV_QCC_NEW, qcc->conn);
3626 goto err;
3627 }
3628
3629 if (ret) {
3630 TRACE_STATE("cannot finalize app ops yet", QMUX_EV_QCC_NEW, qcc->conn);
3631 goto again;
3632 }
3633 }
3634
3635 qcc->app_st = QCC_APP_ST_INIT;
3636
3637 again:
3638 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
3639 return 0;
3640
3641 err:
3642 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
3643 return 1;
3644 }
3645
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.
3649 */
3650 static void qcc_app_shutdown(struct qcc *qcc)
3651 {
3652 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
3653
3654 if (qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL)) {
3655 TRACE_DATA("connection on error", QMUX_EV_QCC_END, qcc->conn);
3656 goto out;
3657 }
3658
3659 if (qcc->app_st >= QCC_APP_ST_SHUT)
3660 goto out;
3661
3662 if (qcc->app_st < QCC_APP_ST_INIT) {
3663 if (qcc_app_init(qcc))
3664 goto out;
3665 }
3666
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);
3671 qcc_io_send(qcc);
3672 }
3673 else {
3674 qcc->err = quic_err_transport(QC_ERR_NO_ERROR);
3675 }
3676
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
3681 * already set.
3682 *
3683 * TODO implement a wrapper function for this in quic-conn module
3684 */
3685 if (!(qcc->conn->handle.qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
3686 qcc->conn->handle.qc->err = qcc->err;
3687 }
3688
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);
3695 }
3696
3697 out:
3698 qcc->app_st = QCC_APP_ST_SHUT;
3699 TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn);
3700 }
3701
3702 /* Free all resources allocated for <qcc> connection. */
3703 static void qcc_release(struct qcc *qcc)
3704 {
3705 struct connection *conn = qcc->conn;
3706 struct eb64_node *node;
3707 struct quic_conn *qc;
3708
3709 TRACE_ENTER(QMUX_EV_QCC_END, conn);
3710
3711 task_destroy(qcc->pacing_task);
3712
3713 if (qcc->task) {
3714 task_destroy(qcc->task);
3715 qcc->task = NULL;
3716 }
3717
3718 /* free remaining qcs instances */
3719 node = eb64_first(&qcc->streams_by_id);
3720 while (node) {
3721 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
3722 node = eb64_next(node);
3723 qcs_free(qcs);
3724 }
3725
3726 if (conn && conn_is_quic(conn)) {
3727 qc = conn->handle.qc;
3728
3729 /* unsubscribe from all remaining qc_stream_desc */
3730 node = eb64_first(&qc->streams_by_id);
3731 while (node) {
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);
3735 }
3736
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;
3741 }
3742 }
3743
3744 tasklet_free(qcc->wait_event.tasklet);
3745 if (conn) {
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,
3750 &qcc->wait_event);
3751 }
3752 }
3753
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);
3757 }
3758
3759 qcc_clear_frms(qcc);
3760
3761 if (qcc->app_ops) {
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;
3766 }
3767 TRACE_PROTO("application layer released", QMUX_EV_QCC_END, conn);
3768
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);
3773 }
3774
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);
3779 }
3780
3781 TRACE_USER("release mux", QMUX_EV_QCC_END, conn);
3782 pool_free(pool_head_qcc, qcc);
3783
3784 if (conn) {
3785 conn->mux = NULL;
3786 conn->ctx = NULL;
3787
3788 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
3789
3790 conn_stop_tracking(conn);
3791 conn_full_close(conn);
3792 if (conn->destroy_cb)
3793 conn->destroy_cb(conn);
3794 conn_free(conn);
3795 }
3796
3797 TRACE_LEAVE(QMUX_EV_QCC_END);
3798 }
3799
3800 struct task *qcc_io_cb(struct task *t, void *ctx, unsigned int state)
3801 {
3802 struct qcc *qcc = ctx;
3803 struct connection *conn;
3804 int total = 0, conn_in_list;
3805
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!
3809 */
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.
3815 */
3816 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3817 tasklet_free((struct tasklet *)t);
3818 t = NULL;
3819 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
3820 return NULL;
3821 }
3822 conn = qcc->conn;
3823 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
3824
3825 /* Remove the connection from the list, to be sure nobody attempts
3826 * to use it while we handle the I/O events
3827 */
3828 if (LIST_INLIST(&conn->idle_list)) {
3829 conn_in_list = 1;
3830 BUG_ON(!(conn->flags & CO_FL_LIST_MASK));
3831 conn_delete_from_tree(conn, tid);
3832 }
3833 else if (LIST_INLIST(&conn->sess_el)) {
3834 conn_in_list = 1;
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);
3838 }
3839 else {
3840 conn_in_list = 0;
3841 }
3842
3843 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3844 } else {
3845 /* we're certain the connection was not in an idle list */
3846 conn = qcc->conn;
3847 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
3848 conn_in_list = 0;
3849 }
3850
3851 if (!(qcc->wait_event.events & SUB_RETRY_SEND))
3852 total += qcc_io_send(qcc);
3853
3854 total += qcc_io_recv(qcc);
3855
3856 if (qcc_io_process(qcc)) {
3857 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, conn);
3858 goto release;
3859 }
3860
3861 if (total)
3862 qcc_refresh_timeout(qcc);
3863
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);
3868 }
3869
3870 if (conn_in_list) {
3871 struct server *srv = __objt_server(conn->target);
3872
3873 if (srv->cur_admin & SRV_ADMF_MAINT) {
3874 /* Do not store an idle conn if server in maintenance. */
3875 goto release;
3876 }
3877
3878 if (conn->flags & CO_FL_SESS_IDLE) {
3879 if (!session_reinsert_idle_conn(conn->owner, conn)) {
3880 /* session add conn failure */
3881 goto release;
3882 }
3883 }
3884 else {
3885 srv_add_idle(srv, conn, (conn->flags & CO_FL_LIST_MASK) == CO_FL_SAFE_LIST);
3886 }
3887
3888 /* Do not access conn without protection as soon as it is reinserted in idle list. */
3889 conn = NULL;
3890 }
3891
3892 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
3893
3894 return t;
3895
3896 release:
3897 qcc_app_shutdown(qcc);
3898 qcc_release(qcc);
3899
3900 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
3901
3902 return NULL;
3903 }
3904
3905 static struct task *qcc_pacing_task(struct task *t, void *ctx, unsigned int state)
3906 {
3907 struct qcc *qcc = ctx;
3908 int expired = tick_is_expired(t->expire, now_ms);
3909
3910 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
3911
3912 if (!expired) {
3913 if (!tick_isset(t->expire))
3914 TRACE_DEVEL("cancelled pacing task", QMUX_EV_QCC_WAKE, qcc->conn);
3915 goto requeue;
3916 }
3917
3918 /* Reschedule I/O immediately. */
3919 tasklet_wakeup_after(NULL, qcc->wait_event.tasklet);
3920
3921 requeue:
3922 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
3923 return t;
3924 }
3925
3926 static struct task *qcc_timeout_task(struct task *t, void *ctx, unsigned int state)
3927 {
3928 struct qcc *qcc = ctx;
3929 int expired = tick_is_expired(t->expire, now_ms);
3930
3931 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
3932
3933 if (qcc) {
3934 /* Make sure nobody stole the connection from us */
3935 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3936
3937 /* Somebody already stole the connection from us, so we should
3938 * not free it, we just have to free the task.
3939 */
3940 if (!t->context) {
3941 qcc = NULL;
3942 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3943 goto out;
3944 }
3945
3946 if (!expired) {
3947 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3948 TRACE_DEVEL("not expired", QMUX_EV_QCC_WAKE, qcc->conn);
3949 goto requeue;
3950 }
3951
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;
3956 goto requeue;
3957 }
3958
3959 /* We're about to destroy the connection, so make sure nobody
3960 * attempts to steal it from us.
3961 */
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);
3966
3967 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
3968 }
3969
3970 task_destroy(t);
3971
3972 if (!qcc) {
3973 TRACE_DEVEL("no more qcc", QMUX_EV_QCC_WAKE);
3974 goto out;
3975 }
3976
3977 /* Mark timeout as triggered by setting task to NULL. */
3978 qcc->task = NULL;
3979
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
3983 * seems legitimate.
3984 */
3985 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
3986 qcc_app_shutdown(qcc);
3987 qcc_release(qcc);
3988
3989 out:
3990 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
3991 return NULL;
3992
3993 requeue:
3994 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
3995 return t;
3996 }
3997
3998 /* Minimal initialization of <qcc> members to use qcc_release() safely. */
3999 static void _qcc_init(struct qcc *qcc)
4000 {
4001 qcc->conn = NULL;
4002 qcc->pacing_task = NULL;
4003 qcc->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);
4009 }
4010
4011 static int qcm_init(struct connection *conn, struct proxy *prx,
4012 struct session *sess, struct buffer *input)
4013 {
4014 struct qcc *qcc;
4015 const struct quic_transport_params *lparams, *rparams;
4016 void *conn_ctx = conn->ctx;
4017
4018 TRACE_ENTER(QMUX_EV_QCC_NEW);
4019
4020 qcc = pool_alloc(pool_head_qcc);
4021 if (!qcc) {
4022 TRACE_ERROR("alloc failure", QMUX_EV_QCC_NEW);
4023 goto err;
4024 }
4025
4026 _qcc_init(qcc);
4027 conn->ctx = qcc;
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;
4031 qcc->glitches = 0;
4032 qcc->term_evts_log = 0;
4033 qcc->err = quic_err_transport(QC_ERR_NO_ERROR);
4034
4035 if (conn_is_quic(conn)) {
4036 /* Server parameters, params used for RX flow control. */
4037 lparams = &conn->handle.qc->rx.params;
4038
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;
4045
4046 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
4047 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
4048
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;
4056 }
4057 else {
4058 rparams = xprt_qmux_rparams(conn->xprt_ctx);
4059 qfctl_init(&qcc->tx.fc, rparams->initial_max_data);
4060
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;
4066
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;
4074
4075 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
4076 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
4077 }
4078
4079 qcc->tx.buf_in_flight = 0;
4080
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;
4084
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);
4089 goto err;
4090 }
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;
4095 }
4096
4097 if (!conn_is_quic(conn)) {
4098 qcc->tx.qmux_buf = BUF_NULL;
4099 qcc->rx.qmux_buf = BUF_NULL;
4100
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));
4105 }
4106
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;
4112 }
4113 else {
4114 qcc->largest_bidi_r = 0x00;
4115 qcc->next_bidi_l = 0x01;
4116 qcc->largest_uni_r = 0x02;
4117 qcc->next_uni_l = 0x03;
4118
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);
4122 }
4123 }
4124
4125 qcc->wait_event.tasklet = tasklet_new();
4126 if (!qcc->wait_event.tasklet) {
4127 TRACE_ERROR("tasklet alloc failure", QMUX_EV_QCC_NEW);
4128 goto err;
4129 }
4130
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);
4136
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;
4141
4142 qcc->proxy = prx;
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;
4148 }
4149 else {
4150 qcc->timeout = prx->timeout.client;
4151 qcc->shut_timeout = tick_isset(prx->timeout.clientfin) ?
4152 prx->timeout.clientfin : prx->timeout.client;
4153 }
4154
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.
4157 */
4158 qcc->task = task_new_here();
4159 if (!qcc->task) {
4160 TRACE_ERROR("timeout task alloc failure", QMUX_EV_QCC_NEW);
4161 goto err;
4162 }
4163 qcc->task->process = qcc_timeout_task;
4164 qcc->task->context = qcc;
4165 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
4166
4167 qcc_reset_idle_start(qcc);
4168 LIST_INIT(&qcc->opening_list);
4169
4170 if (conn_is_quic(conn)) {
4171 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
4172 }
4173 else {
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);
4177 }
4178
4179 /* Register conn as app_ops may use it. */
4180 qcc->conn = conn;
4181
4182 if (qcc_install_app_ops(qcc)) {
4183 TRACE_PROTO("Cannot install app layer", QMUX_EV_QCC_NEW|QMUX_EV_QCC_ERR, conn);
4184 goto err;
4185 }
4186
4187 if (qcc->app_ops == &h3_ops && !conn_is_back(conn))
4188 proxy_inc_fe_cum_sess_ver_ctr(sess->listener, prx, 3);
4189
4190 if (!conn_is_back(conn)) {
4191 /* Register conn for idle front closing. */
4192 LIST_APPEND(&mux_stopping_data[tid].list, &conn->stopping_list);
4193
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);
4198 }
4199
4200 /* init read cycle */
4201 tasklet_wakeup(qcc->wait_event.tasklet);
4202
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.
4207 */
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,
4214 &qcc->wait_event);
4215 qcc->flags |= QC_CF_WAIT_HS;
4216 }
4217 }
4218 }
4219 }
4220 else {
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
4224 * transfer itself.
4225 */
4226 struct qcs *qcs;
4227 struct stconn *sc = conn_ctx;
4228
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);
4233 goto err;
4234 }
4235
4236 qcs = qcc_init_stream_local(qcc, 1);
4237 if (!qcs) {
4238 TRACE_PROTO("Cannot allocate a new locally initiated stream",
4239 QMUX_EV_QCC_NEW|QMUX_EV_QCC_ERR, conn);
4240 goto err;
4241 }
4242
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);
4246 qcs_free(qcs);
4247 goto err;
4248 }
4249
4250 qcs->sess = sess;
4251 qcs->sd = sc->sedesc;
4252 qcc->nb_sc++;
4253 qcc->tot_sc++;
4254 }
4255
4256 TRACE_USER("init mux", QMUX_EV_QCC_NEW, conn);
4257 TRACE_LEAVE(QMUX_EV_QCC_NEW, conn);
4258 return 0;
4259
4260 err:
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);
4267 }
4268 }
4269
4270 if (qcc) {
4271 /* In case of MUX init failure, session will ensure connection is freed. */
4272 qcc->conn = NULL;
4273 qcc_release(qcc);
4274 conn->ctx = NULL;
4275 }
4276
4277 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_NEW, qcc ? conn : NULL);
4278 return -1;
4279 }
4280
4281 static void qcm_destroy(void *ctx)
4282 {
4283 struct qcc *qcc = ctx;
4284
4285 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
4286 qcc_release(qcc);
4287 TRACE_LEAVE(QMUX_EV_QCC_END);
4288 }
4289
4290 static int qcm_strm_attach(struct connection *conn, struct sedesc *sd, struct session *sess)
4291 {
4292 struct qcs *qcs;
4293 struct qcc *qcc = conn->ctx;
4294
4295 TRACE_ENTER(QMUX_EV_QCS_NEW, conn);
4296
4297 /* Flow control limit on bidi streams should already have
4298 * been checked by a prior qcm_avail_streams() invocation.
4299 */
4300 BUG_ON(!qcc_fctl_avail_streams(qcc, 1));
4301
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);
4304
4305 qcs = qcc_init_stream_local(qcc, 1);
4306 if (!qcs) {
4307 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_NEW, qcc->conn);
4308 return -1;
4309 }
4310
4311 if (sc_attach_mux(sd->sc, qcs, conn)) {
4312 TRACE_DEVEL("leaving on error", QMUX_EV_QCS_NEW, qcc->conn);
4313 qcs_free(qcs);
4314 return -1;
4315 }
4316
4317 qcs->sess = sess;
4318 qcs->sd = sd->sc->sedesc;
4319 qcc->nb_sc++;
4320 qcc->tot_sc++;
4321
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);
4325
4326 TRACE_LEAVE(QMUX_EV_QCS_NEW, conn);
4327 return 0;
4328 }
4329
4330 static void qcm_strm_detach(struct sedesc *sd)
4331 {
4332 struct qcs *qcs = sd->se;
4333 struct qcc *qcc = qcs->qcc;
4334 struct connection *conn = qcc->conn;
4335 struct session *sess = qcs->sess;
4336
4337 TRACE_ENTER(QMUX_EV_STRM_END, conn, qcs);
4338
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.
4345 */
4346 //BUG_ON_HOT(!qcs_is_close_remote(qcs));
4347
4348 qcc_rm_sc(qcc);
4349
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);
4356
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");
4360
4361 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn, qcs);
4362 return;
4363 }
4364
4365 qcs_destroy(qcs);
4366
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);
4372
4373 /* Ensure conn is attached into session. Most of the times
4374 * this is already done during connect so this is a no-op.
4375 */
4376 session_add_conn(sess, conn);
4377
4378 if (!qcc->nb_sc) {
4379 if (!LIST_INLIST(&conn->sess_el)) {
4380 /* Session insertion above has failed and connection is idle, remove it. */
4381 goto release;
4382 }
4383
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.
4386 */
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);
4389
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);
4393 goto release;
4394 }
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.
4398 */
4399 conn = NULL;
4400 goto end;
4401 }
4402 }
4403 else {
4404 if (!qcc->nb_sc) {
4405 TRACE_DEVEL("prepare for idle connection reuse", QMUX_EV_STRM_END, conn);
4406
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.
4409 */
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);
4412
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);
4416 goto release;
4417 }
4418
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.
4422 */
4423 conn = NULL;
4424 goto end;
4425 }
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);
4431 }
4432 }
4433 }
4434
4435 if (qcc_is_dead(qcc)) {
4436 TRACE_STATE("killing dead connection", QMUX_EV_STRM_END, conn);
4437 goto release;
4438 }
4439 else {
4440 TRACE_DEVEL("refreshing connection's timeout", QMUX_EV_STRM_END, conn);
4441 qcc_refresh_timeout(qcc);
4442 }
4443
4444 end:
4445 TRACE_LEAVE(QMUX_EV_STRM_END, conn);
4446 return;
4447
4448 release:
4449 qcc_app_shutdown(qcc);
4450 qcc_release(qcc);
4451 TRACE_LEAVE(QMUX_EV_STRM_END);
4452 return;
4453 }
4454
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)
4458 {
4459 struct qcs *qcs = __sc_mux_strm(sc);
4460 struct qcc *qcc = qcs->qcc;
4461 size_t ret = 0;
4462 char fin = 0;
4463
4464 TRACE_ENTER(QMUX_EV_STRM_RECV, qcc->conn, qcs);
4465
4466 ret = qcs_http_rcv_buf(qcs, buf, count, &fin);
4467
4468 if (b_data(&qcs->rx.app_buf)) {
4469 se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
4470 }
4471 else {
4472 se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
4473
4474 /* Set end-of-input when full message properly received. */
4475 if (fin) {
4476 TRACE_STATE("report end-of-input", QMUX_EV_STRM_RECV, qcc->conn, qcs);
4477 se_fl_set(qcs->sd, SE_FL_EOI);
4478
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.
4482 */
4483 se_expect_data(qcs->sd);
4484 }
4485 }
4486
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);
4492
4493 /* Set error if EOI not reached. This may happen on
4494 * RESET_STREAM reception or connection error.
4495 */
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));
4500 }
4501 else {
4502 se_report_term_evt(qcs->sd, (qcc->flags & QC_CF_ERR_CONN ? se_tevt_type_rcv_err : se_tevt_type_eos));
4503 }
4504 }
4505
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);
4509 }
4510
4511 if (b_size(&qcs->rx.app_buf)) {
4512 b_free(&qcs->rx.app_buf);
4513 offer_buffers(NULL, 1);
4514 }
4515 }
4516
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.
4521 */
4522 BUG_ON(!qcs_rx_avail_data(qcs));
4523
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);
4528 }
4529 }
4530
4531 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcc->conn, qcs);
4532
4533 return ret;
4534 }
4535
4536 static size_t qcm_strm_snd_buf(struct stconn *sc, struct buffer *buf,
4537 size_t count, int flags)
4538 {
4539 struct qcs *qcs = __sc_mux_strm(sc);
4540 const size_t old_data = qcs_prep_bytes(qcs);
4541 size_t ret = 0;
4542 char fin;
4543
4544 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4545
4546 /* stream layer has been detached so no transfer must occur after. */
4547 BUG_ON_HOT(qcs->flags & QC_SF_DETACH);
4548
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);
4553 goto end;
4554 }
4555
4556 /* Cannot emit data after FIN/RESET_STREAM, drain extra payload. */
4557 if (qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_RESET)) {
4558 BUG_ON_STRESS(1);
4559 ret = qcs_http_reset_buf(qcs, buf, count);
4560 goto end;
4561 }
4562
4563 if (LIST_INLIST(&qcs->el_buf)) {
4564 TRACE_DEVEL("leaving on no buf avail", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4565 goto end;
4566 }
4567
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);
4576 }
4577 goto end;
4578 }
4579
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);
4584 goto end;
4585 }
4586
4587 ret = qcs_http_snd_buf(qcs, buf, count, &fin);
4588 if (fin) {
4589 TRACE_STATE("reached stream fin", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4590 qcs->flags |= QC_SF_FIN_STREAM;
4591 }
4592
4593 if (ret || fin) {
4594 const size_t data = qcs_prep_bytes(qcs) - old_data;
4595 if (data || fin)
4596 qcc_send_stream(qcs, 0, data);
4597
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);
4601 }
4602
4603 end:
4604 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4605
4606 return ret;
4607 }
4608
4609
4610 static size_t qcm_strm_nego_ff(struct stconn *sc, struct buffer *input,
4611 size_t count, unsigned int flags)
4612 {
4613 struct qcs *qcs = __sc_mux_strm(sc);
4614 size_t ret = 0;
4615
4616 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4617
4618 /* stream layer has been detached so no transfer must occur after. */
4619 BUG_ON_HOT(qcs->flags & QC_SF_DETACH);
4620
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;
4624 goto end;
4625 }
4626
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.
4630 */
4631 TRACE_DEVEL("connection in error", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4632 qcs->sd->iobuf.flags |= IOBUF_FL_NO_FF;
4633 goto end;
4634 }
4635
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.
4639 */
4640 TRACE_DEVEL("stream already closed", QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4641 qcs->sd->iobuf.flags |= IOBUF_FL_NO_FF;
4642 goto end;
4643 }
4644
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;
4648 goto end;
4649 }
4650
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);
4656 }
4657 qcs->sd->iobuf.flags |= IOBUF_FL_FF_BLOCKED;
4658 goto end;
4659 }
4660
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;
4664 goto end;
4665 }
4666
4667 /* Alawys disable splicing */
4668 qcs->sd->iobuf.flags |= IOBUF_FL_NO_SPLICING;
4669
4670 ret = qcs->qcc->app_ops->nego_ff(qcs, count);
4671 if (!ret)
4672 goto end;
4673
4674 /* forward remaining input data */
4675 if (b_data(input)) {
4676 size_t xfer = ret;
4677
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);
4683
4684 /* Cannot forward more data, wait for room */
4685 if (b_data(input)) {
4686 ret = 0;
4687 goto end;
4688 }
4689 }
4690 ret -= qcs->sd->iobuf.data;
4691
4692 end:
4693 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4694 return ret;
4695 }
4696
4697 static size_t qcm_strm_done_ff(struct stconn *sc)
4698 {
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;
4703
4704 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4705
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;
4709 }
4710
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);
4713
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.
4717 */
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);
4720 goto end;
4721 }
4722
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);
4726
4727 /* Reset stconn iobuf information. */
4728 qcs->sd->iobuf.buf = NULL;
4729 qcs->sd->iobuf.offset = 0;
4730 qcs->sd->iobuf.data = 0;
4731
4732 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
4733 tasklet_wakeup(qcc->wait_event.tasklet);
4734
4735 end:
4736 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
4737 return data;
4738 }
4739
4740 static int qcm_strm_resume_ff(struct stconn *sc, unsigned int flags)
4741 {
4742 return 0;
4743 }
4744
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.
4749 */
4750 static int qcm_strm_subscribe(struct stconn *sc, int event_type,
4751 struct wait_event *es)
4752 {
4753 return qcs_subscribe(__sc_mux_strm(sc), event_type, es);
4754 }
4755
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.
4759 */
4760 static int qcm_strm_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
4761 {
4762 struct qcs *qcs = __sc_mux_strm(sc);
4763
4764 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
4765 BUG_ON(qcs->subs && qcs->subs != es);
4766
4767 es->events &= ~event_type;
4768 if (!es->events)
4769 qcs->subs = NULL;
4770
4771 return 0;
4772 }
4773
4774 static int qcm_wake(struct connection *conn)
4775 {
4776 struct qcc *qcc = conn->ctx;
4777
4778 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
4779
4780 if (qcc_io_process(qcc)) {
4781 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
4782 goto release;
4783 }
4784
4785 /* Wake all streams, unless an error is set as qcc_io_process() has
4786 * already woken them in this case.
4787 */
4788 if (!(qcc->flags & (QC_CF_ERR_CONN|QC_CF_ERRL)))
4789 qcc_wake_streams(qcc);
4790
4791 qcc_refresh_timeout(qcc);
4792
4793 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
4794 return 0;
4795
4796 release:
4797 qcc_app_shutdown(qcc);
4798 qcc_release(qcc);
4799 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
4800 return 1;
4801 }
4802
4803 static void qcm_strm_shut(struct stconn *sc, unsigned int mode, struct se_abort_info *reason)
4804 {
4805 struct qcs *qcs = __sc_mux_strm(sc);
4806 struct qcc *qcc = qcs->qcc;
4807 enum qcc_app_ops_lclose_mode lclo_mode;
4808
4809 if (!(mode & (SE_SHW_SILENT|SE_SHW_NORMAL)))
4810 return;
4811
4812 TRACE_ENTER(QMUX_EV_STRM_SHUT, qcc->conn, qcs);
4813
4814 /* Not implemented. */
4815 BUG_ON(mode & SE_SHR_DRAIN);
4816
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))) {
4821
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;
4826 else
4827 lclo_mode = QCC_APP_OPS_LCLO_MODE_ABORT;
4828
4829 qcc->app_ops->lclose(qcs, lclo_mode);
4830 tasklet_wakeup(qcc->wait_event.tasklet);
4831 }
4832
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);
4836 }
4837
4838 out:
4839 TRACE_LEAVE(QMUX_EV_STRM_SHUT, qcc->conn, qcs);
4840 }
4841
4842 static int qcm_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
4843 {
4844 struct qcc *qcc = conn->ctx;
4845
4846 switch (mux_ctl) {
4847 case MUX_CTL_EXIT_STATUS:
4848 return MUX_ES_UNKNOWN;
4849
4850 case MUX_CTL_GET_GLITCHES:
4851 return qcc->glitches;
4852
4853 case MUX_CTL_GET_NBSTRM:
4854 return qcc->nb_hreq;
4855
4856 case MUX_CTL_GET_MAXSTRM:
4857 return qcc->lfctl.ms_bidi_rel;
4858
4859 case MUX_CTL_TEVTS:
4860 return qcc->term_evts_log;
4861
4862 default:
4863 return -1;
4864 }
4865 }
4866
4867 static int qcm_sctl(struct stconn *sc, enum mux_sctl_type mux_sctl, void *output)
4868 {
4869 int ret = 0;
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;
4874 struct buffer *buf;
4875
4876 switch (mux_sctl) {
4877 case MUX_SCTL_SID:
4878 if (output)
4879 *((int64_t *)output) = qcs->id;
4880 return ret;
4881
4882 case MUX_SCTL_DBG_STR:
4883 dbg_ctx = output;
4884 buf = get_trash_chunk();
4885
4886 if (dbg_ctx->arg.debug_flags & MUX_SCTL_DBG_STR_L_MUXS)
4887 qcm_dump_qcs_info(buf, qcs);
4888
4889 if (dbg_ctx->arg.debug_flags & MUX_SCTL_DBG_STR_L_MUXC)
4890 qcm_dump_qcc_info(buf, qcc);
4891
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));
4896 }
4897
4898 if (dbg_ctx->arg.debug_flags & MUX_SCTL_DBG_STR_L_XPRT)
4899 conn->xprt->dump_info(buf, conn);
4900
4901 dbg_ctx->ret.buf = *buf;
4902 return ret;
4903
4904 case MUX_SCTL_TEVTS:
4905 return qcs->sd->term_evts_log;
4906
4907 default:
4908 return -1;
4909 }
4910 }
4911
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
4915 * existing fields.
4916 */
4917 static int qcm_strm_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx)
4918 {
4919 struct qcs *qcs = sd->se;
4920 struct qcc *qcc;
4921 int ret = 0;
4922
4923 if (!qcs)
4924 return ret;
4925
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);
4929
4930 if (pfx)
4931 chunk_appendf(msg, "\n%s", pfx);
4932
4933 qcc = qcs->qcc;
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);
4936 return ret;
4937 }
4938
4939
4940 static const struct mux_ops quic_ops = {
4941 .init = qcm_init,
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,
4951 .wake = qcm_wake,
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,
4957 .ctl = qcm_ctl,
4958 .sctl = qcm_sctl,
4959 .show_sd = qcm_strm_show_sd,
4960 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
4961 .name = "QUIC",
4962 };
4963
4964 void qcc_show_quic(struct qcc *qcc)
4965 {
4966 const struct quic_conn *qc = qcc->conn->handle.qc;
4967 struct eb64_node *node;
4968
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);
4972
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);
4977 }
4978
4979 node = eb64_first(&qcc->streams_by_id);
4980 while (node) {
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));
4985
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);
4989 }
4990
4991 if (!quic_stream_is_uni(qcs->id) || !quic_stream_is_remote(qcc, qcs->id)) {
4992 if (qcs->tx.stream)
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);
4998 }
4999 chunk_appendf(&trash, "\n");
5000 node = eb64_next(node);
5001 }
5002 }
5003
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 };
5006
5007 INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);
5008
5009 static const struct mux_ops qmux_ops = {
5010 .init = qcm_init,
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,
5020 .wake = qcm_wake,
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,
5026 .ctl = qcm_ctl,
5027 .sctl = qcm_sctl,
5028 .show_sd = qcm_strm_show_sd,
5029 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_EXPERIMENTAL,
5030 .name = "QMUX",
5031 };
5032
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 };
5036
5037 INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_qmux);