blob: e68b362b95036465101668ce6a10baf42852ec5d [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 Denoyelledeed7772021-12-03 11:36:46 +010010#include <haproxy/pool.h>
Amaury Denoyelle251eadf2022-03-24 17:14:52 +010011#include <haproxy/sink.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020012#include <haproxy/ssl_sock-t.h>
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010013#include <haproxy/trace.h>
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +010014#include <haproxy/xprt_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010015
Amaury Denoyelledeed7772021-12-03 11:36:46 +010016DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010017DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
18
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010019/* trace source and events */
20static void qmux_trace(enum trace_level level, uint64_t mask,
21 const struct trace_source *src,
22 const struct ist where, const struct ist func,
23 const void *a1, const void *a2, const void *a3, const void *a4);
24
25static const struct trace_event qmux_trace_events[] = {
Amaury Denoyelle4f137572022-03-24 17:10:00 +010026#define QMUX_EV_QCC_RECV (1ULL << 1)
27 { .mask = QMUX_EV_QCC_RECV, .name = "qcc_recv", .desc = "Rx on QUIC connection" },
28#define QMUX_EV_QCC_SEND (1ULL << 2)
29 { .mask = QMUX_EV_QCC_SEND, .name = "qcc_send", .desc = "Tx on QUIC connection" },
30#define QMUX_EV_QCC_WAKE (1ULL << 3)
31 { .mask = QMUX_EV_QCC_WAKE, .name = "qcc_wake", .desc = "QUIC connection woken up" },
32#define QMUX_EV_QCC_END (1ULL << 4)
33 { .mask = QMUX_EV_QCC_END, .name = "qcc_end", .desc = "QUIC connection terminated" },
34#define QMUX_EV_QCC_NQCS (1ULL << 5)
35 { .mask = QMUX_EV_QCC_NQCS, .name = "qcc_no_qcs", .desc = "QUIC stream not found" },
36#define QMUX_EV_QCS_NEW (1ULL << 6)
37 { .mask = QMUX_EV_QCS_NEW, .name = "qcs_new", .desc = "new QUIC stream" },
38#define QMUX_EV_QCS_RECV (1ULL << 7)
39 { .mask = QMUX_EV_QCS_RECV, .name = "qcs_recv", .desc = "Rx on QUIC stream" },
40#define QMUX_EV_QCS_SEND (1ULL << 8)
41 { .mask = QMUX_EV_QCS_SEND, .name = "qcs_send", .desc = "Tx on QUIC stream" },
42#define QMUX_EV_QCS_END (1ULL << 9)
43 { .mask = QMUX_EV_QCS_END, .name = "qcs_end", .desc = "QUIC stream terminated" },
44#define QMUX_EV_STRM_RECV (1ULL << 10)
45 { .mask = QMUX_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
46#define QMUX_EV_STRM_SEND (1ULL << 11)
47 { .mask = QMUX_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
48#define QMUX_EV_STRM_END (1ULL << 12)
49 { .mask = QMUX_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
Amaury Denoyellefa29f332022-03-25 09:09:40 +010050#define QMUX_EV_SEND_FRM (1ULL << 13)
51 { .mask = QMUX_EV_SEND_FRM, .name = "send_frm", .desc = "sending QUIC frame" },
Amaury Denoyellefdcec362022-03-25 09:28:10 +010052/* special event dedicated to qcs_push_frame */
53#define QMUX_EV_QCS_PUSH_FRM (1ULL << 14)
54 { .mask = QMUX_EV_QCS_PUSH_FRM, .name = "qcs_push_frm", .desc = "qcs_push_frame" },
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010055 { }
56};
57
Amaury Denoyellefdcec362022-03-25 09:28:10 +010058/* custom arg for QMUX_EV_QCS_PUSH_FRM */
59struct qcs_push_frm_trace_arg {
60 size_t sent;
61 int xfer;
62 char fin;
63 uint64_t offset;
64};
65
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010066static const struct name_desc qmux_trace_lockon_args[4] = {
67 /* arg1 */ { /* already used by the connection */ },
68 /* arg2 */ { .name="qcs", .desc="QUIC stream" },
69 /* arg3 */ { },
70 /* arg4 */ { }
71};
72
73static const struct name_desc qmux_trace_decoding[] = {
74#define QMUX_VERB_CLEAN 1
75 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
76#define QMUX_VERB_MINIMAL 2
77 { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" },
78 { /* end */ }
79};
80
81struct trace_source trace_qmux = {
82 .name = IST("qmux"),
83 .desc = "QUIC multiplexer",
84 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
85 .default_cb = qmux_trace,
86 .known_events = qmux_trace_events,
87 .lockon_args = qmux_trace_lockon_args,
88 .decoding = qmux_trace_decoding,
89 .report_events = ~0, /* report everything by default */
90};
91
92#define TRACE_SOURCE &trace_qmux
93INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
94
Amaury Denoyelledeed7772021-12-03 11:36:46 +010095/* Allocate a new QUIC streams with id <id> and type <type>. */
96struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +010097{
Amaury Denoyelledeed7772021-12-03 11:36:46 +010098 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010099
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100100 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
101
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100102 qcs = pool_alloc(pool_head_qcs);
103 if (!qcs)
104 goto out;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100105
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100106 qcs->qcc = qcc;
107 qcs->cs = NULL;
108 qcs->flags = QC_SF_NONE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100109
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100110 qcs->by_id.key = id;
111 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
112 qcc->strms[type].nb_streams++;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100113
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100114 /* If stream is local, use peer remote-limit, or else the opposite. */
115 /* TODO use uni limit for unidirectional streams */
116 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
117 qcc->rfctl.msd_bidi_l;
118
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100119 qcs->rx.buf = BUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100120 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100121 qcs->rx.offset = 0;
122 qcs->rx.frms = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100123
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100124 qcs->tx.buf = BUF_NULL;
125 qcs->tx.xprt_buf = BUF_NULL;
126 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100127 qcs->tx.sent_offset = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100128 qcs->tx.ack_offset = 0;
Frédéric Lécaille2ee5c8b2022-03-13 12:31:36 +0100129 qcs->tx.acked_frms = EB_ROOT;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100130
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100131 qcs->wait_event.tasklet = NULL;
132 qcs->wait_event.events = 0;
133 qcs->subs = NULL;
134
135 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100136 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100137 return qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100138}
139
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200140/* Free a qcs. This function must only be done to remove a stream on allocation
141 * error or connection shutdown. Else use qcs_destroy which handle all the
142 * QUIC connection mechanism.
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100143 */
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200144void qcs_free(struct qcs *qcs)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100145{
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200146 b_free(&qcs->rx.buf);
147 b_free(&qcs->tx.buf);
148 b_free(&qcs->tx.xprt_buf);
149
150 BUG_ON(!qcs->qcc->strms[qcs_id_type(qcs->by_id.key)].nb_streams);
151 --qcs->qcc->strms[qcs_id_type(qcs->by_id.key)].nb_streams;
152
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100153 eb64_delete(&qcs->by_id);
154 pool_free(pool_head_qcs, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100155}
156
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100157struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100158{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100159 struct buffer *buf = b_alloc(bptr);
160 BUG_ON(!buf);
161 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100162}
163
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100164int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
165{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100166 struct qcc *qcc = qcs->qcc;
167
168 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100169
170 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
171 BUG_ON(qcs->subs && qcs->subs != es);
172
173 es->events |= event_type;
174 qcs->subs = es;
175
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100176 if (event_type & SUB_RETRY_RECV)
177 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
178
179 if (event_type & SUB_RETRY_SEND)
180 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
181
182 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
183
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100184 return 0;
185}
186
187void qcs_notify_recv(struct qcs *qcs)
188{
189 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
190 tasklet_wakeup(qcs->subs->tasklet);
191 qcs->subs->events &= ~SUB_RETRY_RECV;
192 if (!qcs->subs->events)
193 qcs->subs = NULL;
194 }
195}
196
197void qcs_notify_send(struct qcs *qcs)
198{
199 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
200 tasklet_wakeup(qcs->subs->tasklet);
201 qcs->subs->events &= ~SUB_RETRY_SEND;
202 if (!qcs->subs->events)
203 qcs->subs = NULL;
204 }
205}
206
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100207/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
208 * several streams, depending on the already open ones.
209 * Return this node if succeeded, NULL if not.
210 */
Amaury Denoyelle50742292022-03-29 14:57:19 +0200211struct qcs *qcc_get_qcs(struct qcc *qcc, uint64_t id)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100212{
213 unsigned int strm_type;
214 int64_t sub_id;
215 struct eb64_node *strm_node;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200216 struct qcs *qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100217
218 strm_type = id & QCS_ID_TYPE_MASK;
219 sub_id = id >> QCS_ID_TYPE_SHIFT;
220 strm_node = NULL;
Amaury Denoyelle0dc40f02022-02-07 11:44:17 +0100221 if (quic_stream_is_local(qcc, id)) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100222 /* Local streams: this stream must be already opened. */
223 strm_node = eb64_lookup(&qcc->streams_by_id, id);
224 if (!strm_node) {
225 /* unknown stream id */
226 goto out;
227 }
Amaury Denoyelle50742292022-03-29 14:57:19 +0200228 qcs = eb64_entry(strm_node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100229 }
230 else {
231 /* Remote streams. */
232 struct eb_root *strms;
233 uint64_t largest_id;
234 enum qcs_type qcs_type;
235
236 strms = &qcc->streams_by_id;
237 qcs_type = qcs_id_type(id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100238
239 /* TODO also checks max-streams for uni streams */
240 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100241 if (sub_id + 1 > qcc->lfctl.ms_bidi) {
Amaury Denoyellec055e302022-02-07 16:09:06 +0100242 /* streams limit reached */
243 goto out;
244 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100245 }
246
247 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
248 * correct value.
249 */
250 largest_id = qcc->strms[qcs_type].largest_id;
251 if (sub_id > (int64_t)largest_id) {
252 /* RFC: "A stream ID that is used out of order results in all streams
253 * of that type with lower-numbered stream IDs also being opened".
254 * So, let's "open" these streams.
255 */
256 int64_t i;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200257 struct qcs *tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100258
Amaury Denoyelle50742292022-03-29 14:57:19 +0200259 tmp_qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100260 for (i = largest_id + 1; i <= sub_id; i++) {
261 uint64_t id = (i << QCS_ID_TYPE_SHIFT) | strm_type;
262 enum qcs_type type = id & QCS_ID_DIR_BIT ? QCS_CLT_UNI : QCS_CLT_BIDI;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200263 tmp_qcs = qcs_new(qcc, id, type);
264 if (!tmp_qcs) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100265 /* allocation failure */
266 goto out;
267 }
268
269 qcc->strms[qcs_type].largest_id = i;
270 }
Amaury Denoyelle50742292022-03-29 14:57:19 +0200271 if (tmp_qcs)
272 qcs = tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100273 }
274 else {
275 strm_node = eb64_lookup(strms, id);
Amaury Denoyelle50742292022-03-29 14:57:19 +0200276 if (strm_node)
277 qcs = eb64_entry(strm_node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100278 }
279 }
280
Amaury Denoyelle50742292022-03-29 14:57:19 +0200281 return qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100282
283 out:
284 return NULL;
285}
286
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100287/* Handle a new STREAM frame <strm_frm>. The frame content will be copied in
288 * the buffer of the stream instance. The stream instance will be stored in
289 * <out_qcs>. In case of success, the caller can immediatly call qcc_decode_qcs
290 * to process the frame content.
291 *
292 * Returns 0 on success. On errors, two codes are present.
293 * - 1 is returned if the frame cannot be decoded and must be discarded.
294 * - 2 is returned if the stream cannot decode at the moment the frame. The
295 * frame should be buffered to be handled later.
296 */
297int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
298 char fin, char *data, struct qcs **out_qcs)
299{
300 struct qcs *qcs;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100301 size_t total, diff;
302
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100303 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
304
Amaury Denoyelle50742292022-03-29 14:57:19 +0200305 qcs = qcc_get_qcs(qcc, id);
306 if (!qcs) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100307 TRACE_DEVEL("leaving on stream not found", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100308 return 1;
309 }
310
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100311 *out_qcs = qcs;
312
313 if (offset > qcs->rx.offset)
314 return 2;
315
316 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100317 TRACE_DEVEL("leaving on already received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100318 return 1;
319 }
320
321 /* Last frame already handled for this stream. */
322 BUG_ON(qcs->flags & QC_SF_FIN_RECV);
323
324 if (!qc_get_buf(qcs, &qcs->rx.buf)) {
325 /* TODO should mark qcs as full */
326 return 2;
327 }
328
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100329 TRACE_DEVEL("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100330 diff = qcs->rx.offset - offset;
331
332 /* TODO do not partially copy a frame if not enough size left. Maybe
333 * this can be optimized.
334 */
335 if (len > b_room(&qcs->rx.buf)) {
336 /* TODO handle STREAM frames larger than RX buffer. */
337 BUG_ON(len > b_size(&qcs->rx.buf));
338 return 2;
339 }
340
341 len -= diff;
342 data += diff;
343
344 total = b_putblk(&qcs->rx.buf, data, len);
345 /* TODO handle partial copy of a STREAM frame. */
346 BUG_ON(len != total);
347
348 qcs->rx.offset += total;
349
350 if (fin)
351 qcs->flags |= QC_SF_FIN_RECV;
352
353 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100354 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100355 return 0;
356}
357
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100358/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
359 * the frame.
360 *
361 * Returns 0 on success else non-zero.
362 */
363int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
364{
365 if (qcc->rfctl.md < max) {
366 qcc->rfctl.md = max;
367
368 if (qcc->flags & QC_CF_BLK_MFCTL) {
369 qcc->flags &= ~QC_CF_BLK_MFCTL;
370 tasklet_wakeup(qcc->wait_event.tasklet);
371 }
372 }
373 return 0;
374}
375
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100376/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
377 * field of the frame and <id> is the identifier of the QUIC stream.
378 *
379 * Returns 0 on success else non-zero.
380 */
381int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
382{
383 struct qcs *qcs;
384 struct eb64_node *node;
385
386 node = eb64_lookup(&qcc->streams_by_id, id);
387 if (node) {
388 qcs = eb64_entry(&node->node, struct qcs, by_id);
389 if (max > qcs->tx.msd) {
390 qcs->tx.msd = max;
391
392 if (qcs->flags & QC_SF_BLK_SFCTL) {
393 qcs->flags &= ~QC_SF_BLK_SFCTL;
394 tasklet_wakeup(qcc->wait_event.tasklet);
395 }
396 }
397 }
398
399 return 0;
400}
401
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100402/* Decode the content of STREAM frames already received on the stream instance
403 * <qcs>.
404 *
405 * Returns 0 on success else non-zero.
406 */
407int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
408{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100409 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
410
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100411 if (qcc->app_ops->decode_qcs(qcs, qcs->flags & QC_SF_FIN_RECV, qcc->ctx) < 0) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100412 TRACE_DEVEL("leaving on decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100413 return 1;
414 }
415
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100416 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
417
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100418 return 0;
419}
420
Amaury Denoyellec055e302022-02-07 16:09:06 +0100421static int qc_is_max_streams_needed(struct qcc *qcc)
422{
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100423 return qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100424}
425
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500426/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100427static void qcs_destroy(struct qcs *qcs)
428{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100429 struct connection *conn = qcs->qcc->conn;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100430 const uint64_t id = qcs->by_id.key;
431
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100432 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100433
Amaury Denoyellec055e302022-02-07 16:09:06 +0100434 if (quic_stream_is_remote(qcs->qcc, id)) {
435 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100436 ++qcs->qcc->lfctl.cl_bidi_r;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100437 if (qc_is_max_streams_needed(qcs->qcc))
438 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
439 }
440 }
441
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200442 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100443
444 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100445}
446
447static inline int qcc_is_dead(const struct qcc *qcc)
448{
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100449 if (!qcc->strms[QCS_CLT_BIDI].nb_streams && !qcc->task)
450 return 1;
451
452 return 0;
453}
454
455/* Return true if the mux timeout should be armed. */
456static inline int qcc_may_expire(struct qcc *qcc)
457{
458
459 /* Consider that the timeout must be set if no bidirectional streams
460 * are opened.
461 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100462 if (!qcc->strms[QCS_CLT_BIDI].nb_streams)
463 return 1;
464
465 return 0;
466}
467
468/* release function. This one should be called to free all resources allocated
469 * to the mux.
470 */
471static void qc_release(struct qcc *qcc)
472{
473 struct connection *conn = NULL;
474
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100475 TRACE_ENTER(QMUX_EV_QCC_END);
476
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100477 if (qcc) {
478 /* The connection must be aattached to this mux to be released */
479 if (qcc->conn && qcc->conn->ctx == qcc)
480 conn = qcc->conn;
481
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100482 TRACE_DEVEL("freeing qcc", QMUX_EV_QCC_END, conn);
483
Amaury Denoyellecbc13b72022-03-29 14:46:38 +0200484 if (qcc->app_ops && qcc->app_ops->release)
485 qcc->app_ops->release(qcc->ctx);
486
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100487 if (qcc->wait_event.tasklet)
488 tasklet_free(qcc->wait_event.tasklet);
489
490 pool_free(pool_head_qcc, qcc);
491 }
492
493 if (conn) {
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100494 LIST_DEL_INIT(&conn->stopping_list);
495
Frédéric Lécaille19cd46e2022-01-10 11:40:33 +0100496 conn->qc->conn = NULL;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100497 conn->mux = NULL;
498 conn->ctx = NULL;
499
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100500 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
501
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100502 conn_stop_tracking(conn);
503 conn_full_close(conn);
504 if (conn->destroy_cb)
505 conn->destroy_cb(conn);
506 conn_free(conn);
507 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100508
509 TRACE_LEAVE(QMUX_EV_QCC_END);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100510}
511
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100512/* Prepare a STREAM frame for <qcs> instance. First, transfer data from
513 * <payload> to <out> buffer. The STREAM frame payload points to the <out>
514 * buffer. The frame is then pushed to <frm_list>. If <fin> is set, and the
515 * <payload> buf is emptied after transfer, FIN bit is set on the STREAM frame.
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100516 * Transfer is automatically adjusted to not exceed the stream flow-control
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100517 * limit. <max_data> must contains the current sum offsets for the connection.
518 * This is useful to not exceed the connection flow-control limit when using
519 * repeatdly this function on multiple streams before passing the data to the
520 * lower layer.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100521 *
522 * Returns the total bytes of newly transferred data or a negative error code.
523 */
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100524static int qcs_push_frame(struct qcs *qcs, struct buffer *out,
525 struct buffer *payload, int fin,
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100526 struct list *frm_list, uint64_t max_data)
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200527{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100528 struct qcc *qcc = qcs->qcc;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200529 struct quic_frame *frm;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100530 int head, left, to_xfer;
531 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200532
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100533 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100534
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100535 qc_get_buf(qcs, out);
536
537 /*
538 * QCS out buffer diagram
539 * head left to_xfer
540 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100541 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100542 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100543 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100544 * ^ ack-off ^ sent-off ^ off
545 *
546 * STREAM frame
547 * ^ ^
548 * |xxxxxxxxxxxxxxxxx|
549 */
550
551 BUG_ON_HOT(qcs->tx.sent_offset < qcs->tx.ack_offset);
552 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
553
554 head = qcs->tx.sent_offset - qcs->tx.ack_offset;
555 left = qcs->tx.offset - qcs->tx.sent_offset;
556 to_xfer = QUIC_MIN(b_data(payload), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100557
558 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
559 /* do not exceed flow control limit */
560 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
561 to_xfer = qcs->tx.msd - qcs->tx.offset;
562
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100563 BUG_ON_HOT(max_data > qcc->rfctl.md);
564 /* do not overcome flow control limit on connection */
565 if (max_data + to_xfer > qcc->rfctl.md)
566 to_xfer = qcc->rfctl.md - max_data;
567
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100568 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200569 goto out;
570
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200571 frm = pool_zalloc(pool_head_quic_frame);
572 if (!frm)
573 goto err;
574
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100575 total = b_force_xfer(out, payload, to_xfer);
576
577 frm->type = QUIC_FT_STREAM_8;
578 frm->stream.qcs = (struct qcs *)qcs;
579 frm->stream.id = qcs->by_id.key;
580 frm->stream.buf = out;
581 frm->stream.data = (unsigned char *)b_peek(out, head);
582
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100583 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200584 fin = fin && !b_data(payload);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200585 if (fin)
586 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100587
588 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200589 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100590 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200591 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100592
593 if (left + total) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200594 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100595 frm->stream.len = left + total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200596 }
597
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100598 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100599
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200600 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100601 {
602 struct qcs_push_frm_trace_arg arg = {
603 .sent = b_data(out), .xfer = total, .fin = fin,
604 .offset = qcs->tx.sent_offset
605 };
606 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_PUSH_FRM,
607 qcc->conn, qcs, &arg);
608 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100609
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200610 return total;
611
612 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100613 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200614 return -1;
615}
616
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100617/* This function must be called by the upper layer to inform about the sending
618 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
619 * <offset>.
620 */
621void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
622{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100623 struct qcc *qcc = qcs->qcc;
624 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100625
626 BUG_ON(offset > qcs->tx.sent_offset);
627
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100628 /* check if the STREAM frame has already been notified. It can happen
629 * for retransmission.
630 */
631 if (offset + data <= qcs->tx.sent_offset)
632 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100633
634 diff = offset + data - qcs->tx.sent_offset;
635
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100636 /* increase offset sum on connection */
637 qcc->tx.sent_offsets += diff;
638 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
639 if (qcc->tx.sent_offsets == qcc->rfctl.md)
640 qcc->flags |= QC_CF_BLK_MFCTL;
641
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100642 /* increase offset on stream */
643 qcs->tx.sent_offset += diff;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100644 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
645 if (qcs->tx.sent_offset == qcs->tx.msd)
646 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100647}
648
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100649/* Wrapper for send on transport layer. Send a list of frames <frms> for the
650 * connection <qcc>.
651 *
652 * Returns 0 if all data sent with success else non-zero.
653 */
654static int qc_send_frames(struct qcc *qcc, struct list *frms)
655{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100656 /* TODO implement an opportunistic retry mechanism. This is needed
657 * because qc_send_app_pkts is not completed. It will only prepare data
658 * up to its Tx buffer. The frames left are not send even if the Tx
659 * buffer is emptied by the sendto call.
660 *
661 * To overcome this, we call repeatedly qc_send_app_pkts until we
662 * detect that the transport layer has send nothing. This could happen
663 * on congestion or sendto syscall error.
664 *
665 * When qc_send_app_pkts is improved to handle retry by itself, we can
666 * remove the looping from the MUX.
667 */
668 struct quic_frame *first_frm;
669 uint64_t first_offset = 0;
670 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100671
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100672 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
673
674 if (LIST_ISEMPTY(frms)) {
675 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100676 return 0;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100677 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100678
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100679 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100680 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
681 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
682 first_offset = first_frm->stream.offset.key;
683 first_stream_frame_type = 1;
684 }
685 else {
686 first_stream_frame_type = 0;
687 }
688
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100689 if (!LIST_ISEMPTY(frms))
690 qc_send_app_pkts(qcc->conn->qc, frms);
691
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100692 /* If there is frames left, check if the transport layer has send some
693 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100694 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100695 if (!LIST_ISEMPTY(frms)) {
696 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
697 goto retry_send;
698
699 /* If the first frame is STREAM, check if its offset has
700 * changed.
701 */
702 if (first_stream_frame_type &&
703 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
704 goto retry_send;
705 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100706 }
707
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100708 /* If there is frames left at this stage, transport layer is blocked.
709 * Subscribe on it to retry later.
710 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100711 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100712 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100713 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
714 SUB_RETRY_SEND, &qcc->wait_event);
715 return 1;
716 }
717
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100718 TRACE_LEAVE(QMUX_EV_QCC_SEND);
719
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100720 return 0;
721}
722
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100723/* Proceed to sending. Loop through all available streams for the <qcc>
724 * instance and try to send as much as possible.
725 *
726 * Returns the total of bytes sent to the transport layer.
727 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100728static int qc_send(struct qcc *qcc)
729{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100730 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200731 struct eb64_node *node;
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100732 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200733
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100734 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200735
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100736 if (qcc->flags & QC_CF_BLK_MFCTL)
737 return 0;
738
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100739 /* loop through all streams, construct STREAM frames if data available.
740 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200741 */
742 node = eb64_first(&qcc->streams_by_id);
743 while (node) {
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200744 struct qcs *qcs = container_of(node, struct qcs, by_id);
745 struct buffer *buf = &qcs->tx.buf;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100746 struct buffer *out = &qcs->tx.xprt_buf;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100747
Amaury Denoyellee2ec9422022-03-10 16:46:18 +0100748 /* TODO
749 * for the moment, unidirectional streams have their own
750 * mechanism for sending. This should be unified in the future,
751 * in this case the next check will be removed.
752 */
753 if (quic_stream_is_uni(qcs->by_id.key)) {
754 node = eb64_next(node);
755 continue;
756 }
757
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100758 if (qcs->flags & QC_SF_BLK_SFCTL) {
759 node = eb64_next(node);
760 continue;
761 }
762
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100763 if (b_data(buf) || b_data(out)) {
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100764 int ret;
Amaury Denoyelle92960912022-03-24 18:23:29 +0100765 char fin = !!(qcs->flags & QC_SF_FIN_STREAM);
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100766
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100767 ret = qcs_push_frame(qcs, out, buf, fin, &frms,
768 qcc->tx.sent_offsets + total);
Amaury Denoyelle14551132022-03-04 16:51:20 +0100769 BUG_ON(ret < 0); /* TODO handle this properly */
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200770
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100771 if (ret > 0) {
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100772 qcs_notify_send(qcs);
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100773 if (qcs->flags & QC_SF_BLK_MROOM)
774 qcs->flags &= ~QC_SF_BLK_MROOM;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100775 }
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200776
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200777 qcs->tx.offset += ret;
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100778 total += ret;
Amaury Denoyellea2c58a72021-12-03 14:38:31 +0100779
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100780 /* Subscribe if not all data can be send. */
Amaury Denoyellea2c58a72021-12-03 14:38:31 +0100781 if (b_data(buf)) {
782 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
783 SUB_RETRY_SEND, &qcc->wait_event);
784 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200785 }
786 node = eb64_next(node);
787 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200788
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100789 qc_send_frames(qcc, &frms);
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100790
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100791 TRACE_LEAVE(QMUX_EV_QCC_SEND);
792
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100793 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100794}
795
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +0100796/* Release all streams that are already marked as detached. This is only done
797 * if their TX buffers are empty or if a CONNECTION_CLOSE has been received.
798 *
799 * Return the number of released stream.
800 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100801static int qc_release_detached_streams(struct qcc *qcc)
802{
803 struct eb64_node *node;
804 int release = 0;
805
806 node = eb64_first(&qcc->streams_by_id);
807 while (node) {
808 struct qcs *qcs = container_of(node, struct qcs, by_id);
809 node = eb64_next(node);
810
811 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelled9751482022-02-01 15:15:11 +0100812 if ((!b_data(&qcs->tx.buf) && !b_data(&qcs->tx.xprt_buf))) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100813 qcs_destroy(qcs);
814 release = 1;
815 }
816 else {
817 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
818 SUB_RETRY_SEND, &qcc->wait_event);
819 }
820 }
821 }
822
823 return release;
824}
825
Amaury Denoyellec055e302022-02-07 16:09:06 +0100826/* Send a MAX_STREAM_BIDI frame to update the limit of bidirectional streams
827 * allowed to be opened by the peer. The caller should have first checked if
828 * this is required with qc_is_max_streams_needed.
829 *
830 * Returns 0 on success else non-zero.
831 */
832static int qc_send_max_streams(struct qcc *qcc)
833{
834 struct list frms = LIST_HEAD_INIT(frms);
835 struct quic_frame *frm;
836
837 frm = pool_zalloc(pool_head_quic_frame);
838 BUG_ON(!frm); /* TODO handle this properly */
839
840 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100841 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
842 qcc->lfctl.cl_bidi_r;
Amaury Denoyellefa29f332022-03-25 09:09:40 +0100843 TRACE_DEVEL("sending MAX_STREAMS frame", QMUX_EV_SEND_FRM, qcc->conn, NULL, frm);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100844 LIST_APPEND(&frms, &frm->list);
845
846 if (qc_send_frames(qcc, &frms))
847 return 1;
848
849 /* save the new limit if the frame has been send. */
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100850 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
851 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100852
853 return 0;
854}
855
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100856static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
857{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +0200858 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100859
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100860 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100861
Amaury Denoyellec055e302022-02-07 16:09:06 +0100862 if (qc_is_max_streams_needed(qcc))
863 qc_send_max_streams(qcc);
864
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100865 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100866
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100867 if (qc_release_detached_streams(qcc)) {
Amaury Denoyelle1136e922022-02-01 10:33:09 +0100868 /* Schedule the mux timeout if no bidirectional streams left. */
869 if (qcc_may_expire(qcc)) {
870 qcc->task->expire = tick_add(now_ms, qcc->timeout);
871 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100872 }
873 }
874
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100875 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
876
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100877 return NULL;
878}
879
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100880static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
881{
882 struct qcc *qcc = ctx;
883 int expired = tick_is_expired(t->expire, now_ms);
884
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100885 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100886
887 if (qcc) {
888 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100889 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100890 return t;
891 }
892
893 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100894 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100895 t->expire = TICK_ETERNITY;
896 return t;
897 }
898 }
899
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100900 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +0100901
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100902 if (!qcc) {
903 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +0100904 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100905 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +0100906
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100907 qcc->task = NULL;
908
909 if (qcc_is_dead(qcc))
910 qc_release(qcc);
911
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100912 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
913
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100914 return NULL;
915}
916
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100917static int qc_init(struct connection *conn, struct proxy *prx,
918 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100919{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100920 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100921 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100922
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100923 qcc = pool_alloc(pool_head_qcc);
924 if (!qcc)
925 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100926
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100927 qcc->conn = conn;
928 conn->ctx = qcc;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +0100929 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100930
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100931 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100932
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100933 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100934
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100935 /* Server parameters, params used for RX flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100936 lparams = &conn->qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100937
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100938 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100939 qcc->tx.sent_offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100940
941 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100942 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100943 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
944 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100945 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100946 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100947
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100948 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100949 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
950 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100951 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100952 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100953
954 /* Server initiated streams must respect the server flow control. */
955 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100956 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
957 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100958 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100959 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
960
961 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100962 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
963 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100964 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100965 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100966
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100967 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
968 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100969
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100970 rparams = &conn->qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100971 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100972 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
973 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
974
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100975 qcc->wait_event.tasklet = tasklet_new();
976 if (!qcc->wait_event.tasklet)
977 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100978
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100979 qcc->subs = NULL;
980 qcc->wait_event.tasklet->process = qc_io_cb;
981 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +0100982 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100983
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100984 /* haproxy timeouts */
985 qcc->timeout = prx->timeout.client;
986 qcc->task = task_new_here();
987 if (!qcc->task)
988 goto fail_no_timeout_task;
989 qcc->task->process = qc_timeout_task;
990 qcc->task->context = qcc;
991 qcc->task->expire = tick_add(now_ms, qcc->timeout);
992
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100993 if (!conn_is_back(conn)) {
994 if (!LIST_INLIST(&conn->stopping_list)) {
995 LIST_APPEND(&mux_stopping_data[tid].list,
996 &conn->stopping_list);
997 }
998 }
999
Frédéric Lécailleb80b20c2022-01-12 17:46:56 +01001000 HA_ATOMIC_STORE(&conn->qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001001 /* init read cycle */
1002 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001003
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001004 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001005
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001006 fail_no_timeout_task:
1007 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001008 fail_no_tasklet:
1009 pool_free(pool_head_qcc, qcc);
1010 fail_no_qcc:
1011 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001012}
1013
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001014static void qc_detach(struct conn_stream *cs)
1015{
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001016 struct qcs *qcs = cs->ctx;
1017 struct qcc *qcc = qcs->qcc;
1018
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001019 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001020
Amaury Denoyelled9751482022-02-01 15:15:11 +01001021 /* TODO on CONNECTION_CLOSE reception, it should be possible to free
1022 * qcs instances. This should be done once the buffering and ACK
1023 * managment between xprt and mux is reorganized.
1024 */
1025
1026 if ((b_data(&qcs->tx.buf) || b_data(&qcs->tx.xprt_buf))) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001027 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001028 qcs->flags |= QC_SF_DETACH;
1029 return;
1030 }
1031
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001032 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001033
1034 /* Schedule the mux timeout if no bidirectional streams left. */
1035 if (qcc_may_expire(qcc)) {
1036 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1037 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001038 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001039
1040 TRACE_LEAVE(QMUX_EV_STRM_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001041}
1042
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001043/* Called from the upper layer, to receive data */
1044static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf,
1045 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001046{
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001047 struct qcs *qcs = cs->ctx;
1048 struct htx *qcs_htx = NULL;
1049 struct htx *cs_htx = NULL;
1050 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001051 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001052
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001053 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001054
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001055 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
1056 if (htx_is_empty(qcs_htx)) {
1057 /* Set buffer data to 0 as HTX is empty. */
1058 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1059 goto end;
1060 }
1061
1062 ret = qcs_htx->data;
1063
1064 cs_htx = htx_from_buf(buf);
1065 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
1066 htx_to_buf(cs_htx, buf);
1067 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1068 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
1069 goto end;
1070 }
1071
1072 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
1073 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
1074
1075 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001076 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
1077 cs_htx->flags |= HTX_FL_EOM;
1078 fin = 1;
1079 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001080
1081 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
1082 htx_to_buf(cs_htx, buf);
1083 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1084 ret -= qcs_htx->data;
1085
1086 end:
1087 if (b_data(&qcs->rx.app_buf)) {
1088 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1089 }
1090 else {
1091 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1092 if (cs->flags & CS_FL_ERR_PENDING)
1093 cs->flags |= CS_FL_ERROR;
1094
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001095 if (fin)
1096 cs->flags |= (CS_FL_EOI|CS_FL_EOS);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001097
1098 if (b_size(&qcs->rx.app_buf)) {
1099 b_free(&qcs->rx.app_buf);
1100 offer_buffers(NULL, 1);
1101 }
1102 }
1103
1104 if (ret)
1105 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
1106
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001107 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
1108
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001109 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001110}
1111
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001112static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf,
1113 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001114{
1115 struct qcs *qcs = cs->ctx;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001116 size_t ret;
1117
1118 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001119
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001120 ret = qcs->qcc->app_ops->snd_buf(cs, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001121
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001122 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
1123
1124 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001125}
1126
1127/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1128 * event subscriber <es> is not allowed to change from a previous call as long
1129 * as at least one event is still subscribed. The <event_type> must only be a
1130 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1131 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001132static int qc_subscribe(struct conn_stream *cs, int event_type,
1133 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001134{
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01001135 return qcs_subscribe(cs->ctx, event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001136}
1137
1138/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1139 * The <es> pointer is not allowed to differ from the one passed to the
1140 * subscribe() call. It always returns zero.
1141 */
1142static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
1143{
1144 struct qcs *qcs = cs->ctx;
1145
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001146 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1147 BUG_ON(qcs->subs && qcs->subs != es);
1148
1149 es->events &= ~event_type;
1150 if (!es->events)
1151 qcs->subs = NULL;
1152
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001153 return 0;
1154}
1155
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001156static int qc_wake(struct connection *conn)
1157{
1158 struct qcc *qcc = conn->ctx;
1159
1160 /* Check if a soft-stop is in progress.
1161 * Release idling front connection if this is the case.
1162 */
1163 if (unlikely(conn->qc->li->bind_conf->frontend->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
1164 qc_release(qcc);
1165 }
1166
1167 return 1;
1168}
1169
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001170
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001171static void qmux_trace_frm(const struct quic_frame *frm)
1172{
1173 switch (frm->type) {
1174 case QUIC_FT_MAX_STREAMS_BIDI:
1175 chunk_appendf(&trace_buf, " max_streams=%lu",
1176 frm->max_streams_bidi.max_streams);
1177 break;
1178
1179 case QUIC_FT_MAX_STREAMS_UNI:
1180 chunk_appendf(&trace_buf, " max_streams=%lu",
1181 frm->max_streams_uni.max_streams);
1182 break;
1183
1184 default:
1185 break;
1186 }
1187}
1188
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001189/* quic-mux trace handler */
1190static void qmux_trace(enum trace_level level, uint64_t mask,
1191 const struct trace_source *src,
1192 const struct ist where, const struct ist func,
1193 const void *a1, const void *a2, const void *a3, const void *a4)
1194{
1195 const struct connection *conn = a1;
1196 const struct qcc *qcc = conn ? conn->ctx : NULL;
1197 const struct qcs *qcs = a2;
1198
1199 if (!qcc)
1200 return;
1201
1202 if (src->verbosity > QMUX_VERB_CLEAN) {
1203 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
1204
1205 if (qcs)
1206 chunk_appendf(&trace_buf, " qcs=%p(%llu)", qcs, qcs->by_id.key);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001207
1208 if (mask & QMUX_EV_QCC_NQCS) {
1209 const uint64_t *id = a3;
1210 chunk_appendf(&trace_buf, " id=%lu", *id);
1211 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001212
1213 if (mask & QMUX_EV_SEND_FRM)
1214 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001215
1216 if (mask & QMUX_EV_QCS_PUSH_FRM) {
1217 const struct qcs_push_frm_trace_arg *arg = a3;
1218 chunk_appendf(&trace_buf, " sent=%lu xfer=%d fin=%d offset=%lu",
1219 arg->sent, arg->xfer, arg->fin, arg->offset);
1220 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001221 }
1222}
1223
Amaury Denoyelle251eadf2022-03-24 17:14:52 +01001224/* Function to automatically activate QUIC MUX traces on stdout.
1225 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
1226 * Main use for now is in the docker image for QUIC interop testing.
1227 */
1228static void qmux_init_stdout_traces(void)
1229{
1230#ifdef ENABLE_QUIC_STDOUT_TRACES
1231 trace_qmux.sink = sink_find("stdout");
1232 trace_qmux.level = TRACE_LEVEL_DEVELOPER;
1233 trace_qmux.state = TRACE_STATE_RUNNING;
1234 trace_qmux.verbosity = QMUX_VERB_MINIMAL;
1235#endif
1236}
1237INITCALL0(STG_INIT, qmux_init_stdout_traces);
1238
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001239
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001240static const struct mux_ops qc_ops = {
1241 .init = qc_init,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001242 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001243 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001244 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001245 .subscribe = qc_subscribe,
1246 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001247 .wake = qc_wake,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001248};
1249
1250static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001251 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001252
1253INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);