blob: e64480beb98943ab5ec9d9b82d14acd77f014dc0 [file] [log] [blame]
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001#include <haproxy/mux_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002
Amaury Denoyelleeb01f592021-10-07 16:44:05 +02003#include <import/eb64tree.h>
4
Frédéric Lécailledfbae762021-02-18 09:59:01 +01005#include <haproxy/api.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01006#include <haproxy/connection.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +01007#include <haproxy/dynbuf.h>
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02008#include <haproxy/list.h>
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02009#include <haproxy/ncbuf.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +010010#include <haproxy/pool.h>
Amaury Denoyelled80fbca2022-09-19 17:02:28 +020011#include <haproxy/qmux_http.h>
Amaury Denoyelle36d50bf2022-09-19 16:12:38 +020012#include <haproxy/qmux_trace.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020013#include <haproxy/quic_conn.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020014#include <haproxy/quic_stream.h>
Frédéric Lécaille748ece62022-05-21 23:58:40 +020015#include <haproxy/quic_tp-t.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020016#include <haproxy/ssl_sock-t.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020017#include <haproxy/stconn.h>
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010018#include <haproxy/trace.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010019
Amaury Denoyelledeed7772021-12-03 11:36:46 +010020DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010021DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
22
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +020023/* Emit a CONNECTION_CLOSE with error <err>. This will interrupt all future
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +020024 * send/receive operations.
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +020025 */
26static void qcc_emit_cc(struct qcc *qcc, int err)
27{
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +020028 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
29
30 TRACE_STATE("set CONNECTION_CLOSE on quic-conn", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle57e6db72022-07-13 15:07:56 +020031 quic_set_connection_close(qcc->conn->handle.qc, quic_err_transport(err));
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +020032 qcc->flags |= QC_CF_CC_EMIT;
33 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +020034
35 TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +020036}
37
Amaury Denoyelle4b167002022-12-12 09:59:50 +010038static void qc_free_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
39{
40 struct buffer buf;
41
42 if (ncb_is_null(ncbuf))
43 return;
44
45 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
46 b_free(&buf);
47 offer_buffers(NULL, 1);
48
49 *ncbuf = NCBUF_NULL;
50}
51
52/* Free <qcs> instance. This function is reserved for internal usage : it must
53 * only be called on qcs alloc error or on connection shutdown. Else
54 * qcs_destroy must be prefered to handle QUIC flow-control increase.
55 */
56static void qcs_free(struct qcs *qcs)
57{
58 struct qcc *qcc = qcs->qcc;
59
60 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn, qcs);
61
Amaury Denoyelle15337fd2022-12-20 14:47:16 +010062 /* Safe to use even if already removed from the list. */
63 LIST_DEL_INIT(&qcs->el_opening);
Amaury Denoyelle4b167002022-12-12 09:59:50 +010064
65 /* Release stream endpoint descriptor. */
66 BUG_ON(qcs->sd && !se_fl_test(qcs->sd, SE_FL_ORPHAN));
67 sedesc_free(qcs->sd);
68
69 /* Release app-layer context. */
70 if (qcs->ctx && qcc->app_ops->detach)
71 qcc->app_ops->detach(qcs);
72
73 /* Release qc_stream_desc buffer from quic-conn layer. */
74 qc_stream_desc_release(qcs->stream);
75
76 /* Free Rx/Tx buffers. */
77 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
78 b_free(&qcs->tx.buf);
79
80 BUG_ON(!qcc->strms[qcs_id_type(qcs->id)].nb_streams);
81 --qcc->strms[qcs_id_type(qcs->id)].nb_streams;
82
83 /* Remove qcs from qcc tree. */
84 eb64_delete(&qcs->by_id);
85
86 pool_free(pool_head_qcs, qcs);
87
88 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
89}
90
Amaury Denoyelledeed7772021-12-03 11:36:46 +010091/* Allocate a new QUIC streams with id <id> and type <type>. */
Amaury Denoyellea509ffb2022-07-04 15:50:33 +020092static struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +010093{
Amaury Denoyelledeed7772021-12-03 11:36:46 +010094 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010095
Amaury Denoyelle4f137572022-03-24 17:10:00 +010096 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
97
Amaury Denoyelledeed7772021-12-03 11:36:46 +010098 qcs = pool_alloc(pool_head_qcs);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +020099 if (!qcs) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200100 TRACE_ERROR("alloc failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200101 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200102 }
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200103
104 qcs->stream = NULL;
105 qcs->qcc = qcc;
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +0200106 qcs->sd = NULL;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200107 qcs->flags = QC_SF_NONE;
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200108 qcs->st = QC_SS_IDLE;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200109 qcs->ctx = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100110
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200111 /* App callback attach may register the stream for http-request wait.
112 * These fields must be initialed before.
113 */
114 LIST_INIT(&qcs->el_opening);
115 qcs->start = TICK_ETERNITY;
116
Amaury Denoyelle4b167002022-12-12 09:59:50 +0100117 /* store transport layer stream descriptor in qcc tree */
118 qcs->id = qcs->by_id.key = id;
119 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
120
121 qcc->strms[type].nb_streams++;
122
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200123 /* Allocate transport layer stream descriptor. Only needed for TX. */
124 if (!quic_stream_is_uni(id) || !quic_stream_is_remote(qcc, id)) {
125 struct quic_conn *qc = qcc->conn->handle.qc;
126 qcs->stream = qc_stream_desc_new(id, type, qcs, qc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200127 if (!qcs->stream) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200128 TRACE_ERROR("qc_stream_desc alloc failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200129 goto err;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200130 }
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200131 }
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200132
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200133 if (qcc->app_ops->attach) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200134 if (qcc->app_ops->attach(qcs, qcc->ctx)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200135 TRACE_ERROR("app proto failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200136 goto err;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200137 }
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200138 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100139
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100140 /* If stream is local, use peer remote-limit, or else the opposite. */
Amaury Denoyelle176174f2022-10-21 17:02:18 +0200141 if (quic_stream_is_bidi(id)) {
142 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
143 qcc->rfctl.msd_bidi_l;
144 }
145 else if (quic_stream_is_local(qcc, id)) {
146 qcs->tx.msd = qcc->rfctl.msd_uni_l;
147 }
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100148
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200149 qcs->rx.ncbuf = NCBUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100150 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200151 qcs->rx.offset = qcs->rx.offset_max = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100152
Amaury Denoyelle176174f2022-10-21 17:02:18 +0200153 if (quic_stream_is_bidi(id)) {
154 qcs->rx.msd = quic_stream_is_local(qcc, id) ? qcc->lfctl.msd_bidi_l :
155 qcc->lfctl.msd_bidi_r;
156 }
157 else if (quic_stream_is_remote(qcc, id)) {
158 qcs->rx.msd = qcc->lfctl.msd_uni_r;
159 }
Amaury Denoyellea9773552022-05-16 14:38:25 +0200160 qcs->rx.msd_init = qcs->rx.msd;
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200161
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100162 qcs->tx.buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100163 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100164 qcs->tx.sent_offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100165
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100166 qcs->wait_event.tasklet = NULL;
167 qcs->wait_event.events = 0;
168 qcs->subs = NULL;
169
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200170 qcs->err = 0;
171
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100172 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100173 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100174 return qcs;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200175
176 err:
Amaury Denoyelle4b167002022-12-12 09:59:50 +0100177 qcs_free(qcs);
178 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200179 return NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100180}
181
Amaury Denoyelle3abeb572022-07-04 11:42:27 +0200182static forceinline struct stconn *qcs_sc(const struct qcs *qcs)
183{
184 return qcs->sd ? qcs->sd->sc : NULL;
185}
186
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200187/* Reset the <qcc> inactivity timeout for http-keep-alive timeout. */
188static forceinline void qcc_reset_idle_start(struct qcc *qcc)
189{
190 qcc->idle_start = now_ms;
191}
192
Amaury Denoyellec603de42022-07-25 11:21:46 +0200193/* Decrement <qcc> sc. */
194static forceinline void qcc_rm_sc(struct qcc *qcc)
195{
196 BUG_ON_HOT(!qcc->nb_sc);
197 --qcc->nb_sc;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200198
199 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
200 * refreshed after this on stream detach.
201 */
202 if (!qcc->nb_sc && !qcc->nb_hreq)
203 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200204}
205
206/* Decrement <qcc> hreq. */
207static forceinline void qcc_rm_hreq(struct qcc *qcc)
208{
209 BUG_ON_HOT(!qcc->nb_hreq);
210 --qcc->nb_hreq;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200211
212 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
213 * refreshed after this on I/O handler.
214 */
215 if (!qcc->nb_sc && !qcc->nb_hreq)
216 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200217}
218
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200219static inline int qcc_is_dead(const struct qcc *qcc)
220{
221 /* Mux connection is considered dead if :
222 * - all stream-desc are detached AND
223 * = connection is on error OR
224 * = mux timeout has already fired or is unset
225 */
226 if (!qcc->nb_sc && ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task))
227 return 1;
228
229 return 0;
230}
231
232/* Return true if the mux timeout should be armed. */
233static inline int qcc_may_expire(struct qcc *qcc)
234{
235 return !qcc->nb_sc;
236}
237
238/* Refresh the timeout on <qcc> if needed depending on its state. */
239static void qcc_refresh_timeout(struct qcc *qcc)
240{
241 const struct proxy *px = qcc->proxy;
242
243 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
244
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200245 if (!qcc->task) {
246 TRACE_DEVEL("already expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200247 goto leave;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200248 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200249
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200250 /* Check if upper layer is responsible of timeout management. */
251 if (!qcc_may_expire(qcc)) {
252 TRACE_DEVEL("not eligible for timeout", QMUX_EV_QCC_WAKE, qcc->conn);
253 qcc->task->expire = TICK_ETERNITY;
254 task_queue(qcc->task);
255 goto leave;
256 }
257
258 /* TODO if connection is idle on frontend and proxy is disabled, remove
259 * it with global close_spread delay applied.
260 */
261
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200262 /* TODO implement client/server-fin timeout for graceful shutdown */
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200263
264 /* Frontend timeout management
265 * - detached streams with data left to send -> default timeout
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200266 * - stream waiting on incomplete request or no stream yet activated -> timeout http-request
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200267 * - idle after stream processing -> timeout http-keep-alive
268 */
269 if (!conn_is_back(qcc->conn)) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200270 if (qcc->nb_hreq) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200271 TRACE_DEVEL("one or more requests still in progress", QMUX_EV_QCC_WAKE, qcc->conn);
272 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200273 task_queue(qcc->task);
274 goto leave;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200275 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200276
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200277 if (!LIST_ISEMPTY(&qcc->opening_list) || unlikely(!qcc->largest_bidi_r)) {
278 int timeout = px->timeout.httpreq;
279 struct qcs *qcs = NULL;
280 int base_time;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200281
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200282 /* Use start time of first stream waiting on HTTP or
283 * qcc idle if no stream not yet used.
284 */
285 if (likely(!LIST_ISEMPTY(&qcc->opening_list)))
286 qcs = LIST_ELEM(qcc->opening_list.n, struct qcs *, el_opening);
287 base_time = qcs ? qcs->start : qcc->idle_start;
288
289 TRACE_DEVEL("waiting on http request", QMUX_EV_QCC_WAKE, qcc->conn, qcs);
290 qcc->task->expire = tick_add_ifset(base_time, timeout);
291 }
292 else {
293 /* Use http-request timeout if keep-alive timeout not set */
294 int timeout = tick_isset(px->timeout.httpka) ?
295 px->timeout.httpka : px->timeout.httpreq;
296
297 TRACE_DEVEL("at least one request achieved but none currently in progress", QMUX_EV_QCC_WAKE, qcc->conn);
298 qcc->task->expire = tick_add_ifset(qcc->idle_start, timeout);
299 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200300 }
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200301
302 /* fallback to default timeout if frontend specific undefined or for
303 * backend connections.
304 */
305 if (!tick_isset(qcc->task->expire)) {
306 TRACE_DEVEL("fallback to default timeout", QMUX_EV_QCC_WAKE, qcc->conn);
307 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200308 }
309
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200310 task_queue(qcc->task);
311
312 leave:
313 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
314}
315
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200316/* Mark a stream as open if it was idle. This can be used on every
317 * successful emission/reception operation to update the stream state.
318 */
319static void qcs_idle_open(struct qcs *qcs)
320{
321 /* This operation must not be used if the stream is already closed. */
322 BUG_ON_HOT(qcs->st == QC_SS_CLO);
323
324 if (qcs->st == QC_SS_IDLE) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200325 TRACE_STATE("opening stream", QMUX_EV_QCS_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200326 qcs->st = QC_SS_OPEN;
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200327 }
328}
329
330/* Close the local channel of <qcs> instance. */
331static void qcs_close_local(struct qcs *qcs)
332{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200333 TRACE_STATE("closing stream locally", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
334
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200335 /* The stream must have already been opened. */
336 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
337
338 /* This operation cannot be used multiple times. */
339 BUG_ON_HOT(qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO);
340
341 if (quic_stream_is_bidi(qcs->id)) {
342 qcs->st = (qcs->st == QC_SS_HREM) ? QC_SS_CLO : QC_SS_HLOC;
Amaury Denoyelleafb7b9d2022-09-19 11:58:24 +0200343
344 if (qcs->flags & QC_SF_HREQ_RECV)
345 qcc_rm_hreq(qcs->qcc);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200346 }
347 else {
348 /* Only local uni streams are valid for this operation. */
349 BUG_ON_HOT(quic_stream_is_remote(qcs->qcc, qcs->id));
350 qcs->st = QC_SS_CLO;
351 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200352}
353
354/* Close the remote channel of <qcs> instance. */
355static void qcs_close_remote(struct qcs *qcs)
356{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200357 TRACE_STATE("closing stream remotely", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
358
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200359 /* The stream must have already been opened. */
360 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
361
362 /* This operation cannot be used multiple times. */
363 BUG_ON_HOT(qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO);
364
365 if (quic_stream_is_bidi(qcs->id)) {
366 qcs->st = (qcs->st == QC_SS_HLOC) ? QC_SS_CLO : QC_SS_HREM;
367 }
368 else {
369 /* Only remote uni streams are valid for this operation. */
370 BUG_ON_HOT(quic_stream_is_local(qcs->qcc, qcs->id));
371 qcs->st = QC_SS_CLO;
372 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200373}
374
375static int qcs_is_close_local(struct qcs *qcs)
376{
377 return qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO;
378}
379
Amaury Denoyelle6eb3c4b2022-12-09 16:26:03 +0100380static int qcs_is_close_remote(struct qcs *qcs)
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200381{
382 return qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO;
383}
384
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100385struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100386{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100387 struct buffer *buf = b_alloc(bptr);
388 BUG_ON(!buf);
389 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100390}
391
Amaury Denoyellea441ec92022-07-04 15:48:57 +0200392static struct ncbuf *qc_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200393{
394 struct buffer buf = BUF_NULL;
395
396 if (ncb_is_null(ncbuf)) {
397 b_alloc(&buf);
398 BUG_ON(b_is_null(&buf));
399
400 *ncbuf = ncb_make(buf.area, buf.size, 0);
401 ncb_init(ncbuf, 0);
402 }
403
404 return ncbuf;
405}
406
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500407/* Notify an eventual subscriber on <qcs> or else wakeup up the stconn layer if
Amaury Denoyelle4561f842022-07-06 14:54:34 +0200408 * initialized.
409 */
410static void qcs_alert(struct qcs *qcs)
411{
412 if (qcs->subs) {
413 qcs_notify_recv(qcs);
414 qcs_notify_send(qcs);
415 }
416 else if (qcs_sc(qcs) && qcs->sd->sc->app_ops->wake) {
417 qcs->sd->sc->app_ops->wake(qcs->sd->sc);
418 }
419}
420
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100421int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
422{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100423 struct qcc *qcc = qcs->qcc;
424
425 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100426
427 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
428 BUG_ON(qcs->subs && qcs->subs != es);
429
430 es->events |= event_type;
431 qcs->subs = es;
432
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100433 if (event_type & SUB_RETRY_RECV)
434 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
435
436 if (event_type & SUB_RETRY_SEND)
437 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
438
439 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
440
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100441 return 0;
442}
443
444void qcs_notify_recv(struct qcs *qcs)
445{
446 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
447 tasklet_wakeup(qcs->subs->tasklet);
448 qcs->subs->events &= ~SUB_RETRY_RECV;
449 if (!qcs->subs->events)
450 qcs->subs = NULL;
451 }
452}
453
454void qcs_notify_send(struct qcs *qcs)
455{
456 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
457 tasklet_wakeup(qcs->subs->tasklet);
458 qcs->subs->events &= ~SUB_RETRY_SEND;
459 if (!qcs->subs->events)
460 qcs->subs = NULL;
461 }
462}
463
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200464/* Open a locally initiated stream for the connection <qcc>. Set <bidi> for a
465 * bidirectional stream, else an unidirectional stream is opened. The next
466 * available ID on the connection will be used according to the stream type.
467 *
468 * Returns the allocated stream instance or NULL on error.
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100469 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200470struct qcs *qcc_init_stream_local(struct qcc *qcc, int bidi)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100471{
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200472 struct qcs *qcs;
473 enum qcs_type type;
474 uint64_t *next;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100475
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200476 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
477
478 if (bidi) {
479 next = &qcc->next_bidi_l;
480 type = conn_is_back(qcc->conn) ? QCS_CLT_BIDI : QCS_SRV_BIDI;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100481 }
482 else {
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200483 next = &qcc->next_uni_l;
484 type = conn_is_back(qcc->conn) ? QCS_CLT_UNI : QCS_SRV_UNI;
485 }
486
487 /* TODO ensure that we won't overflow remote peer flow control limit on
488 * streams. Else, we should emit a STREAMS_BLOCKED frame.
489 */
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100490
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200491 qcs = qcs_new(qcc, *next, type);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200492 if (!qcs) {
493 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200494 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200495 }
Amaury Denoyellec055e302022-02-07 16:09:06 +0100496
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200497 TRACE_PROTO("opening local stream", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200498 *next += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100499
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200500 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200501 return qcs;
502}
503
504/* Open a remote initiated stream for the connection <qcc> with ID <id>. The
505 * caller is responsible to ensure that a stream with the same ID was not
506 * already opened. This function will also create all intermediaries streams
507 * with ID smaller than <id> not already opened before.
508 *
509 * Returns the allocated stream instance or NULL on error.
510 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200511static struct qcs *qcc_init_stream_remote(struct qcc *qcc, uint64_t id)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200512{
513 struct qcs *qcs = NULL;
514 enum qcs_type type;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200515 uint64_t *largest, max_id;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100516
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200517 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200518
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200519 BUG_ON_HOT(quic_stream_is_local(qcc, id));
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100520
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200521 if (quic_stream_is_bidi(id)) {
522 largest = &qcc->largest_bidi_r;
523 type = conn_is_back(qcc->conn) ? QCS_SRV_BIDI : QCS_CLT_BIDI;
524 }
525 else {
526 largest = &qcc->largest_uni_r;
527 type = conn_is_back(qcc->conn) ? QCS_SRV_UNI : QCS_CLT_UNI;
528 }
529
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200530 /* RFC 9000 4.6. Controlling Concurrency
531 *
532 * An endpoint that receives a frame with a stream ID exceeding the
533 * limit it has sent MUST treat this as a connection error of type
534 * STREAM_LIMIT_ERROR
535 */
536 max_id = quic_stream_is_bidi(id) ? qcc->lfctl.ms_bidi * 4 :
537 qcc->lfctl.ms_uni * 4;
538 if (id >= max_id) {
539 TRACE_ERROR("flow control error", QMUX_EV_QCS_NEW|QMUX_EV_PROTO_ERR, qcc->conn);
540 qcc_emit_cc(qcc, QC_ERR_STREAM_LIMIT_ERROR);
541 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200542 }
543
544 /* Only stream ID not already opened can be used. */
545 BUG_ON(id < *largest);
546
547 while (id >= *largest) {
Amaury Denoyellefd79ddb2022-08-16 11:13:45 +0200548 const char *str = *largest < id ? "initializing intermediary remote stream" :
549 "initializing remote stream";
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200550
551 qcs = qcs_new(qcc, *largest, type);
552 if (!qcs) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200553 /* TODO emit RESET_STREAM */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200554 TRACE_ERROR("stream fallocation failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200555 goto err;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100556 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200557
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200558 TRACE_PROTO(str, QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200559 *largest += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100560 }
561
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200562 out:
563 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle50742292022-03-29 14:57:19 +0200564 return qcs;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200565
566 err:
567 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
568 return NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200569}
570
571/* Use this function for a stream <id> which is not in <qcc> stream tree. It
572 * returns true if the associated stream is closed.
573 */
574static int qcc_stream_id_is_closed(struct qcc *qcc, uint64_t id)
575{
576 uint64_t *largest;
577
578 /* This function must only be used for stream not present in the stream tree. */
579 BUG_ON_HOT(eb64_lookup(&qcc->streams_by_id, id));
580
581 if (quic_stream_is_local(qcc, id)) {
582 largest = quic_stream_is_uni(id) ? &qcc->next_uni_l :
583 &qcc->next_bidi_l;
584 }
585 else {
586 largest = quic_stream_is_uni(id) ? &qcc->largest_uni_r :
587 &qcc->largest_bidi_r;
588 }
589
590 return id < *largest;
591}
592
593/* Retrieve the stream instance from <id> ID. This can be used when receiving
594 * STREAM, STREAM_DATA_BLOCKED, RESET_STREAM, MAX_STREAM_DATA or STOP_SENDING
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200595 * frames. Set to false <receive_only> or <send_only> if these particular types
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200596 * of streams are not allowed. If the stream instance is found, it is stored in
597 * <out>.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200598 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200599 * Returns 0 on success else non-zero. On error, a RESET_STREAM or a
600 * CONNECTION_CLOSE is automatically emitted. Beware that <out> may be NULL
601 * on success if the stream has already been closed.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200602 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200603int qcc_get_qcs(struct qcc *qcc, uint64_t id, int receive_only, int send_only,
604 struct qcs **out)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200605{
606 struct eb64_node *node;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200607
608 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200609 *out = NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200610
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200611 if (!receive_only && quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200612 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 +0200613 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200614 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200615 }
616
617 if (!send_only && quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200618 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 +0200619 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200620 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200621 }
622
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200623 /* Search the stream in the connection tree. */
624 node = eb64_lookup(&qcc->streams_by_id, id);
625 if (node) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200626 *out = eb64_entry(node, struct qcs, by_id);
627 TRACE_DEVEL("using stream from connection tree", QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200628 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200629 }
630
631 /* Check if stream is already closed. */
632 if (qcc_stream_id_is_closed(qcc, id)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200633 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200634 /* Consider this as a success even if <out> is left NULL. */
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200635 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200636 }
637
638 /* Create the stream. This is valid only for remote initiated one. A
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500639 * local stream must have already been explicitly created by the
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200640 * application protocol layer.
641 */
642 if (quic_stream_is_local(qcc, id)) {
643 /* RFC 9000 19.8. STREAM Frames
644 *
645 * An endpoint MUST terminate the connection with error
646 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
647 * initiated stream that has not yet been created, or for a send-only
648 * stream.
649 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200650 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 +0200651 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200652 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200653 }
654 else {
655 /* Remote stream not found - try to open it. */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200656 *out = qcc_init_stream_remote(qcc, id);
657 if (!*out) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200658 TRACE_ERROR("stream creation error", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200659 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200660 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200661 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100662
663 out:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200664 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200665 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200666
667 err:
668 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
669 return 1;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100670}
671
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200672/* Simple function to duplicate a buffer */
673static inline struct buffer qcs_b_dup(const struct ncbuf *b)
674{
675 return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0));
676}
677
Amaury Denoyelle36d4b5e2022-07-01 11:25:40 +0200678/* Remove <bytes> from <qcs> Rx buffer. Flow-control for received offsets may
679 * be allocated for the peer if needed.
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200680 */
681static void qcs_consume(struct qcs *qcs, uint64_t bytes)
682{
683 struct qcc *qcc = qcs->qcc;
684 struct quic_frame *frm;
685 struct ncbuf *buf = &qcs->rx.ncbuf;
686 enum ncb_ret ret;
687
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200688 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
689
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200690 ret = ncb_advance(buf, bytes);
691 if (ret) {
692 ABORT_NOW(); /* should not happens because removal only in data */
693 }
694
695 if (ncb_is_empty(buf))
696 qc_free_ncbuf(qcs, buf);
697
698 qcs->rx.offset += bytes;
Amaury Denoyellebb6296c2022-12-09 15:00:17 +0100699 /* Not necessary to emit a MAX_STREAM_DATA if all data received. */
700 if (qcs->flags & QC_SF_SIZE_KNOWN)
701 goto conn_fctl;
702
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200703 if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200704 TRACE_DATA("increase stream credit via MAX_STREAM_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200705 frm = pool_zalloc(pool_head_quic_frame);
706 BUG_ON(!frm); /* TODO handle this properly */
707
708 qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init;
709
710 LIST_INIT(&frm->reflist);
711 frm->type = QUIC_FT_MAX_STREAM_DATA;
712 frm->max_stream_data.id = qcs->id;
713 frm->max_stream_data.max_stream_data = qcs->rx.msd;
714
715 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
716 tasklet_wakeup(qcc->wait_event.tasklet);
717 }
718
Amaury Denoyellebb6296c2022-12-09 15:00:17 +0100719 conn_fctl:
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200720 qcc->lfctl.offsets_consume += bytes;
721 if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200722 TRACE_DATA("increase conn credit via MAX_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200723 frm = pool_zalloc(pool_head_quic_frame);
724 BUG_ON(!frm); /* TODO handle this properly */
725
726 qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init;
727
728 LIST_INIT(&frm->reflist);
729 frm->type = QUIC_FT_MAX_DATA;
730 frm->max_data.max_data = qcc->lfctl.md;
731
732 LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list);
733 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
734 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200735
736 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200737}
738
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200739/* Decode the content of STREAM frames already received on the stream instance
740 * <qcs>.
741 *
742 * Returns 0 on success else non-zero.
743 */
744static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
745{
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200746 struct buffer b;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200747 ssize_t ret;
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200748 int fin = 0;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200749
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200750 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
751
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200752 b = qcs_b_dup(&qcs->rx.ncbuf);
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200753
Amaury Denoyelled1310f82022-09-16 13:30:59 +0200754 /* Signal FIN to application if STREAM FIN received with all data. */
755 if (qcs_is_close_remote(qcs))
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200756 fin = 1;
757
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100758 if (!(qcs->flags & QC_SF_READ_ABORTED)) {
759 ret = qcc->app_ops->decode_qcs(qcs, &b, fin);
760 if (ret < 0) {
761 TRACE_ERROR("decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
762 goto err;
763 }
764 }
765 else {
766 TRACE_DATA("ignore read on stream", QMUX_EV_QCS_RECV, qcc->conn, qcs);
767 ret = b_data(&b);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200768 }
769
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200770 if (ret) {
771 qcs_consume(qcs, ret);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200772 qcs_notify_recv(qcs);
773 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200774
775 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200776 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200777
778 err:
779 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
780 return 1;
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200781}
782
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200783/* Emit a CONNECTION_CLOSE_APP with error <err>. Reserved for application error
Amaury Denoyelled666d742022-07-13 15:15:58 +0200784 * code. To close the connection right away, set <immediate> : this is useful
785 * when dealing with a connection fatal error. Else a graceful shutdown will be
786 * conducted : the error-code is only registered. The lower layer is
787 * responsible to close the connection when deemed suitable. Note that in this
788 * case the error code might be overwritten if an immediate close is requested
789 * in the interval.
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200790 */
Amaury Denoyelled666d742022-07-13 15:15:58 +0200791void qcc_emit_cc_app(struct qcc *qcc, int err, int immediate)
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200792{
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200793 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
794
Amaury Denoyelled666d742022-07-13 15:15:58 +0200795 if (immediate) {
796 quic_set_connection_close(qcc->conn->handle.qc, quic_err_app(err));
797 qcc->flags |= QC_CF_CC_EMIT;
798 tasklet_wakeup(qcc->wait_event.tasklet);
799 }
800 else {
801 /* Only register the error code for graceful shutdown. */
802 qcc->conn->handle.qc->err = quic_err_app(err);
803 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200804
805 TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200806}
807
808/* Prepare for the emission of RESET_STREAM on <qcs> with error code <err>. */
809void qcc_reset_stream(struct qcs *qcs, int err)
810{
811 struct qcc *qcc = qcs->qcc;
812
813 if ((qcs->flags & QC_SF_TO_RESET) || qcs_is_close_local(qcs))
814 return;
815
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200816 TRACE_STATE("reset stream", QMUX_EV_QCS_END, qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200817 qcs->flags |= QC_SF_TO_RESET;
818 qcs->err = err;
819 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyelle663e8722022-12-09 14:58:28 +0100820}
821
822/* Prepare for the emission of STOP_SENDING on <qcs>. */
823void qcc_abort_stream_read(struct qcs *qcs)
824{
825 struct qcc *qcc = qcs->qcc;
826
827 TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn, qcs);
828
829 if ((qcs->flags & QC_SF_TO_STOP_SENDING) || qcs_is_close_remote(qcs))
830 goto end;
831
832 TRACE_STATE("abort stream read", QMUX_EV_QCS_END, qcc->conn, qcs);
833 qcs->flags |= (QC_SF_TO_STOP_SENDING|QC_SF_READ_ABORTED);
834 tasklet_wakeup(qcc->wait_event.tasklet);
835
836 end:
837 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn, qcs);
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200838}
839
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200840/* Install the <app_ops> applicative layer of a QUIC connection on mux <qcc>.
841 * Returns 0 on success else non-zero.
842 */
843int qcc_install_app_ops(struct qcc *qcc, const struct qcc_app_ops *app_ops)
844{
845 TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn);
846
847 qcc->app_ops = app_ops;
848 if (qcc->app_ops->init && !qcc->app_ops->init(qcc)) {
849 TRACE_ERROR("app ops init error", QMUX_EV_QCC_NEW, qcc->conn);
850 goto err;
851 }
852
853 TRACE_PROTO("application layer initialized", QMUX_EV_QCC_NEW, qcc->conn);
854
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200855 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
856 return 0;
857
858 err:
859 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
860 return 1;
861}
862
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200863/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
864 * <data> with length <len> and represents the offset <offset>. <fin> is set if
865 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100866 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200867 * Returns 0 on success else non-zero. On error, the received frame should not
868 * be acknowledged.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100869 */
870int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200871 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100872{
873 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200874 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100875
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100876 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
877
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200878 if (qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200879 TRACE_DATA("connection closed", QMUX_EV_QCC_RECV, qcc->conn);
880 goto err;
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200881 }
882
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200883 /* RFC 9000 19.8. STREAM Frames
884 *
885 * An endpoint MUST terminate the connection with error
886 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
887 * initiated stream that has not yet been created, or for a send-only
888 * stream.
889 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200890 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200891 TRACE_DATA("qcs retrieval error", QMUX_EV_QCC_RECV, qcc->conn);
892 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200893 }
894
895 if (!qcs) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200896 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV, qcc->conn);
897 goto out;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200898 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100899
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200900 /* RFC 9000 4.5. Stream Final Size
901 *
902 * Once a final size for a stream is known, it cannot change. If a
903 * RESET_STREAM or STREAM frame is received indicating a change in the
904 * final size for the stream, an endpoint SHOULD respond with an error
905 * of type FINAL_SIZE_ERROR; see Section 11 for details on error
906 * handling.
907 */
908 if (qcs->flags & QC_SF_SIZE_KNOWN &&
909 (offset + len > qcs->rx.offset_max || (fin && offset + len < qcs->rx.offset_max))) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200910 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 +0200911 qcc_emit_cc(qcc, QC_ERR_FINAL_SIZE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200912 goto err;
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200913 }
914
Amaury Denoyelle5854fc02022-12-09 16:25:48 +0100915 if (qcs_is_close_remote(qcs)) {
916 TRACE_DATA("skipping STREAM for remotely closed", QMUX_EV_QCC_RECV, qcc->conn);
917 goto out;
918 }
919
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100920 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelled1310f82022-09-16 13:30:59 +0200921 /* TODO offset may have been received without FIN first and now
922 * with it. In this case, it must be notified to be able to
923 * close the stream.
924 */
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200925 TRACE_DATA("already received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
926 goto out;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100927 }
928
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200929 TRACE_PROTO("receiving STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200930 qcs_idle_open(qcs);
931
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200932 if (offset + len > qcs->rx.offset_max) {
933 uint64_t diff = offset + len - qcs->rx.offset_max;
934 qcs->rx.offset_max = offset + len;
935 qcc->lfctl.offsets_recv += diff;
936
937 if (offset + len > qcs->rx.msd ||
938 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
939 /* RFC 9000 4.1. Data Flow Control
940 *
941 * A receiver MUST close the connection with an error
942 * of type FLOW_CONTROL_ERROR if the sender violates
943 * the advertised connection or stream data limits
944 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200945 TRACE_ERROR("flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200946 qcc->conn, qcs);
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200947 qcc_emit_cc(qcc, QC_ERR_FLOW_CONTROL_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200948 goto err;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200949 }
950 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100951
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200952 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100953 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200954 ABORT_NOW();
955 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100956 }
957
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200958 TRACE_DATA("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200959 if (offset < qcs->rx.offset) {
Frédéric Lécaillea18c3332022-07-04 09:54:58 +0200960 size_t diff = qcs->rx.offset - offset;
961
962 len -= diff;
963 data += diff;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200964 offset = qcs->rx.offset;
965 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100966
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200967 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
968 if (ret != NCB_RET_OK) {
969 if (ret == NCB_RET_DATA_REJ) {
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200970 /* RFC 9000 2.2. Sending and Receiving Data
971 *
972 * An endpoint could receive data for a stream at the
973 * same stream offset multiple times. Data that has
974 * already been received can be discarded. The data at
975 * a given offset MUST NOT change if it is sent
976 * multiple times; an endpoint MAY treat receipt of
977 * different data at the same offset within a stream as
978 * a connection error of type PROTOCOL_VIOLATION.
979 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200980 TRACE_ERROR("overlapping data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200981 qcc->conn, qcs);
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200982 qcc_emit_cc(qcc, QC_ERR_PROTOCOL_VIOLATION);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200983 }
984 else if (ret == NCB_RET_GAP_SIZE) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200985 TRACE_DATA("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
986 qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200987 }
988 return 1;
989 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100990
991 if (fin)
Amaury Denoyelle3f39b402022-07-01 16:11:03 +0200992 qcs->flags |= QC_SF_SIZE_KNOWN;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100993
Amaury Denoyelled1310f82022-09-16 13:30:59 +0200994 if (qcs->flags & QC_SF_SIZE_KNOWN &&
995 qcs->rx.offset_max == qcs->rx.offset + ncb_data(&qcs->rx.ncbuf, 0)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200996 qcs_close_remote(qcs);
Amaury Denoyelled1310f82022-09-16 13:30:59 +0200997 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200998
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200999 if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001000 qcc_decode_qcs(qcc, qcs);
Amaury Denoyelle418ba212022-08-02 15:57:16 +02001001 qcc_refresh_timeout(qcc);
1002 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001003
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001004 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001005 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001006 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001007
1008 err:
1009 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1010 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001011}
1012
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001013/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
1014 * the frame.
1015 *
1016 * Returns 0 on success else non-zero.
1017 */
1018int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
1019{
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001020 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1021
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001022 TRACE_PROTO("receiving MAX_DATA", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001023 if (qcc->rfctl.md < max) {
1024 qcc->rfctl.md = max;
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001025 TRACE_DEVEL("increase remote max-data", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001026
1027 if (qcc->flags & QC_CF_BLK_MFCTL) {
1028 qcc->flags &= ~QC_CF_BLK_MFCTL;
1029 tasklet_wakeup(qcc->wait_event.tasklet);
1030 }
1031 }
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001032
1033 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001034 return 0;
1035}
1036
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001037/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
1038 * field of the frame and <id> is the identifier of the QUIC stream.
1039 *
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001040 * Returns 0 on success else non-zero. On error, the received frame should not
1041 * be acknowledged.
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001042 */
1043int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
1044{
1045 struct qcs *qcs;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001046
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001047 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1048
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001049 /* RFC 9000 19.10. MAX_STREAM_DATA Frames
1050 *
1051 * Receiving a MAX_STREAM_DATA frame for a locally
1052 * initiated stream that has not yet been created MUST be treated as a
1053 * connection error of type STREAM_STATE_ERROR. An endpoint that
1054 * receives a MAX_STREAM_DATA frame for a receive-only stream MUST
1055 * terminate the connection with error STREAM_STATE_ERROR.
1056 */
1057 if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) {
1058 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1059 return 1;
1060 }
1061
1062 if (qcs) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001063 TRACE_PROTO("receiving MAX_STREAM_DATA", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001064 if (max > qcs->tx.msd) {
1065 qcs->tx.msd = max;
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001066 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 +01001067
1068 if (qcs->flags & QC_SF_BLK_SFCTL) {
1069 qcs->flags &= ~QC_SF_BLK_SFCTL;
1070 tasklet_wakeup(qcc->wait_event.tasklet);
1071 }
1072 }
1073 }
1074
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001075 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1076 qcc_refresh_timeout(qcc);
1077
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001078 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1079 return 0;
1080}
1081
1082/* Handle a new RESET_STREAM frame from stream ID <id> with error code <err>
1083 * and final stream size <final_size>.
1084 *
1085 * Returns 0 on success else non-zero. On error, the received frame should not
1086 * be acknowledged.
1087 */
1088int qcc_recv_reset_stream(struct qcc *qcc, uint64_t id, uint64_t err, uint64_t final_size)
1089{
1090 struct qcs *qcs;
1091
1092 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1093
1094 /* RFC 9000 19.4. RESET_STREAM Frames
1095 *
1096 * An endpoint that receives a RESET_STREAM frame for a send-only stream
1097 * MUST terminate the connection with error STREAM_STATE_ERROR.
1098 */
1099 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
1100 TRACE_ERROR("RESET_STREAM for send-only stream received", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1101 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
1102 goto err;
1103 }
1104
1105 if (!qcs || qcs_is_close_remote(qcs))
1106 goto out;
1107
1108 TRACE_PROTO("receiving RESET_STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1109 qcs_idle_open(qcs);
1110
1111 if (qcs->rx.offset_max > final_size ||
1112 ((qcs->flags & QC_SF_SIZE_KNOWN) && qcs->rx.offset_max != final_size)) {
1113 TRACE_ERROR("final size error on RESET_STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1114 qcc_emit_cc(qcc, QC_ERR_FINAL_SIZE_ERROR);
1115 goto err;
1116 }
1117
1118 qcs->flags |= QC_SF_SIZE_KNOWN;
1119 qcs_close_remote(qcs);
1120 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
1121
1122 if (qcs_sc(qcs)) {
1123 se_fl_set_error(qcs->sd);
1124 qcs_alert(qcs);
1125 }
1126
1127 out:
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001128 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001129 return 0;
Amaury Denoyelle5854fc02022-12-09 16:25:48 +01001130
1131 err:
1132 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1133 return 1;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001134}
1135
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001136/* Handle a new STOP_SENDING frame for stream ID <id>. The error code should be
1137 * specified in <err>.
1138 *
1139 * Returns 0 on success else non-zero. On error, the received frame should not
1140 * be acknowledged.
1141 */
1142int qcc_recv_stop_sending(struct qcc *qcc, uint64_t id, uint64_t err)
1143{
1144 struct qcs *qcs;
1145
1146 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1147
1148 /* RFC 9000 19.5. STOP_SENDING Frames
1149 *
1150 * Receiving a STOP_SENDING frame for a
1151 * locally initiated stream that has not yet been created MUST be
1152 * treated as a connection error of type STREAM_STATE_ERROR. An
1153 * endpoint that receives a STOP_SENDING frame for a receive-only stream
1154 * MUST terminate the connection with error STREAM_STATE_ERROR.
1155 */
1156 if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) {
1157 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1158 return 1;
1159 }
1160
1161 if (!qcs)
1162 goto out;
1163
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001164 TRACE_PROTO("receiving STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelled7755372022-10-03 17:20:31 +02001165
1166 /* RFC 9000 3.5. Solicited State Transitions
1167 *
1168 * An endpoint is expected to send another STOP_SENDING frame if a
1169 * packet containing a previous STOP_SENDING is lost. However, once
1170 * either all stream data or a RESET_STREAM frame has been received for
1171 * the stream -- that is, the stream is in any state other than "Recv"
1172 * or "Size Known" -- sending a STOP_SENDING frame is unnecessary.
1173 */
1174
1175 /* TODO thanks to previous RFC clause, STOP_SENDING is ignored if current stream
1176 * has already been closed locally. This is useful to not emit multiple
1177 * RESET_STREAM for a single stream. This is functional if stream is
1178 * locally closed due to all data transmitted, but in this case the RFC
1179 * advices to use an explicit RESET_STREAM.
1180 */
1181 if (qcs_is_close_local(qcs)) {
1182 TRACE_STATE("ignoring STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1183 goto out;
1184 }
1185
Amaury Denoyelle96ca1b72022-08-09 17:36:38 +02001186 qcs_idle_open(qcs);
1187
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001188 /* RFC 9000 3.5. Solicited State Transitions
1189 *
1190 * An endpoint that receives a STOP_SENDING frame
1191 * MUST send a RESET_STREAM frame if the stream is in the "Ready" or
1192 * "Send" state. If the stream is in the "Data Sent" state, the
1193 * endpoint MAY defer sending the RESET_STREAM frame until the packets
1194 * containing outstanding data are acknowledged or declared lost. If
1195 * any outstanding data is declared lost, the endpoint SHOULD send a
1196 * RESET_STREAM frame instead of retransmitting the data.
1197 *
1198 * An endpoint SHOULD copy the error code from the STOP_SENDING frame to
1199 * the RESET_STREAM frame it sends, but it can use any application error
1200 * code.
1201 */
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001202 qcc_reset_stream(qcs, err);
1203
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001204 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1205 qcc_refresh_timeout(qcc);
1206
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001207 out:
1208 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1209 return 0;
1210}
1211
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001212/* Signal the closing of remote stream with id <id>. Flow-control for new
1213 * streams may be allocated for the peer if needed.
1214 */
1215static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +01001216{
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001217 struct quic_frame *frm;
1218
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001219 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn);
1220
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001221 if (quic_stream_is_bidi(id)) {
1222 ++qcc->lfctl.cl_bidi_r;
1223 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001224 TRACE_DATA("increase max stream limit with MAX_STREAMS_BIDI", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001225 frm = pool_zalloc(pool_head_quic_frame);
1226 BUG_ON(!frm); /* TODO handle this properly */
1227
1228 LIST_INIT(&frm->reflist);
1229 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
1230 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
1231 qcc->lfctl.cl_bidi_r;
1232 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
1233 tasklet_wakeup(qcc->wait_event.tasklet);
1234
1235 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
1236 qcc->lfctl.cl_bidi_r = 0;
1237 }
1238 }
1239 else {
Amaury Denoyelle91077312022-12-22 18:56:09 +01001240 /* TODO unidirectional stream flow control with MAX_STREAMS_UNI
1241 * emission not implemented. It should be unnecessary for
1242 * HTTP/3 but may be required if other application protocols
1243 * are supported.
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02001244 */
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001245 }
1246
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001247 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
1248
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001249 return 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001250}
1251
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05001252/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001253static void qcs_destroy(struct qcs *qcs)
1254{
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001255 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001256 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001257
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001258 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001259
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001260 if (quic_stream_is_remote(qcs->qcc, id))
1261 qcc_release_remote_stream(qcs->qcc, id);
Amaury Denoyellec055e302022-02-07 16:09:06 +01001262
Amaury Denoyelledccbd732022-03-29 18:36:59 +02001263 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001264
1265 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001266}
1267
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001268/* Transfer as much as possible data on <qcs> from <in> to <out>. This is done
1269 * in respect with available flow-control at stream and connection level.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001270 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001271 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001272 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001273static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, struct buffer *in)
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001274{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001275 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001276 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001277 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001278
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001279 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001280
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001281 qc_get_buf(qcs, out);
1282
1283 /*
1284 * QCS out buffer diagram
1285 * head left to_xfer
1286 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001287 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001288 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001289 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001290 * ^ ack-off ^ sent-off ^ off
1291 *
1292 * STREAM frame
1293 * ^ ^
1294 * |xxxxxxxxxxxxxxxxx|
1295 */
1296
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001297 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001298 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001299 BUG_ON_HOT(qcc->tx.offsets < qcc->tx.sent_offsets);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001300
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001301 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001302 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001303
1304 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
1305 /* do not exceed flow control limit */
1306 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
1307 to_xfer = qcs->tx.msd - qcs->tx.offset;
1308
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001309 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001310 /* do not overcome flow control limit on connection */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001311 if (qcc->tx.offsets + to_xfer > qcc->rfctl.md)
1312 to_xfer = qcc->rfctl.md - qcc->tx.offsets;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001313
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001314 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001315 goto out;
1316
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001317 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001318
1319 out:
1320 {
1321 struct qcs_xfer_data_trace_arg arg = {
1322 .prep = b_data(out), .xfer = total,
1323 };
1324 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
1325 qcc->conn, qcs, &arg);
1326 }
1327
1328 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001329}
1330
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001331/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
1332 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
1333 * stream frame.
1334 *
1335 * Returns the length of the STREAM frame or a negative error code.
1336 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001337static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
1338 struct list *frm_list)
1339{
1340 struct qcc *qcc = qcs->qcc;
1341 struct quic_frame *frm;
1342 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001343 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001344
1345 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1346
Amaury Denoyellea4569202022-04-15 17:29:25 +02001347 /* if ack_offset < buf_offset, it points to an older buffer. */
1348 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
1349 BUG_ON(qcs->tx.sent_offset < base_off);
1350
1351 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001352 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001353 BUG_ON(total < 0);
1354
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001355 if (!total && !fin) {
1356 /* No need to send anything if total is NULL and no FIN to signal. */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001357 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1358 return 0;
1359 }
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001360 BUG_ON((!total && qcs->tx.sent_offset > qcs->tx.offset) ||
1361 (total && qcs->tx.sent_offset >= qcs->tx.offset));
Amaury Denoyellea4569202022-04-15 17:29:25 +02001362 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001363 BUG_ON(qcc->tx.sent_offsets + total > qcc->rfctl.md);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001364
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001365 TRACE_PROTO("sending STREAM frame", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001366 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001367 if (!frm) {
1368 TRACE_ERROR("frame alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001369 goto err;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001370 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001371
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001372 LIST_INIT(&frm->reflist);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001373 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001374 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001375 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001376 frm->stream.buf = out;
1377 frm->stream.data = (unsigned char *)b_peek(out, head);
1378
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +01001379 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001380 if (fin)
1381 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001382
1383 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001384 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001385 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001386 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001387
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001388 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
1389 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001390
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001391 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001392
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001393 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001394 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001395 struct qcs_build_stream_trace_arg arg = {
1396 .len = frm->stream.len, .fin = fin,
1397 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001398 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001399 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001400 qcc->conn, qcs, &arg);
1401 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001402
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001403 return total;
1404
1405 err:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001406 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001407 return -1;
1408}
1409
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001410/* Check after transferring data from qcs.tx.buf if FIN must be set on the next
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001411 * STREAM frame for <qcs>.
1412 *
1413 * Returns true if FIN must be set else false.
1414 */
1415static int qcs_stream_fin(struct qcs *qcs)
1416{
1417 return qcs->flags & QC_SF_FIN_STREAM && !b_data(&qcs->tx.buf);
1418}
1419
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001420/* This function must be called by the upper layer to inform about the sending
1421 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
1422 * <offset>.
1423 */
1424void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
1425{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001426 struct qcc *qcc = qcs->qcc;
1427 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001428
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001429 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1430
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001431 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001432 BUG_ON(offset + data > qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001433
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001434 /* check if the STREAM frame has already been notified. It can happen
1435 * for retransmission.
1436 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001437 if (offset + data < qcs->tx.sent_offset) {
1438 TRACE_DEVEL("offset already notified", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1439 goto out;
1440 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001441
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001442 qcs_idle_open(qcs);
1443
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001444 diff = offset + data - qcs->tx.sent_offset;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001445 if (diff) {
1446 /* increase offset sum on connection */
1447 qcc->tx.sent_offsets += diff;
1448 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
1449 if (qcc->tx.sent_offsets == qcc->rfctl.md)
1450 qcc->flags |= QC_CF_BLK_MFCTL;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001451
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001452 /* increase offset on stream */
1453 qcs->tx.sent_offset += diff;
1454 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
1455 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
1456 if (qcs->tx.sent_offset == qcs->tx.msd)
1457 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001458
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001459 if (qcs->tx.offset == qcs->tx.sent_offset &&
1460 b_full(&qcs->stream->buf->buf)) {
1461 qc_stream_buf_release(qcs->stream);
1462 /* prepare qcs for immediate send retry if data to send */
1463 if (b_data(&qcs->tx.buf))
1464 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
1465 }
1466 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001467
Amaury Denoyelle3dc4e5a2022-09-13 16:49:21 +02001468 if (qcs->tx.offset == qcs->tx.sent_offset && !b_data(&qcs->tx.buf) &&
1469 qcs->flags & (QC_SF_FIN_STREAM|QC_SF_DETACH)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001470 /* Close stream locally. */
1471 qcs_close_local(qcs);
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001472 /* Reset flag to not emit multiple FIN STREAM frames. */
1473 qcs->flags &= ~QC_SF_FIN_STREAM;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001474 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001475
1476 out:
1477 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001478}
1479
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001480/* Wrapper for send on transport layer. Send a list of frames <frms> for the
1481 * connection <qcc>.
1482 *
1483 * Returns 0 if all data sent with success else non-zero.
1484 */
1485static int qc_send_frames(struct qcc *qcc, struct list *frms)
1486{
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001487 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
1488
1489 if (LIST_ISEMPTY(frms)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001490 TRACE_DEVEL("no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
1491 goto err;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001492 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +01001493
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001494 LIST_INIT(&qcc->send_retry_list);
1495
Amaury Denoyelle036cc5d2022-09-26 15:02:31 +02001496 if (!qc_send_mux(qcc->conn->handle.qc, frms))
1497 goto err;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001498
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001499 /* If there is frames left at this stage, transport layer is blocked.
1500 * Subscribe on it to retry later.
1501 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001502 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001503 TRACE_DEVEL("remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001504 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1505 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001506 goto err;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001507 }
1508
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001509 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001510 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001511
1512 err:
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001513 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001514 return 1;
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001515}
1516
1517/* Emit a RESET_STREAM on <qcs>.
1518 *
1519 * Returns 0 if the frame has been successfully sent else non-zero.
1520 */
1521static int qcs_send_reset(struct qcs *qcs)
1522{
1523 struct list frms = LIST_HEAD_INIT(frms);
1524 struct quic_frame *frm;
1525
1526 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1527
1528 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001529 if (!frm) {
1530 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001531 return 1;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001532 }
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001533
1534 LIST_INIT(&frm->reflist);
1535 frm->type = QUIC_FT_RESET_STREAM;
1536 frm->reset_stream.id = qcs->id;
1537 frm->reset_stream.app_error_code = qcs->err;
1538 frm->reset_stream.final_size = qcs->tx.sent_offset;
1539
1540 LIST_APPEND(&frms, &frm->list);
1541 if (qc_send_frames(qcs->qcc, &frms)) {
1542 pool_free(pool_head_quic_frame, frm);
1543 TRACE_DEVEL("cannot send RESET_STREAM", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1544 return 1;
1545 }
1546
1547 if (qcs_sc(qcs)) {
1548 se_fl_set_error(qcs->sd);
1549 qcs_alert(qcs);
1550 }
1551
1552 qcs_close_local(qcs);
1553 qcs->flags &= ~QC_SF_TO_RESET;
1554
1555 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001556 return 0;
1557}
1558
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001559/* Emit a STOP_SENDING on <qcs>.
1560 *
1561 * Returns 0 if the frame has been successfully sent else non-zero.
1562 */
1563static int qcs_send_stop_sending(struct qcs *qcs)
1564{
1565 struct list frms = LIST_HEAD_INIT(frms);
1566 struct quic_frame *frm;
1567 struct qcc *qcc = qcs->qcc;
1568
1569 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1570
1571 /* RFC 9000 3.3. Permitted Frame Types
1572 *
1573 * A
1574 * receiver MAY send a STOP_SENDING frame in any state where it has not
1575 * received a RESET_STREAM frame -- that is, states other than "Reset
1576 * Recvd" or "Reset Read". However, there is little value in sending a
1577 * STOP_SENDING frame in the "Data Recvd" state, as all stream data has
1578 * been received. A sender could receive either of these two types of
1579 * frames in any state as a result of delayed delivery of packets.¶
1580 */
1581 if (qcs_is_close_remote(qcs)) {
1582 TRACE_STATE("skip STOP_SENDING on remote already closed", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1583 goto done;
1584 }
1585
1586 frm = pool_zalloc(pool_head_quic_frame);
1587 if (!frm) {
1588 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1589 return 1;
1590 }
1591
1592 LIST_INIT(&frm->reflist);
1593 frm->type = QUIC_FT_STOP_SENDING;
1594 frm->stop_sending.id = qcs->id;
1595 frm->stop_sending.app_error_code = qcs->err;
1596
1597 LIST_APPEND(&frms, &frm->list);
1598 if (qc_send_frames(qcs->qcc, &frms)) {
1599 pool_free(pool_head_quic_frame, frm);
1600 TRACE_DEVEL("cannot send STOP_SENDING", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1601 return 1;
1602 }
1603
1604 done:
1605 qcs->flags &= ~QC_SF_TO_STOP_SENDING;
1606
1607 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1608 return 0;
1609}
1610
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001611/* Used internally by qc_send function. Proceed to send for <qcs>. This will
1612 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001613 * is then generated and inserted in <frms> list.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001614 *
1615 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
1616 * be null if out buffer cannot be allocated.
1617 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001618static int _qc_send_qcs(struct qcs *qcs, struct list *frms)
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001619{
1620 struct qcc *qcc = qcs->qcc;
1621 struct buffer *buf = &qcs->tx.buf;
1622 struct buffer *out = qc_stream_buf_get(qcs->stream);
1623 int xfer = 0;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001624 char fin = 0;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001625
1626 /* Allocate <out> buffer if necessary. */
1627 if (!out) {
1628 if (qcc->flags & QC_CF_CONN_FULL)
1629 return 0;
1630
1631 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
1632 if (!out) {
1633 qcc->flags |= QC_CF_CONN_FULL;
1634 return 0;
1635 }
1636 }
1637
1638 /* Transfer data from <buf> to <out>. */
1639 if (b_data(buf)) {
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001640 xfer = qcs_xfer_data(qcs, out, buf);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001641 if (xfer > 0) {
1642 qcs_notify_send(qcs);
1643 qcs->flags &= ~QC_SF_BLK_MROOM;
1644 }
1645
1646 qcs->tx.offset += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001647 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001648 qcc->tx.offsets += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001649 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001650 }
1651
1652 /* out buffer cannot be emptied if qcs offsets differ. */
1653 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1654
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001655 /* FIN is set if all incoming data were transferred. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001656 fin = qcs_stream_fin(qcs);
1657
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001658 /* Build a new STREAM frame with <out> buffer. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001659 if (qcs->tx.sent_offset != qcs->tx.offset || fin) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001660 int ret;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001661 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +02001662 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001663 }
1664
1665 return xfer;
1666}
1667
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001668/* Proceed to sending. Loop through all available streams for the <qcc>
1669 * instance and try to send as much as possible.
1670 *
1671 * Returns the total of bytes sent to the transport layer.
1672 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001673static int qc_send(struct qcc *qcc)
1674{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001675 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001676 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001677 struct qcs *qcs, *qcs_tmp;
1678 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001679
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001680 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001681
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001682 if (qcc->conn->flags & CO_FL_SOCK_WR_SH || qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001683 qcc->conn->flags |= CO_FL_ERROR;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001684 TRACE_DEVEL("connection on error", QMUX_EV_QCC_SEND, qcc->conn);
1685 goto err;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001686 }
1687
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001688 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1689 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
1690 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
1691 goto out;
1692 }
1693 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02001694
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001695 if (qcc->flags & QC_CF_BLK_MFCTL)
1696 return 0;
1697
Frédéric Lécaille3dd79d32022-09-08 17:53:36 +02001698 if (!(qcc->flags & QC_CF_APP_FINAL) && !eb_is_empty(&qcc->streams_by_id) &&
1699 qcc->app_ops->finalize) {
1700 /* Finalize the application layer before sending any stream.
1701 * For h3 this consists in preparing the control stream data (SETTINGS h3).
1702 */
1703 qcc->app_ops->finalize(qcc->ctx);
1704 qcc->flags |= QC_CF_APP_FINAL;
1705 }
1706
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001707 /* loop through all streams, construct STREAM frames if data available.
1708 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001709 */
1710 node = eb64_first(&qcc->streams_by_id);
1711 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001712 int ret;
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001713 uint64_t id;
1714
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001715 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001716 id = qcs->id;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001717
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001718 if (quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001719 node = eb64_next(node);
1720 continue;
1721 }
1722
Amaury Denoyelle663e8722022-12-09 14:58:28 +01001723 if (qcs->flags & QC_SF_TO_STOP_SENDING)
1724 qcs_send_stop_sending(qcs);
1725
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001726 if (qcs->flags & QC_SF_TO_RESET) {
1727 qcs_send_reset(qcs);
1728 node = eb64_next(node);
1729 continue;
1730 }
1731
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001732 if (qcs_is_close_local(qcs)) {
1733 node = eb64_next(node);
1734 continue;
1735 }
1736
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001737 if (qcs->flags & QC_SF_BLK_SFCTL) {
1738 node = eb64_next(node);
1739 continue;
1740 }
1741
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001742 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001743 node = eb64_next(node);
1744 continue;
1745 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001746
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001747 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001748 total += ret;
1749 node = eb64_next(node);
1750 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001751
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001752 if (qc_send_frames(qcc, &frms)) {
1753 /* data rejected by transport layer, do not retry. */
1754 goto out;
1755 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001756
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001757 retry:
1758 tmp_total = 0;
1759 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
1760 int ret;
1761 BUG_ON(!b_data(&qcs->tx.buf));
1762 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001763
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001764 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001765 tmp_total += ret;
1766 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001767 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001768
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001769 total += tmp_total;
1770 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
1771 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +01001772
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001773 out:
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02001774 /* Deallocate frames that the transport layer has rejected. */
1775 if (!LIST_ISEMPTY(&frms)) {
1776 struct quic_frame *frm, *frm2;
1777 list_for_each_entry_safe(frm, frm2, &frms, list) {
1778 LIST_DELETE(&frm->list);
1779 pool_free(pool_head_quic_frame, frm);
1780 }
1781 }
1782
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001783 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001784 return total;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001785
1786 err:
1787 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
1788 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001789}
1790
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001791/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1792 * operation.
1793 *
1794 * Returns 0 on success else non-zero.
1795 */
1796static int qc_recv(struct qcc *qcc)
1797{
1798 struct eb64_node *node;
1799 struct qcs *qcs;
1800
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001801 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001802
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001803 if (qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001804 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001805 return 0;
1806 }
1807
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001808 node = eb64_first(&qcc->streams_by_id);
1809 while (node) {
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001810 uint64_t id;
1811
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001812 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001813 id = qcs->id;
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001814
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001815 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001816 node = eb64_next(node);
1817 continue;
1818 }
1819
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001820 if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001821 node = eb64_next(node);
1822 continue;
1823 }
1824
1825 qcc_decode_qcs(qcc, qcs);
1826 node = eb64_next(node);
1827 }
1828
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001829 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001830 return 0;
1831}
1832
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001833
1834/* Release all streams which have their transfer operation achieved.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001835 *
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001836 * Returns true if at least one stream is released.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001837 */
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001838static int qc_purge_streams(struct qcc *qcc)
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001839{
1840 struct eb64_node *node;
1841 int release = 0;
1842
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001843 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001844
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001845 node = eb64_first(&qcc->streams_by_id);
1846 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001847 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001848 node = eb64_next(node);
1849
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001850 /* Release not attached closed streams. */
1851 if (qcs->st == QC_SS_CLO && !qcs_sc(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001852 TRACE_STATE("purging closed stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001853 qcs_destroy(qcs);
1854 release = 1;
1855 continue;
1856 }
1857
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001858 /* Release detached streams with empty buffer. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001859 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02001860 if (qcs_is_close_local(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001861 TRACE_STATE("purging detached stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001862 qcs_destroy(qcs);
1863 release = 1;
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001864 continue;
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001865 }
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001866
1867 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1868 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001869 }
1870 }
1871
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001872 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001873 return release;
1874}
1875
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001876/* release function. This one should be called to free all resources allocated
1877 * to the mux.
1878 */
1879static void qc_release(struct qcc *qcc)
1880{
1881 struct connection *conn = qcc->conn;
1882 struct eb64_node *node;
1883
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001884 TRACE_ENTER(QMUX_EV_QCC_END, conn);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001885
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001886 if (qcc->app_ops && qcc->app_ops->shutdown) {
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001887 /* Application protocol with dedicated connection closing
1888 * procedure.
1889 */
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001890 qcc->app_ops->shutdown(qcc->ctx);
Amaury Denoyellea154dc02022-06-13 17:09:01 +02001891
1892 /* useful if application protocol should emit some closing
1893 * frames. For example HTTP/3 GOAWAY frame.
1894 */
1895 qc_send(qcc);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001896 }
1897 else {
1898 qcc_emit_cc_app(qcc, QC_ERR_NO_ERROR, 0);
1899 }
1900
1901 if (qcc->task) {
1902 task_destroy(qcc->task);
1903 qcc->task = NULL;
1904 }
1905
1906 if (qcc->wait_event.tasklet)
1907 tasklet_free(qcc->wait_event.tasklet);
1908 if (conn && qcc->wait_event.events) {
1909 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
1910 qcc->wait_event.events,
1911 &qcc->wait_event);
1912 }
1913
1914 /* liberate remaining qcs instances */
1915 node = eb64_first(&qcc->streams_by_id);
1916 while (node) {
1917 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
1918 node = eb64_next(node);
1919 qcs_free(qcs);
1920 }
1921
1922 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1923 struct quic_frame *frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list);
1924 LIST_DELETE(&frm->list);
1925 pool_free(pool_head_quic_frame, frm);
1926 }
1927
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001928 if (qcc->app_ops && qcc->app_ops->release)
1929 qcc->app_ops->release(qcc->ctx);
1930 TRACE_PROTO("application layer released", QMUX_EV_QCC_END, conn);
1931
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001932 pool_free(pool_head_qcc, qcc);
1933
1934 if (conn) {
1935 LIST_DEL_INIT(&conn->stopping_list);
1936
1937 conn->handle.qc->conn = NULL;
1938 conn->mux = NULL;
1939 conn->ctx = NULL;
1940
1941 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
1942
1943 conn_stop_tracking(conn);
1944 conn_full_close(conn);
1945 if (conn->destroy_cb)
1946 conn->destroy_cb(conn);
1947 conn_free(conn);
1948 }
1949
1950 TRACE_LEAVE(QMUX_EV_QCC_END);
1951}
1952
Willy Tarreau41e701e2022-09-08 15:12:59 +02001953struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001954{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001955 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001956
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001957 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001958
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001959 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001960
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001961 if (qc_purge_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001962 if (qcc_is_dead(qcc)) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001963 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001964 goto release;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001965 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001966 }
1967
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001968 qc_recv(qcc);
1969
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02001970 /* TODO check if qcc proxy is disabled. If yes, use graceful shutdown
1971 * to close the connection.
1972 */
1973
1974 qcc_refresh_timeout(qcc);
1975
Amaury Denoyelled3973852022-07-25 14:56:54 +02001976 end:
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001977 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
1978 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001979
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001980 release:
1981 qc_release(qcc);
1982 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001983 return NULL;
1984}
1985
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001986static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1987{
1988 struct qcc *qcc = ctx;
1989 int expired = tick_is_expired(t->expire, now_ms);
1990
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001991 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001992
1993 if (qcc) {
1994 if (!expired) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001995 TRACE_DEVEL("not expired", QMUX_EV_QCC_WAKE, qcc->conn);
1996 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001997 }
1998
1999 if (!qcc_may_expire(qcc)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002000 TRACE_DEVEL("cannot expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002001 t->expire = TICK_ETERNITY;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002002 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002003 }
2004 }
2005
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002006 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01002007
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002008 if (!qcc) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002009 TRACE_DEVEL("no more qcc", QMUX_EV_QCC_WAKE);
2010 goto out;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002011 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01002012
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002013 qcc->task = NULL;
2014
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002015 /* TODO depending on the timeout condition, different shutdown mode
2016 * should be used. For http keep-alive or disabled proxy, a graceful
2017 * shutdown should occurs. For all other cases, an immediate close
2018 * seems legitimate.
2019 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002020 if (qcc_is_dead(qcc)) {
2021 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002022 qc_release(qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002023 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002024
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002025 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002026 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002027 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002028
2029 requeue:
2030 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
2031 return t;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002032}
2033
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002034static int qc_init(struct connection *conn, struct proxy *prx,
2035 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002036{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002037 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002038 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002039
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002040 TRACE_ENTER(QMUX_EV_QCC_NEW);
2041
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002042 qcc = pool_alloc(pool_head_qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002043 if (!qcc) {
2044 TRACE_ERROR("alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002045 goto fail_no_qcc;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002046 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002047
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002048 qcc->conn = conn;
2049 conn->ctx = qcc;
Amaury Denoyellec603de42022-07-25 11:21:46 +02002050 qcc->nb_hreq = qcc->nb_sc = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01002051 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002052
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002053 qcc->app_ops = NULL;
Amaury Denoyelle0aba11e2022-09-29 18:31:24 +02002054 if (qcc_install_app_ops(qcc, conn->handle.qc->app_ops)) {
2055 TRACE_PROTO("Cannot install app layer", QMUX_EV_QCC_NEW, qcc->conn);
2056 /* prepare a CONNECTION_CLOSE frame */
2057 quic_set_connection_close(conn->handle.qc, quic_err_transport(QC_ERR_APPLICATION_ERROR));
2058 goto fail_no_tasklet;
2059 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002060
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002061 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002062
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002063 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02002064 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002065
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002066 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02002067 qcc->tx.sent_offsets = qcc->tx.offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002068
2069 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002070 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002071 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002072 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002073 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002074
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002075 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002076 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002077 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002078 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002079
2080 /* Server initiated streams must respect the server flow control. */
2081 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002082 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002083 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002084 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
2085
2086 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002087 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01002088 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01002089 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002090
Amaury Denoyellec985cb12022-05-16 14:29:59 +02002091 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01002092 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02002093 qcc->lfctl.ms_uni = lparams->initial_max_streams_uni;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02002094 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
2095 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle176174f2022-10-21 17:02:18 +02002096 qcc->lfctl.msd_uni_r = lparams->initial_max_stream_data_uni;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01002097 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01002098
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02002099 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02002100 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02002101
Willy Tarreau784b8682022-04-11 14:18:10 +02002102 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01002103 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002104 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
2105 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle176174f2022-10-21 17:02:18 +02002106 qcc->rfctl.msd_uni_l = rparams->initial_max_stream_data_uni;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002107
Amaury Denoyellea509ffb2022-07-04 15:50:33 +02002108 if (conn_is_back(conn)) {
2109 qcc->next_bidi_l = 0x00;
2110 qcc->largest_bidi_r = 0x01;
2111 qcc->next_uni_l = 0x02;
2112 qcc->largest_uni_r = 0x03;
2113 }
2114 else {
2115 qcc->largest_bidi_r = 0x00;
2116 qcc->next_bidi_l = 0x01;
2117 qcc->largest_uni_r = 0x02;
2118 qcc->next_uni_l = 0x03;
2119 }
2120
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002121 qcc->wait_event.tasklet = tasklet_new();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002122 if (!qcc->wait_event.tasklet) {
2123 TRACE_ERROR("taslket alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002124 goto fail_no_tasklet;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002125 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002126
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02002127 LIST_INIT(&qcc->send_retry_list);
2128
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002129 qcc->wait_event.tasklet->process = qc_io_cb;
2130 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01002131 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002132
Amaury Denoyelle07bf8f42022-07-22 16:16:03 +02002133 qcc->proxy = prx;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002134 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002135 qcc->task = NULL;
Amaury Denoyelleb6309452022-07-25 14:51:56 +02002136 qcc->timeout = conn_is_back(qcc->conn) ? prx->timeout.server :
2137 prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002138 if (tick_isset(qcc->timeout)) {
2139 qcc->task = task_new_here();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002140 if (!qcc->task) {
2141 TRACE_ERROR("timeout task alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002142 goto fail_no_timeout_task;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002143 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002144 qcc->task->process = qc_timeout_task;
2145 qcc->task->context = qcc;
2146 qcc->task->expire = tick_add(now_ms, qcc->timeout);
2147 }
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +02002148 qcc_reset_idle_start(qcc);
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02002149 LIST_INIT(&qcc->opening_list);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002150
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002151 if (!conn_is_back(conn)) {
2152 if (!LIST_INLIST(&conn->stopping_list)) {
2153 LIST_APPEND(&mux_stopping_data[tid].list,
2154 &conn->stopping_list);
2155 }
2156 }
2157
Willy Tarreau784b8682022-04-11 14:18:10 +02002158 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002159 /* init read cycle */
2160 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002161
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002162 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002163 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002164
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002165 fail_no_timeout_task:
2166 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002167 fail_no_tasklet:
Amaury Denoyelle0aba11e2022-09-29 18:31:24 +02002168 if (qcc->app_ops && qcc->app_ops->release)
2169 qcc->app_ops->release(qcc->ctx);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002170 pool_free(pool_head_qcc, qcc);
2171 fail_no_qcc:
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002172 TRACE_LEAVE(QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002173 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002174}
2175
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002176static void qc_destroy(void *ctx)
2177{
2178 struct qcc *qcc = ctx;
2179
2180 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
2181 qc_release(qcc);
2182 TRACE_LEAVE(QMUX_EV_QCC_END);
2183}
2184
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002185static void qc_detach(struct sedesc *sd)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002186{
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002187 struct qcs *qcs = sd->se;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002188 struct qcc *qcc = qcs->qcc;
2189
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002190 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002191
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002192 /* TODO this BUG_ON_HOT() is not correct as the stconn layer may detach
2193 * from the stream even if it is not closed remotely at the QUIC layer.
2194 * This happens for example when a stream must be closed due to a
2195 * rejected request. To better handle these cases, it will be required
2196 * to implement shutr/shutw MUX operations. Once this is done, this
2197 * BUG_ON_HOT() statement can be adjusted.
2198 */
2199 //BUG_ON_HOT(!qcs_is_close_remote(qcs));
Amaury Denoyellec603de42022-07-25 11:21:46 +02002200
2201 qcc_rm_sc(qcc);
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002202
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02002203 if (!qcs_is_close_local(qcs) && !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002204 TRACE_STATE("remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002205 qcs->flags |= QC_SF_DETACH;
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002206 qcc_refresh_timeout(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002207
2208 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002209 return;
2210 }
2211
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002212 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01002213
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002214 if (qcc_is_dead(qcc)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002215 TRACE_STATE("killing dead connection", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002216 goto release;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002217 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002218 else if (qcc->task) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002219 TRACE_DEVEL("refreshing connection's timeout", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002220 qcc_refresh_timeout(qcc);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002221 }
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002222 else {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002223 TRACE_DEVEL("completed", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002224 }
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002225
2226 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002227 return;
2228
2229 release:
2230 qc_release(qcc);
2231 TRACE_LEAVE(QMUX_EV_STRM_END);
2232 return;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002233}
2234
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002235/* Called from the upper layer, to receive data */
Amaury Denoyelleb7ce79c2022-11-24 10:51:19 +01002236static size_t qc_recv_buf(struct stconn *sc, struct buffer *buf,
2237 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002238{
Willy Tarreau3215e732022-05-27 10:09:11 +02002239 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002240 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002241 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002242
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002243 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002244
Amaury Denoyelled80fbca2022-09-19 17:02:28 +02002245 ret = qcs_http_rcv_buf(qcs, buf, count, &fin);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002246
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002247 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002248 se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002249 }
2250 else {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002251 se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
2252 if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING))
2253 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002254
Amaury Denoyelle72a78e82022-07-29 15:34:12 +02002255 /* Set end-of-input if FIN received and all data extracted. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002256 if (fin)
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002257 se_fl_set(qcs->sd, SE_FL_EOI);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002258
2259 if (b_size(&qcs->rx.app_buf)) {
2260 b_free(&qcs->rx.app_buf);
2261 offer_buffers(NULL, 1);
2262 }
2263 }
2264
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002265 if (ret) {
2266 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002267 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002268 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002269
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002270 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
2271
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002272 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002273}
2274
Amaury Denoyelleb7ce79c2022-11-24 10:51:19 +01002275static size_t qc_send_buf(struct stconn *sc, struct buffer *buf,
2276 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002277{
Willy Tarreau3215e732022-05-27 10:09:11 +02002278 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002279 size_t ret;
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002280 char fin;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002281
2282 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002283
Amaury Denoyelle3dc4e5a2022-09-13 16:49:21 +02002284 /* stream layer has been detached so no transfer must occur after. */
2285 BUG_ON_HOT(qcs->flags & QC_SF_DETACH);
2286
Amaury Denoyelle843a1192022-07-04 11:44:38 +02002287 if (qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_RESET)) {
Amaury Denoyelle0ed617a2022-09-20 14:46:40 +02002288 ret = qcs_http_reset_buf(qcs, buf, count);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002289 goto end;
2290 }
2291
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002292 ret = qcs_http_snd_buf(qcs, buf, count, &fin);
2293 if (fin)
2294 qcs->flags |= QC_SF_FIN_STREAM;
2295
2296 if (ret) {
2297 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
2298 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
2299 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002300
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002301 end:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002302 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
2303
2304 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002305}
2306
2307/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2308 * event subscriber <es> is not allowed to change from a previous call as long
2309 * as at least one event is still subscribed. The <event_type> must only be a
2310 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
2311 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002312static int qc_subscribe(struct stconn *sc, int event_type,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002313 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002314{
Willy Tarreau3215e732022-05-27 10:09:11 +02002315 return qcs_subscribe(__sc_mux_strm(sc), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002316}
2317
2318/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
2319 * The <es> pointer is not allowed to differ from the one passed to the
2320 * subscribe() call. It always returns zero.
2321 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002322static int qc_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002323{
Willy Tarreau3215e732022-05-27 10:09:11 +02002324 struct qcs *qcs = __sc_mux_strm(sc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002325
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002326 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
2327 BUG_ON(qcs->subs && qcs->subs != es);
2328
2329 es->events &= ~event_type;
2330 if (!es->events)
2331 qcs->subs = NULL;
2332
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002333 return 0;
2334}
2335
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002336/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Willy Tarreau4596fe22022-05-17 19:07:51 +02002337 * report SE_FL_ERR_PENDING|SE_FL_ERROR on the attached stream connectors and
2338 * wake them.
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002339 */
2340static int qc_wake_some_streams(struct qcc *qcc)
2341{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002342 struct qcs *qcs;
2343 struct eb64_node *node;
2344
2345 for (node = eb64_first(&qcc->streams_by_id); node;
2346 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02002347 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002348
Amaury Denoyelle3abeb572022-07-04 11:42:27 +02002349 if (!qcs_sc(qcs))
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002350 continue;
2351
2352 if (qcc->conn->flags & CO_FL_ERROR) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002353 se_fl_set(qcs->sd, SE_FL_ERR_PENDING);
2354 if (se_fl_test(qcs->sd, SE_FL_EOS))
2355 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002356
Amaury Denoyelle4561f842022-07-06 14:54:34 +02002357 qcs_alert(qcs);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002358 }
2359 }
2360
2361 return 0;
2362}
2363
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002364static int qc_wake(struct connection *conn)
2365{
2366 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02002367 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002368
2369 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002370
2371 /* Check if a soft-stop is in progress.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002372 *
cui flitera94bedc2022-08-29 14:42:57 +08002373 * TODO this is relevant for frontend connections only.
Amaury Denoyelled0c62322022-05-23 08:52:58 +02002374 *
2375 * TODO Client should be notified with a H3 GOAWAY and then a
2376 * CONNECTION_CLOSE. However, quic-conn uses the listener socket for
2377 * sending which at this stage is already closed.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002378 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002379 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Amaury Denoyelled0c62322022-05-23 08:52:58 +02002380 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002381
Willy Tarreau784b8682022-04-11 14:18:10 +02002382 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002383 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
2384
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002385 qc_send(qcc);
2386
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002387 qc_wake_some_streams(qcc);
2388
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002389 if (qcc_is_dead(qcc))
2390 goto release;
2391
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002392 qcc_refresh_timeout(qcc);
2393
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002394 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002395 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002396
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002397 release:
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002398 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002399 qc_release(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002400 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002401 return 1;
2402}
2403
Amaury Denoyellea473f192022-12-21 10:21:58 +01002404static void qc_shutw(struct stconn *sc, enum co_shw_mode mode)
2405{
2406 struct qcs *qcs = __sc_mux_strm(sc);
2407
2408 TRACE_ENTER(QMUX_EV_STRM_SHUT, qcs->qcc->conn, qcs);
2409
2410 /* If QC_SF_FIN_STREAM is not set and stream is not closed locally, it
2411 * means that upper layer reported an early closure. A RESET_STREAM is
2412 * necessary if not already scheduled.
2413 */
2414
2415 if (!qcs_is_close_local(qcs) &&
2416 !(qcs->flags & (QC_SF_FIN_STREAM|QC_SF_TO_RESET))) {
2417 qcc_reset_stream(qcs, 0);
2418 se_fl_set_error(qcs->sd);
2419 }
2420
2421 TRACE_LEAVE(QMUX_EV_STRM_SHUT, qcs->qcc->conn, qcs);
2422}
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002423
Willy Tarreaub4a4fee2022-09-02 16:00:40 +02002424/* for debugging with CLI's "show sess" command. May emit multiple lines, each
2425 * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single
2426 * line is used. Each field starts with a space so it's safe to print it after
2427 * existing fields.
2428 */
2429static int qc_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx)
2430{
2431 struct qcs *qcs = sd->se;
2432 struct qcc *qcc;
2433 int ret = 0;
2434
2435 if (!qcs)
2436 return ret;
2437
2438 chunk_appendf(msg, " qcs=%p .flg=%#x .id=%llu .st=%s .ctx=%p, .err=%#llx",
2439 qcs, qcs->flags, (ull)qcs->id, qcs_st_to_str(qcs->st), qcs->ctx, (ull)qcs->err);
2440
2441 if (pfx)
2442 chunk_appendf(msg, "\n%s", pfx);
2443
2444 qcc = qcs->qcc;
2445 chunk_appendf(msg, " qcc=%p .flg=%#x .nbsc=%llu .nbhreq=%llu, .task=%p",
2446 qcc, qcc->flags, (ull)qcc->nb_sc, (ull)qcc->nb_hreq, qcc->task);
2447 return ret;
2448}
2449
2450
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002451static const struct mux_ops qc_ops = {
2452 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002453 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002454 .detach = qc_detach,
Amaury Denoyelleb7ce79c2022-11-24 10:51:19 +01002455 .rcv_buf = qc_recv_buf,
2456 .snd_buf = qc_send_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002457 .subscribe = qc_subscribe,
2458 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002459 .wake = qc_wake,
Amaury Denoyellea473f192022-12-21 10:21:58 +01002460 .shutw = qc_shutw,
Willy Tarreaub4a4fee2022-09-02 16:00:40 +02002461 .show_sd = qc_show_sd,
Willy Tarreaub5821e12022-04-26 11:54:08 +02002462 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02002463 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002464};
2465
2466static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002467 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002468
2469INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);