blob: d5f448ca5e0e6ac65082c3578a1b417a82f8f7e3 [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 Denoyelledeed7772021-12-03 11:36:46 +010011#include <haproxy/pool.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020012#include <haproxy/quic_stream.h>
Amaury Denoyelle251eadf2022-03-24 17:14:52 +010013#include <haproxy/sink.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 Denoyelledeed7772021-12-03 11:36:46 +0100104/* Allocate a new QUIC streams with id <id> and type <type>. */
105struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100106{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100107 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100108
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100109 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
110
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100111 qcs = pool_alloc(pool_head_qcs);
112 if (!qcs)
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200113 return NULL;
114
115 qcs->stream = NULL;
116 qcs->qcc = qcc;
117 qcs->cs = NULL;
118 qcs->flags = QC_SF_NONE;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200119 qcs->ctx = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100120
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200121 /* allocate transport layer stream descriptor
122 *
123 * TODO qc_stream_desc is only useful for Tx buffering. It should not
124 * be required for unidirectional remote streams.
125 */
Frédéric Lécaille664741e2022-05-02 18:46:58 +0200126 qcs->stream = qc_stream_desc_new(id, type, qcs, qcc->conn->handle.qc);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200127 if (!qcs->stream)
128 goto err;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200129
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200130 if (qcc->app_ops->attach) {
131 if (qcc->app_ops->attach(qcs))
132 goto err;
133 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100134
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100135 qcs->endp = cs_endpoint_new();
136 if (!qcs->endp) {
137 pool_free(pool_head_qcs, qcs);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200138 goto err;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100139 }
140 qcs->endp->target = qcs;
141 qcs->endp->ctx = qcc->conn;
142 qcs->endp->flags |= (CS_EP_T_MUX|CS_EP_ORPHAN|CS_EP_NOT_FIRST);
143
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 Denoyelledeed7772021-12-03 11:36:46 +0100155 qcs->rx.buf = BUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100156 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100157 qcs->rx.offset = 0;
158 qcs->rx.frms = EB_ROOT_UNIQUE;
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 Denoyelledccbd732022-03-29 18:36:59 +0200187/* Free a qcs. This function must only be done to remove a stream on allocation
188 * error or connection shutdown. Else use qcs_destroy which handle all the
189 * QUIC connection mechanism.
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100190 */
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200191void qcs_free(struct qcs *qcs)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100192{
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200193 b_free(&qcs->rx.buf);
194 b_free(&qcs->tx.buf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200195
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200196 BUG_ON(!qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams);
197 --qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams;
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200198
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200199 if (qcs->ctx && qcs->qcc->app_ops->detach)
200 qcs->qcc->app_ops->detach(qcs);
201
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200202 qc_stream_desc_release(qcs->stream);
203
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100204 BUG_ON(qcs->endp && !(qcs->endp->flags & CS_EP_ORPHAN));
205 cs_endpoint_free(qcs->endp);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200206
207 eb64_delete(&qcs->by_id);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100208 pool_free(pool_head_qcs, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100209}
210
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100211struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100212{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100213 struct buffer *buf = b_alloc(bptr);
214 BUG_ON(!buf);
215 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100216}
217
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100218int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
219{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100220 struct qcc *qcc = qcs->qcc;
221
222 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100223
224 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
225 BUG_ON(qcs->subs && qcs->subs != es);
226
227 es->events |= event_type;
228 qcs->subs = es;
229
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100230 if (event_type & SUB_RETRY_RECV)
231 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
232
233 if (event_type & SUB_RETRY_SEND)
234 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
235
236 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
237
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100238 return 0;
239}
240
241void qcs_notify_recv(struct qcs *qcs)
242{
243 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
244 tasklet_wakeup(qcs->subs->tasklet);
245 qcs->subs->events &= ~SUB_RETRY_RECV;
246 if (!qcs->subs->events)
247 qcs->subs = NULL;
248 }
249}
250
251void qcs_notify_send(struct qcs *qcs)
252{
253 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
254 tasklet_wakeup(qcs->subs->tasklet);
255 qcs->subs->events &= ~SUB_RETRY_SEND;
256 if (!qcs->subs->events)
257 qcs->subs = NULL;
258 }
259}
260
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100261/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
262 * several streams, depending on the already open ones.
263 * Return this node if succeeded, NULL if not.
264 */
Amaury Denoyelle50742292022-03-29 14:57:19 +0200265struct qcs *qcc_get_qcs(struct qcc *qcc, uint64_t id)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100266{
267 unsigned int strm_type;
268 int64_t sub_id;
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200269 struct eb64_node *node;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200270 struct qcs *qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100271
272 strm_type = id & QCS_ID_TYPE_MASK;
273 sub_id = id >> QCS_ID_TYPE_SHIFT;
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200274 node = NULL;
Amaury Denoyelle0dc40f02022-02-07 11:44:17 +0100275 if (quic_stream_is_local(qcc, id)) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100276 /* Local streams: this stream must be already opened. */
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200277 node = eb64_lookup(&qcc->streams_by_id, id);
278 if (!node) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100279 /* unknown stream id */
280 goto out;
281 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200282 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100283 }
284 else {
285 /* Remote streams. */
286 struct eb_root *strms;
287 uint64_t largest_id;
288 enum qcs_type qcs_type;
289
290 strms = &qcc->streams_by_id;
291 qcs_type = qcs_id_type(id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100292
293 /* TODO also checks max-streams for uni streams */
294 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100295 if (sub_id + 1 > qcc->lfctl.ms_bidi) {
Amaury Denoyellec055e302022-02-07 16:09:06 +0100296 /* streams limit reached */
297 goto out;
298 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100299 }
300
301 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
302 * correct value.
303 */
304 largest_id = qcc->strms[qcs_type].largest_id;
305 if (sub_id > (int64_t)largest_id) {
306 /* RFC: "A stream ID that is used out of order results in all streams
307 * of that type with lower-numbered stream IDs also being opened".
308 * So, let's "open" these streams.
309 */
310 int64_t i;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200311 struct qcs *tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100312
Amaury Denoyelle50742292022-03-29 14:57:19 +0200313 tmp_qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100314 for (i = largest_id + 1; i <= sub_id; i++) {
315 uint64_t id = (i << QCS_ID_TYPE_SHIFT) | strm_type;
316 enum qcs_type type = id & QCS_ID_DIR_BIT ? QCS_CLT_UNI : QCS_CLT_BIDI;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200317
Amaury Denoyelle50742292022-03-29 14:57:19 +0200318 tmp_qcs = qcs_new(qcc, id, type);
319 if (!tmp_qcs) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100320 /* allocation failure */
321 goto out;
322 }
323
324 qcc->strms[qcs_type].largest_id = i;
325 }
Amaury Denoyelle50742292022-03-29 14:57:19 +0200326 if (tmp_qcs)
327 qcs = tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100328 }
329 else {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200330 node = eb64_lookup(strms, id);
331 if (node)
332 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100333 }
334 }
335
Amaury Denoyelle50742292022-03-29 14:57:19 +0200336 return qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100337
338 out:
339 return NULL;
340}
341
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100342/* Handle a new STREAM frame <strm_frm>. The frame content will be copied in
343 * the buffer of the stream instance. The stream instance will be stored in
344 * <out_qcs>. In case of success, the caller can immediatly call qcc_decode_qcs
345 * to process the frame content.
346 *
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +0200347 * Returns a code indicating how the frame was handled.
Amaury Denoyelle74cf2372022-04-29 15:58:22 +0200348 * - 0: frame received completely and can be dropped.
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +0200349 * - 1: frame not received but can be dropped.
350 * - 2: frame cannot be handled, either partially or not at all. <done>
351 * indicated the number of bytes handled. The rest should be buffered.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100352 */
353int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +0200354 char fin, char *data, struct qcs **out_qcs, size_t *done)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100355{
356 struct qcs *qcs;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100357 size_t total, diff;
358
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100359 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
360
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +0200361 *out_qcs = NULL;
362 *done = 0;
363
Amaury Denoyelle50742292022-03-29 14:57:19 +0200364 qcs = qcc_get_qcs(qcc, id);
365 if (!qcs) {
Frédéric Lécailleb57de072022-05-02 18:58:27 +0200366 if ((id >> QCS_ID_TYPE_SHIFT) <= qcc->strms[qcs_id_type(id)].largest_id) {
367 TRACE_DEVEL("already released stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
368 return 0;
369 }
370 else {
371 TRACE_DEVEL("leaving on stream not found", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
372 return 1;
373 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100374 }
375
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100376 *out_qcs = qcs;
377
378 if (offset > qcs->rx.offset)
379 return 2;
380
381 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100382 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 +0200383 return 0;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100384 }
385
386 /* Last frame already handled for this stream. */
387 BUG_ON(qcs->flags & QC_SF_FIN_RECV);
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200388 /* TODO initial max-stream-data overflow. Implement FLOW_CONTROL_ERROR emission. */
389 BUG_ON(offset + len > qcs->rx.msd);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100390
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +0200391 if (!qc_get_buf(qcs, &qcs->rx.buf) || b_full(&qcs->rx.buf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100392 /* TODO should mark qcs as full */
393 return 2;
394 }
395
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100396 TRACE_DEVEL("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100397 diff = qcs->rx.offset - offset;
398
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100399 len -= diff;
400 data += diff;
401
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +0200402 /* TODO handle STREAM frames larger than RX buffer. */
403 BUG_ON(len > b_size(&qcs->rx.buf));
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100404
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +0200405 total = b_putblk(&qcs->rx.buf, data, len);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100406 qcs->rx.offset += total;
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +0200407 *done = total;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100408
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200409 /* TODO initial max-stream-data reached. Implement MAX_STREAM_DATA emission. */
410 BUG_ON(qcs->rx.offset == qcs->rx.msd);
411
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +0200412 if (total < len) {
413 TRACE_DEVEL("leaving on partially received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
414 return 2;
415 }
416
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100417 if (fin)
418 qcs->flags |= QC_SF_FIN_RECV;
419
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100420 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100421 return 0;
422}
423
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100424/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
425 * the frame.
426 *
427 * Returns 0 on success else non-zero.
428 */
429int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
430{
431 if (qcc->rfctl.md < max) {
432 qcc->rfctl.md = max;
433
434 if (qcc->flags & QC_CF_BLK_MFCTL) {
435 qcc->flags &= ~QC_CF_BLK_MFCTL;
436 tasklet_wakeup(qcc->wait_event.tasklet);
437 }
438 }
439 return 0;
440}
441
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100442/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
443 * field of the frame and <id> is the identifier of the QUIC stream.
444 *
445 * Returns 0 on success else non-zero.
446 */
447int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
448{
449 struct qcs *qcs;
450 struct eb64_node *node;
451
452 node = eb64_lookup(&qcc->streams_by_id, id);
453 if (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200454 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100455 if (max > qcs->tx.msd) {
456 qcs->tx.msd = max;
457
458 if (qcs->flags & QC_SF_BLK_SFCTL) {
459 qcs->flags &= ~QC_SF_BLK_SFCTL;
460 tasklet_wakeup(qcc->wait_event.tasklet);
461 }
462 }
463 }
464
465 return 0;
466}
467
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100468/* Decode the content of STREAM frames already received on the stream instance
469 * <qcs>.
470 *
471 * Returns 0 on success else non-zero.
472 */
473int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
474{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100475 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
476
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100477 if (qcc->app_ops->decode_qcs(qcs, qcs->flags & QC_SF_FIN_RECV, qcc->ctx) < 0) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100478 TRACE_DEVEL("leaving on decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100479 return 1;
480 }
481
Amaury Denoyelleb7104152022-04-22 17:38:43 +0200482 qcs_notify_recv(qcs);
483
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100484 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
485
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100486 return 0;
487}
488
Amaury Denoyellec055e302022-02-07 16:09:06 +0100489static int qc_is_max_streams_needed(struct qcc *qcc)
490{
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100491 return qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100492}
493
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500494/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100495static void qcs_destroy(struct qcs *qcs)
496{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100497 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200498 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100499
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100500 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100501
Amaury Denoyellec055e302022-02-07 16:09:06 +0100502 if (quic_stream_is_remote(qcs->qcc, id)) {
503 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100504 ++qcs->qcc->lfctl.cl_bidi_r;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100505 if (qc_is_max_streams_needed(qcs->qcc))
506 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
507 }
508 }
509
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200510 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100511
512 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100513}
514
515static inline int qcc_is_dead(const struct qcc *qcc)
516{
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200517 if (qcc->app_ops && qcc->app_ops->is_active &&
518 qcc->app_ops->is_active(qcc, qcc->ctx))
519 return 0;
520
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200521 if ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task)
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100522 return 1;
523
524 return 0;
525}
526
527/* Return true if the mux timeout should be armed. */
528static inline int qcc_may_expire(struct qcc *qcc)
529{
Amaury Denoyelle06890aa2022-04-04 16:15:06 +0200530 return !qcc->nb_cs;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100531}
532
533/* release function. This one should be called to free all resources allocated
534 * to the mux.
535 */
536static void qc_release(struct qcc *qcc)
537{
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200538 struct connection *conn = qcc->conn;
539 struct eb64_node *node;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100540
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100541 TRACE_ENTER(QMUX_EV_QCC_END);
542
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200543 if (qcc->app_ops && qcc->app_ops->release)
544 qcc->app_ops->release(qcc->ctx);
Amaury Denoyellef8909452022-03-30 11:51:56 +0200545
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200546 if (qcc->task) {
547 task_destroy(qcc->task);
548 qcc->task = NULL;
549 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100550
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200551 if (qcc->wait_event.tasklet)
552 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyellef3e03a42022-04-21 15:41:34 +0200553 if (conn && qcc->wait_event.events) {
554 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
555 qcc->wait_event.events,
556 &qcc->wait_event);
557 }
Amaury Denoyellef8909452022-03-30 11:51:56 +0200558
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200559 /* liberate remaining qcs instances */
560 node = eb64_first(&qcc->streams_by_id);
561 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200562 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200563 node = eb64_next(node);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200564 qcs_free(qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100565 }
566
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200567 pool_free(pool_head_qcc, qcc);
568
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100569 if (conn) {
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100570 LIST_DEL_INIT(&conn->stopping_list);
571
Willy Tarreau784b8682022-04-11 14:18:10 +0200572 conn->handle.qc->conn = NULL;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100573 conn->mux = NULL;
574 conn->ctx = NULL;
575
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100576 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
577
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100578 conn_stop_tracking(conn);
579 conn_full_close(conn);
580 if (conn->destroy_cb)
581 conn->destroy_cb(conn);
582 conn_free(conn);
583 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100584
585 TRACE_LEAVE(QMUX_EV_QCC_END);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100586}
587
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200588/* Transfer as much as possible data on <qcs> from <in> to <out>. <max_data> is
589 * the current flow-control limit on the connection which must not be exceeded.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100590 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200591 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100592 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200593static int qcs_xfer_data(struct qcs *qcs, struct buffer *out,
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200594 struct buffer *in, uint64_t max_data)
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200595{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100596 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200597 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100598 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200599
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100600 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100601
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100602 qc_get_buf(qcs, out);
603
604 /*
605 * QCS out buffer diagram
606 * head left to_xfer
607 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100608 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100609 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100610 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100611 * ^ ack-off ^ sent-off ^ off
612 *
613 * STREAM frame
614 * ^ ^
615 * |xxxxxxxxxxxxxxxxx|
616 */
617
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200618 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100619 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
620
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100621 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200622 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100623
624 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
625 /* do not exceed flow control limit */
626 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
627 to_xfer = qcs->tx.msd - qcs->tx.offset;
628
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100629 BUG_ON_HOT(max_data > qcc->rfctl.md);
630 /* do not overcome flow control limit on connection */
631 if (max_data + to_xfer > qcc->rfctl.md)
632 to_xfer = qcc->rfctl.md - max_data;
633
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100634 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200635 goto out;
636
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200637 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200638
639 out:
640 {
641 struct qcs_xfer_data_trace_arg arg = {
642 .prep = b_data(out), .xfer = total,
643 };
644 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
645 qcc->conn, qcs, &arg);
646 }
647
648 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200649}
650
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200651/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
652 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
653 * stream frame.
654 *
655 * Returns the length of the STREAM frame or a negative error code.
656 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200657static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
658 struct list *frm_list)
659{
660 struct qcc *qcc = qcs->qcc;
661 struct quic_frame *frm;
662 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200663 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200664
665 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
666
Amaury Denoyellea4569202022-04-15 17:29:25 +0200667 /* if ack_offset < buf_offset, it points to an older buffer. */
668 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
669 BUG_ON(qcs->tx.sent_offset < base_off);
670
671 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200672 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200673 BUG_ON(total < 0);
674
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200675 if (!total) {
676 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
677 return 0;
678 }
Amaury Denoyellea4569202022-04-15 17:29:25 +0200679 BUG_ON(qcs->tx.sent_offset >= qcs->tx.offset);
680 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200681
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200682 frm = pool_zalloc(pool_head_quic_frame);
683 if (!frm)
684 goto err;
685
Frédéric Lécailleb9171912022-04-21 17:32:10 +0200686 LIST_INIT(&frm->reflist);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100687 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200688 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200689 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100690 frm->stream.buf = out;
691 frm->stream.data = (unsigned char *)b_peek(out, head);
692
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100693 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200694 if (fin)
695 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100696
697 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200698 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100699 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200700 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100701
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200702 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
703 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200704
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100705 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100706
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200707 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100708 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200709 struct qcs_build_stream_trace_arg arg = {
710 .len = frm->stream.len, .fin = fin,
711 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100712 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200713 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100714 qcc->conn, qcs, &arg);
715 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100716
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200717 return total;
718
719 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100720 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200721 return -1;
722}
723
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100724/* This function must be called by the upper layer to inform about the sending
725 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
726 * <offset>.
727 */
728void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
729{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100730 struct qcc *qcc = qcs->qcc;
731 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100732
733 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200734 BUG_ON(offset >= qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100735
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100736 /* check if the STREAM frame has already been notified. It can happen
737 * for retransmission.
738 */
739 if (offset + data <= qcs->tx.sent_offset)
740 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100741
742 diff = offset + data - qcs->tx.sent_offset;
743
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100744 /* increase offset sum on connection */
745 qcc->tx.sent_offsets += diff;
746 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
747 if (qcc->tx.sent_offsets == qcc->rfctl.md)
748 qcc->flags |= QC_CF_BLK_MFCTL;
749
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100750 /* increase offset on stream */
751 qcs->tx.sent_offset += diff;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100752 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200753 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100754 if (qcs->tx.sent_offset == qcs->tx.msd)
755 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200756
757 if (qcs->tx.offset == qcs->tx.sent_offset && b_full(&qcs->stream->buf->buf)) {
758 qc_stream_buf_release(qcs->stream);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200759 /* prepare qcs for immediate send retry if data to send */
760 if (b_data(&qcs->tx.buf))
761 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200762 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100763}
764
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100765/* Wrapper for send on transport layer. Send a list of frames <frms> for the
766 * connection <qcc>.
767 *
768 * Returns 0 if all data sent with success else non-zero.
769 */
770static int qc_send_frames(struct qcc *qcc, struct list *frms)
771{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100772 /* TODO implement an opportunistic retry mechanism. This is needed
773 * because qc_send_app_pkts is not completed. It will only prepare data
774 * up to its Tx buffer. The frames left are not send even if the Tx
775 * buffer is emptied by the sendto call.
776 *
777 * To overcome this, we call repeatedly qc_send_app_pkts until we
778 * detect that the transport layer has send nothing. This could happen
779 * on congestion or sendto syscall error.
780 *
781 * When qc_send_app_pkts is improved to handle retry by itself, we can
782 * remove the looping from the MUX.
783 */
784 struct quic_frame *first_frm;
785 uint64_t first_offset = 0;
786 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100787
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100788 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
789
790 if (LIST_ISEMPTY(frms)) {
791 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200792 return 1;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100793 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100794
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200795 LIST_INIT(&qcc->send_retry_list);
796
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100797 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100798 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
799 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
800 first_offset = first_frm->stream.offset.key;
801 first_stream_frame_type = 1;
802 }
803 else {
804 first_stream_frame_type = 0;
805 }
806
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100807 if (!LIST_ISEMPTY(frms))
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +0200808 qc_send_app_pkts(qcc->conn->handle.qc, 0, frms);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100809
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100810 /* If there is frames left, check if the transport layer has send some
811 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100812 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100813 if (!LIST_ISEMPTY(frms)) {
814 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
815 goto retry_send;
816
817 /* If the first frame is STREAM, check if its offset has
818 * changed.
819 */
820 if (first_stream_frame_type &&
821 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
822 goto retry_send;
823 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100824 }
825
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100826 /* If there is frames left at this stage, transport layer is blocked.
827 * Subscribe on it to retry later.
828 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100829 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100830 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100831 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
832 SUB_RETRY_SEND, &qcc->wait_event);
833 return 1;
834 }
835
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100836 TRACE_LEAVE(QMUX_EV_QCC_SEND);
837
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100838 return 0;
839}
840
Amaury Denoyellec9337802022-04-04 16:36:34 +0200841/* Send a MAX_STREAM_BIDI frame to update the limit of bidirectional streams
842 * allowed to be opened by the peer. The caller should have first checked if
843 * this is required with qc_is_max_streams_needed.
844 *
845 * Returns 0 on success else non-zero.
846 */
847static int qc_send_max_streams(struct qcc *qcc)
848{
849 struct list frms = LIST_HEAD_INIT(frms);
850 struct quic_frame *frm;
851
852 frm = pool_zalloc(pool_head_quic_frame);
853 BUG_ON(!frm); /* TODO handle this properly */
854
Frédéric Lécailleb9171912022-04-21 17:32:10 +0200855 LIST_INIT(&frm->reflist);
Amaury Denoyellec9337802022-04-04 16:36:34 +0200856 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
857 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
858 qcc->lfctl.cl_bidi_r;
859 TRACE_DEVEL("sending MAX_STREAMS frame", QMUX_EV_SEND_FRM, qcc->conn, NULL, frm);
860 LIST_APPEND(&frms, &frm->list);
861
862 if (qc_send_frames(qcc, &frms))
863 return 1;
864
865 /* save the new limit if the frame has been send. */
866 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
867 qcc->lfctl.cl_bidi_r = 0;
868
869 return 0;
870}
871
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200872/* Used internally by qc_send function. Proceed to send for <qcs>. This will
873 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
874 * is then generated and inserted in <frms> list. <qcc_max_data> is the current
875 * flow-control max-data at the connection level which must not be surpassed.
876 *
877 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
878 * be null if out buffer cannot be allocated.
879 */
880static int _qc_send_qcs(struct qcs *qcs, struct list *frms,
881 uint64_t qcc_max_data)
882{
883 struct qcc *qcc = qcs->qcc;
884 struct buffer *buf = &qcs->tx.buf;
885 struct buffer *out = qc_stream_buf_get(qcs->stream);
886 int xfer = 0;
887
888 /* Allocate <out> buffer if necessary. */
889 if (!out) {
890 if (qcc->flags & QC_CF_CONN_FULL)
891 return 0;
892
893 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
894 if (!out) {
895 qcc->flags |= QC_CF_CONN_FULL;
896 return 0;
897 }
898 }
899
900 /* Transfer data from <buf> to <out>. */
901 if (b_data(buf)) {
902 xfer = qcs_xfer_data(qcs, out, buf, qcc_max_data);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200903 if (xfer > 0) {
904 qcs_notify_send(qcs);
905 qcs->flags &= ~QC_SF_BLK_MROOM;
906 }
907
908 qcs->tx.offset += xfer;
909 }
910
911 /* out buffer cannot be emptied if qcs offsets differ. */
912 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
913
914 /* Build a new STREAM frame with <out> buffer. */
915 if (qcs->tx.sent_offset != qcs->tx.offset) {
916 int ret;
917 char fin = !!(qcs->flags & QC_SF_FIN_STREAM);
918
919 /* FIN is set if all incoming data were transfered. */
920 fin = !!(fin && !b_data(buf));
921
922 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +0200923 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200924 }
925
926 return xfer;
927}
928
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100929/* Proceed to sending. Loop through all available streams for the <qcc>
930 * instance and try to send as much as possible.
931 *
932 * Returns the total of bytes sent to the transport layer.
933 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100934static int qc_send(struct qcc *qcc)
935{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100936 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200937 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200938 struct qcs *qcs, *qcs_tmp;
939 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200940
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100941 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200942
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200943 if (qcc->conn->flags & CO_FL_SOCK_WR_SH) {
944 qcc->conn->flags |= CO_FL_ERROR;
945 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
946 return 0;
947 }
948
Amaury Denoyellec9337802022-04-04 16:36:34 +0200949 if (qc_is_max_streams_needed(qcc))
950 qc_send_max_streams(qcc);
951
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100952 if (qcc->flags & QC_CF_BLK_MFCTL)
953 return 0;
954
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100955 /* loop through all streams, construct STREAM frames if data available.
956 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200957 */
958 node = eb64_first(&qcc->streams_by_id);
959 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200960 int ret;
961 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100962
Amaury Denoyellee2ec9422022-03-10 16:46:18 +0100963 /* TODO
964 * for the moment, unidirectional streams have their own
965 * mechanism for sending. This should be unified in the future,
966 * in this case the next check will be removed.
967 */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200968 if (quic_stream_is_uni(qcs->id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +0100969 node = eb64_next(node);
970 continue;
971 }
972
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100973 if (qcs->flags & QC_SF_BLK_SFCTL) {
974 node = eb64_next(node);
975 continue;
976 }
977
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200978 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +0200979 node = eb64_next(node);
980 continue;
981 }
Amaury Denoyellea4569202022-04-15 17:29:25 +0200982
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200983 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + total);
984 total += ret;
985 node = eb64_next(node);
986 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200987
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200988 if (qc_send_frames(qcc, &frms)) {
989 /* data rejected by transport layer, do not retry. */
990 goto out;
991 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200992
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200993 retry:
994 tmp_total = 0;
995 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
996 int ret;
997 BUG_ON(!b_data(&qcs->tx.buf));
998 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200999
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001000 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + tmp_total);
1001 tmp_total += ret;
1002 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001003 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001004
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001005 total += tmp_total;
1006 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
1007 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +01001008
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001009 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001010 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1011
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001012 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001013}
1014
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001015/* Release all streams that are already marked as detached. This is only done
1016 * if their TX buffers are empty or if a CONNECTION_CLOSE has been received.
1017 *
1018 * Return the number of released stream.
1019 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001020static int qc_release_detached_streams(struct qcc *qcc)
1021{
1022 struct eb64_node *node;
1023 int release = 0;
1024
1025 node = eb64_first(&qcc->streams_by_id);
1026 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001027 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001028 node = eb64_next(node);
1029
1030 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001031 if (!b_data(&qcs->tx.buf) &&
1032 qcs->tx.offset == qcs->tx.sent_offset) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001033 qcs_destroy(qcs);
1034 release = 1;
1035 }
1036 else {
1037 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1038 SUB_RETRY_SEND, &qcc->wait_event);
1039 }
1040 }
1041 }
1042
1043 return release;
1044}
1045
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001046static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1047{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001048 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001049
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001050 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001051
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001052 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001053
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001054 if (qc_release_detached_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001055 if (qcc_is_dead(qcc)) {
1056 qc_release(qcc);
1057 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001058 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001059 if (qcc_may_expire(qcc))
1060 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1061 else
1062 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001063 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001064 }
1065 }
1066
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001067 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1068
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001069 return NULL;
1070}
1071
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001072static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1073{
1074 struct qcc *qcc = ctx;
1075 int expired = tick_is_expired(t->expire, now_ms);
1076
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001077 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001078
1079 if (qcc) {
1080 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001081 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001082 return t;
1083 }
1084
1085 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001086 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001087 t->expire = TICK_ETERNITY;
1088 return t;
1089 }
1090 }
1091
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001092 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001093
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001094 if (!qcc) {
1095 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001096 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001097 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001098
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001099 qcc->task = NULL;
1100
1101 if (qcc_is_dead(qcc))
1102 qc_release(qcc);
1103
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001104 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1105
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001106 return NULL;
1107}
1108
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001109static int qc_init(struct connection *conn, struct proxy *prx,
1110 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001111{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001112 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001113 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001114
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001115 qcc = pool_alloc(pool_head_qcc);
1116 if (!qcc)
1117 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001118
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001119 qcc->conn = conn;
1120 conn->ctx = qcc;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001121 qcc->nb_cs = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001122 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001123
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001124 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001125
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001126 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001127
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001128 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001129 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001130
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001131 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001132 qcc->tx.sent_offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001133
1134 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001135 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001136 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
1137 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001138 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001139 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001140
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001141 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001142 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
1143 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001144 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001145 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001146
1147 /* Server initiated streams must respect the server flow control. */
1148 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001149 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
1150 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001151 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001152 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1153
1154 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001155 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
1156 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001157 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001158 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001159
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001160 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02001161 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
1162 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001163 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001164
Willy Tarreau784b8682022-04-11 14:18:10 +02001165 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001166 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001167 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1168 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1169
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001170 qcc->wait_event.tasklet = tasklet_new();
1171 if (!qcc->wait_event.tasklet)
1172 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001173
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001174 LIST_INIT(&qcc->send_retry_list);
1175
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001176 qcc->subs = NULL;
1177 qcc->wait_event.tasklet->process = qc_io_cb;
1178 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001179 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001180
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001181 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001182 qcc->task = NULL;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001183 qcc->timeout = prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001184 if (tick_isset(qcc->timeout)) {
1185 qcc->task = task_new_here();
1186 if (!qcc->task)
1187 goto fail_no_timeout_task;
1188 qcc->task->process = qc_timeout_task;
1189 qcc->task->context = qcc;
1190 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1191 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001192
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001193 if (!conn_is_back(conn)) {
1194 if (!LIST_INLIST(&conn->stopping_list)) {
1195 LIST_APPEND(&mux_stopping_data[tid].list,
1196 &conn->stopping_list);
1197 }
1198 }
1199
Willy Tarreau784b8682022-04-11 14:18:10 +02001200 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001201 /* init read cycle */
1202 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001203
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001204 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001205
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001206 fail_no_timeout_task:
1207 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001208 fail_no_tasklet:
1209 pool_free(pool_head_qcc, qcc);
1210 fail_no_qcc:
1211 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001212}
1213
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001214static void qc_destroy(void *ctx)
1215{
1216 struct qcc *qcc = ctx;
1217
1218 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
1219 qc_release(qcc);
1220 TRACE_LEAVE(QMUX_EV_QCC_END);
1221}
1222
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001223static void qc_detach(struct conn_stream *cs)
1224{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001225 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001226 struct qcc *qcc = qcs->qcc;
1227
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001228 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001229
Amaury Denoyelle80388212022-04-08 12:00:12 +02001230 qcs->cs = NULL;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001231 --qcc->nb_cs;
1232
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001233 if ((b_data(&qcs->tx.buf) || qcs->tx.offset > qcs->tx.sent_offset) &&
1234 !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001235 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001236 qcs->flags |= QC_SF_DETACH;
1237 return;
1238 }
1239
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001240 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001241
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001242 if (qcc_is_dead(qcc)) {
1243 qc_release(qcc);
1244 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001245 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001246 if (qcc_may_expire(qcc))
1247 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1248 else
1249 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001250 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001251 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001252
1253 TRACE_LEAVE(QMUX_EV_STRM_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001254}
1255
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001256/* Called from the upper layer, to receive data */
1257static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf,
1258 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001259{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001260 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001261 struct htx *qcs_htx = NULL;
1262 struct htx *cs_htx = NULL;
1263 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001264 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001265
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001266 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001267
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001268 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
1269 if (htx_is_empty(qcs_htx)) {
1270 /* Set buffer data to 0 as HTX is empty. */
1271 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1272 goto end;
1273 }
1274
1275 ret = qcs_htx->data;
1276
1277 cs_htx = htx_from_buf(buf);
1278 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
1279 htx_to_buf(cs_htx, buf);
1280 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1281 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
1282 goto end;
1283 }
1284
1285 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
1286 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
1287
1288 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001289 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
1290 cs_htx->flags |= HTX_FL_EOM;
1291 fin = 1;
1292 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001293
1294 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
1295 htx_to_buf(cs_htx, buf);
1296 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1297 ret -= qcs_htx->data;
1298
1299 end:
1300 if (b_data(&qcs->rx.app_buf)) {
Christopher Fauletb041b232022-03-24 10:27:02 +01001301 cs->endp->flags |= (CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001302 }
1303 else {
Christopher Fauletb041b232022-03-24 10:27:02 +01001304 cs->endp->flags &= ~(CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
1305 if (cs->endp->flags & CS_EP_ERR_PENDING)
1306 cs->endp->flags |= CS_EP_ERROR;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001307
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001308 if (fin)
Amaury Denoyellef6df6b42022-04-22 16:52:14 +02001309 cs->endp->flags |= CS_EP_EOI;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001310
1311 if (b_size(&qcs->rx.app_buf)) {
1312 b_free(&qcs->rx.app_buf);
1313 offer_buffers(NULL, 1);
1314 }
1315 }
1316
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001317 /* TODO QUIC MUX iocb does not treat RX : following wake-up is thus
1318 * useless for the moment. This may causes freezing transfer on POST.
1319 */
1320 if (ret) {
1321 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001322 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001323 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001324
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001325 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
1326
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001327 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001328}
1329
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001330static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf,
1331 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001332{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001333 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001334 size_t ret;
1335
1336 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001337
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001338 ret = qcs->qcc->app_ops->snd_buf(cs, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001339
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001340 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
1341
1342 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001343}
1344
1345/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1346 * event subscriber <es> is not allowed to change from a previous call as long
1347 * as at least one event is still subscribed. The <event_type> must only be a
1348 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1349 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001350static int qc_subscribe(struct conn_stream *cs, int event_type,
1351 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001352{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001353 return qcs_subscribe(__cs_mux(cs), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001354}
1355
1356/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1357 * The <es> pointer is not allowed to differ from the one passed to the
1358 * subscribe() call. It always returns zero.
1359 */
1360static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
1361{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001362 struct qcs *qcs = __cs_mux(cs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001363
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001364 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1365 BUG_ON(qcs->subs && qcs->subs != es);
1366
1367 es->events &= ~event_type;
1368 if (!es->events)
1369 qcs->subs = NULL;
1370
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001371 return 0;
1372}
1373
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001374/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Christopher Fauletb041b232022-03-24 10:27:02 +01001375 * report CS_EP_ERR_PENDING|CS_EP_ERROR on the attached conn-streams and wake
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001376 * them.
1377 */
1378static int qc_wake_some_streams(struct qcc *qcc)
1379{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001380 struct qcs *qcs;
1381 struct eb64_node *node;
1382
1383 for (node = eb64_first(&qcc->streams_by_id); node;
1384 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001385 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001386
1387 if (!qcs->cs)
1388 continue;
1389
1390 if (qcc->conn->flags & CO_FL_ERROR) {
Christopher Fauletb041b232022-03-24 10:27:02 +01001391 qcs->endp->flags |= CS_EP_ERR_PENDING;
1392 if (qcs->endp->flags & CS_EP_EOS)
1393 qcs->endp->flags |= CS_EP_ERROR;
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001394
1395 if (qcs->subs) {
1396 qcs_notify_recv(qcs);
1397 qcs_notify_send(qcs);
1398 }
1399 else if (qcs->cs->data_cb->wake) {
1400 qcs->cs->data_cb->wake(qcs->cs);
1401 }
1402 }
1403 }
1404
1405 return 0;
1406}
1407
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001408static int qc_wake(struct connection *conn)
1409{
1410 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02001411 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001412
1413 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001414
1415 /* Check if a soft-stop is in progress.
1416 * Release idling front connection if this is the case.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001417 *
1418 * TODO this is revelant for frontend connections only.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001419 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001420 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
1421 goto release;
1422
Willy Tarreau784b8682022-04-11 14:18:10 +02001423 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02001424 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
1425
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001426 qc_send(qcc);
1427
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001428 qc_wake_some_streams(qcc);
1429
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001430 if (qcc_is_dead(qcc))
1431 goto release;
1432
1433 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
1434
1435 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001436
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001437 release:
1438 qc_release(qcc);
1439 TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001440 return 1;
1441}
1442
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001443
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001444static void qmux_trace_frm(const struct quic_frame *frm)
1445{
1446 switch (frm->type) {
1447 case QUIC_FT_MAX_STREAMS_BIDI:
1448 chunk_appendf(&trace_buf, " max_streams=%lu",
1449 frm->max_streams_bidi.max_streams);
1450 break;
1451
1452 case QUIC_FT_MAX_STREAMS_UNI:
1453 chunk_appendf(&trace_buf, " max_streams=%lu",
1454 frm->max_streams_uni.max_streams);
1455 break;
1456
1457 default:
1458 break;
1459 }
1460}
1461
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001462/* quic-mux trace handler */
1463static void qmux_trace(enum trace_level level, uint64_t mask,
1464 const struct trace_source *src,
1465 const struct ist where, const struct ist func,
1466 const void *a1, const void *a2, const void *a3, const void *a4)
1467{
1468 const struct connection *conn = a1;
1469 const struct qcc *qcc = conn ? conn->ctx : NULL;
1470 const struct qcs *qcs = a2;
1471
1472 if (!qcc)
1473 return;
1474
1475 if (src->verbosity > QMUX_VERB_CLEAN) {
1476 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
1477
1478 if (qcs)
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001479 chunk_appendf(&trace_buf, " qcs=%p(%lu)", qcs, qcs->id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001480
1481 if (mask & QMUX_EV_QCC_NQCS) {
1482 const uint64_t *id = a3;
1483 chunk_appendf(&trace_buf, " id=%lu", *id);
1484 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001485
1486 if (mask & QMUX_EV_SEND_FRM)
1487 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001488
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001489 if (mask & QMUX_EV_QCS_XFER_DATA) {
1490 const struct qcs_xfer_data_trace_arg *arg = a3;
1491 chunk_appendf(&trace_buf, " prep=%lu xfer=%d",
1492 arg->prep, arg->xfer);
1493 }
1494
1495 if (mask & QMUX_EV_QCS_BUILD_STRM) {
1496 const struct qcs_build_stream_trace_arg *arg = a3;
1497 chunk_appendf(&trace_buf, " len=%lu fin=%d offset=%lu",
1498 arg->len, arg->fin, arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001499 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001500 }
1501}
1502
Amaury Denoyelle251eadf2022-03-24 17:14:52 +01001503/* Function to automatically activate QUIC MUX traces on stdout.
1504 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
1505 * Main use for now is in the docker image for QUIC interop testing.
1506 */
1507static void qmux_init_stdout_traces(void)
1508{
1509#ifdef ENABLE_QUIC_STDOUT_TRACES
1510 trace_qmux.sink = sink_find("stdout");
1511 trace_qmux.level = TRACE_LEVEL_DEVELOPER;
1512 trace_qmux.state = TRACE_STATE_RUNNING;
1513 trace_qmux.verbosity = QMUX_VERB_MINIMAL;
1514#endif
1515}
1516INITCALL0(STG_INIT, qmux_init_stdout_traces);
1517
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001518
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001519static const struct mux_ops qc_ops = {
1520 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001521 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001522 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001523 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001524 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001525 .subscribe = qc_subscribe,
1526 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001527 .wake = qc_wake,
Christopher Fauleta97cced2022-04-12 18:04:10 +02001528 .flags = MX_FL_HTX|MX_FL_NO_UPG,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02001529 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001530};
1531
1532static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001533 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001534
1535INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);