blob: c47e83a2c2da602b858a6dc2f75f2a8bb60356db [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>
Christopher Faulet1329f2a2021-12-16 17:32:56 +01007#include <haproxy/conn_stream.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +01008#include <haproxy/dynbuf.h>
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01009#include <haproxy/htx.h>
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +020010#include <haproxy/list.h>
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +020011#include <haproxy/ncbuf.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +010012#include <haproxy/pool.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020013#include <haproxy/quic_stream.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020014#include <haproxy/ssl_sock-t.h>
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010015#include <haproxy/trace.h>
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +010016#include <haproxy/xprt_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010017
Amaury Denoyelledeed7772021-12-03 11:36:46 +010018DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010019DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
20
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010021/* trace source and events */
22static void qmux_trace(enum trace_level level, uint64_t mask,
23 const struct trace_source *src,
24 const struct ist where, const struct ist func,
25 const void *a1, const void *a2, const void *a3, const void *a4);
26
27static const struct trace_event qmux_trace_events[] = {
Amaury Denoyelle4f137572022-03-24 17:10:00 +010028#define QMUX_EV_QCC_RECV (1ULL << 1)
29 { .mask = QMUX_EV_QCC_RECV, .name = "qcc_recv", .desc = "Rx on QUIC connection" },
30#define QMUX_EV_QCC_SEND (1ULL << 2)
31 { .mask = QMUX_EV_QCC_SEND, .name = "qcc_send", .desc = "Tx on QUIC connection" },
32#define QMUX_EV_QCC_WAKE (1ULL << 3)
33 { .mask = QMUX_EV_QCC_WAKE, .name = "qcc_wake", .desc = "QUIC connection woken up" },
34#define QMUX_EV_QCC_END (1ULL << 4)
35 { .mask = QMUX_EV_QCC_END, .name = "qcc_end", .desc = "QUIC connection terminated" },
36#define QMUX_EV_QCC_NQCS (1ULL << 5)
37 { .mask = QMUX_EV_QCC_NQCS, .name = "qcc_no_qcs", .desc = "QUIC stream not found" },
38#define QMUX_EV_QCS_NEW (1ULL << 6)
39 { .mask = QMUX_EV_QCS_NEW, .name = "qcs_new", .desc = "new QUIC stream" },
40#define QMUX_EV_QCS_RECV (1ULL << 7)
41 { .mask = QMUX_EV_QCS_RECV, .name = "qcs_recv", .desc = "Rx on QUIC stream" },
42#define QMUX_EV_QCS_SEND (1ULL << 8)
43 { .mask = QMUX_EV_QCS_SEND, .name = "qcs_send", .desc = "Tx on QUIC stream" },
44#define QMUX_EV_QCS_END (1ULL << 9)
45 { .mask = QMUX_EV_QCS_END, .name = "qcs_end", .desc = "QUIC stream terminated" },
46#define QMUX_EV_STRM_RECV (1ULL << 10)
47 { .mask = QMUX_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
48#define QMUX_EV_STRM_SEND (1ULL << 11)
49 { .mask = QMUX_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
50#define QMUX_EV_STRM_END (1ULL << 12)
51 { .mask = QMUX_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
Amaury Denoyellefa29f332022-03-25 09:09:40 +010052#define QMUX_EV_SEND_FRM (1ULL << 13)
53 { .mask = QMUX_EV_SEND_FRM, .name = "send_frm", .desc = "sending QUIC frame" },
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020054/* special event dedicated to qcs_xfer_data */
55#define QMUX_EV_QCS_XFER_DATA (1ULL << 14)
56 { .mask = QMUX_EV_QCS_XFER_DATA, .name = "qcs_xfer_data", .desc = "qcs_xfer_data" },
57/* special event dedicated to qcs_build_stream_frm */
58#define QMUX_EV_QCS_BUILD_STRM (1ULL << 15)
59 { .mask = QMUX_EV_QCS_BUILD_STRM, .name = "qcs_build_stream_frm", .desc = "qcs_build_stream_frm" },
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010060 { }
61};
62
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020063/* custom arg for QMUX_EV_QCS_XFER_DATA */
64struct qcs_xfer_data_trace_arg {
65 size_t prep;
Amaury Denoyellefdcec362022-03-25 09:28:10 +010066 int xfer;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020067};
68/* custom arg for QMUX_EV_QCS_BUILD_STRM */
69struct qcs_build_stream_trace_arg {
70 size_t len;
Amaury Denoyellefdcec362022-03-25 09:28:10 +010071 char fin;
72 uint64_t offset;
73};
74
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010075static const struct name_desc qmux_trace_lockon_args[4] = {
76 /* arg1 */ { /* already used by the connection */ },
77 /* arg2 */ { .name="qcs", .desc="QUIC stream" },
78 /* arg3 */ { },
79 /* arg4 */ { }
80};
81
82static const struct name_desc qmux_trace_decoding[] = {
83#define QMUX_VERB_CLEAN 1
84 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
85#define QMUX_VERB_MINIMAL 2
86 { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" },
87 { /* end */ }
88};
89
90struct trace_source trace_qmux = {
91 .name = IST("qmux"),
92 .desc = "QUIC multiplexer",
93 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
94 .default_cb = qmux_trace,
95 .known_events = qmux_trace_events,
96 .lockon_args = qmux_trace_lockon_args,
97 .decoding = qmux_trace_decoding,
98 .report_events = ~0, /* report everything by default */
99};
100
101#define TRACE_SOURCE &trace_qmux
102INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
103
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200104/* Emit a CONNECTION_CLOSE with error <err>. This will interrupt all future
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200105 * send/receive operations.
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200106 */
107static void qcc_emit_cc(struct qcc *qcc, int err)
108{
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200109 quic_set_connection_close(qcc->conn->handle.qc, err, 0);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200110 qcc->flags |= QC_CF_CC_EMIT;
111 tasklet_wakeup(qcc->wait_event.tasklet);
112}
113
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100114/* Allocate a new QUIC streams with id <id> and type <type>. */
115struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100116{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100117 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100118
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100119 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
120
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100121 qcs = pool_alloc(pool_head_qcs);
122 if (!qcs)
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200123 return NULL;
124
125 qcs->stream = NULL;
126 qcs->qcc = qcc;
Willy Tarreau01c2a4a2022-05-10 15:46:10 +0200127 qcs->endp = NULL;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200128 qcs->flags = QC_SF_NONE;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200129 qcs->ctx = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100130
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200131 /* Allocate transport layer stream descriptor. Only needed for TX. */
132 if (!quic_stream_is_uni(id) || !quic_stream_is_remote(qcc, id)) {
133 struct quic_conn *qc = qcc->conn->handle.qc;
134 qcs->stream = qc_stream_desc_new(id, type, qcs, qc);
135 if (!qcs->stream)
136 goto err;
137 }
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200138
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +0200139 qcs->id = qcs->by_id.key = id;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200140 if (qcc->app_ops->attach) {
141 if (qcc->app_ops->attach(qcs))
142 goto err;
143 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100144
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200145 /* store transport layer stream descriptor in qcc tree */
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200146 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100147
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100148 qcc->strms[type].nb_streams++;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100149
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100150 /* If stream is local, use peer remote-limit, or else the opposite. */
151 /* TODO use uni limit for unidirectional streams */
152 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
153 qcc->rfctl.msd_bidi_l;
154
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200155 qcs->rx.ncbuf = NCBUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100156 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200157 qcs->rx.offset = qcs->rx.offset_max = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100158
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200159 /* TODO use uni limit for unidirectional streams */
160 qcs->rx.msd = quic_stream_is_local(qcc, id) ? qcc->lfctl.msd_bidi_l :
161 qcc->lfctl.msd_bidi_r;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200162 qcs->rx.msd_init = qcs->rx.msd;
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200163
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100164 qcs->tx.buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100165 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100166 qcs->tx.sent_offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100167
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100168 qcs->wait_event.tasklet = NULL;
169 qcs->wait_event.events = 0;
170 qcs->subs = NULL;
171
172 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100173 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100174 return qcs;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200175
176 err:
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200177 if (qcs->ctx && qcc->app_ops->detach)
178 qcc->app_ops->detach(qcs);
179
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200180 qc_stream_desc_release(qcs->stream);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200181 pool_free(pool_head_qcs, qcs);
182 return NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100183}
184
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200185static void qc_free_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
186{
187 struct buffer buf;
188
Amaury Denoyelle7313f5e2022-05-17 18:53:21 +0200189 if (ncb_is_null(ncbuf))
190 return;
191
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200192 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
193 b_free(&buf);
Amaury Denoyelle7313f5e2022-05-17 18:53:21 +0200194 offer_buffers(NULL, 1);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200195
196 *ncbuf = NCBUF_NULL;
197}
198
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200199/* Free a qcs. This function must only be done to remove a stream on allocation
200 * error or connection shutdown. Else use qcs_destroy which handle all the
201 * QUIC connection mechanism.
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100202 */
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200203void qcs_free(struct qcs *qcs)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100204{
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200205 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200206 b_free(&qcs->tx.buf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200207
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200208 BUG_ON(!qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams);
209 --qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams;
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200210
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200211 if (qcs->ctx && qcs->qcc->app_ops->detach)
212 qcs->qcc->app_ops->detach(qcs);
213
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200214 qc_stream_desc_release(qcs->stream);
215
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100216 BUG_ON(qcs->endp && !(qcs->endp->flags & CS_EP_ORPHAN));
217 cs_endpoint_free(qcs->endp);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200218
219 eb64_delete(&qcs->by_id);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100220 pool_free(pool_head_qcs, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100221}
222
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100223struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100224{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100225 struct buffer *buf = b_alloc(bptr);
226 BUG_ON(!buf);
227 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100228}
229
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200230struct ncbuf *qc_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
231{
232 struct buffer buf = BUF_NULL;
233
234 if (ncb_is_null(ncbuf)) {
235 b_alloc(&buf);
236 BUG_ON(b_is_null(&buf));
237
238 *ncbuf = ncb_make(buf.area, buf.size, 0);
239 ncb_init(ncbuf, 0);
240 }
241
242 return ncbuf;
243}
244
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100245int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
246{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100247 struct qcc *qcc = qcs->qcc;
248
249 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100250
251 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
252 BUG_ON(qcs->subs && qcs->subs != es);
253
254 es->events |= event_type;
255 qcs->subs = es;
256
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100257 if (event_type & SUB_RETRY_RECV)
258 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
259
260 if (event_type & SUB_RETRY_SEND)
261 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
262
263 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
264
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100265 return 0;
266}
267
268void qcs_notify_recv(struct qcs *qcs)
269{
270 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
271 tasklet_wakeup(qcs->subs->tasklet);
272 qcs->subs->events &= ~SUB_RETRY_RECV;
273 if (!qcs->subs->events)
274 qcs->subs = NULL;
275 }
276}
277
278void qcs_notify_send(struct qcs *qcs)
279{
280 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
281 tasklet_wakeup(qcs->subs->tasklet);
282 qcs->subs->events &= ~SUB_RETRY_SEND;
283 if (!qcs->subs->events)
284 qcs->subs = NULL;
285 }
286}
287
Amaury Denoyellea9773552022-05-16 14:38:25 +0200288/* Remove <bytes> from <qcs> Rx buffer. This must be called by transcoders
289 * after STREAM parsing. Flow-control for received offsets may be allocated for
290 * the peer if needed.
291 */
292void qcs_consume(struct qcs *qcs, uint64_t bytes)
293{
294 struct qcc *qcc = qcs->qcc;
295 struct quic_frame *frm;
Amaury Denoyelle8b87b152022-05-18 16:19:47 +0200296 struct ncbuf *buf = &qcs->rx.ncbuf;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200297 enum ncb_ret ret;
298
Amaury Denoyelle8b87b152022-05-18 16:19:47 +0200299 ret = ncb_advance(buf, bytes);
Amaury Denoyellea9773552022-05-16 14:38:25 +0200300 if (ret) {
301 ABORT_NOW(); /* should not happens because removal only in data */
302 }
303
Amaury Denoyelle8b87b152022-05-18 16:19:47 +0200304 if (ncb_is_empty(buf))
305 qc_free_ncbuf(qcs, buf);
306
Amaury Denoyellea9773552022-05-16 14:38:25 +0200307 qcs->rx.offset += bytes;
308 if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) {
309 frm = pool_zalloc(pool_head_quic_frame);
310 BUG_ON(!frm); /* TODO handle this properly */
311
312 qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init;
313
314 LIST_INIT(&frm->reflist);
315 frm->type = QUIC_FT_MAX_STREAM_DATA;
316 frm->max_stream_data.id = qcs->id;
317 frm->max_stream_data.max_stream_data = qcs->rx.msd;
318
319 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
320 tasklet_wakeup(qcc->wait_event.tasklet);
321 }
Amaury Denoyellec830e1e2022-05-16 16:19:59 +0200322
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200323 qcc->lfctl.offsets_consume += bytes;
324 if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) {
Amaury Denoyellec830e1e2022-05-16 16:19:59 +0200325 frm = pool_zalloc(pool_head_quic_frame);
326 BUG_ON(!frm); /* TODO handle this properly */
327
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200328 qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +0200329
330 LIST_INIT(&frm->reflist);
331 frm->type = QUIC_FT_MAX_DATA;
332 frm->max_data.max_data = qcc->lfctl.md;
333
334 LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list);
335 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
336 }
Amaury Denoyellea9773552022-05-16 14:38:25 +0200337}
338
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100339/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
340 * several streams, depending on the already open ones.
341 * Return this node if succeeded, NULL if not.
342 */
Amaury Denoyelle50742292022-03-29 14:57:19 +0200343struct qcs *qcc_get_qcs(struct qcc *qcc, uint64_t id)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100344{
345 unsigned int strm_type;
346 int64_t sub_id;
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200347 struct eb64_node *node;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200348 struct qcs *qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100349
350 strm_type = id & QCS_ID_TYPE_MASK;
351 sub_id = id >> QCS_ID_TYPE_SHIFT;
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200352 node = NULL;
Amaury Denoyelle0dc40f02022-02-07 11:44:17 +0100353 if (quic_stream_is_local(qcc, id)) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100354 /* Local streams: this stream must be already opened. */
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200355 node = eb64_lookup(&qcc->streams_by_id, id);
356 if (!node) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100357 /* unknown stream id */
358 goto out;
359 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200360 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100361 }
362 else {
363 /* Remote streams. */
364 struct eb_root *strms;
365 uint64_t largest_id;
366 enum qcs_type qcs_type;
367
368 strms = &qcc->streams_by_id;
369 qcs_type = qcs_id_type(id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100370
371 /* TODO also checks max-streams for uni streams */
372 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100373 if (sub_id + 1 > qcc->lfctl.ms_bidi) {
Amaury Denoyelle209404b2022-05-20 16:45:32 +0200374 /* RFC 9000 4.6. Controlling Concurrency
375 *
376 * An endpoint that receives a frame with a
377 * stream ID exceeding the limit it has sent
378 * MUST treat this as a connection error of
379 * type STREAM_LIMIT_ERROR
380 */
381 qcc_emit_cc(qcc, QC_ERR_STREAM_LIMIT_ERROR);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100382 goto out;
383 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100384 }
385
386 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
387 * correct value.
388 */
389 largest_id = qcc->strms[qcs_type].largest_id;
390 if (sub_id > (int64_t)largest_id) {
391 /* RFC: "A stream ID that is used out of order results in all streams
392 * of that type with lower-numbered stream IDs also being opened".
393 * So, let's "open" these streams.
394 */
395 int64_t i;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200396 struct qcs *tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100397
Amaury Denoyelle50742292022-03-29 14:57:19 +0200398 tmp_qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100399 for (i = largest_id + 1; i <= sub_id; i++) {
400 uint64_t id = (i << QCS_ID_TYPE_SHIFT) | strm_type;
401 enum qcs_type type = id & QCS_ID_DIR_BIT ? QCS_CLT_UNI : QCS_CLT_BIDI;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200402
Amaury Denoyelle50742292022-03-29 14:57:19 +0200403 tmp_qcs = qcs_new(qcc, id, type);
404 if (!tmp_qcs) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100405 /* allocation failure */
406 goto out;
407 }
408
409 qcc->strms[qcs_type].largest_id = i;
410 }
Amaury Denoyelle50742292022-03-29 14:57:19 +0200411 if (tmp_qcs)
412 qcs = tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100413 }
414 else {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200415 node = eb64_lookup(strms, id);
416 if (node)
417 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100418 }
419 }
420
Amaury Denoyelle50742292022-03-29 14:57:19 +0200421 return qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100422
423 out:
424 return NULL;
425}
426
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200427/* Decode the content of STREAM frames already received on the stream instance
428 * <qcs>.
429 *
430 * Returns 0 on success else non-zero.
431 */
432static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
433{
434 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
435
Amaury Denoyelle0ffd6e72022-05-24 11:07:28 +0200436 if (qcc->app_ops->decode_qcs(qcs, qcs->flags & QC_SF_FIN_RECV, qcc->ctx)) {
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200437 TRACE_DEVEL("leaving on decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
438 return 1;
439 }
440
441 qcs_notify_recv(qcs);
442
443 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
444
445 return 0;
446}
447
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200448/* Emit a CONNECTION_CLOSE_APP with error <err>. Reserved for application error
449 * code. This will interrupt all future send/receive operations.
450 */
451void qcc_emit_cc_app(struct qcc *qcc, int err)
452{
453 quic_set_connection_close(qcc->conn->handle.qc, err, 1);
454 qcc->flags |= QC_CF_CC_EMIT;
455 tasklet_wakeup(qcc->wait_event.tasklet);
456}
457
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200458/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
459 * <data> with length <len> and represents the offset <offset>. <fin> is set if
460 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100461 *
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200462 * Returns 0 on success else non-zero.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100463 */
464int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200465 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100466{
467 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200468 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100469
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100470 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
471
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200472 if (qcc->flags & QC_CF_CC_EMIT) {
473 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
474 return 0;
475 }
476
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200477 /* RFC 9000 19.8. STREAM Frames
478 *
479 * An endpoint MUST terminate the connection with error
480 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
481 * initiated stream that has not yet been created, or for a send-only
482 * stream.
483 */
484 if (quic_stream_is_local(qcc, id) && quic_stream_is_uni(id)) {
485 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
486 TRACE_DEVEL("leaving on invalid reception for a send-only stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
487 return 1;
488 }
489
Amaury Denoyelle50742292022-03-29 14:57:19 +0200490 qcs = qcc_get_qcs(qcc, id);
491 if (!qcs) {
Frédéric Lécailleb57de072022-05-02 18:58:27 +0200492 if ((id >> QCS_ID_TYPE_SHIFT) <= qcc->strms[qcs_id_type(id)].largest_id) {
493 TRACE_DEVEL("already released stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
494 return 0;
495 }
496 else {
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200497 /* RFC 9000 19.8. STREAM Frames
498 *
499 * An endpoint MUST terminate the connection with error
500 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
501 * initiated stream that has not yet been created, or for a send-only
502 * stream.
503 */
504 if (quic_stream_is_local(qcc, id)) {
505 TRACE_DEVEL("leaving on locally initiated stream not yet created", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
506 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
507 return 1;
508 }
509 else {
510 TRACE_DEVEL("leaving on stream not found", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
511 return 1;
512 }
Frédéric Lécailleb57de072022-05-02 18:58:27 +0200513 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100514 }
515
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100516 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100517 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 +0200518 return 0;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100519 }
520
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200521 /* TODO if last frame already received, stream size must not change.
522 * Else send FINAL_SIZE_ERROR.
523 */
524
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200525 if (offset + len > qcs->rx.offset_max) {
526 uint64_t diff = offset + len - qcs->rx.offset_max;
527 qcs->rx.offset_max = offset + len;
528 qcc->lfctl.offsets_recv += diff;
529
530 if (offset + len > qcs->rx.msd ||
531 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
532 /* RFC 9000 4.1. Data Flow Control
533 *
534 * A receiver MUST close the connection with an error
535 * of type FLOW_CONTROL_ERROR if the sender violates
536 * the advertised connection or stream data limits
537 */
538 TRACE_DEVEL("leaving on flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
539 qcc->conn, qcs);
540 qcc_emit_cc(qcc, QC_ERR_FLOW_CONTROL_ERROR);
541 return 1;
542 }
543 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100544
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200545 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100546 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200547 ABORT_NOW();
548 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100549 }
550
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100551 TRACE_DEVEL("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200552 if (offset < qcs->rx.offset) {
553 len -= qcs->rx.offset - offset;
554 offset = qcs->rx.offset;
555 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100556
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200557 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
558 if (ret != NCB_RET_OK) {
559 if (ret == NCB_RET_DATA_REJ) {
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200560 /* RFC 9000 2.2. Sending and Receiving Data
561 *
562 * An endpoint could receive data for a stream at the
563 * same stream offset multiple times. Data that has
564 * already been received can be discarded. The data at
565 * a given offset MUST NOT change if it is sent
566 * multiple times; an endpoint MAY treat receipt of
567 * different data at the same offset within a stream as
568 * a connection error of type PROTOCOL_VIOLATION.
569 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200570 TRACE_DEVEL("leaving on data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
571 qcc->conn, qcs);
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200572 qcc_emit_cc(qcc, QC_ERR_PROTOCOL_VIOLATION);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200573 }
574 else if (ret == NCB_RET_GAP_SIZE) {
575 TRACE_DEVEL("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
576 qcc->conn, qcs);
577 }
578 return 1;
579 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100580
581 if (fin)
582 qcs->flags |= QC_SF_FIN_RECV;
583
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200584 if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL))
585 qcc_decode_qcs(qcc, qcs);
586
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100587 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100588 return 0;
589}
590
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100591/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
592 * the frame.
593 *
594 * Returns 0 on success else non-zero.
595 */
596int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
597{
598 if (qcc->rfctl.md < max) {
599 qcc->rfctl.md = max;
600
601 if (qcc->flags & QC_CF_BLK_MFCTL) {
602 qcc->flags &= ~QC_CF_BLK_MFCTL;
603 tasklet_wakeup(qcc->wait_event.tasklet);
604 }
605 }
606 return 0;
607}
608
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100609/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
610 * field of the frame and <id> is the identifier of the QUIC stream.
611 *
612 * Returns 0 on success else non-zero.
613 */
614int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
615{
616 struct qcs *qcs;
617 struct eb64_node *node;
618
619 node = eb64_lookup(&qcc->streams_by_id, id);
620 if (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200621 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100622 if (max > qcs->tx.msd) {
623 qcs->tx.msd = max;
624
625 if (qcs->flags & QC_SF_BLK_SFCTL) {
626 qcs->flags &= ~QC_SF_BLK_SFCTL;
627 tasklet_wakeup(qcc->wait_event.tasklet);
628 }
629 }
630 }
631
632 return 0;
633}
634
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200635/* Signal the closing of remote stream with id <id>. Flow-control for new
636 * streams may be allocated for the peer if needed.
637 */
638static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +0100639{
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200640 struct quic_frame *frm;
641
642 if (quic_stream_is_bidi(id)) {
643 ++qcc->lfctl.cl_bidi_r;
644 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
645 frm = pool_zalloc(pool_head_quic_frame);
646 BUG_ON(!frm); /* TODO handle this properly */
647
648 LIST_INIT(&frm->reflist);
649 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
650 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
651 qcc->lfctl.cl_bidi_r;
652 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
653 tasklet_wakeup(qcc->wait_event.tasklet);
654
655 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
656 qcc->lfctl.cl_bidi_r = 0;
657 }
658 }
659 else {
660 /* TODO */
661 }
662
663 return 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100664}
665
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500666/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100667static void qcs_destroy(struct qcs *qcs)
668{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100669 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200670 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100671
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100672 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100673
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200674 if (quic_stream_is_remote(qcs->qcc, id))
675 qcc_release_remote_stream(qcs->qcc, id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100676
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200677 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100678
679 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100680}
681
682static inline int qcc_is_dead(const struct qcc *qcc)
683{
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200684 if (qcc->app_ops && qcc->app_ops->is_active &&
685 qcc->app_ops->is_active(qcc, qcc->ctx))
686 return 0;
687
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200688 if ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task)
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100689 return 1;
690
691 return 0;
692}
693
694/* Return true if the mux timeout should be armed. */
695static inline int qcc_may_expire(struct qcc *qcc)
696{
Amaury Denoyelle06890aa2022-04-04 16:15:06 +0200697 return !qcc->nb_cs;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100698}
699
700/* release function. This one should be called to free all resources allocated
701 * to the mux.
702 */
703static void qc_release(struct qcc *qcc)
704{
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200705 struct connection *conn = qcc->conn;
706 struct eb64_node *node;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100707
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100708 TRACE_ENTER(QMUX_EV_QCC_END);
709
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200710 if (qcc->app_ops && qcc->app_ops->release)
711 qcc->app_ops->release(qcc->ctx);
Amaury Denoyellef8909452022-03-30 11:51:56 +0200712
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200713 if (qcc->task) {
714 task_destroy(qcc->task);
715 qcc->task = NULL;
716 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100717
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200718 if (qcc->wait_event.tasklet)
719 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyellef3e03a42022-04-21 15:41:34 +0200720 if (conn && qcc->wait_event.events) {
721 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
722 qcc->wait_event.events,
723 &qcc->wait_event);
724 }
Amaury Denoyellef8909452022-03-30 11:51:56 +0200725
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200726 /* liberate remaining qcs instances */
727 node = eb64_first(&qcc->streams_by_id);
728 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200729 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200730 node = eb64_next(node);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200731 qcs_free(qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100732 }
733
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200734 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
735 struct quic_frame *frm = LIST_ELEM(&qcc->lfctl.frms, struct quic_frame *, list);
736 LIST_DELETE(&frm->list);
737 pool_free(pool_head_quic_frame, frm);
738 }
739
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200740 pool_free(pool_head_qcc, qcc);
741
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100742 if (conn) {
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100743 LIST_DEL_INIT(&conn->stopping_list);
744
Willy Tarreau784b8682022-04-11 14:18:10 +0200745 conn->handle.qc->conn = NULL;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100746 conn->mux = NULL;
747 conn->ctx = NULL;
748
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100749 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
750
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100751 conn_stop_tracking(conn);
752 conn_full_close(conn);
753 if (conn->destroy_cb)
754 conn->destroy_cb(conn);
755 conn_free(conn);
756 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100757
758 TRACE_LEAVE(QMUX_EV_QCC_END);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100759}
760
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200761/* Transfer as much as possible data on <qcs> from <in> to <out>. <max_data> is
762 * the current flow-control limit on the connection which must not be exceeded.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100763 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200764 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100765 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200766static int qcs_xfer_data(struct qcs *qcs, struct buffer *out,
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200767 struct buffer *in, uint64_t max_data)
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200768{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100769 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200770 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100771 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200772
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100773 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100774
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100775 qc_get_buf(qcs, out);
776
777 /*
778 * QCS out buffer diagram
779 * head left to_xfer
780 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100781 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100782 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100783 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100784 * ^ ack-off ^ sent-off ^ off
785 *
786 * STREAM frame
787 * ^ ^
788 * |xxxxxxxxxxxxxxxxx|
789 */
790
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200791 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100792 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
793
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100794 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200795 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100796
797 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
798 /* do not exceed flow control limit */
799 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
800 to_xfer = qcs->tx.msd - qcs->tx.offset;
801
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100802 BUG_ON_HOT(max_data > qcc->rfctl.md);
803 /* do not overcome flow control limit on connection */
804 if (max_data + to_xfer > qcc->rfctl.md)
805 to_xfer = qcc->rfctl.md - max_data;
806
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100807 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200808 goto out;
809
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200810 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200811
812 out:
813 {
814 struct qcs_xfer_data_trace_arg arg = {
815 .prep = b_data(out), .xfer = total,
816 };
817 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
818 qcc->conn, qcs, &arg);
819 }
820
821 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200822}
823
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200824/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
825 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
826 * stream frame.
827 *
828 * Returns the length of the STREAM frame or a negative error code.
829 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200830static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
831 struct list *frm_list)
832{
833 struct qcc *qcc = qcs->qcc;
834 struct quic_frame *frm;
835 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200836 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200837
838 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
839
Amaury Denoyellea4569202022-04-15 17:29:25 +0200840 /* if ack_offset < buf_offset, it points to an older buffer. */
841 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
842 BUG_ON(qcs->tx.sent_offset < base_off);
843
844 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200845 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200846 BUG_ON(total < 0);
847
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200848 if (!total) {
849 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
850 return 0;
851 }
Amaury Denoyellea4569202022-04-15 17:29:25 +0200852 BUG_ON(qcs->tx.sent_offset >= qcs->tx.offset);
853 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200854
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200855 frm = pool_zalloc(pool_head_quic_frame);
856 if (!frm)
857 goto err;
858
Frédéric Lécailleb9171912022-04-21 17:32:10 +0200859 LIST_INIT(&frm->reflist);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100860 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200861 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200862 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100863 frm->stream.buf = out;
864 frm->stream.data = (unsigned char *)b_peek(out, head);
865
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100866 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200867 if (fin)
868 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100869
870 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200871 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100872 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200873 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100874
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200875 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
876 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200877
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100878 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100879
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200880 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100881 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200882 struct qcs_build_stream_trace_arg arg = {
883 .len = frm->stream.len, .fin = fin,
884 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100885 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200886 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100887 qcc->conn, qcs, &arg);
888 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100889
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200890 return total;
891
892 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100893 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200894 return -1;
895}
896
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100897/* This function must be called by the upper layer to inform about the sending
898 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
899 * <offset>.
900 */
901void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
902{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100903 struct qcc *qcc = qcs->qcc;
904 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100905
906 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200907 BUG_ON(offset >= qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100908
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100909 /* check if the STREAM frame has already been notified. It can happen
910 * for retransmission.
911 */
912 if (offset + data <= qcs->tx.sent_offset)
913 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100914
915 diff = offset + data - qcs->tx.sent_offset;
916
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100917 /* increase offset sum on connection */
918 qcc->tx.sent_offsets += diff;
919 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
920 if (qcc->tx.sent_offsets == qcc->rfctl.md)
921 qcc->flags |= QC_CF_BLK_MFCTL;
922
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100923 /* increase offset on stream */
924 qcs->tx.sent_offset += diff;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100925 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200926 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100927 if (qcs->tx.sent_offset == qcs->tx.msd)
928 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200929
930 if (qcs->tx.offset == qcs->tx.sent_offset && b_full(&qcs->stream->buf->buf)) {
931 qc_stream_buf_release(qcs->stream);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200932 /* prepare qcs for immediate send retry if data to send */
933 if (b_data(&qcs->tx.buf))
934 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200935 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100936}
937
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100938/* Wrapper for send on transport layer. Send a list of frames <frms> for the
939 * connection <qcc>.
940 *
941 * Returns 0 if all data sent with success else non-zero.
942 */
943static int qc_send_frames(struct qcc *qcc, struct list *frms)
944{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100945 /* TODO implement an opportunistic retry mechanism. This is needed
946 * because qc_send_app_pkts is not completed. It will only prepare data
947 * up to its Tx buffer. The frames left are not send even if the Tx
948 * buffer is emptied by the sendto call.
949 *
950 * To overcome this, we call repeatedly qc_send_app_pkts until we
951 * detect that the transport layer has send nothing. This could happen
952 * on congestion or sendto syscall error.
953 *
954 * When qc_send_app_pkts is improved to handle retry by itself, we can
955 * remove the looping from the MUX.
956 */
957 struct quic_frame *first_frm;
958 uint64_t first_offset = 0;
959 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100960
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100961 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
962
963 if (LIST_ISEMPTY(frms)) {
964 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200965 return 1;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100966 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100967
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200968 LIST_INIT(&qcc->send_retry_list);
969
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100970 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100971 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
972 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
973 first_offset = first_frm->stream.offset.key;
974 first_stream_frame_type = 1;
975 }
976 else {
977 first_stream_frame_type = 0;
978 }
979
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100980 if (!LIST_ISEMPTY(frms))
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +0200981 qc_send_app_pkts(qcc->conn->handle.qc, 0, frms);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100982
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100983 /* If there is frames left, check if the transport layer has send some
984 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100985 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100986 if (!LIST_ISEMPTY(frms)) {
987 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
988 goto retry_send;
989
990 /* If the first frame is STREAM, check if its offset has
991 * changed.
992 */
993 if (first_stream_frame_type &&
994 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
995 goto retry_send;
996 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100997 }
998
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100999 /* If there is frames left at this stage, transport layer is blocked.
1000 * Subscribe on it to retry later.
1001 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001002 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001003 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001004 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1005 SUB_RETRY_SEND, &qcc->wait_event);
1006 return 1;
1007 }
1008
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001009 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1010
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001011 return 0;
1012}
1013
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001014/* Used internally by qc_send function. Proceed to send for <qcs>. This will
1015 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
1016 * is then generated and inserted in <frms> list. <qcc_max_data> is the current
1017 * flow-control max-data at the connection level which must not be surpassed.
1018 *
1019 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
1020 * be null if out buffer cannot be allocated.
1021 */
1022static int _qc_send_qcs(struct qcs *qcs, struct list *frms,
1023 uint64_t qcc_max_data)
1024{
1025 struct qcc *qcc = qcs->qcc;
1026 struct buffer *buf = &qcs->tx.buf;
1027 struct buffer *out = qc_stream_buf_get(qcs->stream);
1028 int xfer = 0;
1029
1030 /* Allocate <out> buffer if necessary. */
1031 if (!out) {
1032 if (qcc->flags & QC_CF_CONN_FULL)
1033 return 0;
1034
1035 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
1036 if (!out) {
1037 qcc->flags |= QC_CF_CONN_FULL;
1038 return 0;
1039 }
1040 }
1041
1042 /* Transfer data from <buf> to <out>. */
1043 if (b_data(buf)) {
1044 xfer = qcs_xfer_data(qcs, out, buf, qcc_max_data);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001045 if (xfer > 0) {
1046 qcs_notify_send(qcs);
1047 qcs->flags &= ~QC_SF_BLK_MROOM;
1048 }
1049
1050 qcs->tx.offset += xfer;
1051 }
1052
1053 /* out buffer cannot be emptied if qcs offsets differ. */
1054 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1055
1056 /* Build a new STREAM frame with <out> buffer. */
1057 if (qcs->tx.sent_offset != qcs->tx.offset) {
1058 int ret;
1059 char fin = !!(qcs->flags & QC_SF_FIN_STREAM);
1060
1061 /* FIN is set if all incoming data were transfered. */
1062 fin = !!(fin && !b_data(buf));
1063
1064 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +02001065 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001066 }
1067
1068 return xfer;
1069}
1070
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001071/* Proceed to sending. Loop through all available streams for the <qcc>
1072 * instance and try to send as much as possible.
1073 *
1074 * Returns the total of bytes sent to the transport layer.
1075 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001076static int qc_send(struct qcc *qcc)
1077{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001078 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001079 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001080 struct qcs *qcs, *qcs_tmp;
1081 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001082
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001083 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001084
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001085 if (qcc->conn->flags & CO_FL_SOCK_WR_SH || qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001086 qcc->conn->flags |= CO_FL_ERROR;
1087 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
1088 return 0;
1089 }
1090
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001091 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1092 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
1093 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
1094 goto out;
1095 }
1096 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02001097
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001098 if (qcc->flags & QC_CF_BLK_MFCTL)
1099 return 0;
1100
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001101 /* loop through all streams, construct STREAM frames if data available.
1102 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001103 */
1104 node = eb64_first(&qcc->streams_by_id);
1105 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001106 int ret;
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001107 uint64_t id;
1108
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001109 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001110 id = qcs->id;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001111
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001112 if (quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001113 node = eb64_next(node);
1114 continue;
1115 }
1116
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001117 if (qcs->flags & QC_SF_BLK_SFCTL) {
1118 node = eb64_next(node);
1119 continue;
1120 }
1121
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001122 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001123 node = eb64_next(node);
1124 continue;
1125 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001126
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001127 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + total);
1128 total += ret;
1129 node = eb64_next(node);
1130 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001131
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001132 if (qc_send_frames(qcc, &frms)) {
1133 /* data rejected by transport layer, do not retry. */
1134 goto out;
1135 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001136
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001137 retry:
1138 tmp_total = 0;
1139 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
1140 int ret;
1141 BUG_ON(!b_data(&qcs->tx.buf));
1142 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001143
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001144 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + tmp_total);
1145 tmp_total += ret;
1146 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001147 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001148
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001149 total += tmp_total;
1150 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
1151 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +01001152
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001153 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001154 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1155
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001156 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001157}
1158
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001159/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1160 * operation.
1161 *
1162 * Returns 0 on success else non-zero.
1163 */
1164static int qc_recv(struct qcc *qcc)
1165{
1166 struct eb64_node *node;
1167 struct qcs *qcs;
1168
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001169 TRACE_ENTER(QMUX_EV_QCC_RECV);
1170
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001171 if (qcc->flags & QC_CF_CC_EMIT) {
1172 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
1173 return 0;
1174 }
1175
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001176 node = eb64_first(&qcc->streams_by_id);
1177 while (node) {
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001178 uint64_t id;
1179
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001180 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001181 id = qcs->id;
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001182
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001183 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001184 node = eb64_next(node);
1185 continue;
1186 }
1187
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001188 if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001189 node = eb64_next(node);
1190 continue;
1191 }
1192
1193 qcc_decode_qcs(qcc, qcs);
1194 node = eb64_next(node);
1195 }
1196
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001197 TRACE_LEAVE(QMUX_EV_QCC_RECV);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001198 return 0;
1199}
1200
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001201/* Release all streams that are already marked as detached. This is only done
1202 * if their TX buffers are empty or if a CONNECTION_CLOSE has been received.
1203 *
1204 * Return the number of released stream.
1205 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001206static int qc_release_detached_streams(struct qcc *qcc)
1207{
1208 struct eb64_node *node;
1209 int release = 0;
1210
1211 node = eb64_first(&qcc->streams_by_id);
1212 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001213 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001214 node = eb64_next(node);
1215
1216 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001217 if (!b_data(&qcs->tx.buf) &&
1218 qcs->tx.offset == qcs->tx.sent_offset) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001219 qcs_destroy(qcs);
1220 release = 1;
1221 }
1222 else {
1223 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1224 SUB_RETRY_SEND, &qcc->wait_event);
1225 }
1226 }
1227 }
1228
1229 return release;
1230}
1231
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001232static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1233{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001234 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001235
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001236 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001237
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001238 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001239
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001240 if (qc_release_detached_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001241 if (qcc_is_dead(qcc)) {
1242 qc_release(qcc);
1243 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001244 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001245 if (qcc_may_expire(qcc))
1246 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1247 else
1248 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001249 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001250 }
1251 }
1252
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001253 qc_recv(qcc);
1254
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001255 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1256
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001257 return NULL;
1258}
1259
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001260static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1261{
1262 struct qcc *qcc = ctx;
1263 int expired = tick_is_expired(t->expire, now_ms);
1264
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001265 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001266
1267 if (qcc) {
1268 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001269 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001270 return t;
1271 }
1272
1273 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001274 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001275 t->expire = TICK_ETERNITY;
1276 return t;
1277 }
1278 }
1279
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001280 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001281
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001282 if (!qcc) {
1283 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001284 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001285 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001286
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001287 qcc->task = NULL;
1288
1289 if (qcc_is_dead(qcc))
1290 qc_release(qcc);
1291
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001292 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1293
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001294 return NULL;
1295}
1296
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001297static int qc_init(struct connection *conn, struct proxy *prx,
1298 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001299{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001300 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001301 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001302
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001303 qcc = pool_alloc(pool_head_qcc);
1304 if (!qcc)
1305 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001306
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001307 qcc->conn = conn;
1308 conn->ctx = qcc;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001309 qcc->nb_cs = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001310 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001311
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001312 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001313
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001314 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001315
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001316 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001317 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001318
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001319 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001320 qcc->tx.sent_offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001321
1322 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001323 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001324 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
1325 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001326 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001327 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001328
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001329 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001330 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
1331 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001332 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001333 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001334
1335 /* Server initiated streams must respect the server flow control. */
1336 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001337 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
1338 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001339 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001340 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1341
1342 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001343 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
1344 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001345 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001346 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001347
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001348 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001349 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02001350 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
1351 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001352 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001353
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001354 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001355 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001356
Willy Tarreau784b8682022-04-11 14:18:10 +02001357 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001358 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001359 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1360 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1361
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001362 qcc->wait_event.tasklet = tasklet_new();
1363 if (!qcc->wait_event.tasklet)
1364 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001365
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001366 LIST_INIT(&qcc->send_retry_list);
1367
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001368 qcc->subs = NULL;
1369 qcc->wait_event.tasklet->process = qc_io_cb;
1370 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001371 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001372
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001373 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001374 qcc->task = NULL;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001375 qcc->timeout = prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001376 if (tick_isset(qcc->timeout)) {
1377 qcc->task = task_new_here();
1378 if (!qcc->task)
1379 goto fail_no_timeout_task;
1380 qcc->task->process = qc_timeout_task;
1381 qcc->task->context = qcc;
1382 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1383 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001384
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001385 if (!conn_is_back(conn)) {
1386 if (!LIST_INLIST(&conn->stopping_list)) {
1387 LIST_APPEND(&mux_stopping_data[tid].list,
1388 &conn->stopping_list);
1389 }
1390 }
1391
Willy Tarreau784b8682022-04-11 14:18:10 +02001392 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001393 /* init read cycle */
1394 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001395
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001396 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001397
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001398 fail_no_timeout_task:
1399 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001400 fail_no_tasklet:
1401 pool_free(pool_head_qcc, qcc);
1402 fail_no_qcc:
1403 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001404}
1405
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001406static void qc_destroy(void *ctx)
1407{
1408 struct qcc *qcc = ctx;
1409
1410 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
1411 qc_release(qcc);
1412 TRACE_LEAVE(QMUX_EV_QCC_END);
1413}
1414
Willy Tarreau4201ab72022-05-10 19:18:52 +02001415static void qc_detach(struct cs_endpoint *endp)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001416{
Willy Tarreaudf943132022-05-13 16:31:23 +02001417 struct qcs *qcs = endp->target;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001418 struct qcc *qcc = qcs->qcc;
1419
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001420 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001421
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001422 --qcc->nb_cs;
1423
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001424 if ((b_data(&qcs->tx.buf) || qcs->tx.offset > qcs->tx.sent_offset) &&
1425 !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001426 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001427 qcs->flags |= QC_SF_DETACH;
1428 return;
1429 }
1430
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001431 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001432
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001433 if (qcc_is_dead(qcc)) {
1434 qc_release(qcc);
1435 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001436 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001437 if (qcc_may_expire(qcc))
1438 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1439 else
1440 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001441 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001442 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001443
1444 TRACE_LEAVE(QMUX_EV_STRM_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001445}
1446
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001447/* Called from the upper layer, to receive data */
1448static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf,
1449 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001450{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001451 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001452 struct htx *qcs_htx = NULL;
1453 struct htx *cs_htx = NULL;
1454 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001455 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001456
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001457 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001458
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001459 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
1460 if (htx_is_empty(qcs_htx)) {
1461 /* Set buffer data to 0 as HTX is empty. */
1462 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1463 goto end;
1464 }
1465
1466 ret = qcs_htx->data;
1467
1468 cs_htx = htx_from_buf(buf);
1469 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
1470 htx_to_buf(cs_htx, buf);
1471 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1472 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
1473 goto end;
1474 }
1475
1476 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
1477 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
1478
1479 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001480 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
1481 cs_htx->flags |= HTX_FL_EOM;
1482 fin = 1;
1483 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001484
1485 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
1486 htx_to_buf(cs_htx, buf);
1487 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1488 ret -= qcs_htx->data;
1489
1490 end:
1491 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreau15b07212022-05-10 11:24:26 +02001492 qcs->endp->flags |= (CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001493 }
1494 else {
Willy Tarreau15b07212022-05-10 11:24:26 +02001495 qcs->endp->flags &= ~(CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
1496 if (qcs->endp->flags & CS_EP_ERR_PENDING)
1497 qcs->endp->flags |= CS_EP_ERROR;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001498
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001499 if (fin)
Willy Tarreau15b07212022-05-10 11:24:26 +02001500 qcs->endp->flags |= CS_EP_EOI;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001501
1502 if (b_size(&qcs->rx.app_buf)) {
1503 b_free(&qcs->rx.app_buf);
1504 offer_buffers(NULL, 1);
1505 }
1506 }
1507
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001508 if (ret) {
1509 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001510 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001511 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001512
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001513 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
1514
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001515 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001516}
1517
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001518static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf,
1519 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001520{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001521 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001522 size_t ret;
1523
1524 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001525
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001526 ret = qcs->qcc->app_ops->snd_buf(cs, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001527
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001528 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
1529
1530 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001531}
1532
1533/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1534 * event subscriber <es> is not allowed to change from a previous call as long
1535 * as at least one event is still subscribed. The <event_type> must only be a
1536 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1537 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001538static int qc_subscribe(struct conn_stream *cs, int event_type,
1539 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001540{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001541 return qcs_subscribe(__cs_mux(cs), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001542}
1543
1544/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1545 * The <es> pointer is not allowed to differ from the one passed to the
1546 * subscribe() call. It always returns zero.
1547 */
1548static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
1549{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001550 struct qcs *qcs = __cs_mux(cs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001551
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001552 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1553 BUG_ON(qcs->subs && qcs->subs != es);
1554
1555 es->events &= ~event_type;
1556 if (!es->events)
1557 qcs->subs = NULL;
1558
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001559 return 0;
1560}
1561
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001562/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Christopher Fauletb041b232022-03-24 10:27:02 +01001563 * report CS_EP_ERR_PENDING|CS_EP_ERROR on the attached conn-streams and wake
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001564 * them.
1565 */
1566static int qc_wake_some_streams(struct qcc *qcc)
1567{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001568 struct qcs *qcs;
1569 struct eb64_node *node;
1570
1571 for (node = eb64_first(&qcc->streams_by_id); node;
1572 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001573 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001574
Amaury Denoyelle1c25b182022-05-24 16:53:56 +02001575 if (!qcs->endp || !qcs->endp->cs)
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001576 continue;
1577
1578 if (qcc->conn->flags & CO_FL_ERROR) {
Christopher Fauletb041b232022-03-24 10:27:02 +01001579 qcs->endp->flags |= CS_EP_ERR_PENDING;
1580 if (qcs->endp->flags & CS_EP_EOS)
1581 qcs->endp->flags |= CS_EP_ERROR;
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001582
1583 if (qcs->subs) {
1584 qcs_notify_recv(qcs);
1585 qcs_notify_send(qcs);
1586 }
Willy Tarreau01c2a4a2022-05-10 15:46:10 +02001587 else if (qcs->endp->cs->data_cb->wake) {
1588 qcs->endp->cs->data_cb->wake(qcs->endp->cs);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001589 }
1590 }
1591 }
1592
1593 return 0;
1594}
1595
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001596static int qc_wake(struct connection *conn)
1597{
1598 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02001599 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001600
1601 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001602
1603 /* Check if a soft-stop is in progress.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001604 *
1605 * TODO this is revelant for frontend connections only.
Amaury Denoyelled0c62322022-05-23 08:52:58 +02001606 *
1607 * TODO Client should be notified with a H3 GOAWAY and then a
1608 * CONNECTION_CLOSE. However, quic-conn uses the listener socket for
1609 * sending which at this stage is already closed.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001610 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001611 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Amaury Denoyelled0c62322022-05-23 08:52:58 +02001612 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001613
Willy Tarreau784b8682022-04-11 14:18:10 +02001614 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02001615 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
1616
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001617 qc_send(qcc);
1618
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001619 qc_wake_some_streams(qcc);
1620
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001621 if (qcc_is_dead(qcc))
1622 goto release;
1623
1624 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
1625
1626 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001627
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001628 release:
1629 qc_release(qcc);
1630 TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001631 return 1;
1632}
1633
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001634
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001635static void qmux_trace_frm(const struct quic_frame *frm)
1636{
1637 switch (frm->type) {
1638 case QUIC_FT_MAX_STREAMS_BIDI:
1639 chunk_appendf(&trace_buf, " max_streams=%lu",
1640 frm->max_streams_bidi.max_streams);
1641 break;
1642
1643 case QUIC_FT_MAX_STREAMS_UNI:
1644 chunk_appendf(&trace_buf, " max_streams=%lu",
1645 frm->max_streams_uni.max_streams);
1646 break;
1647
1648 default:
1649 break;
1650 }
1651}
1652
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001653/* quic-mux trace handler */
1654static void qmux_trace(enum trace_level level, uint64_t mask,
1655 const struct trace_source *src,
1656 const struct ist where, const struct ist func,
1657 const void *a1, const void *a2, const void *a3, const void *a4)
1658{
1659 const struct connection *conn = a1;
1660 const struct qcc *qcc = conn ? conn->ctx : NULL;
1661 const struct qcs *qcs = a2;
1662
1663 if (!qcc)
1664 return;
1665
1666 if (src->verbosity > QMUX_VERB_CLEAN) {
1667 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
1668
1669 if (qcs)
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001670 chunk_appendf(&trace_buf, " qcs=%p(%lu)", qcs, qcs->id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001671
1672 if (mask & QMUX_EV_QCC_NQCS) {
1673 const uint64_t *id = a3;
1674 chunk_appendf(&trace_buf, " id=%lu", *id);
1675 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001676
1677 if (mask & QMUX_EV_SEND_FRM)
1678 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001679
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001680 if (mask & QMUX_EV_QCS_XFER_DATA) {
1681 const struct qcs_xfer_data_trace_arg *arg = a3;
1682 chunk_appendf(&trace_buf, " prep=%lu xfer=%d",
1683 arg->prep, arg->xfer);
1684 }
1685
1686 if (mask & QMUX_EV_QCS_BUILD_STRM) {
1687 const struct qcs_build_stream_trace_arg *arg = a3;
1688 chunk_appendf(&trace_buf, " len=%lu fin=%d offset=%lu",
1689 arg->len, arg->fin, arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001690 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001691 }
1692}
1693
1694
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001695static const struct mux_ops qc_ops = {
1696 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001697 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001698 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001699 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001700 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001701 .subscribe = qc_subscribe,
1702 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001703 .wake = qc_wake,
Willy Tarreaub5821e12022-04-26 11:54:08 +02001704 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02001705 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001706};
1707
1708static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001709 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001710
1711INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);