blob: d744f91b361c1279a25d75b2cd935d204254aa23 [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
105 * send operations.
106 */
107static void qcc_emit_cc(struct qcc *qcc, int err)
108{
109 quic_set_connection_close(qcc->conn->handle.qc, err);
110 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 Denoyelle47447af2022-04-27 15:17:11 +0200139 if (qcc->app_ops->attach) {
140 if (qcc->app_ops->attach(qcs))
141 goto err;
142 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100143
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200144 qcs->id = qcs->by_id.key = id;
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
448/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
449 * <data> with length <len> and represents the offset <offset>. <fin> is set if
450 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100451 *
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200452 * Returns 0 on success else non-zero.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100453 */
454int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200455 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100456{
457 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200458 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100459
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100460 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
461
Amaury Denoyelle50742292022-03-29 14:57:19 +0200462 qcs = qcc_get_qcs(qcc, id);
463 if (!qcs) {
Frédéric Lécailleb57de072022-05-02 18:58:27 +0200464 if ((id >> QCS_ID_TYPE_SHIFT) <= qcc->strms[qcs_id_type(id)].largest_id) {
465 TRACE_DEVEL("already released stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
466 return 0;
467 }
468 else {
469 TRACE_DEVEL("leaving on stream not found", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
470 return 1;
471 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100472 }
473
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100474 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100475 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 +0200476 return 0;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100477 }
478
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200479 /* TODO if last frame already received, stream size must not change.
480 * Else send FINAL_SIZE_ERROR.
481 */
482
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200483 if (offset + len > qcs->rx.offset_max) {
484 uint64_t diff = offset + len - qcs->rx.offset_max;
485 qcs->rx.offset_max = offset + len;
486 qcc->lfctl.offsets_recv += diff;
487
488 if (offset + len > qcs->rx.msd ||
489 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
490 /* RFC 9000 4.1. Data Flow Control
491 *
492 * A receiver MUST close the connection with an error
493 * of type FLOW_CONTROL_ERROR if the sender violates
494 * the advertised connection or stream data limits
495 */
496 TRACE_DEVEL("leaving on flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
497 qcc->conn, qcs);
498 qcc_emit_cc(qcc, QC_ERR_FLOW_CONTROL_ERROR);
499 return 1;
500 }
501 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100502
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200503 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100504 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200505 ABORT_NOW();
506 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100507 }
508
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100509 TRACE_DEVEL("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200510 if (offset < qcs->rx.offset) {
511 len -= qcs->rx.offset - offset;
512 offset = qcs->rx.offset;
513 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100514
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200515 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
516 if (ret != NCB_RET_OK) {
517 if (ret == NCB_RET_DATA_REJ) {
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200518 /* RFC 9000 2.2. Sending and Receiving Data
519 *
520 * An endpoint could receive data for a stream at the
521 * same stream offset multiple times. Data that has
522 * already been received can be discarded. The data at
523 * a given offset MUST NOT change if it is sent
524 * multiple times; an endpoint MAY treat receipt of
525 * different data at the same offset within a stream as
526 * a connection error of type PROTOCOL_VIOLATION.
527 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200528 TRACE_DEVEL("leaving on data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
529 qcc->conn, qcs);
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200530 qcc_emit_cc(qcc, QC_ERR_PROTOCOL_VIOLATION);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200531 }
532 else if (ret == NCB_RET_GAP_SIZE) {
533 TRACE_DEVEL("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
534 qcc->conn, qcs);
535 }
536 return 1;
537 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100538
539 if (fin)
540 qcs->flags |= QC_SF_FIN_RECV;
541
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200542 if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL))
543 qcc_decode_qcs(qcc, qcs);
544
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100545 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100546 return 0;
547}
548
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100549/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
550 * the frame.
551 *
552 * Returns 0 on success else non-zero.
553 */
554int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
555{
556 if (qcc->rfctl.md < max) {
557 qcc->rfctl.md = max;
558
559 if (qcc->flags & QC_CF_BLK_MFCTL) {
560 qcc->flags &= ~QC_CF_BLK_MFCTL;
561 tasklet_wakeup(qcc->wait_event.tasklet);
562 }
563 }
564 return 0;
565}
566
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100567/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
568 * field of the frame and <id> is the identifier of the QUIC stream.
569 *
570 * Returns 0 on success else non-zero.
571 */
572int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
573{
574 struct qcs *qcs;
575 struct eb64_node *node;
576
577 node = eb64_lookup(&qcc->streams_by_id, id);
578 if (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200579 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100580 if (max > qcs->tx.msd) {
581 qcs->tx.msd = max;
582
583 if (qcs->flags & QC_SF_BLK_SFCTL) {
584 qcs->flags &= ~QC_SF_BLK_SFCTL;
585 tasklet_wakeup(qcc->wait_event.tasklet);
586 }
587 }
588 }
589
590 return 0;
591}
592
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200593/* Signal the closing of remote stream with id <id>. Flow-control for new
594 * streams may be allocated for the peer if needed.
595 */
596static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +0100597{
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200598 struct quic_frame *frm;
599
600 if (quic_stream_is_bidi(id)) {
601 ++qcc->lfctl.cl_bidi_r;
602 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
603 frm = pool_zalloc(pool_head_quic_frame);
604 BUG_ON(!frm); /* TODO handle this properly */
605
606 LIST_INIT(&frm->reflist);
607 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
608 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
609 qcc->lfctl.cl_bidi_r;
610 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
611 tasklet_wakeup(qcc->wait_event.tasklet);
612
613 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
614 qcc->lfctl.cl_bidi_r = 0;
615 }
616 }
617 else {
618 /* TODO */
619 }
620
621 return 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100622}
623
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500624/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100625static void qcs_destroy(struct qcs *qcs)
626{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100627 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200628 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100629
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100630 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100631
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200632 if (quic_stream_is_remote(qcs->qcc, id))
633 qcc_release_remote_stream(qcs->qcc, id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100634
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200635 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100636
637 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100638}
639
640static inline int qcc_is_dead(const struct qcc *qcc)
641{
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200642 if (qcc->app_ops && qcc->app_ops->is_active &&
643 qcc->app_ops->is_active(qcc, qcc->ctx))
644 return 0;
645
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200646 if ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task)
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100647 return 1;
648
649 return 0;
650}
651
652/* Return true if the mux timeout should be armed. */
653static inline int qcc_may_expire(struct qcc *qcc)
654{
Amaury Denoyelle06890aa2022-04-04 16:15:06 +0200655 return !qcc->nb_cs;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100656}
657
658/* release function. This one should be called to free all resources allocated
659 * to the mux.
660 */
661static void qc_release(struct qcc *qcc)
662{
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200663 struct connection *conn = qcc->conn;
664 struct eb64_node *node;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100665
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100666 TRACE_ENTER(QMUX_EV_QCC_END);
667
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200668 if (qcc->app_ops && qcc->app_ops->release)
669 qcc->app_ops->release(qcc->ctx);
Amaury Denoyellef8909452022-03-30 11:51:56 +0200670
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200671 if (qcc->task) {
672 task_destroy(qcc->task);
673 qcc->task = NULL;
674 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100675
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200676 if (qcc->wait_event.tasklet)
677 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyellef3e03a42022-04-21 15:41:34 +0200678 if (conn && qcc->wait_event.events) {
679 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
680 qcc->wait_event.events,
681 &qcc->wait_event);
682 }
Amaury Denoyellef8909452022-03-30 11:51:56 +0200683
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200684 /* liberate remaining qcs instances */
685 node = eb64_first(&qcc->streams_by_id);
686 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200687 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200688 node = eb64_next(node);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200689 qcs_free(qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100690 }
691
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200692 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
693 struct quic_frame *frm = LIST_ELEM(&qcc->lfctl.frms, struct quic_frame *, list);
694 LIST_DELETE(&frm->list);
695 pool_free(pool_head_quic_frame, frm);
696 }
697
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200698 pool_free(pool_head_qcc, qcc);
699
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100700 if (conn) {
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100701 LIST_DEL_INIT(&conn->stopping_list);
702
Willy Tarreau784b8682022-04-11 14:18:10 +0200703 conn->handle.qc->conn = NULL;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100704 conn->mux = NULL;
705 conn->ctx = NULL;
706
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100707 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
708
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100709 conn_stop_tracking(conn);
710 conn_full_close(conn);
711 if (conn->destroy_cb)
712 conn->destroy_cb(conn);
713 conn_free(conn);
714 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100715
716 TRACE_LEAVE(QMUX_EV_QCC_END);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100717}
718
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200719/* Transfer as much as possible data on <qcs> from <in> to <out>. <max_data> is
720 * the current flow-control limit on the connection which must not be exceeded.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100721 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200722 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100723 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200724static int qcs_xfer_data(struct qcs *qcs, struct buffer *out,
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200725 struct buffer *in, uint64_t max_data)
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200726{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100727 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200728 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100729 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200730
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100731 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100732
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100733 qc_get_buf(qcs, out);
734
735 /*
736 * QCS out buffer diagram
737 * head left to_xfer
738 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100739 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100740 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100741 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100742 * ^ ack-off ^ sent-off ^ off
743 *
744 * STREAM frame
745 * ^ ^
746 * |xxxxxxxxxxxxxxxxx|
747 */
748
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200749 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100750 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
751
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100752 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200753 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100754
755 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
756 /* do not exceed flow control limit */
757 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
758 to_xfer = qcs->tx.msd - qcs->tx.offset;
759
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100760 BUG_ON_HOT(max_data > qcc->rfctl.md);
761 /* do not overcome flow control limit on connection */
762 if (max_data + to_xfer > qcc->rfctl.md)
763 to_xfer = qcc->rfctl.md - max_data;
764
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100765 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200766 goto out;
767
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200768 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200769
770 out:
771 {
772 struct qcs_xfer_data_trace_arg arg = {
773 .prep = b_data(out), .xfer = total,
774 };
775 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
776 qcc->conn, qcs, &arg);
777 }
778
779 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200780}
781
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200782/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
783 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
784 * stream frame.
785 *
786 * Returns the length of the STREAM frame or a negative error code.
787 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200788static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
789 struct list *frm_list)
790{
791 struct qcc *qcc = qcs->qcc;
792 struct quic_frame *frm;
793 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200794 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200795
796 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
797
Amaury Denoyellea4569202022-04-15 17:29:25 +0200798 /* if ack_offset < buf_offset, it points to an older buffer. */
799 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
800 BUG_ON(qcs->tx.sent_offset < base_off);
801
802 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200803 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200804 BUG_ON(total < 0);
805
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200806 if (!total) {
807 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
808 return 0;
809 }
Amaury Denoyellea4569202022-04-15 17:29:25 +0200810 BUG_ON(qcs->tx.sent_offset >= qcs->tx.offset);
811 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200812
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200813 frm = pool_zalloc(pool_head_quic_frame);
814 if (!frm)
815 goto err;
816
Frédéric Lécailleb9171912022-04-21 17:32:10 +0200817 LIST_INIT(&frm->reflist);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100818 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200819 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200820 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100821 frm->stream.buf = out;
822 frm->stream.data = (unsigned char *)b_peek(out, head);
823
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100824 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200825 if (fin)
826 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100827
828 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200829 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100830 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200831 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100832
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200833 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
834 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200835
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100836 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100837
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200838 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100839 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200840 struct qcs_build_stream_trace_arg arg = {
841 .len = frm->stream.len, .fin = fin,
842 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100843 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200844 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100845 qcc->conn, qcs, &arg);
846 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100847
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200848 return total;
849
850 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100851 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200852 return -1;
853}
854
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100855/* This function must be called by the upper layer to inform about the sending
856 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
857 * <offset>.
858 */
859void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
860{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100861 struct qcc *qcc = qcs->qcc;
862 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100863
864 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200865 BUG_ON(offset >= qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100866
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100867 /* check if the STREAM frame has already been notified. It can happen
868 * for retransmission.
869 */
870 if (offset + data <= qcs->tx.sent_offset)
871 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100872
873 diff = offset + data - qcs->tx.sent_offset;
874
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100875 /* increase offset sum on connection */
876 qcc->tx.sent_offsets += diff;
877 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
878 if (qcc->tx.sent_offsets == qcc->rfctl.md)
879 qcc->flags |= QC_CF_BLK_MFCTL;
880
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100881 /* increase offset on stream */
882 qcs->tx.sent_offset += diff;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100883 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200884 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100885 if (qcs->tx.sent_offset == qcs->tx.msd)
886 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200887
888 if (qcs->tx.offset == qcs->tx.sent_offset && b_full(&qcs->stream->buf->buf)) {
889 qc_stream_buf_release(qcs->stream);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200890 /* prepare qcs for immediate send retry if data to send */
891 if (b_data(&qcs->tx.buf))
892 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200893 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100894}
895
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100896/* Wrapper for send on transport layer. Send a list of frames <frms> for the
897 * connection <qcc>.
898 *
899 * Returns 0 if all data sent with success else non-zero.
900 */
901static int qc_send_frames(struct qcc *qcc, struct list *frms)
902{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100903 /* TODO implement an opportunistic retry mechanism. This is needed
904 * because qc_send_app_pkts is not completed. It will only prepare data
905 * up to its Tx buffer. The frames left are not send even if the Tx
906 * buffer is emptied by the sendto call.
907 *
908 * To overcome this, we call repeatedly qc_send_app_pkts until we
909 * detect that the transport layer has send nothing. This could happen
910 * on congestion or sendto syscall error.
911 *
912 * When qc_send_app_pkts is improved to handle retry by itself, we can
913 * remove the looping from the MUX.
914 */
915 struct quic_frame *first_frm;
916 uint64_t first_offset = 0;
917 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100918
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100919 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
920
921 if (LIST_ISEMPTY(frms)) {
922 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200923 return 1;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100924 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100925
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200926 LIST_INIT(&qcc->send_retry_list);
927
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100928 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100929 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
930 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
931 first_offset = first_frm->stream.offset.key;
932 first_stream_frame_type = 1;
933 }
934 else {
935 first_stream_frame_type = 0;
936 }
937
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100938 if (!LIST_ISEMPTY(frms))
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +0200939 qc_send_app_pkts(qcc->conn->handle.qc, 0, frms);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100940
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100941 /* If there is frames left, check if the transport layer has send some
942 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100943 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100944 if (!LIST_ISEMPTY(frms)) {
945 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
946 goto retry_send;
947
948 /* If the first frame is STREAM, check if its offset has
949 * changed.
950 */
951 if (first_stream_frame_type &&
952 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
953 goto retry_send;
954 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100955 }
956
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100957 /* If there is frames left at this stage, transport layer is blocked.
958 * Subscribe on it to retry later.
959 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100960 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100961 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100962 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
963 SUB_RETRY_SEND, &qcc->wait_event);
964 return 1;
965 }
966
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100967 TRACE_LEAVE(QMUX_EV_QCC_SEND);
968
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100969 return 0;
970}
971
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200972/* Used internally by qc_send function. Proceed to send for <qcs>. This will
973 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
974 * is then generated and inserted in <frms> list. <qcc_max_data> is the current
975 * flow-control max-data at the connection level which must not be surpassed.
976 *
977 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
978 * be null if out buffer cannot be allocated.
979 */
980static int _qc_send_qcs(struct qcs *qcs, struct list *frms,
981 uint64_t qcc_max_data)
982{
983 struct qcc *qcc = qcs->qcc;
984 struct buffer *buf = &qcs->tx.buf;
985 struct buffer *out = qc_stream_buf_get(qcs->stream);
986 int xfer = 0;
987
988 /* Allocate <out> buffer if necessary. */
989 if (!out) {
990 if (qcc->flags & QC_CF_CONN_FULL)
991 return 0;
992
993 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
994 if (!out) {
995 qcc->flags |= QC_CF_CONN_FULL;
996 return 0;
997 }
998 }
999
1000 /* Transfer data from <buf> to <out>. */
1001 if (b_data(buf)) {
1002 xfer = qcs_xfer_data(qcs, out, buf, qcc_max_data);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001003 if (xfer > 0) {
1004 qcs_notify_send(qcs);
1005 qcs->flags &= ~QC_SF_BLK_MROOM;
1006 }
1007
1008 qcs->tx.offset += xfer;
1009 }
1010
1011 /* out buffer cannot be emptied if qcs offsets differ. */
1012 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1013
1014 /* Build a new STREAM frame with <out> buffer. */
1015 if (qcs->tx.sent_offset != qcs->tx.offset) {
1016 int ret;
1017 char fin = !!(qcs->flags & QC_SF_FIN_STREAM);
1018
1019 /* FIN is set if all incoming data were transfered. */
1020 fin = !!(fin && !b_data(buf));
1021
1022 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +02001023 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001024 }
1025
1026 return xfer;
1027}
1028
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001029/* Proceed to sending. Loop through all available streams for the <qcc>
1030 * instance and try to send as much as possible.
1031 *
1032 * Returns the total of bytes sent to the transport layer.
1033 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001034static int qc_send(struct qcc *qcc)
1035{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001036 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001037 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001038 struct qcs *qcs, *qcs_tmp;
1039 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001040
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001041 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001042
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001043 if (qcc->conn->flags & CO_FL_SOCK_WR_SH || qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001044 qcc->conn->flags |= CO_FL_ERROR;
1045 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
1046 return 0;
1047 }
1048
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001049 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1050 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
1051 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
1052 goto out;
1053 }
1054 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02001055
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001056 if (qcc->flags & QC_CF_BLK_MFCTL)
1057 return 0;
1058
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001059 /* loop through all streams, construct STREAM frames if data available.
1060 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001061 */
1062 node = eb64_first(&qcc->streams_by_id);
1063 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001064 int ret;
1065 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001066
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001067 /* TODO
1068 * for the moment, unidirectional streams have their own
1069 * mechanism for sending. This should be unified in the future,
1070 * in this case the next check will be removed.
1071 */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001072 if (quic_stream_is_uni(qcs->id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001073 node = eb64_next(node);
1074 continue;
1075 }
1076
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001077 if (qcs->flags & QC_SF_BLK_SFCTL) {
1078 node = eb64_next(node);
1079 continue;
1080 }
1081
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001082 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001083 node = eb64_next(node);
1084 continue;
1085 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001086
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001087 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + total);
1088 total += ret;
1089 node = eb64_next(node);
1090 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001091
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001092 if (qc_send_frames(qcc, &frms)) {
1093 /* data rejected by transport layer, do not retry. */
1094 goto out;
1095 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001096
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001097 retry:
1098 tmp_total = 0;
1099 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
1100 int ret;
1101 BUG_ON(!b_data(&qcs->tx.buf));
1102 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001103
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001104 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + tmp_total);
1105 tmp_total += ret;
1106 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001107 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001108
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001109 total += tmp_total;
1110 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
1111 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +01001112
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001113 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001114 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1115
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001116 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001117}
1118
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001119/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1120 * operation.
1121 *
1122 * Returns 0 on success else non-zero.
1123 */
1124static int qc_recv(struct qcc *qcc)
1125{
1126 struct eb64_node *node;
1127 struct qcs *qcs;
1128
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001129 TRACE_ENTER(QMUX_EV_QCC_RECV);
1130
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001131 node = eb64_first(&qcc->streams_by_id);
1132 while (node) {
1133 qcs = eb64_entry(node, struct qcs, by_id);
1134
1135 /* TODO unidirectional streams have their own mechanism for Rx.
1136 * This should be unified.
1137 */
1138 if (quic_stream_is_uni(qcs->id)) {
1139 node = eb64_next(node);
1140 continue;
1141 }
1142
1143 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
1144 node = eb64_next(node);
1145 continue;
1146 }
1147
1148 qcc_decode_qcs(qcc, qcs);
1149 node = eb64_next(node);
1150 }
1151
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001152 TRACE_LEAVE(QMUX_EV_QCC_RECV);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001153 return 0;
1154}
1155
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001156/* Release all streams that are already marked as detached. This is only done
1157 * if their TX buffers are empty or if a CONNECTION_CLOSE has been received.
1158 *
1159 * Return the number of released stream.
1160 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001161static int qc_release_detached_streams(struct qcc *qcc)
1162{
1163 struct eb64_node *node;
1164 int release = 0;
1165
1166 node = eb64_first(&qcc->streams_by_id);
1167 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001168 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001169 node = eb64_next(node);
1170
1171 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001172 if (!b_data(&qcs->tx.buf) &&
1173 qcs->tx.offset == qcs->tx.sent_offset) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001174 qcs_destroy(qcs);
1175 release = 1;
1176 }
1177 else {
1178 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1179 SUB_RETRY_SEND, &qcc->wait_event);
1180 }
1181 }
1182 }
1183
1184 return release;
1185}
1186
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001187static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1188{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001189 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001190
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001191 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001192
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001193 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001194
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001195 if (qc_release_detached_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001196 if (qcc_is_dead(qcc)) {
1197 qc_release(qcc);
1198 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001199 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001200 if (qcc_may_expire(qcc))
1201 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1202 else
1203 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001204 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001205 }
1206 }
1207
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001208 qc_recv(qcc);
1209
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001210 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1211
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001212 return NULL;
1213}
1214
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001215static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1216{
1217 struct qcc *qcc = ctx;
1218 int expired = tick_is_expired(t->expire, now_ms);
1219
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001220 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001221
1222 if (qcc) {
1223 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001224 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001225 return t;
1226 }
1227
1228 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001229 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001230 t->expire = TICK_ETERNITY;
1231 return t;
1232 }
1233 }
1234
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001235 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001236
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001237 if (!qcc) {
1238 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001239 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001240 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001241
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001242 qcc->task = NULL;
1243
1244 if (qcc_is_dead(qcc))
1245 qc_release(qcc);
1246
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001247 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1248
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001249 return NULL;
1250}
1251
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001252static int qc_init(struct connection *conn, struct proxy *prx,
1253 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001254{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001255 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001256 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001257
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001258 qcc = pool_alloc(pool_head_qcc);
1259 if (!qcc)
1260 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001261
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001262 qcc->conn = conn;
1263 conn->ctx = qcc;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001264 qcc->nb_cs = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001265 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001266
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001267 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001268
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001269 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001270
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001271 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001272 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001273
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001274 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001275 qcc->tx.sent_offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001276
1277 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001278 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001279 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
1280 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001281 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001282 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001283
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001284 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001285 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
1286 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001287 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001288 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001289
1290 /* Server initiated streams must respect the server flow control. */
1291 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001292 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
1293 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001294 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001295 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1296
1297 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001298 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
1299 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001300 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001301 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001302
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001303 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001304 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02001305 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
1306 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001307 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001308
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001309 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001310 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001311
Willy Tarreau784b8682022-04-11 14:18:10 +02001312 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001313 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001314 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1315 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1316
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001317 qcc->wait_event.tasklet = tasklet_new();
1318 if (!qcc->wait_event.tasklet)
1319 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001320
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001321 LIST_INIT(&qcc->send_retry_list);
1322
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001323 qcc->subs = NULL;
1324 qcc->wait_event.tasklet->process = qc_io_cb;
1325 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001326 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001327
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001328 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001329 qcc->task = NULL;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001330 qcc->timeout = prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001331 if (tick_isset(qcc->timeout)) {
1332 qcc->task = task_new_here();
1333 if (!qcc->task)
1334 goto fail_no_timeout_task;
1335 qcc->task->process = qc_timeout_task;
1336 qcc->task->context = qcc;
1337 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1338 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001339
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001340 if (!conn_is_back(conn)) {
1341 if (!LIST_INLIST(&conn->stopping_list)) {
1342 LIST_APPEND(&mux_stopping_data[tid].list,
1343 &conn->stopping_list);
1344 }
1345 }
1346
Willy Tarreau784b8682022-04-11 14:18:10 +02001347 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001348 /* init read cycle */
1349 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001350
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001351 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001352
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001353 fail_no_timeout_task:
1354 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001355 fail_no_tasklet:
1356 pool_free(pool_head_qcc, qcc);
1357 fail_no_qcc:
1358 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001359}
1360
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001361static void qc_destroy(void *ctx)
1362{
1363 struct qcc *qcc = ctx;
1364
1365 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
1366 qc_release(qcc);
1367 TRACE_LEAVE(QMUX_EV_QCC_END);
1368}
1369
Willy Tarreau4201ab72022-05-10 19:18:52 +02001370static void qc_detach(struct cs_endpoint *endp)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001371{
Willy Tarreaudf943132022-05-13 16:31:23 +02001372 struct qcs *qcs = endp->target;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001373 struct qcc *qcc = qcs->qcc;
1374
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001375 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001376
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001377 --qcc->nb_cs;
1378
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001379 if ((b_data(&qcs->tx.buf) || qcs->tx.offset > qcs->tx.sent_offset) &&
1380 !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001381 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001382 qcs->flags |= QC_SF_DETACH;
1383 return;
1384 }
1385
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001386 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001387
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001388 if (qcc_is_dead(qcc)) {
1389 qc_release(qcc);
1390 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001391 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001392 if (qcc_may_expire(qcc))
1393 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1394 else
1395 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001396 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001397 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001398
1399 TRACE_LEAVE(QMUX_EV_STRM_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001400}
1401
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001402/* Called from the upper layer, to receive data */
1403static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf,
1404 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001405{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001406 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001407 struct htx *qcs_htx = NULL;
1408 struct htx *cs_htx = NULL;
1409 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001410 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001411
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001412 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001413
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001414 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
1415 if (htx_is_empty(qcs_htx)) {
1416 /* Set buffer data to 0 as HTX is empty. */
1417 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1418 goto end;
1419 }
1420
1421 ret = qcs_htx->data;
1422
1423 cs_htx = htx_from_buf(buf);
1424 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
1425 htx_to_buf(cs_htx, buf);
1426 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1427 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
1428 goto end;
1429 }
1430
1431 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
1432 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
1433
1434 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001435 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
1436 cs_htx->flags |= HTX_FL_EOM;
1437 fin = 1;
1438 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001439
1440 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
1441 htx_to_buf(cs_htx, buf);
1442 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1443 ret -= qcs_htx->data;
1444
1445 end:
1446 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreau15b07212022-05-10 11:24:26 +02001447 qcs->endp->flags |= (CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001448 }
1449 else {
Willy Tarreau15b07212022-05-10 11:24:26 +02001450 qcs->endp->flags &= ~(CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
1451 if (qcs->endp->flags & CS_EP_ERR_PENDING)
1452 qcs->endp->flags |= CS_EP_ERROR;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001453
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001454 if (fin)
Willy Tarreau15b07212022-05-10 11:24:26 +02001455 qcs->endp->flags |= CS_EP_EOI;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001456
1457 if (b_size(&qcs->rx.app_buf)) {
1458 b_free(&qcs->rx.app_buf);
1459 offer_buffers(NULL, 1);
1460 }
1461 }
1462
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001463 if (ret) {
1464 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001465 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001466 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001467
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001468 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
1469
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001470 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001471}
1472
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001473static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf,
1474 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001475{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001476 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001477 size_t ret;
1478
1479 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001480
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001481 ret = qcs->qcc->app_ops->snd_buf(cs, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001482
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001483 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
1484
1485 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001486}
1487
1488/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1489 * event subscriber <es> is not allowed to change from a previous call as long
1490 * as at least one event is still subscribed. The <event_type> must only be a
1491 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1492 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001493static int qc_subscribe(struct conn_stream *cs, int event_type,
1494 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001495{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001496 return qcs_subscribe(__cs_mux(cs), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001497}
1498
1499/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1500 * The <es> pointer is not allowed to differ from the one passed to the
1501 * subscribe() call. It always returns zero.
1502 */
1503static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
1504{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001505 struct qcs *qcs = __cs_mux(cs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001506
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001507 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1508 BUG_ON(qcs->subs && qcs->subs != es);
1509
1510 es->events &= ~event_type;
1511 if (!es->events)
1512 qcs->subs = NULL;
1513
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001514 return 0;
1515}
1516
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001517/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Christopher Fauletb041b232022-03-24 10:27:02 +01001518 * report CS_EP_ERR_PENDING|CS_EP_ERROR on the attached conn-streams and wake
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001519 * them.
1520 */
1521static int qc_wake_some_streams(struct qcc *qcc)
1522{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001523 struct qcs *qcs;
1524 struct eb64_node *node;
1525
1526 for (node = eb64_first(&qcc->streams_by_id); node;
1527 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001528 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001529
Amaury Denoyelle1c25b182022-05-24 16:53:56 +02001530 if (!qcs->endp || !qcs->endp->cs)
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001531 continue;
1532
1533 if (qcc->conn->flags & CO_FL_ERROR) {
Christopher Fauletb041b232022-03-24 10:27:02 +01001534 qcs->endp->flags |= CS_EP_ERR_PENDING;
1535 if (qcs->endp->flags & CS_EP_EOS)
1536 qcs->endp->flags |= CS_EP_ERROR;
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001537
1538 if (qcs->subs) {
1539 qcs_notify_recv(qcs);
1540 qcs_notify_send(qcs);
1541 }
Willy Tarreau01c2a4a2022-05-10 15:46:10 +02001542 else if (qcs->endp->cs->data_cb->wake) {
1543 qcs->endp->cs->data_cb->wake(qcs->endp->cs);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001544 }
1545 }
1546 }
1547
1548 return 0;
1549}
1550
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001551static int qc_wake(struct connection *conn)
1552{
1553 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02001554 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001555
1556 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001557
1558 /* Check if a soft-stop is in progress.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001559 *
1560 * TODO this is revelant for frontend connections only.
Amaury Denoyelled0c62322022-05-23 08:52:58 +02001561 *
1562 * TODO Client should be notified with a H3 GOAWAY and then a
1563 * CONNECTION_CLOSE. However, quic-conn uses the listener socket for
1564 * sending which at this stage is already closed.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001565 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001566 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Amaury Denoyelled0c62322022-05-23 08:52:58 +02001567 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001568
Willy Tarreau784b8682022-04-11 14:18:10 +02001569 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02001570 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
1571
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001572 qc_send(qcc);
1573
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001574 qc_wake_some_streams(qcc);
1575
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001576 if (qcc_is_dead(qcc))
1577 goto release;
1578
1579 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
1580
1581 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001582
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001583 release:
1584 qc_release(qcc);
1585 TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001586 return 1;
1587}
1588
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001589
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001590static void qmux_trace_frm(const struct quic_frame *frm)
1591{
1592 switch (frm->type) {
1593 case QUIC_FT_MAX_STREAMS_BIDI:
1594 chunk_appendf(&trace_buf, " max_streams=%lu",
1595 frm->max_streams_bidi.max_streams);
1596 break;
1597
1598 case QUIC_FT_MAX_STREAMS_UNI:
1599 chunk_appendf(&trace_buf, " max_streams=%lu",
1600 frm->max_streams_uni.max_streams);
1601 break;
1602
1603 default:
1604 break;
1605 }
1606}
1607
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001608/* quic-mux trace handler */
1609static void qmux_trace(enum trace_level level, uint64_t mask,
1610 const struct trace_source *src,
1611 const struct ist where, const struct ist func,
1612 const void *a1, const void *a2, const void *a3, const void *a4)
1613{
1614 const struct connection *conn = a1;
1615 const struct qcc *qcc = conn ? conn->ctx : NULL;
1616 const struct qcs *qcs = a2;
1617
1618 if (!qcc)
1619 return;
1620
1621 if (src->verbosity > QMUX_VERB_CLEAN) {
1622 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
1623
1624 if (qcs)
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001625 chunk_appendf(&trace_buf, " qcs=%p(%lu)", qcs, qcs->id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001626
1627 if (mask & QMUX_EV_QCC_NQCS) {
1628 const uint64_t *id = a3;
1629 chunk_appendf(&trace_buf, " id=%lu", *id);
1630 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001631
1632 if (mask & QMUX_EV_SEND_FRM)
1633 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001634
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001635 if (mask & QMUX_EV_QCS_XFER_DATA) {
1636 const struct qcs_xfer_data_trace_arg *arg = a3;
1637 chunk_appendf(&trace_buf, " prep=%lu xfer=%d",
1638 arg->prep, arg->xfer);
1639 }
1640
1641 if (mask & QMUX_EV_QCS_BUILD_STRM) {
1642 const struct qcs_build_stream_trace_arg *arg = a3;
1643 chunk_appendf(&trace_buf, " len=%lu fin=%d offset=%lu",
1644 arg->len, arg->fin, arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001645 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001646 }
1647}
1648
1649
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001650static const struct mux_ops qc_ops = {
1651 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001652 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001653 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001654 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001655 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001656 .subscribe = qc_subscribe,
1657 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001658 .wake = qc_wake,
Willy Tarreaub5821e12022-04-26 11:54:08 +02001659 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02001660 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001661};
1662
1663static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001664 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001665
1666INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);