blob: 68b2bd8a587ca1190ebd78226312dce1ddb0ffa7 [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
192 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
193 b_free(&buf);
194
195 *ncbuf = NCBUF_NULL;
196}
197
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200198/* Free a qcs. This function must only be done to remove a stream on allocation
199 * error or connection shutdown. Else use qcs_destroy which handle all the
200 * QUIC connection mechanism.
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100201 */
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200202void qcs_free(struct qcs *qcs)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100203{
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200204 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200205 b_free(&qcs->tx.buf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200206
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200207 BUG_ON(!qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams);
208 --qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams;
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200209
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200210 if (qcs->ctx && qcs->qcc->app_ops->detach)
211 qcs->qcc->app_ops->detach(qcs);
212
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200213 qc_stream_desc_release(qcs->stream);
214
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100215 BUG_ON(qcs->endp && !(qcs->endp->flags & CS_EP_ORPHAN));
216 cs_endpoint_free(qcs->endp);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200217
218 eb64_delete(&qcs->by_id);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100219 pool_free(pool_head_qcs, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100220}
221
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100222struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100223{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100224 struct buffer *buf = b_alloc(bptr);
225 BUG_ON(!buf);
226 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100227}
228
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200229struct ncbuf *qc_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
230{
231 struct buffer buf = BUF_NULL;
232
233 if (ncb_is_null(ncbuf)) {
234 b_alloc(&buf);
235 BUG_ON(b_is_null(&buf));
236
237 *ncbuf = ncb_make(buf.area, buf.size, 0);
238 ncb_init(ncbuf, 0);
239 }
240
241 return ncbuf;
242}
243
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100244int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
245{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100246 struct qcc *qcc = qcs->qcc;
247
248 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100249
250 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
251 BUG_ON(qcs->subs && qcs->subs != es);
252
253 es->events |= event_type;
254 qcs->subs = es;
255
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100256 if (event_type & SUB_RETRY_RECV)
257 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
258
259 if (event_type & SUB_RETRY_SEND)
260 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
261
262 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
263
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100264 return 0;
265}
266
267void qcs_notify_recv(struct qcs *qcs)
268{
269 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
270 tasklet_wakeup(qcs->subs->tasklet);
271 qcs->subs->events &= ~SUB_RETRY_RECV;
272 if (!qcs->subs->events)
273 qcs->subs = NULL;
274 }
275}
276
277void qcs_notify_send(struct qcs *qcs)
278{
279 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
280 tasklet_wakeup(qcs->subs->tasklet);
281 qcs->subs->events &= ~SUB_RETRY_SEND;
282 if (!qcs->subs->events)
283 qcs->subs = NULL;
284 }
285}
286
Amaury Denoyellea9773552022-05-16 14:38:25 +0200287/* Remove <bytes> from <qcs> Rx buffer. This must be called by transcoders
288 * after STREAM parsing. Flow-control for received offsets may be allocated for
289 * the peer if needed.
290 */
291void qcs_consume(struct qcs *qcs, uint64_t bytes)
292{
293 struct qcc *qcc = qcs->qcc;
294 struct quic_frame *frm;
295 enum ncb_ret ret;
296
297 ret = ncb_advance(&qcs->rx.ncbuf, bytes);
298 if (ret) {
299 ABORT_NOW(); /* should not happens because removal only in data */
300 }
301
302 qcs->rx.offset += bytes;
303 if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) {
304 frm = pool_zalloc(pool_head_quic_frame);
305 BUG_ON(!frm); /* TODO handle this properly */
306
307 qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init;
308
309 LIST_INIT(&frm->reflist);
310 frm->type = QUIC_FT_MAX_STREAM_DATA;
311 frm->max_stream_data.id = qcs->id;
312 frm->max_stream_data.max_stream_data = qcs->rx.msd;
313
314 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
315 tasklet_wakeup(qcc->wait_event.tasklet);
316 }
Amaury Denoyellec830e1e2022-05-16 16:19:59 +0200317
318 qcc->lfctl.sent_offsets += bytes;
319 if (qcc->lfctl.md - qcc->lfctl.sent_offsets < qcc->lfctl.md_init / 2) {
320 frm = pool_zalloc(pool_head_quic_frame);
321 BUG_ON(!frm); /* TODO handle this properly */
322
323 qcc->lfctl.md = qcc->lfctl.sent_offsets + qcc->lfctl.md_init;
324
325 LIST_INIT(&frm->reflist);
326 frm->type = QUIC_FT_MAX_DATA;
327 frm->max_data.max_data = qcc->lfctl.md;
328
329 LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list);
330 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
331 }
Amaury Denoyellea9773552022-05-16 14:38:25 +0200332}
333
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100334/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
335 * several streams, depending on the already open ones.
336 * Return this node if succeeded, NULL if not.
337 */
Amaury Denoyelle50742292022-03-29 14:57:19 +0200338struct qcs *qcc_get_qcs(struct qcc *qcc, uint64_t id)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100339{
340 unsigned int strm_type;
341 int64_t sub_id;
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200342 struct eb64_node *node;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200343 struct qcs *qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100344
345 strm_type = id & QCS_ID_TYPE_MASK;
346 sub_id = id >> QCS_ID_TYPE_SHIFT;
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200347 node = NULL;
Amaury Denoyelle0dc40f02022-02-07 11:44:17 +0100348 if (quic_stream_is_local(qcc, id)) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100349 /* Local streams: this stream must be already opened. */
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200350 node = eb64_lookup(&qcc->streams_by_id, id);
351 if (!node) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100352 /* unknown stream id */
353 goto out;
354 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200355 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100356 }
357 else {
358 /* Remote streams. */
359 struct eb_root *strms;
360 uint64_t largest_id;
361 enum qcs_type qcs_type;
362
363 strms = &qcc->streams_by_id;
364 qcs_type = qcs_id_type(id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100365
366 /* TODO also checks max-streams for uni streams */
367 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100368 if (sub_id + 1 > qcc->lfctl.ms_bidi) {
Amaury Denoyellec055e302022-02-07 16:09:06 +0100369 /* streams limit reached */
370 goto out;
371 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100372 }
373
374 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
375 * correct value.
376 */
377 largest_id = qcc->strms[qcs_type].largest_id;
378 if (sub_id > (int64_t)largest_id) {
379 /* RFC: "A stream ID that is used out of order results in all streams
380 * of that type with lower-numbered stream IDs also being opened".
381 * So, let's "open" these streams.
382 */
383 int64_t i;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200384 struct qcs *tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100385
Amaury Denoyelle50742292022-03-29 14:57:19 +0200386 tmp_qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100387 for (i = largest_id + 1; i <= sub_id; i++) {
388 uint64_t id = (i << QCS_ID_TYPE_SHIFT) | strm_type;
389 enum qcs_type type = id & QCS_ID_DIR_BIT ? QCS_CLT_UNI : QCS_CLT_BIDI;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200390
Amaury Denoyelle50742292022-03-29 14:57:19 +0200391 tmp_qcs = qcs_new(qcc, id, type);
392 if (!tmp_qcs) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100393 /* allocation failure */
394 goto out;
395 }
396
397 qcc->strms[qcs_type].largest_id = i;
398 }
Amaury Denoyelle50742292022-03-29 14:57:19 +0200399 if (tmp_qcs)
400 qcs = tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100401 }
402 else {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200403 node = eb64_lookup(strms, id);
404 if (node)
405 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100406 }
407 }
408
Amaury Denoyelle50742292022-03-29 14:57:19 +0200409 return qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100410
411 out:
412 return NULL;
413}
414
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200415/* Decode the content of STREAM frames already received on the stream instance
416 * <qcs>.
417 *
418 * Returns 0 on success else non-zero.
419 */
420static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
421{
422 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
423
424 if (qcc->app_ops->decode_qcs(qcs, qcs->flags & QC_SF_FIN_RECV, qcc->ctx) < 0) {
425 TRACE_DEVEL("leaving on decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
426 return 1;
427 }
428
429 qcs_notify_recv(qcs);
430
431 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
432
433 return 0;
434}
435
436/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
437 * <data> with length <len> and represents the offset <offset>. <fin> is set if
438 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100439 *
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200440 * Returns 0 on success else non-zero.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100441 */
442int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200443 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100444{
445 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200446 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100447
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100448 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
449
Amaury Denoyelle50742292022-03-29 14:57:19 +0200450 qcs = qcc_get_qcs(qcc, id);
451 if (!qcs) {
Frédéric Lécailleb57de072022-05-02 18:58:27 +0200452 if ((id >> QCS_ID_TYPE_SHIFT) <= qcc->strms[qcs_id_type(id)].largest_id) {
453 TRACE_DEVEL("already released stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
454 return 0;
455 }
456 else {
457 TRACE_DEVEL("leaving on stream not found", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
458 return 1;
459 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100460 }
461
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100462 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100463 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 +0200464 return 0;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100465 }
466
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200467 /* TODO if last frame already received, stream size must not change.
468 * Else send FINAL_SIZE_ERROR.
469 */
470
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200471 /* TODO initial max-stream-data overflow. Implement FLOW_CONTROL_ERROR emission. */
472 BUG_ON(offset + len > qcs->rx.msd);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100473
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200474 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100475 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200476 ABORT_NOW();
477 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100478 }
479
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100480 TRACE_DEVEL("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200481 if (offset < qcs->rx.offset) {
482 len -= qcs->rx.offset - offset;
483 offset = qcs->rx.offset;
484 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100485
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200486 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
487 if (ret != NCB_RET_OK) {
488 if (ret == NCB_RET_DATA_REJ) {
489 /* TODO generate PROTOCOL_VIOLATION error */
490 TRACE_DEVEL("leaving on data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
491 qcc->conn, qcs);
492 }
493 else if (ret == NCB_RET_GAP_SIZE) {
494 TRACE_DEVEL("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
495 qcc->conn, qcs);
496 }
497 return 1;
498 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100499
500 if (fin)
501 qcs->flags |= QC_SF_FIN_RECV;
502
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200503 if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL))
504 qcc_decode_qcs(qcc, qcs);
505
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100506 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100507 return 0;
508}
509
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100510/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
511 * the frame.
512 *
513 * Returns 0 on success else non-zero.
514 */
515int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
516{
517 if (qcc->rfctl.md < max) {
518 qcc->rfctl.md = max;
519
520 if (qcc->flags & QC_CF_BLK_MFCTL) {
521 qcc->flags &= ~QC_CF_BLK_MFCTL;
522 tasklet_wakeup(qcc->wait_event.tasklet);
523 }
524 }
525 return 0;
526}
527
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100528/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
529 * field of the frame and <id> is the identifier of the QUIC stream.
530 *
531 * Returns 0 on success else non-zero.
532 */
533int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
534{
535 struct qcs *qcs;
536 struct eb64_node *node;
537
538 node = eb64_lookup(&qcc->streams_by_id, id);
539 if (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200540 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100541 if (max > qcs->tx.msd) {
542 qcs->tx.msd = max;
543
544 if (qcs->flags & QC_SF_BLK_SFCTL) {
545 qcs->flags &= ~QC_SF_BLK_SFCTL;
546 tasklet_wakeup(qcc->wait_event.tasklet);
547 }
548 }
549 }
550
551 return 0;
552}
553
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200554/* Signal the closing of remote stream with id <id>. Flow-control for new
555 * streams may be allocated for the peer if needed.
556 */
557static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +0100558{
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200559 struct quic_frame *frm;
560
561 if (quic_stream_is_bidi(id)) {
562 ++qcc->lfctl.cl_bidi_r;
563 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
564 frm = pool_zalloc(pool_head_quic_frame);
565 BUG_ON(!frm); /* TODO handle this properly */
566
567 LIST_INIT(&frm->reflist);
568 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
569 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
570 qcc->lfctl.cl_bidi_r;
571 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
572 tasklet_wakeup(qcc->wait_event.tasklet);
573
574 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
575 qcc->lfctl.cl_bidi_r = 0;
576 }
577 }
578 else {
579 /* TODO */
580 }
581
582 return 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100583}
584
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500585/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100586static void qcs_destroy(struct qcs *qcs)
587{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100588 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200589 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100590
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100591 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100592
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200593 if (quic_stream_is_remote(qcs->qcc, id))
594 qcc_release_remote_stream(qcs->qcc, id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100595
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200596 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100597
598 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100599}
600
601static inline int qcc_is_dead(const struct qcc *qcc)
602{
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200603 if (qcc->app_ops && qcc->app_ops->is_active &&
604 qcc->app_ops->is_active(qcc, qcc->ctx))
605 return 0;
606
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200607 if ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task)
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100608 return 1;
609
610 return 0;
611}
612
613/* Return true if the mux timeout should be armed. */
614static inline int qcc_may_expire(struct qcc *qcc)
615{
Amaury Denoyelle06890aa2022-04-04 16:15:06 +0200616 return !qcc->nb_cs;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100617}
618
619/* release function. This one should be called to free all resources allocated
620 * to the mux.
621 */
622static void qc_release(struct qcc *qcc)
623{
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200624 struct connection *conn = qcc->conn;
625 struct eb64_node *node;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100626
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100627 TRACE_ENTER(QMUX_EV_QCC_END);
628
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200629 if (qcc->app_ops && qcc->app_ops->release)
630 qcc->app_ops->release(qcc->ctx);
Amaury Denoyellef8909452022-03-30 11:51:56 +0200631
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200632 if (qcc->task) {
633 task_destroy(qcc->task);
634 qcc->task = NULL;
635 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100636
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200637 if (qcc->wait_event.tasklet)
638 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyellef3e03a42022-04-21 15:41:34 +0200639 if (conn && qcc->wait_event.events) {
640 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
641 qcc->wait_event.events,
642 &qcc->wait_event);
643 }
Amaury Denoyellef8909452022-03-30 11:51:56 +0200644
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200645 /* liberate remaining qcs instances */
646 node = eb64_first(&qcc->streams_by_id);
647 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200648 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200649 node = eb64_next(node);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200650 qcs_free(qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100651 }
652
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200653 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
654 struct quic_frame *frm = LIST_ELEM(&qcc->lfctl.frms, struct quic_frame *, list);
655 LIST_DELETE(&frm->list);
656 pool_free(pool_head_quic_frame, frm);
657 }
658
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200659 pool_free(pool_head_qcc, qcc);
660
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100661 if (conn) {
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100662 LIST_DEL_INIT(&conn->stopping_list);
663
Willy Tarreau784b8682022-04-11 14:18:10 +0200664 conn->handle.qc->conn = NULL;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100665 conn->mux = NULL;
666 conn->ctx = NULL;
667
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100668 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
669
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100670 conn_stop_tracking(conn);
671 conn_full_close(conn);
672 if (conn->destroy_cb)
673 conn->destroy_cb(conn);
674 conn_free(conn);
675 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100676
677 TRACE_LEAVE(QMUX_EV_QCC_END);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100678}
679
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200680/* Transfer as much as possible data on <qcs> from <in> to <out>. <max_data> is
681 * the current flow-control limit on the connection which must not be exceeded.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100682 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200683 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100684 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200685static int qcs_xfer_data(struct qcs *qcs, struct buffer *out,
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200686 struct buffer *in, uint64_t max_data)
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200687{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100688 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200689 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100690 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200691
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100692 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100693
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100694 qc_get_buf(qcs, out);
695
696 /*
697 * QCS out buffer diagram
698 * head left to_xfer
699 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100700 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100701 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100702 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100703 * ^ ack-off ^ sent-off ^ off
704 *
705 * STREAM frame
706 * ^ ^
707 * |xxxxxxxxxxxxxxxxx|
708 */
709
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200710 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100711 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
712
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100713 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200714 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100715
716 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
717 /* do not exceed flow control limit */
718 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
719 to_xfer = qcs->tx.msd - qcs->tx.offset;
720
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100721 BUG_ON_HOT(max_data > qcc->rfctl.md);
722 /* do not overcome flow control limit on connection */
723 if (max_data + to_xfer > qcc->rfctl.md)
724 to_xfer = qcc->rfctl.md - max_data;
725
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100726 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200727 goto out;
728
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200729 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200730
731 out:
732 {
733 struct qcs_xfer_data_trace_arg arg = {
734 .prep = b_data(out), .xfer = total,
735 };
736 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
737 qcc->conn, qcs, &arg);
738 }
739
740 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200741}
742
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200743/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
744 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
745 * stream frame.
746 *
747 * Returns the length of the STREAM frame or a negative error code.
748 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200749static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
750 struct list *frm_list)
751{
752 struct qcc *qcc = qcs->qcc;
753 struct quic_frame *frm;
754 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200755 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200756
757 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
758
Amaury Denoyellea4569202022-04-15 17:29:25 +0200759 /* if ack_offset < buf_offset, it points to an older buffer. */
760 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
761 BUG_ON(qcs->tx.sent_offset < base_off);
762
763 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200764 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200765 BUG_ON(total < 0);
766
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200767 if (!total) {
768 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
769 return 0;
770 }
Amaury Denoyellea4569202022-04-15 17:29:25 +0200771 BUG_ON(qcs->tx.sent_offset >= qcs->tx.offset);
772 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200773
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200774 frm = pool_zalloc(pool_head_quic_frame);
775 if (!frm)
776 goto err;
777
Frédéric Lécailleb9171912022-04-21 17:32:10 +0200778 LIST_INIT(&frm->reflist);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100779 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200780 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200781 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100782 frm->stream.buf = out;
783 frm->stream.data = (unsigned char *)b_peek(out, head);
784
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100785 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200786 if (fin)
787 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100788
789 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200790 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100791 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200792 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100793
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200794 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
795 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200796
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100797 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100798
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200799 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100800 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200801 struct qcs_build_stream_trace_arg arg = {
802 .len = frm->stream.len, .fin = fin,
803 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100804 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200805 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100806 qcc->conn, qcs, &arg);
807 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100808
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200809 return total;
810
811 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100812 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200813 return -1;
814}
815
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100816/* This function must be called by the upper layer to inform about the sending
817 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
818 * <offset>.
819 */
820void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
821{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100822 struct qcc *qcc = qcs->qcc;
823 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100824
825 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200826 BUG_ON(offset >= qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100827
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100828 /* check if the STREAM frame has already been notified. It can happen
829 * for retransmission.
830 */
831 if (offset + data <= qcs->tx.sent_offset)
832 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100833
834 diff = offset + data - qcs->tx.sent_offset;
835
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100836 /* increase offset sum on connection */
837 qcc->tx.sent_offsets += diff;
838 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
839 if (qcc->tx.sent_offsets == qcc->rfctl.md)
840 qcc->flags |= QC_CF_BLK_MFCTL;
841
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100842 /* increase offset on stream */
843 qcs->tx.sent_offset += diff;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100844 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200845 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100846 if (qcs->tx.sent_offset == qcs->tx.msd)
847 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200848
849 if (qcs->tx.offset == qcs->tx.sent_offset && b_full(&qcs->stream->buf->buf)) {
850 qc_stream_buf_release(qcs->stream);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200851 /* prepare qcs for immediate send retry if data to send */
852 if (b_data(&qcs->tx.buf))
853 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200854 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100855}
856
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100857/* Wrapper for send on transport layer. Send a list of frames <frms> for the
858 * connection <qcc>.
859 *
860 * Returns 0 if all data sent with success else non-zero.
861 */
862static int qc_send_frames(struct qcc *qcc, struct list *frms)
863{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100864 /* TODO implement an opportunistic retry mechanism. This is needed
865 * because qc_send_app_pkts is not completed. It will only prepare data
866 * up to its Tx buffer. The frames left are not send even if the Tx
867 * buffer is emptied by the sendto call.
868 *
869 * To overcome this, we call repeatedly qc_send_app_pkts until we
870 * detect that the transport layer has send nothing. This could happen
871 * on congestion or sendto syscall error.
872 *
873 * When qc_send_app_pkts is improved to handle retry by itself, we can
874 * remove the looping from the MUX.
875 */
876 struct quic_frame *first_frm;
877 uint64_t first_offset = 0;
878 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100879
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100880 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
881
882 if (LIST_ISEMPTY(frms)) {
883 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200884 return 1;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100885 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100886
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200887 LIST_INIT(&qcc->send_retry_list);
888
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100889 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100890 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
891 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
892 first_offset = first_frm->stream.offset.key;
893 first_stream_frame_type = 1;
894 }
895 else {
896 first_stream_frame_type = 0;
897 }
898
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100899 if (!LIST_ISEMPTY(frms))
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +0200900 qc_send_app_pkts(qcc->conn->handle.qc, 0, frms);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100901
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100902 /* If there is frames left, check if the transport layer has send some
903 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100904 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100905 if (!LIST_ISEMPTY(frms)) {
906 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
907 goto retry_send;
908
909 /* If the first frame is STREAM, check if its offset has
910 * changed.
911 */
912 if (first_stream_frame_type &&
913 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
914 goto retry_send;
915 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100916 }
917
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100918 /* If there is frames left at this stage, transport layer is blocked.
919 * Subscribe on it to retry later.
920 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100921 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100922 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100923 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
924 SUB_RETRY_SEND, &qcc->wait_event);
925 return 1;
926 }
927
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100928 TRACE_LEAVE(QMUX_EV_QCC_SEND);
929
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100930 return 0;
931}
932
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200933/* Used internally by qc_send function. Proceed to send for <qcs>. This will
934 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
935 * is then generated and inserted in <frms> list. <qcc_max_data> is the current
936 * flow-control max-data at the connection level which must not be surpassed.
937 *
938 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
939 * be null if out buffer cannot be allocated.
940 */
941static int _qc_send_qcs(struct qcs *qcs, struct list *frms,
942 uint64_t qcc_max_data)
943{
944 struct qcc *qcc = qcs->qcc;
945 struct buffer *buf = &qcs->tx.buf;
946 struct buffer *out = qc_stream_buf_get(qcs->stream);
947 int xfer = 0;
948
949 /* Allocate <out> buffer if necessary. */
950 if (!out) {
951 if (qcc->flags & QC_CF_CONN_FULL)
952 return 0;
953
954 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
955 if (!out) {
956 qcc->flags |= QC_CF_CONN_FULL;
957 return 0;
958 }
959 }
960
961 /* Transfer data from <buf> to <out>. */
962 if (b_data(buf)) {
963 xfer = qcs_xfer_data(qcs, out, buf, qcc_max_data);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200964 if (xfer > 0) {
965 qcs_notify_send(qcs);
966 qcs->flags &= ~QC_SF_BLK_MROOM;
967 }
968
969 qcs->tx.offset += xfer;
970 }
971
972 /* out buffer cannot be emptied if qcs offsets differ. */
973 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
974
975 /* Build a new STREAM frame with <out> buffer. */
976 if (qcs->tx.sent_offset != qcs->tx.offset) {
977 int ret;
978 char fin = !!(qcs->flags & QC_SF_FIN_STREAM);
979
980 /* FIN is set if all incoming data were transfered. */
981 fin = !!(fin && !b_data(buf));
982
983 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +0200984 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200985 }
986
987 return xfer;
988}
989
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100990/* Proceed to sending. Loop through all available streams for the <qcc>
991 * instance and try to send as much as possible.
992 *
993 * Returns the total of bytes sent to the transport layer.
994 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100995static int qc_send(struct qcc *qcc)
996{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100997 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200998 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200999 struct qcs *qcs, *qcs_tmp;
1000 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001001
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001002 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001003
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001004 if (qcc->conn->flags & CO_FL_SOCK_WR_SH) {
1005 qcc->conn->flags |= CO_FL_ERROR;
1006 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
1007 return 0;
1008 }
1009
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001010 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1011 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
1012 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
1013 goto out;
1014 }
1015 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02001016
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001017 if (qcc->flags & QC_CF_BLK_MFCTL)
1018 return 0;
1019
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001020 /* loop through all streams, construct STREAM frames if data available.
1021 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001022 */
1023 node = eb64_first(&qcc->streams_by_id);
1024 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001025 int ret;
1026 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001027
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001028 /* TODO
1029 * for the moment, unidirectional streams have their own
1030 * mechanism for sending. This should be unified in the future,
1031 * in this case the next check will be removed.
1032 */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001033 if (quic_stream_is_uni(qcs->id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001034 node = eb64_next(node);
1035 continue;
1036 }
1037
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001038 if (qcs->flags & QC_SF_BLK_SFCTL) {
1039 node = eb64_next(node);
1040 continue;
1041 }
1042
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001043 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001044 node = eb64_next(node);
1045 continue;
1046 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001047
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001048 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + total);
1049 total += ret;
1050 node = eb64_next(node);
1051 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001052
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001053 if (qc_send_frames(qcc, &frms)) {
1054 /* data rejected by transport layer, do not retry. */
1055 goto out;
1056 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001057
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001058 retry:
1059 tmp_total = 0;
1060 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
1061 int ret;
1062 BUG_ON(!b_data(&qcs->tx.buf));
1063 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001064
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001065 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + tmp_total);
1066 tmp_total += ret;
1067 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001068 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001069
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001070 total += tmp_total;
1071 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
1072 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +01001073
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001074 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001075 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1076
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001077 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001078}
1079
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001080/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1081 * operation.
1082 *
1083 * Returns 0 on success else non-zero.
1084 */
1085static int qc_recv(struct qcc *qcc)
1086{
1087 struct eb64_node *node;
1088 struct qcs *qcs;
1089
1090 node = eb64_first(&qcc->streams_by_id);
1091 while (node) {
1092 qcs = eb64_entry(node, struct qcs, by_id);
1093
1094 /* TODO unidirectional streams have their own mechanism for Rx.
1095 * This should be unified.
1096 */
1097 if (quic_stream_is_uni(qcs->id)) {
1098 node = eb64_next(node);
1099 continue;
1100 }
1101
1102 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
1103 node = eb64_next(node);
1104 continue;
1105 }
1106
1107 qcc_decode_qcs(qcc, qcs);
1108 node = eb64_next(node);
1109 }
1110
1111 return 0;
1112}
1113
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001114/* Release all streams that are already marked as detached. This is only done
1115 * if their TX buffers are empty or if a CONNECTION_CLOSE has been received.
1116 *
1117 * Return the number of released stream.
1118 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001119static int qc_release_detached_streams(struct qcc *qcc)
1120{
1121 struct eb64_node *node;
1122 int release = 0;
1123
1124 node = eb64_first(&qcc->streams_by_id);
1125 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001126 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001127 node = eb64_next(node);
1128
1129 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001130 if (!b_data(&qcs->tx.buf) &&
1131 qcs->tx.offset == qcs->tx.sent_offset) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001132 qcs_destroy(qcs);
1133 release = 1;
1134 }
1135 else {
1136 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1137 SUB_RETRY_SEND, &qcc->wait_event);
1138 }
1139 }
1140 }
1141
1142 return release;
1143}
1144
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001145static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1146{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001147 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001148
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001149 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001150
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001151 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001152
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001153 if (qc_release_detached_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001154 if (qcc_is_dead(qcc)) {
1155 qc_release(qcc);
1156 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001157 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001158 if (qcc_may_expire(qcc))
1159 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1160 else
1161 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001162 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001163 }
1164 }
1165
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001166 qc_recv(qcc);
1167
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001168 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1169
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001170 return NULL;
1171}
1172
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001173static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1174{
1175 struct qcc *qcc = ctx;
1176 int expired = tick_is_expired(t->expire, now_ms);
1177
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001178 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001179
1180 if (qcc) {
1181 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001182 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001183 return t;
1184 }
1185
1186 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001187 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001188 t->expire = TICK_ETERNITY;
1189 return t;
1190 }
1191 }
1192
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001193 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001194
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001195 if (!qcc) {
1196 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001197 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001198 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001199
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001200 qcc->task = NULL;
1201
1202 if (qcc_is_dead(qcc))
1203 qc_release(qcc);
1204
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001205 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1206
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001207 return NULL;
1208}
1209
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001210static int qc_init(struct connection *conn, struct proxy *prx,
1211 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001212{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001213 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001214 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001215
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001216 qcc = pool_alloc(pool_head_qcc);
1217 if (!qcc)
1218 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001219
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001220 qcc->conn = conn;
1221 conn->ctx = qcc;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001222 qcc->nb_cs = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001223 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001224
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001225 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001226
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001227 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001228
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001229 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001230 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001231
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001232 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001233 qcc->tx.sent_offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001234
1235 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001236 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001237 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
1238 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001239 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001240 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001241
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001242 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001243 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
1244 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001245 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001246 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001247
1248 /* Server initiated streams must respect the server flow control. */
1249 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001250 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
1251 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001252 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001253 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1254
1255 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001256 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
1257 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001258 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001259 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001260
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001261 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001262 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02001263 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
1264 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001265 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001266
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001267 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
1268 qcc->lfctl.sent_offsets = 0;
1269
Willy Tarreau784b8682022-04-11 14:18:10 +02001270 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001271 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001272 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1273 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1274
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001275 qcc->wait_event.tasklet = tasklet_new();
1276 if (!qcc->wait_event.tasklet)
1277 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001278
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001279 LIST_INIT(&qcc->send_retry_list);
1280
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001281 qcc->subs = NULL;
1282 qcc->wait_event.tasklet->process = qc_io_cb;
1283 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001284 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001285
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001286 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001287 qcc->task = NULL;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001288 qcc->timeout = prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001289 if (tick_isset(qcc->timeout)) {
1290 qcc->task = task_new_here();
1291 if (!qcc->task)
1292 goto fail_no_timeout_task;
1293 qcc->task->process = qc_timeout_task;
1294 qcc->task->context = qcc;
1295 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1296 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001297
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001298 if (!conn_is_back(conn)) {
1299 if (!LIST_INLIST(&conn->stopping_list)) {
1300 LIST_APPEND(&mux_stopping_data[tid].list,
1301 &conn->stopping_list);
1302 }
1303 }
1304
Willy Tarreau784b8682022-04-11 14:18:10 +02001305 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001306 /* init read cycle */
1307 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001308
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001309 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001310
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001311 fail_no_timeout_task:
1312 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001313 fail_no_tasklet:
1314 pool_free(pool_head_qcc, qcc);
1315 fail_no_qcc:
1316 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001317}
1318
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001319static void qc_destroy(void *ctx)
1320{
1321 struct qcc *qcc = ctx;
1322
1323 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
1324 qc_release(qcc);
1325 TRACE_LEAVE(QMUX_EV_QCC_END);
1326}
1327
Willy Tarreau4201ab72022-05-10 19:18:52 +02001328static void qc_detach(struct cs_endpoint *endp)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001329{
Willy Tarreaudf943132022-05-13 16:31:23 +02001330 struct qcs *qcs = endp->target;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001331 struct qcc *qcc = qcs->qcc;
1332
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001333 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001334
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001335 --qcc->nb_cs;
1336
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001337 if ((b_data(&qcs->tx.buf) || qcs->tx.offset > qcs->tx.sent_offset) &&
1338 !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001339 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001340 qcs->flags |= QC_SF_DETACH;
1341 return;
1342 }
1343
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001344 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001345
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001346 if (qcc_is_dead(qcc)) {
1347 qc_release(qcc);
1348 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001349 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001350 if (qcc_may_expire(qcc))
1351 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1352 else
1353 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001354 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001355 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001356
1357 TRACE_LEAVE(QMUX_EV_STRM_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001358}
1359
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001360/* Called from the upper layer, to receive data */
1361static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf,
1362 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001363{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001364 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001365 struct htx *qcs_htx = NULL;
1366 struct htx *cs_htx = NULL;
1367 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001368 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001369
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001370 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001371
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001372 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
1373 if (htx_is_empty(qcs_htx)) {
1374 /* Set buffer data to 0 as HTX is empty. */
1375 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1376 goto end;
1377 }
1378
1379 ret = qcs_htx->data;
1380
1381 cs_htx = htx_from_buf(buf);
1382 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
1383 htx_to_buf(cs_htx, buf);
1384 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1385 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
1386 goto end;
1387 }
1388
1389 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
1390 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
1391
1392 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001393 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
1394 cs_htx->flags |= HTX_FL_EOM;
1395 fin = 1;
1396 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001397
1398 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
1399 htx_to_buf(cs_htx, buf);
1400 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1401 ret -= qcs_htx->data;
1402
1403 end:
1404 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreau15b07212022-05-10 11:24:26 +02001405 qcs->endp->flags |= (CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001406 }
1407 else {
Willy Tarreau15b07212022-05-10 11:24:26 +02001408 qcs->endp->flags &= ~(CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
1409 if (qcs->endp->flags & CS_EP_ERR_PENDING)
1410 qcs->endp->flags |= CS_EP_ERROR;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001411
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001412 if (fin)
Willy Tarreau15b07212022-05-10 11:24:26 +02001413 qcs->endp->flags |= CS_EP_EOI;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001414
1415 if (b_size(&qcs->rx.app_buf)) {
1416 b_free(&qcs->rx.app_buf);
1417 offer_buffers(NULL, 1);
1418 }
1419 }
1420
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001421 if (ret) {
1422 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001423 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001424 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001425
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001426 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
1427
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001428 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001429}
1430
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001431static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf,
1432 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001433{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001434 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001435 size_t ret;
1436
1437 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001438
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001439 ret = qcs->qcc->app_ops->snd_buf(cs, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001440
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001441 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
1442
1443 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001444}
1445
1446/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1447 * event subscriber <es> is not allowed to change from a previous call as long
1448 * as at least one event is still subscribed. The <event_type> must only be a
1449 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1450 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001451static int qc_subscribe(struct conn_stream *cs, int event_type,
1452 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001453{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001454 return qcs_subscribe(__cs_mux(cs), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001455}
1456
1457/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1458 * The <es> pointer is not allowed to differ from the one passed to the
1459 * subscribe() call. It always returns zero.
1460 */
1461static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
1462{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001463 struct qcs *qcs = __cs_mux(cs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001464
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001465 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1466 BUG_ON(qcs->subs && qcs->subs != es);
1467
1468 es->events &= ~event_type;
1469 if (!es->events)
1470 qcs->subs = NULL;
1471
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001472 return 0;
1473}
1474
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001475/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Christopher Fauletb041b232022-03-24 10:27:02 +01001476 * report CS_EP_ERR_PENDING|CS_EP_ERROR on the attached conn-streams and wake
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001477 * them.
1478 */
1479static int qc_wake_some_streams(struct qcc *qcc)
1480{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001481 struct qcs *qcs;
1482 struct eb64_node *node;
1483
1484 for (node = eb64_first(&qcc->streams_by_id); node;
1485 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001486 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001487
Willy Tarreau01c2a4a2022-05-10 15:46:10 +02001488 if (!qcs->endp->cs)
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001489 continue;
1490
1491 if (qcc->conn->flags & CO_FL_ERROR) {
Christopher Fauletb041b232022-03-24 10:27:02 +01001492 qcs->endp->flags |= CS_EP_ERR_PENDING;
1493 if (qcs->endp->flags & CS_EP_EOS)
1494 qcs->endp->flags |= CS_EP_ERROR;
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001495
1496 if (qcs->subs) {
1497 qcs_notify_recv(qcs);
1498 qcs_notify_send(qcs);
1499 }
Willy Tarreau01c2a4a2022-05-10 15:46:10 +02001500 else if (qcs->endp->cs->data_cb->wake) {
1501 qcs->endp->cs->data_cb->wake(qcs->endp->cs);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001502 }
1503 }
1504 }
1505
1506 return 0;
1507}
1508
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001509static int qc_wake(struct connection *conn)
1510{
1511 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02001512 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001513
1514 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001515
1516 /* Check if a soft-stop is in progress.
1517 * Release idling front connection if this is the case.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001518 *
1519 * TODO this is revelant for frontend connections only.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001520 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001521 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
1522 goto release;
1523
Willy Tarreau784b8682022-04-11 14:18:10 +02001524 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02001525 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
1526
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001527 qc_send(qcc);
1528
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001529 qc_wake_some_streams(qcc);
1530
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001531 if (qcc_is_dead(qcc))
1532 goto release;
1533
1534 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
1535
1536 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001537
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001538 release:
1539 qc_release(qcc);
1540 TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001541 return 1;
1542}
1543
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001544
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001545static void qmux_trace_frm(const struct quic_frame *frm)
1546{
1547 switch (frm->type) {
1548 case QUIC_FT_MAX_STREAMS_BIDI:
1549 chunk_appendf(&trace_buf, " max_streams=%lu",
1550 frm->max_streams_bidi.max_streams);
1551 break;
1552
1553 case QUIC_FT_MAX_STREAMS_UNI:
1554 chunk_appendf(&trace_buf, " max_streams=%lu",
1555 frm->max_streams_uni.max_streams);
1556 break;
1557
1558 default:
1559 break;
1560 }
1561}
1562
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001563/* quic-mux trace handler */
1564static void qmux_trace(enum trace_level level, uint64_t mask,
1565 const struct trace_source *src,
1566 const struct ist where, const struct ist func,
1567 const void *a1, const void *a2, const void *a3, const void *a4)
1568{
1569 const struct connection *conn = a1;
1570 const struct qcc *qcc = conn ? conn->ctx : NULL;
1571 const struct qcs *qcs = a2;
1572
1573 if (!qcc)
1574 return;
1575
1576 if (src->verbosity > QMUX_VERB_CLEAN) {
1577 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
1578
1579 if (qcs)
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001580 chunk_appendf(&trace_buf, " qcs=%p(%lu)", qcs, qcs->id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001581
1582 if (mask & QMUX_EV_QCC_NQCS) {
1583 const uint64_t *id = a3;
1584 chunk_appendf(&trace_buf, " id=%lu", *id);
1585 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001586
1587 if (mask & QMUX_EV_SEND_FRM)
1588 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001589
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001590 if (mask & QMUX_EV_QCS_XFER_DATA) {
1591 const struct qcs_xfer_data_trace_arg *arg = a3;
1592 chunk_appendf(&trace_buf, " prep=%lu xfer=%d",
1593 arg->prep, arg->xfer);
1594 }
1595
1596 if (mask & QMUX_EV_QCS_BUILD_STRM) {
1597 const struct qcs_build_stream_trace_arg *arg = a3;
1598 chunk_appendf(&trace_buf, " len=%lu fin=%d offset=%lu",
1599 arg->len, arg->fin, arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001600 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001601 }
1602}
1603
Amaury Denoyelle251eadf2022-03-24 17:14:52 +01001604/* Function to automatically activate QUIC MUX traces on stdout.
1605 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
1606 * Main use for now is in the docker image for QUIC interop testing.
1607 */
1608static void qmux_init_stdout_traces(void)
1609{
1610#ifdef ENABLE_QUIC_STDOUT_TRACES
1611 trace_qmux.sink = sink_find("stdout");
1612 trace_qmux.level = TRACE_LEVEL_DEVELOPER;
1613 trace_qmux.state = TRACE_STATE_RUNNING;
1614 trace_qmux.verbosity = QMUX_VERB_MINIMAL;
1615#endif
1616}
1617INITCALL0(STG_INIT, qmux_init_stdout_traces);
1618
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001619
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001620static const struct mux_ops qc_ops = {
1621 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001622 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001623 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001624 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001625 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001626 .subscribe = qc_subscribe,
1627 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001628 .wake = qc_wake,
Christopher Fauleta97cced2022-04-12 18:04:10 +02001629 .flags = MX_FL_HTX|MX_FL_NO_UPG,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02001630 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001631};
1632
1633static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001634 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001635
1636INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);