blob: d4851570c36e6a8100c79ad2384d8cd848159a51 [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 Denoyelle9a327a72022-02-14 17:11:09 +01008#include <haproxy/htx.h>
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02009#include <haproxy/list.h>
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +020010#include <haproxy/ncbuf.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +010011#include <haproxy/pool.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020012#include <haproxy/quic_stream.h>
Frédéric Lécaille748ece62022-05-21 23:58:40 +020013#include <haproxy/quic_tp-t.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020014#include <haproxy/ssl_sock-t.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020015#include <haproxy/stconn.h>
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010016#include <haproxy/trace.h>
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +010017#include <haproxy/xprt_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010018
Amaury Denoyelledeed7772021-12-03 11:36:46 +010019DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010020DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
21
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010022/* trace source and events */
23static void qmux_trace(enum trace_level level, uint64_t mask,
24 const struct trace_source *src,
25 const struct ist where, const struct ist func,
26 const void *a1, const void *a2, const void *a3, const void *a4);
27
28static const struct trace_event qmux_trace_events[] = {
Amaury Denoyelle4f137572022-03-24 17:10:00 +010029#define QMUX_EV_QCC_RECV (1ULL << 1)
30 { .mask = QMUX_EV_QCC_RECV, .name = "qcc_recv", .desc = "Rx on QUIC connection" },
31#define QMUX_EV_QCC_SEND (1ULL << 2)
32 { .mask = QMUX_EV_QCC_SEND, .name = "qcc_send", .desc = "Tx on QUIC connection" },
33#define QMUX_EV_QCC_WAKE (1ULL << 3)
34 { .mask = QMUX_EV_QCC_WAKE, .name = "qcc_wake", .desc = "QUIC connection woken up" },
35#define QMUX_EV_QCC_END (1ULL << 4)
36 { .mask = QMUX_EV_QCC_END, .name = "qcc_end", .desc = "QUIC connection terminated" },
37#define QMUX_EV_QCC_NQCS (1ULL << 5)
38 { .mask = QMUX_EV_QCC_NQCS, .name = "qcc_no_qcs", .desc = "QUIC stream not found" },
39#define QMUX_EV_QCS_NEW (1ULL << 6)
40 { .mask = QMUX_EV_QCS_NEW, .name = "qcs_new", .desc = "new QUIC stream" },
41#define QMUX_EV_QCS_RECV (1ULL << 7)
42 { .mask = QMUX_EV_QCS_RECV, .name = "qcs_recv", .desc = "Rx on QUIC stream" },
43#define QMUX_EV_QCS_SEND (1ULL << 8)
44 { .mask = QMUX_EV_QCS_SEND, .name = "qcs_send", .desc = "Tx on QUIC stream" },
45#define QMUX_EV_QCS_END (1ULL << 9)
46 { .mask = QMUX_EV_QCS_END, .name = "qcs_end", .desc = "QUIC stream terminated" },
47#define QMUX_EV_STRM_RECV (1ULL << 10)
48 { .mask = QMUX_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
49#define QMUX_EV_STRM_SEND (1ULL << 11)
50 { .mask = QMUX_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
51#define QMUX_EV_STRM_END (1ULL << 12)
52 { .mask = QMUX_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
Amaury Denoyellefa29f332022-03-25 09:09:40 +010053#define QMUX_EV_SEND_FRM (1ULL << 13)
54 { .mask = QMUX_EV_SEND_FRM, .name = "send_frm", .desc = "sending QUIC frame" },
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020055/* special event dedicated to qcs_xfer_data */
56#define QMUX_EV_QCS_XFER_DATA (1ULL << 14)
57 { .mask = QMUX_EV_QCS_XFER_DATA, .name = "qcs_xfer_data", .desc = "qcs_xfer_data" },
58/* special event dedicated to qcs_build_stream_frm */
59#define QMUX_EV_QCS_BUILD_STRM (1ULL << 15)
60 { .mask = QMUX_EV_QCS_BUILD_STRM, .name = "qcs_build_stream_frm", .desc = "qcs_build_stream_frm" },
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010061 { }
62};
63
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020064/* custom arg for QMUX_EV_QCS_XFER_DATA */
65struct qcs_xfer_data_trace_arg {
66 size_t prep;
Amaury Denoyellefdcec362022-03-25 09:28:10 +010067 int xfer;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020068};
69/* custom arg for QMUX_EV_QCS_BUILD_STRM */
70struct qcs_build_stream_trace_arg {
71 size_t len;
Amaury Denoyellefdcec362022-03-25 09:28:10 +010072 char fin;
73 uint64_t offset;
74};
75
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010076static const struct name_desc qmux_trace_lockon_args[4] = {
77 /* arg1 */ { /* already used by the connection */ },
78 /* arg2 */ { .name="qcs", .desc="QUIC stream" },
79 /* arg3 */ { },
80 /* arg4 */ { }
81};
82
83static const struct name_desc qmux_trace_decoding[] = {
84#define QMUX_VERB_CLEAN 1
85 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
86#define QMUX_VERB_MINIMAL 2
87 { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" },
88 { /* end */ }
89};
90
91struct trace_source trace_qmux = {
92 .name = IST("qmux"),
93 .desc = "QUIC multiplexer",
94 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
95 .default_cb = qmux_trace,
96 .known_events = qmux_trace_events,
97 .lockon_args = qmux_trace_lockon_args,
98 .decoding = qmux_trace_decoding,
99 .report_events = ~0, /* report everything by default */
100};
101
102#define TRACE_SOURCE &trace_qmux
103INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
104
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200105/* Emit a CONNECTION_CLOSE with error <err>. This will interrupt all future
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200106 * send/receive operations.
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200107 */
108static void qcc_emit_cc(struct qcc *qcc, int err)
109{
Amaury Denoyelle57e6db72022-07-13 15:07:56 +0200110 quic_set_connection_close(qcc->conn->handle.qc, quic_err_transport(err));
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200111 qcc->flags |= QC_CF_CC_EMIT;
112 tasklet_wakeup(qcc->wait_event.tasklet);
113}
114
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100115/* Allocate a new QUIC streams with id <id> and type <type>. */
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200116static struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100117{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100118 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100119
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100120 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
121
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100122 qcs = pool_alloc(pool_head_qcs);
123 if (!qcs)
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200124 return NULL;
125
126 qcs->stream = NULL;
127 qcs->qcc = qcc;
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +0200128 qcs->sd = NULL;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200129 qcs->flags = QC_SF_NONE;
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200130 qcs->st = QC_SS_IDLE;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200131 qcs->ctx = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100132
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200133 /* Allocate transport layer stream descriptor. Only needed for TX. */
134 if (!quic_stream_is_uni(id) || !quic_stream_is_remote(qcc, id)) {
135 struct quic_conn *qc = qcc->conn->handle.qc;
136 qcs->stream = qc_stream_desc_new(id, type, qcs, qc);
137 if (!qcs->stream)
138 goto err;
139 }
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200140
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +0200141 qcs->id = qcs->by_id.key = id;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200142 if (qcc->app_ops->attach) {
Amaury Denoyellec0156792022-06-03 15:29:07 +0200143 if (qcc->app_ops->attach(qcs, qcc->ctx))
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200144 goto err;
145 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100146
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200147 /* store transport layer stream descriptor in qcc tree */
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200148 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100149
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100150 qcc->strms[type].nb_streams++;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100151
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100152 /* If stream is local, use peer remote-limit, or else the opposite. */
153 /* TODO use uni limit for unidirectional streams */
154 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
155 qcc->rfctl.msd_bidi_l;
156
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200157 qcs->rx.ncbuf = NCBUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100158 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200159 qcs->rx.offset = qcs->rx.offset_max = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100160
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200161 /* TODO use uni limit for unidirectional streams */
162 qcs->rx.msd = quic_stream_is_local(qcc, id) ? qcc->lfctl.msd_bidi_l :
163 qcc->lfctl.msd_bidi_r;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200164 qcs->rx.msd_init = qcs->rx.msd;
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200165
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100166 qcs->tx.buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100167 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100168 qcs->tx.sent_offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100169
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100170 qcs->wait_event.tasklet = NULL;
171 qcs->wait_event.events = 0;
172 qcs->subs = NULL;
173
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200174 qcs->err = 0;
175
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100176 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100177 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100178 return qcs;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200179
180 err:
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200181 if (qcs->ctx && qcc->app_ops->detach)
182 qcc->app_ops->detach(qcs);
183
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200184 qc_stream_desc_release(qcs->stream);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200185 pool_free(pool_head_qcs, qcs);
186 return NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100187}
188
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200189static void qc_free_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
190{
191 struct buffer buf;
192
Amaury Denoyelle7313f5e2022-05-17 18:53:21 +0200193 if (ncb_is_null(ncbuf))
194 return;
195
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200196 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
197 b_free(&buf);
Amaury Denoyelle7313f5e2022-05-17 18:53:21 +0200198 offer_buffers(NULL, 1);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200199
200 *ncbuf = NCBUF_NULL;
201}
202
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200203/* Free a qcs. This function must only be done to remove a stream on allocation
204 * error or connection shutdown. Else use qcs_destroy which handle all the
205 * QUIC connection mechanism.
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100206 */
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200207static void qcs_free(struct qcs *qcs)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100208{
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200209 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200210 b_free(&qcs->tx.buf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200211
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200212 BUG_ON(!qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams);
213 --qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams;
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200214
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200215 if (qcs->ctx && qcs->qcc->app_ops->detach)
216 qcs->qcc->app_ops->detach(qcs);
217
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200218 qc_stream_desc_release(qcs->stream);
219
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +0200220 BUG_ON(qcs->sd && !se_fl_test(qcs->sd, SE_FL_ORPHAN));
221 sedesc_free(qcs->sd);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200222
223 eb64_delete(&qcs->by_id);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100224 pool_free(pool_head_qcs, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100225}
226
Amaury Denoyelle3abeb572022-07-04 11:42:27 +0200227static forceinline struct stconn *qcs_sc(const struct qcs *qcs)
228{
229 return qcs->sd ? qcs->sd->sc : NULL;
230}
231
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200232/* Mark a stream as open if it was idle. This can be used on every
233 * successful emission/reception operation to update the stream state.
234 */
235static void qcs_idle_open(struct qcs *qcs)
236{
237 /* This operation must not be used if the stream is already closed. */
238 BUG_ON_HOT(qcs->st == QC_SS_CLO);
239
240 if (qcs->st == QC_SS_IDLE) {
241 qcs->st = QC_SS_OPEN;
242 TRACE_DEVEL("opening stream", QMUX_EV_QCS_NEW, qcs->qcc->conn, qcs);
243 }
244}
245
246/* Close the local channel of <qcs> instance. */
247static void qcs_close_local(struct qcs *qcs)
248{
249 /* The stream must have already been opened. */
250 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
251
252 /* This operation cannot be used multiple times. */
253 BUG_ON_HOT(qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO);
254
255 if (quic_stream_is_bidi(qcs->id)) {
256 qcs->st = (qcs->st == QC_SS_HREM) ? QC_SS_CLO : QC_SS_HLOC;
257 }
258 else {
259 /* Only local uni streams are valid for this operation. */
260 BUG_ON_HOT(quic_stream_is_remote(qcs->qcc, qcs->id));
261 qcs->st = QC_SS_CLO;
262 }
263
264 TRACE_DEVEL("closing stream locally", QMUX_EV_QCS_END, qcs->qcc->conn, qcs);
265}
266
267/* Close the remote channel of <qcs> instance. */
268static void qcs_close_remote(struct qcs *qcs)
269{
270 /* The stream must have already been opened. */
271 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
272
273 /* This operation cannot be used multiple times. */
274 BUG_ON_HOT(qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO);
275
276 if (quic_stream_is_bidi(qcs->id)) {
277 qcs->st = (qcs->st == QC_SS_HLOC) ? QC_SS_CLO : QC_SS_HREM;
278 }
279 else {
280 /* Only remote uni streams are valid for this operation. */
281 BUG_ON_HOT(quic_stream_is_local(qcs->qcc, qcs->id));
282 qcs->st = QC_SS_CLO;
283 }
284
285 TRACE_DEVEL("closing stream remotely", QMUX_EV_QCS_END, qcs->qcc->conn, qcs);
286}
287
288static int qcs_is_close_local(struct qcs *qcs)
289{
290 return qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO;
291}
292
293static __maybe_unused int qcs_is_close_remote(struct qcs *qcs)
294{
295 return qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO;
296}
297
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100298struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100299{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100300 struct buffer *buf = b_alloc(bptr);
301 BUG_ON(!buf);
302 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100303}
304
Amaury Denoyellea441ec92022-07-04 15:48:57 +0200305static struct ncbuf *qc_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200306{
307 struct buffer buf = BUF_NULL;
308
309 if (ncb_is_null(ncbuf)) {
310 b_alloc(&buf);
311 BUG_ON(b_is_null(&buf));
312
313 *ncbuf = ncb_make(buf.area, buf.size, 0);
314 ncb_init(ncbuf, 0);
315 }
316
317 return ncbuf;
318}
319
Amaury Denoyelle4561f842022-07-06 14:54:34 +0200320/* Notify an eventual subscriber on <qcs> or else wakup up the stconn layer if
321 * initialized.
322 */
323static void qcs_alert(struct qcs *qcs)
324{
325 if (qcs->subs) {
326 qcs_notify_recv(qcs);
327 qcs_notify_send(qcs);
328 }
329 else if (qcs_sc(qcs) && qcs->sd->sc->app_ops->wake) {
330 qcs->sd->sc->app_ops->wake(qcs->sd->sc);
331 }
332}
333
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100334int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
335{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100336 struct qcc *qcc = qcs->qcc;
337
338 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100339
340 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
341 BUG_ON(qcs->subs && qcs->subs != es);
342
343 es->events |= event_type;
344 qcs->subs = es;
345
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100346 if (event_type & SUB_RETRY_RECV)
347 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
348
349 if (event_type & SUB_RETRY_SEND)
350 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
351
352 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
353
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100354 return 0;
355}
356
357void qcs_notify_recv(struct qcs *qcs)
358{
359 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
360 tasklet_wakeup(qcs->subs->tasklet);
361 qcs->subs->events &= ~SUB_RETRY_RECV;
362 if (!qcs->subs->events)
363 qcs->subs = NULL;
364 }
365}
366
367void qcs_notify_send(struct qcs *qcs)
368{
369 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
370 tasklet_wakeup(qcs->subs->tasklet);
371 qcs->subs->events &= ~SUB_RETRY_SEND;
372 if (!qcs->subs->events)
373 qcs->subs = NULL;
374 }
375}
376
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200377/* Open a locally initiated stream for the connection <qcc>. Set <bidi> for a
378 * bidirectional stream, else an unidirectional stream is opened. The next
379 * available ID on the connection will be used according to the stream type.
380 *
381 * Returns the allocated stream instance or NULL on error.
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100382 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200383struct qcs *qcc_init_stream_local(struct qcc *qcc, int bidi)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100384{
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200385 struct qcs *qcs;
386 enum qcs_type type;
387 uint64_t *next;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100388
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200389 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
390
391 if (bidi) {
392 next = &qcc->next_bidi_l;
393 type = conn_is_back(qcc->conn) ? QCS_CLT_BIDI : QCS_SRV_BIDI;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100394 }
395 else {
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200396 next = &qcc->next_uni_l;
397 type = conn_is_back(qcc->conn) ? QCS_CLT_UNI : QCS_SRV_UNI;
398 }
399
400 /* TODO ensure that we won't overflow remote peer flow control limit on
401 * streams. Else, we should emit a STREAMS_BLOCKED frame.
402 */
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100403
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200404 qcs = qcs_new(qcc, *next, type);
405 if (!qcs)
406 return NULL;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100407
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200408 TRACE_DEVEL("opening local stream", QMUX_EV_QCS_NEW, qcc->conn, qcs);
409 *next += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100410
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200411 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
412 return qcs;
413}
414
415/* Open a remote initiated stream for the connection <qcc> with ID <id>. The
416 * caller is responsible to ensure that a stream with the same ID was not
417 * already opened. This function will also create all intermediaries streams
418 * with ID smaller than <id> not already opened before.
419 *
420 * Returns the allocated stream instance or NULL on error.
421 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200422static struct qcs *qcc_init_stream_remote(struct qcc *qcc, uint64_t id)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200423{
424 struct qcs *qcs = NULL;
425 enum qcs_type type;
426 uint64_t *largest;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100427
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200428 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200429
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200430 BUG_ON_HOT(quic_stream_is_local(qcc, id));
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100431
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200432 if (quic_stream_is_bidi(id)) {
433 largest = &qcc->largest_bidi_r;
434 type = conn_is_back(qcc->conn) ? QCS_SRV_BIDI : QCS_CLT_BIDI;
435 }
436 else {
437 largest = &qcc->largest_uni_r;
438 type = conn_is_back(qcc->conn) ? QCS_SRV_UNI : QCS_CLT_UNI;
439 }
440
441 /* TODO also checks max-streams for uni streams */
442 if (quic_stream_is_bidi(id)) {
443 if (id >= qcc->lfctl.ms_bidi * 4) {
444 /* RFC 9000 4.6. Controlling Concurrency
445 *
446 * An endpoint that receives a frame with a
447 * stream ID exceeding the limit it has sent
448 * MUST treat this as a connection error of
449 * type STREAM_LIMIT_ERROR
450 */
451 TRACE_DEVEL("leaving on flow control error", QMUX_EV_QCS_NEW, qcc->conn);
452 qcc_emit_cc(qcc, QC_ERR_STREAM_LIMIT_ERROR);
453 return NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100454 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200455 }
456
457 /* Only stream ID not already opened can be used. */
458 BUG_ON(id < *largest);
459
460 while (id >= *largest) {
461 const char *str = *largest < id ? "opening intermediary stream" :
462 "opening remote stream";
463
464 qcs = qcs_new(qcc, *largest, type);
465 if (!qcs) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200466 /* TODO emit RESET_STREAM */
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200467 TRACE_DEVEL("leaving on stream fallocation failure", QMUX_EV_QCS_NEW, qcc->conn);
468 return NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100469 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200470
471 TRACE_DEVEL(str, QMUX_EV_QCS_NEW, qcc->conn, qcs);
472 *largest += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100473 }
474
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200475 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle50742292022-03-29 14:57:19 +0200476 return qcs;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200477}
478
479/* Use this function for a stream <id> which is not in <qcc> stream tree. It
480 * returns true if the associated stream is closed.
481 */
482static int qcc_stream_id_is_closed(struct qcc *qcc, uint64_t id)
483{
484 uint64_t *largest;
485
486 /* This function must only be used for stream not present in the stream tree. */
487 BUG_ON_HOT(eb64_lookup(&qcc->streams_by_id, id));
488
489 if (quic_stream_is_local(qcc, id)) {
490 largest = quic_stream_is_uni(id) ? &qcc->next_uni_l :
491 &qcc->next_bidi_l;
492 }
493 else {
494 largest = quic_stream_is_uni(id) ? &qcc->largest_uni_r :
495 &qcc->largest_bidi_r;
496 }
497
498 return id < *largest;
499}
500
501/* Retrieve the stream instance from <id> ID. This can be used when receiving
502 * STREAM, STREAM_DATA_BLOCKED, RESET_STREAM, MAX_STREAM_DATA or STOP_SENDING
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200503 * frames. Set to false <receive_only> or <send_only> if these particular types
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200504 * of streams are not allowed. If the stream instance is found, it is stored in
505 * <out>.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200506 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200507 * Returns 0 on success else non-zero. On error, a RESET_STREAM or a
508 * CONNECTION_CLOSE is automatically emitted. Beware that <out> may be NULL
509 * on success if the stream has already been closed.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200510 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200511int qcc_get_qcs(struct qcc *qcc, uint64_t id, int receive_only, int send_only,
512 struct qcs **out)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200513{
514 struct eb64_node *node;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200515
516 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200517 *out = NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200518
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200519 if (!receive_only && quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
520 TRACE_DEVEL("leaving on receive-only stream not allowed", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
521 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200522 return 1;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200523 }
524
525 if (!send_only && quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
526 TRACE_DEVEL("leaving on send-only stream not allowed", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
527 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200528 return 1;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200529 }
530
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200531 /* Search the stream in the connection tree. */
532 node = eb64_lookup(&qcc->streams_by_id, id);
533 if (node) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200534 *out = eb64_entry(node, struct qcs, by_id);
535 TRACE_DEVEL("using stream from connection tree", QMUX_EV_QCC_RECV, qcc->conn, *out);
536 return 0;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200537 }
538
539 /* Check if stream is already closed. */
540 if (qcc_stream_id_is_closed(qcc, id)) {
541 TRACE_DEVEL("already released stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200542 /* Consider this as a success even if <out> is left NULL. */
543 return 0;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200544 }
545
546 /* Create the stream. This is valid only for remote initiated one. A
547 * local stream must have already been explicitely created by the
548 * application protocol layer.
549 */
550 if (quic_stream_is_local(qcc, id)) {
551 /* RFC 9000 19.8. STREAM Frames
552 *
553 * An endpoint MUST terminate the connection with error
554 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
555 * initiated stream that has not yet been created, or for a send-only
556 * stream.
557 */
558 TRACE_DEVEL("leaving on locally initiated stream not yet created", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
559 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200560 return 1;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200561 }
562 else {
563 /* Remote stream not found - try to open it. */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200564 *out = qcc_init_stream_remote(qcc, id);
565 if (!*out) {
566 TRACE_DEVEL("leaving on stream creation error", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
567 return 1;
568 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200569 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100570
571 out:
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200572 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200573 return 0;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100574}
575
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200576/* Simple function to duplicate a buffer */
577static inline struct buffer qcs_b_dup(const struct ncbuf *b)
578{
579 return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0));
580}
581
Amaury Denoyelle36d4b5e2022-07-01 11:25:40 +0200582/* Remove <bytes> from <qcs> Rx buffer. Flow-control for received offsets may
583 * be allocated for the peer if needed.
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200584 */
585static void qcs_consume(struct qcs *qcs, uint64_t bytes)
586{
587 struct qcc *qcc = qcs->qcc;
588 struct quic_frame *frm;
589 struct ncbuf *buf = &qcs->rx.ncbuf;
590 enum ncb_ret ret;
591
592 ret = ncb_advance(buf, bytes);
593 if (ret) {
594 ABORT_NOW(); /* should not happens because removal only in data */
595 }
596
597 if (ncb_is_empty(buf))
598 qc_free_ncbuf(qcs, buf);
599
600 qcs->rx.offset += bytes;
601 if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) {
602 frm = pool_zalloc(pool_head_quic_frame);
603 BUG_ON(!frm); /* TODO handle this properly */
604
605 qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init;
606
607 LIST_INIT(&frm->reflist);
608 frm->type = QUIC_FT_MAX_STREAM_DATA;
609 frm->max_stream_data.id = qcs->id;
610 frm->max_stream_data.max_stream_data = qcs->rx.msd;
611
612 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
613 tasklet_wakeup(qcc->wait_event.tasklet);
614 }
615
616 qcc->lfctl.offsets_consume += bytes;
617 if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) {
618 frm = pool_zalloc(pool_head_quic_frame);
619 BUG_ON(!frm); /* TODO handle this properly */
620
621 qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init;
622
623 LIST_INIT(&frm->reflist);
624 frm->type = QUIC_FT_MAX_DATA;
625 frm->max_data.max_data = qcc->lfctl.md;
626
627 LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list);
628 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
629 }
630}
631
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200632/* Decode the content of STREAM frames already received on the stream instance
633 * <qcs>.
634 *
635 * Returns 0 on success else non-zero.
636 */
637static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
638{
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200639 struct buffer b;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200640 ssize_t ret;
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200641 int fin = 0;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200642
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200643 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
644
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200645 b = qcs_b_dup(&qcs->rx.ncbuf);
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200646
647 /* Signal FIN to application if STREAM FIN received and there is no gap
648 * in the Rx buffer.
649 */
Amaury Denoyelle3f39b402022-07-01 16:11:03 +0200650 if (qcs->flags & QC_SF_SIZE_KNOWN && !ncb_is_fragmented(&qcs->rx.ncbuf))
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200651 fin = 1;
652
653 ret = qcc->app_ops->decode_qcs(qcs, &b, fin);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200654 if (ret < 0) {
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200655 TRACE_DEVEL("leaving on decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
656 return 1;
657 }
658
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200659 if (ret) {
660 qcs_consume(qcs, ret);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200661 qcs_notify_recv(qcs);
662 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200663
664 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
665
666 return 0;
667}
668
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200669/* Emit a CONNECTION_CLOSE_APP with error <err>. Reserved for application error
Amaury Denoyelled666d742022-07-13 15:15:58 +0200670 * code. To close the connection right away, set <immediate> : this is useful
671 * when dealing with a connection fatal error. Else a graceful shutdown will be
672 * conducted : the error-code is only registered. The lower layer is
673 * responsible to close the connection when deemed suitable. Note that in this
674 * case the error code might be overwritten if an immediate close is requested
675 * in the interval.
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200676 */
Amaury Denoyelled666d742022-07-13 15:15:58 +0200677void qcc_emit_cc_app(struct qcc *qcc, int err, int immediate)
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200678{
Amaury Denoyelled666d742022-07-13 15:15:58 +0200679 if (immediate) {
680 quic_set_connection_close(qcc->conn->handle.qc, quic_err_app(err));
681 qcc->flags |= QC_CF_CC_EMIT;
682 tasklet_wakeup(qcc->wait_event.tasklet);
683 }
684 else {
685 /* Only register the error code for graceful shutdown. */
686 qcc->conn->handle.qc->err = quic_err_app(err);
687 }
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200688}
689
690/* Prepare for the emission of RESET_STREAM on <qcs> with error code <err>. */
691void qcc_reset_stream(struct qcs *qcs, int err)
692{
693 struct qcc *qcc = qcs->qcc;
694
695 if ((qcs->flags & QC_SF_TO_RESET) || qcs_is_close_local(qcs))
696 return;
697
698 qcs->flags |= QC_SF_TO_RESET;
699 qcs->err = err;
700 tasklet_wakeup(qcc->wait_event.tasklet);
701 TRACE_DEVEL("reset stream", QMUX_EV_QCS_END, qcc->conn, qcs);
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200702}
703
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200704/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
705 * <data> with length <len> and represents the offset <offset>. <fin> is set if
706 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100707 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200708 * Returns 0 on success else non-zero. On error, the received frame should not
709 * be acknowledged.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100710 */
711int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200712 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100713{
714 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200715 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100716
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100717 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
718
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200719 if (qcc->flags & QC_CF_CC_EMIT) {
720 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200721 return 1;
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200722 }
723
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200724 /* RFC 9000 19.8. STREAM Frames
725 *
726 * An endpoint MUST terminate the connection with error
727 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
728 * initiated stream that has not yet been created, or for a send-only
729 * stream.
730 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200731 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
732 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
733 return 1;
734 }
735
736 if (!qcs) {
737 /* Already closed stream. */
738 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200739 return 0;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200740 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100741
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200742 /* RFC 9000 4.5. Stream Final Size
743 *
744 * Once a final size for a stream is known, it cannot change. If a
745 * RESET_STREAM or STREAM frame is received indicating a change in the
746 * final size for the stream, an endpoint SHOULD respond with an error
747 * of type FINAL_SIZE_ERROR; see Section 11 for details on error
748 * handling.
749 */
750 if (qcs->flags & QC_SF_SIZE_KNOWN &&
751 (offset + len > qcs->rx.offset_max || (fin && offset + len < qcs->rx.offset_max))) {
752 TRACE_DEVEL("leaving on final size error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
753 qcc_emit_cc(qcc, QC_ERR_FINAL_SIZE_ERROR);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200754 return 1;
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200755 }
756
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100757 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100758 TRACE_DEVEL("leaving on already received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle74cf2372022-04-29 15:58:22 +0200759 return 0;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100760 }
761
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200762 qcs_idle_open(qcs);
763
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200764 if (offset + len > qcs->rx.offset_max) {
765 uint64_t diff = offset + len - qcs->rx.offset_max;
766 qcs->rx.offset_max = offset + len;
767 qcc->lfctl.offsets_recv += diff;
768
769 if (offset + len > qcs->rx.msd ||
770 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
771 /* RFC 9000 4.1. Data Flow Control
772 *
773 * A receiver MUST close the connection with an error
774 * of type FLOW_CONTROL_ERROR if the sender violates
775 * the advertised connection or stream data limits
776 */
777 TRACE_DEVEL("leaving on flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
778 qcc->conn, qcs);
779 qcc_emit_cc(qcc, QC_ERR_FLOW_CONTROL_ERROR);
780 return 1;
781 }
782 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100783
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200784 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100785 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200786 ABORT_NOW();
787 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100788 }
789
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100790 TRACE_DEVEL("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200791 if (offset < qcs->rx.offset) {
Frédéric Lécaillea18c3332022-07-04 09:54:58 +0200792 size_t diff = qcs->rx.offset - offset;
793
794 len -= diff;
795 data += diff;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200796 offset = qcs->rx.offset;
797 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100798
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200799 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
800 if (ret != NCB_RET_OK) {
801 if (ret == NCB_RET_DATA_REJ) {
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200802 /* RFC 9000 2.2. Sending and Receiving Data
803 *
804 * An endpoint could receive data for a stream at the
805 * same stream offset multiple times. Data that has
806 * already been received can be discarded. The data at
807 * a given offset MUST NOT change if it is sent
808 * multiple times; an endpoint MAY treat receipt of
809 * different data at the same offset within a stream as
810 * a connection error of type PROTOCOL_VIOLATION.
811 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200812 TRACE_DEVEL("leaving on data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
813 qcc->conn, qcs);
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200814 qcc_emit_cc(qcc, QC_ERR_PROTOCOL_VIOLATION);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200815 }
816 else if (ret == NCB_RET_GAP_SIZE) {
817 TRACE_DEVEL("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
818 qcc->conn, qcs);
819 }
820 return 1;
821 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100822
823 if (fin)
Amaury Denoyelle3f39b402022-07-01 16:11:03 +0200824 qcs->flags |= QC_SF_SIZE_KNOWN;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100825
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200826 if (qcs->flags & QC_SF_SIZE_KNOWN && !ncb_is_fragmented(&qcs->rx.ncbuf))
827 qcs_close_remote(qcs);
828
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200829 if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL))
830 qcc_decode_qcs(qcc, qcs);
831
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200832 if (qcs->flags & QC_SF_READ_ABORTED) {
833 /* TODO should send a STOP_SENDING */
834 qcs_free(qcs);
835 }
836
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100837 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100838 return 0;
839}
840
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100841/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
842 * the frame.
843 *
844 * Returns 0 on success else non-zero.
845 */
846int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
847{
Amaury Denoyelle392e94e2022-07-06 15:44:16 +0200848 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
849
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100850 if (qcc->rfctl.md < max) {
851 qcc->rfctl.md = max;
Amaury Denoyelle392e94e2022-07-06 15:44:16 +0200852 TRACE_DEVEL("increase remote max-data", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100853
854 if (qcc->flags & QC_CF_BLK_MFCTL) {
855 qcc->flags &= ~QC_CF_BLK_MFCTL;
856 tasklet_wakeup(qcc->wait_event.tasklet);
857 }
858 }
Amaury Denoyelle392e94e2022-07-06 15:44:16 +0200859
860 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100861 return 0;
862}
863
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100864/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
865 * field of the frame and <id> is the identifier of the QUIC stream.
866 *
Amaury Denoyelleb68559a2022-07-06 15:45:20 +0200867 * Returns 0 on success else non-zero. On error, the received frame should not
868 * be acknowledged.
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100869 */
870int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
871{
872 struct qcs *qcs;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100873
Amaury Denoyelle392e94e2022-07-06 15:44:16 +0200874 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
875
Amaury Denoyelleb68559a2022-07-06 15:45:20 +0200876 /* RFC 9000 19.10. MAX_STREAM_DATA Frames
877 *
878 * Receiving a MAX_STREAM_DATA frame for a locally
879 * initiated stream that has not yet been created MUST be treated as a
880 * connection error of type STREAM_STATE_ERROR. An endpoint that
881 * receives a MAX_STREAM_DATA frame for a receive-only stream MUST
882 * terminate the connection with error STREAM_STATE_ERROR.
883 */
884 if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) {
885 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
886 return 1;
887 }
888
889 if (qcs) {
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100890 if (max > qcs->tx.msd) {
891 qcs->tx.msd = max;
Amaury Denoyelle392e94e2022-07-06 15:44:16 +0200892 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 +0100893
894 if (qcs->flags & QC_SF_BLK_SFCTL) {
895 qcs->flags &= ~QC_SF_BLK_SFCTL;
896 tasklet_wakeup(qcc->wait_event.tasklet);
897 }
898 }
899 }
900
Amaury Denoyelle392e94e2022-07-06 15:44:16 +0200901 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100902 return 0;
903}
904
Amaury Denoyellea5b50752022-07-04 11:44:53 +0200905/* Handle a new STOP_SENDING frame for stream ID <id>. The error code should be
906 * specified in <err>.
907 *
908 * Returns 0 on success else non-zero. On error, the received frame should not
909 * be acknowledged.
910 */
911int qcc_recv_stop_sending(struct qcc *qcc, uint64_t id, uint64_t err)
912{
913 struct qcs *qcs;
914
915 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
916
917 /* RFC 9000 19.5. STOP_SENDING Frames
918 *
919 * Receiving a STOP_SENDING frame for a
920 * locally initiated stream that has not yet been created MUST be
921 * treated as a connection error of type STREAM_STATE_ERROR. An
922 * endpoint that receives a STOP_SENDING frame for a receive-only stream
923 * MUST terminate the connection with error STREAM_STATE_ERROR.
924 */
925 if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) {
926 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
927 return 1;
928 }
929
930 if (!qcs)
931 goto out;
932
933 /* RFC 9000 3.5. Solicited State Transitions
934 *
935 * An endpoint that receives a STOP_SENDING frame
936 * MUST send a RESET_STREAM frame if the stream is in the "Ready" or
937 * "Send" state. If the stream is in the "Data Sent" state, the
938 * endpoint MAY defer sending the RESET_STREAM frame until the packets
939 * containing outstanding data are acknowledged or declared lost. If
940 * any outstanding data is declared lost, the endpoint SHOULD send a
941 * RESET_STREAM frame instead of retransmitting the data.
942 *
943 * An endpoint SHOULD copy the error code from the STOP_SENDING frame to
944 * the RESET_STREAM frame it sends, but it can use any application error
945 * code.
946 */
947 TRACE_DEVEL("receiving STOP_SENDING on stream", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
948 qcc_reset_stream(qcs, err);
949
950 out:
951 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
952 return 0;
953}
954
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200955/* Signal the closing of remote stream with id <id>. Flow-control for new
956 * streams may be allocated for the peer if needed.
957 */
958static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +0100959{
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200960 struct quic_frame *frm;
961
962 if (quic_stream_is_bidi(id)) {
963 ++qcc->lfctl.cl_bidi_r;
964 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
965 frm = pool_zalloc(pool_head_quic_frame);
966 BUG_ON(!frm); /* TODO handle this properly */
967
968 LIST_INIT(&frm->reflist);
969 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
970 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
971 qcc->lfctl.cl_bidi_r;
972 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
973 tasklet_wakeup(qcc->wait_event.tasklet);
974
975 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
976 qcc->lfctl.cl_bidi_r = 0;
977 }
978 }
979 else {
980 /* TODO */
981 }
982
983 return 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100984}
985
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500986/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100987static void qcs_destroy(struct qcs *qcs)
988{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100989 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200990 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100991
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100992 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100993
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200994 if (quic_stream_is_remote(qcs->qcc, id))
995 qcc_release_remote_stream(qcs->qcc, id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100996
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200997 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100998
999 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001000}
1001
1002static inline int qcc_is_dead(const struct qcc *qcc)
1003{
Amaury Denoyelle198d35f2022-04-01 17:56:58 +02001004 if (qcc->app_ops && qcc->app_ops->is_active &&
1005 qcc->app_ops->is_active(qcc, qcc->ctx))
1006 return 0;
1007
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001008 if ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task)
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001009 return 1;
1010
1011 return 0;
1012}
1013
1014/* Return true if the mux timeout should be armed. */
1015static inline int qcc_may_expire(struct qcc *qcc)
1016{
Willy Tarreau3215e732022-05-27 10:09:11 +02001017 return !qcc->nb_sc;
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001018}
1019
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001020/* Transfer as much as possible data on <qcs> from <in> to <out>. This is done
1021 * in respect with available flow-control at stream and connection level.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001022 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001023 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001024 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001025static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, struct buffer *in)
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001026{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001027 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001028 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001029 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001030
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001031 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001032
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001033 qc_get_buf(qcs, out);
1034
1035 /*
1036 * QCS out buffer diagram
1037 * head left to_xfer
1038 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001039 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001040 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001041 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001042 * ^ ack-off ^ sent-off ^ off
1043 *
1044 * STREAM frame
1045 * ^ ^
1046 * |xxxxxxxxxxxxxxxxx|
1047 */
1048
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001049 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001050 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001051 BUG_ON_HOT(qcc->tx.offsets < qcc->tx.sent_offsets);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001052
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001053 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001054 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001055
1056 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
1057 /* do not exceed flow control limit */
1058 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
1059 to_xfer = qcs->tx.msd - qcs->tx.offset;
1060
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001061 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001062 /* do not overcome flow control limit on connection */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001063 if (qcc->tx.offsets + to_xfer > qcc->rfctl.md)
1064 to_xfer = qcc->rfctl.md - qcc->tx.offsets;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001065
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001066 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001067 goto out;
1068
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001069 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001070
1071 out:
1072 {
1073 struct qcs_xfer_data_trace_arg arg = {
1074 .prep = b_data(out), .xfer = total,
1075 };
1076 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
1077 qcc->conn, qcs, &arg);
1078 }
1079
1080 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001081}
1082
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001083/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
1084 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
1085 * stream frame.
1086 *
1087 * Returns the length of the STREAM frame or a negative error code.
1088 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001089static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
1090 struct list *frm_list)
1091{
1092 struct qcc *qcc = qcs->qcc;
1093 struct quic_frame *frm;
1094 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001095 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001096
1097 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1098
Amaury Denoyellea4569202022-04-15 17:29:25 +02001099 /* if ack_offset < buf_offset, it points to an older buffer. */
1100 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
1101 BUG_ON(qcs->tx.sent_offset < base_off);
1102
1103 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001104 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001105 BUG_ON(total < 0);
1106
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001107 if (!total && !fin) {
1108 /* No need to send anything if total is NULL and no FIN to signal. */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001109 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1110 return 0;
1111 }
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001112 BUG_ON((!total && qcs->tx.sent_offset > qcs->tx.offset) ||
1113 (total && qcs->tx.sent_offset >= qcs->tx.offset));
Amaury Denoyellea4569202022-04-15 17:29:25 +02001114 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001115 BUG_ON(qcc->tx.sent_offsets + total > qcc->rfctl.md);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001116
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001117 frm = pool_zalloc(pool_head_quic_frame);
1118 if (!frm)
1119 goto err;
1120
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001121 LIST_INIT(&frm->reflist);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001122 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001123 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001124 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001125 frm->stream.buf = out;
1126 frm->stream.data = (unsigned char *)b_peek(out, head);
1127
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +01001128 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001129 if (fin)
1130 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001131
1132 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001133 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001134 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001135 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001136
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001137 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
1138 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001139
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001140 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001141
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001142 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001143 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001144 struct qcs_build_stream_trace_arg arg = {
1145 .len = frm->stream.len, .fin = fin,
1146 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001147 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001148 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001149 qcc->conn, qcs, &arg);
1150 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001151
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001152 return total;
1153
1154 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001155 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001156 return -1;
1157}
1158
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001159/* Check after transfering data from qcs.tx.buf if FIN must be set on the next
1160 * STREAM frame for <qcs>.
1161 *
1162 * Returns true if FIN must be set else false.
1163 */
1164static int qcs_stream_fin(struct qcs *qcs)
1165{
1166 return qcs->flags & QC_SF_FIN_STREAM && !b_data(&qcs->tx.buf);
1167}
1168
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001169/* This function must be called by the upper layer to inform about the sending
1170 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
1171 * <offset>.
1172 */
1173void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
1174{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001175 struct qcc *qcc = qcs->qcc;
1176 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001177
1178 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001179 BUG_ON(offset + data > qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001180
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001181 /* check if the STREAM frame has already been notified. It can happen
1182 * for retransmission.
1183 */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001184 if (offset + data < qcs->tx.sent_offset)
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001185 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001186
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001187 qcs_idle_open(qcs);
1188
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001189 diff = offset + data - qcs->tx.sent_offset;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001190 if (diff) {
1191 /* increase offset sum on connection */
1192 qcc->tx.sent_offsets += diff;
1193 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
1194 if (qcc->tx.sent_offsets == qcc->rfctl.md)
1195 qcc->flags |= QC_CF_BLK_MFCTL;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001196
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001197 /* increase offset on stream */
1198 qcs->tx.sent_offset += diff;
1199 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
1200 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
1201 if (qcs->tx.sent_offset == qcs->tx.msd)
1202 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001203
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001204 if (qcs->tx.offset == qcs->tx.sent_offset &&
1205 b_full(&qcs->stream->buf->buf)) {
1206 qc_stream_buf_release(qcs->stream);
1207 /* prepare qcs for immediate send retry if data to send */
1208 if (b_data(&qcs->tx.buf))
1209 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
1210 }
1211 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001212
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001213 if (qcs->tx.offset == qcs->tx.sent_offset && qcs_stream_fin(qcs)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001214 /* Close stream locally. */
1215 qcs_close_local(qcs);
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001216 /* Reset flag to not emit multiple FIN STREAM frames. */
1217 qcs->flags &= ~QC_SF_FIN_STREAM;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001218 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001219}
1220
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001221/* Wrapper for send on transport layer. Send a list of frames <frms> for the
1222 * connection <qcc>.
1223 *
1224 * Returns 0 if all data sent with success else non-zero.
1225 */
1226static int qc_send_frames(struct qcc *qcc, struct list *frms)
1227{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001228 /* TODO implement an opportunistic retry mechanism. This is needed
1229 * because qc_send_app_pkts is not completed. It will only prepare data
1230 * up to its Tx buffer. The frames left are not send even if the Tx
1231 * buffer is emptied by the sendto call.
1232 *
1233 * To overcome this, we call repeatedly qc_send_app_pkts until we
1234 * detect that the transport layer has send nothing. This could happen
1235 * on congestion or sendto syscall error.
1236 *
1237 * When qc_send_app_pkts is improved to handle retry by itself, we can
1238 * remove the looping from the MUX.
1239 */
1240 struct quic_frame *first_frm;
1241 uint64_t first_offset = 0;
1242 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001243
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001244 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
1245
1246 if (LIST_ISEMPTY(frms)) {
1247 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001248 return 1;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001249 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +01001250
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001251 LIST_INIT(&qcc->send_retry_list);
1252
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001253 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001254 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
1255 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
1256 first_offset = first_frm->stream.offset.key;
1257 first_stream_frame_type = 1;
1258 }
1259 else {
1260 first_stream_frame_type = 0;
1261 }
1262
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001263 if (!LIST_ISEMPTY(frms))
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02001264 qc_send_app_pkts(qcc->conn->handle.qc, 0, frms);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001265
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001266 /* If there is frames left, check if the transport layer has send some
1267 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001268 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001269 if (!LIST_ISEMPTY(frms)) {
1270 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
1271 goto retry_send;
1272
1273 /* If the first frame is STREAM, check if its offset has
1274 * changed.
1275 */
1276 if (first_stream_frame_type &&
1277 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
1278 goto retry_send;
1279 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001280 }
1281
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001282 /* If there is frames left at this stage, transport layer is blocked.
1283 * Subscribe on it to retry later.
1284 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001285 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001286 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001287 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1288 SUB_RETRY_SEND, &qcc->wait_event);
1289 return 1;
1290 }
1291
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001292 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1293
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001294 return 0;
1295}
1296
1297/* Emit a RESET_STREAM on <qcs>.
1298 *
1299 * Returns 0 if the frame has been successfully sent else non-zero.
1300 */
1301static int qcs_send_reset(struct qcs *qcs)
1302{
1303 struct list frms = LIST_HEAD_INIT(frms);
1304 struct quic_frame *frm;
1305
1306 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1307
1308 frm = pool_zalloc(pool_head_quic_frame);
1309 if (!frm)
1310 return 1;
1311
1312 LIST_INIT(&frm->reflist);
1313 frm->type = QUIC_FT_RESET_STREAM;
1314 frm->reset_stream.id = qcs->id;
1315 frm->reset_stream.app_error_code = qcs->err;
1316 frm->reset_stream.final_size = qcs->tx.sent_offset;
1317
1318 LIST_APPEND(&frms, &frm->list);
1319 if (qc_send_frames(qcs->qcc, &frms)) {
1320 pool_free(pool_head_quic_frame, frm);
1321 TRACE_DEVEL("cannot send RESET_STREAM", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1322 return 1;
1323 }
1324
1325 if (qcs_sc(qcs)) {
1326 se_fl_set_error(qcs->sd);
1327 qcs_alert(qcs);
1328 }
1329
1330 qcs_close_local(qcs);
1331 qcs->flags &= ~QC_SF_TO_RESET;
1332
1333 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001334 return 0;
1335}
1336
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001337/* Used internally by qc_send function. Proceed to send for <qcs>. This will
1338 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001339 * is then generated and inserted in <frms> list.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001340 *
1341 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
1342 * be null if out buffer cannot be allocated.
1343 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001344static int _qc_send_qcs(struct qcs *qcs, struct list *frms)
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001345{
1346 struct qcc *qcc = qcs->qcc;
1347 struct buffer *buf = &qcs->tx.buf;
1348 struct buffer *out = qc_stream_buf_get(qcs->stream);
1349 int xfer = 0;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001350 char fin = 0;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001351
1352 /* Allocate <out> buffer if necessary. */
1353 if (!out) {
1354 if (qcc->flags & QC_CF_CONN_FULL)
1355 return 0;
1356
1357 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
1358 if (!out) {
1359 qcc->flags |= QC_CF_CONN_FULL;
1360 return 0;
1361 }
1362 }
1363
1364 /* Transfer data from <buf> to <out>. */
1365 if (b_data(buf)) {
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001366 xfer = qcs_xfer_data(qcs, out, buf);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001367 if (xfer > 0) {
1368 qcs_notify_send(qcs);
1369 qcs->flags &= ~QC_SF_BLK_MROOM;
1370 }
1371
1372 qcs->tx.offset += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001373 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001374 qcc->tx.offsets += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001375 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001376 }
1377
1378 /* out buffer cannot be emptied if qcs offsets differ. */
1379 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1380
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001381 /* FIN is set if all incoming data were transfered. */
1382 fin = qcs_stream_fin(qcs);
1383
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001384 /* Build a new STREAM frame with <out> buffer. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001385 if (qcs->tx.sent_offset != qcs->tx.offset || fin) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001386 int ret;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001387 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +02001388 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001389 }
1390
1391 return xfer;
1392}
1393
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001394/* Proceed to sending. Loop through all available streams for the <qcc>
1395 * instance and try to send as much as possible.
1396 *
1397 * Returns the total of bytes sent to the transport layer.
1398 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001399static int qc_send(struct qcc *qcc)
1400{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001401 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001402 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001403 struct qcs *qcs, *qcs_tmp;
1404 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001405
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001406 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001407
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001408 if (qcc->conn->flags & CO_FL_SOCK_WR_SH || qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001409 qcc->conn->flags |= CO_FL_ERROR;
1410 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
1411 return 0;
1412 }
1413
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001414 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1415 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
1416 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
1417 goto out;
1418 }
1419 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02001420
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001421 if (qcc->flags & QC_CF_BLK_MFCTL)
1422 return 0;
1423
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001424 /* loop through all streams, construct STREAM frames if data available.
1425 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001426 */
1427 node = eb64_first(&qcc->streams_by_id);
1428 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001429 int ret;
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001430 uint64_t id;
1431
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001432 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001433 id = qcs->id;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001434
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001435 if (quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001436 node = eb64_next(node);
1437 continue;
1438 }
1439
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001440 if (qcs->flags & QC_SF_TO_RESET) {
1441 qcs_send_reset(qcs);
1442 node = eb64_next(node);
1443 continue;
1444 }
1445
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001446 if (qcs_is_close_local(qcs)) {
1447 node = eb64_next(node);
1448 continue;
1449 }
1450
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001451 if (qcs->flags & QC_SF_BLK_SFCTL) {
1452 node = eb64_next(node);
1453 continue;
1454 }
1455
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001456 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001457 node = eb64_next(node);
1458 continue;
1459 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001460
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001461 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001462 total += ret;
1463 node = eb64_next(node);
1464 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001465
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001466 if (qc_send_frames(qcc, &frms)) {
1467 /* data rejected by transport layer, do not retry. */
1468 goto out;
1469 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001470
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001471 retry:
1472 tmp_total = 0;
1473 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
1474 int ret;
1475 BUG_ON(!b_data(&qcs->tx.buf));
1476 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001477
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001478 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001479 tmp_total += ret;
1480 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001481 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001482
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001483 total += tmp_total;
1484 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
1485 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +01001486
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001487 out:
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02001488 /* Deallocate frames that the transport layer has rejected. */
1489 if (!LIST_ISEMPTY(&frms)) {
1490 struct quic_frame *frm, *frm2;
1491 list_for_each_entry_safe(frm, frm2, &frms, list) {
1492 LIST_DELETE(&frm->list);
1493 pool_free(pool_head_quic_frame, frm);
1494 }
1495 }
1496
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001497 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1498
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001499 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001500}
1501
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001502/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1503 * operation.
1504 *
1505 * Returns 0 on success else non-zero.
1506 */
1507static int qc_recv(struct qcc *qcc)
1508{
1509 struct eb64_node *node;
1510 struct qcs *qcs;
1511
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001512 TRACE_ENTER(QMUX_EV_QCC_RECV);
1513
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001514 if (qcc->flags & QC_CF_CC_EMIT) {
1515 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
1516 return 0;
1517 }
1518
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001519 node = eb64_first(&qcc->streams_by_id);
1520 while (node) {
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001521 uint64_t id;
1522
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001523 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001524 id = qcs->id;
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001525
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001526 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001527 node = eb64_next(node);
1528 continue;
1529 }
1530
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001531 if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001532 node = eb64_next(node);
1533 continue;
1534 }
1535
1536 qcc_decode_qcs(qcc, qcs);
1537 node = eb64_next(node);
Amaury Denoyelle849b24f2022-05-24 17:22:07 +02001538
1539 if (qcs->flags & QC_SF_READ_ABORTED) {
1540 /* TODO should send a STOP_SENDING */
1541 qcs_free(qcs);
1542 }
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001543 }
1544
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001545 TRACE_LEAVE(QMUX_EV_QCC_RECV);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001546 return 0;
1547}
1548
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001549
1550/* Release all streams which have their transfer operation achieved.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001551 *
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001552 * Returns true if at least one stream is released.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001553 */
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001554static int qc_purge_streams(struct qcc *qcc)
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001555{
1556 struct eb64_node *node;
1557 int release = 0;
1558
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001559 TRACE_ENTER(QMUX_EV_QCC_WAKE);
1560
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001561 node = eb64_first(&qcc->streams_by_id);
1562 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001563 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001564 node = eb64_next(node);
1565
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001566 /* Release not attached closed streams. */
1567 if (qcs->st == QC_SS_CLO && !qcs_sc(qcs)) {
1568 TRACE_DEVEL("purging closed stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
1569 qcs_destroy(qcs);
1570 release = 1;
1571 continue;
1572 }
1573
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001574 /* Release detached streams with empty buffer. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001575 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02001576 if (qcs_is_close_local(qcs)) {
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001577 TRACE_DEVEL("purging detached stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001578 qcs_destroy(qcs);
1579 release = 1;
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001580 continue;
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001581 }
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001582
1583 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1584 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001585 }
1586 }
1587
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001588 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001589 return release;
1590}
1591
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001592/* release function. This one should be called to free all resources allocated
1593 * to the mux.
1594 */
1595static void qc_release(struct qcc *qcc)
1596{
1597 struct connection *conn = qcc->conn;
1598 struct eb64_node *node;
1599
1600 TRACE_ENTER(QMUX_EV_QCC_END);
1601
1602 if (qcc->app_ops && qcc->app_ops->release) {
1603 /* Application protocol with dedicated connection closing
1604 * procedure.
1605 */
1606 qcc->app_ops->release(qcc->ctx);
Amaury Denoyellea154dc02022-06-13 17:09:01 +02001607
1608 /* useful if application protocol should emit some closing
1609 * frames. For example HTTP/3 GOAWAY frame.
1610 */
1611 qc_send(qcc);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001612 }
1613 else {
1614 qcc_emit_cc_app(qcc, QC_ERR_NO_ERROR, 0);
1615 }
1616
1617 if (qcc->task) {
1618 task_destroy(qcc->task);
1619 qcc->task = NULL;
1620 }
1621
1622 if (qcc->wait_event.tasklet)
1623 tasklet_free(qcc->wait_event.tasklet);
1624 if (conn && qcc->wait_event.events) {
1625 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
1626 qcc->wait_event.events,
1627 &qcc->wait_event);
1628 }
1629
1630 /* liberate remaining qcs instances */
1631 node = eb64_first(&qcc->streams_by_id);
1632 while (node) {
1633 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
1634 node = eb64_next(node);
1635 qcs_free(qcs);
1636 }
1637
1638 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1639 struct quic_frame *frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list);
1640 LIST_DELETE(&frm->list);
1641 pool_free(pool_head_quic_frame, frm);
1642 }
1643
1644 pool_free(pool_head_qcc, qcc);
1645
1646 if (conn) {
1647 LIST_DEL_INIT(&conn->stopping_list);
1648
1649 conn->handle.qc->conn = NULL;
1650 conn->mux = NULL;
1651 conn->ctx = NULL;
1652
1653 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
1654
1655 conn_stop_tracking(conn);
1656 conn_full_close(conn);
1657 if (conn->destroy_cb)
1658 conn->destroy_cb(conn);
1659 conn_free(conn);
1660 }
1661
1662 TRACE_LEAVE(QMUX_EV_QCC_END);
1663}
1664
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001665static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1666{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001667 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001668
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001669 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001670
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001671 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001672
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001673 if (qc_purge_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001674 if (qcc_is_dead(qcc)) {
1675 qc_release(qcc);
1676 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001677 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001678 if (qcc_may_expire(qcc))
1679 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1680 else
1681 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001682 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001683 }
1684 }
1685
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001686 qc_recv(qcc);
1687
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001688 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1689
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001690 return NULL;
1691}
1692
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001693static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1694{
1695 struct qcc *qcc = ctx;
1696 int expired = tick_is_expired(t->expire, now_ms);
1697
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001698 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001699
1700 if (qcc) {
1701 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001702 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001703 return t;
1704 }
1705
1706 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001707 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001708 t->expire = TICK_ETERNITY;
1709 return t;
1710 }
1711 }
1712
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001713 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001714
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001715 if (!qcc) {
1716 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001717 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001718 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001719
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001720 qcc->task = NULL;
1721
1722 if (qcc_is_dead(qcc))
1723 qc_release(qcc);
1724
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001725 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1726
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001727 return NULL;
1728}
1729
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001730static int qc_init(struct connection *conn, struct proxy *prx,
1731 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001732{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001733 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001734 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001735
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001736 qcc = pool_alloc(pool_head_qcc);
1737 if (!qcc)
1738 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001739
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001740 qcc->conn = conn;
1741 conn->ctx = qcc;
Willy Tarreau3215e732022-05-27 10:09:11 +02001742 qcc->nb_sc = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001743 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001744
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001745 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001746
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001747 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001748
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001749 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001750 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001751
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001752 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001753 qcc->tx.sent_offsets = qcc->tx.offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001754
1755 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001756 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001757 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001758 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001759 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001760
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001761 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001762 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001763 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001764 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001765
1766 /* Server initiated streams must respect the server flow control. */
1767 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001768 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001769 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001770 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1771
1772 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001773 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001774 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001775 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001776
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001777 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001778 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02001779 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
1780 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001781 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001782
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001783 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001784 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001785
Willy Tarreau784b8682022-04-11 14:18:10 +02001786 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001787 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001788 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1789 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1790
Amaury Denoyellea509ffb2022-07-04 15:50:33 +02001791 if (conn_is_back(conn)) {
1792 qcc->next_bidi_l = 0x00;
1793 qcc->largest_bidi_r = 0x01;
1794 qcc->next_uni_l = 0x02;
1795 qcc->largest_uni_r = 0x03;
1796 }
1797 else {
1798 qcc->largest_bidi_r = 0x00;
1799 qcc->next_bidi_l = 0x01;
1800 qcc->largest_uni_r = 0x02;
1801 qcc->next_uni_l = 0x03;
1802 }
1803
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001804 qcc->wait_event.tasklet = tasklet_new();
1805 if (!qcc->wait_event.tasklet)
1806 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001807
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001808 LIST_INIT(&qcc->send_retry_list);
1809
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001810 qcc->subs = NULL;
1811 qcc->wait_event.tasklet->process = qc_io_cb;
1812 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001813 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001814
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001815 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001816 qcc->task = NULL;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001817 qcc->timeout = prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001818 if (tick_isset(qcc->timeout)) {
1819 qcc->task = task_new_here();
1820 if (!qcc->task)
1821 goto fail_no_timeout_task;
1822 qcc->task->process = qc_timeout_task;
1823 qcc->task->context = qcc;
1824 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1825 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001826
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001827 if (!conn_is_back(conn)) {
1828 if (!LIST_INLIST(&conn->stopping_list)) {
1829 LIST_APPEND(&mux_stopping_data[tid].list,
1830 &conn->stopping_list);
1831 }
1832 }
1833
Willy Tarreau784b8682022-04-11 14:18:10 +02001834 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001835 /* init read cycle */
1836 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001837
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001838 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001839
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001840 fail_no_timeout_task:
1841 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001842 fail_no_tasklet:
1843 pool_free(pool_head_qcc, qcc);
1844 fail_no_qcc:
1845 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001846}
1847
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001848static void qc_destroy(void *ctx)
1849{
1850 struct qcc *qcc = ctx;
1851
1852 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
1853 qc_release(qcc);
1854 TRACE_LEAVE(QMUX_EV_QCC_END);
1855}
1856
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001857static void qc_detach(struct sedesc *sd)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001858{
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001859 struct qcs *qcs = sd->se;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001860 struct qcc *qcc = qcs->qcc;
1861
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001862 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001863
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001864 /* TODO this BUG_ON_HOT() is not correct as the stconn layer may detach
1865 * from the stream even if it is not closed remotely at the QUIC layer.
1866 * This happens for example when a stream must be closed due to a
1867 * rejected request. To better handle these cases, it will be required
1868 * to implement shutr/shutw MUX operations. Once this is done, this
1869 * BUG_ON_HOT() statement can be adjusted.
1870 */
1871 //BUG_ON_HOT(!qcs_is_close_remote(qcs));
Willy Tarreau3215e732022-05-27 10:09:11 +02001872 --qcc->nb_sc;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001873
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02001874 if (!qcs_is_close_local(qcs) && !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001875 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001876 qcs->flags |= QC_SF_DETACH;
1877 return;
1878 }
1879
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001880 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001881
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001882 if (qcc_is_dead(qcc)) {
1883 qc_release(qcc);
1884 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001885 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001886 if (qcc_may_expire(qcc))
1887 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1888 else
1889 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001890 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001891 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001892
1893 TRACE_LEAVE(QMUX_EV_STRM_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001894}
1895
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001896/* Called from the upper layer, to receive data */
Willy Tarreau3215e732022-05-27 10:09:11 +02001897static size_t qc_rcv_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001898 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001899{
Willy Tarreau3215e732022-05-27 10:09:11 +02001900 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001901 struct htx *qcs_htx = NULL;
1902 struct htx *cs_htx = NULL;
1903 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001904 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001905
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001906 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001907
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001908 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
1909 if (htx_is_empty(qcs_htx)) {
1910 /* Set buffer data to 0 as HTX is empty. */
1911 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1912 goto end;
1913 }
1914
1915 ret = qcs_htx->data;
1916
1917 cs_htx = htx_from_buf(buf);
1918 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
Amaury Denoyelle72a78e82022-07-29 15:34:12 +02001919 /* EOM will be copied to cs_htx via b_xfer(). */
1920 if (qcs_htx->flags & HTX_FL_EOM)
1921 fin = 1;
1922
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001923 htx_to_buf(cs_htx, buf);
1924 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1925 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
1926 goto end;
1927 }
1928
1929 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
1930 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
1931
1932 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001933 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
1934 cs_htx->flags |= HTX_FL_EOM;
1935 fin = 1;
1936 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001937
1938 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
1939 htx_to_buf(cs_htx, buf);
1940 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1941 ret -= qcs_htx->data;
1942
1943 end:
1944 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001945 se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001946 }
1947 else {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001948 se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
1949 if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING))
1950 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001951
Amaury Denoyelle72a78e82022-07-29 15:34:12 +02001952 /* Set end-of-input if FIN received and all data extracted. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001953 if (fin)
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001954 se_fl_set(qcs->sd, SE_FL_EOI);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001955
1956 if (b_size(&qcs->rx.app_buf)) {
1957 b_free(&qcs->rx.app_buf);
1958 offer_buffers(NULL, 1);
1959 }
1960 }
1961
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001962 if (ret) {
1963 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001964 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001965 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001966
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001967 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
1968
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001969 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001970}
1971
Willy Tarreau3215e732022-05-27 10:09:11 +02001972static size_t qc_snd_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001973 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001974{
Willy Tarreau3215e732022-05-27 10:09:11 +02001975 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001976 size_t ret;
1977
1978 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001979
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001980 if (qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_RESET)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001981 ret = count;
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001982 goto end;
1983 }
1984
Willy Tarreau3215e732022-05-27 10:09:11 +02001985 ret = qcs->qcc->app_ops->snd_buf(sc, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001986
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001987 end:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001988 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
1989
1990 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001991}
1992
1993/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1994 * event subscriber <es> is not allowed to change from a previous call as long
1995 * as at least one event is still subscribed. The <event_type> must only be a
1996 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1997 */
Willy Tarreau3215e732022-05-27 10:09:11 +02001998static int qc_subscribe(struct stconn *sc, int event_type,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001999 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002000{
Willy Tarreau3215e732022-05-27 10:09:11 +02002001 return qcs_subscribe(__sc_mux_strm(sc), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002002}
2003
2004/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
2005 * The <es> pointer is not allowed to differ from the one passed to the
2006 * subscribe() call. It always returns zero.
2007 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002008static int qc_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002009{
Willy Tarreau3215e732022-05-27 10:09:11 +02002010 struct qcs *qcs = __sc_mux_strm(sc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002011
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002012 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
2013 BUG_ON(qcs->subs && qcs->subs != es);
2014
2015 es->events &= ~event_type;
2016 if (!es->events)
2017 qcs->subs = NULL;
2018
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002019 return 0;
2020}
2021
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002022/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Willy Tarreau4596fe22022-05-17 19:07:51 +02002023 * report SE_FL_ERR_PENDING|SE_FL_ERROR on the attached stream connectors and
2024 * wake them.
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002025 */
2026static int qc_wake_some_streams(struct qcc *qcc)
2027{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002028 struct qcs *qcs;
2029 struct eb64_node *node;
2030
2031 for (node = eb64_first(&qcc->streams_by_id); node;
2032 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02002033 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002034
Amaury Denoyelle3abeb572022-07-04 11:42:27 +02002035 if (!qcs_sc(qcs))
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002036 continue;
2037
2038 if (qcc->conn->flags & CO_FL_ERROR) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002039 se_fl_set(qcs->sd, SE_FL_ERR_PENDING);
2040 if (se_fl_test(qcs->sd, SE_FL_EOS))
2041 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002042
Amaury Denoyelle4561f842022-07-06 14:54:34 +02002043 qcs_alert(qcs);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002044 }
2045 }
2046
2047 return 0;
2048}
2049
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002050static int qc_wake(struct connection *conn)
2051{
2052 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02002053 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002054
2055 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002056
2057 /* Check if a soft-stop is in progress.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002058 *
2059 * TODO this is revelant for frontend connections only.
Amaury Denoyelled0c62322022-05-23 08:52:58 +02002060 *
2061 * TODO Client should be notified with a H3 GOAWAY and then a
2062 * CONNECTION_CLOSE. However, quic-conn uses the listener socket for
2063 * sending which at this stage is already closed.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002064 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002065 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Amaury Denoyelled0c62322022-05-23 08:52:58 +02002066 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002067
Willy Tarreau784b8682022-04-11 14:18:10 +02002068 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002069 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
2070
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002071 qc_send(qcc);
2072
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002073 qc_wake_some_streams(qcc);
2074
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002075 if (qcc_is_dead(qcc))
2076 goto release;
2077
2078 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
2079
2080 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002081
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002082 release:
2083 qc_release(qcc);
2084 TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002085 return 1;
2086}
2087
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002088
2089static char *qcs_st_to_str(enum qcs_state st)
2090{
2091 switch (st) {
2092 case QC_SS_IDLE: return "IDL";
2093 case QC_SS_OPEN: return "OPN";
2094 case QC_SS_HLOC: return "HCL";
2095 case QC_SS_HREM: return "HCR";
2096 case QC_SS_CLO: return "CLO";
2097 default: return "???";
2098 }
2099}
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01002100
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002101static void qmux_trace_frm(const struct quic_frame *frm)
2102{
2103 switch (frm->type) {
2104 case QUIC_FT_MAX_STREAMS_BIDI:
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002105 chunk_appendf(&trace_buf, " max_streams=%llu",
2106 (ull)frm->max_streams_bidi.max_streams);
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002107 break;
2108
2109 case QUIC_FT_MAX_STREAMS_UNI:
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002110 chunk_appendf(&trace_buf, " max_streams=%llu",
2111 (ull)frm->max_streams_uni.max_streams);
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002112 break;
2113
2114 default:
2115 break;
2116 }
2117}
2118
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01002119/* quic-mux trace handler */
2120static void qmux_trace(enum trace_level level, uint64_t mask,
2121 const struct trace_source *src,
2122 const struct ist where, const struct ist func,
2123 const void *a1, const void *a2, const void *a3, const void *a4)
2124{
2125 const struct connection *conn = a1;
2126 const struct qcc *qcc = conn ? conn->ctx : NULL;
2127 const struct qcs *qcs = a2;
2128
2129 if (!qcc)
2130 return;
2131
2132 if (src->verbosity > QMUX_VERB_CLEAN) {
2133 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
2134
2135 if (qcs)
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002136 chunk_appendf(&trace_buf, " qcs=%p .id=%llu .st=%s",
2137 qcs, (ull)qcs->id,
2138 qcs_st_to_str(qcs->st));
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002139
2140 if (mask & QMUX_EV_QCC_NQCS) {
2141 const uint64_t *id = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002142 chunk_appendf(&trace_buf, " id=%llu", (ull)*id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002143 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002144
2145 if (mask & QMUX_EV_SEND_FRM)
2146 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01002147
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02002148 if (mask & QMUX_EV_QCS_XFER_DATA) {
2149 const struct qcs_xfer_data_trace_arg *arg = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002150 chunk_appendf(&trace_buf, " prep=%llu xfer=%d",
2151 (ull)arg->prep, arg->xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02002152 }
2153
2154 if (mask & QMUX_EV_QCS_BUILD_STRM) {
2155 const struct qcs_build_stream_trace_arg *arg = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002156 chunk_appendf(&trace_buf, " len=%llu fin=%d offset=%llu",
2157 (ull)arg->len, arg->fin, (ull)arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01002158 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01002159 }
2160}
2161
2162
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002163static const struct mux_ops qc_ops = {
2164 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002165 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002166 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002167 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002168 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002169 .subscribe = qc_subscribe,
2170 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002171 .wake = qc_wake,
Willy Tarreaub5821e12022-04-26 11:54:08 +02002172 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02002173 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002174};
2175
2176static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002177 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002178
2179INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);