blob: 6cc7756d88729063175b4d7eb51f148202fcbcc8 [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;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200163 qcs->rx.msd_init = qcs->rx.msd;
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200164
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100165 qcs->tx.buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100166 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100167 qcs->tx.sent_offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100168
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100169 qcs->wait_event.tasklet = NULL;
170 qcs->wait_event.events = 0;
171 qcs->subs = NULL;
172
173 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100174 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100175 return qcs;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200176
177 err:
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200178 if (qcs->ctx && qcc->app_ops->detach)
179 qcc->app_ops->detach(qcs);
180
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200181 if (qcs->stream)
182 qc_stream_desc_release(qcs->stream);
183
184 pool_free(pool_head_qcs, qcs);
185 return NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100186}
187
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200188static void qc_free_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
189{
190 struct buffer buf;
191
Amaury Denoyelle7313f5e2022-05-17 18:53:21 +0200192 if (ncb_is_null(ncbuf))
193 return;
194
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200195 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
196 b_free(&buf);
Amaury Denoyelle7313f5e2022-05-17 18:53:21 +0200197 offer_buffers(NULL, 1);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200198
199 *ncbuf = NCBUF_NULL;
200}
201
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200202/* Free a qcs. This function must only be done to remove a stream on allocation
203 * error or connection shutdown. Else use qcs_destroy which handle all the
204 * QUIC connection mechanism.
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100205 */
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200206void qcs_free(struct qcs *qcs)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100207{
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200208 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200209 b_free(&qcs->tx.buf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200210
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200211 BUG_ON(!qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams);
212 --qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams;
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200213
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200214 if (qcs->ctx && qcs->qcc->app_ops->detach)
215 qcs->qcc->app_ops->detach(qcs);
216
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200217 qc_stream_desc_release(qcs->stream);
218
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100219 BUG_ON(qcs->endp && !(qcs->endp->flags & CS_EP_ORPHAN));
220 cs_endpoint_free(qcs->endp);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200221
222 eb64_delete(&qcs->by_id);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100223 pool_free(pool_head_qcs, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100224}
225
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100226struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100227{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100228 struct buffer *buf = b_alloc(bptr);
229 BUG_ON(!buf);
230 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100231}
232
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200233struct ncbuf *qc_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
234{
235 struct buffer buf = BUF_NULL;
236
237 if (ncb_is_null(ncbuf)) {
238 b_alloc(&buf);
239 BUG_ON(b_is_null(&buf));
240
241 *ncbuf = ncb_make(buf.area, buf.size, 0);
242 ncb_init(ncbuf, 0);
243 }
244
245 return ncbuf;
246}
247
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100248int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
249{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100250 struct qcc *qcc = qcs->qcc;
251
252 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100253
254 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
255 BUG_ON(qcs->subs && qcs->subs != es);
256
257 es->events |= event_type;
258 qcs->subs = es;
259
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100260 if (event_type & SUB_RETRY_RECV)
261 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
262
263 if (event_type & SUB_RETRY_SEND)
264 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
265
266 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
267
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100268 return 0;
269}
270
271void qcs_notify_recv(struct qcs *qcs)
272{
273 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
274 tasklet_wakeup(qcs->subs->tasklet);
275 qcs->subs->events &= ~SUB_RETRY_RECV;
276 if (!qcs->subs->events)
277 qcs->subs = NULL;
278 }
279}
280
281void qcs_notify_send(struct qcs *qcs)
282{
283 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
284 tasklet_wakeup(qcs->subs->tasklet);
285 qcs->subs->events &= ~SUB_RETRY_SEND;
286 if (!qcs->subs->events)
287 qcs->subs = NULL;
288 }
289}
290
Amaury Denoyellea9773552022-05-16 14:38:25 +0200291/* Remove <bytes> from <qcs> Rx buffer. This must be called by transcoders
292 * after STREAM parsing. Flow-control for received offsets may be allocated for
293 * the peer if needed.
294 */
295void qcs_consume(struct qcs *qcs, uint64_t bytes)
296{
297 struct qcc *qcc = qcs->qcc;
298 struct quic_frame *frm;
299 enum ncb_ret ret;
300
301 ret = ncb_advance(&qcs->rx.ncbuf, bytes);
302 if (ret) {
303 ABORT_NOW(); /* should not happens because removal only in data */
304 }
305
306 qcs->rx.offset += bytes;
307 if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) {
308 frm = pool_zalloc(pool_head_quic_frame);
309 BUG_ON(!frm); /* TODO handle this properly */
310
311 qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init;
312
313 LIST_INIT(&frm->reflist);
314 frm->type = QUIC_FT_MAX_STREAM_DATA;
315 frm->max_stream_data.id = qcs->id;
316 frm->max_stream_data.max_stream_data = qcs->rx.msd;
317
318 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
319 tasklet_wakeup(qcc->wait_event.tasklet);
320 }
Amaury Denoyellec830e1e2022-05-16 16:19:59 +0200321
322 qcc->lfctl.sent_offsets += bytes;
323 if (qcc->lfctl.md - qcc->lfctl.sent_offsets < qcc->lfctl.md_init / 2) {
324 frm = pool_zalloc(pool_head_quic_frame);
325 BUG_ON(!frm); /* TODO handle this properly */
326
327 qcc->lfctl.md = qcc->lfctl.sent_offsets + qcc->lfctl.md_init;
328
329 LIST_INIT(&frm->reflist);
330 frm->type = QUIC_FT_MAX_DATA;
331 frm->max_data.max_data = qcc->lfctl.md;
332
333 LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list);
334 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
335 }
Amaury Denoyellea9773552022-05-16 14:38:25 +0200336}
337
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100338/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
339 * several streams, depending on the already open ones.
340 * Return this node if succeeded, NULL if not.
341 */
Amaury Denoyelle50742292022-03-29 14:57:19 +0200342struct qcs *qcc_get_qcs(struct qcc *qcc, uint64_t id)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100343{
344 unsigned int strm_type;
345 int64_t sub_id;
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200346 struct eb64_node *node;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200347 struct qcs *qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100348
349 strm_type = id & QCS_ID_TYPE_MASK;
350 sub_id = id >> QCS_ID_TYPE_SHIFT;
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200351 node = NULL;
Amaury Denoyelle0dc40f02022-02-07 11:44:17 +0100352 if (quic_stream_is_local(qcc, id)) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100353 /* Local streams: this stream must be already opened. */
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200354 node = eb64_lookup(&qcc->streams_by_id, id);
355 if (!node) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100356 /* unknown stream id */
357 goto out;
358 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200359 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100360 }
361 else {
362 /* Remote streams. */
363 struct eb_root *strms;
364 uint64_t largest_id;
365 enum qcs_type qcs_type;
366
367 strms = &qcc->streams_by_id;
368 qcs_type = qcs_id_type(id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100369
370 /* TODO also checks max-streams for uni streams */
371 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100372 if (sub_id + 1 > qcc->lfctl.ms_bidi) {
Amaury Denoyellec055e302022-02-07 16:09:06 +0100373 /* streams limit reached */
374 goto out;
375 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100376 }
377
378 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
379 * correct value.
380 */
381 largest_id = qcc->strms[qcs_type].largest_id;
382 if (sub_id > (int64_t)largest_id) {
383 /* RFC: "A stream ID that is used out of order results in all streams
384 * of that type with lower-numbered stream IDs also being opened".
385 * So, let's "open" these streams.
386 */
387 int64_t i;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200388 struct qcs *tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100389
Amaury Denoyelle50742292022-03-29 14:57:19 +0200390 tmp_qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100391 for (i = largest_id + 1; i <= sub_id; i++) {
392 uint64_t id = (i << QCS_ID_TYPE_SHIFT) | strm_type;
393 enum qcs_type type = id & QCS_ID_DIR_BIT ? QCS_CLT_UNI : QCS_CLT_BIDI;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200394
Amaury Denoyelle50742292022-03-29 14:57:19 +0200395 tmp_qcs = qcs_new(qcc, id, type);
396 if (!tmp_qcs) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100397 /* allocation failure */
398 goto out;
399 }
400
401 qcc->strms[qcs_type].largest_id = i;
402 }
Amaury Denoyelle50742292022-03-29 14:57:19 +0200403 if (tmp_qcs)
404 qcs = tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100405 }
406 else {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200407 node = eb64_lookup(strms, id);
408 if (node)
409 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100410 }
411 }
412
Amaury Denoyelle50742292022-03-29 14:57:19 +0200413 return qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100414
415 out:
416 return NULL;
417}
418
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200419/* Decode the content of STREAM frames already received on the stream instance
420 * <qcs>.
421 *
422 * Returns 0 on success else non-zero.
423 */
424static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
425{
426 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
427
428 if (qcc->app_ops->decode_qcs(qcs, qcs->flags & QC_SF_FIN_RECV, qcc->ctx) < 0) {
429 TRACE_DEVEL("leaving on decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
430 return 1;
431 }
432
433 qcs_notify_recv(qcs);
434
435 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
436
437 return 0;
438}
439
440/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
441 * <data> with length <len> and represents the offset <offset>. <fin> is set if
442 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100443 *
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200444 * Returns 0 on success else non-zero.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100445 */
446int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200447 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100448{
449 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200450 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100451
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100452 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
453
Amaury Denoyelle50742292022-03-29 14:57:19 +0200454 qcs = qcc_get_qcs(qcc, id);
455 if (!qcs) {
Frédéric Lécailleb57de072022-05-02 18:58:27 +0200456 if ((id >> QCS_ID_TYPE_SHIFT) <= qcc->strms[qcs_id_type(id)].largest_id) {
457 TRACE_DEVEL("already released stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
458 return 0;
459 }
460 else {
461 TRACE_DEVEL("leaving on stream not found", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
462 return 1;
463 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100464 }
465
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100466 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100467 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 +0200468 return 0;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100469 }
470
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200471 /* TODO if last frame already received, stream size must not change.
472 * Else send FINAL_SIZE_ERROR.
473 */
474
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200475 /* TODO initial max-stream-data overflow. Implement FLOW_CONTROL_ERROR emission. */
476 BUG_ON(offset + len > qcs->rx.msd);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100477
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200478 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100479 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200480 ABORT_NOW();
481 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100482 }
483
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100484 TRACE_DEVEL("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200485 if (offset < qcs->rx.offset) {
486 len -= qcs->rx.offset - offset;
487 offset = qcs->rx.offset;
488 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100489
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200490 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
491 if (ret != NCB_RET_OK) {
492 if (ret == NCB_RET_DATA_REJ) {
493 /* TODO generate PROTOCOL_VIOLATION error */
494 TRACE_DEVEL("leaving on data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
495 qcc->conn, qcs);
496 }
497 else if (ret == NCB_RET_GAP_SIZE) {
498 TRACE_DEVEL("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
499 qcc->conn, qcs);
500 }
501 return 1;
502 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100503
504 if (fin)
505 qcs->flags |= QC_SF_FIN_RECV;
506
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200507 if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL))
508 qcc_decode_qcs(qcc, qcs);
509
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100510 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100511 return 0;
512}
513
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100514/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
515 * the frame.
516 *
517 * Returns 0 on success else non-zero.
518 */
519int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
520{
521 if (qcc->rfctl.md < max) {
522 qcc->rfctl.md = max;
523
524 if (qcc->flags & QC_CF_BLK_MFCTL) {
525 qcc->flags &= ~QC_CF_BLK_MFCTL;
526 tasklet_wakeup(qcc->wait_event.tasklet);
527 }
528 }
529 return 0;
530}
531
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100532/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
533 * field of the frame and <id> is the identifier of the QUIC stream.
534 *
535 * Returns 0 on success else non-zero.
536 */
537int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
538{
539 struct qcs *qcs;
540 struct eb64_node *node;
541
542 node = eb64_lookup(&qcc->streams_by_id, id);
543 if (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200544 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100545 if (max > qcs->tx.msd) {
546 qcs->tx.msd = max;
547
548 if (qcs->flags & QC_SF_BLK_SFCTL) {
549 qcs->flags &= ~QC_SF_BLK_SFCTL;
550 tasklet_wakeup(qcc->wait_event.tasklet);
551 }
552 }
553 }
554
555 return 0;
556}
557
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200558/* Signal the closing of remote stream with id <id>. Flow-control for new
559 * streams may be allocated for the peer if needed.
560 */
561static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +0100562{
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200563 struct quic_frame *frm;
564
565 if (quic_stream_is_bidi(id)) {
566 ++qcc->lfctl.cl_bidi_r;
567 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
568 frm = pool_zalloc(pool_head_quic_frame);
569 BUG_ON(!frm); /* TODO handle this properly */
570
571 LIST_INIT(&frm->reflist);
572 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
573 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
574 qcc->lfctl.cl_bidi_r;
575 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
576 tasklet_wakeup(qcc->wait_event.tasklet);
577
578 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
579 qcc->lfctl.cl_bidi_r = 0;
580 }
581 }
582 else {
583 /* TODO */
584 }
585
586 return 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100587}
588
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500589/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100590static void qcs_destroy(struct qcs *qcs)
591{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100592 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200593 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100594
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100595 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100596
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200597 if (quic_stream_is_remote(qcs->qcc, id))
598 qcc_release_remote_stream(qcs->qcc, id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100599
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200600 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100601
602 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100603}
604
605static inline int qcc_is_dead(const struct qcc *qcc)
606{
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200607 if (qcc->app_ops && qcc->app_ops->is_active &&
608 qcc->app_ops->is_active(qcc, qcc->ctx))
609 return 0;
610
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200611 if ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task)
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100612 return 1;
613
614 return 0;
615}
616
617/* Return true if the mux timeout should be armed. */
618static inline int qcc_may_expire(struct qcc *qcc)
619{
Amaury Denoyelle06890aa2022-04-04 16:15:06 +0200620 return !qcc->nb_cs;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100621}
622
623/* release function. This one should be called to free all resources allocated
624 * to the mux.
625 */
626static void qc_release(struct qcc *qcc)
627{
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200628 struct connection *conn = qcc->conn;
629 struct eb64_node *node;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100630
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100631 TRACE_ENTER(QMUX_EV_QCC_END);
632
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200633 if (qcc->app_ops && qcc->app_ops->release)
634 qcc->app_ops->release(qcc->ctx);
Amaury Denoyellef8909452022-03-30 11:51:56 +0200635
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200636 if (qcc->task) {
637 task_destroy(qcc->task);
638 qcc->task = NULL;
639 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100640
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200641 if (qcc->wait_event.tasklet)
642 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyellef3e03a42022-04-21 15:41:34 +0200643 if (conn && qcc->wait_event.events) {
644 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
645 qcc->wait_event.events,
646 &qcc->wait_event);
647 }
Amaury Denoyellef8909452022-03-30 11:51:56 +0200648
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200649 /* liberate remaining qcs instances */
650 node = eb64_first(&qcc->streams_by_id);
651 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200652 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200653 node = eb64_next(node);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200654 qcs_free(qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100655 }
656
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200657 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
658 struct quic_frame *frm = LIST_ELEM(&qcc->lfctl.frms, struct quic_frame *, list);
659 LIST_DELETE(&frm->list);
660 pool_free(pool_head_quic_frame, frm);
661 }
662
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200663 pool_free(pool_head_qcc, qcc);
664
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100665 if (conn) {
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100666 LIST_DEL_INIT(&conn->stopping_list);
667
Willy Tarreau784b8682022-04-11 14:18:10 +0200668 conn->handle.qc->conn = NULL;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100669 conn->mux = NULL;
670 conn->ctx = NULL;
671
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100672 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
673
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100674 conn_stop_tracking(conn);
675 conn_full_close(conn);
676 if (conn->destroy_cb)
677 conn->destroy_cb(conn);
678 conn_free(conn);
679 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100680
681 TRACE_LEAVE(QMUX_EV_QCC_END);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100682}
683
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200684/* Transfer as much as possible data on <qcs> from <in> to <out>. <max_data> is
685 * the current flow-control limit on the connection which must not be exceeded.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100686 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200687 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100688 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200689static int qcs_xfer_data(struct qcs *qcs, struct buffer *out,
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200690 struct buffer *in, uint64_t max_data)
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200691{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100692 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200693 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100694 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200695
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100696 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100697
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100698 qc_get_buf(qcs, out);
699
700 /*
701 * QCS out buffer diagram
702 * head left to_xfer
703 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100704 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100705 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100706 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100707 * ^ ack-off ^ sent-off ^ off
708 *
709 * STREAM frame
710 * ^ ^
711 * |xxxxxxxxxxxxxxxxx|
712 */
713
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200714 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100715 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
716
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100717 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200718 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100719
720 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
721 /* do not exceed flow control limit */
722 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
723 to_xfer = qcs->tx.msd - qcs->tx.offset;
724
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100725 BUG_ON_HOT(max_data > qcc->rfctl.md);
726 /* do not overcome flow control limit on connection */
727 if (max_data + to_xfer > qcc->rfctl.md)
728 to_xfer = qcc->rfctl.md - max_data;
729
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100730 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200731 goto out;
732
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200733 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200734
735 out:
736 {
737 struct qcs_xfer_data_trace_arg arg = {
738 .prep = b_data(out), .xfer = total,
739 };
740 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
741 qcc->conn, qcs, &arg);
742 }
743
744 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200745}
746
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200747/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
748 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
749 * stream frame.
750 *
751 * Returns the length of the STREAM frame or a negative error code.
752 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200753static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
754 struct list *frm_list)
755{
756 struct qcc *qcc = qcs->qcc;
757 struct quic_frame *frm;
758 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200759 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200760
761 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
762
Amaury Denoyellea4569202022-04-15 17:29:25 +0200763 /* if ack_offset < buf_offset, it points to an older buffer. */
764 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
765 BUG_ON(qcs->tx.sent_offset < base_off);
766
767 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200768 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200769 BUG_ON(total < 0);
770
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200771 if (!total) {
772 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
773 return 0;
774 }
Amaury Denoyellea4569202022-04-15 17:29:25 +0200775 BUG_ON(qcs->tx.sent_offset >= qcs->tx.offset);
776 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200777
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200778 frm = pool_zalloc(pool_head_quic_frame);
779 if (!frm)
780 goto err;
781
Frédéric Lécailleb9171912022-04-21 17:32:10 +0200782 LIST_INIT(&frm->reflist);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100783 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200784 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200785 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100786 frm->stream.buf = out;
787 frm->stream.data = (unsigned char *)b_peek(out, head);
788
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100789 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200790 if (fin)
791 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100792
793 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200794 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100795 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200796 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100797
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200798 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
799 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200800
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100801 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100802
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200803 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100804 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200805 struct qcs_build_stream_trace_arg arg = {
806 .len = frm->stream.len, .fin = fin,
807 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100808 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200809 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100810 qcc->conn, qcs, &arg);
811 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100812
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200813 return total;
814
815 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100816 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200817 return -1;
818}
819
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100820/* This function must be called by the upper layer to inform about the sending
821 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
822 * <offset>.
823 */
824void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
825{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100826 struct qcc *qcc = qcs->qcc;
827 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100828
829 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200830 BUG_ON(offset >= qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100831
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100832 /* check if the STREAM frame has already been notified. It can happen
833 * for retransmission.
834 */
835 if (offset + data <= qcs->tx.sent_offset)
836 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100837
838 diff = offset + data - qcs->tx.sent_offset;
839
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100840 /* increase offset sum on connection */
841 qcc->tx.sent_offsets += diff;
842 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
843 if (qcc->tx.sent_offsets == qcc->rfctl.md)
844 qcc->flags |= QC_CF_BLK_MFCTL;
845
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100846 /* increase offset on stream */
847 qcs->tx.sent_offset += diff;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100848 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200849 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100850 if (qcs->tx.sent_offset == qcs->tx.msd)
851 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200852
853 if (qcs->tx.offset == qcs->tx.sent_offset && b_full(&qcs->stream->buf->buf)) {
854 qc_stream_buf_release(qcs->stream);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200855 /* prepare qcs for immediate send retry if data to send */
856 if (b_data(&qcs->tx.buf))
857 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200858 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100859}
860
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100861/* Wrapper for send on transport layer. Send a list of frames <frms> for the
862 * connection <qcc>.
863 *
864 * Returns 0 if all data sent with success else non-zero.
865 */
866static int qc_send_frames(struct qcc *qcc, struct list *frms)
867{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100868 /* TODO implement an opportunistic retry mechanism. This is needed
869 * because qc_send_app_pkts is not completed. It will only prepare data
870 * up to its Tx buffer. The frames left are not send even if the Tx
871 * buffer is emptied by the sendto call.
872 *
873 * To overcome this, we call repeatedly qc_send_app_pkts until we
874 * detect that the transport layer has send nothing. This could happen
875 * on congestion or sendto syscall error.
876 *
877 * When qc_send_app_pkts is improved to handle retry by itself, we can
878 * remove the looping from the MUX.
879 */
880 struct quic_frame *first_frm;
881 uint64_t first_offset = 0;
882 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100883
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100884 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
885
886 if (LIST_ISEMPTY(frms)) {
887 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200888 return 1;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100889 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100890
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200891 LIST_INIT(&qcc->send_retry_list);
892
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100893 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100894 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
895 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
896 first_offset = first_frm->stream.offset.key;
897 first_stream_frame_type = 1;
898 }
899 else {
900 first_stream_frame_type = 0;
901 }
902
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100903 if (!LIST_ISEMPTY(frms))
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +0200904 qc_send_app_pkts(qcc->conn->handle.qc, 0, frms);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100905
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100906 /* If there is frames left, check if the transport layer has send some
907 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100908 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100909 if (!LIST_ISEMPTY(frms)) {
910 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
911 goto retry_send;
912
913 /* If the first frame is STREAM, check if its offset has
914 * changed.
915 */
916 if (first_stream_frame_type &&
917 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
918 goto retry_send;
919 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100920 }
921
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100922 /* If there is frames left at this stage, transport layer is blocked.
923 * Subscribe on it to retry later.
924 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100925 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100926 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100927 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
928 SUB_RETRY_SEND, &qcc->wait_event);
929 return 1;
930 }
931
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100932 TRACE_LEAVE(QMUX_EV_QCC_SEND);
933
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100934 return 0;
935}
936
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200937/* Used internally by qc_send function. Proceed to send for <qcs>. This will
938 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
939 * is then generated and inserted in <frms> list. <qcc_max_data> is the current
940 * flow-control max-data at the connection level which must not be surpassed.
941 *
942 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
943 * be null if out buffer cannot be allocated.
944 */
945static int _qc_send_qcs(struct qcs *qcs, struct list *frms,
946 uint64_t qcc_max_data)
947{
948 struct qcc *qcc = qcs->qcc;
949 struct buffer *buf = &qcs->tx.buf;
950 struct buffer *out = qc_stream_buf_get(qcs->stream);
951 int xfer = 0;
952
953 /* Allocate <out> buffer if necessary. */
954 if (!out) {
955 if (qcc->flags & QC_CF_CONN_FULL)
956 return 0;
957
958 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
959 if (!out) {
960 qcc->flags |= QC_CF_CONN_FULL;
961 return 0;
962 }
963 }
964
965 /* Transfer data from <buf> to <out>. */
966 if (b_data(buf)) {
967 xfer = qcs_xfer_data(qcs, out, buf, qcc_max_data);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200968 if (xfer > 0) {
969 qcs_notify_send(qcs);
970 qcs->flags &= ~QC_SF_BLK_MROOM;
971 }
972
973 qcs->tx.offset += xfer;
974 }
975
976 /* out buffer cannot be emptied if qcs offsets differ. */
977 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
978
979 /* Build a new STREAM frame with <out> buffer. */
980 if (qcs->tx.sent_offset != qcs->tx.offset) {
981 int ret;
982 char fin = !!(qcs->flags & QC_SF_FIN_STREAM);
983
984 /* FIN is set if all incoming data were transfered. */
985 fin = !!(fin && !b_data(buf));
986
987 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +0200988 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200989 }
990
991 return xfer;
992}
993
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100994/* Proceed to sending. Loop through all available streams for the <qcc>
995 * instance and try to send as much as possible.
996 *
997 * Returns the total of bytes sent to the transport layer.
998 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100999static int qc_send(struct qcc *qcc)
1000{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001001 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001002 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001003 struct qcs *qcs, *qcs_tmp;
1004 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001005
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001006 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001007
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001008 if (qcc->conn->flags & CO_FL_SOCK_WR_SH) {
1009 qcc->conn->flags |= CO_FL_ERROR;
1010 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
1011 return 0;
1012 }
1013
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001014 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1015 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
1016 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
1017 goto out;
1018 }
1019 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02001020
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001021 if (qcc->flags & QC_CF_BLK_MFCTL)
1022 return 0;
1023
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001024 /* loop through all streams, construct STREAM frames if data available.
1025 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001026 */
1027 node = eb64_first(&qcc->streams_by_id);
1028 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001029 int ret;
1030 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001031
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001032 /* TODO
1033 * for the moment, unidirectional streams have their own
1034 * mechanism for sending. This should be unified in the future,
1035 * in this case the next check will be removed.
1036 */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001037 if (quic_stream_is_uni(qcs->id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001038 node = eb64_next(node);
1039 continue;
1040 }
1041
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001042 if (qcs->flags & QC_SF_BLK_SFCTL) {
1043 node = eb64_next(node);
1044 continue;
1045 }
1046
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001047 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001048 node = eb64_next(node);
1049 continue;
1050 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001051
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001052 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + total);
1053 total += ret;
1054 node = eb64_next(node);
1055 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001056
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001057 if (qc_send_frames(qcc, &frms)) {
1058 /* data rejected by transport layer, do not retry. */
1059 goto out;
1060 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001061
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001062 retry:
1063 tmp_total = 0;
1064 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
1065 int ret;
1066 BUG_ON(!b_data(&qcs->tx.buf));
1067 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001068
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001069 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + tmp_total);
1070 tmp_total += ret;
1071 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001072 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001073
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001074 total += tmp_total;
1075 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
1076 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +01001077
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001078 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001079 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1080
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001081 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001082}
1083
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001084/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1085 * operation.
1086 *
1087 * Returns 0 on success else non-zero.
1088 */
1089static int qc_recv(struct qcc *qcc)
1090{
1091 struct eb64_node *node;
1092 struct qcs *qcs;
1093
1094 node = eb64_first(&qcc->streams_by_id);
1095 while (node) {
1096 qcs = eb64_entry(node, struct qcs, by_id);
1097
1098 /* TODO unidirectional streams have their own mechanism for Rx.
1099 * This should be unified.
1100 */
1101 if (quic_stream_is_uni(qcs->id)) {
1102 node = eb64_next(node);
1103 continue;
1104 }
1105
1106 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
1107 node = eb64_next(node);
1108 continue;
1109 }
1110
1111 qcc_decode_qcs(qcc, qcs);
1112 node = eb64_next(node);
1113 }
1114
1115 return 0;
1116}
1117
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001118/* Release all streams that are already marked as detached. This is only done
1119 * if their TX buffers are empty or if a CONNECTION_CLOSE has been received.
1120 *
1121 * Return the number of released stream.
1122 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001123static int qc_release_detached_streams(struct qcc *qcc)
1124{
1125 struct eb64_node *node;
1126 int release = 0;
1127
1128 node = eb64_first(&qcc->streams_by_id);
1129 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001130 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001131 node = eb64_next(node);
1132
1133 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001134 if (!b_data(&qcs->tx.buf) &&
1135 qcs->tx.offset == qcs->tx.sent_offset) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001136 qcs_destroy(qcs);
1137 release = 1;
1138 }
1139 else {
1140 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1141 SUB_RETRY_SEND, &qcc->wait_event);
1142 }
1143 }
1144 }
1145
1146 return release;
1147}
1148
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001149static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1150{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001151 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001152
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001153 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001154
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001155 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001156
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001157 if (qc_release_detached_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001158 if (qcc_is_dead(qcc)) {
1159 qc_release(qcc);
1160 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001161 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001162 if (qcc_may_expire(qcc))
1163 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1164 else
1165 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001166 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001167 }
1168 }
1169
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001170 qc_recv(qcc);
1171
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001172 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1173
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001174 return NULL;
1175}
1176
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001177static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1178{
1179 struct qcc *qcc = ctx;
1180 int expired = tick_is_expired(t->expire, now_ms);
1181
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001182 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001183
1184 if (qcc) {
1185 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001186 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001187 return t;
1188 }
1189
1190 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001191 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001192 t->expire = TICK_ETERNITY;
1193 return t;
1194 }
1195 }
1196
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001197 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001198
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001199 if (!qcc) {
1200 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001201 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001202 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001203
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001204 qcc->task = NULL;
1205
1206 if (qcc_is_dead(qcc))
1207 qc_release(qcc);
1208
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001209 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1210
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001211 return NULL;
1212}
1213
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001214static int qc_init(struct connection *conn, struct proxy *prx,
1215 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001216{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001217 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001218 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001219
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001220 qcc = pool_alloc(pool_head_qcc);
1221 if (!qcc)
1222 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001223
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001224 qcc->conn = conn;
1225 conn->ctx = qcc;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001226 qcc->nb_cs = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001227 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001228
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001229 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001230
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001231 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001232
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001233 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001234 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001235
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001236 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001237 qcc->tx.sent_offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001238
1239 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001240 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001241 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
1242 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001243 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001244 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001245
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001246 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001247 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
1248 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001249 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001250 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001251
1252 /* Server initiated streams must respect the server flow control. */
1253 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001254 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
1255 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001256 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001257 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1258
1259 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001260 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
1261 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001262 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001263 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001264
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001265 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001266 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02001267 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
1268 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001269 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001270
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001271 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
1272 qcc->lfctl.sent_offsets = 0;
1273
Willy Tarreau784b8682022-04-11 14:18:10 +02001274 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001275 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001276 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1277 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1278
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001279 qcc->wait_event.tasklet = tasklet_new();
1280 if (!qcc->wait_event.tasklet)
1281 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001282
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001283 LIST_INIT(&qcc->send_retry_list);
1284
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001285 qcc->subs = NULL;
1286 qcc->wait_event.tasklet->process = qc_io_cb;
1287 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001288 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001289
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001290 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001291 qcc->task = NULL;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001292 qcc->timeout = prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001293 if (tick_isset(qcc->timeout)) {
1294 qcc->task = task_new_here();
1295 if (!qcc->task)
1296 goto fail_no_timeout_task;
1297 qcc->task->process = qc_timeout_task;
1298 qcc->task->context = qcc;
1299 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1300 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001301
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001302 if (!conn_is_back(conn)) {
1303 if (!LIST_INLIST(&conn->stopping_list)) {
1304 LIST_APPEND(&mux_stopping_data[tid].list,
1305 &conn->stopping_list);
1306 }
1307 }
1308
Willy Tarreau784b8682022-04-11 14:18:10 +02001309 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001310 /* init read cycle */
1311 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001312
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001313 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001314
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001315 fail_no_timeout_task:
1316 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001317 fail_no_tasklet:
1318 pool_free(pool_head_qcc, qcc);
1319 fail_no_qcc:
1320 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001321}
1322
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001323static void qc_destroy(void *ctx)
1324{
1325 struct qcc *qcc = ctx;
1326
1327 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
1328 qc_release(qcc);
1329 TRACE_LEAVE(QMUX_EV_QCC_END);
1330}
1331
Willy Tarreau4201ab72022-05-10 19:18:52 +02001332static void qc_detach(struct cs_endpoint *endp)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001333{
Willy Tarreaudf943132022-05-13 16:31:23 +02001334 struct qcs *qcs = endp->target;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001335 struct qcc *qcc = qcs->qcc;
1336
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001337 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001338
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001339 --qcc->nb_cs;
1340
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001341 if ((b_data(&qcs->tx.buf) || qcs->tx.offset > qcs->tx.sent_offset) &&
1342 !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001343 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001344 qcs->flags |= QC_SF_DETACH;
1345 return;
1346 }
1347
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001348 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001349
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001350 if (qcc_is_dead(qcc)) {
1351 qc_release(qcc);
1352 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001353 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001354 if (qcc_may_expire(qcc))
1355 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1356 else
1357 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001358 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001359 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001360
1361 TRACE_LEAVE(QMUX_EV_STRM_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001362}
1363
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001364/* Called from the upper layer, to receive data */
1365static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf,
1366 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001367{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001368 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001369 struct htx *qcs_htx = NULL;
1370 struct htx *cs_htx = NULL;
1371 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001372 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001373
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001374 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001375
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001376 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
1377 if (htx_is_empty(qcs_htx)) {
1378 /* Set buffer data to 0 as HTX is empty. */
1379 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1380 goto end;
1381 }
1382
1383 ret = qcs_htx->data;
1384
1385 cs_htx = htx_from_buf(buf);
1386 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
1387 htx_to_buf(cs_htx, buf);
1388 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1389 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
1390 goto end;
1391 }
1392
1393 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
1394 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
1395
1396 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001397 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
1398 cs_htx->flags |= HTX_FL_EOM;
1399 fin = 1;
1400 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001401
1402 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
1403 htx_to_buf(cs_htx, buf);
1404 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1405 ret -= qcs_htx->data;
1406
1407 end:
1408 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreau15b07212022-05-10 11:24:26 +02001409 qcs->endp->flags |= (CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001410 }
1411 else {
Willy Tarreau15b07212022-05-10 11:24:26 +02001412 qcs->endp->flags &= ~(CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
1413 if (qcs->endp->flags & CS_EP_ERR_PENDING)
1414 qcs->endp->flags |= CS_EP_ERROR;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001415
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001416 if (fin)
Willy Tarreau15b07212022-05-10 11:24:26 +02001417 qcs->endp->flags |= CS_EP_EOI;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001418
1419 if (b_size(&qcs->rx.app_buf)) {
1420 b_free(&qcs->rx.app_buf);
1421 offer_buffers(NULL, 1);
1422 }
1423 }
1424
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001425 if (ret) {
1426 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001427 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001428 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001429
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001430 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
1431
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001432 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001433}
1434
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001435static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf,
1436 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001437{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001438 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001439 size_t ret;
1440
1441 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001442
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001443 ret = qcs->qcc->app_ops->snd_buf(cs, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001444
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001445 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
1446
1447 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001448}
1449
1450/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1451 * event subscriber <es> is not allowed to change from a previous call as long
1452 * as at least one event is still subscribed. The <event_type> must only be a
1453 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1454 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001455static int qc_subscribe(struct conn_stream *cs, int event_type,
1456 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001457{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001458 return qcs_subscribe(__cs_mux(cs), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001459}
1460
1461/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1462 * The <es> pointer is not allowed to differ from the one passed to the
1463 * subscribe() call. It always returns zero.
1464 */
1465static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
1466{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001467 struct qcs *qcs = __cs_mux(cs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001468
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001469 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1470 BUG_ON(qcs->subs && qcs->subs != es);
1471
1472 es->events &= ~event_type;
1473 if (!es->events)
1474 qcs->subs = NULL;
1475
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001476 return 0;
1477}
1478
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001479/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Christopher Fauletb041b232022-03-24 10:27:02 +01001480 * report CS_EP_ERR_PENDING|CS_EP_ERROR on the attached conn-streams and wake
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001481 * them.
1482 */
1483static int qc_wake_some_streams(struct qcc *qcc)
1484{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001485 struct qcs *qcs;
1486 struct eb64_node *node;
1487
1488 for (node = eb64_first(&qcc->streams_by_id); node;
1489 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001490 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001491
Willy Tarreau01c2a4a2022-05-10 15:46:10 +02001492 if (!qcs->endp->cs)
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001493 continue;
1494
1495 if (qcc->conn->flags & CO_FL_ERROR) {
Christopher Fauletb041b232022-03-24 10:27:02 +01001496 qcs->endp->flags |= CS_EP_ERR_PENDING;
1497 if (qcs->endp->flags & CS_EP_EOS)
1498 qcs->endp->flags |= CS_EP_ERROR;
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001499
1500 if (qcs->subs) {
1501 qcs_notify_recv(qcs);
1502 qcs_notify_send(qcs);
1503 }
Willy Tarreau01c2a4a2022-05-10 15:46:10 +02001504 else if (qcs->endp->cs->data_cb->wake) {
1505 qcs->endp->cs->data_cb->wake(qcs->endp->cs);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001506 }
1507 }
1508 }
1509
1510 return 0;
1511}
1512
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001513static int qc_wake(struct connection *conn)
1514{
1515 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02001516 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001517
1518 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001519
1520 /* Check if a soft-stop is in progress.
1521 * Release idling front connection if this is the case.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001522 *
1523 * TODO this is revelant for frontend connections only.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001524 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001525 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
1526 goto release;
1527
Willy Tarreau784b8682022-04-11 14:18:10 +02001528 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02001529 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
1530
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001531 qc_send(qcc);
1532
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001533 qc_wake_some_streams(qcc);
1534
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001535 if (qcc_is_dead(qcc))
1536 goto release;
1537
1538 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
1539
1540 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001541
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001542 release:
1543 qc_release(qcc);
1544 TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001545 return 1;
1546}
1547
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001548
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001549static void qmux_trace_frm(const struct quic_frame *frm)
1550{
1551 switch (frm->type) {
1552 case QUIC_FT_MAX_STREAMS_BIDI:
1553 chunk_appendf(&trace_buf, " max_streams=%lu",
1554 frm->max_streams_bidi.max_streams);
1555 break;
1556
1557 case QUIC_FT_MAX_STREAMS_UNI:
1558 chunk_appendf(&trace_buf, " max_streams=%lu",
1559 frm->max_streams_uni.max_streams);
1560 break;
1561
1562 default:
1563 break;
1564 }
1565}
1566
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001567/* quic-mux trace handler */
1568static void qmux_trace(enum trace_level level, uint64_t mask,
1569 const struct trace_source *src,
1570 const struct ist where, const struct ist func,
1571 const void *a1, const void *a2, const void *a3, const void *a4)
1572{
1573 const struct connection *conn = a1;
1574 const struct qcc *qcc = conn ? conn->ctx : NULL;
1575 const struct qcs *qcs = a2;
1576
1577 if (!qcc)
1578 return;
1579
1580 if (src->verbosity > QMUX_VERB_CLEAN) {
1581 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
1582
1583 if (qcs)
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001584 chunk_appendf(&trace_buf, " qcs=%p(%lu)", qcs, qcs->id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001585
1586 if (mask & QMUX_EV_QCC_NQCS) {
1587 const uint64_t *id = a3;
1588 chunk_appendf(&trace_buf, " id=%lu", *id);
1589 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001590
1591 if (mask & QMUX_EV_SEND_FRM)
1592 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001593
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001594 if (mask & QMUX_EV_QCS_XFER_DATA) {
1595 const struct qcs_xfer_data_trace_arg *arg = a3;
1596 chunk_appendf(&trace_buf, " prep=%lu xfer=%d",
1597 arg->prep, arg->xfer);
1598 }
1599
1600 if (mask & QMUX_EV_QCS_BUILD_STRM) {
1601 const struct qcs_build_stream_trace_arg *arg = a3;
1602 chunk_appendf(&trace_buf, " len=%lu fin=%d offset=%lu",
1603 arg->len, arg->fin, arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001604 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001605 }
1606}
1607
Amaury Denoyelle251eadf2022-03-24 17:14:52 +01001608/* Function to automatically activate QUIC MUX traces on stdout.
1609 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
1610 * Main use for now is in the docker image for QUIC interop testing.
1611 */
1612static void qmux_init_stdout_traces(void)
1613{
1614#ifdef ENABLE_QUIC_STDOUT_TRACES
1615 trace_qmux.sink = sink_find("stdout");
1616 trace_qmux.level = TRACE_LEVEL_DEVELOPER;
1617 trace_qmux.state = TRACE_STATE_RUNNING;
1618 trace_qmux.verbosity = QMUX_VERB_MINIMAL;
1619#endif
1620}
1621INITCALL0(STG_INIT, qmux_init_stdout_traces);
1622
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001623
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001624static const struct mux_ops qc_ops = {
1625 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001626 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001627 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001628 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001629 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001630 .subscribe = qc_subscribe,
1631 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001632 .wake = qc_wake,
Christopher Fauleta97cced2022-04-12 18:04:10 +02001633 .flags = MX_FL_HTX|MX_FL_NO_UPG,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02001634 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001635};
1636
1637static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001638 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001639
1640INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);