blob: bd671af9de99479f795f18adc573e50c97b64f19 [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 Denoyelle0e3010b2022-02-28 11:37:48 +0100367/* Handle a new STREAM frame <strm_frm>. The frame content will be copied in
368 * the buffer of the stream instance. The stream instance will be stored in
369 * <out_qcs>. In case of success, the caller can immediatly call qcc_decode_qcs
370 * to process the frame content.
371 *
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200372 * Returns 0 on success else non-zero.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100373 */
374int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200375 char fin, char *data, struct qcs **out_qcs)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100376{
377 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200378 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100379
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100380 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
381
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +0200382 *out_qcs = NULL;
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +0200383
Amaury Denoyelle50742292022-03-29 14:57:19 +0200384 qcs = qcc_get_qcs(qcc, id);
385 if (!qcs) {
Frédéric Lécailleb57de072022-05-02 18:58:27 +0200386 if ((id >> QCS_ID_TYPE_SHIFT) <= qcc->strms[qcs_id_type(id)].largest_id) {
387 TRACE_DEVEL("already released stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
388 return 0;
389 }
390 else {
391 TRACE_DEVEL("leaving on stream not found", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
392 return 1;
393 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100394 }
395
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100396 *out_qcs = qcs;
397
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100398 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100399 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 +0200400 return 0;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100401 }
402
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200403 /* TODO if last frame already received, stream size must not change.
404 * Else send FINAL_SIZE_ERROR.
405 */
406
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200407 /* TODO initial max-stream-data overflow. Implement FLOW_CONTROL_ERROR emission. */
408 BUG_ON(offset + len > qcs->rx.msd);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100409
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200410 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100411 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200412 ABORT_NOW();
413 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100414 }
415
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100416 TRACE_DEVEL("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200417 if (offset < qcs->rx.offset) {
418 len -= qcs->rx.offset - offset;
419 offset = qcs->rx.offset;
420 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100421
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200422 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
423 if (ret != NCB_RET_OK) {
424 if (ret == NCB_RET_DATA_REJ) {
425 /* TODO generate PROTOCOL_VIOLATION error */
426 TRACE_DEVEL("leaving on data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
427 qcc->conn, qcs);
428 }
429 else if (ret == NCB_RET_GAP_SIZE) {
430 TRACE_DEVEL("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
431 qcc->conn, qcs);
432 }
433 return 1;
434 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100435
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200436 /* TODO initial max-stream-data reached. Implement MAX_STREAM_DATA emission. */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200437 BUG_ON(offset + len == qcs->rx.msd);
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +0200438
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100439 if (fin)
440 qcs->flags |= QC_SF_FIN_RECV;
441
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100442 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100443 return 0;
444}
445
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100446/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
447 * the frame.
448 *
449 * Returns 0 on success else non-zero.
450 */
451int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
452{
453 if (qcc->rfctl.md < max) {
454 qcc->rfctl.md = max;
455
456 if (qcc->flags & QC_CF_BLK_MFCTL) {
457 qcc->flags &= ~QC_CF_BLK_MFCTL;
458 tasklet_wakeup(qcc->wait_event.tasklet);
459 }
460 }
461 return 0;
462}
463
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100464/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
465 * field of the frame and <id> is the identifier of the QUIC stream.
466 *
467 * Returns 0 on success else non-zero.
468 */
469int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
470{
471 struct qcs *qcs;
472 struct eb64_node *node;
473
474 node = eb64_lookup(&qcc->streams_by_id, id);
475 if (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200476 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100477 if (max > qcs->tx.msd) {
478 qcs->tx.msd = max;
479
480 if (qcs->flags & QC_SF_BLK_SFCTL) {
481 qcs->flags &= ~QC_SF_BLK_SFCTL;
482 tasklet_wakeup(qcc->wait_event.tasklet);
483 }
484 }
485 }
486
487 return 0;
488}
489
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100490/* Decode the content of STREAM frames already received on the stream instance
491 * <qcs>.
492 *
493 * Returns 0 on success else non-zero.
494 */
495int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
496{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100497 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
498
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100499 if (qcc->app_ops->decode_qcs(qcs, qcs->flags & QC_SF_FIN_RECV, qcc->ctx) < 0) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100500 TRACE_DEVEL("leaving on decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100501 return 1;
502 }
503
Amaury Denoyelleb7104152022-04-22 17:38:43 +0200504 qcs_notify_recv(qcs);
505
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100506 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
507
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100508 return 0;
509}
510
Amaury Denoyellec055e302022-02-07 16:09:06 +0100511static int qc_is_max_streams_needed(struct qcc *qcc)
512{
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100513 return qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100514}
515
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500516/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100517static void qcs_destroy(struct qcs *qcs)
518{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100519 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200520 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100521
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100522 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100523
Amaury Denoyellec055e302022-02-07 16:09:06 +0100524 if (quic_stream_is_remote(qcs->qcc, id)) {
525 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100526 ++qcs->qcc->lfctl.cl_bidi_r;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100527 if (qc_is_max_streams_needed(qcs->qcc))
528 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
529 }
530 }
531
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200532 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100533
534 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100535}
536
537static inline int qcc_is_dead(const struct qcc *qcc)
538{
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200539 if (qcc->app_ops && qcc->app_ops->is_active &&
540 qcc->app_ops->is_active(qcc, qcc->ctx))
541 return 0;
542
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200543 if ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task)
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100544 return 1;
545
546 return 0;
547}
548
549/* Return true if the mux timeout should be armed. */
550static inline int qcc_may_expire(struct qcc *qcc)
551{
Amaury Denoyelle06890aa2022-04-04 16:15:06 +0200552 return !qcc->nb_cs;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100553}
554
555/* release function. This one should be called to free all resources allocated
556 * to the mux.
557 */
558static void qc_release(struct qcc *qcc)
559{
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200560 struct connection *conn = qcc->conn;
561 struct eb64_node *node;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100562
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100563 TRACE_ENTER(QMUX_EV_QCC_END);
564
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200565 if (qcc->app_ops && qcc->app_ops->release)
566 qcc->app_ops->release(qcc->ctx);
Amaury Denoyellef8909452022-03-30 11:51:56 +0200567
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200568 if (qcc->task) {
569 task_destroy(qcc->task);
570 qcc->task = NULL;
571 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100572
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200573 if (qcc->wait_event.tasklet)
574 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyellef3e03a42022-04-21 15:41:34 +0200575 if (conn && qcc->wait_event.events) {
576 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
577 qcc->wait_event.events,
578 &qcc->wait_event);
579 }
Amaury Denoyellef8909452022-03-30 11:51:56 +0200580
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200581 /* liberate remaining qcs instances */
582 node = eb64_first(&qcc->streams_by_id);
583 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200584 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200585 node = eb64_next(node);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200586 qcs_free(qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100587 }
588
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200589 pool_free(pool_head_qcc, qcc);
590
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100591 if (conn) {
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100592 LIST_DEL_INIT(&conn->stopping_list);
593
Willy Tarreau784b8682022-04-11 14:18:10 +0200594 conn->handle.qc->conn = NULL;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100595 conn->mux = NULL;
596 conn->ctx = NULL;
597
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100598 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
599
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100600 conn_stop_tracking(conn);
601 conn_full_close(conn);
602 if (conn->destroy_cb)
603 conn->destroy_cb(conn);
604 conn_free(conn);
605 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100606
607 TRACE_LEAVE(QMUX_EV_QCC_END);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100608}
609
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200610/* Transfer as much as possible data on <qcs> from <in> to <out>. <max_data> is
611 * the current flow-control limit on the connection which must not be exceeded.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100612 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200613 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100614 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200615static int qcs_xfer_data(struct qcs *qcs, struct buffer *out,
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200616 struct buffer *in, uint64_t max_data)
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200617{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100618 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200619 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100620 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200621
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100622 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100623
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100624 qc_get_buf(qcs, out);
625
626 /*
627 * QCS out buffer diagram
628 * head left to_xfer
629 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100630 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100631 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100632 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100633 * ^ ack-off ^ sent-off ^ off
634 *
635 * STREAM frame
636 * ^ ^
637 * |xxxxxxxxxxxxxxxxx|
638 */
639
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200640 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100641 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
642
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100643 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200644 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100645
646 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
647 /* do not exceed flow control limit */
648 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
649 to_xfer = qcs->tx.msd - qcs->tx.offset;
650
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100651 BUG_ON_HOT(max_data > qcc->rfctl.md);
652 /* do not overcome flow control limit on connection */
653 if (max_data + to_xfer > qcc->rfctl.md)
654 to_xfer = qcc->rfctl.md - max_data;
655
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100656 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200657 goto out;
658
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200659 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200660
661 out:
662 {
663 struct qcs_xfer_data_trace_arg arg = {
664 .prep = b_data(out), .xfer = total,
665 };
666 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
667 qcc->conn, qcs, &arg);
668 }
669
670 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200671}
672
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200673/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
674 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
675 * stream frame.
676 *
677 * Returns the length of the STREAM frame or a negative error code.
678 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200679static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
680 struct list *frm_list)
681{
682 struct qcc *qcc = qcs->qcc;
683 struct quic_frame *frm;
684 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200685 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200686
687 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
688
Amaury Denoyellea4569202022-04-15 17:29:25 +0200689 /* if ack_offset < buf_offset, it points to an older buffer. */
690 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
691 BUG_ON(qcs->tx.sent_offset < base_off);
692
693 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200694 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200695 BUG_ON(total < 0);
696
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200697 if (!total) {
698 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
699 return 0;
700 }
Amaury Denoyellea4569202022-04-15 17:29:25 +0200701 BUG_ON(qcs->tx.sent_offset >= qcs->tx.offset);
702 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200703
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200704 frm = pool_zalloc(pool_head_quic_frame);
705 if (!frm)
706 goto err;
707
Frédéric Lécailleb9171912022-04-21 17:32:10 +0200708 LIST_INIT(&frm->reflist);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100709 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200710 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200711 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100712 frm->stream.buf = out;
713 frm->stream.data = (unsigned char *)b_peek(out, head);
714
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100715 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200716 if (fin)
717 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100718
719 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200720 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100721 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200722 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100723
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200724 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
725 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200726
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100727 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100728
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200729 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100730 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200731 struct qcs_build_stream_trace_arg arg = {
732 .len = frm->stream.len, .fin = fin,
733 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100734 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200735 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100736 qcc->conn, qcs, &arg);
737 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100738
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200739 return total;
740
741 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100742 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200743 return -1;
744}
745
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100746/* This function must be called by the upper layer to inform about the sending
747 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
748 * <offset>.
749 */
750void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
751{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100752 struct qcc *qcc = qcs->qcc;
753 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100754
755 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200756 BUG_ON(offset >= qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100757
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100758 /* check if the STREAM frame has already been notified. It can happen
759 * for retransmission.
760 */
761 if (offset + data <= qcs->tx.sent_offset)
762 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100763
764 diff = offset + data - qcs->tx.sent_offset;
765
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100766 /* increase offset sum on connection */
767 qcc->tx.sent_offsets += diff;
768 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
769 if (qcc->tx.sent_offsets == qcc->rfctl.md)
770 qcc->flags |= QC_CF_BLK_MFCTL;
771
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100772 /* increase offset on stream */
773 qcs->tx.sent_offset += diff;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100774 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200775 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100776 if (qcs->tx.sent_offset == qcs->tx.msd)
777 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200778
779 if (qcs->tx.offset == qcs->tx.sent_offset && b_full(&qcs->stream->buf->buf)) {
780 qc_stream_buf_release(qcs->stream);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200781 /* prepare qcs for immediate send retry if data to send */
782 if (b_data(&qcs->tx.buf))
783 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200784 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100785}
786
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100787/* Wrapper for send on transport layer. Send a list of frames <frms> for the
788 * connection <qcc>.
789 *
790 * Returns 0 if all data sent with success else non-zero.
791 */
792static int qc_send_frames(struct qcc *qcc, struct list *frms)
793{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100794 /* TODO implement an opportunistic retry mechanism. This is needed
795 * because qc_send_app_pkts is not completed. It will only prepare data
796 * up to its Tx buffer. The frames left are not send even if the Tx
797 * buffer is emptied by the sendto call.
798 *
799 * To overcome this, we call repeatedly qc_send_app_pkts until we
800 * detect that the transport layer has send nothing. This could happen
801 * on congestion or sendto syscall error.
802 *
803 * When qc_send_app_pkts is improved to handle retry by itself, we can
804 * remove the looping from the MUX.
805 */
806 struct quic_frame *first_frm;
807 uint64_t first_offset = 0;
808 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100809
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100810 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
811
812 if (LIST_ISEMPTY(frms)) {
813 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200814 return 1;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100815 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100816
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200817 LIST_INIT(&qcc->send_retry_list);
818
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100819 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100820 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
821 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
822 first_offset = first_frm->stream.offset.key;
823 first_stream_frame_type = 1;
824 }
825 else {
826 first_stream_frame_type = 0;
827 }
828
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100829 if (!LIST_ISEMPTY(frms))
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +0200830 qc_send_app_pkts(qcc->conn->handle.qc, 0, frms);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100831
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100832 /* If there is frames left, check if the transport layer has send some
833 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100834 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100835 if (!LIST_ISEMPTY(frms)) {
836 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
837 goto retry_send;
838
839 /* If the first frame is STREAM, check if its offset has
840 * changed.
841 */
842 if (first_stream_frame_type &&
843 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
844 goto retry_send;
845 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100846 }
847
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100848 /* If there is frames left at this stage, transport layer is blocked.
849 * Subscribe on it to retry later.
850 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100851 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100852 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100853 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
854 SUB_RETRY_SEND, &qcc->wait_event);
855 return 1;
856 }
857
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100858 TRACE_LEAVE(QMUX_EV_QCC_SEND);
859
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100860 return 0;
861}
862
Amaury Denoyellec9337802022-04-04 16:36:34 +0200863/* Send a MAX_STREAM_BIDI frame to update the limit of bidirectional streams
864 * allowed to be opened by the peer. The caller should have first checked if
865 * this is required with qc_is_max_streams_needed.
866 *
867 * Returns 0 on success else non-zero.
868 */
869static int qc_send_max_streams(struct qcc *qcc)
870{
871 struct list frms = LIST_HEAD_INIT(frms);
872 struct quic_frame *frm;
873
874 frm = pool_zalloc(pool_head_quic_frame);
875 BUG_ON(!frm); /* TODO handle this properly */
876
Frédéric Lécailleb9171912022-04-21 17:32:10 +0200877 LIST_INIT(&frm->reflist);
Amaury Denoyellec9337802022-04-04 16:36:34 +0200878 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
879 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
880 qcc->lfctl.cl_bidi_r;
881 TRACE_DEVEL("sending MAX_STREAMS frame", QMUX_EV_SEND_FRM, qcc->conn, NULL, frm);
882 LIST_APPEND(&frms, &frm->list);
883
884 if (qc_send_frames(qcc, &frms))
885 return 1;
886
887 /* save the new limit if the frame has been send. */
888 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
889 qcc->lfctl.cl_bidi_r = 0;
890
891 return 0;
892}
893
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200894/* Used internally by qc_send function. Proceed to send for <qcs>. This will
895 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
896 * is then generated and inserted in <frms> list. <qcc_max_data> is the current
897 * flow-control max-data at the connection level which must not be surpassed.
898 *
899 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
900 * be null if out buffer cannot be allocated.
901 */
902static int _qc_send_qcs(struct qcs *qcs, struct list *frms,
903 uint64_t qcc_max_data)
904{
905 struct qcc *qcc = qcs->qcc;
906 struct buffer *buf = &qcs->tx.buf;
907 struct buffer *out = qc_stream_buf_get(qcs->stream);
908 int xfer = 0;
909
910 /* Allocate <out> buffer if necessary. */
911 if (!out) {
912 if (qcc->flags & QC_CF_CONN_FULL)
913 return 0;
914
915 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
916 if (!out) {
917 qcc->flags |= QC_CF_CONN_FULL;
918 return 0;
919 }
920 }
921
922 /* Transfer data from <buf> to <out>. */
923 if (b_data(buf)) {
924 xfer = qcs_xfer_data(qcs, out, buf, qcc_max_data);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200925 if (xfer > 0) {
926 qcs_notify_send(qcs);
927 qcs->flags &= ~QC_SF_BLK_MROOM;
928 }
929
930 qcs->tx.offset += xfer;
931 }
932
933 /* out buffer cannot be emptied if qcs offsets differ. */
934 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
935
936 /* Build a new STREAM frame with <out> buffer. */
937 if (qcs->tx.sent_offset != qcs->tx.offset) {
938 int ret;
939 char fin = !!(qcs->flags & QC_SF_FIN_STREAM);
940
941 /* FIN is set if all incoming data were transfered. */
942 fin = !!(fin && !b_data(buf));
943
944 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +0200945 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200946 }
947
948 return xfer;
949}
950
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100951/* Proceed to sending. Loop through all available streams for the <qcc>
952 * instance and try to send as much as possible.
953 *
954 * Returns the total of bytes sent to the transport layer.
955 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100956static int qc_send(struct qcc *qcc)
957{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100958 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200959 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200960 struct qcs *qcs, *qcs_tmp;
961 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200962
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100963 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200964
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200965 if (qcc->conn->flags & CO_FL_SOCK_WR_SH) {
966 qcc->conn->flags |= CO_FL_ERROR;
967 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
968 return 0;
969 }
970
Amaury Denoyellec9337802022-04-04 16:36:34 +0200971 if (qc_is_max_streams_needed(qcc))
972 qc_send_max_streams(qcc);
973
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100974 if (qcc->flags & QC_CF_BLK_MFCTL)
975 return 0;
976
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100977 /* loop through all streams, construct STREAM frames if data available.
978 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200979 */
980 node = eb64_first(&qcc->streams_by_id);
981 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200982 int ret;
983 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100984
Amaury Denoyellee2ec9422022-03-10 16:46:18 +0100985 /* TODO
986 * for the moment, unidirectional streams have their own
987 * mechanism for sending. This should be unified in the future,
988 * in this case the next check will be removed.
989 */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200990 if (quic_stream_is_uni(qcs->id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +0100991 node = eb64_next(node);
992 continue;
993 }
994
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100995 if (qcs->flags & QC_SF_BLK_SFCTL) {
996 node = eb64_next(node);
997 continue;
998 }
999
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001000 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001001 node = eb64_next(node);
1002 continue;
1003 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001004
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001005 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + total);
1006 total += ret;
1007 node = eb64_next(node);
1008 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001009
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001010 if (qc_send_frames(qcc, &frms)) {
1011 /* data rejected by transport layer, do not retry. */
1012 goto out;
1013 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001014
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001015 retry:
1016 tmp_total = 0;
1017 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
1018 int ret;
1019 BUG_ON(!b_data(&qcs->tx.buf));
1020 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001021
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001022 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + tmp_total);
1023 tmp_total += ret;
1024 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001025 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001026
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001027 total += tmp_total;
1028 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
1029 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +01001030
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001031 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001032 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1033
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001034 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001035}
1036
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001037/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1038 * operation.
1039 *
1040 * Returns 0 on success else non-zero.
1041 */
1042static int qc_recv(struct qcc *qcc)
1043{
1044 struct eb64_node *node;
1045 struct qcs *qcs;
1046
1047 node = eb64_first(&qcc->streams_by_id);
1048 while (node) {
1049 qcs = eb64_entry(node, struct qcs, by_id);
1050
1051 /* TODO unidirectional streams have their own mechanism for Rx.
1052 * This should be unified.
1053 */
1054 if (quic_stream_is_uni(qcs->id)) {
1055 node = eb64_next(node);
1056 continue;
1057 }
1058
1059 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
1060 node = eb64_next(node);
1061 continue;
1062 }
1063
1064 qcc_decode_qcs(qcc, qcs);
1065 node = eb64_next(node);
1066 }
1067
1068 return 0;
1069}
1070
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001071/* Release all streams that are already marked as detached. This is only done
1072 * if their TX buffers are empty or if a CONNECTION_CLOSE has been received.
1073 *
1074 * Return the number of released stream.
1075 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001076static int qc_release_detached_streams(struct qcc *qcc)
1077{
1078 struct eb64_node *node;
1079 int release = 0;
1080
1081 node = eb64_first(&qcc->streams_by_id);
1082 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001083 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001084 node = eb64_next(node);
1085
1086 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001087 if (!b_data(&qcs->tx.buf) &&
1088 qcs->tx.offset == qcs->tx.sent_offset) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001089 qcs_destroy(qcs);
1090 release = 1;
1091 }
1092 else {
1093 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1094 SUB_RETRY_SEND, &qcc->wait_event);
1095 }
1096 }
1097 }
1098
1099 return release;
1100}
1101
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001102static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1103{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001104 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001105
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001106 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001107
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001108 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001109
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001110 if (qc_release_detached_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001111 if (qcc_is_dead(qcc)) {
1112 qc_release(qcc);
1113 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001114 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001115 if (qcc_may_expire(qcc))
1116 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1117 else
1118 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001119 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001120 }
1121 }
1122
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001123 qc_recv(qcc);
1124
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001125 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1126
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001127 return NULL;
1128}
1129
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001130static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1131{
1132 struct qcc *qcc = ctx;
1133 int expired = tick_is_expired(t->expire, now_ms);
1134
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001135 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001136
1137 if (qcc) {
1138 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001139 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001140 return t;
1141 }
1142
1143 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001144 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001145 t->expire = TICK_ETERNITY;
1146 return t;
1147 }
1148 }
1149
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001150 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001151
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001152 if (!qcc) {
1153 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001154 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001155 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001156
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001157 qcc->task = NULL;
1158
1159 if (qcc_is_dead(qcc))
1160 qc_release(qcc);
1161
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001162 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1163
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001164 return NULL;
1165}
1166
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001167static int qc_init(struct connection *conn, struct proxy *prx,
1168 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001169{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001170 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001171 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001172
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001173 qcc = pool_alloc(pool_head_qcc);
1174 if (!qcc)
1175 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001176
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001177 qcc->conn = conn;
1178 conn->ctx = qcc;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001179 qcc->nb_cs = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001180 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001181
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001182 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001183
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001184 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001185
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001186 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001187 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001188
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001189 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001190 qcc->tx.sent_offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001191
1192 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001193 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001194 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
1195 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001196 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001197 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001198
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001199 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001200 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
1201 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001202 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001203 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001204
1205 /* Server initiated streams must respect the server flow control. */
1206 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001207 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
1208 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001209 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001210 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1211
1212 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001213 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
1214 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001215 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001216 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001217
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001218 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02001219 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
1220 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001221 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001222
Willy Tarreau784b8682022-04-11 14:18:10 +02001223 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001224 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001225 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1226 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1227
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001228 qcc->wait_event.tasklet = tasklet_new();
1229 if (!qcc->wait_event.tasklet)
1230 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001231
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001232 LIST_INIT(&qcc->send_retry_list);
1233
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001234 qcc->subs = NULL;
1235 qcc->wait_event.tasklet->process = qc_io_cb;
1236 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001237 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001238
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001239 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001240 qcc->task = NULL;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001241 qcc->timeout = prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001242 if (tick_isset(qcc->timeout)) {
1243 qcc->task = task_new_here();
1244 if (!qcc->task)
1245 goto fail_no_timeout_task;
1246 qcc->task->process = qc_timeout_task;
1247 qcc->task->context = qcc;
1248 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1249 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001250
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001251 if (!conn_is_back(conn)) {
1252 if (!LIST_INLIST(&conn->stopping_list)) {
1253 LIST_APPEND(&mux_stopping_data[tid].list,
1254 &conn->stopping_list);
1255 }
1256 }
1257
Willy Tarreau784b8682022-04-11 14:18:10 +02001258 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001259 /* init read cycle */
1260 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001261
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001262 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001263
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001264 fail_no_timeout_task:
1265 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001266 fail_no_tasklet:
1267 pool_free(pool_head_qcc, qcc);
1268 fail_no_qcc:
1269 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001270}
1271
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001272static void qc_destroy(void *ctx)
1273{
1274 struct qcc *qcc = ctx;
1275
1276 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
1277 qc_release(qcc);
1278 TRACE_LEAVE(QMUX_EV_QCC_END);
1279}
1280
Willy Tarreau4201ab72022-05-10 19:18:52 +02001281static void qc_detach(struct cs_endpoint *endp)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001282{
Willy Tarreaudf943132022-05-13 16:31:23 +02001283 struct qcs *qcs = endp->target;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001284 struct qcc *qcc = qcs->qcc;
1285
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001286 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001287
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001288 --qcc->nb_cs;
1289
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001290 if ((b_data(&qcs->tx.buf) || qcs->tx.offset > qcs->tx.sent_offset) &&
1291 !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001292 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001293 qcs->flags |= QC_SF_DETACH;
1294 return;
1295 }
1296
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001297 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001298
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001299 if (qcc_is_dead(qcc)) {
1300 qc_release(qcc);
1301 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001302 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001303 if (qcc_may_expire(qcc))
1304 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1305 else
1306 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001307 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001308 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001309
1310 TRACE_LEAVE(QMUX_EV_STRM_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001311}
1312
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001313/* Called from the upper layer, to receive data */
1314static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf,
1315 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001316{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001317 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001318 struct htx *qcs_htx = NULL;
1319 struct htx *cs_htx = NULL;
1320 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001321 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001322
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001323 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001324
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001325 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
1326 if (htx_is_empty(qcs_htx)) {
1327 /* Set buffer data to 0 as HTX is empty. */
1328 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1329 goto end;
1330 }
1331
1332 ret = qcs_htx->data;
1333
1334 cs_htx = htx_from_buf(buf);
1335 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
1336 htx_to_buf(cs_htx, buf);
1337 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1338 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
1339 goto end;
1340 }
1341
1342 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
1343 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
1344
1345 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001346 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
1347 cs_htx->flags |= HTX_FL_EOM;
1348 fin = 1;
1349 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001350
1351 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
1352 htx_to_buf(cs_htx, buf);
1353 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1354 ret -= qcs_htx->data;
1355
1356 end:
1357 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreau15b07212022-05-10 11:24:26 +02001358 qcs->endp->flags |= (CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001359 }
1360 else {
Willy Tarreau15b07212022-05-10 11:24:26 +02001361 qcs->endp->flags &= ~(CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
1362 if (qcs->endp->flags & CS_EP_ERR_PENDING)
1363 qcs->endp->flags |= CS_EP_ERROR;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001364
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001365 if (fin)
Willy Tarreau15b07212022-05-10 11:24:26 +02001366 qcs->endp->flags |= CS_EP_EOI;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001367
1368 if (b_size(&qcs->rx.app_buf)) {
1369 b_free(&qcs->rx.app_buf);
1370 offer_buffers(NULL, 1);
1371 }
1372 }
1373
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001374 if (ret) {
1375 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001376 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001377 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001378
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001379 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
1380
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001381 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001382}
1383
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001384static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf,
1385 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001386{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001387 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001388 size_t ret;
1389
1390 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001391
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001392 ret = qcs->qcc->app_ops->snd_buf(cs, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001393
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001394 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
1395
1396 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001397}
1398
1399/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1400 * event subscriber <es> is not allowed to change from a previous call as long
1401 * as at least one event is still subscribed. The <event_type> must only be a
1402 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1403 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001404static int qc_subscribe(struct conn_stream *cs, int event_type,
1405 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001406{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001407 return qcs_subscribe(__cs_mux(cs), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001408}
1409
1410/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1411 * The <es> pointer is not allowed to differ from the one passed to the
1412 * subscribe() call. It always returns zero.
1413 */
1414static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
1415{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001416 struct qcs *qcs = __cs_mux(cs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001417
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001418 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1419 BUG_ON(qcs->subs && qcs->subs != es);
1420
1421 es->events &= ~event_type;
1422 if (!es->events)
1423 qcs->subs = NULL;
1424
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001425 return 0;
1426}
1427
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001428/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Christopher Fauletb041b232022-03-24 10:27:02 +01001429 * report CS_EP_ERR_PENDING|CS_EP_ERROR on the attached conn-streams and wake
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001430 * them.
1431 */
1432static int qc_wake_some_streams(struct qcc *qcc)
1433{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001434 struct qcs *qcs;
1435 struct eb64_node *node;
1436
1437 for (node = eb64_first(&qcc->streams_by_id); node;
1438 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001439 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001440
Willy Tarreau01c2a4a2022-05-10 15:46:10 +02001441 if (!qcs->endp->cs)
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001442 continue;
1443
1444 if (qcc->conn->flags & CO_FL_ERROR) {
Christopher Fauletb041b232022-03-24 10:27:02 +01001445 qcs->endp->flags |= CS_EP_ERR_PENDING;
1446 if (qcs->endp->flags & CS_EP_EOS)
1447 qcs->endp->flags |= CS_EP_ERROR;
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001448
1449 if (qcs->subs) {
1450 qcs_notify_recv(qcs);
1451 qcs_notify_send(qcs);
1452 }
Willy Tarreau01c2a4a2022-05-10 15:46:10 +02001453 else if (qcs->endp->cs->data_cb->wake) {
1454 qcs->endp->cs->data_cb->wake(qcs->endp->cs);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001455 }
1456 }
1457 }
1458
1459 return 0;
1460}
1461
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001462static int qc_wake(struct connection *conn)
1463{
1464 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02001465 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001466
1467 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001468
1469 /* Check if a soft-stop is in progress.
1470 * Release idling front connection if this is the case.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001471 *
1472 * TODO this is revelant for frontend connections only.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001473 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001474 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
1475 goto release;
1476
Willy Tarreau784b8682022-04-11 14:18:10 +02001477 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02001478 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
1479
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001480 qc_send(qcc);
1481
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001482 qc_wake_some_streams(qcc);
1483
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001484 if (qcc_is_dead(qcc))
1485 goto release;
1486
1487 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
1488
1489 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001490
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001491 release:
1492 qc_release(qcc);
1493 TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001494 return 1;
1495}
1496
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001497
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001498static void qmux_trace_frm(const struct quic_frame *frm)
1499{
1500 switch (frm->type) {
1501 case QUIC_FT_MAX_STREAMS_BIDI:
1502 chunk_appendf(&trace_buf, " max_streams=%lu",
1503 frm->max_streams_bidi.max_streams);
1504 break;
1505
1506 case QUIC_FT_MAX_STREAMS_UNI:
1507 chunk_appendf(&trace_buf, " max_streams=%lu",
1508 frm->max_streams_uni.max_streams);
1509 break;
1510
1511 default:
1512 break;
1513 }
1514}
1515
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001516/* quic-mux trace handler */
1517static void qmux_trace(enum trace_level level, uint64_t mask,
1518 const struct trace_source *src,
1519 const struct ist where, const struct ist func,
1520 const void *a1, const void *a2, const void *a3, const void *a4)
1521{
1522 const struct connection *conn = a1;
1523 const struct qcc *qcc = conn ? conn->ctx : NULL;
1524 const struct qcs *qcs = a2;
1525
1526 if (!qcc)
1527 return;
1528
1529 if (src->verbosity > QMUX_VERB_CLEAN) {
1530 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
1531
1532 if (qcs)
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001533 chunk_appendf(&trace_buf, " qcs=%p(%lu)", qcs, qcs->id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001534
1535 if (mask & QMUX_EV_QCC_NQCS) {
1536 const uint64_t *id = a3;
1537 chunk_appendf(&trace_buf, " id=%lu", *id);
1538 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001539
1540 if (mask & QMUX_EV_SEND_FRM)
1541 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001542
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001543 if (mask & QMUX_EV_QCS_XFER_DATA) {
1544 const struct qcs_xfer_data_trace_arg *arg = a3;
1545 chunk_appendf(&trace_buf, " prep=%lu xfer=%d",
1546 arg->prep, arg->xfer);
1547 }
1548
1549 if (mask & QMUX_EV_QCS_BUILD_STRM) {
1550 const struct qcs_build_stream_trace_arg *arg = a3;
1551 chunk_appendf(&trace_buf, " len=%lu fin=%d offset=%lu",
1552 arg->len, arg->fin, arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001553 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001554 }
1555}
1556
Amaury Denoyelle251eadf2022-03-24 17:14:52 +01001557/* Function to automatically activate QUIC MUX traces on stdout.
1558 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
1559 * Main use for now is in the docker image for QUIC interop testing.
1560 */
1561static void qmux_init_stdout_traces(void)
1562{
1563#ifdef ENABLE_QUIC_STDOUT_TRACES
1564 trace_qmux.sink = sink_find("stdout");
1565 trace_qmux.level = TRACE_LEVEL_DEVELOPER;
1566 trace_qmux.state = TRACE_STATE_RUNNING;
1567 trace_qmux.verbosity = QMUX_VERB_MINIMAL;
1568#endif
1569}
1570INITCALL0(STG_INIT, qmux_init_stdout_traces);
1571
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001572
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001573static const struct mux_ops qc_ops = {
1574 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001575 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001576 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001577 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001578 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001579 .subscribe = qc_subscribe,
1580 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001581 .wake = qc_wake,
Christopher Fauleta97cced2022-04-12 18:04:10 +02001582 .flags = MX_FL_HTX|MX_FL_NO_UPG,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02001583 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001584};
1585
1586static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001587 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001588
1589INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);