blob: 26006fb66789a7e605de33fbeff53bb4426530aa [file] [log] [blame]
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001#include <haproxy/mux_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002
Amaury Denoyelleeb01f592021-10-07 16:44:05 +02003#include <import/eb64tree.h>
4
Frédéric Lécailledfbae762021-02-18 09:59:01 +01005#include <haproxy/api.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01006#include <haproxy/connection.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +01007#include <haproxy/dynbuf.h>
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01008#include <haproxy/h3.h>
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02009#include <haproxy/list.h>
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +020010#include <haproxy/ncbuf.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +010011#include <haproxy/pool.h>
Frédéric Lécaille9969adb2023-01-18 11:52:21 +010012#include <haproxy/proxy.h>
Amaury Denoyelled80fbca2022-09-19 17:02:28 +020013#include <haproxy/qmux_http.h>
Amaury Denoyelle36d50bf2022-09-19 16:12:38 +020014#include <haproxy/qmux_trace.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020015#include <haproxy/quic_conn.h>
Amaury Denoyelle40c24f12023-01-27 17:47:49 +010016#include <haproxy/quic_frame.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020017#include <haproxy/quic_stream.h>
Frédéric Lécaille748ece62022-05-21 23:58:40 +020018#include <haproxy/quic_tp-t.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020019#include <haproxy/ssl_sock-t.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020020#include <haproxy/stconn.h>
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010021#include <haproxy/trace.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010022
Amaury Denoyelledeed7772021-12-03 11:36:46 +010023DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010024DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
25
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +020026/* Emit a CONNECTION_CLOSE with error <err>. This will interrupt all future
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +020027 * send/receive operations.
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +020028 */
29static void qcc_emit_cc(struct qcc *qcc, int err)
30{
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +020031 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
32
33 TRACE_STATE("set CONNECTION_CLOSE on quic-conn", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle57e6db72022-07-13 15:07:56 +020034 quic_set_connection_close(qcc->conn->handle.qc, quic_err_transport(err));
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +020035 qcc->flags |= QC_CF_CC_EMIT;
36 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +020037
38 TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +020039}
40
Amaury Denoyelle4b167002022-12-12 09:59:50 +010041static void qc_free_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
42{
43 struct buffer buf;
44
45 if (ncb_is_null(ncbuf))
46 return;
47
48 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
49 b_free(&buf);
50 offer_buffers(NULL, 1);
51
52 *ncbuf = NCBUF_NULL;
53}
54
55/* Free <qcs> instance. This function is reserved for internal usage : it must
56 * only be called on qcs alloc error or on connection shutdown. Else
57 * qcs_destroy must be prefered to handle QUIC flow-control increase.
58 */
59static void qcs_free(struct qcs *qcs)
60{
61 struct qcc *qcc = qcs->qcc;
62
63 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn, qcs);
64
Amaury Denoyelle15337fd2022-12-20 14:47:16 +010065 /* Safe to use even if already removed from the list. */
66 LIST_DEL_INIT(&qcs->el_opening);
Amaury Denoyelle20f2a422023-01-03 14:39:24 +010067 LIST_DEL_INIT(&qcs->el_send);
Amaury Denoyelle4b167002022-12-12 09:59:50 +010068
69 /* Release stream endpoint descriptor. */
70 BUG_ON(qcs->sd && !se_fl_test(qcs->sd, SE_FL_ORPHAN));
71 sedesc_free(qcs->sd);
72
73 /* Release app-layer context. */
74 if (qcs->ctx && qcc->app_ops->detach)
75 qcc->app_ops->detach(qcs);
76
77 /* Release qc_stream_desc buffer from quic-conn layer. */
78 qc_stream_desc_release(qcs->stream);
79
80 /* Free Rx/Tx buffers. */
81 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
82 b_free(&qcs->tx.buf);
83
84 BUG_ON(!qcc->strms[qcs_id_type(qcs->id)].nb_streams);
85 --qcc->strms[qcs_id_type(qcs->id)].nb_streams;
86
87 /* Remove qcs from qcc tree. */
88 eb64_delete(&qcs->by_id);
89
90 pool_free(pool_head_qcs, qcs);
91
92 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
93}
94
Amaury Denoyelledeed7772021-12-03 11:36:46 +010095/* Allocate a new QUIC streams with id <id> and type <type>. */
Amaury Denoyellea509ffb2022-07-04 15:50:33 +020096static struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +010097{
Amaury Denoyelledeed7772021-12-03 11:36:46 +010098 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010099
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100100 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
101
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100102 qcs = pool_alloc(pool_head_qcs);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200103 if (!qcs) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200104 TRACE_ERROR("alloc failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200105 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200106 }
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200107
108 qcs->stream = NULL;
109 qcs->qcc = qcc;
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +0200110 qcs->sd = NULL;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200111 qcs->flags = QC_SF_NONE;
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200112 qcs->st = QC_SS_IDLE;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200113 qcs->ctx = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100114
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200115 /* App callback attach may register the stream for http-request wait.
116 * These fields must be initialed before.
117 */
118 LIST_INIT(&qcs->el_opening);
Amaury Denoyelle20f2a422023-01-03 14:39:24 +0100119 LIST_INIT(&qcs->el_send);
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200120 qcs->start = TICK_ETERNITY;
121
Amaury Denoyelle4b167002022-12-12 09:59:50 +0100122 /* store transport layer stream descriptor in qcc tree */
123 qcs->id = qcs->by_id.key = id;
124 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
125
126 qcc->strms[type].nb_streams++;
127
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200128 /* Allocate transport layer stream descriptor. Only needed for TX. */
129 if (!quic_stream_is_uni(id) || !quic_stream_is_remote(qcc, id)) {
130 struct quic_conn *qc = qcc->conn->handle.qc;
131 qcs->stream = qc_stream_desc_new(id, type, qcs, qc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200132 if (!qcs->stream) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200133 TRACE_ERROR("qc_stream_desc alloc failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200134 goto err;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200135 }
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200136 }
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200137
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200138 if (qcc->app_ops->attach) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200139 if (qcc->app_ops->attach(qcs, qcc->ctx)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200140 TRACE_ERROR("app proto failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200141 goto err;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200142 }
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200143 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100144
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100145 /* If stream is local, use peer remote-limit, or else the opposite. */
Amaury Denoyelle176174f2022-10-21 17:02:18 +0200146 if (quic_stream_is_bidi(id)) {
147 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
148 qcc->rfctl.msd_bidi_l;
149 }
150 else if (quic_stream_is_local(qcc, id)) {
151 qcs->tx.msd = qcc->rfctl.msd_uni_l;
152 }
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100153
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200154 qcs->rx.ncbuf = NCBUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100155 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200156 qcs->rx.offset = qcs->rx.offset_max = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100157
Amaury Denoyelle176174f2022-10-21 17:02:18 +0200158 if (quic_stream_is_bidi(id)) {
159 qcs->rx.msd = quic_stream_is_local(qcc, id) ? qcc->lfctl.msd_bidi_l :
160 qcc->lfctl.msd_bidi_r;
161 }
162 else if (quic_stream_is_remote(qcc, id)) {
163 qcs->rx.msd = qcc->lfctl.msd_uni_r;
164 }
Amaury Denoyellea9773552022-05-16 14:38:25 +0200165 qcs->rx.msd_init = qcs->rx.msd;
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200166
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100167 qcs->tx.buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100168 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100169 qcs->tx.sent_offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100170
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100171 qcs->wait_event.tasklet = NULL;
172 qcs->wait_event.events = 0;
173 qcs->subs = NULL;
174
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200175 qcs->err = 0;
176
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100177 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100178 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100179 return qcs;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200180
181 err:
Amaury Denoyelle4b167002022-12-12 09:59:50 +0100182 qcs_free(qcs);
183 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200184 return NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100185}
186
Amaury Denoyelle3abeb572022-07-04 11:42:27 +0200187static forceinline struct stconn *qcs_sc(const struct qcs *qcs)
188{
189 return qcs->sd ? qcs->sd->sc : NULL;
190}
191
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200192/* Reset the <qcc> inactivity timeout for http-keep-alive timeout. */
193static forceinline void qcc_reset_idle_start(struct qcc *qcc)
194{
195 qcc->idle_start = now_ms;
196}
197
Amaury Denoyellec603de42022-07-25 11:21:46 +0200198/* Decrement <qcc> sc. */
199static forceinline void qcc_rm_sc(struct qcc *qcc)
200{
201 BUG_ON_HOT(!qcc->nb_sc);
202 --qcc->nb_sc;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200203
204 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
205 * refreshed after this on stream detach.
206 */
207 if (!qcc->nb_sc && !qcc->nb_hreq)
208 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200209}
210
211/* Decrement <qcc> hreq. */
212static forceinline void qcc_rm_hreq(struct qcc *qcc)
213{
214 BUG_ON_HOT(!qcc->nb_hreq);
215 --qcc->nb_hreq;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200216
217 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
218 * refreshed after this on I/O handler.
219 */
220 if (!qcc->nb_sc && !qcc->nb_hreq)
221 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200222}
223
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200224static inline int qcc_is_dead(const struct qcc *qcc)
225{
226 /* Mux connection is considered dead if :
227 * - all stream-desc are detached AND
228 * = connection is on error OR
229 * = mux timeout has already fired or is unset
230 */
231 if (!qcc->nb_sc && ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task))
232 return 1;
233
234 return 0;
235}
236
237/* Return true if the mux timeout should be armed. */
238static inline int qcc_may_expire(struct qcc *qcc)
239{
240 return !qcc->nb_sc;
241}
242
243/* Refresh the timeout on <qcc> if needed depending on its state. */
244static void qcc_refresh_timeout(struct qcc *qcc)
245{
246 const struct proxy *px = qcc->proxy;
247
248 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
249
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200250 if (!qcc->task) {
251 TRACE_DEVEL("already expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200252 goto leave;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200253 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200254
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200255 /* Check if upper layer is responsible of timeout management. */
256 if (!qcc_may_expire(qcc)) {
257 TRACE_DEVEL("not eligible for timeout", QMUX_EV_QCC_WAKE, qcc->conn);
258 qcc->task->expire = TICK_ETERNITY;
259 task_queue(qcc->task);
260 goto leave;
261 }
262
263 /* TODO if connection is idle on frontend and proxy is disabled, remove
264 * it with global close_spread delay applied.
265 */
266
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200267 /* TODO implement client/server-fin timeout for graceful shutdown */
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200268
269 /* Frontend timeout management
270 * - detached streams with data left to send -> default timeout
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200271 * - stream waiting on incomplete request or no stream yet activated -> timeout http-request
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200272 * - idle after stream processing -> timeout http-keep-alive
273 */
274 if (!conn_is_back(qcc->conn)) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200275 if (qcc->nb_hreq) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200276 TRACE_DEVEL("one or more requests still in progress", QMUX_EV_QCC_WAKE, qcc->conn);
277 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200278 task_queue(qcc->task);
279 goto leave;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200280 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200281
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200282 if (!LIST_ISEMPTY(&qcc->opening_list) || unlikely(!qcc->largest_bidi_r)) {
283 int timeout = px->timeout.httpreq;
284 struct qcs *qcs = NULL;
285 int base_time;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200286
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200287 /* Use start time of first stream waiting on HTTP or
288 * qcc idle if no stream not yet used.
289 */
290 if (likely(!LIST_ISEMPTY(&qcc->opening_list)))
291 qcs = LIST_ELEM(qcc->opening_list.n, struct qcs *, el_opening);
292 base_time = qcs ? qcs->start : qcc->idle_start;
293
294 TRACE_DEVEL("waiting on http request", QMUX_EV_QCC_WAKE, qcc->conn, qcs);
295 qcc->task->expire = tick_add_ifset(base_time, timeout);
296 }
297 else {
298 /* Use http-request timeout if keep-alive timeout not set */
299 int timeout = tick_isset(px->timeout.httpka) ?
300 px->timeout.httpka : px->timeout.httpreq;
301
302 TRACE_DEVEL("at least one request achieved but none currently in progress", QMUX_EV_QCC_WAKE, qcc->conn);
303 qcc->task->expire = tick_add_ifset(qcc->idle_start, timeout);
304 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200305 }
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200306
307 /* fallback to default timeout if frontend specific undefined or for
308 * backend connections.
309 */
310 if (!tick_isset(qcc->task->expire)) {
311 TRACE_DEVEL("fallback to default timeout", QMUX_EV_QCC_WAKE, qcc->conn);
312 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200313 }
314
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200315 task_queue(qcc->task);
316
317 leave:
318 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
319}
320
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200321/* Mark a stream as open if it was idle. This can be used on every
322 * successful emission/reception operation to update the stream state.
323 */
324static void qcs_idle_open(struct qcs *qcs)
325{
326 /* This operation must not be used if the stream is already closed. */
327 BUG_ON_HOT(qcs->st == QC_SS_CLO);
328
329 if (qcs->st == QC_SS_IDLE) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200330 TRACE_STATE("opening stream", QMUX_EV_QCS_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200331 qcs->st = QC_SS_OPEN;
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200332 }
333}
334
335/* Close the local channel of <qcs> instance. */
336static void qcs_close_local(struct qcs *qcs)
337{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200338 TRACE_STATE("closing stream locally", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
339
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200340 /* The stream must have already been opened. */
341 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
342
343 /* This operation cannot be used multiple times. */
344 BUG_ON_HOT(qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO);
345
346 if (quic_stream_is_bidi(qcs->id)) {
347 qcs->st = (qcs->st == QC_SS_HREM) ? QC_SS_CLO : QC_SS_HLOC;
Amaury Denoyelleafb7b9d2022-09-19 11:58:24 +0200348
349 if (qcs->flags & QC_SF_HREQ_RECV)
350 qcc_rm_hreq(qcs->qcc);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200351 }
352 else {
353 /* Only local uni streams are valid for this operation. */
354 BUG_ON_HOT(quic_stream_is_remote(qcs->qcc, qcs->id));
355 qcs->st = QC_SS_CLO;
356 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200357}
358
359/* Close the remote channel of <qcs> instance. */
360static void qcs_close_remote(struct qcs *qcs)
361{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200362 TRACE_STATE("closing stream remotely", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
363
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200364 /* The stream must have already been opened. */
365 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
366
367 /* This operation cannot be used multiple times. */
368 BUG_ON_HOT(qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO);
369
370 if (quic_stream_is_bidi(qcs->id)) {
371 qcs->st = (qcs->st == QC_SS_HLOC) ? QC_SS_CLO : QC_SS_HREM;
372 }
373 else {
374 /* Only remote uni streams are valid for this operation. */
375 BUG_ON_HOT(quic_stream_is_local(qcs->qcc, qcs->id));
376 qcs->st = QC_SS_CLO;
377 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200378}
379
380static int qcs_is_close_local(struct qcs *qcs)
381{
382 return qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO;
383}
384
Amaury Denoyelle6eb3c4b2022-12-09 16:26:03 +0100385static int qcs_is_close_remote(struct qcs *qcs)
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200386{
387 return qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO;
388}
389
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100390struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100391{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100392 struct buffer *buf = b_alloc(bptr);
393 BUG_ON(!buf);
394 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100395}
396
Amaury Denoyellea441ec92022-07-04 15:48:57 +0200397static struct ncbuf *qc_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200398{
399 struct buffer buf = BUF_NULL;
400
401 if (ncb_is_null(ncbuf)) {
402 b_alloc(&buf);
403 BUG_ON(b_is_null(&buf));
404
405 *ncbuf = ncb_make(buf.area, buf.size, 0);
406 ncb_init(ncbuf, 0);
407 }
408
409 return ncbuf;
410}
411
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500412/* Notify an eventual subscriber on <qcs> or else wakeup up the stconn layer if
Amaury Denoyelle4561f842022-07-06 14:54:34 +0200413 * initialized.
414 */
415static void qcs_alert(struct qcs *qcs)
416{
417 if (qcs->subs) {
418 qcs_notify_recv(qcs);
419 qcs_notify_send(qcs);
420 }
421 else if (qcs_sc(qcs) && qcs->sd->sc->app_ops->wake) {
422 qcs->sd->sc->app_ops->wake(qcs->sd->sc);
423 }
424}
425
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100426int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
427{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100428 struct qcc *qcc = qcs->qcc;
429
430 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100431
432 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
433 BUG_ON(qcs->subs && qcs->subs != es);
434
435 es->events |= event_type;
436 qcs->subs = es;
437
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100438 if (event_type & SUB_RETRY_RECV)
439 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
440
441 if (event_type & SUB_RETRY_SEND)
442 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
443
444 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
445
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100446 return 0;
447}
448
449void qcs_notify_recv(struct qcs *qcs)
450{
451 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
452 tasklet_wakeup(qcs->subs->tasklet);
453 qcs->subs->events &= ~SUB_RETRY_RECV;
454 if (!qcs->subs->events)
455 qcs->subs = NULL;
456 }
457}
458
459void qcs_notify_send(struct qcs *qcs)
460{
461 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
462 tasklet_wakeup(qcs->subs->tasklet);
463 qcs->subs->events &= ~SUB_RETRY_SEND;
464 if (!qcs->subs->events)
465 qcs->subs = NULL;
466 }
467}
468
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200469/* Open a locally initiated stream for the connection <qcc>. Set <bidi> for a
470 * bidirectional stream, else an unidirectional stream is opened. The next
471 * available ID on the connection will be used according to the stream type.
472 *
473 * Returns the allocated stream instance or NULL on error.
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100474 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200475struct qcs *qcc_init_stream_local(struct qcc *qcc, int bidi)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100476{
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200477 struct qcs *qcs;
478 enum qcs_type type;
479 uint64_t *next;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100480
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200481 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
482
483 if (bidi) {
484 next = &qcc->next_bidi_l;
485 type = conn_is_back(qcc->conn) ? QCS_CLT_BIDI : QCS_SRV_BIDI;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100486 }
487 else {
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200488 next = &qcc->next_uni_l;
489 type = conn_is_back(qcc->conn) ? QCS_CLT_UNI : QCS_SRV_UNI;
490 }
491
492 /* TODO ensure that we won't overflow remote peer flow control limit on
493 * streams. Else, we should emit a STREAMS_BLOCKED frame.
494 */
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100495
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200496 qcs = qcs_new(qcc, *next, type);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200497 if (!qcs) {
498 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200499 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200500 }
Amaury Denoyellec055e302022-02-07 16:09:06 +0100501
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200502 TRACE_PROTO("opening local stream", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200503 *next += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100504
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200505 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200506 return qcs;
507}
508
509/* Open a remote initiated stream for the connection <qcc> with ID <id>. The
510 * caller is responsible to ensure that a stream with the same ID was not
511 * already opened. This function will also create all intermediaries streams
512 * with ID smaller than <id> not already opened before.
513 *
514 * Returns the allocated stream instance or NULL on error.
515 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200516static struct qcs *qcc_init_stream_remote(struct qcc *qcc, uint64_t id)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200517{
518 struct qcs *qcs = NULL;
519 enum qcs_type type;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200520 uint64_t *largest, max_id;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100521
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200522 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200523
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200524 BUG_ON_HOT(quic_stream_is_local(qcc, id));
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100525
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200526 if (quic_stream_is_bidi(id)) {
527 largest = &qcc->largest_bidi_r;
528 type = conn_is_back(qcc->conn) ? QCS_SRV_BIDI : QCS_CLT_BIDI;
529 }
530 else {
531 largest = &qcc->largest_uni_r;
532 type = conn_is_back(qcc->conn) ? QCS_SRV_UNI : QCS_CLT_UNI;
533 }
534
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200535 /* RFC 9000 4.6. Controlling Concurrency
536 *
537 * An endpoint that receives a frame with a stream ID exceeding the
538 * limit it has sent MUST treat this as a connection error of type
539 * STREAM_LIMIT_ERROR
540 */
541 max_id = quic_stream_is_bidi(id) ? qcc->lfctl.ms_bidi * 4 :
542 qcc->lfctl.ms_uni * 4;
543 if (id >= max_id) {
544 TRACE_ERROR("flow control error", QMUX_EV_QCS_NEW|QMUX_EV_PROTO_ERR, qcc->conn);
545 qcc_emit_cc(qcc, QC_ERR_STREAM_LIMIT_ERROR);
546 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200547 }
548
549 /* Only stream ID not already opened can be used. */
550 BUG_ON(id < *largest);
551
552 while (id >= *largest) {
Amaury Denoyellefd79ddb2022-08-16 11:13:45 +0200553 const char *str = *largest < id ? "initializing intermediary remote stream" :
554 "initializing remote stream";
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200555
556 qcs = qcs_new(qcc, *largest, type);
557 if (!qcs) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200558 /* TODO emit RESET_STREAM */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200559 TRACE_ERROR("stream fallocation failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200560 goto err;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100561 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200562
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200563 TRACE_PROTO(str, QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200564 *largest += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100565 }
566
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200567 out:
568 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle50742292022-03-29 14:57:19 +0200569 return qcs;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200570
571 err:
572 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
573 return NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200574}
575
576/* Use this function for a stream <id> which is not in <qcc> stream tree. It
577 * returns true if the associated stream is closed.
578 */
579static int qcc_stream_id_is_closed(struct qcc *qcc, uint64_t id)
580{
581 uint64_t *largest;
582
583 /* This function must only be used for stream not present in the stream tree. */
584 BUG_ON_HOT(eb64_lookup(&qcc->streams_by_id, id));
585
586 if (quic_stream_is_local(qcc, id)) {
587 largest = quic_stream_is_uni(id) ? &qcc->next_uni_l :
588 &qcc->next_bidi_l;
589 }
590 else {
591 largest = quic_stream_is_uni(id) ? &qcc->largest_uni_r :
592 &qcc->largest_bidi_r;
593 }
594
595 return id < *largest;
596}
597
598/* Retrieve the stream instance from <id> ID. This can be used when receiving
599 * STREAM, STREAM_DATA_BLOCKED, RESET_STREAM, MAX_STREAM_DATA or STOP_SENDING
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200600 * frames. Set to false <receive_only> or <send_only> if these particular types
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200601 * of streams are not allowed. If the stream instance is found, it is stored in
602 * <out>.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200603 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200604 * Returns 0 on success else non-zero. On error, a RESET_STREAM or a
605 * CONNECTION_CLOSE is automatically emitted. Beware that <out> may be NULL
606 * on success if the stream has already been closed.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200607 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200608int qcc_get_qcs(struct qcc *qcc, uint64_t id, int receive_only, int send_only,
609 struct qcs **out)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200610{
611 struct eb64_node *node;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200612
613 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200614 *out = NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200615
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200616 if (!receive_only && quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200617 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 +0200618 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200619 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200620 }
621
622 if (!send_only && quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200623 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 +0200624 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200625 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200626 }
627
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200628 /* Search the stream in the connection tree. */
629 node = eb64_lookup(&qcc->streams_by_id, id);
630 if (node) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200631 *out = eb64_entry(node, struct qcs, by_id);
632 TRACE_DEVEL("using stream from connection tree", QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200633 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200634 }
635
636 /* Check if stream is already closed. */
637 if (qcc_stream_id_is_closed(qcc, id)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200638 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200639 /* Consider this as a success even if <out> is left NULL. */
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200640 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200641 }
642
643 /* Create the stream. This is valid only for remote initiated one. A
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500644 * local stream must have already been explicitly created by the
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200645 * application protocol layer.
646 */
647 if (quic_stream_is_local(qcc, id)) {
648 /* RFC 9000 19.8. STREAM Frames
649 *
650 * An endpoint MUST terminate the connection with error
651 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
652 * initiated stream that has not yet been created, or for a send-only
653 * stream.
654 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200655 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 +0200656 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200657 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200658 }
659 else {
660 /* Remote stream not found - try to open it. */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200661 *out = qcc_init_stream_remote(qcc, id);
662 if (!*out) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200663 TRACE_ERROR("stream creation error", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200664 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200665 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200666 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100667
668 out:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200669 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200670 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200671
672 err:
673 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
674 return 1;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100675}
676
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200677/* Simple function to duplicate a buffer */
678static inline struct buffer qcs_b_dup(const struct ncbuf *b)
679{
680 return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0));
681}
682
Amaury Denoyelle36d4b5e2022-07-01 11:25:40 +0200683/* Remove <bytes> from <qcs> Rx buffer. Flow-control for received offsets may
684 * be allocated for the peer if needed.
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200685 */
686static void qcs_consume(struct qcs *qcs, uint64_t bytes)
687{
688 struct qcc *qcc = qcs->qcc;
689 struct quic_frame *frm;
690 struct ncbuf *buf = &qcs->rx.ncbuf;
691 enum ncb_ret ret;
692
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200693 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
694
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200695 ret = ncb_advance(buf, bytes);
696 if (ret) {
697 ABORT_NOW(); /* should not happens because removal only in data */
698 }
699
700 if (ncb_is_empty(buf))
701 qc_free_ncbuf(qcs, buf);
702
703 qcs->rx.offset += bytes;
Amaury Denoyellebb6296c2022-12-09 15:00:17 +0100704 /* Not necessary to emit a MAX_STREAM_DATA if all data received. */
705 if (qcs->flags & QC_SF_SIZE_KNOWN)
706 goto conn_fctl;
707
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200708 if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200709 TRACE_DATA("increase stream credit via MAX_STREAM_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +0100710 frm = qc_frm_alloc(QUIC_FT_MAX_STREAM_DATA);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200711 BUG_ON(!frm); /* TODO handle this properly */
712
713 qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init;
714
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200715 frm->max_stream_data.id = qcs->id;
716 frm->max_stream_data.max_stream_data = qcs->rx.msd;
717
718 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
719 tasklet_wakeup(qcc->wait_event.tasklet);
720 }
721
Amaury Denoyellebb6296c2022-12-09 15:00:17 +0100722 conn_fctl:
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200723 qcc->lfctl.offsets_consume += bytes;
724 if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200725 TRACE_DATA("increase conn credit via MAX_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +0100726 frm = qc_frm_alloc(QUIC_FT_MAX_DATA);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200727 BUG_ON(!frm); /* TODO handle this properly */
728
729 qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init;
730
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200731 frm->max_data.max_data = qcc->lfctl.md;
732
733 LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list);
734 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
735 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200736
737 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200738}
739
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200740/* Decode the content of STREAM frames already received on the stream instance
741 * <qcs>.
742 *
743 * Returns 0 on success else non-zero.
744 */
745static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
746{
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200747 struct buffer b;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200748 ssize_t ret;
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200749 int fin = 0;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200750
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200751 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
752
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200753 b = qcs_b_dup(&qcs->rx.ncbuf);
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200754
Amaury Denoyelled1310f82022-09-16 13:30:59 +0200755 /* Signal FIN to application if STREAM FIN received with all data. */
756 if (qcs_is_close_remote(qcs))
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200757 fin = 1;
758
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100759 if (!(qcs->flags & QC_SF_READ_ABORTED)) {
760 ret = qcc->app_ops->decode_qcs(qcs, &b, fin);
761 if (ret < 0) {
762 TRACE_ERROR("decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
763 goto err;
764 }
765 }
766 else {
767 TRACE_DATA("ignore read on stream", QMUX_EV_QCS_RECV, qcc->conn, qcs);
768 ret = b_data(&b);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200769 }
770
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200771 if (ret) {
772 qcs_consume(qcs, ret);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200773 qcs_notify_recv(qcs);
774 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200775
776 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200777 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200778
779 err:
780 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
781 return 1;
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200782}
783
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200784/* Emit a CONNECTION_CLOSE_APP with error <err>. Reserved for application error
Amaury Denoyelled666d742022-07-13 15:15:58 +0200785 * code. To close the connection right away, set <immediate> : this is useful
786 * when dealing with a connection fatal error. Else a graceful shutdown will be
787 * conducted : the error-code is only registered. The lower layer is
788 * responsible to close the connection when deemed suitable. Note that in this
789 * case the error code might be overwritten if an immediate close is requested
790 * in the interval.
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200791 */
Amaury Denoyelled666d742022-07-13 15:15:58 +0200792void qcc_emit_cc_app(struct qcc *qcc, int err, int immediate)
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200793{
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200794 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
795
Amaury Denoyelled666d742022-07-13 15:15:58 +0200796 if (immediate) {
797 quic_set_connection_close(qcc->conn->handle.qc, quic_err_app(err));
798 qcc->flags |= QC_CF_CC_EMIT;
799 tasklet_wakeup(qcc->wait_event.tasklet);
800 }
801 else {
802 /* Only register the error code for graceful shutdown. */
803 qcc->conn->handle.qc->err = quic_err_app(err);
804 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200805
806 TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200807}
808
809/* Prepare for the emission of RESET_STREAM on <qcs> with error code <err>. */
810void qcc_reset_stream(struct qcs *qcs, int err)
811{
812 struct qcc *qcc = qcs->qcc;
813
814 if ((qcs->flags & QC_SF_TO_RESET) || qcs_is_close_local(qcs))
815 return;
816
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200817 TRACE_STATE("reset stream", QMUX_EV_QCS_END, qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200818 qcs->flags |= QC_SF_TO_RESET;
819 qcs->err = err;
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +0100820
821 qcc_send_stream(qcs, 1);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200822 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100823}
824
Amaury Denoyellef9b03262023-01-09 10:34:25 +0100825/* Register <qcs> stream for emission of STREAM, STOP_SENDING or RESET_STREAM.
826 * Set <urg> to 1 if stream content should be treated in priority compared to
827 * other streams.
828 */
829void qcc_send_stream(struct qcs *qcs, int urg)
Amaury Denoyelle20f2a422023-01-03 14:39:24 +0100830{
831 struct qcc *qcc = qcs->qcc;
832
833 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
834
835 /* Cannot send if already closed. */
836 BUG_ON(qcs_is_close_local(qcs));
837
Amaury Denoyellef9b03262023-01-09 10:34:25 +0100838 if (urg) {
839 LIST_DEL_INIT(&qcs->el_send);
840 LIST_INSERT(&qcc->send_list, &qcs->el_send);
841 }
842 else {
843 if (!LIST_INLIST(&qcs->el_send))
844 LIST_APPEND(&qcs->qcc->send_list, &qcs->el_send);
845 }
Amaury Denoyelle20f2a422023-01-03 14:39:24 +0100846
847 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
848}
849
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100850/* Prepare for the emission of STOP_SENDING on <qcs>. */
851void qcc_abort_stream_read(struct qcs *qcs)
852{
853 struct qcc *qcc = qcs->qcc;
854
855 TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn, qcs);
856
857 if ((qcs->flags & QC_SF_TO_STOP_SENDING) || qcs_is_close_remote(qcs))
858 goto end;
859
860 TRACE_STATE("abort stream read", QMUX_EV_QCS_END, qcc->conn, qcs);
861 qcs->flags |= (QC_SF_TO_STOP_SENDING|QC_SF_READ_ABORTED);
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +0100862
863 qcc_send_stream(qcs, 1);
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100864 tasklet_wakeup(qcc->wait_event.tasklet);
865
866 end:
867 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn, qcs);
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200868}
869
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200870/* Install the <app_ops> applicative layer of a QUIC connection on mux <qcc>.
871 * Returns 0 on success else non-zero.
872 */
873int qcc_install_app_ops(struct qcc *qcc, const struct qcc_app_ops *app_ops)
874{
875 TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn);
876
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +0100877 if (app_ops->init && !app_ops->init(qcc)) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200878 TRACE_ERROR("app ops init error", QMUX_EV_QCC_NEW, qcc->conn);
879 goto err;
880 }
881
882 TRACE_PROTO("application layer initialized", QMUX_EV_QCC_NEW, qcc->conn);
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +0100883 qcc->app_ops = app_ops;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200884
Amaury Denoyelle71fd0362023-01-24 17:35:37 +0100885 /* RFC 9114 7.2.4.2. Initialization
886 *
887 * Endpoints MUST NOT require any data to be
888 * received from the peer prior to sending the SETTINGS frame;
889 * settings MUST be sent as soon as the transport is ready to
890 * send data.
891 */
892 if (qcc->app_ops->finalize) {
893 if (qcc->app_ops->finalize(qcc->ctx)) {
894 TRACE_ERROR("app ops finalize error", QMUX_EV_QCC_NEW, qcc->conn);
895 goto err;
896 }
897 tasklet_wakeup(qcc->wait_event.tasklet);
898 }
899
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200900 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
901 return 0;
902
903 err:
904 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
905 return 1;
906}
907
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200908/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
909 * <data> with length <len> and represents the offset <offset>. <fin> is set if
910 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100911 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200912 * Returns 0 on success else non-zero. On error, the received frame should not
913 * be acknowledged.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100914 */
915int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200916 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100917{
918 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200919 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100920
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100921 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
922
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200923 if (qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200924 TRACE_DATA("connection closed", QMUX_EV_QCC_RECV, qcc->conn);
925 goto err;
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200926 }
927
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200928 /* RFC 9000 19.8. STREAM Frames
929 *
930 * An endpoint MUST terminate the connection with error
931 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
932 * initiated stream that has not yet been created, or for a send-only
933 * stream.
934 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200935 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200936 TRACE_DATA("qcs retrieval error", QMUX_EV_QCC_RECV, qcc->conn);
937 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200938 }
939
940 if (!qcs) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200941 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV, qcc->conn);
942 goto out;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200943 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100944
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200945 /* RFC 9000 4.5. Stream Final Size
946 *
947 * Once a final size for a stream is known, it cannot change. If a
948 * RESET_STREAM or STREAM frame is received indicating a change in the
949 * final size for the stream, an endpoint SHOULD respond with an error
950 * of type FINAL_SIZE_ERROR; see Section 11 for details on error
951 * handling.
952 */
953 if (qcs->flags & QC_SF_SIZE_KNOWN &&
954 (offset + len > qcs->rx.offset_max || (fin && offset + len < qcs->rx.offset_max))) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200955 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 +0200956 qcc_emit_cc(qcc, QC_ERR_FINAL_SIZE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200957 goto err;
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200958 }
959
Amaury Denoyelle5854fc02022-12-09 16:25:48 +0100960 if (qcs_is_close_remote(qcs)) {
961 TRACE_DATA("skipping STREAM for remotely closed", QMUX_EV_QCC_RECV, qcc->conn);
962 goto out;
963 }
964
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100965 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelled1310f82022-09-16 13:30:59 +0200966 /* TODO offset may have been received without FIN first and now
967 * with it. In this case, it must be notified to be able to
968 * close the stream.
969 */
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200970 TRACE_DATA("already received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
971 goto out;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100972 }
973
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200974 TRACE_PROTO("receiving STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200975 qcs_idle_open(qcs);
976
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200977 if (offset + len > qcs->rx.offset_max) {
978 uint64_t diff = offset + len - qcs->rx.offset_max;
979 qcs->rx.offset_max = offset + len;
980 qcc->lfctl.offsets_recv += diff;
981
982 if (offset + len > qcs->rx.msd ||
983 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
984 /* RFC 9000 4.1. Data Flow Control
985 *
986 * A receiver MUST close the connection with an error
987 * of type FLOW_CONTROL_ERROR if the sender violates
988 * the advertised connection or stream data limits
989 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200990 TRACE_ERROR("flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200991 qcc->conn, qcs);
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200992 qcc_emit_cc(qcc, QC_ERR_FLOW_CONTROL_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200993 goto err;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200994 }
995 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100996
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200997 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100998 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200999 ABORT_NOW();
1000 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001001 }
1002
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001003 TRACE_DATA("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001004 if (offset < qcs->rx.offset) {
Frédéric Lécaillea18c3332022-07-04 09:54:58 +02001005 size_t diff = qcs->rx.offset - offset;
1006
1007 len -= diff;
1008 data += diff;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001009 offset = qcs->rx.offset;
1010 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001011
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001012 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
1013 if (ret != NCB_RET_OK) {
1014 if (ret == NCB_RET_DATA_REJ) {
Amaury Denoyellecc3d7162022-05-20 15:14:57 +02001015 /* RFC 9000 2.2. Sending and Receiving Data
1016 *
1017 * An endpoint could receive data for a stream at the
1018 * same stream offset multiple times. Data that has
1019 * already been received can be discarded. The data at
1020 * a given offset MUST NOT change if it is sent
1021 * multiple times; an endpoint MAY treat receipt of
1022 * different data at the same offset within a stream as
1023 * a connection error of type PROTOCOL_VIOLATION.
1024 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +02001025 TRACE_ERROR("overlapping data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001026 qcc->conn, qcs);
Amaury Denoyellecc3d7162022-05-20 15:14:57 +02001027 qcc_emit_cc(qcc, QC_ERR_PROTOCOL_VIOLATION);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001028 }
1029 else if (ret == NCB_RET_GAP_SIZE) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001030 TRACE_DATA("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
1031 qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001032 }
1033 return 1;
1034 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001035
1036 if (fin)
Amaury Denoyelle3f39b402022-07-01 16:11:03 +02001037 qcs->flags |= QC_SF_SIZE_KNOWN;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001038
Amaury Denoyelled1310f82022-09-16 13:30:59 +02001039 if (qcs->flags & QC_SF_SIZE_KNOWN &&
1040 qcs->rx.offset_max == qcs->rx.offset + ncb_data(&qcs->rx.ncbuf, 0)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001041 qcs_close_remote(qcs);
Amaury Denoyelled1310f82022-09-16 13:30:59 +02001042 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001043
Amaury Denoyelle418ba212022-08-02 15:57:16 +02001044 if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001045 qcc_decode_qcs(qcc, qcs);
Amaury Denoyelle418ba212022-08-02 15:57:16 +02001046 qcc_refresh_timeout(qcc);
1047 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001048
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001049 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001050 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001051 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001052
1053 err:
1054 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1055 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001056}
1057
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001058/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
1059 * the frame.
1060 *
1061 * Returns 0 on success else non-zero.
1062 */
1063int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
1064{
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001065 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1066
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001067 TRACE_PROTO("receiving MAX_DATA", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001068 if (qcc->rfctl.md < max) {
1069 qcc->rfctl.md = max;
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001070 TRACE_DEVEL("increase remote max-data", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001071
1072 if (qcc->flags & QC_CF_BLK_MFCTL) {
1073 qcc->flags &= ~QC_CF_BLK_MFCTL;
1074 tasklet_wakeup(qcc->wait_event.tasklet);
1075 }
1076 }
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001077
1078 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001079 return 0;
1080}
1081
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001082/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
1083 * field of the frame and <id> is the identifier of the QUIC stream.
1084 *
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001085 * Returns 0 on success else non-zero. On error, the received frame should not
1086 * be acknowledged.
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001087 */
1088int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
1089{
1090 struct qcs *qcs;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001091
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001092 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1093
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001094 /* RFC 9000 19.10. MAX_STREAM_DATA Frames
1095 *
1096 * Receiving a MAX_STREAM_DATA frame for a locally
1097 * initiated stream that has not yet been created MUST be treated as a
1098 * connection error of type STREAM_STATE_ERROR. An endpoint that
1099 * receives a MAX_STREAM_DATA frame for a receive-only stream MUST
1100 * terminate the connection with error STREAM_STATE_ERROR.
1101 */
1102 if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) {
1103 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1104 return 1;
1105 }
1106
1107 if (qcs) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001108 TRACE_PROTO("receiving MAX_STREAM_DATA", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001109 if (max > qcs->tx.msd) {
1110 qcs->tx.msd = max;
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001111 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 +01001112
1113 if (qcs->flags & QC_SF_BLK_SFCTL) {
1114 qcs->flags &= ~QC_SF_BLK_SFCTL;
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001115 /* TODO optim: only wakeup IO-CB if stream has data to sent. */
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001116 tasklet_wakeup(qcc->wait_event.tasklet);
1117 }
1118 }
1119 }
1120
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001121 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1122 qcc_refresh_timeout(qcc);
1123
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001124 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1125 return 0;
1126}
1127
1128/* Handle a new RESET_STREAM frame from stream ID <id> with error code <err>
1129 * and final stream size <final_size>.
1130 *
1131 * Returns 0 on success else non-zero. On error, the received frame should not
1132 * be acknowledged.
1133 */
1134int qcc_recv_reset_stream(struct qcc *qcc, uint64_t id, uint64_t err, uint64_t final_size)
1135{
1136 struct qcs *qcs;
1137
1138 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1139
1140 /* RFC 9000 19.4. RESET_STREAM Frames
1141 *
1142 * An endpoint that receives a RESET_STREAM frame for a send-only stream
1143 * MUST terminate the connection with error STREAM_STATE_ERROR.
1144 */
1145 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
1146 TRACE_ERROR("RESET_STREAM for send-only stream received", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1147 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
1148 goto err;
1149 }
1150
1151 if (!qcs || qcs_is_close_remote(qcs))
1152 goto out;
1153
1154 TRACE_PROTO("receiving RESET_STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1155 qcs_idle_open(qcs);
1156
Amaury Denoyellee269aeb2023-01-30 12:13:22 +01001157 if (qcc->app_ops->close) {
1158 if (qcc->app_ops->close(qcs, QCC_APP_OPS_CLOSE_SIDE_RD)) {
1159 TRACE_ERROR("closure rejected by app layer", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1160 goto out;
1161 }
1162 }
1163
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001164 if (qcs->rx.offset_max > final_size ||
1165 ((qcs->flags & QC_SF_SIZE_KNOWN) && qcs->rx.offset_max != final_size)) {
1166 TRACE_ERROR("final size error on RESET_STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1167 qcc_emit_cc(qcc, QC_ERR_FINAL_SIZE_ERROR);
1168 goto err;
1169 }
1170
1171 qcs->flags |= QC_SF_SIZE_KNOWN;
1172 qcs_close_remote(qcs);
1173 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
1174
1175 if (qcs_sc(qcs)) {
1176 se_fl_set_error(qcs->sd);
1177 qcs_alert(qcs);
1178 }
1179
1180 out:
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001181 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001182 return 0;
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001183
1184 err:
1185 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1186 return 1;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001187}
1188
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001189/* Handle a new STOP_SENDING frame for stream ID <id>. The error code should be
1190 * specified in <err>.
1191 *
1192 * Returns 0 on success else non-zero. On error, the received frame should not
1193 * be acknowledged.
1194 */
1195int qcc_recv_stop_sending(struct qcc *qcc, uint64_t id, uint64_t err)
1196{
1197 struct qcs *qcs;
1198
1199 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1200
1201 /* RFC 9000 19.5. STOP_SENDING Frames
1202 *
1203 * Receiving a STOP_SENDING frame for a
1204 * locally initiated stream that has not yet been created MUST be
1205 * treated as a connection error of type STREAM_STATE_ERROR. An
1206 * endpoint that receives a STOP_SENDING frame for a receive-only stream
1207 * MUST terminate the connection with error STREAM_STATE_ERROR.
1208 */
1209 if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) {
1210 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1211 return 1;
1212 }
1213
1214 if (!qcs)
1215 goto out;
1216
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001217 TRACE_PROTO("receiving STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelled7755372022-10-03 17:20:31 +02001218
1219 /* RFC 9000 3.5. Solicited State Transitions
1220 *
1221 * An endpoint is expected to send another STOP_SENDING frame if a
1222 * packet containing a previous STOP_SENDING is lost. However, once
1223 * either all stream data or a RESET_STREAM frame has been received for
1224 * the stream -- that is, the stream is in any state other than "Recv"
1225 * or "Size Known" -- sending a STOP_SENDING frame is unnecessary.
1226 */
1227
1228 /* TODO thanks to previous RFC clause, STOP_SENDING is ignored if current stream
1229 * has already been closed locally. This is useful to not emit multiple
1230 * RESET_STREAM for a single stream. This is functional if stream is
1231 * locally closed due to all data transmitted, but in this case the RFC
1232 * advices to use an explicit RESET_STREAM.
1233 */
1234 if (qcs_is_close_local(qcs)) {
1235 TRACE_STATE("ignoring STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1236 goto out;
1237 }
1238
Amaury Denoyelle96ca1b72022-08-09 17:36:38 +02001239 qcs_idle_open(qcs);
1240
Amaury Denoyelle87f87662023-01-30 12:12:43 +01001241 if (qcc->app_ops->close) {
1242 if (qcc->app_ops->close(qcs, QCC_APP_OPS_CLOSE_SIDE_WR)) {
1243 TRACE_ERROR("closure rejected by app layer", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1244 goto out;
1245 }
1246 }
1247
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001248 /* RFC 9000 3.5. Solicited State Transitions
1249 *
1250 * An endpoint that receives a STOP_SENDING frame
1251 * MUST send a RESET_STREAM frame if the stream is in the "Ready" or
1252 * "Send" state. If the stream is in the "Data Sent" state, the
1253 * endpoint MAY defer sending the RESET_STREAM frame until the packets
1254 * containing outstanding data are acknowledged or declared lost. If
1255 * any outstanding data is declared lost, the endpoint SHOULD send a
1256 * RESET_STREAM frame instead of retransmitting the data.
1257 *
1258 * An endpoint SHOULD copy the error code from the STOP_SENDING frame to
1259 * the RESET_STREAM frame it sends, but it can use any application error
1260 * code.
1261 */
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001262 qcc_reset_stream(qcs, err);
1263
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001264 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1265 qcc_refresh_timeout(qcc);
1266
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001267 out:
1268 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1269 return 0;
1270}
1271
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001272/* Signal the closing of remote stream with id <id>. Flow-control for new
1273 * streams may be allocated for the peer if needed.
1274 */
1275static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +01001276{
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001277 struct quic_frame *frm;
1278
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001279 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn);
1280
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001281 if (quic_stream_is_bidi(id)) {
1282 ++qcc->lfctl.cl_bidi_r;
1283 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001284 TRACE_DATA("increase max stream limit with MAX_STREAMS_BIDI", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001285 frm = qc_frm_alloc(QUIC_FT_MAX_STREAMS_BIDI);
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001286 BUG_ON(!frm); /* TODO handle this properly */
1287
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001288 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
1289 qcc->lfctl.cl_bidi_r;
1290 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
1291 tasklet_wakeup(qcc->wait_event.tasklet);
1292
1293 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
1294 qcc->lfctl.cl_bidi_r = 0;
1295 }
1296 }
1297 else {
Amaury Denoyelle91077312022-12-22 18:56:09 +01001298 /* TODO unidirectional stream flow control with MAX_STREAMS_UNI
1299 * emission not implemented. It should be unnecessary for
1300 * HTTP/3 but may be required if other application protocols
1301 * are supported.
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02001302 */
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001303 }
1304
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001305 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
1306
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001307 return 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001308}
1309
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05001310/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001311static void qcs_destroy(struct qcs *qcs)
1312{
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001313 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001314 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001315
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001316 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001317
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001318 if (quic_stream_is_remote(qcs->qcc, id))
1319 qcc_release_remote_stream(qcs->qcc, id);
Amaury Denoyellec055e302022-02-07 16:09:06 +01001320
Amaury Denoyelledccbd732022-03-29 18:36:59 +02001321 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001322
1323 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001324}
1325
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001326/* Transfer as much as possible data on <qcs> from <in> to <out>. This is done
1327 * in respect with available flow-control at stream and connection level.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001328 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001329 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001330 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001331static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, struct buffer *in)
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001332{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001333 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001334 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001335 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001336
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001337 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001338
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001339 qc_get_buf(qcs, out);
1340
1341 /*
1342 * QCS out buffer diagram
1343 * head left to_xfer
1344 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001345 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001346 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001347 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001348 * ^ ack-off ^ sent-off ^ off
1349 *
1350 * STREAM frame
1351 * ^ ^
1352 * |xxxxxxxxxxxxxxxxx|
1353 */
1354
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001355 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001356 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001357 BUG_ON_HOT(qcc->tx.offsets < qcc->tx.sent_offsets);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001358
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001359 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001360 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001361
1362 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
1363 /* do not exceed flow control limit */
1364 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
1365 to_xfer = qcs->tx.msd - qcs->tx.offset;
1366
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001367 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001368 /* do not overcome flow control limit on connection */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001369 if (qcc->tx.offsets + to_xfer > qcc->rfctl.md)
1370 to_xfer = qcc->rfctl.md - qcc->tx.offsets;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001371
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001372 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001373 goto out;
1374
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001375 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001376
1377 out:
1378 {
1379 struct qcs_xfer_data_trace_arg arg = {
1380 .prep = b_data(out), .xfer = total,
1381 };
1382 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
1383 qcc->conn, qcs, &arg);
1384 }
1385
1386 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001387}
1388
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001389/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
1390 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
1391 * stream frame.
1392 *
1393 * Returns the length of the STREAM frame or a negative error code.
1394 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001395static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
1396 struct list *frm_list)
1397{
1398 struct qcc *qcc = qcs->qcc;
1399 struct quic_frame *frm;
1400 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001401 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001402
1403 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1404
Amaury Denoyellea4569202022-04-15 17:29:25 +02001405 /* if ack_offset < buf_offset, it points to an older buffer. */
1406 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
1407 BUG_ON(qcs->tx.sent_offset < base_off);
1408
1409 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001410 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001411 BUG_ON(total < 0);
1412
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001413 if (!total && !fin) {
1414 /* No need to send anything if total is NULL and no FIN to signal. */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001415 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1416 return 0;
1417 }
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001418 BUG_ON((!total && qcs->tx.sent_offset > qcs->tx.offset) ||
1419 (total && qcs->tx.sent_offset >= qcs->tx.offset));
Amaury Denoyellea4569202022-04-15 17:29:25 +02001420 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001421 BUG_ON(qcc->tx.sent_offsets + total > qcc->rfctl.md);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001422
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001423 TRACE_PROTO("sending STREAM frame", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001424 frm = qc_frm_alloc(QUIC_FT_STREAM_8);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001425 if (!frm) {
1426 TRACE_ERROR("frame alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001427 goto err;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001428 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001429
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001430 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001431 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001432 frm->stream.buf = out;
1433 frm->stream.data = (unsigned char *)b_peek(out, head);
Amaury Denoyelle1dac0182023-02-02 16:45:07 +01001434 frm->stream.offset.key = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001435
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +01001436 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001437 if (fin)
1438 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001439
1440 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001441 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001442 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001443 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001444
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001445 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
1446 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001447
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001448 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001449
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001450 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001451 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001452 struct qcs_build_stream_trace_arg arg = {
1453 .len = frm->stream.len, .fin = fin,
1454 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001455 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001456 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001457 qcc->conn, qcs, &arg);
1458 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001459
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001460 return total;
1461
1462 err:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001463 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001464 return -1;
1465}
1466
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001467/* Check after transferring data from qcs.tx.buf if FIN must be set on the next
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001468 * STREAM frame for <qcs>.
1469 *
1470 * Returns true if FIN must be set else false.
1471 */
1472static int qcs_stream_fin(struct qcs *qcs)
1473{
1474 return qcs->flags & QC_SF_FIN_STREAM && !b_data(&qcs->tx.buf);
1475}
1476
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001477/* Return true if <qcs> has data to send in new STREAM frames. */
1478static forceinline int qcs_need_sending(struct qcs *qcs)
1479{
1480 return b_data(&qcs->tx.buf) || qcs->tx.sent_offset < qcs->tx.offset ||
1481 qcs_stream_fin(qcs);
1482}
1483
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001484/* This function must be called by the upper layer to inform about the sending
1485 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
1486 * <offset>.
1487 */
1488void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
1489{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001490 struct qcc *qcc = qcs->qcc;
1491 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001492
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001493 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1494
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001495 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001496 BUG_ON(offset + data > qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001497
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001498 /* check if the STREAM frame has already been notified. It can happen
1499 * for retransmission.
1500 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001501 if (offset + data < qcs->tx.sent_offset) {
1502 TRACE_DEVEL("offset already notified", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1503 goto out;
1504 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001505
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001506 qcs_idle_open(qcs);
1507
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001508 diff = offset + data - qcs->tx.sent_offset;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001509 if (diff) {
1510 /* increase offset sum on connection */
1511 qcc->tx.sent_offsets += diff;
1512 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001513 if (qcc->tx.sent_offsets == qcc->rfctl.md) {
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001514 qcc->flags |= QC_CF_BLK_MFCTL;
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001515 TRACE_STATE("connection flow-control reached", QMUX_EV_QCS_SEND, qcc->conn);
1516 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001517
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001518 /* increase offset on stream */
1519 qcs->tx.sent_offset += diff;
1520 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
1521 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001522 if (qcs->tx.sent_offset == qcs->tx.msd) {
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001523 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyelle31d20572023-01-06 15:29:59 +01001524 TRACE_STATE("stream flow-control reached", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1525 }
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001526
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01001527 /* If qcs.stream.buf is full, release it to the lower layer. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001528 if (qcs->tx.offset == qcs->tx.sent_offset &&
1529 b_full(&qcs->stream->buf->buf)) {
1530 qc_stream_buf_release(qcs->stream);
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001531 }
1532 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001533
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001534 if (qcs->tx.offset == qcs->tx.sent_offset && !b_data(&qcs->tx.buf)) {
1535 /* Remove stream from send_list if all was sent. */
1536 LIST_DEL_INIT(&qcs->el_send);
1537 TRACE_STATE("stream sent done", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1538
1539 if (qcs->flags & (QC_SF_FIN_STREAM|QC_SF_DETACH)) {
1540 /* Close stream locally. */
1541 qcs_close_local(qcs);
1542 /* Reset flag to not emit multiple FIN STREAM frames. */
1543 qcs->flags &= ~QC_SF_FIN_STREAM;
1544 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001545 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001546
1547 out:
1548 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001549}
1550
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001551/* Wrapper for send on transport layer. Send a list of frames <frms> for the
1552 * connection <qcc>.
1553 *
1554 * Returns 0 if all data sent with success else non-zero.
1555 */
1556static int qc_send_frames(struct qcc *qcc, struct list *frms)
1557{
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001558 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
1559
1560 if (LIST_ISEMPTY(frms)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001561 TRACE_DEVEL("no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
1562 goto err;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001563 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +01001564
Amaury Denoyelle036cc5d2022-09-26 15:02:31 +02001565 if (!qc_send_mux(qcc->conn->handle.qc, frms))
1566 goto err;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001567
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001568 /* If there is frames left at this stage, transport layer is blocked.
1569 * Subscribe on it to retry later.
1570 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001571 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001572 TRACE_DEVEL("remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001573 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1574 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001575 goto err;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001576 }
1577
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001578 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001579 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001580
1581 err:
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001582 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001583 return 1;
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001584}
1585
1586/* Emit a RESET_STREAM on <qcs>.
1587 *
1588 * Returns 0 if the frame has been successfully sent else non-zero.
1589 */
1590static int qcs_send_reset(struct qcs *qcs)
1591{
1592 struct list frms = LIST_HEAD_INIT(frms);
1593 struct quic_frame *frm;
1594
1595 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1596
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001597 frm = qc_frm_alloc(QUIC_FT_RESET_STREAM);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001598 if (!frm) {
1599 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001600 return 1;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001601 }
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001602
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001603 frm->reset_stream.id = qcs->id;
1604 frm->reset_stream.app_error_code = qcs->err;
1605 frm->reset_stream.final_size = qcs->tx.sent_offset;
1606
1607 LIST_APPEND(&frms, &frm->list);
1608 if (qc_send_frames(qcs->qcc, &frms)) {
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001609 qc_frm_free(&frm);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001610 TRACE_DEVEL("cannot send RESET_STREAM", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1611 return 1;
1612 }
1613
1614 if (qcs_sc(qcs)) {
1615 se_fl_set_error(qcs->sd);
1616 qcs_alert(qcs);
1617 }
1618
1619 qcs_close_local(qcs);
1620 qcs->flags &= ~QC_SF_TO_RESET;
1621
1622 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001623 return 0;
1624}
1625
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001626/* Emit a STOP_SENDING on <qcs>.
1627 *
1628 * Returns 0 if the frame has been successfully sent else non-zero.
1629 */
1630static int qcs_send_stop_sending(struct qcs *qcs)
1631{
1632 struct list frms = LIST_HEAD_INIT(frms);
1633 struct quic_frame *frm;
1634 struct qcc *qcc = qcs->qcc;
1635
1636 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1637
1638 /* RFC 9000 3.3. Permitted Frame Types
1639 *
1640 * A
1641 * receiver MAY send a STOP_SENDING frame in any state where it has not
1642 * received a RESET_STREAM frame -- that is, states other than "Reset
1643 * Recvd" or "Reset Read". However, there is little value in sending a
1644 * STOP_SENDING frame in the "Data Recvd" state, as all stream data has
1645 * been received. A sender could receive either of these two types of
1646 * frames in any state as a result of delayed delivery of packets.¶
1647 */
1648 if (qcs_is_close_remote(qcs)) {
1649 TRACE_STATE("skip STOP_SENDING on remote already closed", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1650 goto done;
1651 }
1652
Amaury Denoyelle40c24f12023-01-27 17:47:49 +01001653 frm = qc_frm_alloc(QUIC_FT_STOP_SENDING);
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001654 if (!frm) {
1655 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1656 return 1;
1657 }
1658
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001659 frm->stop_sending.id = qcs->id;
1660 frm->stop_sending.app_error_code = qcs->err;
1661
1662 LIST_APPEND(&frms, &frm->list);
1663 if (qc_send_frames(qcs->qcc, &frms)) {
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001664 qc_frm_free(&frm);
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001665 TRACE_DEVEL("cannot send STOP_SENDING", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1666 return 1;
1667 }
1668
1669 done:
1670 qcs->flags &= ~QC_SF_TO_STOP_SENDING;
1671
1672 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1673 return 0;
1674}
1675
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001676/* Used internally by qc_send function. Proceed to send for <qcs>. This will
1677 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001678 * is then generated and inserted in <frms> list.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001679 *
1680 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
1681 * be null if out buffer cannot be allocated.
1682 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001683static int _qc_send_qcs(struct qcs *qcs, struct list *frms)
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001684{
1685 struct qcc *qcc = qcs->qcc;
1686 struct buffer *buf = &qcs->tx.buf;
1687 struct buffer *out = qc_stream_buf_get(qcs->stream);
1688 int xfer = 0;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001689 char fin = 0;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001690
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001691 /* Cannot send STREAM on remote unidirectional streams. */
1692 BUG_ON(quic_stream_is_uni(qcs->id) && quic_stream_is_remote(qcc, qcs->id));
1693
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001694 /* Allocate <out> buffer if necessary. */
1695 if (!out) {
1696 if (qcc->flags & QC_CF_CONN_FULL)
1697 return 0;
1698
1699 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
1700 if (!out) {
1701 qcc->flags |= QC_CF_CONN_FULL;
1702 return 0;
1703 }
1704 }
1705
1706 /* Transfer data from <buf> to <out>. */
1707 if (b_data(buf)) {
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001708 xfer = qcs_xfer_data(qcs, out, buf);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001709 if (xfer > 0) {
1710 qcs_notify_send(qcs);
1711 qcs->flags &= ~QC_SF_BLK_MROOM;
1712 }
1713
1714 qcs->tx.offset += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001715 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001716 qcc->tx.offsets += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001717 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001718 }
1719
1720 /* out buffer cannot be emptied if qcs offsets differ. */
1721 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1722
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001723 /* FIN is set if all incoming data were transferred. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001724 fin = qcs_stream_fin(qcs);
1725
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001726 /* Build a new STREAM frame with <out> buffer. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001727 if (qcs->tx.sent_offset != qcs->tx.offset || fin) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001728 int ret;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001729 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +02001730 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001731 }
1732
1733 return xfer;
1734}
1735
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001736/* Proceed to sending. Loop through all available streams for the <qcc>
1737 * instance and try to send as much as possible.
1738 *
1739 * Returns the total of bytes sent to the transport layer.
1740 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001741static int qc_send(struct qcc *qcc)
1742{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001743 struct list frms = LIST_HEAD_INIT(frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001744 struct qcs *qcs, *qcs_tmp;
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01001745 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001746
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001747 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001748
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001749 if (qcc->conn->flags & CO_FL_SOCK_WR_SH || qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001750 qcc->conn->flags |= CO_FL_ERROR;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001751 TRACE_DEVEL("connection on error", QMUX_EV_QCC_SEND, qcc->conn);
1752 goto err;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001753 }
1754
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001755 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1756 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
1757 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
1758 goto out;
1759 }
1760 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02001761
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001762 if (qcc->flags & QC_CF_BLK_MFCTL)
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001763 goto err;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001764
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001765 /* Send STREAM/STOP_SENDING/RESET_STREAM data for registered streams. */
1766 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_list, el_send) {
1767 /* Stream must not be present in send_list if it has nothing to send. */
1768 BUG_ON(!(qcs->flags & (QC_SF_TO_STOP_SENDING|QC_SF_TO_RESET)) &&
1769 !qcs_need_sending(qcs));
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001770
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001771 /* Each STOP_SENDING/RESET_STREAM frame is sent individually to
1772 * guarantee its emission.
1773 *
1774 * TODO multiplex several frames in same datagram to optimize sending
1775 */
1776 if (qcs->flags & QC_SF_TO_STOP_SENDING) {
1777 if (qcs_send_stop_sending(qcs))
1778 goto out;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001779
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001780 /* Remove stream from send_list if it had only STOP_SENDING
1781 * to send.
1782 */
1783 if (!(qcs->flags & QC_SF_TO_RESET) && !qcs_need_sending(qcs)) {
1784 LIST_DEL_INIT(&qcs->el_send);
1785 continue;
1786 }
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001787 }
1788
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001789 if (qcs->flags & QC_SF_TO_RESET) {
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001790 if (qcs_send_reset(qcs))
1791 goto out;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001792
Amaury Denoyelle0a1154a2023-01-06 17:43:11 +01001793 /* RFC 9000 3.3. Permitted Frame Types
1794 *
1795 * A sender MUST NOT send
1796 * a STREAM or STREAM_DATA_BLOCKED frame for a stream in the
1797 * "Reset Sent" state or any terminal state -- that is, after
1798 * sending a RESET_STREAM frame.
1799 */
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001800 LIST_DEL_INIT(&qcs->el_send);
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001801 continue;
1802 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001803
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001804 if (!(qcs->flags & QC_SF_BLK_SFCTL))
1805 total += _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001806 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001807
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01001808 /* Retry sending until no frame to send, data rejected or connection
1809 * flow-control limit reached.
1810 */
1811 while (qc_send_frames(qcc, &frms) == 0 && !(qcc->flags & QC_CF_BLK_MFCTL)) {
1812 /* Reloop over <qcc.send_list>. Useful for streams which have
1813 * fulfilled their qc_stream_desc buf and have now release it.
1814 */
1815 list_for_each_entry(qcs, &qcc->send_list, el_send) {
1816 /* Only streams blocked on flow-control or waiting on a
1817 * new qc_stream_desc should be present in send_list as
1818 * long as transport layer can handle all data.
1819 */
1820 BUG_ON(qcs->stream->buf && !(qcs->flags & QC_SF_BLK_SFCTL));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001821
Amaury Denoyellea9de7ea2023-01-06 17:16:47 +01001822 if (!(qcs->flags & QC_SF_BLK_SFCTL))
1823 total += _qc_send_qcs(qcs, &frms);
1824 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001825 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001826
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001827 out:
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02001828 /* Deallocate frames that the transport layer has rejected. */
1829 if (!LIST_ISEMPTY(&frms)) {
1830 struct quic_frame *frm, *frm2;
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01001831
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001832 list_for_each_entry_safe(frm, frm2, &frms, list)
1833 qc_frm_free(&frm);
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02001834 }
1835
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001836 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001837 return total;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001838
1839 err:
1840 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
1841 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001842}
1843
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001844/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1845 * operation.
1846 *
1847 * Returns 0 on success else non-zero.
1848 */
1849static int qc_recv(struct qcc *qcc)
1850{
1851 struct eb64_node *node;
1852 struct qcs *qcs;
1853
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001854 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001855
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001856 if (qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001857 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001858 return 0;
1859 }
1860
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001861 node = eb64_first(&qcc->streams_by_id);
1862 while (node) {
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001863 uint64_t id;
1864
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001865 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001866 id = qcs->id;
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001867
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001868 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001869 node = eb64_next(node);
1870 continue;
1871 }
1872
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001873 if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001874 node = eb64_next(node);
1875 continue;
1876 }
1877
1878 qcc_decode_qcs(qcc, qcs);
1879 node = eb64_next(node);
1880 }
1881
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001882 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001883 return 0;
1884}
1885
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001886
1887/* Release all streams which have their transfer operation achieved.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001888 *
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001889 * Returns true if at least one stream is released.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001890 */
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001891static int qc_purge_streams(struct qcc *qcc)
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001892{
1893 struct eb64_node *node;
1894 int release = 0;
1895
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001896 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001897
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001898 node = eb64_first(&qcc->streams_by_id);
1899 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001900 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001901 node = eb64_next(node);
1902
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001903 /* Release not attached closed streams. */
1904 if (qcs->st == QC_SS_CLO && !qcs_sc(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001905 TRACE_STATE("purging closed stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001906 qcs_destroy(qcs);
1907 release = 1;
1908 continue;
1909 }
1910
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001911 /* Release detached streams with empty buffer. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001912 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02001913 if (qcs_is_close_local(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001914 TRACE_STATE("purging detached stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001915 qcs_destroy(qcs);
1916 release = 1;
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001917 continue;
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001918 }
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001919
1920 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1921 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001922 }
1923 }
1924
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001925 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001926 return release;
1927}
1928
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001929/* release function. This one should be called to free all resources allocated
1930 * to the mux.
1931 */
1932static void qc_release(struct qcc *qcc)
1933{
1934 struct connection *conn = qcc->conn;
1935 struct eb64_node *node;
1936
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001937 TRACE_ENTER(QMUX_EV_QCC_END, conn);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001938
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001939 if (qcc->app_ops && qcc->app_ops->shutdown) {
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001940 /* Application protocol with dedicated connection closing
1941 * procedure.
1942 */
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001943 qcc->app_ops->shutdown(qcc->ctx);
Amaury Denoyellea154dc02022-06-13 17:09:01 +02001944
1945 /* useful if application protocol should emit some closing
1946 * frames. For example HTTP/3 GOAWAY frame.
1947 */
1948 qc_send(qcc);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001949 }
1950 else {
1951 qcc_emit_cc_app(qcc, QC_ERR_NO_ERROR, 0);
1952 }
1953
1954 if (qcc->task) {
1955 task_destroy(qcc->task);
1956 qcc->task = NULL;
1957 }
1958
1959 if (qcc->wait_event.tasklet)
1960 tasklet_free(qcc->wait_event.tasklet);
1961 if (conn && qcc->wait_event.events) {
1962 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
1963 qcc->wait_event.events,
1964 &qcc->wait_event);
1965 }
1966
1967 /* liberate remaining qcs instances */
1968 node = eb64_first(&qcc->streams_by_id);
1969 while (node) {
1970 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
1971 node = eb64_next(node);
1972 qcs_free(qcs);
1973 }
1974
1975 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1976 struct quic_frame *frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list);
Amaury Denoyelle57b3eaa2023-02-02 16:12:09 +01001977 qc_frm_free(&frm);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001978 }
1979
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001980 if (qcc->app_ops && qcc->app_ops->release)
1981 qcc->app_ops->release(qcc->ctx);
1982 TRACE_PROTO("application layer released", QMUX_EV_QCC_END, conn);
1983
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001984 pool_free(pool_head_qcc, qcc);
1985
1986 if (conn) {
1987 LIST_DEL_INIT(&conn->stopping_list);
1988
1989 conn->handle.qc->conn = NULL;
1990 conn->mux = NULL;
1991 conn->ctx = NULL;
1992
1993 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
1994
1995 conn_stop_tracking(conn);
1996 conn_full_close(conn);
1997 if (conn->destroy_cb)
1998 conn->destroy_cb(conn);
1999 conn_free(conn);
2000 }
2001
2002 TRACE_LEAVE(QMUX_EV_QCC_END);
2003}
2004
Willy Tarreau41e701e2022-09-08 15:12:59 +02002005struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002006{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02002007 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002008
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002009 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002010
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002011 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002012
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02002013 if (qc_purge_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002014 if (qcc_is_dead(qcc)) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002015 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002016 goto release;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002017 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002018 }
2019
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02002020 qc_recv(qcc);
2021
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002022 /* TODO check if qcc proxy is disabled. If yes, use graceful shutdown
2023 * to close the connection.
2024 */
2025
2026 qcc_refresh_timeout(qcc);
2027
Amaury Denoyelled3973852022-07-25 14:56:54 +02002028 end:
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002029 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
2030 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002031
Amaury Denoyelle3baab742022-08-11 18:35:55 +02002032 release:
2033 qc_release(qcc);
2034 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002035 return NULL;
2036}
2037
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002038static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
2039{
2040 struct qcc *qcc = ctx;
2041 int expired = tick_is_expired(t->expire, now_ms);
2042
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002043 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002044
2045 if (qcc) {
2046 if (!expired) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002047 TRACE_DEVEL("not expired", QMUX_EV_QCC_WAKE, qcc->conn);
2048 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002049 }
2050
2051 if (!qcc_may_expire(qcc)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002052 TRACE_DEVEL("cannot expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002053 t->expire = TICK_ETERNITY;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002054 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002055 }
2056 }
2057
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002058 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01002059
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002060 if (!qcc) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002061 TRACE_DEVEL("no more qcc", QMUX_EV_QCC_WAKE);
2062 goto out;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002063 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01002064
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002065 qcc->task = NULL;
2066
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002067 /* TODO depending on the timeout condition, different shutdown mode
2068 * should be used. For http keep-alive or disabled proxy, a graceful
2069 * shutdown should occurs. For all other cases, an immediate close
2070 * seems legitimate.
2071 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002072 if (qcc_is_dead(qcc)) {
2073 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002074 qc_release(qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002075 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002076
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002077 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002078 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002079 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002080
2081 requeue:
2082 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
2083 return t;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002084}
2085
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002086static int qc_init(struct connection *conn, struct proxy *prx,
2087 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002088{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002089 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002090 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002091
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002092 TRACE_ENTER(QMUX_EV_QCC_NEW);
2093
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002094 qcc = pool_alloc(pool_head_qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002095 if (!qcc) {
2096 TRACE_ERROR("alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002097 goto fail_no_qcc;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002098 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002099
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002100 qcc->conn = conn;
2101 conn->ctx = qcc;
Amaury Denoyellec603de42022-07-25 11:21:46 +02002102 qcc->nb_hreq = qcc->nb_sc = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01002103 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002104
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002105 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002106
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002107 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002108
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002109 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02002110 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002111
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002112 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02002113 qcc->tx.sent_offsets = qcc->tx.offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002114
2115 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002116 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002117 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002118 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002119 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002120
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002121 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002122 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002123 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002124 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002125
2126 /* Server initiated streams must respect the server flow control. */
2127 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002128 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002129 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002130 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
2131
2132 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002133 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002134 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002135 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002136
Amaury Denoyellec985cb12022-05-16 14:29:59 +02002137 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01002138 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02002139 qcc->lfctl.ms_uni = lparams->initial_max_streams_uni;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02002140 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
2141 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle176174f2022-10-21 17:02:18 +02002142 qcc->lfctl.msd_uni_r = lparams->initial_max_stream_data_uni;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01002143 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01002144
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02002145 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02002146 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02002147
Willy Tarreau784b8682022-04-11 14:18:10 +02002148 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01002149 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002150 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
2151 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle176174f2022-10-21 17:02:18 +02002152 qcc->rfctl.msd_uni_l = rparams->initial_max_stream_data_uni;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002153
Amaury Denoyellea509ffb2022-07-04 15:50:33 +02002154 if (conn_is_back(conn)) {
2155 qcc->next_bidi_l = 0x00;
2156 qcc->largest_bidi_r = 0x01;
2157 qcc->next_uni_l = 0x02;
2158 qcc->largest_uni_r = 0x03;
2159 }
2160 else {
2161 qcc->largest_bidi_r = 0x00;
2162 qcc->next_bidi_l = 0x01;
2163 qcc->largest_uni_r = 0x02;
2164 qcc->next_uni_l = 0x03;
2165 }
2166
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002167 qcc->wait_event.tasklet = tasklet_new();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002168 if (!qcc->wait_event.tasklet) {
2169 TRACE_ERROR("taslket alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002170 goto fail_no_tasklet;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002171 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002172
Amaury Denoyelle20f2a422023-01-03 14:39:24 +01002173 LIST_INIT(&qcc->send_list);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02002174
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002175 qcc->wait_event.tasklet->process = qc_io_cb;
2176 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01002177 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002178
Amaury Denoyelle07bf8f42022-07-22 16:16:03 +02002179 qcc->proxy = prx;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002180 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002181 qcc->task = NULL;
Amaury Denoyelleb6309452022-07-25 14:51:56 +02002182 qcc->timeout = conn_is_back(qcc->conn) ? prx->timeout.server :
2183 prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002184 if (tick_isset(qcc->timeout)) {
2185 qcc->task = task_new_here();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002186 if (!qcc->task) {
2187 TRACE_ERROR("timeout task alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002188 goto fail_no_timeout_task;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002189 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002190 qcc->task->process = qc_timeout_task;
2191 qcc->task->context = qcc;
2192 qcc->task->expire = tick_add(now_ms, qcc->timeout);
2193 }
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +02002194 qcc_reset_idle_start(qcc);
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02002195 LIST_INIT(&qcc->opening_list);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002196
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002197 if (!conn_is_back(conn)) {
2198 if (!LIST_INLIST(&conn->stopping_list)) {
2199 LIST_APPEND(&mux_stopping_data[tid].list,
2200 &conn->stopping_list);
2201 }
2202 }
2203
Willy Tarreau784b8682022-04-11 14:18:10 +02002204 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +01002205
2206 if (qcc_install_app_ops(qcc, conn->handle.qc->app_ops)) {
2207 TRACE_PROTO("Cannot install app layer", QMUX_EV_QCC_NEW, qcc->conn);
2208 /* prepare a CONNECTION_CLOSE frame */
2209 quic_set_connection_close(conn->handle.qc, quic_err_transport(QC_ERR_APPLICATION_ERROR));
2210 goto fail_install_app_ops;
2211 }
2212
Frédéric Lécaille9969adb2023-01-18 11:52:21 +01002213 if (qcc->app_ops == &h3_ops)
2214 proxy_inc_fe_cum_sess_ver_ctr(sess->listener, prx, 3);
2215
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002216 /* init read cycle */
2217 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002218
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002219 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002220 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002221
Amaury Denoyelleb4d119f2023-01-25 17:44:36 +01002222 fail_install_app_ops:
2223 if (qcc->app_ops && qcc->app_ops->release)
2224 qcc->app_ops->release(qcc->ctx);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002225 fail_no_timeout_task:
2226 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002227 fail_no_tasklet:
2228 pool_free(pool_head_qcc, qcc);
2229 fail_no_qcc:
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002230 TRACE_LEAVE(QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002231 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002232}
2233
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002234static void qc_destroy(void *ctx)
2235{
2236 struct qcc *qcc = ctx;
2237
2238 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
2239 qc_release(qcc);
2240 TRACE_LEAVE(QMUX_EV_QCC_END);
2241}
2242
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002243static void qc_detach(struct sedesc *sd)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002244{
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002245 struct qcs *qcs = sd->se;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002246 struct qcc *qcc = qcs->qcc;
2247
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002248 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002249
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002250 /* TODO this BUG_ON_HOT() is not correct as the stconn layer may detach
2251 * from the stream even if it is not closed remotely at the QUIC layer.
2252 * This happens for example when a stream must be closed due to a
2253 * rejected request. To better handle these cases, it will be required
2254 * to implement shutr/shutw MUX operations. Once this is done, this
2255 * BUG_ON_HOT() statement can be adjusted.
2256 */
2257 //BUG_ON_HOT(!qcs_is_close_remote(qcs));
Amaury Denoyellec603de42022-07-25 11:21:46 +02002258
2259 qcc_rm_sc(qcc);
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002260
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02002261 if (!qcs_is_close_local(qcs) && !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002262 TRACE_STATE("remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002263 qcs->flags |= QC_SF_DETACH;
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002264 qcc_refresh_timeout(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002265
2266 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002267 return;
2268 }
2269
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002270 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01002271
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002272 if (qcc_is_dead(qcc)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002273 TRACE_STATE("killing dead connection", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002274 goto release;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002275 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002276 else if (qcc->task) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002277 TRACE_DEVEL("refreshing connection's timeout", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002278 qcc_refresh_timeout(qcc);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002279 }
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002280 else {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002281 TRACE_DEVEL("completed", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002282 }
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002283
2284 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002285 return;
2286
2287 release:
2288 qc_release(qcc);
2289 TRACE_LEAVE(QMUX_EV_STRM_END);
2290 return;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002291}
2292
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002293/* Called from the upper layer, to receive data */
Amaury Denoyelleb7ce79c2022-11-24 10:51:19 +01002294static size_t qc_recv_buf(struct stconn *sc, struct buffer *buf,
2295 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002296{
Willy Tarreau3215e732022-05-27 10:09:11 +02002297 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002298 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002299 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002300
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002301 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002302
Amaury Denoyelled80fbca2022-09-19 17:02:28 +02002303 ret = qcs_http_rcv_buf(qcs, buf, count, &fin);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002304
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002305 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002306 se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002307 }
2308 else {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002309 se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
2310 if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING))
2311 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002312
Amaury Denoyelle72a78e82022-07-29 15:34:12 +02002313 /* Set end-of-input if FIN received and all data extracted. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002314 if (fin)
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002315 se_fl_set(qcs->sd, SE_FL_EOI);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002316
2317 if (b_size(&qcs->rx.app_buf)) {
2318 b_free(&qcs->rx.app_buf);
2319 offer_buffers(NULL, 1);
2320 }
2321 }
2322
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002323 if (ret) {
2324 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002325 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002326 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002327
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002328 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
2329
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002330 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002331}
2332
Amaury Denoyelleb7ce79c2022-11-24 10:51:19 +01002333static size_t qc_send_buf(struct stconn *sc, struct buffer *buf,
2334 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002335{
Willy Tarreau3215e732022-05-27 10:09:11 +02002336 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002337 size_t ret;
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002338 char fin;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002339
2340 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002341
Amaury Denoyelle3dc4e5a2022-09-13 16:49:21 +02002342 /* stream layer has been detached so no transfer must occur after. */
2343 BUG_ON_HOT(qcs->flags & QC_SF_DETACH);
2344
Amaury Denoyelle843a1192022-07-04 11:44:38 +02002345 if (qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_RESET)) {
Amaury Denoyelle0ed617a2022-09-20 14:46:40 +02002346 ret = qcs_http_reset_buf(qcs, buf, count);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002347 goto end;
2348 }
2349
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002350 ret = qcs_http_snd_buf(qcs, buf, count, &fin);
2351 if (fin)
2352 qcs->flags |= QC_SF_FIN_STREAM;
2353
Amaury Denoyelleab6cdec2023-01-10 10:41:41 +01002354 if (ret || fin) {
Amaury Denoyellef9b03262023-01-09 10:34:25 +01002355 qcc_send_stream(qcs, 0);
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002356 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
2357 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
2358 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002359
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002360 end:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002361 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
2362
2363 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002364}
2365
2366/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2367 * event subscriber <es> is not allowed to change from a previous call as long
2368 * as at least one event is still subscribed. The <event_type> must only be a
2369 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
2370 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002371static int qc_subscribe(struct stconn *sc, int event_type,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002372 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002373{
Willy Tarreau3215e732022-05-27 10:09:11 +02002374 return qcs_subscribe(__sc_mux_strm(sc), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002375}
2376
2377/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
2378 * The <es> pointer is not allowed to differ from the one passed to the
2379 * subscribe() call. It always returns zero.
2380 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002381static int qc_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002382{
Willy Tarreau3215e732022-05-27 10:09:11 +02002383 struct qcs *qcs = __sc_mux_strm(sc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002384
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002385 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
2386 BUG_ON(qcs->subs && qcs->subs != es);
2387
2388 es->events &= ~event_type;
2389 if (!es->events)
2390 qcs->subs = NULL;
2391
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002392 return 0;
2393}
2394
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002395/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Willy Tarreau4596fe22022-05-17 19:07:51 +02002396 * report SE_FL_ERR_PENDING|SE_FL_ERROR on the attached stream connectors and
2397 * wake them.
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002398 */
2399static int qc_wake_some_streams(struct qcc *qcc)
2400{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002401 struct qcs *qcs;
2402 struct eb64_node *node;
2403
2404 for (node = eb64_first(&qcc->streams_by_id); node;
2405 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02002406 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002407
Amaury Denoyelle3abeb572022-07-04 11:42:27 +02002408 if (!qcs_sc(qcs))
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002409 continue;
2410
2411 if (qcc->conn->flags & CO_FL_ERROR) {
Willy Tarreau35c4dd02023-01-17 16:25:29 +01002412 se_fl_set_error(qcs->sd);
Amaury Denoyelle4561f842022-07-06 14:54:34 +02002413 qcs_alert(qcs);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002414 }
2415 }
2416
2417 return 0;
2418}
2419
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002420static int qc_wake(struct connection *conn)
2421{
2422 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02002423 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002424
2425 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002426
2427 /* Check if a soft-stop is in progress.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002428 *
cui flitera94bedc2022-08-29 14:42:57 +08002429 * TODO this is relevant for frontend connections only.
Amaury Denoyelled0c62322022-05-23 08:52:58 +02002430 *
2431 * TODO Client should be notified with a H3 GOAWAY and then a
2432 * CONNECTION_CLOSE. However, quic-conn uses the listener socket for
2433 * sending which at this stage is already closed.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002434 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002435 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Amaury Denoyelled0c62322022-05-23 08:52:58 +02002436 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002437
Willy Tarreau784b8682022-04-11 14:18:10 +02002438 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002439 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
2440
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002441 qc_send(qcc);
2442
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002443 qc_wake_some_streams(qcc);
2444
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002445 if (qcc_is_dead(qcc))
2446 goto release;
2447
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002448 qcc_refresh_timeout(qcc);
2449
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002450 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002451 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002452
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002453 release:
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002454 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002455 qc_release(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002456 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002457 return 1;
2458}
2459
Amaury Denoyellea473f192022-12-21 10:21:58 +01002460static void qc_shutw(struct stconn *sc, enum co_shw_mode mode)
2461{
2462 struct qcs *qcs = __sc_mux_strm(sc);
2463
2464 TRACE_ENTER(QMUX_EV_STRM_SHUT, qcs->qcc->conn, qcs);
2465
2466 /* If QC_SF_FIN_STREAM is not set and stream is not closed locally, it
2467 * means that upper layer reported an early closure. A RESET_STREAM is
2468 * necessary if not already scheduled.
2469 */
2470
2471 if (!qcs_is_close_local(qcs) &&
2472 !(qcs->flags & (QC_SF_FIN_STREAM|QC_SF_TO_RESET))) {
2473 qcc_reset_stream(qcs, 0);
2474 se_fl_set_error(qcs->sd);
2475 }
2476
2477 TRACE_LEAVE(QMUX_EV_STRM_SHUT, qcs->qcc->conn, qcs);
2478}
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002479
Willy Tarreaub4a4fee2022-09-02 16:00:40 +02002480/* for debugging with CLI's "show sess" command. May emit multiple lines, each
2481 * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single
2482 * line is used. Each field starts with a space so it's safe to print it after
2483 * existing fields.
2484 */
2485static int qc_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx)
2486{
2487 struct qcs *qcs = sd->se;
2488 struct qcc *qcc;
2489 int ret = 0;
2490
2491 if (!qcs)
2492 return ret;
2493
2494 chunk_appendf(msg, " qcs=%p .flg=%#x .id=%llu .st=%s .ctx=%p, .err=%#llx",
2495 qcs, qcs->flags, (ull)qcs->id, qcs_st_to_str(qcs->st), qcs->ctx, (ull)qcs->err);
2496
2497 if (pfx)
2498 chunk_appendf(msg, "\n%s", pfx);
2499
2500 qcc = qcs->qcc;
2501 chunk_appendf(msg, " qcc=%p .flg=%#x .nbsc=%llu .nbhreq=%llu, .task=%p",
2502 qcc, qcc->flags, (ull)qcc->nb_sc, (ull)qcc->nb_hreq, qcc->task);
2503 return ret;
2504}
2505
2506
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002507static const struct mux_ops qc_ops = {
2508 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002509 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002510 .detach = qc_detach,
Amaury Denoyelleb7ce79c2022-11-24 10:51:19 +01002511 .rcv_buf = qc_recv_buf,
2512 .snd_buf = qc_send_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002513 .subscribe = qc_subscribe,
2514 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002515 .wake = qc_wake,
Amaury Denoyellea473f192022-12-21 10:21:58 +01002516 .shutw = qc_shutw,
Willy Tarreaub4a4fee2022-09-02 16:00:40 +02002517 .show_sd = qc_show_sd,
Willy Tarreaub5821e12022-04-26 11:54:08 +02002518 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02002519 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002520};
2521
2522static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002523 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002524
2525INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);