blob: ff0b8eab8619f6aa315ff49b7ae51bdc1b289999 [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) {
792 len -= qcs->rx.offset - offset;
793 offset = qcs->rx.offset;
794 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100795
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200796 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
797 if (ret != NCB_RET_OK) {
798 if (ret == NCB_RET_DATA_REJ) {
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200799 /* RFC 9000 2.2. Sending and Receiving Data
800 *
801 * An endpoint could receive data for a stream at the
802 * same stream offset multiple times. Data that has
803 * already been received can be discarded. The data at
804 * a given offset MUST NOT change if it is sent
805 * multiple times; an endpoint MAY treat receipt of
806 * different data at the same offset within a stream as
807 * a connection error of type PROTOCOL_VIOLATION.
808 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200809 TRACE_DEVEL("leaving on data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
810 qcc->conn, qcs);
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200811 qcc_emit_cc(qcc, QC_ERR_PROTOCOL_VIOLATION);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200812 }
813 else if (ret == NCB_RET_GAP_SIZE) {
814 TRACE_DEVEL("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
815 qcc->conn, qcs);
816 }
817 return 1;
818 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100819
820 if (fin)
Amaury Denoyelle3f39b402022-07-01 16:11:03 +0200821 qcs->flags |= QC_SF_SIZE_KNOWN;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100822
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200823 if (qcs->flags & QC_SF_SIZE_KNOWN && !ncb_is_fragmented(&qcs->rx.ncbuf))
824 qcs_close_remote(qcs);
825
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200826 if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL))
827 qcc_decode_qcs(qcc, qcs);
828
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200829 if (qcs->flags & QC_SF_READ_ABORTED) {
830 /* TODO should send a STOP_SENDING */
831 qcs_free(qcs);
832 }
833
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100834 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100835 return 0;
836}
837
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100838/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
839 * the frame.
840 *
841 * Returns 0 on success else non-zero.
842 */
843int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
844{
Amaury Denoyelle392e94e2022-07-06 15:44:16 +0200845 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
846
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100847 if (qcc->rfctl.md < max) {
848 qcc->rfctl.md = max;
Amaury Denoyelle392e94e2022-07-06 15:44:16 +0200849 TRACE_DEVEL("increase remote max-data", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100850
851 if (qcc->flags & QC_CF_BLK_MFCTL) {
852 qcc->flags &= ~QC_CF_BLK_MFCTL;
853 tasklet_wakeup(qcc->wait_event.tasklet);
854 }
855 }
Amaury Denoyelle392e94e2022-07-06 15:44:16 +0200856
857 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100858 return 0;
859}
860
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100861/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
862 * field of the frame and <id> is the identifier of the QUIC stream.
863 *
Amaury Denoyelleb68559a2022-07-06 15:45:20 +0200864 * Returns 0 on success else non-zero. On error, the received frame should not
865 * be acknowledged.
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100866 */
867int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
868{
869 struct qcs *qcs;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100870
Amaury Denoyelle392e94e2022-07-06 15:44:16 +0200871 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
872
Amaury Denoyelleb68559a2022-07-06 15:45:20 +0200873 /* RFC 9000 19.10. MAX_STREAM_DATA Frames
874 *
875 * Receiving a MAX_STREAM_DATA frame for a locally
876 * initiated stream that has not yet been created MUST be treated as a
877 * connection error of type STREAM_STATE_ERROR. An endpoint that
878 * receives a MAX_STREAM_DATA frame for a receive-only stream MUST
879 * terminate the connection with error STREAM_STATE_ERROR.
880 */
881 if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) {
882 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
883 return 1;
884 }
885
886 if (qcs) {
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100887 if (max > qcs->tx.msd) {
888 qcs->tx.msd = max;
Amaury Denoyelle392e94e2022-07-06 15:44:16 +0200889 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 +0100890
891 if (qcs->flags & QC_SF_BLK_SFCTL) {
892 qcs->flags &= ~QC_SF_BLK_SFCTL;
893 tasklet_wakeup(qcc->wait_event.tasklet);
894 }
895 }
896 }
897
Amaury Denoyelle392e94e2022-07-06 15:44:16 +0200898 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100899 return 0;
900}
901
Amaury Denoyellea5b50752022-07-04 11:44:53 +0200902/* Handle a new STOP_SENDING frame for stream ID <id>. The error code should be
903 * specified in <err>.
904 *
905 * Returns 0 on success else non-zero. On error, the received frame should not
906 * be acknowledged.
907 */
908int qcc_recv_stop_sending(struct qcc *qcc, uint64_t id, uint64_t err)
909{
910 struct qcs *qcs;
911
912 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
913
914 /* RFC 9000 19.5. STOP_SENDING Frames
915 *
916 * Receiving a STOP_SENDING frame for a
917 * locally initiated stream that has not yet been created MUST be
918 * treated as a connection error of type STREAM_STATE_ERROR. An
919 * endpoint that receives a STOP_SENDING frame for a receive-only stream
920 * MUST terminate the connection with error STREAM_STATE_ERROR.
921 */
922 if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) {
923 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
924 return 1;
925 }
926
927 if (!qcs)
928 goto out;
929
930 /* RFC 9000 3.5. Solicited State Transitions
931 *
932 * An endpoint that receives a STOP_SENDING frame
933 * MUST send a RESET_STREAM frame if the stream is in the "Ready" or
934 * "Send" state. If the stream is in the "Data Sent" state, the
935 * endpoint MAY defer sending the RESET_STREAM frame until the packets
936 * containing outstanding data are acknowledged or declared lost. If
937 * any outstanding data is declared lost, the endpoint SHOULD send a
938 * RESET_STREAM frame instead of retransmitting the data.
939 *
940 * An endpoint SHOULD copy the error code from the STOP_SENDING frame to
941 * the RESET_STREAM frame it sends, but it can use any application error
942 * code.
943 */
944 TRACE_DEVEL("receiving STOP_SENDING on stream", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
945 qcc_reset_stream(qcs, err);
946
947 out:
948 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
949 return 0;
950}
951
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200952/* Signal the closing of remote stream with id <id>. Flow-control for new
953 * streams may be allocated for the peer if needed.
954 */
955static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +0100956{
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200957 struct quic_frame *frm;
958
959 if (quic_stream_is_bidi(id)) {
960 ++qcc->lfctl.cl_bidi_r;
961 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
962 frm = pool_zalloc(pool_head_quic_frame);
963 BUG_ON(!frm); /* TODO handle this properly */
964
965 LIST_INIT(&frm->reflist);
966 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
967 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
968 qcc->lfctl.cl_bidi_r;
969 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
970 tasklet_wakeup(qcc->wait_event.tasklet);
971
972 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
973 qcc->lfctl.cl_bidi_r = 0;
974 }
975 }
976 else {
977 /* TODO */
978 }
979
980 return 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100981}
982
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500983/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100984static void qcs_destroy(struct qcs *qcs)
985{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100986 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200987 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100988
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100989 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100990
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200991 if (quic_stream_is_remote(qcs->qcc, id))
992 qcc_release_remote_stream(qcs->qcc, id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100993
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200994 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100995
996 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100997}
998
999static inline int qcc_is_dead(const struct qcc *qcc)
1000{
Amaury Denoyelle198d35f2022-04-01 17:56:58 +02001001 if (qcc->app_ops && qcc->app_ops->is_active &&
1002 qcc->app_ops->is_active(qcc, qcc->ctx))
1003 return 0;
1004
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001005 if ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task)
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001006 return 1;
1007
1008 return 0;
1009}
1010
1011/* Return true if the mux timeout should be armed. */
1012static inline int qcc_may_expire(struct qcc *qcc)
1013{
Willy Tarreau3215e732022-05-27 10:09:11 +02001014 return !qcc->nb_sc;
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001015}
1016
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001017/* Transfer as much as possible data on <qcs> from <in> to <out>. This is done
1018 * in respect with available flow-control at stream and connection level.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001019 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001020 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001021 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001022static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, struct buffer *in)
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001023{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001024 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001025 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001026 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001027
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001028 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001029
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001030 qc_get_buf(qcs, out);
1031
1032 /*
1033 * QCS out buffer diagram
1034 * head left to_xfer
1035 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001036 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001037 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001038 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001039 * ^ ack-off ^ sent-off ^ off
1040 *
1041 * STREAM frame
1042 * ^ ^
1043 * |xxxxxxxxxxxxxxxxx|
1044 */
1045
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001046 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001047 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001048 BUG_ON_HOT(qcc->tx.offsets < qcc->tx.sent_offsets);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001049
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001050 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001051 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001052
1053 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
1054 /* do not exceed flow control limit */
1055 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
1056 to_xfer = qcs->tx.msd - qcs->tx.offset;
1057
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001058 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001059 /* do not overcome flow control limit on connection */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001060 if (qcc->tx.offsets + to_xfer > qcc->rfctl.md)
1061 to_xfer = qcc->rfctl.md - qcc->tx.offsets;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001062
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001063 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001064 goto out;
1065
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001066 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001067
1068 out:
1069 {
1070 struct qcs_xfer_data_trace_arg arg = {
1071 .prep = b_data(out), .xfer = total,
1072 };
1073 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
1074 qcc->conn, qcs, &arg);
1075 }
1076
1077 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001078}
1079
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001080/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
1081 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
1082 * stream frame.
1083 *
1084 * Returns the length of the STREAM frame or a negative error code.
1085 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001086static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
1087 struct list *frm_list)
1088{
1089 struct qcc *qcc = qcs->qcc;
1090 struct quic_frame *frm;
1091 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001092 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001093
1094 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1095
Amaury Denoyellea4569202022-04-15 17:29:25 +02001096 /* if ack_offset < buf_offset, it points to an older buffer. */
1097 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
1098 BUG_ON(qcs->tx.sent_offset < base_off);
1099
1100 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001101 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001102 BUG_ON(total < 0);
1103
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001104 if (!total && !fin) {
1105 /* No need to send anything if total is NULL and no FIN to signal. */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001106 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1107 return 0;
1108 }
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001109 BUG_ON((!total && qcs->tx.sent_offset > qcs->tx.offset) ||
1110 (total && qcs->tx.sent_offset >= qcs->tx.offset));
Amaury Denoyellea4569202022-04-15 17:29:25 +02001111 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001112 BUG_ON(qcc->tx.sent_offsets + total > qcc->rfctl.md);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001113
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001114 frm = pool_zalloc(pool_head_quic_frame);
1115 if (!frm)
1116 goto err;
1117
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001118 LIST_INIT(&frm->reflist);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001119 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001120 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001121 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001122 frm->stream.buf = out;
1123 frm->stream.data = (unsigned char *)b_peek(out, head);
1124
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +01001125 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001126 if (fin)
1127 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001128
1129 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001130 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001131 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001132 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001133
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001134 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
1135 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001136
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001137 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001138
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001139 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001140 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001141 struct qcs_build_stream_trace_arg arg = {
1142 .len = frm->stream.len, .fin = fin,
1143 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001144 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001145 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001146 qcc->conn, qcs, &arg);
1147 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001148
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001149 return total;
1150
1151 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001152 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001153 return -1;
1154}
1155
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001156/* Check after transfering data from qcs.tx.buf if FIN must be set on the next
1157 * STREAM frame for <qcs>.
1158 *
1159 * Returns true if FIN must be set else false.
1160 */
1161static int qcs_stream_fin(struct qcs *qcs)
1162{
1163 return qcs->flags & QC_SF_FIN_STREAM && !b_data(&qcs->tx.buf);
1164}
1165
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001166/* This function must be called by the upper layer to inform about the sending
1167 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
1168 * <offset>.
1169 */
1170void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
1171{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001172 struct qcc *qcc = qcs->qcc;
1173 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001174
1175 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001176 BUG_ON(offset + data > qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001177
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001178 /* check if the STREAM frame has already been notified. It can happen
1179 * for retransmission.
1180 */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001181 if (offset + data < qcs->tx.sent_offset)
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001182 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001183
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001184 qcs_idle_open(qcs);
1185
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001186 diff = offset + data - qcs->tx.sent_offset;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001187 if (diff) {
1188 /* increase offset sum on connection */
1189 qcc->tx.sent_offsets += diff;
1190 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
1191 if (qcc->tx.sent_offsets == qcc->rfctl.md)
1192 qcc->flags |= QC_CF_BLK_MFCTL;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001193
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001194 /* increase offset on stream */
1195 qcs->tx.sent_offset += diff;
1196 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
1197 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
1198 if (qcs->tx.sent_offset == qcs->tx.msd)
1199 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001200
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001201 if (qcs->tx.offset == qcs->tx.sent_offset &&
1202 b_full(&qcs->stream->buf->buf)) {
1203 qc_stream_buf_release(qcs->stream);
1204 /* prepare qcs for immediate send retry if data to send */
1205 if (b_data(&qcs->tx.buf))
1206 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
1207 }
1208 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001209
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001210 if (qcs->tx.offset == qcs->tx.sent_offset && qcs_stream_fin(qcs)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001211 /* Close stream locally. */
1212 qcs_close_local(qcs);
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001213 /* Reset flag to not emit multiple FIN STREAM frames. */
1214 qcs->flags &= ~QC_SF_FIN_STREAM;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001215 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001216}
1217
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001218/* Wrapper for send on transport layer. Send a list of frames <frms> for the
1219 * connection <qcc>.
1220 *
1221 * Returns 0 if all data sent with success else non-zero.
1222 */
1223static int qc_send_frames(struct qcc *qcc, struct list *frms)
1224{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001225 /* TODO implement an opportunistic retry mechanism. This is needed
1226 * because qc_send_app_pkts is not completed. It will only prepare data
1227 * up to its Tx buffer. The frames left are not send even if the Tx
1228 * buffer is emptied by the sendto call.
1229 *
1230 * To overcome this, we call repeatedly qc_send_app_pkts until we
1231 * detect that the transport layer has send nothing. This could happen
1232 * on congestion or sendto syscall error.
1233 *
1234 * When qc_send_app_pkts is improved to handle retry by itself, we can
1235 * remove the looping from the MUX.
1236 */
1237 struct quic_frame *first_frm;
1238 uint64_t first_offset = 0;
1239 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001240
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001241 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
1242
1243 if (LIST_ISEMPTY(frms)) {
1244 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001245 return 1;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001246 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +01001247
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001248 LIST_INIT(&qcc->send_retry_list);
1249
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001250 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001251 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
1252 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
1253 first_offset = first_frm->stream.offset.key;
1254 first_stream_frame_type = 1;
1255 }
1256 else {
1257 first_stream_frame_type = 0;
1258 }
1259
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001260 if (!LIST_ISEMPTY(frms))
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02001261 qc_send_app_pkts(qcc->conn->handle.qc, 0, frms);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001262
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001263 /* If there is frames left, check if the transport layer has send some
1264 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001265 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001266 if (!LIST_ISEMPTY(frms)) {
1267 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
1268 goto retry_send;
1269
1270 /* If the first frame is STREAM, check if its offset has
1271 * changed.
1272 */
1273 if (first_stream_frame_type &&
1274 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
1275 goto retry_send;
1276 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001277 }
1278
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001279 /* If there is frames left at this stage, transport layer is blocked.
1280 * Subscribe on it to retry later.
1281 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001282 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001283 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001284 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1285 SUB_RETRY_SEND, &qcc->wait_event);
1286 return 1;
1287 }
1288
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001289 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1290
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001291 return 0;
1292}
1293
1294/* Emit a RESET_STREAM on <qcs>.
1295 *
1296 * Returns 0 if the frame has been successfully sent else non-zero.
1297 */
1298static int qcs_send_reset(struct qcs *qcs)
1299{
1300 struct list frms = LIST_HEAD_INIT(frms);
1301 struct quic_frame *frm;
1302
1303 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1304
1305 frm = pool_zalloc(pool_head_quic_frame);
1306 if (!frm)
1307 return 1;
1308
1309 LIST_INIT(&frm->reflist);
1310 frm->type = QUIC_FT_RESET_STREAM;
1311 frm->reset_stream.id = qcs->id;
1312 frm->reset_stream.app_error_code = qcs->err;
1313 frm->reset_stream.final_size = qcs->tx.sent_offset;
1314
1315 LIST_APPEND(&frms, &frm->list);
1316 if (qc_send_frames(qcs->qcc, &frms)) {
1317 pool_free(pool_head_quic_frame, frm);
1318 TRACE_DEVEL("cannot send RESET_STREAM", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1319 return 1;
1320 }
1321
1322 if (qcs_sc(qcs)) {
1323 se_fl_set_error(qcs->sd);
1324 qcs_alert(qcs);
1325 }
1326
1327 qcs_close_local(qcs);
1328 qcs->flags &= ~QC_SF_TO_RESET;
1329
1330 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001331 return 0;
1332}
1333
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001334/* Used internally by qc_send function. Proceed to send for <qcs>. This will
1335 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001336 * is then generated and inserted in <frms> list.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001337 *
1338 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
1339 * be null if out buffer cannot be allocated.
1340 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001341static int _qc_send_qcs(struct qcs *qcs, struct list *frms)
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001342{
1343 struct qcc *qcc = qcs->qcc;
1344 struct buffer *buf = &qcs->tx.buf;
1345 struct buffer *out = qc_stream_buf_get(qcs->stream);
1346 int xfer = 0;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001347 char fin = 0;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001348
1349 /* Allocate <out> buffer if necessary. */
1350 if (!out) {
1351 if (qcc->flags & QC_CF_CONN_FULL)
1352 return 0;
1353
1354 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
1355 if (!out) {
1356 qcc->flags |= QC_CF_CONN_FULL;
1357 return 0;
1358 }
1359 }
1360
1361 /* Transfer data from <buf> to <out>. */
1362 if (b_data(buf)) {
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001363 xfer = qcs_xfer_data(qcs, out, buf);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001364 if (xfer > 0) {
1365 qcs_notify_send(qcs);
1366 qcs->flags &= ~QC_SF_BLK_MROOM;
1367 }
1368
1369 qcs->tx.offset += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001370 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001371 qcc->tx.offsets += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001372 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001373 }
1374
1375 /* out buffer cannot be emptied if qcs offsets differ. */
1376 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1377
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001378 /* FIN is set if all incoming data were transfered. */
1379 fin = qcs_stream_fin(qcs);
1380
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001381 /* Build a new STREAM frame with <out> buffer. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001382 if (qcs->tx.sent_offset != qcs->tx.offset || fin) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001383 int ret;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001384 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +02001385 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001386 }
1387
1388 return xfer;
1389}
1390
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001391/* Proceed to sending. Loop through all available streams for the <qcc>
1392 * instance and try to send as much as possible.
1393 *
1394 * Returns the total of bytes sent to the transport layer.
1395 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001396static int qc_send(struct qcc *qcc)
1397{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001398 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001399 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001400 struct qcs *qcs, *qcs_tmp;
1401 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001402
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001403 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001404
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001405 if (qcc->conn->flags & CO_FL_SOCK_WR_SH || qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001406 qcc->conn->flags |= CO_FL_ERROR;
1407 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
1408 return 0;
1409 }
1410
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001411 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1412 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
1413 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
1414 goto out;
1415 }
1416 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02001417
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001418 if (qcc->flags & QC_CF_BLK_MFCTL)
1419 return 0;
1420
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001421 /* loop through all streams, construct STREAM frames if data available.
1422 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001423 */
1424 node = eb64_first(&qcc->streams_by_id);
1425 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001426 int ret;
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001427 uint64_t id;
1428
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001429 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001430 id = qcs->id;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001431
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001432 if (quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001433 node = eb64_next(node);
1434 continue;
1435 }
1436
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001437 if (qcs->flags & QC_SF_TO_RESET) {
1438 qcs_send_reset(qcs);
1439 node = eb64_next(node);
1440 continue;
1441 }
1442
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001443 if (qcs_is_close_local(qcs)) {
1444 node = eb64_next(node);
1445 continue;
1446 }
1447
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001448 if (qcs->flags & QC_SF_BLK_SFCTL) {
1449 node = eb64_next(node);
1450 continue;
1451 }
1452
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001453 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001454 node = eb64_next(node);
1455 continue;
1456 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001457
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001458 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001459 total += ret;
1460 node = eb64_next(node);
1461 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001462
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001463 if (qc_send_frames(qcc, &frms)) {
1464 /* data rejected by transport layer, do not retry. */
1465 goto out;
1466 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001467
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001468 retry:
1469 tmp_total = 0;
1470 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
1471 int ret;
1472 BUG_ON(!b_data(&qcs->tx.buf));
1473 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001474
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001475 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001476 tmp_total += ret;
1477 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001478 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001479
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001480 total += tmp_total;
1481 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
1482 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +01001483
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001484 out:
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02001485 /* Deallocate frames that the transport layer has rejected. */
1486 if (!LIST_ISEMPTY(&frms)) {
1487 struct quic_frame *frm, *frm2;
1488 list_for_each_entry_safe(frm, frm2, &frms, list) {
1489 LIST_DELETE(&frm->list);
1490 pool_free(pool_head_quic_frame, frm);
1491 }
1492 }
1493
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001494 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1495
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001496 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001497}
1498
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001499/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1500 * operation.
1501 *
1502 * Returns 0 on success else non-zero.
1503 */
1504static int qc_recv(struct qcc *qcc)
1505{
1506 struct eb64_node *node;
1507 struct qcs *qcs;
1508
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001509 TRACE_ENTER(QMUX_EV_QCC_RECV);
1510
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001511 if (qcc->flags & QC_CF_CC_EMIT) {
1512 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
1513 return 0;
1514 }
1515
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001516 node = eb64_first(&qcc->streams_by_id);
1517 while (node) {
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001518 uint64_t id;
1519
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001520 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001521 id = qcs->id;
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001522
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001523 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001524 node = eb64_next(node);
1525 continue;
1526 }
1527
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001528 if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001529 node = eb64_next(node);
1530 continue;
1531 }
1532
1533 qcc_decode_qcs(qcc, qcs);
1534 node = eb64_next(node);
Amaury Denoyelle849b24f2022-05-24 17:22:07 +02001535
1536 if (qcs->flags & QC_SF_READ_ABORTED) {
1537 /* TODO should send a STOP_SENDING */
1538 qcs_free(qcs);
1539 }
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001540 }
1541
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001542 TRACE_LEAVE(QMUX_EV_QCC_RECV);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001543 return 0;
1544}
1545
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001546
1547/* Release all streams which have their transfer operation achieved.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001548 *
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001549 * Returns true if at least one stream is released.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001550 */
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001551static int qc_purge_streams(struct qcc *qcc)
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001552{
1553 struct eb64_node *node;
1554 int release = 0;
1555
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001556 TRACE_ENTER(QMUX_EV_QCC_WAKE);
1557
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001558 node = eb64_first(&qcc->streams_by_id);
1559 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001560 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001561 node = eb64_next(node);
1562
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001563 /* Release not attached closed streams. */
1564 if (qcs->st == QC_SS_CLO && !qcs_sc(qcs)) {
1565 TRACE_DEVEL("purging closed stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
1566 qcs_destroy(qcs);
1567 release = 1;
1568 continue;
1569 }
1570
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001571 /* Release detached streams with empty buffer. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001572 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02001573 if (qcs_is_close_local(qcs)) {
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001574 TRACE_DEVEL("purging detached stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001575 qcs_destroy(qcs);
1576 release = 1;
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001577 continue;
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001578 }
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001579
1580 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1581 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001582 }
1583 }
1584
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001585 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001586 return release;
1587}
1588
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001589/* release function. This one should be called to free all resources allocated
1590 * to the mux.
1591 */
1592static void qc_release(struct qcc *qcc)
1593{
1594 struct connection *conn = qcc->conn;
1595 struct eb64_node *node;
1596
1597 TRACE_ENTER(QMUX_EV_QCC_END);
1598
1599 if (qcc->app_ops && qcc->app_ops->release) {
1600 /* Application protocol with dedicated connection closing
1601 * procedure.
1602 */
1603 qcc->app_ops->release(qcc->ctx);
Amaury Denoyellea154dc02022-06-13 17:09:01 +02001604
1605 /* useful if application protocol should emit some closing
1606 * frames. For example HTTP/3 GOAWAY frame.
1607 */
1608 qc_send(qcc);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001609 }
1610 else {
1611 qcc_emit_cc_app(qcc, QC_ERR_NO_ERROR, 0);
1612 }
1613
1614 if (qcc->task) {
1615 task_destroy(qcc->task);
1616 qcc->task = NULL;
1617 }
1618
1619 if (qcc->wait_event.tasklet)
1620 tasklet_free(qcc->wait_event.tasklet);
1621 if (conn && qcc->wait_event.events) {
1622 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
1623 qcc->wait_event.events,
1624 &qcc->wait_event);
1625 }
1626
1627 /* liberate remaining qcs instances */
1628 node = eb64_first(&qcc->streams_by_id);
1629 while (node) {
1630 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
1631 node = eb64_next(node);
1632 qcs_free(qcs);
1633 }
1634
1635 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1636 struct quic_frame *frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list);
1637 LIST_DELETE(&frm->list);
1638 pool_free(pool_head_quic_frame, frm);
1639 }
1640
1641 pool_free(pool_head_qcc, qcc);
1642
1643 if (conn) {
1644 LIST_DEL_INIT(&conn->stopping_list);
1645
1646 conn->handle.qc->conn = NULL;
1647 conn->mux = NULL;
1648 conn->ctx = NULL;
1649
1650 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
1651
1652 conn_stop_tracking(conn);
1653 conn_full_close(conn);
1654 if (conn->destroy_cb)
1655 conn->destroy_cb(conn);
1656 conn_free(conn);
1657 }
1658
1659 TRACE_LEAVE(QMUX_EV_QCC_END);
1660}
1661
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001662static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1663{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001664 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001665
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001666 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001667
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001668 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001669
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001670 if (qc_purge_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001671 if (qcc_is_dead(qcc)) {
1672 qc_release(qcc);
1673 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001674 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001675 if (qcc_may_expire(qcc))
1676 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1677 else
1678 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001679 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001680 }
1681 }
1682
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001683 qc_recv(qcc);
1684
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001685 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1686
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001687 return NULL;
1688}
1689
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001690static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1691{
1692 struct qcc *qcc = ctx;
1693 int expired = tick_is_expired(t->expire, now_ms);
1694
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001695 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001696
1697 if (qcc) {
1698 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001699 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001700 return t;
1701 }
1702
1703 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001704 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001705 t->expire = TICK_ETERNITY;
1706 return t;
1707 }
1708 }
1709
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001710 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001711
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001712 if (!qcc) {
1713 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001714 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001715 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001716
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001717 qcc->task = NULL;
1718
1719 if (qcc_is_dead(qcc))
1720 qc_release(qcc);
1721
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001722 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1723
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001724 return NULL;
1725}
1726
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001727static int qc_init(struct connection *conn, struct proxy *prx,
1728 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001729{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001730 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001731 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001732
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001733 qcc = pool_alloc(pool_head_qcc);
1734 if (!qcc)
1735 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001736
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001737 qcc->conn = conn;
1738 conn->ctx = qcc;
Willy Tarreau3215e732022-05-27 10:09:11 +02001739 qcc->nb_sc = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001740 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001741
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001742 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001743
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001744 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001745
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001746 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001747 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001748
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001749 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001750 qcc->tx.sent_offsets = qcc->tx.offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001751
1752 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001753 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001754 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001755 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001756 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001757
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001758 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001759 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001760 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001761 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001762
1763 /* Server initiated streams must respect the server flow control. */
1764 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001765 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001766 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001767 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1768
1769 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001770 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001771 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001772 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001773
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001774 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001775 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02001776 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
1777 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001778 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001779
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001780 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001781 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001782
Willy Tarreau784b8682022-04-11 14:18:10 +02001783 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001784 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001785 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1786 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1787
Amaury Denoyellea509ffb2022-07-04 15:50:33 +02001788 if (conn_is_back(conn)) {
1789 qcc->next_bidi_l = 0x00;
1790 qcc->largest_bidi_r = 0x01;
1791 qcc->next_uni_l = 0x02;
1792 qcc->largest_uni_r = 0x03;
1793 }
1794 else {
1795 qcc->largest_bidi_r = 0x00;
1796 qcc->next_bidi_l = 0x01;
1797 qcc->largest_uni_r = 0x02;
1798 qcc->next_uni_l = 0x03;
1799 }
1800
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001801 qcc->wait_event.tasklet = tasklet_new();
1802 if (!qcc->wait_event.tasklet)
1803 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001804
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001805 LIST_INIT(&qcc->send_retry_list);
1806
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001807 qcc->subs = NULL;
1808 qcc->wait_event.tasklet->process = qc_io_cb;
1809 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001810 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001811
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001812 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001813 qcc->task = NULL;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001814 qcc->timeout = prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001815 if (tick_isset(qcc->timeout)) {
1816 qcc->task = task_new_here();
1817 if (!qcc->task)
1818 goto fail_no_timeout_task;
1819 qcc->task->process = qc_timeout_task;
1820 qcc->task->context = qcc;
1821 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1822 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001823
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001824 if (!conn_is_back(conn)) {
1825 if (!LIST_INLIST(&conn->stopping_list)) {
1826 LIST_APPEND(&mux_stopping_data[tid].list,
1827 &conn->stopping_list);
1828 }
1829 }
1830
Willy Tarreau784b8682022-04-11 14:18:10 +02001831 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001832 /* init read cycle */
1833 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001834
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001835 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001836
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001837 fail_no_timeout_task:
1838 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001839 fail_no_tasklet:
1840 pool_free(pool_head_qcc, qcc);
1841 fail_no_qcc:
1842 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001843}
1844
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001845static void qc_destroy(void *ctx)
1846{
1847 struct qcc *qcc = ctx;
1848
1849 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
1850 qc_release(qcc);
1851 TRACE_LEAVE(QMUX_EV_QCC_END);
1852}
1853
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001854static void qc_detach(struct sedesc *sd)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001855{
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001856 struct qcs *qcs = sd->se;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001857 struct qcc *qcc = qcs->qcc;
1858
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001859 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001860
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001861 /* TODO this BUG_ON_HOT() is not correct as the stconn layer may detach
1862 * from the stream even if it is not closed remotely at the QUIC layer.
1863 * This happens for example when a stream must be closed due to a
1864 * rejected request. To better handle these cases, it will be required
1865 * to implement shutr/shutw MUX operations. Once this is done, this
1866 * BUG_ON_HOT() statement can be adjusted.
1867 */
1868 //BUG_ON_HOT(!qcs_is_close_remote(qcs));
Willy Tarreau3215e732022-05-27 10:09:11 +02001869 --qcc->nb_sc;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001870
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02001871 if (!qcs_is_close_local(qcs) && !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001872 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001873 qcs->flags |= QC_SF_DETACH;
1874 return;
1875 }
1876
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001877 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001878
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001879 if (qcc_is_dead(qcc)) {
1880 qc_release(qcc);
1881 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001882 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001883 if (qcc_may_expire(qcc))
1884 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1885 else
1886 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001887 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001888 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001889
1890 TRACE_LEAVE(QMUX_EV_STRM_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001891}
1892
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001893/* Called from the upper layer, to receive data */
Willy Tarreau3215e732022-05-27 10:09:11 +02001894static size_t qc_rcv_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001895 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001896{
Willy Tarreau3215e732022-05-27 10:09:11 +02001897 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001898 struct htx *qcs_htx = NULL;
1899 struct htx *cs_htx = NULL;
1900 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001901 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001902
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001903 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001904
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001905 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
1906 if (htx_is_empty(qcs_htx)) {
1907 /* Set buffer data to 0 as HTX is empty. */
1908 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1909 goto end;
1910 }
1911
1912 ret = qcs_htx->data;
1913
1914 cs_htx = htx_from_buf(buf);
1915 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
1916 htx_to_buf(cs_htx, buf);
1917 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1918 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
1919 goto end;
1920 }
1921
1922 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
1923 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
1924
1925 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001926 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
1927 cs_htx->flags |= HTX_FL_EOM;
1928 fin = 1;
1929 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001930
1931 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
1932 htx_to_buf(cs_htx, buf);
1933 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1934 ret -= qcs_htx->data;
1935
1936 end:
1937 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001938 se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001939 }
1940 else {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001941 se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
1942 if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING))
1943 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001944
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001945 if (fin)
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001946 se_fl_set(qcs->sd, SE_FL_EOI);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001947
1948 if (b_size(&qcs->rx.app_buf)) {
1949 b_free(&qcs->rx.app_buf);
1950 offer_buffers(NULL, 1);
1951 }
1952 }
1953
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001954 if (ret) {
1955 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001956 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001957 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001958
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001959 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
1960
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001961 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001962}
1963
Willy Tarreau3215e732022-05-27 10:09:11 +02001964static size_t qc_snd_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001965 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001966{
Willy Tarreau3215e732022-05-27 10:09:11 +02001967 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001968 size_t ret;
1969
1970 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001971
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001972 if (qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_RESET)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001973 ret = count;
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001974 goto end;
1975 }
1976
Willy Tarreau3215e732022-05-27 10:09:11 +02001977 ret = qcs->qcc->app_ops->snd_buf(sc, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001978
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001979 end:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001980 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
1981
1982 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001983}
1984
1985/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1986 * event subscriber <es> is not allowed to change from a previous call as long
1987 * as at least one event is still subscribed. The <event_type> must only be a
1988 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1989 */
Willy Tarreau3215e732022-05-27 10:09:11 +02001990static int qc_subscribe(struct stconn *sc, int event_type,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001991 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001992{
Willy Tarreau3215e732022-05-27 10:09:11 +02001993 return qcs_subscribe(__sc_mux_strm(sc), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001994}
1995
1996/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1997 * The <es> pointer is not allowed to differ from the one passed to the
1998 * subscribe() call. It always returns zero.
1999 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002000static int qc_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002001{
Willy Tarreau3215e732022-05-27 10:09:11 +02002002 struct qcs *qcs = __sc_mux_strm(sc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002003
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002004 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
2005 BUG_ON(qcs->subs && qcs->subs != es);
2006
2007 es->events &= ~event_type;
2008 if (!es->events)
2009 qcs->subs = NULL;
2010
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002011 return 0;
2012}
2013
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002014/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Willy Tarreau4596fe22022-05-17 19:07:51 +02002015 * report SE_FL_ERR_PENDING|SE_FL_ERROR on the attached stream connectors and
2016 * wake them.
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002017 */
2018static int qc_wake_some_streams(struct qcc *qcc)
2019{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002020 struct qcs *qcs;
2021 struct eb64_node *node;
2022
2023 for (node = eb64_first(&qcc->streams_by_id); node;
2024 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02002025 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002026
Amaury Denoyelle3abeb572022-07-04 11:42:27 +02002027 if (!qcs_sc(qcs))
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002028 continue;
2029
2030 if (qcc->conn->flags & CO_FL_ERROR) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002031 se_fl_set(qcs->sd, SE_FL_ERR_PENDING);
2032 if (se_fl_test(qcs->sd, SE_FL_EOS))
2033 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002034
Amaury Denoyelle4561f842022-07-06 14:54:34 +02002035 qcs_alert(qcs);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002036 }
2037 }
2038
2039 return 0;
2040}
2041
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002042static int qc_wake(struct connection *conn)
2043{
2044 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02002045 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002046
2047 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002048
2049 /* Check if a soft-stop is in progress.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002050 *
2051 * TODO this is revelant for frontend connections only.
Amaury Denoyelled0c62322022-05-23 08:52:58 +02002052 *
2053 * TODO Client should be notified with a H3 GOAWAY and then a
2054 * CONNECTION_CLOSE. However, quic-conn uses the listener socket for
2055 * sending which at this stage is already closed.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002056 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002057 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Amaury Denoyelled0c62322022-05-23 08:52:58 +02002058 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002059
Willy Tarreau784b8682022-04-11 14:18:10 +02002060 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002061 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
2062
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002063 qc_send(qcc);
2064
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002065 qc_wake_some_streams(qcc);
2066
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002067 if (qcc_is_dead(qcc))
2068 goto release;
2069
2070 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
2071
2072 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002073
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002074 release:
2075 qc_release(qcc);
2076 TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002077 return 1;
2078}
2079
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002080
2081static char *qcs_st_to_str(enum qcs_state st)
2082{
2083 switch (st) {
2084 case QC_SS_IDLE: return "IDL";
2085 case QC_SS_OPEN: return "OPN";
2086 case QC_SS_HLOC: return "HCL";
2087 case QC_SS_HREM: return "HCR";
2088 case QC_SS_CLO: return "CLO";
2089 default: return "???";
2090 }
2091}
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01002092
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002093static void qmux_trace_frm(const struct quic_frame *frm)
2094{
2095 switch (frm->type) {
2096 case QUIC_FT_MAX_STREAMS_BIDI:
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002097 chunk_appendf(&trace_buf, " max_streams=%llu",
2098 (ull)frm->max_streams_bidi.max_streams);
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002099 break;
2100
2101 case QUIC_FT_MAX_STREAMS_UNI:
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002102 chunk_appendf(&trace_buf, " max_streams=%llu",
2103 (ull)frm->max_streams_uni.max_streams);
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002104 break;
2105
2106 default:
2107 break;
2108 }
2109}
2110
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01002111/* quic-mux trace handler */
2112static void qmux_trace(enum trace_level level, uint64_t mask,
2113 const struct trace_source *src,
2114 const struct ist where, const struct ist func,
2115 const void *a1, const void *a2, const void *a3, const void *a4)
2116{
2117 const struct connection *conn = a1;
2118 const struct qcc *qcc = conn ? conn->ctx : NULL;
2119 const struct qcs *qcs = a2;
2120
2121 if (!qcc)
2122 return;
2123
2124 if (src->verbosity > QMUX_VERB_CLEAN) {
2125 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
2126
2127 if (qcs)
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002128 chunk_appendf(&trace_buf, " qcs=%p .id=%llu .st=%s",
2129 qcs, (ull)qcs->id,
2130 qcs_st_to_str(qcs->st));
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002131
2132 if (mask & QMUX_EV_QCC_NQCS) {
2133 const uint64_t *id = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002134 chunk_appendf(&trace_buf, " id=%llu", (ull)*id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002135 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002136
2137 if (mask & QMUX_EV_SEND_FRM)
2138 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01002139
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02002140 if (mask & QMUX_EV_QCS_XFER_DATA) {
2141 const struct qcs_xfer_data_trace_arg *arg = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002142 chunk_appendf(&trace_buf, " prep=%llu xfer=%d",
2143 (ull)arg->prep, arg->xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02002144 }
2145
2146 if (mask & QMUX_EV_QCS_BUILD_STRM) {
2147 const struct qcs_build_stream_trace_arg *arg = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002148 chunk_appendf(&trace_buf, " len=%llu fin=%d offset=%llu",
2149 (ull)arg->len, arg->fin, (ull)arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01002150 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01002151 }
2152}
2153
2154
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002155static const struct mux_ops qc_ops = {
2156 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002157 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002158 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002159 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002160 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002161 .subscribe = qc_subscribe,
2162 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002163 .wake = qc_wake,
Willy Tarreaub5821e12022-04-26 11:54:08 +02002164 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02002165 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002166};
2167
2168static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002169 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002170
2171INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);