blob: 6a65d6923b4ae94f750d9fc518ec6f6990afdba3 [file] [log] [blame]
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001#include <haproxy/mux_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002
Amaury Denoyelleeb01f592021-10-07 16:44:05 +02003#include <import/eb64tree.h>
4
Frédéric Lécailledfbae762021-02-18 09:59:01 +01005#include <haproxy/api.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01006#include <haproxy/connection.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +01007#include <haproxy/conn_stream.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +01008#include <haproxy/dynbuf.h>
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01009#include <haproxy/htx.h>
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +020010#include <haproxy/list.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +010011#include <haproxy/pool.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020012#include <haproxy/quic_stream.h>
Amaury Denoyelle251eadf2022-03-24 17:14:52 +010013#include <haproxy/sink.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020014#include <haproxy/ssl_sock-t.h>
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010015#include <haproxy/trace.h>
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +010016#include <haproxy/xprt_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010017
Amaury Denoyelledeed7772021-12-03 11:36:46 +010018DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010019DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
20
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010021/* trace source and events */
22static void qmux_trace(enum trace_level level, uint64_t mask,
23 const struct trace_source *src,
24 const struct ist where, const struct ist func,
25 const void *a1, const void *a2, const void *a3, const void *a4);
26
27static const struct trace_event qmux_trace_events[] = {
Amaury Denoyelle4f137572022-03-24 17:10:00 +010028#define QMUX_EV_QCC_RECV (1ULL << 1)
29 { .mask = QMUX_EV_QCC_RECV, .name = "qcc_recv", .desc = "Rx on QUIC connection" },
30#define QMUX_EV_QCC_SEND (1ULL << 2)
31 { .mask = QMUX_EV_QCC_SEND, .name = "qcc_send", .desc = "Tx on QUIC connection" },
32#define QMUX_EV_QCC_WAKE (1ULL << 3)
33 { .mask = QMUX_EV_QCC_WAKE, .name = "qcc_wake", .desc = "QUIC connection woken up" },
34#define QMUX_EV_QCC_END (1ULL << 4)
35 { .mask = QMUX_EV_QCC_END, .name = "qcc_end", .desc = "QUIC connection terminated" },
36#define QMUX_EV_QCC_NQCS (1ULL << 5)
37 { .mask = QMUX_EV_QCC_NQCS, .name = "qcc_no_qcs", .desc = "QUIC stream not found" },
38#define QMUX_EV_QCS_NEW (1ULL << 6)
39 { .mask = QMUX_EV_QCS_NEW, .name = "qcs_new", .desc = "new QUIC stream" },
40#define QMUX_EV_QCS_RECV (1ULL << 7)
41 { .mask = QMUX_EV_QCS_RECV, .name = "qcs_recv", .desc = "Rx on QUIC stream" },
42#define QMUX_EV_QCS_SEND (1ULL << 8)
43 { .mask = QMUX_EV_QCS_SEND, .name = "qcs_send", .desc = "Tx on QUIC stream" },
44#define QMUX_EV_QCS_END (1ULL << 9)
45 { .mask = QMUX_EV_QCS_END, .name = "qcs_end", .desc = "QUIC stream terminated" },
46#define QMUX_EV_STRM_RECV (1ULL << 10)
47 { .mask = QMUX_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
48#define QMUX_EV_STRM_SEND (1ULL << 11)
49 { .mask = QMUX_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
50#define QMUX_EV_STRM_END (1ULL << 12)
51 { .mask = QMUX_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
Amaury Denoyellefa29f332022-03-25 09:09:40 +010052#define QMUX_EV_SEND_FRM (1ULL << 13)
53 { .mask = QMUX_EV_SEND_FRM, .name = "send_frm", .desc = "sending QUIC frame" },
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020054/* special event dedicated to qcs_xfer_data */
55#define QMUX_EV_QCS_XFER_DATA (1ULL << 14)
56 { .mask = QMUX_EV_QCS_XFER_DATA, .name = "qcs_xfer_data", .desc = "qcs_xfer_data" },
57/* special event dedicated to qcs_build_stream_frm */
58#define QMUX_EV_QCS_BUILD_STRM (1ULL << 15)
59 { .mask = QMUX_EV_QCS_BUILD_STRM, .name = "qcs_build_stream_frm", .desc = "qcs_build_stream_frm" },
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010060 { }
61};
62
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020063/* custom arg for QMUX_EV_QCS_XFER_DATA */
64struct qcs_xfer_data_trace_arg {
65 size_t prep;
Amaury Denoyellefdcec362022-03-25 09:28:10 +010066 int xfer;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020067};
68/* custom arg for QMUX_EV_QCS_BUILD_STRM */
69struct qcs_build_stream_trace_arg {
70 size_t len;
Amaury Denoyellefdcec362022-03-25 09:28:10 +010071 char fin;
72 uint64_t offset;
73};
74
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010075static const struct name_desc qmux_trace_lockon_args[4] = {
76 /* arg1 */ { /* already used by the connection */ },
77 /* arg2 */ { .name="qcs", .desc="QUIC stream" },
78 /* arg3 */ { },
79 /* arg4 */ { }
80};
81
82static const struct name_desc qmux_trace_decoding[] = {
83#define QMUX_VERB_CLEAN 1
84 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
85#define QMUX_VERB_MINIMAL 2
86 { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" },
87 { /* end */ }
88};
89
90struct trace_source trace_qmux = {
91 .name = IST("qmux"),
92 .desc = "QUIC multiplexer",
93 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
94 .default_cb = qmux_trace,
95 .known_events = qmux_trace_events,
96 .lockon_args = qmux_trace_lockon_args,
97 .decoding = qmux_trace_decoding,
98 .report_events = ~0, /* report everything by default */
99};
100
101#define TRACE_SOURCE &trace_qmux
102INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
103
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100104/* Allocate a new QUIC streams with id <id> and type <type>. */
105struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100106{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100107 struct qcs *qcs;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200108 struct qc_stream_desc *stream;
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)
114 goto out;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100115
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200116 /* allocate transport layer stream descriptor
117 *
118 * TODO qc_stream_desc is only useful for Tx buffering. It should not
119 * be required for unidirectional remote streams.
120 */
121 stream = qc_stream_desc_new(id, qcs, qcc->conn->handle.qc);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200122 if (!stream) {
123 pool_free(pool_head_qcs, qcs);
124 qcs = NULL;
125 goto out;
126 }
127
128 qcs->stream = stream;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100129 qcs->qcc = qcc;
130 qcs->cs = NULL;
131 qcs->flags = QC_SF_NONE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100132
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100133 qcs->endp = cs_endpoint_new();
134 if (!qcs->endp) {
135 pool_free(pool_head_qcs, qcs);
136 return NULL;
137 }
138 qcs->endp->target = qcs;
139 qcs->endp->ctx = qcc->conn;
140 qcs->endp->flags |= (CS_EP_T_MUX|CS_EP_ORPHAN|CS_EP_NOT_FIRST);
141
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200142 qcs->id = qcs->by_id.key = id;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200143 /* store transport layer stream descriptor in qcc tree */
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200144 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100145
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100146 qcc->strms[type].nb_streams++;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100147
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100148 /* If stream is local, use peer remote-limit, or else the opposite. */
149 /* TODO use uni limit for unidirectional streams */
150 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
151 qcc->rfctl.msd_bidi_l;
152
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100153 qcs->rx.buf = BUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100154 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100155 qcs->rx.offset = 0;
156 qcs->rx.frms = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100157
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100158 qcs->tx.buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100159 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100160 qcs->tx.sent_offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100161
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100162 qcs->wait_event.tasklet = NULL;
163 qcs->wait_event.events = 0;
164 qcs->subs = NULL;
165
166 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100167 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100168 return qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100169}
170
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200171/* Free a qcs. This function must only be done to remove a stream on allocation
172 * error or connection shutdown. Else use qcs_destroy which handle all the
173 * QUIC connection mechanism.
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100174 */
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200175void qcs_free(struct qcs *qcs)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100176{
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200177 b_free(&qcs->rx.buf);
178 b_free(&qcs->tx.buf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200179
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200180 BUG_ON(!qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams);
181 --qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams;
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200182
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200183 qc_stream_desc_release(qcs->stream);
184
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100185 BUG_ON(qcs->endp && !(qcs->endp->flags & CS_EP_ORPHAN));
186 cs_endpoint_free(qcs->endp);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200187
188 eb64_delete(&qcs->by_id);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100189 pool_free(pool_head_qcs, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100190}
191
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100192struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100193{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100194 struct buffer *buf = b_alloc(bptr);
195 BUG_ON(!buf);
196 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100197}
198
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100199int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
200{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100201 struct qcc *qcc = qcs->qcc;
202
203 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100204
205 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
206 BUG_ON(qcs->subs && qcs->subs != es);
207
208 es->events |= event_type;
209 qcs->subs = es;
210
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100211 if (event_type & SUB_RETRY_RECV)
212 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
213
214 if (event_type & SUB_RETRY_SEND)
215 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
216
217 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
218
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100219 return 0;
220}
221
222void qcs_notify_recv(struct qcs *qcs)
223{
224 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
225 tasklet_wakeup(qcs->subs->tasklet);
226 qcs->subs->events &= ~SUB_RETRY_RECV;
227 if (!qcs->subs->events)
228 qcs->subs = NULL;
229 }
230}
231
232void qcs_notify_send(struct qcs *qcs)
233{
234 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
235 tasklet_wakeup(qcs->subs->tasklet);
236 qcs->subs->events &= ~SUB_RETRY_SEND;
237 if (!qcs->subs->events)
238 qcs->subs = NULL;
239 }
240}
241
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100242/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
243 * several streams, depending on the already open ones.
244 * Return this node if succeeded, NULL if not.
245 */
Amaury Denoyelle50742292022-03-29 14:57:19 +0200246struct qcs *qcc_get_qcs(struct qcc *qcc, uint64_t id)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100247{
248 unsigned int strm_type;
249 int64_t sub_id;
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200250 struct eb64_node *node;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200251 struct qcs *qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100252
253 strm_type = id & QCS_ID_TYPE_MASK;
254 sub_id = id >> QCS_ID_TYPE_SHIFT;
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200255 node = NULL;
Amaury Denoyelle0dc40f02022-02-07 11:44:17 +0100256 if (quic_stream_is_local(qcc, id)) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100257 /* Local streams: this stream must be already opened. */
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200258 node = eb64_lookup(&qcc->streams_by_id, id);
259 if (!node) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100260 /* unknown stream id */
261 goto out;
262 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200263 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100264 }
265 else {
266 /* Remote streams. */
267 struct eb_root *strms;
268 uint64_t largest_id;
269 enum qcs_type qcs_type;
270
271 strms = &qcc->streams_by_id;
272 qcs_type = qcs_id_type(id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100273
274 /* TODO also checks max-streams for uni streams */
275 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100276 if (sub_id + 1 > qcc->lfctl.ms_bidi) {
Amaury Denoyellec055e302022-02-07 16:09:06 +0100277 /* streams limit reached */
278 goto out;
279 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100280 }
281
282 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
283 * correct value.
284 */
285 largest_id = qcc->strms[qcs_type].largest_id;
286 if (sub_id > (int64_t)largest_id) {
287 /* RFC: "A stream ID that is used out of order results in all streams
288 * of that type with lower-numbered stream IDs also being opened".
289 * So, let's "open" these streams.
290 */
291 int64_t i;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200292 struct qcs *tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100293
Amaury Denoyelle50742292022-03-29 14:57:19 +0200294 tmp_qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100295 for (i = largest_id + 1; i <= sub_id; i++) {
296 uint64_t id = (i << QCS_ID_TYPE_SHIFT) | strm_type;
297 enum qcs_type type = id & QCS_ID_DIR_BIT ? QCS_CLT_UNI : QCS_CLT_BIDI;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200298
Amaury Denoyelle50742292022-03-29 14:57:19 +0200299 tmp_qcs = qcs_new(qcc, id, type);
300 if (!tmp_qcs) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100301 /* allocation failure */
302 goto out;
303 }
304
305 qcc->strms[qcs_type].largest_id = i;
306 }
Amaury Denoyelle50742292022-03-29 14:57:19 +0200307 if (tmp_qcs)
308 qcs = tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100309 }
310 else {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200311 node = eb64_lookup(strms, id);
312 if (node)
313 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100314 }
315 }
316
Amaury Denoyelle50742292022-03-29 14:57:19 +0200317 return qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100318
319 out:
320 return NULL;
321}
322
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100323/* Handle a new STREAM frame <strm_frm>. The frame content will be copied in
324 * the buffer of the stream instance. The stream instance will be stored in
325 * <out_qcs>. In case of success, the caller can immediatly call qcc_decode_qcs
326 * to process the frame content.
327 *
328 * Returns 0 on success. On errors, two codes are present.
329 * - 1 is returned if the frame cannot be decoded and must be discarded.
330 * - 2 is returned if the stream cannot decode at the moment the frame. The
331 * frame should be buffered to be handled later.
332 */
333int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
334 char fin, char *data, struct qcs **out_qcs)
335{
336 struct qcs *qcs;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100337 size_t total, diff;
338
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100339 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
340
Amaury Denoyelle50742292022-03-29 14:57:19 +0200341 qcs = qcc_get_qcs(qcc, id);
342 if (!qcs) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100343 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 +0100344 return 1;
345 }
346
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100347 *out_qcs = qcs;
348
349 if (offset > qcs->rx.offset)
350 return 2;
351
352 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100353 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 +0100354 return 1;
355 }
356
357 /* Last frame already handled for this stream. */
358 BUG_ON(qcs->flags & QC_SF_FIN_RECV);
359
360 if (!qc_get_buf(qcs, &qcs->rx.buf)) {
361 /* TODO should mark qcs as full */
362 return 2;
363 }
364
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100365 TRACE_DEVEL("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100366 diff = qcs->rx.offset - offset;
367
368 /* TODO do not partially copy a frame if not enough size left. Maybe
369 * this can be optimized.
370 */
371 if (len > b_room(&qcs->rx.buf)) {
372 /* TODO handle STREAM frames larger than RX buffer. */
373 BUG_ON(len > b_size(&qcs->rx.buf));
374 return 2;
375 }
376
377 len -= diff;
378 data += diff;
379
380 total = b_putblk(&qcs->rx.buf, data, len);
381 /* TODO handle partial copy of a STREAM frame. */
382 BUG_ON(len != total);
383
384 qcs->rx.offset += total;
385
386 if (fin)
387 qcs->flags |= QC_SF_FIN_RECV;
388
389 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100390 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100391 return 0;
392}
393
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100394/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
395 * the frame.
396 *
397 * Returns 0 on success else non-zero.
398 */
399int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
400{
401 if (qcc->rfctl.md < max) {
402 qcc->rfctl.md = max;
403
404 if (qcc->flags & QC_CF_BLK_MFCTL) {
405 qcc->flags &= ~QC_CF_BLK_MFCTL;
406 tasklet_wakeup(qcc->wait_event.tasklet);
407 }
408 }
409 return 0;
410}
411
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100412/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
413 * field of the frame and <id> is the identifier of the QUIC stream.
414 *
415 * Returns 0 on success else non-zero.
416 */
417int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
418{
419 struct qcs *qcs;
420 struct eb64_node *node;
421
422 node = eb64_lookup(&qcc->streams_by_id, id);
423 if (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200424 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100425 if (max > qcs->tx.msd) {
426 qcs->tx.msd = max;
427
428 if (qcs->flags & QC_SF_BLK_SFCTL) {
429 qcs->flags &= ~QC_SF_BLK_SFCTL;
430 tasklet_wakeup(qcc->wait_event.tasklet);
431 }
432 }
433 }
434
435 return 0;
436}
437
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100438/* Decode the content of STREAM frames already received on the stream instance
439 * <qcs>.
440 *
441 * Returns 0 on success else non-zero.
442 */
443int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
444{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100445 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
446
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100447 if (qcc->app_ops->decode_qcs(qcs, qcs->flags & QC_SF_FIN_RECV, qcc->ctx) < 0) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100448 TRACE_DEVEL("leaving on decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100449 return 1;
450 }
451
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100452 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
453
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100454 return 0;
455}
456
Amaury Denoyellec055e302022-02-07 16:09:06 +0100457static int qc_is_max_streams_needed(struct qcc *qcc)
458{
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100459 return qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100460}
461
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500462/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100463static void qcs_destroy(struct qcs *qcs)
464{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100465 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200466 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100467
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100468 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100469
Amaury Denoyellec055e302022-02-07 16:09:06 +0100470 if (quic_stream_is_remote(qcs->qcc, id)) {
471 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100472 ++qcs->qcc->lfctl.cl_bidi_r;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100473 if (qc_is_max_streams_needed(qcs->qcc))
474 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
475 }
476 }
477
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200478 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100479
480 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100481}
482
483static inline int qcc_is_dead(const struct qcc *qcc)
484{
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200485 if (qcc->app_ops && qcc->app_ops->is_active &&
486 qcc->app_ops->is_active(qcc, qcc->ctx))
487 return 0;
488
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200489 if ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task)
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100490 return 1;
491
492 return 0;
493}
494
495/* Return true if the mux timeout should be armed. */
496static inline int qcc_may_expire(struct qcc *qcc)
497{
Amaury Denoyelle06890aa2022-04-04 16:15:06 +0200498 return !qcc->nb_cs;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100499}
500
501/* release function. This one should be called to free all resources allocated
502 * to the mux.
503 */
504static void qc_release(struct qcc *qcc)
505{
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200506 struct connection *conn = qcc->conn;
507 struct eb64_node *node;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100508
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100509 TRACE_ENTER(QMUX_EV_QCC_END);
510
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200511 if (qcc->app_ops && qcc->app_ops->release)
512 qcc->app_ops->release(qcc->ctx);
Amaury Denoyellef8909452022-03-30 11:51:56 +0200513
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200514 if (qcc->task) {
515 task_destroy(qcc->task);
516 qcc->task = NULL;
517 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100518
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200519 if (qcc->wait_event.tasklet)
520 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyellef8909452022-03-30 11:51:56 +0200521
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200522 /* liberate remaining qcs instances */
523 node = eb64_first(&qcc->streams_by_id);
524 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200525 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200526 node = eb64_next(node);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200527 qcs_free(qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100528 }
529
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200530 pool_free(pool_head_qcc, qcc);
531
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100532 if (conn) {
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100533 LIST_DEL_INIT(&conn->stopping_list);
534
Willy Tarreau784b8682022-04-11 14:18:10 +0200535 conn->handle.qc->conn = NULL;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100536 conn->mux = NULL;
537 conn->ctx = NULL;
538
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100539 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
540
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100541 conn_stop_tracking(conn);
542 conn_full_close(conn);
543 if (conn->destroy_cb)
544 conn->destroy_cb(conn);
545 conn_free(conn);
546 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100547
548 TRACE_LEAVE(QMUX_EV_QCC_END);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100549}
550
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100551/* Prepare a STREAM frame for <qcs> instance. First, transfer data from
552 * <payload> to <out> buffer. The STREAM frame payload points to the <out>
553 * buffer. The frame is then pushed to <frm_list>. If <fin> is set, and the
554 * <payload> buf is emptied after transfer, FIN bit is set on the STREAM frame.
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100555 * Transfer is automatically adjusted to not exceed the stream flow-control
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100556 * limit. <max_data> must contains the current sum offsets for the connection.
557 * This is useful to not exceed the connection flow-control limit when using
558 * repeatdly this function on multiple streams before passing the data to the
559 * lower layer.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100560 *
561 * Returns the total bytes of newly transferred data or a negative error code.
562 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200563static int qcs_xfer_data(struct qcs *qcs, struct buffer *out,
564 struct buffer *payload, uint64_t max_data)
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200565{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100566 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200567 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100568 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200569
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100570 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100571
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100572 qc_get_buf(qcs, out);
573
574 /*
575 * QCS out buffer diagram
576 * head left to_xfer
577 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100578 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100579 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100580 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100581 * ^ ack-off ^ sent-off ^ off
582 *
583 * STREAM frame
584 * ^ ^
585 * |xxxxxxxxxxxxxxxxx|
586 */
587
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200588 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100589 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
590
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100591 left = qcs->tx.offset - qcs->tx.sent_offset;
592 to_xfer = QUIC_MIN(b_data(payload), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100593
594 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
595 /* do not exceed flow control limit */
596 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
597 to_xfer = qcs->tx.msd - qcs->tx.offset;
598
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100599 BUG_ON_HOT(max_data > qcc->rfctl.md);
600 /* do not overcome flow control limit on connection */
601 if (max_data + to_xfer > qcc->rfctl.md)
602 to_xfer = qcc->rfctl.md - max_data;
603
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100604 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200605 goto out;
606
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200607 total = b_force_xfer(out, payload, to_xfer);
608
609 out:
610 {
611 struct qcs_xfer_data_trace_arg arg = {
612 .prep = b_data(out), .xfer = total,
613 };
614 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
615 qcc->conn, qcs, &arg);
616 }
617
618 return total;
619
620 err:
621 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
622 return -1;
623}
624
625static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
626 struct list *frm_list)
627{
628 struct qcc *qcc = qcs->qcc;
629 struct quic_frame *frm;
630 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200631 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200632
633 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
634
Amaury Denoyellea4569202022-04-15 17:29:25 +0200635 /* if ack_offset < buf_offset, it points to an older buffer. */
636 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
637 BUG_ON(qcs->tx.sent_offset < base_off);
638
639 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200640 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200641 BUG_ON(total < 0);
642
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200643 if (!total) {
644 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
645 return 0;
646 }
Amaury Denoyellea4569202022-04-15 17:29:25 +0200647 BUG_ON(qcs->tx.sent_offset >= qcs->tx.offset);
648 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200649
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200650 frm = pool_zalloc(pool_head_quic_frame);
651 if (!frm)
652 goto err;
653
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100654 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200655 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200656 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100657 frm->stream.buf = out;
658 frm->stream.data = (unsigned char *)b_peek(out, head);
659
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100660 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200661 if (fin)
662 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100663
664 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200665 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100666 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200667 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100668
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200669 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
670 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200671
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100672 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100673
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200674 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100675 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200676 struct qcs_build_stream_trace_arg arg = {
677 .len = frm->stream.len, .fin = fin,
678 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100679 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200680 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100681 qcc->conn, qcs, &arg);
682 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100683
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200684 return total;
685
686 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100687 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200688 return -1;
689}
690
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100691/* This function must be called by the upper layer to inform about the sending
692 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
693 * <offset>.
694 */
695void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
696{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100697 struct qcc *qcc = qcs->qcc;
698 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100699
700 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200701 BUG_ON(offset >= qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100702
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100703 /* check if the STREAM frame has already been notified. It can happen
704 * for retransmission.
705 */
706 if (offset + data <= qcs->tx.sent_offset)
707 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100708
709 diff = offset + data - qcs->tx.sent_offset;
710
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100711 /* increase offset sum on connection */
712 qcc->tx.sent_offsets += diff;
713 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
714 if (qcc->tx.sent_offsets == qcc->rfctl.md)
715 qcc->flags |= QC_CF_BLK_MFCTL;
716
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100717 /* increase offset on stream */
718 qcs->tx.sent_offset += diff;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100719 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200720 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100721 if (qcs->tx.sent_offset == qcs->tx.msd)
722 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200723
724 if (qcs->tx.offset == qcs->tx.sent_offset && b_full(&qcs->stream->buf->buf)) {
725 qc_stream_buf_release(qcs->stream);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200726 /* prepare qcs for immediate send retry if data to send */
727 if (b_data(&qcs->tx.buf))
728 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200729 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100730}
731
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100732/* Wrapper for send on transport layer. Send a list of frames <frms> for the
733 * connection <qcc>.
734 *
735 * Returns 0 if all data sent with success else non-zero.
736 */
737static int qc_send_frames(struct qcc *qcc, struct list *frms)
738{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100739 /* TODO implement an opportunistic retry mechanism. This is needed
740 * because qc_send_app_pkts is not completed. It will only prepare data
741 * up to its Tx buffer. The frames left are not send even if the Tx
742 * buffer is emptied by the sendto call.
743 *
744 * To overcome this, we call repeatedly qc_send_app_pkts until we
745 * detect that the transport layer has send nothing. This could happen
746 * on congestion or sendto syscall error.
747 *
748 * When qc_send_app_pkts is improved to handle retry by itself, we can
749 * remove the looping from the MUX.
750 */
751 struct quic_frame *first_frm;
752 uint64_t first_offset = 0;
753 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100754
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100755 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
756
757 if (LIST_ISEMPTY(frms)) {
758 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200759 return 1;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100760 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100761
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200762 LIST_INIT(&qcc->send_retry_list);
763
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100764 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100765 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
766 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
767 first_offset = first_frm->stream.offset.key;
768 first_stream_frame_type = 1;
769 }
770 else {
771 first_stream_frame_type = 0;
772 }
773
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100774 if (!LIST_ISEMPTY(frms))
Willy Tarreau784b8682022-04-11 14:18:10 +0200775 qc_send_app_pkts(qcc->conn->handle.qc, frms);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100776
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100777 /* If there is frames left, check if the transport layer has send some
778 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100779 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100780 if (!LIST_ISEMPTY(frms)) {
781 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
782 goto retry_send;
783
784 /* If the first frame is STREAM, check if its offset has
785 * changed.
786 */
787 if (first_stream_frame_type &&
788 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
789 goto retry_send;
790 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100791 }
792
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100793 /* If there is frames left at this stage, transport layer is blocked.
794 * Subscribe on it to retry later.
795 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100796 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100797 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100798 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
799 SUB_RETRY_SEND, &qcc->wait_event);
800 return 1;
801 }
802
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100803 TRACE_LEAVE(QMUX_EV_QCC_SEND);
804
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100805 return 0;
806}
807
Amaury Denoyellec9337802022-04-04 16:36:34 +0200808/* Send a MAX_STREAM_BIDI frame to update the limit of bidirectional streams
809 * allowed to be opened by the peer. The caller should have first checked if
810 * this is required with qc_is_max_streams_needed.
811 *
812 * Returns 0 on success else non-zero.
813 */
814static int qc_send_max_streams(struct qcc *qcc)
815{
816 struct list frms = LIST_HEAD_INIT(frms);
817 struct quic_frame *frm;
818
819 frm = pool_zalloc(pool_head_quic_frame);
820 BUG_ON(!frm); /* TODO handle this properly */
821
822 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
823 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
824 qcc->lfctl.cl_bidi_r;
825 TRACE_DEVEL("sending MAX_STREAMS frame", QMUX_EV_SEND_FRM, qcc->conn, NULL, frm);
826 LIST_APPEND(&frms, &frm->list);
827
828 if (qc_send_frames(qcc, &frms))
829 return 1;
830
831 /* save the new limit if the frame has been send. */
832 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
833 qcc->lfctl.cl_bidi_r = 0;
834
835 return 0;
836}
837
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200838/* Used internally by qc_send function. Proceed to send for <qcs>. This will
839 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
840 * is then generated and inserted in <frms> list. <qcc_max_data> is the current
841 * flow-control max-data at the connection level which must not be surpassed.
842 *
843 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
844 * be null if out buffer cannot be allocated.
845 */
846static int _qc_send_qcs(struct qcs *qcs, struct list *frms,
847 uint64_t qcc_max_data)
848{
849 struct qcc *qcc = qcs->qcc;
850 struct buffer *buf = &qcs->tx.buf;
851 struct buffer *out = qc_stream_buf_get(qcs->stream);
852 int xfer = 0;
853
854 /* Allocate <out> buffer if necessary. */
855 if (!out) {
856 if (qcc->flags & QC_CF_CONN_FULL)
857 return 0;
858
859 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
860 if (!out) {
861 qcc->flags |= QC_CF_CONN_FULL;
862 return 0;
863 }
864 }
865
866 /* Transfer data from <buf> to <out>. */
867 if (b_data(buf)) {
868 xfer = qcs_xfer_data(qcs, out, buf, qcc_max_data);
869 BUG_ON(xfer < 0); /* TODO handle this properly */
870
871 if (xfer > 0) {
872 qcs_notify_send(qcs);
873 qcs->flags &= ~QC_SF_BLK_MROOM;
874 }
875
876 qcs->tx.offset += xfer;
877 }
878
879 /* out buffer cannot be emptied if qcs offsets differ. */
880 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
881
882 /* Build a new STREAM frame with <out> buffer. */
883 if (qcs->tx.sent_offset != qcs->tx.offset) {
884 int ret;
885 char fin = !!(qcs->flags & QC_SF_FIN_STREAM);
886
887 /* FIN is set if all incoming data were transfered. */
888 fin = !!(fin && !b_data(buf));
889
890 ret = qcs_build_stream_frm(qcs, out, fin, frms);
891 BUG_ON(ret < 0); /* TODO handle this properly */
892 }
893
894 return xfer;
895}
896
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100897/* Proceed to sending. Loop through all available streams for the <qcc>
898 * instance and try to send as much as possible.
899 *
900 * Returns the total of bytes sent to the transport layer.
901 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100902static int qc_send(struct qcc *qcc)
903{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100904 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200905 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200906 struct qcs *qcs, *qcs_tmp;
907 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200908
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100909 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200910
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200911 if (qcc->conn->flags & CO_FL_SOCK_WR_SH) {
912 qcc->conn->flags |= CO_FL_ERROR;
913 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
914 return 0;
915 }
916
Amaury Denoyellec9337802022-04-04 16:36:34 +0200917 if (qc_is_max_streams_needed(qcc))
918 qc_send_max_streams(qcc);
919
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100920 if (qcc->flags & QC_CF_BLK_MFCTL)
921 return 0;
922
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100923 /* loop through all streams, construct STREAM frames if data available.
924 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200925 */
926 node = eb64_first(&qcc->streams_by_id);
927 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200928 int ret;
929 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100930
Amaury Denoyellee2ec9422022-03-10 16:46:18 +0100931 /* TODO
932 * for the moment, unidirectional streams have their own
933 * mechanism for sending. This should be unified in the future,
934 * in this case the next check will be removed.
935 */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200936 if (quic_stream_is_uni(qcs->id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +0100937 node = eb64_next(node);
938 continue;
939 }
940
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100941 if (qcs->flags & QC_SF_BLK_SFCTL) {
942 node = eb64_next(node);
943 continue;
944 }
945
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200946 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +0200947 node = eb64_next(node);
948 continue;
949 }
Amaury Denoyellea4569202022-04-15 17:29:25 +0200950
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200951 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + total);
952 total += ret;
953 node = eb64_next(node);
954 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200955
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200956 if (qc_send_frames(qcc, &frms)) {
957 /* data rejected by transport layer, do not retry. */
958 goto out;
959 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200960
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200961 retry:
962 tmp_total = 0;
963 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
964 int ret;
965 BUG_ON(!b_data(&qcs->tx.buf));
966 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200967
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200968 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + tmp_total);
969 tmp_total += ret;
970 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200971 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200972
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200973 total += tmp_total;
974 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
975 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100976
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200977 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100978 TRACE_LEAVE(QMUX_EV_QCC_SEND);
979
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100980 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100981}
982
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +0100983/* Release all streams that are already marked as detached. This is only done
984 * if their TX buffers are empty or if a CONNECTION_CLOSE has been received.
985 *
986 * Return the number of released stream.
987 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100988static int qc_release_detached_streams(struct qcc *qcc)
989{
990 struct eb64_node *node;
991 int release = 0;
992
993 node = eb64_first(&qcc->streams_by_id);
994 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200995 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100996 node = eb64_next(node);
997
998 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200999 if (!b_data(&qcs->tx.buf) &&
1000 qcs->tx.offset == qcs->tx.sent_offset) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001001 qcs_destroy(qcs);
1002 release = 1;
1003 }
1004 else {
1005 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1006 SUB_RETRY_SEND, &qcc->wait_event);
1007 }
1008 }
1009 }
1010
1011 return release;
1012}
1013
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001014static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1015{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001016 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001017
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001018 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001019
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001020 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001021
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001022 if (qc_release_detached_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001023 if (qcc_is_dead(qcc)) {
1024 qc_release(qcc);
1025 }
1026 else {
1027 if (qcc_may_expire(qcc))
1028 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1029 else
1030 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001031 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001032 }
1033 }
1034
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001035 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1036
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001037 return NULL;
1038}
1039
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001040static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1041{
1042 struct qcc *qcc = ctx;
1043 int expired = tick_is_expired(t->expire, now_ms);
1044
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001045 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001046
1047 if (qcc) {
1048 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001049 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001050 return t;
1051 }
1052
1053 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001054 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001055 t->expire = TICK_ETERNITY;
1056 return t;
1057 }
1058 }
1059
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001060 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001061
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001062 if (!qcc) {
1063 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001064 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001065 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001066
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001067 qcc->task = NULL;
1068
1069 if (qcc_is_dead(qcc))
1070 qc_release(qcc);
1071
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001072 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1073
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001074 return NULL;
1075}
1076
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001077static int qc_init(struct connection *conn, struct proxy *prx,
1078 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001079{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001080 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001081 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001082
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001083 qcc = pool_alloc(pool_head_qcc);
1084 if (!qcc)
1085 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001086
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001087 qcc->conn = conn;
1088 conn->ctx = qcc;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001089 qcc->nb_cs = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001090 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001091
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001092 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001093
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001094 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001095
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001096 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001097 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001098
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001099 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001100 qcc->tx.sent_offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001101
1102 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001103 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001104 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
1105 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001106 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001107 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001108
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001109 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001110 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
1111 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001112 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001113 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001114
1115 /* Server initiated streams must respect the server flow control. */
1116 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001117 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
1118 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001119 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001120 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1121
1122 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001123 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
1124 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001125 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001126 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001127
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001128 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
1129 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001130
Willy Tarreau784b8682022-04-11 14:18:10 +02001131 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001132 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001133 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1134 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1135
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001136 qcc->wait_event.tasklet = tasklet_new();
1137 if (!qcc->wait_event.tasklet)
1138 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001139
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001140 LIST_INIT(&qcc->send_retry_list);
1141
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001142 qcc->subs = NULL;
1143 qcc->wait_event.tasklet->process = qc_io_cb;
1144 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001145 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001146
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001147 /* haproxy timeouts */
1148 qcc->timeout = prx->timeout.client;
1149 qcc->task = task_new_here();
1150 if (!qcc->task)
1151 goto fail_no_timeout_task;
1152 qcc->task->process = qc_timeout_task;
1153 qcc->task->context = qcc;
1154 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1155
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001156 if (!conn_is_back(conn)) {
1157 if (!LIST_INLIST(&conn->stopping_list)) {
1158 LIST_APPEND(&mux_stopping_data[tid].list,
1159 &conn->stopping_list);
1160 }
1161 }
1162
Willy Tarreau784b8682022-04-11 14:18:10 +02001163 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001164 /* init read cycle */
1165 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001166
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001167 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001168
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001169 fail_no_timeout_task:
1170 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001171 fail_no_tasklet:
1172 pool_free(pool_head_qcc, qcc);
1173 fail_no_qcc:
1174 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001175}
1176
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001177static void qc_destroy(void *ctx)
1178{
1179 struct qcc *qcc = ctx;
1180
1181 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
1182 qc_release(qcc);
1183 TRACE_LEAVE(QMUX_EV_QCC_END);
1184}
1185
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001186static void qc_detach(struct conn_stream *cs)
1187{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001188 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001189 struct qcc *qcc = qcs->qcc;
1190
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001191 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001192
Amaury Denoyelle80388212022-04-08 12:00:12 +02001193 qcs->cs = NULL;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001194 --qcc->nb_cs;
1195
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001196 if ((b_data(&qcs->tx.buf) || qcs->tx.offset > qcs->tx.sent_offset) &&
1197 !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001198 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001199 qcs->flags |= QC_SF_DETACH;
1200 return;
1201 }
1202
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001203 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001204
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001205 if (qcc_is_dead(qcc)) {
1206 qc_release(qcc);
1207 }
1208 else {
1209 if (qcc_may_expire(qcc))
1210 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1211 else
1212 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001213 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001214 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001215
1216 TRACE_LEAVE(QMUX_EV_STRM_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001217}
1218
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001219/* Called from the upper layer, to receive data */
1220static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf,
1221 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001222{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001223 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001224 struct htx *qcs_htx = NULL;
1225 struct htx *cs_htx = NULL;
1226 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001227 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001228
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001229 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001230
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001231 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
1232 if (htx_is_empty(qcs_htx)) {
1233 /* Set buffer data to 0 as HTX is empty. */
1234 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1235 goto end;
1236 }
1237
1238 ret = qcs_htx->data;
1239
1240 cs_htx = htx_from_buf(buf);
1241 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
1242 htx_to_buf(cs_htx, buf);
1243 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1244 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
1245 goto end;
1246 }
1247
1248 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
1249 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
1250
1251 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001252 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
1253 cs_htx->flags |= HTX_FL_EOM;
1254 fin = 1;
1255 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001256
1257 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
1258 htx_to_buf(cs_htx, buf);
1259 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1260 ret -= qcs_htx->data;
1261
1262 end:
1263 if (b_data(&qcs->rx.app_buf)) {
Christopher Fauletb041b232022-03-24 10:27:02 +01001264 cs->endp->flags |= (CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001265 }
1266 else {
Christopher Fauletb041b232022-03-24 10:27:02 +01001267 cs->endp->flags &= ~(CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
1268 if (cs->endp->flags & CS_EP_ERR_PENDING)
1269 cs->endp->flags |= CS_EP_ERROR;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001270
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001271 if (fin)
Christopher Fauletb041b232022-03-24 10:27:02 +01001272 cs->endp->flags |= (CS_EP_EOI|CS_EP_EOS);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001273
1274 if (b_size(&qcs->rx.app_buf)) {
1275 b_free(&qcs->rx.app_buf);
1276 offer_buffers(NULL, 1);
1277 }
1278 }
1279
1280 if (ret)
1281 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
1282
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001283 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
1284
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001285 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001286}
1287
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001288static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf,
1289 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001290{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001291 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001292 size_t ret;
1293
1294 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001295
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001296 ret = qcs->qcc->app_ops->snd_buf(cs, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001297
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001298 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
1299
1300 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001301}
1302
1303/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1304 * event subscriber <es> is not allowed to change from a previous call as long
1305 * as at least one event is still subscribed. The <event_type> must only be a
1306 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1307 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001308static int qc_subscribe(struct conn_stream *cs, int event_type,
1309 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001310{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001311 return qcs_subscribe(__cs_mux(cs), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001312}
1313
1314/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1315 * The <es> pointer is not allowed to differ from the one passed to the
1316 * subscribe() call. It always returns zero.
1317 */
1318static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
1319{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001320 struct qcs *qcs = __cs_mux(cs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001321
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001322 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1323 BUG_ON(qcs->subs && qcs->subs != es);
1324
1325 es->events &= ~event_type;
1326 if (!es->events)
1327 qcs->subs = NULL;
1328
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001329 return 0;
1330}
1331
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001332/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Christopher Fauletb041b232022-03-24 10:27:02 +01001333 * report CS_EP_ERR_PENDING|CS_EP_ERROR on the attached conn-streams and wake
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001334 * them.
1335 */
1336static int qc_wake_some_streams(struct qcc *qcc)
1337{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001338 struct qcs *qcs;
1339 struct eb64_node *node;
1340
1341 for (node = eb64_first(&qcc->streams_by_id); node;
1342 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001343 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001344
1345 if (!qcs->cs)
1346 continue;
1347
1348 if (qcc->conn->flags & CO_FL_ERROR) {
Christopher Fauletb041b232022-03-24 10:27:02 +01001349 qcs->endp->flags |= CS_EP_ERR_PENDING;
1350 if (qcs->endp->flags & CS_EP_EOS)
1351 qcs->endp->flags |= CS_EP_ERROR;
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001352
1353 if (qcs->subs) {
1354 qcs_notify_recv(qcs);
1355 qcs_notify_send(qcs);
1356 }
1357 else if (qcs->cs->data_cb->wake) {
1358 qcs->cs->data_cb->wake(qcs->cs);
1359 }
1360 }
1361 }
1362
1363 return 0;
1364}
1365
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001366static int qc_wake(struct connection *conn)
1367{
1368 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02001369 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001370
1371 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001372
1373 /* Check if a soft-stop is in progress.
1374 * Release idling front connection if this is the case.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001375 *
1376 * TODO this is revelant for frontend connections only.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001377 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001378 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
1379 goto release;
1380
Willy Tarreau784b8682022-04-11 14:18:10 +02001381 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02001382 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
1383
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001384 qc_send(qcc);
1385
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001386 qc_wake_some_streams(qcc);
1387
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001388 if (qcc_is_dead(qcc))
1389 goto release;
1390
1391 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
1392
1393 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001394
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001395 release:
1396 qc_release(qcc);
1397 TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001398 return 1;
1399}
1400
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001401
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001402static void qmux_trace_frm(const struct quic_frame *frm)
1403{
1404 switch (frm->type) {
1405 case QUIC_FT_MAX_STREAMS_BIDI:
1406 chunk_appendf(&trace_buf, " max_streams=%lu",
1407 frm->max_streams_bidi.max_streams);
1408 break;
1409
1410 case QUIC_FT_MAX_STREAMS_UNI:
1411 chunk_appendf(&trace_buf, " max_streams=%lu",
1412 frm->max_streams_uni.max_streams);
1413 break;
1414
1415 default:
1416 break;
1417 }
1418}
1419
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001420/* quic-mux trace handler */
1421static void qmux_trace(enum trace_level level, uint64_t mask,
1422 const struct trace_source *src,
1423 const struct ist where, const struct ist func,
1424 const void *a1, const void *a2, const void *a3, const void *a4)
1425{
1426 const struct connection *conn = a1;
1427 const struct qcc *qcc = conn ? conn->ctx : NULL;
1428 const struct qcs *qcs = a2;
1429
1430 if (!qcc)
1431 return;
1432
1433 if (src->verbosity > QMUX_VERB_CLEAN) {
1434 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
1435
1436 if (qcs)
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001437 chunk_appendf(&trace_buf, " qcs=%p(%lu)", qcs, qcs->id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001438
1439 if (mask & QMUX_EV_QCC_NQCS) {
1440 const uint64_t *id = a3;
1441 chunk_appendf(&trace_buf, " id=%lu", *id);
1442 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001443
1444 if (mask & QMUX_EV_SEND_FRM)
1445 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001446
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001447 if (mask & QMUX_EV_QCS_XFER_DATA) {
1448 const struct qcs_xfer_data_trace_arg *arg = a3;
1449 chunk_appendf(&trace_buf, " prep=%lu xfer=%d",
1450 arg->prep, arg->xfer);
1451 }
1452
1453 if (mask & QMUX_EV_QCS_BUILD_STRM) {
1454 const struct qcs_build_stream_trace_arg *arg = a3;
1455 chunk_appendf(&trace_buf, " len=%lu fin=%d offset=%lu",
1456 arg->len, arg->fin, arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001457 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001458 }
1459}
1460
Amaury Denoyelle251eadf2022-03-24 17:14:52 +01001461/* Function to automatically activate QUIC MUX traces on stdout.
1462 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
1463 * Main use for now is in the docker image for QUIC interop testing.
1464 */
1465static void qmux_init_stdout_traces(void)
1466{
1467#ifdef ENABLE_QUIC_STDOUT_TRACES
1468 trace_qmux.sink = sink_find("stdout");
1469 trace_qmux.level = TRACE_LEVEL_DEVELOPER;
1470 trace_qmux.state = TRACE_STATE_RUNNING;
1471 trace_qmux.verbosity = QMUX_VERB_MINIMAL;
1472#endif
1473}
1474INITCALL0(STG_INIT, qmux_init_stdout_traces);
1475
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001476
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001477static const struct mux_ops qc_ops = {
1478 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001479 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001480 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001481 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001482 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001483 .subscribe = qc_subscribe,
1484 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001485 .wake = qc_wake,
Christopher Fauleta97cced2022-04-12 18:04:10 +02001486 .flags = MX_FL_HTX|MX_FL_NO_UPG,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02001487 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001488};
1489
1490static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001491 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001492
1493INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);