blob: 6a324acf52819bc93fe86aa81c7baf38fbb7e879 [file] [log] [blame]
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001#include <haproxy/mux_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002
Amaury Denoyelleeb01f592021-10-07 16:44:05 +02003#include <import/eb64tree.h>
4
Frédéric Lécailledfbae762021-02-18 09:59:01 +01005#include <haproxy/api.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01006#include <haproxy/connection.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +01007#include <haproxy/dynbuf.h>
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02008#include <haproxy/list.h>
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02009#include <haproxy/ncbuf.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +010010#include <haproxy/pool.h>
Amaury Denoyelled80fbca2022-09-19 17:02:28 +020011#include <haproxy/qmux_http.h>
Amaury Denoyelle36d50bf2022-09-19 16:12:38 +020012#include <haproxy/qmux_trace.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020013#include <haproxy/quic_conn.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020014#include <haproxy/quic_stream.h>
Frédéric Lécaille748ece62022-05-21 23:58:40 +020015#include <haproxy/quic_tp-t.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020016#include <haproxy/ssl_sock-t.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020017#include <haproxy/stconn.h>
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010018#include <haproxy/trace.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010019
Amaury Denoyelledeed7772021-12-03 11:36:46 +010020DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010021DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
22
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +020023/* Emit a CONNECTION_CLOSE with error <err>. This will interrupt all future
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +020024 * send/receive operations.
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +020025 */
26static void qcc_emit_cc(struct qcc *qcc, int err)
27{
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +020028 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
29
30 TRACE_STATE("set CONNECTION_CLOSE on quic-conn", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle57e6db72022-07-13 15:07:56 +020031 quic_set_connection_close(qcc->conn->handle.qc, quic_err_transport(err));
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +020032 qcc->flags |= QC_CF_CC_EMIT;
33 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +020034
35 TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +020036}
37
Amaury Denoyelle4b167002022-12-12 09:59:50 +010038static void qc_free_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
39{
40 struct buffer buf;
41
42 if (ncb_is_null(ncbuf))
43 return;
44
45 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
46 b_free(&buf);
47 offer_buffers(NULL, 1);
48
49 *ncbuf = NCBUF_NULL;
50}
51
52/* Free <qcs> instance. This function is reserved for internal usage : it must
53 * only be called on qcs alloc error or on connection shutdown. Else
54 * qcs_destroy must be prefered to handle QUIC flow-control increase.
55 */
56static void qcs_free(struct qcs *qcs)
57{
58 struct qcc *qcc = qcs->qcc;
59
60 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn, qcs);
61
Amaury Denoyelle15337fd2022-12-20 14:47:16 +010062 /* Safe to use even if already removed from the list. */
63 LIST_DEL_INIT(&qcs->el_opening);
Amaury Denoyelle20f2a422023-01-03 14:39:24 +010064 LIST_DEL_INIT(&qcs->el_send);
Amaury Denoyelle4b167002022-12-12 09:59:50 +010065
66 /* Release stream endpoint descriptor. */
67 BUG_ON(qcs->sd && !se_fl_test(qcs->sd, SE_FL_ORPHAN));
68 sedesc_free(qcs->sd);
69
70 /* Release app-layer context. */
71 if (qcs->ctx && qcc->app_ops->detach)
72 qcc->app_ops->detach(qcs);
73
74 /* Release qc_stream_desc buffer from quic-conn layer. */
75 qc_stream_desc_release(qcs->stream);
76
77 /* Free Rx/Tx buffers. */
78 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
79 b_free(&qcs->tx.buf);
80
81 BUG_ON(!qcc->strms[qcs_id_type(qcs->id)].nb_streams);
82 --qcc->strms[qcs_id_type(qcs->id)].nb_streams;
83
84 /* Remove qcs from qcc tree. */
85 eb64_delete(&qcs->by_id);
86
87 pool_free(pool_head_qcs, qcs);
88
89 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
90}
91
Amaury Denoyelledeed7772021-12-03 11:36:46 +010092/* Allocate a new QUIC streams with id <id> and type <type>. */
Amaury Denoyellea509ffb2022-07-04 15:50:33 +020093static struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +010094{
Amaury Denoyelledeed7772021-12-03 11:36:46 +010095 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010096
Amaury Denoyelle4f137572022-03-24 17:10:00 +010097 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
98
Amaury Denoyelledeed7772021-12-03 11:36:46 +010099 qcs = pool_alloc(pool_head_qcs);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200100 if (!qcs) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200101 TRACE_ERROR("alloc failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200102 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200103 }
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200104
105 qcs->stream = NULL;
106 qcs->qcc = qcc;
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +0200107 qcs->sd = NULL;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200108 qcs->flags = QC_SF_NONE;
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200109 qcs->st = QC_SS_IDLE;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200110 qcs->ctx = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100111
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200112 /* App callback attach may register the stream for http-request wait.
113 * These fields must be initialed before.
114 */
115 LIST_INIT(&qcs->el_opening);
Amaury Denoyelle20f2a422023-01-03 14:39:24 +0100116 LIST_INIT(&qcs->el_send);
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200117 qcs->start = TICK_ETERNITY;
118
Amaury Denoyelle4b167002022-12-12 09:59:50 +0100119 /* store transport layer stream descriptor in qcc tree */
120 qcs->id = qcs->by_id.key = id;
121 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
122
123 qcc->strms[type].nb_streams++;
124
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200125 /* Allocate transport layer stream descriptor. Only needed for TX. */
126 if (!quic_stream_is_uni(id) || !quic_stream_is_remote(qcc, id)) {
127 struct quic_conn *qc = qcc->conn->handle.qc;
128 qcs->stream = qc_stream_desc_new(id, type, qcs, qc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200129 if (!qcs->stream) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200130 TRACE_ERROR("qc_stream_desc alloc failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200131 goto err;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200132 }
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200133 }
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200134
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200135 if (qcc->app_ops->attach) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200136 if (qcc->app_ops->attach(qcs, qcc->ctx)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200137 TRACE_ERROR("app proto failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200138 goto err;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200139 }
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200140 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100141
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100142 /* If stream is local, use peer remote-limit, or else the opposite. */
Amaury Denoyelle176174f2022-10-21 17:02:18 +0200143 if (quic_stream_is_bidi(id)) {
144 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
145 qcc->rfctl.msd_bidi_l;
146 }
147 else if (quic_stream_is_local(qcc, id)) {
148 qcs->tx.msd = qcc->rfctl.msd_uni_l;
149 }
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100150
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200151 qcs->rx.ncbuf = NCBUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100152 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200153 qcs->rx.offset = qcs->rx.offset_max = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100154
Amaury Denoyelle176174f2022-10-21 17:02:18 +0200155 if (quic_stream_is_bidi(id)) {
156 qcs->rx.msd = quic_stream_is_local(qcc, id) ? qcc->lfctl.msd_bidi_l :
157 qcc->lfctl.msd_bidi_r;
158 }
159 else if (quic_stream_is_remote(qcc, id)) {
160 qcs->rx.msd = qcc->lfctl.msd_uni_r;
161 }
Amaury Denoyellea9773552022-05-16 14:38:25 +0200162 qcs->rx.msd_init = qcs->rx.msd;
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200163
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100164 qcs->tx.buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100165 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100166 qcs->tx.sent_offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100167
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100168 qcs->wait_event.tasklet = NULL;
169 qcs->wait_event.events = 0;
170 qcs->subs = NULL;
171
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200172 qcs->err = 0;
173
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100174 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100175 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100176 return qcs;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200177
178 err:
Amaury Denoyelle4b167002022-12-12 09:59:50 +0100179 qcs_free(qcs);
180 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200181 return NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100182}
183
Amaury Denoyelle3abeb572022-07-04 11:42:27 +0200184static forceinline struct stconn *qcs_sc(const struct qcs *qcs)
185{
186 return qcs->sd ? qcs->sd->sc : NULL;
187}
188
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200189/* Reset the <qcc> inactivity timeout for http-keep-alive timeout. */
190static forceinline void qcc_reset_idle_start(struct qcc *qcc)
191{
192 qcc->idle_start = now_ms;
193}
194
Amaury Denoyellec603de42022-07-25 11:21:46 +0200195/* Decrement <qcc> sc. */
196static forceinline void qcc_rm_sc(struct qcc *qcc)
197{
198 BUG_ON_HOT(!qcc->nb_sc);
199 --qcc->nb_sc;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200200
201 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
202 * refreshed after this on stream detach.
203 */
204 if (!qcc->nb_sc && !qcc->nb_hreq)
205 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200206}
207
208/* Decrement <qcc> hreq. */
209static forceinline void qcc_rm_hreq(struct qcc *qcc)
210{
211 BUG_ON_HOT(!qcc->nb_hreq);
212 --qcc->nb_hreq;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200213
214 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
215 * refreshed after this on I/O handler.
216 */
217 if (!qcc->nb_sc && !qcc->nb_hreq)
218 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200219}
220
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200221static inline int qcc_is_dead(const struct qcc *qcc)
222{
223 /* Mux connection is considered dead if :
224 * - all stream-desc are detached AND
225 * = connection is on error OR
226 * = mux timeout has already fired or is unset
227 */
228 if (!qcc->nb_sc && ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task))
229 return 1;
230
231 return 0;
232}
233
234/* Return true if the mux timeout should be armed. */
235static inline int qcc_may_expire(struct qcc *qcc)
236{
237 return !qcc->nb_sc;
238}
239
240/* Refresh the timeout on <qcc> if needed depending on its state. */
241static void qcc_refresh_timeout(struct qcc *qcc)
242{
243 const struct proxy *px = qcc->proxy;
244
245 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
246
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200247 if (!qcc->task) {
248 TRACE_DEVEL("already expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200249 goto leave;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200250 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200251
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200252 /* Check if upper layer is responsible of timeout management. */
253 if (!qcc_may_expire(qcc)) {
254 TRACE_DEVEL("not eligible for timeout", QMUX_EV_QCC_WAKE, qcc->conn);
255 qcc->task->expire = TICK_ETERNITY;
256 task_queue(qcc->task);
257 goto leave;
258 }
259
260 /* TODO if connection is idle on frontend and proxy is disabled, remove
261 * it with global close_spread delay applied.
262 */
263
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200264 /* TODO implement client/server-fin timeout for graceful shutdown */
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200265
266 /* Frontend timeout management
267 * - detached streams with data left to send -> default timeout
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200268 * - stream waiting on incomplete request or no stream yet activated -> timeout http-request
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200269 * - idle after stream processing -> timeout http-keep-alive
270 */
271 if (!conn_is_back(qcc->conn)) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200272 if (qcc->nb_hreq) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200273 TRACE_DEVEL("one or more requests still in progress", QMUX_EV_QCC_WAKE, qcc->conn);
274 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200275 task_queue(qcc->task);
276 goto leave;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200277 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200278
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200279 if (!LIST_ISEMPTY(&qcc->opening_list) || unlikely(!qcc->largest_bidi_r)) {
280 int timeout = px->timeout.httpreq;
281 struct qcs *qcs = NULL;
282 int base_time;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200283
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200284 /* Use start time of first stream waiting on HTTP or
285 * qcc idle if no stream not yet used.
286 */
287 if (likely(!LIST_ISEMPTY(&qcc->opening_list)))
288 qcs = LIST_ELEM(qcc->opening_list.n, struct qcs *, el_opening);
289 base_time = qcs ? qcs->start : qcc->idle_start;
290
291 TRACE_DEVEL("waiting on http request", QMUX_EV_QCC_WAKE, qcc->conn, qcs);
292 qcc->task->expire = tick_add_ifset(base_time, timeout);
293 }
294 else {
295 /* Use http-request timeout if keep-alive timeout not set */
296 int timeout = tick_isset(px->timeout.httpka) ?
297 px->timeout.httpka : px->timeout.httpreq;
298
299 TRACE_DEVEL("at least one request achieved but none currently in progress", QMUX_EV_QCC_WAKE, qcc->conn);
300 qcc->task->expire = tick_add_ifset(qcc->idle_start, timeout);
301 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200302 }
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200303
304 /* fallback to default timeout if frontend specific undefined or for
305 * backend connections.
306 */
307 if (!tick_isset(qcc->task->expire)) {
308 TRACE_DEVEL("fallback to default timeout", QMUX_EV_QCC_WAKE, qcc->conn);
309 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200310 }
311
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200312 task_queue(qcc->task);
313
314 leave:
315 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
316}
317
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200318/* Mark a stream as open if it was idle. This can be used on every
319 * successful emission/reception operation to update the stream state.
320 */
321static void qcs_idle_open(struct qcs *qcs)
322{
323 /* This operation must not be used if the stream is already closed. */
324 BUG_ON_HOT(qcs->st == QC_SS_CLO);
325
326 if (qcs->st == QC_SS_IDLE) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200327 TRACE_STATE("opening stream", QMUX_EV_QCS_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200328 qcs->st = QC_SS_OPEN;
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200329 }
330}
331
332/* Close the local channel of <qcs> instance. */
333static void qcs_close_local(struct qcs *qcs)
334{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200335 TRACE_STATE("closing stream locally", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
336
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200337 /* The stream must have already been opened. */
338 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
339
340 /* This operation cannot be used multiple times. */
341 BUG_ON_HOT(qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO);
342
343 if (quic_stream_is_bidi(qcs->id)) {
344 qcs->st = (qcs->st == QC_SS_HREM) ? QC_SS_CLO : QC_SS_HLOC;
Amaury Denoyelleafb7b9d2022-09-19 11:58:24 +0200345
346 if (qcs->flags & QC_SF_HREQ_RECV)
347 qcc_rm_hreq(qcs->qcc);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200348 }
349 else {
350 /* Only local uni streams are valid for this operation. */
351 BUG_ON_HOT(quic_stream_is_remote(qcs->qcc, qcs->id));
352 qcs->st = QC_SS_CLO;
353 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200354}
355
356/* Close the remote channel of <qcs> instance. */
357static void qcs_close_remote(struct qcs *qcs)
358{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200359 TRACE_STATE("closing stream remotely", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
360
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200361 /* The stream must have already been opened. */
362 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
363
364 /* This operation cannot be used multiple times. */
365 BUG_ON_HOT(qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO);
366
367 if (quic_stream_is_bidi(qcs->id)) {
368 qcs->st = (qcs->st == QC_SS_HLOC) ? QC_SS_CLO : QC_SS_HREM;
369 }
370 else {
371 /* Only remote uni streams are valid for this operation. */
372 BUG_ON_HOT(quic_stream_is_local(qcs->qcc, qcs->id));
373 qcs->st = QC_SS_CLO;
374 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200375}
376
377static int qcs_is_close_local(struct qcs *qcs)
378{
379 return qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO;
380}
381
Amaury Denoyelle6eb3c4b2022-12-09 16:26:03 +0100382static int qcs_is_close_remote(struct qcs *qcs)
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200383{
384 return qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO;
385}
386
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100387struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100388{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100389 struct buffer *buf = b_alloc(bptr);
390 BUG_ON(!buf);
391 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100392}
393
Amaury Denoyellea441ec92022-07-04 15:48:57 +0200394static struct ncbuf *qc_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200395{
396 struct buffer buf = BUF_NULL;
397
398 if (ncb_is_null(ncbuf)) {
399 b_alloc(&buf);
400 BUG_ON(b_is_null(&buf));
401
402 *ncbuf = ncb_make(buf.area, buf.size, 0);
403 ncb_init(ncbuf, 0);
404 }
405
406 return ncbuf;
407}
408
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500409/* Notify an eventual subscriber on <qcs> or else wakeup up the stconn layer if
Amaury Denoyelle4561f842022-07-06 14:54:34 +0200410 * initialized.
411 */
412static void qcs_alert(struct qcs *qcs)
413{
414 if (qcs->subs) {
415 qcs_notify_recv(qcs);
416 qcs_notify_send(qcs);
417 }
418 else if (qcs_sc(qcs) && qcs->sd->sc->app_ops->wake) {
419 qcs->sd->sc->app_ops->wake(qcs->sd->sc);
420 }
421}
422
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100423int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
424{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100425 struct qcc *qcc = qcs->qcc;
426
427 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100428
429 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
430 BUG_ON(qcs->subs && qcs->subs != es);
431
432 es->events |= event_type;
433 qcs->subs = es;
434
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100435 if (event_type & SUB_RETRY_RECV)
436 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
437
438 if (event_type & SUB_RETRY_SEND)
439 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
440
441 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
442
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100443 return 0;
444}
445
446void qcs_notify_recv(struct qcs *qcs)
447{
448 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
449 tasklet_wakeup(qcs->subs->tasklet);
450 qcs->subs->events &= ~SUB_RETRY_RECV;
451 if (!qcs->subs->events)
452 qcs->subs = NULL;
453 }
454}
455
456void qcs_notify_send(struct qcs *qcs)
457{
458 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
459 tasklet_wakeup(qcs->subs->tasklet);
460 qcs->subs->events &= ~SUB_RETRY_SEND;
461 if (!qcs->subs->events)
462 qcs->subs = NULL;
463 }
464}
465
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200466/* Open a locally initiated stream for the connection <qcc>. Set <bidi> for a
467 * bidirectional stream, else an unidirectional stream is opened. The next
468 * available ID on the connection will be used according to the stream type.
469 *
470 * Returns the allocated stream instance or NULL on error.
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100471 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200472struct qcs *qcc_init_stream_local(struct qcc *qcc, int bidi)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100473{
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200474 struct qcs *qcs;
475 enum qcs_type type;
476 uint64_t *next;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100477
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200478 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
479
480 if (bidi) {
481 next = &qcc->next_bidi_l;
482 type = conn_is_back(qcc->conn) ? QCS_CLT_BIDI : QCS_SRV_BIDI;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100483 }
484 else {
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200485 next = &qcc->next_uni_l;
486 type = conn_is_back(qcc->conn) ? QCS_CLT_UNI : QCS_SRV_UNI;
487 }
488
489 /* TODO ensure that we won't overflow remote peer flow control limit on
490 * streams. Else, we should emit a STREAMS_BLOCKED frame.
491 */
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100492
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200493 qcs = qcs_new(qcc, *next, type);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200494 if (!qcs) {
495 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200496 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200497 }
Amaury Denoyellec055e302022-02-07 16:09:06 +0100498
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200499 TRACE_PROTO("opening local stream", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200500 *next += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100501
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200502 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200503 return qcs;
504}
505
506/* Open a remote initiated stream for the connection <qcc> with ID <id>. The
507 * caller is responsible to ensure that a stream with the same ID was not
508 * already opened. This function will also create all intermediaries streams
509 * with ID smaller than <id> not already opened before.
510 *
511 * Returns the allocated stream instance or NULL on error.
512 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200513static struct qcs *qcc_init_stream_remote(struct qcc *qcc, uint64_t id)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200514{
515 struct qcs *qcs = NULL;
516 enum qcs_type type;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200517 uint64_t *largest, max_id;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100518
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200519 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200520
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200521 BUG_ON_HOT(quic_stream_is_local(qcc, id));
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100522
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200523 if (quic_stream_is_bidi(id)) {
524 largest = &qcc->largest_bidi_r;
525 type = conn_is_back(qcc->conn) ? QCS_SRV_BIDI : QCS_CLT_BIDI;
526 }
527 else {
528 largest = &qcc->largest_uni_r;
529 type = conn_is_back(qcc->conn) ? QCS_SRV_UNI : QCS_CLT_UNI;
530 }
531
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200532 /* RFC 9000 4.6. Controlling Concurrency
533 *
534 * An endpoint that receives a frame with a stream ID exceeding the
535 * limit it has sent MUST treat this as a connection error of type
536 * STREAM_LIMIT_ERROR
537 */
538 max_id = quic_stream_is_bidi(id) ? qcc->lfctl.ms_bidi * 4 :
539 qcc->lfctl.ms_uni * 4;
540 if (id >= max_id) {
541 TRACE_ERROR("flow control error", QMUX_EV_QCS_NEW|QMUX_EV_PROTO_ERR, qcc->conn);
542 qcc_emit_cc(qcc, QC_ERR_STREAM_LIMIT_ERROR);
543 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200544 }
545
546 /* Only stream ID not already opened can be used. */
547 BUG_ON(id < *largest);
548
549 while (id >= *largest) {
Amaury Denoyellefd79ddb2022-08-16 11:13:45 +0200550 const char *str = *largest < id ? "initializing intermediary remote stream" :
551 "initializing remote stream";
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200552
553 qcs = qcs_new(qcc, *largest, type);
554 if (!qcs) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200555 /* TODO emit RESET_STREAM */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200556 TRACE_ERROR("stream fallocation failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200557 goto err;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100558 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200559
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200560 TRACE_PROTO(str, QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200561 *largest += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100562 }
563
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200564 out:
565 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle50742292022-03-29 14:57:19 +0200566 return qcs;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200567
568 err:
569 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
570 return NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200571}
572
573/* Use this function for a stream <id> which is not in <qcc> stream tree. It
574 * returns true if the associated stream is closed.
575 */
576static int qcc_stream_id_is_closed(struct qcc *qcc, uint64_t id)
577{
578 uint64_t *largest;
579
580 /* This function must only be used for stream not present in the stream tree. */
581 BUG_ON_HOT(eb64_lookup(&qcc->streams_by_id, id));
582
583 if (quic_stream_is_local(qcc, id)) {
584 largest = quic_stream_is_uni(id) ? &qcc->next_uni_l :
585 &qcc->next_bidi_l;
586 }
587 else {
588 largest = quic_stream_is_uni(id) ? &qcc->largest_uni_r :
589 &qcc->largest_bidi_r;
590 }
591
592 return id < *largest;
593}
594
595/* Retrieve the stream instance from <id> ID. This can be used when receiving
596 * STREAM, STREAM_DATA_BLOCKED, RESET_STREAM, MAX_STREAM_DATA or STOP_SENDING
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200597 * frames. Set to false <receive_only> or <send_only> if these particular types
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200598 * of streams are not allowed. If the stream instance is found, it is stored in
599 * <out>.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200600 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200601 * Returns 0 on success else non-zero. On error, a RESET_STREAM or a
602 * CONNECTION_CLOSE is automatically emitted. Beware that <out> may be NULL
603 * on success if the stream has already been closed.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200604 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200605int qcc_get_qcs(struct qcc *qcc, uint64_t id, int receive_only, int send_only,
606 struct qcs **out)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200607{
608 struct eb64_node *node;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200609
610 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200611 *out = NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200612
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200613 if (!receive_only && quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200614 TRACE_ERROR("receive-only stream not allowed", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id);
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200615 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200616 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200617 }
618
619 if (!send_only && quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200620 TRACE_ERROR("send-only stream not allowed", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id);
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200621 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200622 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200623 }
624
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200625 /* Search the stream in the connection tree. */
626 node = eb64_lookup(&qcc->streams_by_id, id);
627 if (node) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200628 *out = eb64_entry(node, struct qcs, by_id);
629 TRACE_DEVEL("using stream from connection tree", QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200630 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200631 }
632
633 /* Check if stream is already closed. */
634 if (qcc_stream_id_is_closed(qcc, id)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200635 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200636 /* Consider this as a success even if <out> is left NULL. */
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200637 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200638 }
639
640 /* Create the stream. This is valid only for remote initiated one. A
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500641 * local stream must have already been explicitly created by the
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200642 * application protocol layer.
643 */
644 if (quic_stream_is_local(qcc, id)) {
645 /* RFC 9000 19.8. STREAM Frames
646 *
647 * An endpoint MUST terminate the connection with error
648 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
649 * initiated stream that has not yet been created, or for a send-only
650 * stream.
651 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200652 TRACE_ERROR("locally initiated stream not yet created", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200653 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200654 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200655 }
656 else {
657 /* Remote stream not found - try to open it. */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200658 *out = qcc_init_stream_remote(qcc, id);
659 if (!*out) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200660 TRACE_ERROR("stream creation error", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200661 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200662 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200663 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100664
665 out:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200666 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200667 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200668
669 err:
670 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
671 return 1;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100672}
673
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200674/* Simple function to duplicate a buffer */
675static inline struct buffer qcs_b_dup(const struct ncbuf *b)
676{
677 return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0));
678}
679
Amaury Denoyelle36d4b5e2022-07-01 11:25:40 +0200680/* Remove <bytes> from <qcs> Rx buffer. Flow-control for received offsets may
681 * be allocated for the peer if needed.
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200682 */
683static void qcs_consume(struct qcs *qcs, uint64_t bytes)
684{
685 struct qcc *qcc = qcs->qcc;
686 struct quic_frame *frm;
687 struct ncbuf *buf = &qcs->rx.ncbuf;
688 enum ncb_ret ret;
689
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200690 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
691
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200692 ret = ncb_advance(buf, bytes);
693 if (ret) {
694 ABORT_NOW(); /* should not happens because removal only in data */
695 }
696
697 if (ncb_is_empty(buf))
698 qc_free_ncbuf(qcs, buf);
699
700 qcs->rx.offset += bytes;
Amaury Denoyellebb6296c2022-12-09 15:00:17 +0100701 /* Not necessary to emit a MAX_STREAM_DATA if all data received. */
702 if (qcs->flags & QC_SF_SIZE_KNOWN)
703 goto conn_fctl;
704
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200705 if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200706 TRACE_DATA("increase stream credit via MAX_STREAM_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200707 frm = pool_zalloc(pool_head_quic_frame);
708 BUG_ON(!frm); /* TODO handle this properly */
709
710 qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init;
711
712 LIST_INIT(&frm->reflist);
713 frm->type = QUIC_FT_MAX_STREAM_DATA;
714 frm->max_stream_data.id = qcs->id;
715 frm->max_stream_data.max_stream_data = qcs->rx.msd;
716
717 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
718 tasklet_wakeup(qcc->wait_event.tasklet);
719 }
720
Amaury Denoyellebb6296c2022-12-09 15:00:17 +0100721 conn_fctl:
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200722 qcc->lfctl.offsets_consume += bytes;
723 if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200724 TRACE_DATA("increase conn credit via MAX_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200725 frm = pool_zalloc(pool_head_quic_frame);
726 BUG_ON(!frm); /* TODO handle this properly */
727
728 qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init;
729
730 LIST_INIT(&frm->reflist);
731 frm->type = QUIC_FT_MAX_DATA;
732 frm->max_data.max_data = qcc->lfctl.md;
733
734 LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list);
735 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
736 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200737
738 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200739}
740
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200741/* Decode the content of STREAM frames already received on the stream instance
742 * <qcs>.
743 *
744 * Returns 0 on success else non-zero.
745 */
746static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
747{
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200748 struct buffer b;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200749 ssize_t ret;
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200750 int fin = 0;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200751
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200752 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
753
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200754 b = qcs_b_dup(&qcs->rx.ncbuf);
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200755
Amaury Denoyelled1310f82022-09-16 13:30:59 +0200756 /* Signal FIN to application if STREAM FIN received with all data. */
757 if (qcs_is_close_remote(qcs))
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200758 fin = 1;
759
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100760 if (!(qcs->flags & QC_SF_READ_ABORTED)) {
761 ret = qcc->app_ops->decode_qcs(qcs, &b, fin);
762 if (ret < 0) {
763 TRACE_ERROR("decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
764 goto err;
765 }
766 }
767 else {
768 TRACE_DATA("ignore read on stream", QMUX_EV_QCS_RECV, qcc->conn, qcs);
769 ret = b_data(&b);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200770 }
771
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200772 if (ret) {
773 qcs_consume(qcs, ret);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200774 qcs_notify_recv(qcs);
775 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200776
777 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200778 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200779
780 err:
781 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
782 return 1;
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200783}
784
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200785/* Emit a CONNECTION_CLOSE_APP with error <err>. Reserved for application error
Amaury Denoyelled666d742022-07-13 15:15:58 +0200786 * code. To close the connection right away, set <immediate> : this is useful
787 * when dealing with a connection fatal error. Else a graceful shutdown will be
788 * conducted : the error-code is only registered. The lower layer is
789 * responsible to close the connection when deemed suitable. Note that in this
790 * case the error code might be overwritten if an immediate close is requested
791 * in the interval.
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200792 */
Amaury Denoyelled666d742022-07-13 15:15:58 +0200793void qcc_emit_cc_app(struct qcc *qcc, int err, int immediate)
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200794{
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200795 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
796
Amaury Denoyelled666d742022-07-13 15:15:58 +0200797 if (immediate) {
798 quic_set_connection_close(qcc->conn->handle.qc, quic_err_app(err));
799 qcc->flags |= QC_CF_CC_EMIT;
800 tasklet_wakeup(qcc->wait_event.tasklet);
801 }
802 else {
803 /* Only register the error code for graceful shutdown. */
804 qcc->conn->handle.qc->err = quic_err_app(err);
805 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200806
807 TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200808}
809
810/* Prepare for the emission of RESET_STREAM on <qcs> with error code <err>. */
811void qcc_reset_stream(struct qcs *qcs, int err)
812{
813 struct qcc *qcc = qcs->qcc;
814
815 if ((qcs->flags & QC_SF_TO_RESET) || qcs_is_close_local(qcs))
816 return;
817
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200818 TRACE_STATE("reset stream", QMUX_EV_QCS_END, qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200819 qcs->flags |= QC_SF_TO_RESET;
820 qcs->err = err;
821 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100822}
823
Amaury Denoyelle20f2a422023-01-03 14:39:24 +0100824/* Register <qcs> stream for emission of STREAM, STOP_SENDING or RESET_STREAM. */
825void qcc_send_stream(struct qcs *qcs)
826{
827 struct qcc *qcc = qcs->qcc;
828
829 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
830
831 /* Cannot send if already closed. */
832 BUG_ON(qcs_is_close_local(qcs));
833
834 if (!LIST_INLIST(&qcs->el_send))
835 LIST_APPEND(&qcs->qcc->send_list, &qcs->el_send);
836
837 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
838}
839
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100840/* Prepare for the emission of STOP_SENDING on <qcs>. */
841void qcc_abort_stream_read(struct qcs *qcs)
842{
843 struct qcc *qcc = qcs->qcc;
844
845 TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn, qcs);
846
847 if ((qcs->flags & QC_SF_TO_STOP_SENDING) || qcs_is_close_remote(qcs))
848 goto end;
849
850 TRACE_STATE("abort stream read", QMUX_EV_QCS_END, qcc->conn, qcs);
851 qcs->flags |= (QC_SF_TO_STOP_SENDING|QC_SF_READ_ABORTED);
852 tasklet_wakeup(qcc->wait_event.tasklet);
853
854 end:
855 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn, qcs);
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200856}
857
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200858/* Install the <app_ops> applicative layer of a QUIC connection on mux <qcc>.
859 * Returns 0 on success else non-zero.
860 */
861int qcc_install_app_ops(struct qcc *qcc, const struct qcc_app_ops *app_ops)
862{
863 TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn);
864
865 qcc->app_ops = app_ops;
866 if (qcc->app_ops->init && !qcc->app_ops->init(qcc)) {
867 TRACE_ERROR("app ops init error", QMUX_EV_QCC_NEW, qcc->conn);
868 goto err;
869 }
870
871 TRACE_PROTO("application layer initialized", QMUX_EV_QCC_NEW, qcc->conn);
872
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200873 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
874 return 0;
875
876 err:
877 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
878 return 1;
879}
880
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200881/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
882 * <data> with length <len> and represents the offset <offset>. <fin> is set if
883 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100884 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200885 * Returns 0 on success else non-zero. On error, the received frame should not
886 * be acknowledged.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100887 */
888int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200889 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100890{
891 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200892 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100893
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100894 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
895
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200896 if (qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200897 TRACE_DATA("connection closed", QMUX_EV_QCC_RECV, qcc->conn);
898 goto err;
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200899 }
900
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200901 /* RFC 9000 19.8. STREAM Frames
902 *
903 * An endpoint MUST terminate the connection with error
904 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
905 * initiated stream that has not yet been created, or for a send-only
906 * stream.
907 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200908 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200909 TRACE_DATA("qcs retrieval error", QMUX_EV_QCC_RECV, qcc->conn);
910 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200911 }
912
913 if (!qcs) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200914 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV, qcc->conn);
915 goto out;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200916 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100917
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200918 /* RFC 9000 4.5. Stream Final Size
919 *
920 * Once a final size for a stream is known, it cannot change. If a
921 * RESET_STREAM or STREAM frame is received indicating a change in the
922 * final size for the stream, an endpoint SHOULD respond with an error
923 * of type FINAL_SIZE_ERROR; see Section 11 for details on error
924 * handling.
925 */
926 if (qcs->flags & QC_SF_SIZE_KNOWN &&
927 (offset + len > qcs->rx.offset_max || (fin && offset + len < qcs->rx.offset_max))) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200928 TRACE_ERROR("final size error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR, qcc->conn, qcs);
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200929 qcc_emit_cc(qcc, QC_ERR_FINAL_SIZE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200930 goto err;
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200931 }
932
Amaury Denoyelle5854fc02022-12-09 16:25:48 +0100933 if (qcs_is_close_remote(qcs)) {
934 TRACE_DATA("skipping STREAM for remotely closed", QMUX_EV_QCC_RECV, qcc->conn);
935 goto out;
936 }
937
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100938 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelled1310f82022-09-16 13:30:59 +0200939 /* TODO offset may have been received without FIN first and now
940 * with it. In this case, it must be notified to be able to
941 * close the stream.
942 */
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200943 TRACE_DATA("already received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
944 goto out;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100945 }
946
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200947 TRACE_PROTO("receiving STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200948 qcs_idle_open(qcs);
949
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200950 if (offset + len > qcs->rx.offset_max) {
951 uint64_t diff = offset + len - qcs->rx.offset_max;
952 qcs->rx.offset_max = offset + len;
953 qcc->lfctl.offsets_recv += diff;
954
955 if (offset + len > qcs->rx.msd ||
956 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
957 /* RFC 9000 4.1. Data Flow Control
958 *
959 * A receiver MUST close the connection with an error
960 * of type FLOW_CONTROL_ERROR if the sender violates
961 * the advertised connection or stream data limits
962 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200963 TRACE_ERROR("flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200964 qcc->conn, qcs);
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200965 qcc_emit_cc(qcc, QC_ERR_FLOW_CONTROL_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200966 goto err;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200967 }
968 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100969
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200970 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100971 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200972 ABORT_NOW();
973 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100974 }
975
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200976 TRACE_DATA("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200977 if (offset < qcs->rx.offset) {
Frédéric Lécaillea18c3332022-07-04 09:54:58 +0200978 size_t diff = qcs->rx.offset - offset;
979
980 len -= diff;
981 data += diff;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200982 offset = qcs->rx.offset;
983 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100984
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200985 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
986 if (ret != NCB_RET_OK) {
987 if (ret == NCB_RET_DATA_REJ) {
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200988 /* RFC 9000 2.2. Sending and Receiving Data
989 *
990 * An endpoint could receive data for a stream at the
991 * same stream offset multiple times. Data that has
992 * already been received can be discarded. The data at
993 * a given offset MUST NOT change if it is sent
994 * multiple times; an endpoint MAY treat receipt of
995 * different data at the same offset within a stream as
996 * a connection error of type PROTOCOL_VIOLATION.
997 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200998 TRACE_ERROR("overlapping data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200999 qcc->conn, qcs);
Amaury Denoyellecc3d7162022-05-20 15:14:57 +02001000 qcc_emit_cc(qcc, QC_ERR_PROTOCOL_VIOLATION);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001001 }
1002 else if (ret == NCB_RET_GAP_SIZE) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001003 TRACE_DATA("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
1004 qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001005 }
1006 return 1;
1007 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001008
1009 if (fin)
Amaury Denoyelle3f39b402022-07-01 16:11:03 +02001010 qcs->flags |= QC_SF_SIZE_KNOWN;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001011
Amaury Denoyelled1310f82022-09-16 13:30:59 +02001012 if (qcs->flags & QC_SF_SIZE_KNOWN &&
1013 qcs->rx.offset_max == qcs->rx.offset + ncb_data(&qcs->rx.ncbuf, 0)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001014 qcs_close_remote(qcs);
Amaury Denoyelled1310f82022-09-16 13:30:59 +02001015 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001016
Amaury Denoyelle418ba212022-08-02 15:57:16 +02001017 if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001018 qcc_decode_qcs(qcc, qcs);
Amaury Denoyelle418ba212022-08-02 15:57:16 +02001019 qcc_refresh_timeout(qcc);
1020 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001021
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001022 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001023 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001024 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001025
1026 err:
1027 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1028 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001029}
1030
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001031/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
1032 * the frame.
1033 *
1034 * Returns 0 on success else non-zero.
1035 */
1036int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
1037{
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001038 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1039
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001040 TRACE_PROTO("receiving MAX_DATA", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001041 if (qcc->rfctl.md < max) {
1042 qcc->rfctl.md = max;
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001043 TRACE_DEVEL("increase remote max-data", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001044
1045 if (qcc->flags & QC_CF_BLK_MFCTL) {
1046 qcc->flags &= ~QC_CF_BLK_MFCTL;
1047 tasklet_wakeup(qcc->wait_event.tasklet);
1048 }
1049 }
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001050
1051 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001052 return 0;
1053}
1054
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001055/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
1056 * field of the frame and <id> is the identifier of the QUIC stream.
1057 *
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001058 * Returns 0 on success else non-zero. On error, the received frame should not
1059 * be acknowledged.
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001060 */
1061int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
1062{
1063 struct qcs *qcs;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001064
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001065 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1066
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001067 /* RFC 9000 19.10. MAX_STREAM_DATA Frames
1068 *
1069 * Receiving a MAX_STREAM_DATA frame for a locally
1070 * initiated stream that has not yet been created MUST be treated as a
1071 * connection error of type STREAM_STATE_ERROR. An endpoint that
1072 * receives a MAX_STREAM_DATA frame for a receive-only stream MUST
1073 * terminate the connection with error STREAM_STATE_ERROR.
1074 */
1075 if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) {
1076 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1077 return 1;
1078 }
1079
1080 if (qcs) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001081 TRACE_PROTO("receiving MAX_STREAM_DATA", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001082 if (max > qcs->tx.msd) {
1083 qcs->tx.msd = max;
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001084 TRACE_DEVEL("increase remote max-stream-data", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001085
1086 if (qcs->flags & QC_SF_BLK_SFCTL) {
1087 qcs->flags &= ~QC_SF_BLK_SFCTL;
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001088 /* TODO optim: only wakeup IO-CB if stream has data to sent. */
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001089 tasklet_wakeup(qcc->wait_event.tasklet);
1090 }
1091 }
1092 }
1093
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001094 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1095 qcc_refresh_timeout(qcc);
1096
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001097 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1098 return 0;
1099}
1100
1101/* Handle a new RESET_STREAM frame from stream ID <id> with error code <err>
1102 * and final stream size <final_size>.
1103 *
1104 * Returns 0 on success else non-zero. On error, the received frame should not
1105 * be acknowledged.
1106 */
1107int qcc_recv_reset_stream(struct qcc *qcc, uint64_t id, uint64_t err, uint64_t final_size)
1108{
1109 struct qcs *qcs;
1110
1111 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1112
1113 /* RFC 9000 19.4. RESET_STREAM Frames
1114 *
1115 * An endpoint that receives a RESET_STREAM frame for a send-only stream
1116 * MUST terminate the connection with error STREAM_STATE_ERROR.
1117 */
1118 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
1119 TRACE_ERROR("RESET_STREAM for send-only stream received", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1120 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
1121 goto err;
1122 }
1123
1124 if (!qcs || qcs_is_close_remote(qcs))
1125 goto out;
1126
1127 TRACE_PROTO("receiving RESET_STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1128 qcs_idle_open(qcs);
1129
1130 if (qcs->rx.offset_max > final_size ||
1131 ((qcs->flags & QC_SF_SIZE_KNOWN) && qcs->rx.offset_max != final_size)) {
1132 TRACE_ERROR("final size error on RESET_STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1133 qcc_emit_cc(qcc, QC_ERR_FINAL_SIZE_ERROR);
1134 goto err;
1135 }
1136
1137 qcs->flags |= QC_SF_SIZE_KNOWN;
1138 qcs_close_remote(qcs);
1139 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
1140
1141 if (qcs_sc(qcs)) {
1142 se_fl_set_error(qcs->sd);
1143 qcs_alert(qcs);
1144 }
1145
1146 out:
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001147 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001148 return 0;
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001149
1150 err:
1151 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1152 return 1;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001153}
1154
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001155/* Handle a new STOP_SENDING frame for stream ID <id>. The error code should be
1156 * specified in <err>.
1157 *
1158 * Returns 0 on success else non-zero. On error, the received frame should not
1159 * be acknowledged.
1160 */
1161int qcc_recv_stop_sending(struct qcc *qcc, uint64_t id, uint64_t err)
1162{
1163 struct qcs *qcs;
1164
1165 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1166
1167 /* RFC 9000 19.5. STOP_SENDING Frames
1168 *
1169 * Receiving a STOP_SENDING frame for a
1170 * locally initiated stream that has not yet been created MUST be
1171 * treated as a connection error of type STREAM_STATE_ERROR. An
1172 * endpoint that receives a STOP_SENDING frame for a receive-only stream
1173 * MUST terminate the connection with error STREAM_STATE_ERROR.
1174 */
1175 if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) {
1176 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1177 return 1;
1178 }
1179
1180 if (!qcs)
1181 goto out;
1182
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001183 TRACE_PROTO("receiving STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelled7755372022-10-03 17:20:31 +02001184
1185 /* RFC 9000 3.5. Solicited State Transitions
1186 *
1187 * An endpoint is expected to send another STOP_SENDING frame if a
1188 * packet containing a previous STOP_SENDING is lost. However, once
1189 * either all stream data or a RESET_STREAM frame has been received for
1190 * the stream -- that is, the stream is in any state other than "Recv"
1191 * or "Size Known" -- sending a STOP_SENDING frame is unnecessary.
1192 */
1193
1194 /* TODO thanks to previous RFC clause, STOP_SENDING is ignored if current stream
1195 * has already been closed locally. This is useful to not emit multiple
1196 * RESET_STREAM for a single stream. This is functional if stream is
1197 * locally closed due to all data transmitted, but in this case the RFC
1198 * advices to use an explicit RESET_STREAM.
1199 */
1200 if (qcs_is_close_local(qcs)) {
1201 TRACE_STATE("ignoring STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1202 goto out;
1203 }
1204
Amaury Denoyelle96ca1b72022-08-09 17:36:38 +02001205 qcs_idle_open(qcs);
1206
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001207 /* RFC 9000 3.5. Solicited State Transitions
1208 *
1209 * An endpoint that receives a STOP_SENDING frame
1210 * MUST send a RESET_STREAM frame if the stream is in the "Ready" or
1211 * "Send" state. If the stream is in the "Data Sent" state, the
1212 * endpoint MAY defer sending the RESET_STREAM frame until the packets
1213 * containing outstanding data are acknowledged or declared lost. If
1214 * any outstanding data is declared lost, the endpoint SHOULD send a
1215 * RESET_STREAM frame instead of retransmitting the data.
1216 *
1217 * An endpoint SHOULD copy the error code from the STOP_SENDING frame to
1218 * the RESET_STREAM frame it sends, but it can use any application error
1219 * code.
1220 */
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001221 qcc_reset_stream(qcs, err);
1222
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001223 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1224 qcc_refresh_timeout(qcc);
1225
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001226 out:
1227 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1228 return 0;
1229}
1230
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001231/* Signal the closing of remote stream with id <id>. Flow-control for new
1232 * streams may be allocated for the peer if needed.
1233 */
1234static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +01001235{
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001236 struct quic_frame *frm;
1237
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001238 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn);
1239
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001240 if (quic_stream_is_bidi(id)) {
1241 ++qcc->lfctl.cl_bidi_r;
1242 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001243 TRACE_DATA("increase max stream limit with MAX_STREAMS_BIDI", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001244 frm = pool_zalloc(pool_head_quic_frame);
1245 BUG_ON(!frm); /* TODO handle this properly */
1246
1247 LIST_INIT(&frm->reflist);
1248 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
1249 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
1250 qcc->lfctl.cl_bidi_r;
1251 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
1252 tasklet_wakeup(qcc->wait_event.tasklet);
1253
1254 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
1255 qcc->lfctl.cl_bidi_r = 0;
1256 }
1257 }
1258 else {
Amaury Denoyelle91077312022-12-22 18:56:09 +01001259 /* TODO unidirectional stream flow control with MAX_STREAMS_UNI
1260 * emission not implemented. It should be unnecessary for
1261 * HTTP/3 but may be required if other application protocols
1262 * are supported.
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02001263 */
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001264 }
1265
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001266 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
1267
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001268 return 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001269}
1270
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05001271/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001272static void qcs_destroy(struct qcs *qcs)
1273{
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001274 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001275 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001276
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001277 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001278
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001279 if (quic_stream_is_remote(qcs->qcc, id))
1280 qcc_release_remote_stream(qcs->qcc, id);
Amaury Denoyellec055e302022-02-07 16:09:06 +01001281
Amaury Denoyelledccbd732022-03-29 18:36:59 +02001282 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001283
1284 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001285}
1286
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001287/* Transfer as much as possible data on <qcs> from <in> to <out>. This is done
1288 * in respect with available flow-control at stream and connection level.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001289 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001290 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001291 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001292static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, struct buffer *in)
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001293{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001294 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001295 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001296 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001297
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001298 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001299
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001300 qc_get_buf(qcs, out);
1301
1302 /*
1303 * QCS out buffer diagram
1304 * head left to_xfer
1305 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001306 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001307 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001308 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001309 * ^ ack-off ^ sent-off ^ off
1310 *
1311 * STREAM frame
1312 * ^ ^
1313 * |xxxxxxxxxxxxxxxxx|
1314 */
1315
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001316 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001317 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001318 BUG_ON_HOT(qcc->tx.offsets < qcc->tx.sent_offsets);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001319
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001320 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001321 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001322
1323 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
1324 /* do not exceed flow control limit */
1325 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
1326 to_xfer = qcs->tx.msd - qcs->tx.offset;
1327
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001328 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001329 /* do not overcome flow control limit on connection */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001330 if (qcc->tx.offsets + to_xfer > qcc->rfctl.md)
1331 to_xfer = qcc->rfctl.md - qcc->tx.offsets;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001332
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001333 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001334 goto out;
1335
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001336 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001337
1338 out:
1339 {
1340 struct qcs_xfer_data_trace_arg arg = {
1341 .prep = b_data(out), .xfer = total,
1342 };
1343 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
1344 qcc->conn, qcs, &arg);
1345 }
1346
1347 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001348}
1349
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001350/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
1351 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
1352 * stream frame.
1353 *
1354 * Returns the length of the STREAM frame or a negative error code.
1355 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001356static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
1357 struct list *frm_list)
1358{
1359 struct qcc *qcc = qcs->qcc;
1360 struct quic_frame *frm;
1361 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001362 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001363
1364 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1365
Amaury Denoyellea4569202022-04-15 17:29:25 +02001366 /* if ack_offset < buf_offset, it points to an older buffer. */
1367 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
1368 BUG_ON(qcs->tx.sent_offset < base_off);
1369
1370 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001371 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001372 BUG_ON(total < 0);
1373
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001374 if (!total && !fin) {
1375 /* No need to send anything if total is NULL and no FIN to signal. */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001376 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1377 return 0;
1378 }
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001379 BUG_ON((!total && qcs->tx.sent_offset > qcs->tx.offset) ||
1380 (total && qcs->tx.sent_offset >= qcs->tx.offset));
Amaury Denoyellea4569202022-04-15 17:29:25 +02001381 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001382 BUG_ON(qcc->tx.sent_offsets + total > qcc->rfctl.md);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001383
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001384 TRACE_PROTO("sending STREAM frame", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001385 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001386 if (!frm) {
1387 TRACE_ERROR("frame alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001388 goto err;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001389 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001390
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001391 LIST_INIT(&frm->reflist);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001392 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001393 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001394 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001395 frm->stream.buf = out;
1396 frm->stream.data = (unsigned char *)b_peek(out, head);
1397
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +01001398 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001399 if (fin)
1400 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001401
1402 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001403 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001404 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001405 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001406
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001407 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
1408 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001409
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001410 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001411
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001412 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001413 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001414 struct qcs_build_stream_trace_arg arg = {
1415 .len = frm->stream.len, .fin = fin,
1416 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001417 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001418 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001419 qcc->conn, qcs, &arg);
1420 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001421
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001422 return total;
1423
1424 err:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001425 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001426 return -1;
1427}
1428
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001429/* Check after transferring data from qcs.tx.buf if FIN must be set on the next
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001430 * STREAM frame for <qcs>.
1431 *
1432 * Returns true if FIN must be set else false.
1433 */
1434static int qcs_stream_fin(struct qcs *qcs)
1435{
1436 return qcs->flags & QC_SF_FIN_STREAM && !b_data(&qcs->tx.buf);
1437}
1438
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001439/* This function must be called by the upper layer to inform about the sending
1440 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
1441 * <offset>.
1442 */
1443void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
1444{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001445 struct qcc *qcc = qcs->qcc;
1446 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001447
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001448 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1449
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001450 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001451 BUG_ON(offset + data > qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001452
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001453 /* check if the STREAM frame has already been notified. It can happen
1454 * for retransmission.
1455 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001456 if (offset + data < qcs->tx.sent_offset) {
1457 TRACE_DEVEL("offset already notified", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1458 goto out;
1459 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001460
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001461 qcs_idle_open(qcs);
1462
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001463 diff = offset + data - qcs->tx.sent_offset;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001464 if (diff) {
1465 /* increase offset sum on connection */
1466 qcc->tx.sent_offsets += diff;
1467 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001468 if (qcc->tx.sent_offsets == qcc->rfctl.md) {
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001469 qcc->flags |= QC_CF_BLK_MFCTL;
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001470 TRACE_STATE("connection flow-control reached", QMUX_EV_QCS_SEND, qcc->conn);
1471 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001472
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001473 /* increase offset on stream */
1474 qcs->tx.sent_offset += diff;
1475 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
1476 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001477 if (qcs->tx.sent_offset == qcs->tx.msd) {
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001478 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001479 TRACE_STATE("stream flow-control reached", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1480 }
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001481
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001482 if (qcs->tx.offset == qcs->tx.sent_offset &&
1483 b_full(&qcs->stream->buf->buf)) {
1484 qc_stream_buf_release(qcs->stream);
1485 /* prepare qcs for immediate send retry if data to send */
1486 if (b_data(&qcs->tx.buf))
1487 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
1488 }
1489 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001490
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001491 if (qcs->tx.offset == qcs->tx.sent_offset && !b_data(&qcs->tx.buf)) {
1492 /* Remove stream from send_list if all was sent. */
1493 LIST_DEL_INIT(&qcs->el_send);
1494 TRACE_STATE("stream sent done", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1495
1496 if (qcs->flags & (QC_SF_FIN_STREAM|QC_SF_DETACH)) {
1497 /* Close stream locally. */
1498 qcs_close_local(qcs);
1499 /* Reset flag to not emit multiple FIN STREAM frames. */
1500 qcs->flags &= ~QC_SF_FIN_STREAM;
1501 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001502 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001503
1504 out:
1505 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001506}
1507
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001508/* Wrapper for send on transport layer. Send a list of frames <frms> for the
1509 * connection <qcc>.
1510 *
1511 * Returns 0 if all data sent with success else non-zero.
1512 */
1513static int qc_send_frames(struct qcc *qcc, struct list *frms)
1514{
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001515 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
1516
1517 if (LIST_ISEMPTY(frms)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001518 TRACE_DEVEL("no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
1519 goto err;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001520 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +01001521
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001522 LIST_INIT(&qcc->send_retry_list);
1523
Amaury Denoyelle036cc5d2022-09-26 15:02:31 +02001524 if (!qc_send_mux(qcc->conn->handle.qc, frms))
1525 goto err;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001526
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001527 /* If there is frames left at this stage, transport layer is blocked.
1528 * Subscribe on it to retry later.
1529 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001530 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001531 TRACE_DEVEL("remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001532 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1533 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001534 goto err;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001535 }
1536
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001537 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001538 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001539
1540 err:
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001541 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001542 return 1;
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001543}
1544
1545/* Emit a RESET_STREAM on <qcs>.
1546 *
1547 * Returns 0 if the frame has been successfully sent else non-zero.
1548 */
1549static int qcs_send_reset(struct qcs *qcs)
1550{
1551 struct list frms = LIST_HEAD_INIT(frms);
1552 struct quic_frame *frm;
1553
1554 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1555
1556 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001557 if (!frm) {
1558 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001559 return 1;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001560 }
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001561
1562 LIST_INIT(&frm->reflist);
1563 frm->type = QUIC_FT_RESET_STREAM;
1564 frm->reset_stream.id = qcs->id;
1565 frm->reset_stream.app_error_code = qcs->err;
1566 frm->reset_stream.final_size = qcs->tx.sent_offset;
1567
1568 LIST_APPEND(&frms, &frm->list);
1569 if (qc_send_frames(qcs->qcc, &frms)) {
1570 pool_free(pool_head_quic_frame, frm);
1571 TRACE_DEVEL("cannot send RESET_STREAM", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1572 return 1;
1573 }
1574
1575 if (qcs_sc(qcs)) {
1576 se_fl_set_error(qcs->sd);
1577 qcs_alert(qcs);
1578 }
1579
1580 qcs_close_local(qcs);
1581 qcs->flags &= ~QC_SF_TO_RESET;
1582
1583 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001584 return 0;
1585}
1586
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001587/* Emit a STOP_SENDING on <qcs>.
1588 *
1589 * Returns 0 if the frame has been successfully sent else non-zero.
1590 */
1591static int qcs_send_stop_sending(struct qcs *qcs)
1592{
1593 struct list frms = LIST_HEAD_INIT(frms);
1594 struct quic_frame *frm;
1595 struct qcc *qcc = qcs->qcc;
1596
1597 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1598
1599 /* RFC 9000 3.3. Permitted Frame Types
1600 *
1601 * A
1602 * receiver MAY send a STOP_SENDING frame in any state where it has not
1603 * received a RESET_STREAM frame -- that is, states other than "Reset
1604 * Recvd" or "Reset Read". However, there is little value in sending a
1605 * STOP_SENDING frame in the "Data Recvd" state, as all stream data has
1606 * been received. A sender could receive either of these two types of
1607 * frames in any state as a result of delayed delivery of packets.¶
1608 */
1609 if (qcs_is_close_remote(qcs)) {
1610 TRACE_STATE("skip STOP_SENDING on remote already closed", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1611 goto done;
1612 }
1613
1614 frm = pool_zalloc(pool_head_quic_frame);
1615 if (!frm) {
1616 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1617 return 1;
1618 }
1619
1620 LIST_INIT(&frm->reflist);
1621 frm->type = QUIC_FT_STOP_SENDING;
1622 frm->stop_sending.id = qcs->id;
1623 frm->stop_sending.app_error_code = qcs->err;
1624
1625 LIST_APPEND(&frms, &frm->list);
1626 if (qc_send_frames(qcs->qcc, &frms)) {
1627 pool_free(pool_head_quic_frame, frm);
1628 TRACE_DEVEL("cannot send STOP_SENDING", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1629 return 1;
1630 }
1631
1632 done:
1633 qcs->flags &= ~QC_SF_TO_STOP_SENDING;
1634
1635 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1636 return 0;
1637}
1638
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001639/* Used internally by qc_send function. Proceed to send for <qcs>. This will
1640 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001641 * is then generated and inserted in <frms> list.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001642 *
1643 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
1644 * be null if out buffer cannot be allocated.
1645 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001646static int _qc_send_qcs(struct qcs *qcs, struct list *frms)
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001647{
1648 struct qcc *qcc = qcs->qcc;
1649 struct buffer *buf = &qcs->tx.buf;
1650 struct buffer *out = qc_stream_buf_get(qcs->stream);
1651 int xfer = 0;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001652 char fin = 0;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001653
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001654 /* Cannot send STREAM on remote unidirectional streams. */
1655 BUG_ON(quic_stream_is_uni(qcs->id) && quic_stream_is_remote(qcc, qcs->id));
1656
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001657 /* Allocate <out> buffer if necessary. */
1658 if (!out) {
1659 if (qcc->flags & QC_CF_CONN_FULL)
1660 return 0;
1661
1662 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
1663 if (!out) {
1664 qcc->flags |= QC_CF_CONN_FULL;
1665 return 0;
1666 }
1667 }
1668
1669 /* Transfer data from <buf> to <out>. */
1670 if (b_data(buf)) {
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001671 xfer = qcs_xfer_data(qcs, out, buf);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001672 if (xfer > 0) {
1673 qcs_notify_send(qcs);
1674 qcs->flags &= ~QC_SF_BLK_MROOM;
1675 }
1676
1677 qcs->tx.offset += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001678 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001679 qcc->tx.offsets += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001680 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001681 }
1682
1683 /* out buffer cannot be emptied if qcs offsets differ. */
1684 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1685
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001686 /* FIN is set if all incoming data were transferred. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001687 fin = qcs_stream_fin(qcs);
1688
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001689 /* Build a new STREAM frame with <out> buffer. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001690 if (qcs->tx.sent_offset != qcs->tx.offset || fin) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001691 int ret;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001692 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +02001693 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001694 }
1695
1696 return xfer;
1697}
1698
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001699/* Proceed to sending. Loop through all available streams for the <qcc>
1700 * instance and try to send as much as possible.
1701 *
1702 * Returns the total of bytes sent to the transport layer.
1703 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001704static int qc_send(struct qcc *qcc)
1705{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001706 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001707 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001708 struct qcs *qcs, *qcs_tmp;
1709 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001710
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001711 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001712
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001713 if (qcc->conn->flags & CO_FL_SOCK_WR_SH || qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001714 qcc->conn->flags |= CO_FL_ERROR;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001715 TRACE_DEVEL("connection on error", QMUX_EV_QCC_SEND, qcc->conn);
1716 goto err;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001717 }
1718
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001719 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1720 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
1721 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
1722 goto out;
1723 }
1724 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02001725
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001726 if (qcc->flags & QC_CF_BLK_MFCTL)
1727 return 0;
1728
Frédéric Lécaille3dd79d32022-09-08 17:53:36 +02001729 if (!(qcc->flags & QC_CF_APP_FINAL) && !eb_is_empty(&qcc->streams_by_id) &&
1730 qcc->app_ops->finalize) {
1731 /* Finalize the application layer before sending any stream.
1732 * For h3 this consists in preparing the control stream data (SETTINGS h3).
1733 */
1734 qcc->app_ops->finalize(qcc->ctx);
1735 qcc->flags |= QC_CF_APP_FINAL;
1736 }
1737
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001738 /* Loop through all streams for STOP_SENDING/RESET_STREAM sending. Each
1739 * frame is send individually to guarantee emission.
1740 *
1741 * TODO Optimize sending by multiplexing several frames in one datagram.
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001742 */
1743 node = eb64_first(&qcc->streams_by_id);
1744 while (node) {
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001745 uint64_t id;
1746
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001747 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001748 id = qcs->id;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001749
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001750 if (quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001751 node = eb64_next(node);
1752 continue;
1753 }
1754
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001755 if (qcs->flags & QC_SF_TO_STOP_SENDING)
1756 qcs_send_stop_sending(qcs);
1757
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001758 if (qcs->flags & QC_SF_TO_RESET) {
1759 qcs_send_reset(qcs);
1760 node = eb64_next(node);
1761 continue;
1762 }
1763
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001764 node = eb64_next(node);
1765 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001766
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001767 /* Send STREAM data for registered streams. */
1768 list_for_each_entry(qcs, &qcc->send_list, el_send) {
1769 /* Stream must not be present in send_list if it has nothing to send. */
1770 BUG_ON(!b_data(&qcs->tx.buf) &&
1771 qcs->tx.sent_offset == qcs->tx.offset &&
1772 !qcs_stream_fin(qcs));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001773
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001774 if (qcs_is_close_local(qcs)) {
1775 LIST_DEL_INIT(&qcs->el_send);
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001776 continue;
1777 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001778
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001779 if (!(qcs->flags & QC_SF_BLK_SFCTL))
1780 total += _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001781 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001782
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001783 if (qc_send_frames(qcc, &frms)) {
1784 /* data rejected by transport layer, do not retry. */
1785 goto out;
1786 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001787
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001788 retry:
1789 tmp_total = 0;
1790 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
1791 int ret;
1792 BUG_ON(!b_data(&qcs->tx.buf));
1793 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001794
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001795 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001796 tmp_total += ret;
1797 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001798 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001799
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001800 total += tmp_total;
1801 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
1802 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +01001803
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001804 out:
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02001805 /* Deallocate frames that the transport layer has rejected. */
1806 if (!LIST_ISEMPTY(&frms)) {
1807 struct quic_frame *frm, *frm2;
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001808
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02001809 list_for_each_entry_safe(frm, frm2, &frms, list) {
1810 LIST_DELETE(&frm->list);
1811 pool_free(pool_head_quic_frame, frm);
1812 }
1813 }
1814
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001815 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001816 return total;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001817
1818 err:
1819 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
1820 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001821}
1822
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001823/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1824 * operation.
1825 *
1826 * Returns 0 on success else non-zero.
1827 */
1828static int qc_recv(struct qcc *qcc)
1829{
1830 struct eb64_node *node;
1831 struct qcs *qcs;
1832
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001833 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001834
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001835 if (qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001836 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001837 return 0;
1838 }
1839
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001840 node = eb64_first(&qcc->streams_by_id);
1841 while (node) {
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001842 uint64_t id;
1843
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001844 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001845 id = qcs->id;
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001846
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001847 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001848 node = eb64_next(node);
1849 continue;
1850 }
1851
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001852 if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001853 node = eb64_next(node);
1854 continue;
1855 }
1856
1857 qcc_decode_qcs(qcc, qcs);
1858 node = eb64_next(node);
1859 }
1860
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001861 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001862 return 0;
1863}
1864
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001865
1866/* Release all streams which have their transfer operation achieved.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001867 *
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001868 * Returns true if at least one stream is released.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001869 */
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001870static int qc_purge_streams(struct qcc *qcc)
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001871{
1872 struct eb64_node *node;
1873 int release = 0;
1874
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001875 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001876
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001877 node = eb64_first(&qcc->streams_by_id);
1878 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001879 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001880 node = eb64_next(node);
1881
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001882 /* Release not attached closed streams. */
1883 if (qcs->st == QC_SS_CLO && !qcs_sc(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001884 TRACE_STATE("purging closed stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001885 qcs_destroy(qcs);
1886 release = 1;
1887 continue;
1888 }
1889
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001890 /* Release detached streams with empty buffer. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001891 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02001892 if (qcs_is_close_local(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001893 TRACE_STATE("purging detached stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001894 qcs_destroy(qcs);
1895 release = 1;
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001896 continue;
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001897 }
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001898
1899 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1900 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001901 }
1902 }
1903
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001904 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001905 return release;
1906}
1907
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001908/* release function. This one should be called to free all resources allocated
1909 * to the mux.
1910 */
1911static void qc_release(struct qcc *qcc)
1912{
1913 struct connection *conn = qcc->conn;
1914 struct eb64_node *node;
1915
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001916 TRACE_ENTER(QMUX_EV_QCC_END, conn);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001917
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001918 if (qcc->app_ops && qcc->app_ops->shutdown) {
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001919 /* Application protocol with dedicated connection closing
1920 * procedure.
1921 */
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001922 qcc->app_ops->shutdown(qcc->ctx);
Amaury Denoyellea154dc02022-06-13 17:09:01 +02001923
1924 /* useful if application protocol should emit some closing
1925 * frames. For example HTTP/3 GOAWAY frame.
1926 */
1927 qc_send(qcc);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001928 }
1929 else {
1930 qcc_emit_cc_app(qcc, QC_ERR_NO_ERROR, 0);
1931 }
1932
1933 if (qcc->task) {
1934 task_destroy(qcc->task);
1935 qcc->task = NULL;
1936 }
1937
1938 if (qcc->wait_event.tasklet)
1939 tasklet_free(qcc->wait_event.tasklet);
1940 if (conn && qcc->wait_event.events) {
1941 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
1942 qcc->wait_event.events,
1943 &qcc->wait_event);
1944 }
1945
1946 /* liberate remaining qcs instances */
1947 node = eb64_first(&qcc->streams_by_id);
1948 while (node) {
1949 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
1950 node = eb64_next(node);
1951 qcs_free(qcs);
1952 }
1953
1954 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1955 struct quic_frame *frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list);
1956 LIST_DELETE(&frm->list);
1957 pool_free(pool_head_quic_frame, frm);
1958 }
1959
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001960 if (qcc->app_ops && qcc->app_ops->release)
1961 qcc->app_ops->release(qcc->ctx);
1962 TRACE_PROTO("application layer released", QMUX_EV_QCC_END, conn);
1963
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001964 pool_free(pool_head_qcc, qcc);
1965
1966 if (conn) {
1967 LIST_DEL_INIT(&conn->stopping_list);
1968
1969 conn->handle.qc->conn = NULL;
1970 conn->mux = NULL;
1971 conn->ctx = NULL;
1972
1973 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
1974
1975 conn_stop_tracking(conn);
1976 conn_full_close(conn);
1977 if (conn->destroy_cb)
1978 conn->destroy_cb(conn);
1979 conn_free(conn);
1980 }
1981
1982 TRACE_LEAVE(QMUX_EV_QCC_END);
1983}
1984
Willy Tarreau41e701e2022-09-08 15:12:59 +02001985struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001986{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001987 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001988
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001989 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001990
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001991 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001992
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001993 if (qc_purge_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001994 if (qcc_is_dead(qcc)) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001995 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001996 goto release;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001997 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001998 }
1999
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002000 qc_recv(qcc);
2001
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002002 /* TODO check if qcc proxy is disabled. If yes, use graceful shutdown
2003 * to close the connection.
2004 */
2005
2006 qcc_refresh_timeout(qcc);
2007
Amaury Denoyelled3973852022-07-25 14:56:54 +02002008 end:
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002009 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
2010 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002011
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002012 release:
2013 qc_release(qcc);
2014 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002015 return NULL;
2016}
2017
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002018static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
2019{
2020 struct qcc *qcc = ctx;
2021 int expired = tick_is_expired(t->expire, now_ms);
2022
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002023 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002024
2025 if (qcc) {
2026 if (!expired) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002027 TRACE_DEVEL("not expired", QMUX_EV_QCC_WAKE, qcc->conn);
2028 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002029 }
2030
2031 if (!qcc_may_expire(qcc)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002032 TRACE_DEVEL("cannot expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002033 t->expire = TICK_ETERNITY;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002034 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002035 }
2036 }
2037
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002038 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01002039
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002040 if (!qcc) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002041 TRACE_DEVEL("no more qcc", QMUX_EV_QCC_WAKE);
2042 goto out;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002043 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01002044
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002045 qcc->task = NULL;
2046
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002047 /* TODO depending on the timeout condition, different shutdown mode
2048 * should be used. For http keep-alive or disabled proxy, a graceful
2049 * shutdown should occurs. For all other cases, an immediate close
2050 * seems legitimate.
2051 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002052 if (qcc_is_dead(qcc)) {
2053 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002054 qc_release(qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002055 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002056
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002057 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002058 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002059 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002060
2061 requeue:
2062 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
2063 return t;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002064}
2065
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002066static int qc_init(struct connection *conn, struct proxy *prx,
2067 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002068{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002069 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002070 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002071
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002072 TRACE_ENTER(QMUX_EV_QCC_NEW);
2073
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002074 qcc = pool_alloc(pool_head_qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002075 if (!qcc) {
2076 TRACE_ERROR("alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002077 goto fail_no_qcc;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002078 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002079
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002080 qcc->conn = conn;
2081 conn->ctx = qcc;
Amaury Denoyellec603de42022-07-25 11:21:46 +02002082 qcc->nb_hreq = qcc->nb_sc = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01002083 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002084
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002085 qcc->app_ops = NULL;
Amaury Denoyelle0aba11e2022-09-29 18:31:24 +02002086 if (qcc_install_app_ops(qcc, conn->handle.qc->app_ops)) {
2087 TRACE_PROTO("Cannot install app layer", QMUX_EV_QCC_NEW, qcc->conn);
2088 /* prepare a CONNECTION_CLOSE frame */
2089 quic_set_connection_close(conn->handle.qc, quic_err_transport(QC_ERR_APPLICATION_ERROR));
2090 goto fail_no_tasklet;
2091 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002092
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002093 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002094
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002095 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02002096 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002097
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002098 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02002099 qcc->tx.sent_offsets = qcc->tx.offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002100
2101 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002102 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002103 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002104 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002105 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002106
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002107 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002108 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002109 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002110 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002111
2112 /* Server initiated streams must respect the server flow control. */
2113 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002114 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002115 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002116 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
2117
2118 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002119 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002120 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002121 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002122
Amaury Denoyellec985cb12022-05-16 14:29:59 +02002123 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01002124 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02002125 qcc->lfctl.ms_uni = lparams->initial_max_streams_uni;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02002126 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
2127 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle176174f2022-10-21 17:02:18 +02002128 qcc->lfctl.msd_uni_r = lparams->initial_max_stream_data_uni;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01002129 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01002130
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02002131 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02002132 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02002133
Willy Tarreau784b8682022-04-11 14:18:10 +02002134 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01002135 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002136 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
2137 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle176174f2022-10-21 17:02:18 +02002138 qcc->rfctl.msd_uni_l = rparams->initial_max_stream_data_uni;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002139
Amaury Denoyellea509ffb2022-07-04 15:50:33 +02002140 if (conn_is_back(conn)) {
2141 qcc->next_bidi_l = 0x00;
2142 qcc->largest_bidi_r = 0x01;
2143 qcc->next_uni_l = 0x02;
2144 qcc->largest_uni_r = 0x03;
2145 }
2146 else {
2147 qcc->largest_bidi_r = 0x00;
2148 qcc->next_bidi_l = 0x01;
2149 qcc->largest_uni_r = 0x02;
2150 qcc->next_uni_l = 0x03;
2151 }
2152
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002153 qcc->wait_event.tasklet = tasklet_new();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002154 if (!qcc->wait_event.tasklet) {
2155 TRACE_ERROR("taslket alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002156 goto fail_no_tasklet;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002157 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002158
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01002159 LIST_INIT(&qcc->send_list);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02002160 LIST_INIT(&qcc->send_retry_list);
2161
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002162 qcc->wait_event.tasklet->process = qc_io_cb;
2163 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01002164 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002165
Amaury Denoyelle07bf8f42022-07-22 16:16:03 +02002166 qcc->proxy = prx;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002167 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002168 qcc->task = NULL;
Amaury Denoyelleb6309452022-07-25 14:51:56 +02002169 qcc->timeout = conn_is_back(qcc->conn) ? prx->timeout.server :
2170 prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002171 if (tick_isset(qcc->timeout)) {
2172 qcc->task = task_new_here();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002173 if (!qcc->task) {
2174 TRACE_ERROR("timeout task alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002175 goto fail_no_timeout_task;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002176 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002177 qcc->task->process = qc_timeout_task;
2178 qcc->task->context = qcc;
2179 qcc->task->expire = tick_add(now_ms, qcc->timeout);
2180 }
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +02002181 qcc_reset_idle_start(qcc);
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02002182 LIST_INIT(&qcc->opening_list);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002183
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002184 if (!conn_is_back(conn)) {
2185 if (!LIST_INLIST(&conn->stopping_list)) {
2186 LIST_APPEND(&mux_stopping_data[tid].list,
2187 &conn->stopping_list);
2188 }
2189 }
2190
Willy Tarreau784b8682022-04-11 14:18:10 +02002191 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002192 /* init read cycle */
2193 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002194
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002195 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002196 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002197
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002198 fail_no_timeout_task:
2199 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002200 fail_no_tasklet:
Amaury Denoyelle0aba11e2022-09-29 18:31:24 +02002201 if (qcc->app_ops && qcc->app_ops->release)
2202 qcc->app_ops->release(qcc->ctx);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002203 pool_free(pool_head_qcc, qcc);
2204 fail_no_qcc:
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002205 TRACE_LEAVE(QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002206 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002207}
2208
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002209static void qc_destroy(void *ctx)
2210{
2211 struct qcc *qcc = ctx;
2212
2213 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
2214 qc_release(qcc);
2215 TRACE_LEAVE(QMUX_EV_QCC_END);
2216}
2217
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002218static void qc_detach(struct sedesc *sd)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002219{
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002220 struct qcs *qcs = sd->se;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002221 struct qcc *qcc = qcs->qcc;
2222
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002223 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002224
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002225 /* TODO this BUG_ON_HOT() is not correct as the stconn layer may detach
2226 * from the stream even if it is not closed remotely at the QUIC layer.
2227 * This happens for example when a stream must be closed due to a
2228 * rejected request. To better handle these cases, it will be required
2229 * to implement shutr/shutw MUX operations. Once this is done, this
2230 * BUG_ON_HOT() statement can be adjusted.
2231 */
2232 //BUG_ON_HOT(!qcs_is_close_remote(qcs));
Amaury Denoyellec603de42022-07-25 11:21:46 +02002233
2234 qcc_rm_sc(qcc);
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002235
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02002236 if (!qcs_is_close_local(qcs) && !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002237 TRACE_STATE("remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002238 qcs->flags |= QC_SF_DETACH;
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002239 qcc_refresh_timeout(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002240
2241 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002242 return;
2243 }
2244
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002245 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01002246
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002247 if (qcc_is_dead(qcc)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002248 TRACE_STATE("killing dead connection", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002249 goto release;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002250 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002251 else if (qcc->task) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002252 TRACE_DEVEL("refreshing connection's timeout", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002253 qcc_refresh_timeout(qcc);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002254 }
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002255 else {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002256 TRACE_DEVEL("completed", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002257 }
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002258
2259 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002260 return;
2261
2262 release:
2263 qc_release(qcc);
2264 TRACE_LEAVE(QMUX_EV_STRM_END);
2265 return;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002266}
2267
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002268/* Called from the upper layer, to receive data */
Amaury Denoyelleb7ce79c2022-11-24 10:51:19 +01002269static size_t qc_recv_buf(struct stconn *sc, struct buffer *buf,
2270 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002271{
Willy Tarreau3215e732022-05-27 10:09:11 +02002272 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002273 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002274 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002275
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002276 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002277
Amaury Denoyelled80fbca2022-09-19 17:02:28 +02002278 ret = qcs_http_rcv_buf(qcs, buf, count, &fin);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002279
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002280 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002281 se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002282 }
2283 else {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002284 se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
2285 if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING))
2286 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002287
Amaury Denoyelle72a78e82022-07-29 15:34:12 +02002288 /* Set end-of-input if FIN received and all data extracted. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002289 if (fin)
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002290 se_fl_set(qcs->sd, SE_FL_EOI);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002291
2292 if (b_size(&qcs->rx.app_buf)) {
2293 b_free(&qcs->rx.app_buf);
2294 offer_buffers(NULL, 1);
2295 }
2296 }
2297
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002298 if (ret) {
2299 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002300 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002301 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002302
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002303 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
2304
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002305 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002306}
2307
Amaury Denoyelleb7ce79c2022-11-24 10:51:19 +01002308static size_t qc_send_buf(struct stconn *sc, struct buffer *buf,
2309 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002310{
Willy Tarreau3215e732022-05-27 10:09:11 +02002311 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002312 size_t ret;
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002313 char fin;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002314
2315 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002316
Amaury Denoyelle3dc4e5a2022-09-13 16:49:21 +02002317 /* stream layer has been detached so no transfer must occur after. */
2318 BUG_ON_HOT(qcs->flags & QC_SF_DETACH);
2319
Amaury Denoyelle843a1192022-07-04 11:44:38 +02002320 if (qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_RESET)) {
Amaury Denoyelle0ed617a2022-09-20 14:46:40 +02002321 ret = qcs_http_reset_buf(qcs, buf, count);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002322 goto end;
2323 }
2324
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002325 ret = qcs_http_snd_buf(qcs, buf, count, &fin);
2326 if (fin)
2327 qcs->flags |= QC_SF_FIN_STREAM;
2328
Amaury Denoyelleab6cdec2023-01-10 10:41:41 +01002329 if (ret || fin) {
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01002330 qcc_send_stream(qcs);
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002331 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
2332 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
2333 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002334
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002335 end:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002336 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
2337
2338 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002339}
2340
2341/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2342 * event subscriber <es> is not allowed to change from a previous call as long
2343 * as at least one event is still subscribed. The <event_type> must only be a
2344 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
2345 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002346static int qc_subscribe(struct stconn *sc, int event_type,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002347 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002348{
Willy Tarreau3215e732022-05-27 10:09:11 +02002349 return qcs_subscribe(__sc_mux_strm(sc), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002350}
2351
2352/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
2353 * The <es> pointer is not allowed to differ from the one passed to the
2354 * subscribe() call. It always returns zero.
2355 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002356static int qc_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002357{
Willy Tarreau3215e732022-05-27 10:09:11 +02002358 struct qcs *qcs = __sc_mux_strm(sc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002359
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002360 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
2361 BUG_ON(qcs->subs && qcs->subs != es);
2362
2363 es->events &= ~event_type;
2364 if (!es->events)
2365 qcs->subs = NULL;
2366
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002367 return 0;
2368}
2369
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002370/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Willy Tarreau4596fe22022-05-17 19:07:51 +02002371 * report SE_FL_ERR_PENDING|SE_FL_ERROR on the attached stream connectors and
2372 * wake them.
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002373 */
2374static int qc_wake_some_streams(struct qcc *qcc)
2375{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002376 struct qcs *qcs;
2377 struct eb64_node *node;
2378
2379 for (node = eb64_first(&qcc->streams_by_id); node;
2380 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02002381 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002382
Amaury Denoyelle3abeb572022-07-04 11:42:27 +02002383 if (!qcs_sc(qcs))
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002384 continue;
2385
2386 if (qcc->conn->flags & CO_FL_ERROR) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002387 se_fl_set(qcs->sd, SE_FL_ERR_PENDING);
2388 if (se_fl_test(qcs->sd, SE_FL_EOS))
2389 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002390
Amaury Denoyelle4561f842022-07-06 14:54:34 +02002391 qcs_alert(qcs);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002392 }
2393 }
2394
2395 return 0;
2396}
2397
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002398static int qc_wake(struct connection *conn)
2399{
2400 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02002401 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002402
2403 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002404
2405 /* Check if a soft-stop is in progress.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002406 *
cui flitera94bedc2022-08-29 14:42:57 +08002407 * TODO this is relevant for frontend connections only.
Amaury Denoyelled0c62322022-05-23 08:52:58 +02002408 *
2409 * TODO Client should be notified with a H3 GOAWAY and then a
2410 * CONNECTION_CLOSE. However, quic-conn uses the listener socket for
2411 * sending which at this stage is already closed.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002412 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002413 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Amaury Denoyelled0c62322022-05-23 08:52:58 +02002414 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002415
Willy Tarreau784b8682022-04-11 14:18:10 +02002416 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002417 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
2418
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002419 qc_send(qcc);
2420
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002421 qc_wake_some_streams(qcc);
2422
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002423 if (qcc_is_dead(qcc))
2424 goto release;
2425
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002426 qcc_refresh_timeout(qcc);
2427
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002428 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002429 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002430
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002431 release:
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002432 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002433 qc_release(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002434 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002435 return 1;
2436}
2437
Amaury Denoyellea473f192022-12-21 10:21:58 +01002438static void qc_shutw(struct stconn *sc, enum co_shw_mode mode)
2439{
2440 struct qcs *qcs = __sc_mux_strm(sc);
2441
2442 TRACE_ENTER(QMUX_EV_STRM_SHUT, qcs->qcc->conn, qcs);
2443
2444 /* If QC_SF_FIN_STREAM is not set and stream is not closed locally, it
2445 * means that upper layer reported an early closure. A RESET_STREAM is
2446 * necessary if not already scheduled.
2447 */
2448
2449 if (!qcs_is_close_local(qcs) &&
2450 !(qcs->flags & (QC_SF_FIN_STREAM|QC_SF_TO_RESET))) {
2451 qcc_reset_stream(qcs, 0);
2452 se_fl_set_error(qcs->sd);
2453 }
2454
2455 TRACE_LEAVE(QMUX_EV_STRM_SHUT, qcs->qcc->conn, qcs);
2456}
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002457
Willy Tarreaub4a4fee2022-09-02 16:00:40 +02002458/* for debugging with CLI's "show sess" command. May emit multiple lines, each
2459 * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single
2460 * line is used. Each field starts with a space so it's safe to print it after
2461 * existing fields.
2462 */
2463static int qc_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx)
2464{
2465 struct qcs *qcs = sd->se;
2466 struct qcc *qcc;
2467 int ret = 0;
2468
2469 if (!qcs)
2470 return ret;
2471
2472 chunk_appendf(msg, " qcs=%p .flg=%#x .id=%llu .st=%s .ctx=%p, .err=%#llx",
2473 qcs, qcs->flags, (ull)qcs->id, qcs_st_to_str(qcs->st), qcs->ctx, (ull)qcs->err);
2474
2475 if (pfx)
2476 chunk_appendf(msg, "\n%s", pfx);
2477
2478 qcc = qcs->qcc;
2479 chunk_appendf(msg, " qcc=%p .flg=%#x .nbsc=%llu .nbhreq=%llu, .task=%p",
2480 qcc, qcc->flags, (ull)qcc->nb_sc, (ull)qcc->nb_hreq, qcc->task);
2481 return ret;
2482}
2483
2484
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002485static const struct mux_ops qc_ops = {
2486 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002487 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002488 .detach = qc_detach,
Amaury Denoyelleb7ce79c2022-11-24 10:51:19 +01002489 .rcv_buf = qc_recv_buf,
2490 .snd_buf = qc_send_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002491 .subscribe = qc_subscribe,
2492 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002493 .wake = qc_wake,
Amaury Denoyellea473f192022-12-21 10:21:58 +01002494 .shutw = qc_shutw,
Willy Tarreaub4a4fee2022-09-02 16:00:40 +02002495 .show_sd = qc_show_sd,
Willy Tarreaub5821e12022-04-26 11:54:08 +02002496 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02002497 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002498};
2499
2500static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002501 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002502
2503INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);