blob: 1f15fa76aeb47f8ada9c02acc9dcfc2648a8d74b [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 Denoyelle251eadf2022-03-24 17:14:52 +010014#include <haproxy/sink.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020015#include <haproxy/ssl_sock-t.h>
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010016#include <haproxy/trace.h>
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +010017#include <haproxy/xprt_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010018
Amaury Denoyelledeed7772021-12-03 11:36:46 +010019DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010020DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
21
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010022/* trace source and events */
23static void qmux_trace(enum trace_level level, uint64_t mask,
24 const struct trace_source *src,
25 const struct ist where, const struct ist func,
26 const void *a1, const void *a2, const void *a3, const void *a4);
27
28static const struct trace_event qmux_trace_events[] = {
Amaury Denoyelle4f137572022-03-24 17:10:00 +010029#define QMUX_EV_QCC_RECV (1ULL << 1)
30 { .mask = QMUX_EV_QCC_RECV, .name = "qcc_recv", .desc = "Rx on QUIC connection" },
31#define QMUX_EV_QCC_SEND (1ULL << 2)
32 { .mask = QMUX_EV_QCC_SEND, .name = "qcc_send", .desc = "Tx on QUIC connection" },
33#define QMUX_EV_QCC_WAKE (1ULL << 3)
34 { .mask = QMUX_EV_QCC_WAKE, .name = "qcc_wake", .desc = "QUIC connection woken up" },
35#define QMUX_EV_QCC_END (1ULL << 4)
36 { .mask = QMUX_EV_QCC_END, .name = "qcc_end", .desc = "QUIC connection terminated" },
37#define QMUX_EV_QCC_NQCS (1ULL << 5)
38 { .mask = QMUX_EV_QCC_NQCS, .name = "qcc_no_qcs", .desc = "QUIC stream not found" },
39#define QMUX_EV_QCS_NEW (1ULL << 6)
40 { .mask = QMUX_EV_QCS_NEW, .name = "qcs_new", .desc = "new QUIC stream" },
41#define QMUX_EV_QCS_RECV (1ULL << 7)
42 { .mask = QMUX_EV_QCS_RECV, .name = "qcs_recv", .desc = "Rx on QUIC stream" },
43#define QMUX_EV_QCS_SEND (1ULL << 8)
44 { .mask = QMUX_EV_QCS_SEND, .name = "qcs_send", .desc = "Tx on QUIC stream" },
45#define QMUX_EV_QCS_END (1ULL << 9)
46 { .mask = QMUX_EV_QCS_END, .name = "qcs_end", .desc = "QUIC stream terminated" },
47#define QMUX_EV_STRM_RECV (1ULL << 10)
48 { .mask = QMUX_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
49#define QMUX_EV_STRM_SEND (1ULL << 11)
50 { .mask = QMUX_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
51#define QMUX_EV_STRM_END (1ULL << 12)
52 { .mask = QMUX_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
Amaury Denoyellefa29f332022-03-25 09:09:40 +010053#define QMUX_EV_SEND_FRM (1ULL << 13)
54 { .mask = QMUX_EV_SEND_FRM, .name = "send_frm", .desc = "sending QUIC frame" },
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020055/* special event dedicated to qcs_xfer_data */
56#define QMUX_EV_QCS_XFER_DATA (1ULL << 14)
57 { .mask = QMUX_EV_QCS_XFER_DATA, .name = "qcs_xfer_data", .desc = "qcs_xfer_data" },
58/* special event dedicated to qcs_build_stream_frm */
59#define QMUX_EV_QCS_BUILD_STRM (1ULL << 15)
60 { .mask = QMUX_EV_QCS_BUILD_STRM, .name = "qcs_build_stream_frm", .desc = "qcs_build_stream_frm" },
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010061 { }
62};
63
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020064/* custom arg for QMUX_EV_QCS_XFER_DATA */
65struct qcs_xfer_data_trace_arg {
66 size_t prep;
Amaury Denoyellefdcec362022-03-25 09:28:10 +010067 int xfer;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020068};
69/* custom arg for QMUX_EV_QCS_BUILD_STRM */
70struct qcs_build_stream_trace_arg {
71 size_t len;
Amaury Denoyellefdcec362022-03-25 09:28:10 +010072 char fin;
73 uint64_t offset;
74};
75
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010076static const struct name_desc qmux_trace_lockon_args[4] = {
77 /* arg1 */ { /* already used by the connection */ },
78 /* arg2 */ { .name="qcs", .desc="QUIC stream" },
79 /* arg3 */ { },
80 /* arg4 */ { }
81};
82
83static const struct name_desc qmux_trace_decoding[] = {
84#define QMUX_VERB_CLEAN 1
85 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
86#define QMUX_VERB_MINIMAL 2
87 { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" },
88 { /* end */ }
89};
90
91struct trace_source trace_qmux = {
92 .name = IST("qmux"),
93 .desc = "QUIC multiplexer",
94 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
95 .default_cb = qmux_trace,
96 .known_events = qmux_trace_events,
97 .lockon_args = qmux_trace_lockon_args,
98 .decoding = qmux_trace_decoding,
99 .report_events = ~0, /* report everything by default */
100};
101
102#define TRACE_SOURCE &trace_qmux
103INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
104
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100105/* Allocate a new QUIC streams with id <id> and type <type>. */
106struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100107{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100108 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100109
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100110 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
111
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100112 qcs = pool_alloc(pool_head_qcs);
113 if (!qcs)
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200114 return NULL;
115
116 qcs->stream = NULL;
117 qcs->qcc = qcc;
Willy Tarreau01c2a4a2022-05-10 15:46:10 +0200118 qcs->endp = NULL;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200119 qcs->flags = QC_SF_NONE;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200120 qcs->ctx = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100121
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200122 /* allocate transport layer stream descriptor
123 *
124 * TODO qc_stream_desc is only useful for Tx buffering. It should not
125 * be required for unidirectional remote streams.
126 */
Frédéric Lécaille664741e2022-05-02 18:46:58 +0200127 qcs->stream = qc_stream_desc_new(id, type, qcs, qcc->conn->handle.qc);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200128 if (!qcs->stream)
129 goto err;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200130
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200131 if (qcc->app_ops->attach) {
132 if (qcc->app_ops->attach(qcs))
133 goto err;
134 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100135
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100136 qcs->endp = cs_endpoint_new();
137 if (!qcs->endp) {
138 pool_free(pool_head_qcs, qcs);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200139 goto err;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100140 }
141 qcs->endp->target = qcs;
142 qcs->endp->ctx = qcc->conn;
143 qcs->endp->flags |= (CS_EP_T_MUX|CS_EP_ORPHAN|CS_EP_NOT_FIRST);
144
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200145 qcs->id = qcs->by_id.key = id;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200146 /* store transport layer stream descriptor in qcc tree */
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200147 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100148
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100149 qcc->strms[type].nb_streams++;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100150
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100151 /* If stream is local, use peer remote-limit, or else the opposite. */
152 /* TODO use uni limit for unidirectional streams */
153 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
154 qcc->rfctl.msd_bidi_l;
155
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200156 qcs->rx.ncbuf = NCBUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100157 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100158 qcs->rx.offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100159
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200160 /* TODO use uni limit for unidirectional streams */
161 qcs->rx.msd = quic_stream_is_local(qcc, id) ? qcc->lfctl.msd_bidi_l :
162 qcc->lfctl.msd_bidi_r;
163
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 Denoyelle17014a62022-04-27 15:09:27 +0200180 if (qcs->stream)
181 qc_stream_desc_release(qcs->stream);
182
183 pool_free(pool_head_qcs, qcs);
184 return NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100185}
186
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200187static void qc_free_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
188{
189 struct buffer buf;
190
191 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
192 b_free(&buf);
193
194 *ncbuf = NCBUF_NULL;
195}
196
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200197/* Free a qcs. This function must only be done to remove a stream on allocation
198 * error or connection shutdown. Else use qcs_destroy which handle all the
199 * QUIC connection mechanism.
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100200 */
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200201void qcs_free(struct qcs *qcs)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100202{
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200203 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200204 b_free(&qcs->tx.buf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200205
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200206 BUG_ON(!qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams);
207 --qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams;
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200208
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200209 if (qcs->ctx && qcs->qcc->app_ops->detach)
210 qcs->qcc->app_ops->detach(qcs);
211
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200212 qc_stream_desc_release(qcs->stream);
213
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100214 BUG_ON(qcs->endp && !(qcs->endp->flags & CS_EP_ORPHAN));
215 cs_endpoint_free(qcs->endp);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200216
217 eb64_delete(&qcs->by_id);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100218 pool_free(pool_head_qcs, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100219}
220
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100221struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100222{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100223 struct buffer *buf = b_alloc(bptr);
224 BUG_ON(!buf);
225 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100226}
227
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200228struct ncbuf *qc_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
229{
230 struct buffer buf = BUF_NULL;
231
232 if (ncb_is_null(ncbuf)) {
233 b_alloc(&buf);
234 BUG_ON(b_is_null(&buf));
235
236 *ncbuf = ncb_make(buf.area, buf.size, 0);
237 ncb_init(ncbuf, 0);
238 }
239
240 return ncbuf;
241}
242
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100243int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
244{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100245 struct qcc *qcc = qcs->qcc;
246
247 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100248
249 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
250 BUG_ON(qcs->subs && qcs->subs != es);
251
252 es->events |= event_type;
253 qcs->subs = es;
254
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100255 if (event_type & SUB_RETRY_RECV)
256 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
257
258 if (event_type & SUB_RETRY_SEND)
259 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
260
261 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
262
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100263 return 0;
264}
265
266void qcs_notify_recv(struct qcs *qcs)
267{
268 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
269 tasklet_wakeup(qcs->subs->tasklet);
270 qcs->subs->events &= ~SUB_RETRY_RECV;
271 if (!qcs->subs->events)
272 qcs->subs = NULL;
273 }
274}
275
276void qcs_notify_send(struct qcs *qcs)
277{
278 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
279 tasklet_wakeup(qcs->subs->tasklet);
280 qcs->subs->events &= ~SUB_RETRY_SEND;
281 if (!qcs->subs->events)
282 qcs->subs = NULL;
283 }
284}
285
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100286/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
287 * several streams, depending on the already open ones.
288 * Return this node if succeeded, NULL if not.
289 */
Amaury Denoyelle50742292022-03-29 14:57:19 +0200290struct qcs *qcc_get_qcs(struct qcc *qcc, uint64_t id)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100291{
292 unsigned int strm_type;
293 int64_t sub_id;
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200294 struct eb64_node *node;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200295 struct qcs *qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100296
297 strm_type = id & QCS_ID_TYPE_MASK;
298 sub_id = id >> QCS_ID_TYPE_SHIFT;
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200299 node = NULL;
Amaury Denoyelle0dc40f02022-02-07 11:44:17 +0100300 if (quic_stream_is_local(qcc, id)) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100301 /* Local streams: this stream must be already opened. */
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200302 node = eb64_lookup(&qcc->streams_by_id, id);
303 if (!node) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100304 /* unknown stream id */
305 goto out;
306 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200307 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100308 }
309 else {
310 /* Remote streams. */
311 struct eb_root *strms;
312 uint64_t largest_id;
313 enum qcs_type qcs_type;
314
315 strms = &qcc->streams_by_id;
316 qcs_type = qcs_id_type(id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100317
318 /* TODO also checks max-streams for uni streams */
319 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100320 if (sub_id + 1 > qcc->lfctl.ms_bidi) {
Amaury Denoyellec055e302022-02-07 16:09:06 +0100321 /* streams limit reached */
322 goto out;
323 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100324 }
325
326 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
327 * correct value.
328 */
329 largest_id = qcc->strms[qcs_type].largest_id;
330 if (sub_id > (int64_t)largest_id) {
331 /* RFC: "A stream ID that is used out of order results in all streams
332 * of that type with lower-numbered stream IDs also being opened".
333 * So, let's "open" these streams.
334 */
335 int64_t i;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200336 struct qcs *tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100337
Amaury Denoyelle50742292022-03-29 14:57:19 +0200338 tmp_qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100339 for (i = largest_id + 1; i <= sub_id; i++) {
340 uint64_t id = (i << QCS_ID_TYPE_SHIFT) | strm_type;
341 enum qcs_type type = id & QCS_ID_DIR_BIT ? QCS_CLT_UNI : QCS_CLT_BIDI;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200342
Amaury Denoyelle50742292022-03-29 14:57:19 +0200343 tmp_qcs = qcs_new(qcc, id, type);
344 if (!tmp_qcs) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100345 /* allocation failure */
346 goto out;
347 }
348
349 qcc->strms[qcs_type].largest_id = i;
350 }
Amaury Denoyelle50742292022-03-29 14:57:19 +0200351 if (tmp_qcs)
352 qcs = tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100353 }
354 else {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200355 node = eb64_lookup(strms, id);
356 if (node)
357 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100358 }
359 }
360
Amaury Denoyelle50742292022-03-29 14:57:19 +0200361 return qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100362
363 out:
364 return NULL;
365}
366
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200367/* Decode the content of STREAM frames already received on the stream instance
368 * <qcs>.
369 *
370 * Returns 0 on success else non-zero.
371 */
372static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
373{
374 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
375
376 if (qcc->app_ops->decode_qcs(qcs, qcs->flags & QC_SF_FIN_RECV, qcc->ctx) < 0) {
377 TRACE_DEVEL("leaving on decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
378 return 1;
379 }
380
381 qcs_notify_recv(qcs);
382
383 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
384
385 return 0;
386}
387
388/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
389 * <data> with length <len> and represents the offset <offset>. <fin> is set if
390 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100391 *
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200392 * Returns 0 on success else non-zero.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100393 */
394int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200395 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100396{
397 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200398 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100399
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100400 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
401
Amaury Denoyelle50742292022-03-29 14:57:19 +0200402 qcs = qcc_get_qcs(qcc, id);
403 if (!qcs) {
Frédéric Lécailleb57de072022-05-02 18:58:27 +0200404 if ((id >> QCS_ID_TYPE_SHIFT) <= qcc->strms[qcs_id_type(id)].largest_id) {
405 TRACE_DEVEL("already released stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
406 return 0;
407 }
408 else {
409 TRACE_DEVEL("leaving on stream not found", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
410 return 1;
411 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100412 }
413
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100414 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100415 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 +0200416 return 0;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100417 }
418
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200419 /* TODO if last frame already received, stream size must not change.
420 * Else send FINAL_SIZE_ERROR.
421 */
422
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200423 /* TODO initial max-stream-data overflow. Implement FLOW_CONTROL_ERROR emission. */
424 BUG_ON(offset + len > qcs->rx.msd);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100425
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200426 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100427 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200428 ABORT_NOW();
429 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100430 }
431
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100432 TRACE_DEVEL("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200433 if (offset < qcs->rx.offset) {
434 len -= qcs->rx.offset - offset;
435 offset = qcs->rx.offset;
436 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100437
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200438 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
439 if (ret != NCB_RET_OK) {
440 if (ret == NCB_RET_DATA_REJ) {
441 /* TODO generate PROTOCOL_VIOLATION error */
442 TRACE_DEVEL("leaving on data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
443 qcc->conn, qcs);
444 }
445 else if (ret == NCB_RET_GAP_SIZE) {
446 TRACE_DEVEL("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
447 qcc->conn, qcs);
448 }
449 return 1;
450 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100451
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200452 /* TODO initial max-stream-data reached. Implement MAX_STREAM_DATA emission. */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200453 BUG_ON(offset + len == qcs->rx.msd);
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +0200454
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100455 if (fin)
456 qcs->flags |= QC_SF_FIN_RECV;
457
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200458 if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL))
459 qcc_decode_qcs(qcc, qcs);
460
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100461 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100462 return 0;
463}
464
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100465/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
466 * the frame.
467 *
468 * Returns 0 on success else non-zero.
469 */
470int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
471{
472 if (qcc->rfctl.md < max) {
473 qcc->rfctl.md = max;
474
475 if (qcc->flags & QC_CF_BLK_MFCTL) {
476 qcc->flags &= ~QC_CF_BLK_MFCTL;
477 tasklet_wakeup(qcc->wait_event.tasklet);
478 }
479 }
480 return 0;
481}
482
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100483/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
484 * field of the frame and <id> is the identifier of the QUIC stream.
485 *
486 * Returns 0 on success else non-zero.
487 */
488int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
489{
490 struct qcs *qcs;
491 struct eb64_node *node;
492
493 node = eb64_lookup(&qcc->streams_by_id, id);
494 if (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200495 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100496 if (max > qcs->tx.msd) {
497 qcs->tx.msd = max;
498
499 if (qcs->flags & QC_SF_BLK_SFCTL) {
500 qcs->flags &= ~QC_SF_BLK_SFCTL;
501 tasklet_wakeup(qcc->wait_event.tasklet);
502 }
503 }
504 }
505
506 return 0;
507}
508
Amaury Denoyellec055e302022-02-07 16:09:06 +0100509static int qc_is_max_streams_needed(struct qcc *qcc)
510{
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100511 return qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100512}
513
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500514/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100515static void qcs_destroy(struct qcs *qcs)
516{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100517 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200518 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100519
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100520 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100521
Amaury Denoyellec055e302022-02-07 16:09:06 +0100522 if (quic_stream_is_remote(qcs->qcc, id)) {
523 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100524 ++qcs->qcc->lfctl.cl_bidi_r;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100525 if (qc_is_max_streams_needed(qcs->qcc))
526 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
527 }
528 }
529
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200530 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100531
532 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100533}
534
535static inline int qcc_is_dead(const struct qcc *qcc)
536{
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200537 if (qcc->app_ops && qcc->app_ops->is_active &&
538 qcc->app_ops->is_active(qcc, qcc->ctx))
539 return 0;
540
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200541 if ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task)
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100542 return 1;
543
544 return 0;
545}
546
547/* Return true if the mux timeout should be armed. */
548static inline int qcc_may_expire(struct qcc *qcc)
549{
Amaury Denoyelle06890aa2022-04-04 16:15:06 +0200550 return !qcc->nb_cs;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100551}
552
553/* release function. This one should be called to free all resources allocated
554 * to the mux.
555 */
556static void qc_release(struct qcc *qcc)
557{
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200558 struct connection *conn = qcc->conn;
559 struct eb64_node *node;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100560
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100561 TRACE_ENTER(QMUX_EV_QCC_END);
562
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200563 if (qcc->app_ops && qcc->app_ops->release)
564 qcc->app_ops->release(qcc->ctx);
Amaury Denoyellef8909452022-03-30 11:51:56 +0200565
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200566 if (qcc->task) {
567 task_destroy(qcc->task);
568 qcc->task = NULL;
569 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100570
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200571 if (qcc->wait_event.tasklet)
572 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyellef3e03a42022-04-21 15:41:34 +0200573 if (conn && qcc->wait_event.events) {
574 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
575 qcc->wait_event.events,
576 &qcc->wait_event);
577 }
Amaury Denoyellef8909452022-03-30 11:51:56 +0200578
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200579 /* liberate remaining qcs instances */
580 node = eb64_first(&qcc->streams_by_id);
581 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200582 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200583 node = eb64_next(node);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200584 qcs_free(qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100585 }
586
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200587 pool_free(pool_head_qcc, qcc);
588
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100589 if (conn) {
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100590 LIST_DEL_INIT(&conn->stopping_list);
591
Willy Tarreau784b8682022-04-11 14:18:10 +0200592 conn->handle.qc->conn = NULL;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100593 conn->mux = NULL;
594 conn->ctx = NULL;
595
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100596 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
597
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100598 conn_stop_tracking(conn);
599 conn_full_close(conn);
600 if (conn->destroy_cb)
601 conn->destroy_cb(conn);
602 conn_free(conn);
603 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100604
605 TRACE_LEAVE(QMUX_EV_QCC_END);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100606}
607
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200608/* Transfer as much as possible data on <qcs> from <in> to <out>. <max_data> is
609 * the current flow-control limit on the connection which must not be exceeded.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100610 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200611 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100612 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200613static int qcs_xfer_data(struct qcs *qcs, struct buffer *out,
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200614 struct buffer *in, uint64_t max_data)
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200615{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100616 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200617 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100618 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200619
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100620 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100621
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100622 qc_get_buf(qcs, out);
623
624 /*
625 * QCS out buffer diagram
626 * head left to_xfer
627 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100628 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100629 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100630 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100631 * ^ ack-off ^ sent-off ^ off
632 *
633 * STREAM frame
634 * ^ ^
635 * |xxxxxxxxxxxxxxxxx|
636 */
637
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200638 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100639 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
640
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100641 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200642 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100643
644 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
645 /* do not exceed flow control limit */
646 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
647 to_xfer = qcs->tx.msd - qcs->tx.offset;
648
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100649 BUG_ON_HOT(max_data > qcc->rfctl.md);
650 /* do not overcome flow control limit on connection */
651 if (max_data + to_xfer > qcc->rfctl.md)
652 to_xfer = qcc->rfctl.md - max_data;
653
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100654 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200655 goto out;
656
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200657 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200658
659 out:
660 {
661 struct qcs_xfer_data_trace_arg arg = {
662 .prep = b_data(out), .xfer = total,
663 };
664 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
665 qcc->conn, qcs, &arg);
666 }
667
668 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200669}
670
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200671/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
672 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
673 * stream frame.
674 *
675 * Returns the length of the STREAM frame or a negative error code.
676 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200677static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
678 struct list *frm_list)
679{
680 struct qcc *qcc = qcs->qcc;
681 struct quic_frame *frm;
682 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200683 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200684
685 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
686
Amaury Denoyellea4569202022-04-15 17:29:25 +0200687 /* if ack_offset < buf_offset, it points to an older buffer. */
688 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
689 BUG_ON(qcs->tx.sent_offset < base_off);
690
691 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200692 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200693 BUG_ON(total < 0);
694
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200695 if (!total) {
696 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
697 return 0;
698 }
Amaury Denoyellea4569202022-04-15 17:29:25 +0200699 BUG_ON(qcs->tx.sent_offset >= qcs->tx.offset);
700 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200701
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200702 frm = pool_zalloc(pool_head_quic_frame);
703 if (!frm)
704 goto err;
705
Frédéric Lécailleb9171912022-04-21 17:32:10 +0200706 LIST_INIT(&frm->reflist);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100707 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200708 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200709 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100710 frm->stream.buf = out;
711 frm->stream.data = (unsigned char *)b_peek(out, head);
712
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100713 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200714 if (fin)
715 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100716
717 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200718 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100719 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200720 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100721
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200722 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
723 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200724
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100725 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100726
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200727 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100728 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200729 struct qcs_build_stream_trace_arg arg = {
730 .len = frm->stream.len, .fin = fin,
731 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100732 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200733 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100734 qcc->conn, qcs, &arg);
735 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100736
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200737 return total;
738
739 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100740 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200741 return -1;
742}
743
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100744/* This function must be called by the upper layer to inform about the sending
745 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
746 * <offset>.
747 */
748void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
749{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100750 struct qcc *qcc = qcs->qcc;
751 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100752
753 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200754 BUG_ON(offset >= qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100755
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100756 /* check if the STREAM frame has already been notified. It can happen
757 * for retransmission.
758 */
759 if (offset + data <= qcs->tx.sent_offset)
760 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100761
762 diff = offset + data - qcs->tx.sent_offset;
763
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100764 /* increase offset sum on connection */
765 qcc->tx.sent_offsets += diff;
766 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
767 if (qcc->tx.sent_offsets == qcc->rfctl.md)
768 qcc->flags |= QC_CF_BLK_MFCTL;
769
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100770 /* increase offset on stream */
771 qcs->tx.sent_offset += diff;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100772 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200773 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100774 if (qcs->tx.sent_offset == qcs->tx.msd)
775 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200776
777 if (qcs->tx.offset == qcs->tx.sent_offset && b_full(&qcs->stream->buf->buf)) {
778 qc_stream_buf_release(qcs->stream);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200779 /* prepare qcs for immediate send retry if data to send */
780 if (b_data(&qcs->tx.buf))
781 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200782 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100783}
784
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100785/* Wrapper for send on transport layer. Send a list of frames <frms> for the
786 * connection <qcc>.
787 *
788 * Returns 0 if all data sent with success else non-zero.
789 */
790static int qc_send_frames(struct qcc *qcc, struct list *frms)
791{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100792 /* TODO implement an opportunistic retry mechanism. This is needed
793 * because qc_send_app_pkts is not completed. It will only prepare data
794 * up to its Tx buffer. The frames left are not send even if the Tx
795 * buffer is emptied by the sendto call.
796 *
797 * To overcome this, we call repeatedly qc_send_app_pkts until we
798 * detect that the transport layer has send nothing. This could happen
799 * on congestion or sendto syscall error.
800 *
801 * When qc_send_app_pkts is improved to handle retry by itself, we can
802 * remove the looping from the MUX.
803 */
804 struct quic_frame *first_frm;
805 uint64_t first_offset = 0;
806 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100807
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100808 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
809
810 if (LIST_ISEMPTY(frms)) {
811 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200812 return 1;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100813 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100814
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200815 LIST_INIT(&qcc->send_retry_list);
816
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100817 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100818 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
819 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
820 first_offset = first_frm->stream.offset.key;
821 first_stream_frame_type = 1;
822 }
823 else {
824 first_stream_frame_type = 0;
825 }
826
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100827 if (!LIST_ISEMPTY(frms))
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +0200828 qc_send_app_pkts(qcc->conn->handle.qc, 0, frms);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100829
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100830 /* If there is frames left, check if the transport layer has send some
831 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100832 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100833 if (!LIST_ISEMPTY(frms)) {
834 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
835 goto retry_send;
836
837 /* If the first frame is STREAM, check if its offset has
838 * changed.
839 */
840 if (first_stream_frame_type &&
841 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
842 goto retry_send;
843 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100844 }
845
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100846 /* If there is frames left at this stage, transport layer is blocked.
847 * Subscribe on it to retry later.
848 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100849 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100850 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100851 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
852 SUB_RETRY_SEND, &qcc->wait_event);
853 return 1;
854 }
855
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100856 TRACE_LEAVE(QMUX_EV_QCC_SEND);
857
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100858 return 0;
859}
860
Amaury Denoyellec9337802022-04-04 16:36:34 +0200861/* Send a MAX_STREAM_BIDI frame to update the limit of bidirectional streams
862 * allowed to be opened by the peer. The caller should have first checked if
863 * this is required with qc_is_max_streams_needed.
864 *
865 * Returns 0 on success else non-zero.
866 */
867static int qc_send_max_streams(struct qcc *qcc)
868{
869 struct list frms = LIST_HEAD_INIT(frms);
870 struct quic_frame *frm;
871
872 frm = pool_zalloc(pool_head_quic_frame);
873 BUG_ON(!frm); /* TODO handle this properly */
874
Frédéric Lécailleb9171912022-04-21 17:32:10 +0200875 LIST_INIT(&frm->reflist);
Amaury Denoyellec9337802022-04-04 16:36:34 +0200876 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
877 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
878 qcc->lfctl.cl_bidi_r;
879 TRACE_DEVEL("sending MAX_STREAMS frame", QMUX_EV_SEND_FRM, qcc->conn, NULL, frm);
880 LIST_APPEND(&frms, &frm->list);
881
882 if (qc_send_frames(qcc, &frms))
883 return 1;
884
885 /* save the new limit if the frame has been send. */
886 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
887 qcc->lfctl.cl_bidi_r = 0;
888
889 return 0;
890}
891
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200892/* Used internally by qc_send function. Proceed to send for <qcs>. This will
893 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
894 * is then generated and inserted in <frms> list. <qcc_max_data> is the current
895 * flow-control max-data at the connection level which must not be surpassed.
896 *
897 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
898 * be null if out buffer cannot be allocated.
899 */
900static int _qc_send_qcs(struct qcs *qcs, struct list *frms,
901 uint64_t qcc_max_data)
902{
903 struct qcc *qcc = qcs->qcc;
904 struct buffer *buf = &qcs->tx.buf;
905 struct buffer *out = qc_stream_buf_get(qcs->stream);
906 int xfer = 0;
907
908 /* Allocate <out> buffer if necessary. */
909 if (!out) {
910 if (qcc->flags & QC_CF_CONN_FULL)
911 return 0;
912
913 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
914 if (!out) {
915 qcc->flags |= QC_CF_CONN_FULL;
916 return 0;
917 }
918 }
919
920 /* Transfer data from <buf> to <out>. */
921 if (b_data(buf)) {
922 xfer = qcs_xfer_data(qcs, out, buf, qcc_max_data);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200923 if (xfer > 0) {
924 qcs_notify_send(qcs);
925 qcs->flags &= ~QC_SF_BLK_MROOM;
926 }
927
928 qcs->tx.offset += xfer;
929 }
930
931 /* out buffer cannot be emptied if qcs offsets differ. */
932 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
933
934 /* Build a new STREAM frame with <out> buffer. */
935 if (qcs->tx.sent_offset != qcs->tx.offset) {
936 int ret;
937 char fin = !!(qcs->flags & QC_SF_FIN_STREAM);
938
939 /* FIN is set if all incoming data were transfered. */
940 fin = !!(fin && !b_data(buf));
941
942 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +0200943 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200944 }
945
946 return xfer;
947}
948
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100949/* Proceed to sending. Loop through all available streams for the <qcc>
950 * instance and try to send as much as possible.
951 *
952 * Returns the total of bytes sent to the transport layer.
953 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100954static int qc_send(struct qcc *qcc)
955{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100956 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200957 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200958 struct qcs *qcs, *qcs_tmp;
959 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200960
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100961 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200962
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200963 if (qcc->conn->flags & CO_FL_SOCK_WR_SH) {
964 qcc->conn->flags |= CO_FL_ERROR;
965 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
966 return 0;
967 }
968
Amaury Denoyellec9337802022-04-04 16:36:34 +0200969 if (qc_is_max_streams_needed(qcc))
970 qc_send_max_streams(qcc);
971
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100972 if (qcc->flags & QC_CF_BLK_MFCTL)
973 return 0;
974
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100975 /* loop through all streams, construct STREAM frames if data available.
976 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200977 */
978 node = eb64_first(&qcc->streams_by_id);
979 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200980 int ret;
981 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100982
Amaury Denoyellee2ec9422022-03-10 16:46:18 +0100983 /* TODO
984 * for the moment, unidirectional streams have their own
985 * mechanism for sending. This should be unified in the future,
986 * in this case the next check will be removed.
987 */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200988 if (quic_stream_is_uni(qcs->id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +0100989 node = eb64_next(node);
990 continue;
991 }
992
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100993 if (qcs->flags & QC_SF_BLK_SFCTL) {
994 node = eb64_next(node);
995 continue;
996 }
997
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200998 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +0200999 node = eb64_next(node);
1000 continue;
1001 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001002
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001003 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + total);
1004 total += ret;
1005 node = eb64_next(node);
1006 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001007
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001008 if (qc_send_frames(qcc, &frms)) {
1009 /* data rejected by transport layer, do not retry. */
1010 goto out;
1011 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001012
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001013 retry:
1014 tmp_total = 0;
1015 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
1016 int ret;
1017 BUG_ON(!b_data(&qcs->tx.buf));
1018 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001019
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001020 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + tmp_total);
1021 tmp_total += ret;
1022 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001023 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001024
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001025 total += tmp_total;
1026 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
1027 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +01001028
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001029 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001030 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1031
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001032 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001033}
1034
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001035/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1036 * operation.
1037 *
1038 * Returns 0 on success else non-zero.
1039 */
1040static int qc_recv(struct qcc *qcc)
1041{
1042 struct eb64_node *node;
1043 struct qcs *qcs;
1044
1045 node = eb64_first(&qcc->streams_by_id);
1046 while (node) {
1047 qcs = eb64_entry(node, struct qcs, by_id);
1048
1049 /* TODO unidirectional streams have their own mechanism for Rx.
1050 * This should be unified.
1051 */
1052 if (quic_stream_is_uni(qcs->id)) {
1053 node = eb64_next(node);
1054 continue;
1055 }
1056
1057 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
1058 node = eb64_next(node);
1059 continue;
1060 }
1061
1062 qcc_decode_qcs(qcc, qcs);
1063 node = eb64_next(node);
1064 }
1065
1066 return 0;
1067}
1068
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001069/* Release all streams that are already marked as detached. This is only done
1070 * if their TX buffers are empty or if a CONNECTION_CLOSE has been received.
1071 *
1072 * Return the number of released stream.
1073 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001074static int qc_release_detached_streams(struct qcc *qcc)
1075{
1076 struct eb64_node *node;
1077 int release = 0;
1078
1079 node = eb64_first(&qcc->streams_by_id);
1080 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001081 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001082 node = eb64_next(node);
1083
1084 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001085 if (!b_data(&qcs->tx.buf) &&
1086 qcs->tx.offset == qcs->tx.sent_offset) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001087 qcs_destroy(qcs);
1088 release = 1;
1089 }
1090 else {
1091 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1092 SUB_RETRY_SEND, &qcc->wait_event);
1093 }
1094 }
1095 }
1096
1097 return release;
1098}
1099
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001100static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1101{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001102 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001103
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001104 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001105
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001106 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001107
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001108 if (qc_release_detached_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001109 if (qcc_is_dead(qcc)) {
1110 qc_release(qcc);
1111 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001112 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001113 if (qcc_may_expire(qcc))
1114 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1115 else
1116 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001117 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001118 }
1119 }
1120
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001121 qc_recv(qcc);
1122
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001123 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1124
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001125 return NULL;
1126}
1127
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001128static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1129{
1130 struct qcc *qcc = ctx;
1131 int expired = tick_is_expired(t->expire, now_ms);
1132
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001133 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001134
1135 if (qcc) {
1136 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001137 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001138 return t;
1139 }
1140
1141 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001142 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001143 t->expire = TICK_ETERNITY;
1144 return t;
1145 }
1146 }
1147
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001148 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001149
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001150 if (!qcc) {
1151 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001152 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001153 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001154
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001155 qcc->task = NULL;
1156
1157 if (qcc_is_dead(qcc))
1158 qc_release(qcc);
1159
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001160 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1161
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001162 return NULL;
1163}
1164
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001165static int qc_init(struct connection *conn, struct proxy *prx,
1166 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001167{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001168 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001169 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001170
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001171 qcc = pool_alloc(pool_head_qcc);
1172 if (!qcc)
1173 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001174
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001175 qcc->conn = conn;
1176 conn->ctx = qcc;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001177 qcc->nb_cs = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001178 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001179
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001180 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001181
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001182 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001183
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001184 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001185 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001186
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001187 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001188 qcc->tx.sent_offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001189
1190 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001191 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001192 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
1193 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001194 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001195 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001196
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001197 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001198 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
1199 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001200 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001201 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001202
1203 /* Server initiated streams must respect the server flow control. */
1204 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001205 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
1206 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001207 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001208 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1209
1210 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001211 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
1212 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001213 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001214 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001215
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001216 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02001217 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
1218 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001219 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001220
Willy Tarreau784b8682022-04-11 14:18:10 +02001221 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001222 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001223 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1224 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1225
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001226 qcc->wait_event.tasklet = tasklet_new();
1227 if (!qcc->wait_event.tasklet)
1228 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001229
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001230 LIST_INIT(&qcc->send_retry_list);
1231
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001232 qcc->subs = NULL;
1233 qcc->wait_event.tasklet->process = qc_io_cb;
1234 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001235 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001236
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001237 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001238 qcc->task = NULL;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001239 qcc->timeout = prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001240 if (tick_isset(qcc->timeout)) {
1241 qcc->task = task_new_here();
1242 if (!qcc->task)
1243 goto fail_no_timeout_task;
1244 qcc->task->process = qc_timeout_task;
1245 qcc->task->context = qcc;
1246 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1247 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001248
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001249 if (!conn_is_back(conn)) {
1250 if (!LIST_INLIST(&conn->stopping_list)) {
1251 LIST_APPEND(&mux_stopping_data[tid].list,
1252 &conn->stopping_list);
1253 }
1254 }
1255
Willy Tarreau784b8682022-04-11 14:18:10 +02001256 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001257 /* init read cycle */
1258 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001259
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001260 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001261
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001262 fail_no_timeout_task:
1263 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001264 fail_no_tasklet:
1265 pool_free(pool_head_qcc, qcc);
1266 fail_no_qcc:
1267 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001268}
1269
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001270static void qc_destroy(void *ctx)
1271{
1272 struct qcc *qcc = ctx;
1273
1274 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
1275 qc_release(qcc);
1276 TRACE_LEAVE(QMUX_EV_QCC_END);
1277}
1278
Willy Tarreau4201ab72022-05-10 19:18:52 +02001279static void qc_detach(struct cs_endpoint *endp)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001280{
Willy Tarreaudf943132022-05-13 16:31:23 +02001281 struct qcs *qcs = endp->target;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001282 struct qcc *qcc = qcs->qcc;
1283
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001284 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001285
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001286 --qcc->nb_cs;
1287
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001288 if ((b_data(&qcs->tx.buf) || qcs->tx.offset > qcs->tx.sent_offset) &&
1289 !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001290 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001291 qcs->flags |= QC_SF_DETACH;
1292 return;
1293 }
1294
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001295 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001296
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001297 if (qcc_is_dead(qcc)) {
1298 qc_release(qcc);
1299 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001300 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001301 if (qcc_may_expire(qcc))
1302 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1303 else
1304 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001305 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001306 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001307
1308 TRACE_LEAVE(QMUX_EV_STRM_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001309}
1310
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001311/* Called from the upper layer, to receive data */
1312static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf,
1313 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001314{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001315 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001316 struct htx *qcs_htx = NULL;
1317 struct htx *cs_htx = NULL;
1318 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001319 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001320
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001321 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001322
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001323 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
1324 if (htx_is_empty(qcs_htx)) {
1325 /* Set buffer data to 0 as HTX is empty. */
1326 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1327 goto end;
1328 }
1329
1330 ret = qcs_htx->data;
1331
1332 cs_htx = htx_from_buf(buf);
1333 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
1334 htx_to_buf(cs_htx, buf);
1335 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1336 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
1337 goto end;
1338 }
1339
1340 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
1341 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
1342
1343 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001344 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
1345 cs_htx->flags |= HTX_FL_EOM;
1346 fin = 1;
1347 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001348
1349 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
1350 htx_to_buf(cs_htx, buf);
1351 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1352 ret -= qcs_htx->data;
1353
1354 end:
1355 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreau15b07212022-05-10 11:24:26 +02001356 qcs->endp->flags |= (CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001357 }
1358 else {
Willy Tarreau15b07212022-05-10 11:24:26 +02001359 qcs->endp->flags &= ~(CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
1360 if (qcs->endp->flags & CS_EP_ERR_PENDING)
1361 qcs->endp->flags |= CS_EP_ERROR;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001362
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001363 if (fin)
Willy Tarreau15b07212022-05-10 11:24:26 +02001364 qcs->endp->flags |= CS_EP_EOI;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001365
1366 if (b_size(&qcs->rx.app_buf)) {
1367 b_free(&qcs->rx.app_buf);
1368 offer_buffers(NULL, 1);
1369 }
1370 }
1371
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001372 if (ret) {
1373 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001374 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001375 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001376
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001377 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
1378
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001379 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001380}
1381
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001382static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf,
1383 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001384{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001385 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001386 size_t ret;
1387
1388 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001389
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001390 ret = qcs->qcc->app_ops->snd_buf(cs, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001391
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001392 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
1393
1394 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001395}
1396
1397/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1398 * event subscriber <es> is not allowed to change from a previous call as long
1399 * as at least one event is still subscribed. The <event_type> must only be a
1400 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1401 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001402static int qc_subscribe(struct conn_stream *cs, int event_type,
1403 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001404{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001405 return qcs_subscribe(__cs_mux(cs), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001406}
1407
1408/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1409 * The <es> pointer is not allowed to differ from the one passed to the
1410 * subscribe() call. It always returns zero.
1411 */
1412static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
1413{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001414 struct qcs *qcs = __cs_mux(cs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001415
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001416 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1417 BUG_ON(qcs->subs && qcs->subs != es);
1418
1419 es->events &= ~event_type;
1420 if (!es->events)
1421 qcs->subs = NULL;
1422
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001423 return 0;
1424}
1425
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001426/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Christopher Fauletb041b232022-03-24 10:27:02 +01001427 * report CS_EP_ERR_PENDING|CS_EP_ERROR on the attached conn-streams and wake
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001428 * them.
1429 */
1430static int qc_wake_some_streams(struct qcc *qcc)
1431{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001432 struct qcs *qcs;
1433 struct eb64_node *node;
1434
1435 for (node = eb64_first(&qcc->streams_by_id); node;
1436 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001437 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001438
Willy Tarreau01c2a4a2022-05-10 15:46:10 +02001439 if (!qcs->endp->cs)
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001440 continue;
1441
1442 if (qcc->conn->flags & CO_FL_ERROR) {
Christopher Fauletb041b232022-03-24 10:27:02 +01001443 qcs->endp->flags |= CS_EP_ERR_PENDING;
1444 if (qcs->endp->flags & CS_EP_EOS)
1445 qcs->endp->flags |= CS_EP_ERROR;
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001446
1447 if (qcs->subs) {
1448 qcs_notify_recv(qcs);
1449 qcs_notify_send(qcs);
1450 }
Willy Tarreau01c2a4a2022-05-10 15:46:10 +02001451 else if (qcs->endp->cs->data_cb->wake) {
1452 qcs->endp->cs->data_cb->wake(qcs->endp->cs);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001453 }
1454 }
1455 }
1456
1457 return 0;
1458}
1459
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001460static int qc_wake(struct connection *conn)
1461{
1462 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02001463 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001464
1465 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001466
1467 /* Check if a soft-stop is in progress.
1468 * Release idling front connection if this is the case.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001469 *
1470 * TODO this is revelant for frontend connections only.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001471 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001472 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
1473 goto release;
1474
Willy Tarreau784b8682022-04-11 14:18:10 +02001475 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02001476 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
1477
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001478 qc_send(qcc);
1479
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001480 qc_wake_some_streams(qcc);
1481
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001482 if (qcc_is_dead(qcc))
1483 goto release;
1484
1485 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
1486
1487 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001488
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001489 release:
1490 qc_release(qcc);
1491 TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001492 return 1;
1493}
1494
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001495
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001496static void qmux_trace_frm(const struct quic_frame *frm)
1497{
1498 switch (frm->type) {
1499 case QUIC_FT_MAX_STREAMS_BIDI:
1500 chunk_appendf(&trace_buf, " max_streams=%lu",
1501 frm->max_streams_bidi.max_streams);
1502 break;
1503
1504 case QUIC_FT_MAX_STREAMS_UNI:
1505 chunk_appendf(&trace_buf, " max_streams=%lu",
1506 frm->max_streams_uni.max_streams);
1507 break;
1508
1509 default:
1510 break;
1511 }
1512}
1513
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001514/* quic-mux trace handler */
1515static void qmux_trace(enum trace_level level, uint64_t mask,
1516 const struct trace_source *src,
1517 const struct ist where, const struct ist func,
1518 const void *a1, const void *a2, const void *a3, const void *a4)
1519{
1520 const struct connection *conn = a1;
1521 const struct qcc *qcc = conn ? conn->ctx : NULL;
1522 const struct qcs *qcs = a2;
1523
1524 if (!qcc)
1525 return;
1526
1527 if (src->verbosity > QMUX_VERB_CLEAN) {
1528 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
1529
1530 if (qcs)
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001531 chunk_appendf(&trace_buf, " qcs=%p(%lu)", qcs, qcs->id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001532
1533 if (mask & QMUX_EV_QCC_NQCS) {
1534 const uint64_t *id = a3;
1535 chunk_appendf(&trace_buf, " id=%lu", *id);
1536 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001537
1538 if (mask & QMUX_EV_SEND_FRM)
1539 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001540
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001541 if (mask & QMUX_EV_QCS_XFER_DATA) {
1542 const struct qcs_xfer_data_trace_arg *arg = a3;
1543 chunk_appendf(&trace_buf, " prep=%lu xfer=%d",
1544 arg->prep, arg->xfer);
1545 }
1546
1547 if (mask & QMUX_EV_QCS_BUILD_STRM) {
1548 const struct qcs_build_stream_trace_arg *arg = a3;
1549 chunk_appendf(&trace_buf, " len=%lu fin=%d offset=%lu",
1550 arg->len, arg->fin, arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001551 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001552 }
1553}
1554
Amaury Denoyelle251eadf2022-03-24 17:14:52 +01001555/* Function to automatically activate QUIC MUX traces on stdout.
1556 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
1557 * Main use for now is in the docker image for QUIC interop testing.
1558 */
1559static void qmux_init_stdout_traces(void)
1560{
1561#ifdef ENABLE_QUIC_STDOUT_TRACES
1562 trace_qmux.sink = sink_find("stdout");
1563 trace_qmux.level = TRACE_LEVEL_DEVELOPER;
1564 trace_qmux.state = TRACE_STATE_RUNNING;
1565 trace_qmux.verbosity = QMUX_VERB_MINIMAL;
1566#endif
1567}
1568INITCALL0(STG_INIT, qmux_init_stdout_traces);
1569
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001570
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001571static const struct mux_ops qc_ops = {
1572 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001573 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001574 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001575 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001576 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001577 .subscribe = qc_subscribe,
1578 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001579 .wake = qc_wake,
Christopher Fauleta97cced2022-04-12 18:04:10 +02001580 .flags = MX_FL_HTX|MX_FL_NO_UPG,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02001581 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001582};
1583
1584static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001585 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001586
1587INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);