blob: 04f0c71edf4ad4066941812250da7bf5cd4ba7c8 [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 Denoyelleb7104152022-04-22 17:38:43 +0200452 qcs_notify_recv(qcs);
453
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100454 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
455
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100456 return 0;
457}
458
Amaury Denoyellec055e302022-02-07 16:09:06 +0100459static int qc_is_max_streams_needed(struct qcc *qcc)
460{
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100461 return qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100462}
463
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500464/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100465static void qcs_destroy(struct qcs *qcs)
466{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100467 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200468 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100469
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100470 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100471
Amaury Denoyellec055e302022-02-07 16:09:06 +0100472 if (quic_stream_is_remote(qcs->qcc, id)) {
473 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100474 ++qcs->qcc->lfctl.cl_bidi_r;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100475 if (qc_is_max_streams_needed(qcs->qcc))
476 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
477 }
478 }
479
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200480 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100481
482 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100483}
484
485static inline int qcc_is_dead(const struct qcc *qcc)
486{
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200487 if (qcc->app_ops && qcc->app_ops->is_active &&
488 qcc->app_ops->is_active(qcc, qcc->ctx))
489 return 0;
490
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200491 if ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task)
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100492 return 1;
493
494 return 0;
495}
496
497/* Return true if the mux timeout should be armed. */
498static inline int qcc_may_expire(struct qcc *qcc)
499{
Amaury Denoyelle06890aa2022-04-04 16:15:06 +0200500 return !qcc->nb_cs;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100501}
502
503/* release function. This one should be called to free all resources allocated
504 * to the mux.
505 */
506static void qc_release(struct qcc *qcc)
507{
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200508 struct connection *conn = qcc->conn;
509 struct eb64_node *node;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100510
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100511 TRACE_ENTER(QMUX_EV_QCC_END);
512
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200513 if (qcc->app_ops && qcc->app_ops->release)
514 qcc->app_ops->release(qcc->ctx);
Amaury Denoyellef8909452022-03-30 11:51:56 +0200515
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200516 if (qcc->task) {
517 task_destroy(qcc->task);
518 qcc->task = NULL;
519 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100520
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200521 if (qcc->wait_event.tasklet)
522 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyellef3e03a42022-04-21 15:41:34 +0200523 if (conn && qcc->wait_event.events) {
524 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
525 qcc->wait_event.events,
526 &qcc->wait_event);
527 }
Amaury Denoyellef8909452022-03-30 11:51:56 +0200528
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200529 /* liberate remaining qcs instances */
530 node = eb64_first(&qcc->streams_by_id);
531 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200532 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200533 node = eb64_next(node);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200534 qcs_free(qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100535 }
536
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200537 pool_free(pool_head_qcc, qcc);
538
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100539 if (conn) {
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100540 LIST_DEL_INIT(&conn->stopping_list);
541
Willy Tarreau784b8682022-04-11 14:18:10 +0200542 conn->handle.qc->conn = NULL;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100543 conn->mux = NULL;
544 conn->ctx = NULL;
545
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100546 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
547
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100548 conn_stop_tracking(conn);
549 conn_full_close(conn);
550 if (conn->destroy_cb)
551 conn->destroy_cb(conn);
552 conn_free(conn);
553 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100554
555 TRACE_LEAVE(QMUX_EV_QCC_END);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100556}
557
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100558/* Prepare a STREAM frame for <qcs> instance. First, transfer data from
559 * <payload> to <out> buffer. The STREAM frame payload points to the <out>
560 * buffer. The frame is then pushed to <frm_list>. If <fin> is set, and the
561 * <payload> buf is emptied after transfer, FIN bit is set on the STREAM frame.
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100562 * Transfer is automatically adjusted to not exceed the stream flow-control
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100563 * limit. <max_data> must contains the current sum offsets for the connection.
564 * This is useful to not exceed the connection flow-control limit when using
565 * repeatdly this function on multiple streams before passing the data to the
566 * lower layer.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100567 *
Amaury Denoyelle3eb892f2022-04-22 09:47:58 +0200568 * Returns the total bytes of newly transferred data. It may be 0 if none.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100569 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200570static int qcs_xfer_data(struct qcs *qcs, struct buffer *out,
571 struct buffer *payload, uint64_t max_data)
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200572{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100573 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200574 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100575 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200576
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100577 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100578
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100579 qc_get_buf(qcs, out);
580
581 /*
582 * QCS out buffer diagram
583 * head left to_xfer
584 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100585 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100586 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100587 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100588 * ^ ack-off ^ sent-off ^ off
589 *
590 * STREAM frame
591 * ^ ^
592 * |xxxxxxxxxxxxxxxxx|
593 */
594
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200595 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100596 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
597
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100598 left = qcs->tx.offset - qcs->tx.sent_offset;
599 to_xfer = QUIC_MIN(b_data(payload), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100600
601 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
602 /* do not exceed flow control limit */
603 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
604 to_xfer = qcs->tx.msd - qcs->tx.offset;
605
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100606 BUG_ON_HOT(max_data > qcc->rfctl.md);
607 /* do not overcome flow control limit on connection */
608 if (max_data + to_xfer > qcc->rfctl.md)
609 to_xfer = qcc->rfctl.md - max_data;
610
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100611 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200612 goto out;
613
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200614 total = b_force_xfer(out, payload, to_xfer);
615
616 out:
617 {
618 struct qcs_xfer_data_trace_arg arg = {
619 .prep = b_data(out), .xfer = total,
620 };
621 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
622 qcc->conn, qcs, &arg);
623 }
624
625 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200626}
627
628static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
629 struct list *frm_list)
630{
631 struct qcc *qcc = qcs->qcc;
632 struct quic_frame *frm;
633 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200634 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200635
636 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
637
Amaury Denoyellea4569202022-04-15 17:29:25 +0200638 /* if ack_offset < buf_offset, it points to an older buffer. */
639 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
640 BUG_ON(qcs->tx.sent_offset < base_off);
641
642 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200643 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200644 BUG_ON(total < 0);
645
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200646 if (!total) {
647 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
648 return 0;
649 }
Amaury Denoyellea4569202022-04-15 17:29:25 +0200650 BUG_ON(qcs->tx.sent_offset >= qcs->tx.offset);
651 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200652
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200653 frm = pool_zalloc(pool_head_quic_frame);
654 if (!frm)
655 goto err;
656
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100657 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200658 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200659 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100660 frm->stream.buf = out;
661 frm->stream.data = (unsigned char *)b_peek(out, head);
662
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100663 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200664 if (fin)
665 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100666
667 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200668 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100669 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200670 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100671
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200672 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
673 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200674
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100675 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100676
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200677 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100678 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200679 struct qcs_build_stream_trace_arg arg = {
680 .len = frm->stream.len, .fin = fin,
681 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100682 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200683 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100684 qcc->conn, qcs, &arg);
685 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100686
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200687 return total;
688
689 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100690 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200691 return -1;
692}
693
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100694/* This function must be called by the upper layer to inform about the sending
695 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
696 * <offset>.
697 */
698void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
699{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100700 struct qcc *qcc = qcs->qcc;
701 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100702
703 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200704 BUG_ON(offset >= qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100705
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100706 /* check if the STREAM frame has already been notified. It can happen
707 * for retransmission.
708 */
709 if (offset + data <= qcs->tx.sent_offset)
710 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100711
712 diff = offset + data - qcs->tx.sent_offset;
713
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100714 /* increase offset sum on connection */
715 qcc->tx.sent_offsets += diff;
716 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
717 if (qcc->tx.sent_offsets == qcc->rfctl.md)
718 qcc->flags |= QC_CF_BLK_MFCTL;
719
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100720 /* increase offset on stream */
721 qcs->tx.sent_offset += diff;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100722 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200723 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100724 if (qcs->tx.sent_offset == qcs->tx.msd)
725 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200726
727 if (qcs->tx.offset == qcs->tx.sent_offset && b_full(&qcs->stream->buf->buf)) {
728 qc_stream_buf_release(qcs->stream);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200729 /* prepare qcs for immediate send retry if data to send */
730 if (b_data(&qcs->tx.buf))
731 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200732 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100733}
734
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100735/* Wrapper for send on transport layer. Send a list of frames <frms> for the
736 * connection <qcc>.
737 *
738 * Returns 0 if all data sent with success else non-zero.
739 */
740static int qc_send_frames(struct qcc *qcc, struct list *frms)
741{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100742 /* TODO implement an opportunistic retry mechanism. This is needed
743 * because qc_send_app_pkts is not completed. It will only prepare data
744 * up to its Tx buffer. The frames left are not send even if the Tx
745 * buffer is emptied by the sendto call.
746 *
747 * To overcome this, we call repeatedly qc_send_app_pkts until we
748 * detect that the transport layer has send nothing. This could happen
749 * on congestion or sendto syscall error.
750 *
751 * When qc_send_app_pkts is improved to handle retry by itself, we can
752 * remove the looping from the MUX.
753 */
754 struct quic_frame *first_frm;
755 uint64_t first_offset = 0;
756 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100757
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100758 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
759
760 if (LIST_ISEMPTY(frms)) {
761 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200762 return 1;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100763 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100764
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200765 LIST_INIT(&qcc->send_retry_list);
766
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100767 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100768 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
769 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
770 first_offset = first_frm->stream.offset.key;
771 first_stream_frame_type = 1;
772 }
773 else {
774 first_stream_frame_type = 0;
775 }
776
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100777 if (!LIST_ISEMPTY(frms))
Willy Tarreau784b8682022-04-11 14:18:10 +0200778 qc_send_app_pkts(qcc->conn->handle.qc, frms);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100779
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100780 /* If there is frames left, check if the transport layer has send some
781 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100782 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100783 if (!LIST_ISEMPTY(frms)) {
784 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
785 goto retry_send;
786
787 /* If the first frame is STREAM, check if its offset has
788 * changed.
789 */
790 if (first_stream_frame_type &&
791 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
792 goto retry_send;
793 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100794 }
795
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100796 /* If there is frames left at this stage, transport layer is blocked.
797 * Subscribe on it to retry later.
798 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100799 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100800 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100801 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
802 SUB_RETRY_SEND, &qcc->wait_event);
803 return 1;
804 }
805
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100806 TRACE_LEAVE(QMUX_EV_QCC_SEND);
807
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100808 return 0;
809}
810
Amaury Denoyellec9337802022-04-04 16:36:34 +0200811/* Send a MAX_STREAM_BIDI frame to update the limit of bidirectional streams
812 * allowed to be opened by the peer. The caller should have first checked if
813 * this is required with qc_is_max_streams_needed.
814 *
815 * Returns 0 on success else non-zero.
816 */
817static int qc_send_max_streams(struct qcc *qcc)
818{
819 struct list frms = LIST_HEAD_INIT(frms);
820 struct quic_frame *frm;
821
822 frm = pool_zalloc(pool_head_quic_frame);
823 BUG_ON(!frm); /* TODO handle this properly */
824
825 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
826 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
827 qcc->lfctl.cl_bidi_r;
828 TRACE_DEVEL("sending MAX_STREAMS frame", QMUX_EV_SEND_FRM, qcc->conn, NULL, frm);
829 LIST_APPEND(&frms, &frm->list);
830
831 if (qc_send_frames(qcc, &frms))
832 return 1;
833
834 /* save the new limit if the frame has been send. */
835 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
836 qcc->lfctl.cl_bidi_r = 0;
837
838 return 0;
839}
840
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200841/* Used internally by qc_send function. Proceed to send for <qcs>. This will
842 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
843 * is then generated and inserted in <frms> list. <qcc_max_data> is the current
844 * flow-control max-data at the connection level which must not be surpassed.
845 *
846 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
847 * be null if out buffer cannot be allocated.
848 */
849static int _qc_send_qcs(struct qcs *qcs, struct list *frms,
850 uint64_t qcc_max_data)
851{
852 struct qcc *qcc = qcs->qcc;
853 struct buffer *buf = &qcs->tx.buf;
854 struct buffer *out = qc_stream_buf_get(qcs->stream);
855 int xfer = 0;
856
857 /* Allocate <out> buffer if necessary. */
858 if (!out) {
859 if (qcc->flags & QC_CF_CONN_FULL)
860 return 0;
861
862 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
863 if (!out) {
864 qcc->flags |= QC_CF_CONN_FULL;
865 return 0;
866 }
867 }
868
869 /* Transfer data from <buf> to <out>. */
870 if (b_data(buf)) {
871 xfer = qcs_xfer_data(qcs, out, buf, qcc_max_data);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200872 if (xfer > 0) {
873 qcs_notify_send(qcs);
874 qcs->flags &= ~QC_SF_BLK_MROOM;
875 }
876
877 qcs->tx.offset += xfer;
878 }
879
880 /* out buffer cannot be emptied if qcs offsets differ. */
881 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
882
883 /* Build a new STREAM frame with <out> buffer. */
884 if (qcs->tx.sent_offset != qcs->tx.offset) {
885 int ret;
886 char fin = !!(qcs->flags & QC_SF_FIN_STREAM);
887
888 /* FIN is set if all incoming data were transfered. */
889 fin = !!(fin && !b_data(buf));
890
891 ret = qcs_build_stream_frm(qcs, out, fin, frms);
892 BUG_ON(ret < 0); /* TODO handle this properly */
893 }
894
895 return xfer;
896}
897
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100898/* Proceed to sending. Loop through all available streams for the <qcc>
899 * instance and try to send as much as possible.
900 *
901 * Returns the total of bytes sent to the transport layer.
902 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100903static int qc_send(struct qcc *qcc)
904{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100905 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200906 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200907 struct qcs *qcs, *qcs_tmp;
908 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200909
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100910 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200911
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200912 if (qcc->conn->flags & CO_FL_SOCK_WR_SH) {
913 qcc->conn->flags |= CO_FL_ERROR;
914 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
915 return 0;
916 }
917
Amaury Denoyellec9337802022-04-04 16:36:34 +0200918 if (qc_is_max_streams_needed(qcc))
919 qc_send_max_streams(qcc);
920
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100921 if (qcc->flags & QC_CF_BLK_MFCTL)
922 return 0;
923
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100924 /* loop through all streams, construct STREAM frames if data available.
925 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200926 */
927 node = eb64_first(&qcc->streams_by_id);
928 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200929 int ret;
930 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100931
Amaury Denoyellee2ec9422022-03-10 16:46:18 +0100932 /* TODO
933 * for the moment, unidirectional streams have their own
934 * mechanism for sending. This should be unified in the future,
935 * in this case the next check will be removed.
936 */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200937 if (quic_stream_is_uni(qcs->id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +0100938 node = eb64_next(node);
939 continue;
940 }
941
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100942 if (qcs->flags & QC_SF_BLK_SFCTL) {
943 node = eb64_next(node);
944 continue;
945 }
946
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200947 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +0200948 node = eb64_next(node);
949 continue;
950 }
Amaury Denoyellea4569202022-04-15 17:29:25 +0200951
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200952 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + total);
953 total += ret;
954 node = eb64_next(node);
955 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200956
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200957 if (qc_send_frames(qcc, &frms)) {
958 /* data rejected by transport layer, do not retry. */
959 goto out;
960 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200961
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200962 retry:
963 tmp_total = 0;
964 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
965 int ret;
966 BUG_ON(!b_data(&qcs->tx.buf));
967 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200968
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200969 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + tmp_total);
970 tmp_total += ret;
971 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200972 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200973
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200974 total += tmp_total;
975 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
976 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100977
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200978 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100979 TRACE_LEAVE(QMUX_EV_QCC_SEND);
980
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100981 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100982}
983
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +0100984/* Release all streams that are already marked as detached. This is only done
985 * if their TX buffers are empty or if a CONNECTION_CLOSE has been received.
986 *
987 * Return the number of released stream.
988 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100989static int qc_release_detached_streams(struct qcc *qcc)
990{
991 struct eb64_node *node;
992 int release = 0;
993
994 node = eb64_first(&qcc->streams_by_id);
995 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200996 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100997 node = eb64_next(node);
998
999 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001000 if (!b_data(&qcs->tx.buf) &&
1001 qcs->tx.offset == qcs->tx.sent_offset) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001002 qcs_destroy(qcs);
1003 release = 1;
1004 }
1005 else {
1006 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1007 SUB_RETRY_SEND, &qcc->wait_event);
1008 }
1009 }
1010 }
1011
1012 return release;
1013}
1014
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001015static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1016{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001017 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001018
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001019 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001020
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001021 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001022
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001023 if (qc_release_detached_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001024 if (qcc_is_dead(qcc)) {
1025 qc_release(qcc);
1026 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001027 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001028 if (qcc_may_expire(qcc))
1029 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1030 else
1031 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001032 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001033 }
1034 }
1035
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001036 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1037
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001038 return NULL;
1039}
1040
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001041static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1042{
1043 struct qcc *qcc = ctx;
1044 int expired = tick_is_expired(t->expire, now_ms);
1045
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001046 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001047
1048 if (qcc) {
1049 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001050 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001051 return t;
1052 }
1053
1054 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001055 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001056 t->expire = TICK_ETERNITY;
1057 return t;
1058 }
1059 }
1060
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001061 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001062
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001063 if (!qcc) {
1064 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001065 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001066 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001067
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001068 qcc->task = NULL;
1069
1070 if (qcc_is_dead(qcc))
1071 qc_release(qcc);
1072
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001073 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1074
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001075 return NULL;
1076}
1077
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001078static int qc_init(struct connection *conn, struct proxy *prx,
1079 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001080{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001081 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001082 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001083
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001084 qcc = pool_alloc(pool_head_qcc);
1085 if (!qcc)
1086 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001087
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001088 qcc->conn = conn;
1089 conn->ctx = qcc;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001090 qcc->nb_cs = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001091 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001092
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001093 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001094
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001095 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001096
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001097 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001098 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001099
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001100 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001101 qcc->tx.sent_offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001102
1103 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001104 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001105 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
1106 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001107 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001108 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001109
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001110 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001111 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
1112 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001113 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001114 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001115
1116 /* Server initiated streams must respect the server flow control. */
1117 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001118 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
1119 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001120 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001121 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1122
1123 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001124 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
1125 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001126 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001127 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001128
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001129 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
1130 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001131
Willy Tarreau784b8682022-04-11 14:18:10 +02001132 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001133 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001134 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1135 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1136
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001137 qcc->wait_event.tasklet = tasklet_new();
1138 if (!qcc->wait_event.tasklet)
1139 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001140
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001141 LIST_INIT(&qcc->send_retry_list);
1142
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001143 qcc->subs = NULL;
1144 qcc->wait_event.tasklet->process = qc_io_cb;
1145 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001146 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001147
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001148 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001149 qcc->task = NULL;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001150 qcc->timeout = prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001151 if (tick_isset(qcc->timeout)) {
1152 qcc->task = task_new_here();
1153 if (!qcc->task)
1154 goto fail_no_timeout_task;
1155 qcc->task->process = qc_timeout_task;
1156 qcc->task->context = qcc;
1157 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1158 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001159
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001160 if (!conn_is_back(conn)) {
1161 if (!LIST_INLIST(&conn->stopping_list)) {
1162 LIST_APPEND(&mux_stopping_data[tid].list,
1163 &conn->stopping_list);
1164 }
1165 }
1166
Willy Tarreau784b8682022-04-11 14:18:10 +02001167 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001168 /* init read cycle */
1169 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001170
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001171 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001172
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001173 fail_no_timeout_task:
1174 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001175 fail_no_tasklet:
1176 pool_free(pool_head_qcc, qcc);
1177 fail_no_qcc:
1178 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001179}
1180
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001181static void qc_destroy(void *ctx)
1182{
1183 struct qcc *qcc = ctx;
1184
1185 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
1186 qc_release(qcc);
1187 TRACE_LEAVE(QMUX_EV_QCC_END);
1188}
1189
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001190static void qc_detach(struct conn_stream *cs)
1191{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001192 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001193 struct qcc *qcc = qcs->qcc;
1194
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001195 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001196
Amaury Denoyelle80388212022-04-08 12:00:12 +02001197 qcs->cs = NULL;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001198 --qcc->nb_cs;
1199
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001200 if ((b_data(&qcs->tx.buf) || qcs->tx.offset > qcs->tx.sent_offset) &&
1201 !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001202 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001203 qcs->flags |= QC_SF_DETACH;
1204 return;
1205 }
1206
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001207 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001208
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001209 if (qcc_is_dead(qcc)) {
1210 qc_release(qcc);
1211 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001212 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001213 if (qcc_may_expire(qcc))
1214 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1215 else
1216 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001217 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001218 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001219
1220 TRACE_LEAVE(QMUX_EV_STRM_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001221}
1222
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001223/* Called from the upper layer, to receive data */
1224static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf,
1225 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001226{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001227 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001228 struct htx *qcs_htx = NULL;
1229 struct htx *cs_htx = NULL;
1230 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001231 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001232
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001233 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001234
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001235 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
1236 if (htx_is_empty(qcs_htx)) {
1237 /* Set buffer data to 0 as HTX is empty. */
1238 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1239 goto end;
1240 }
1241
1242 ret = qcs_htx->data;
1243
1244 cs_htx = htx_from_buf(buf);
1245 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
1246 htx_to_buf(cs_htx, buf);
1247 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1248 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
1249 goto end;
1250 }
1251
1252 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
1253 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
1254
1255 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001256 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
1257 cs_htx->flags |= HTX_FL_EOM;
1258 fin = 1;
1259 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001260
1261 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
1262 htx_to_buf(cs_htx, buf);
1263 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1264 ret -= qcs_htx->data;
1265
1266 end:
1267 if (b_data(&qcs->rx.app_buf)) {
Christopher Fauletb041b232022-03-24 10:27:02 +01001268 cs->endp->flags |= (CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001269 }
1270 else {
Christopher Fauletb041b232022-03-24 10:27:02 +01001271 cs->endp->flags &= ~(CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
1272 if (cs->endp->flags & CS_EP_ERR_PENDING)
1273 cs->endp->flags |= CS_EP_ERROR;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001274
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001275 if (fin)
Christopher Fauletb041b232022-03-24 10:27:02 +01001276 cs->endp->flags |= (CS_EP_EOI|CS_EP_EOS);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001277
1278 if (b_size(&qcs->rx.app_buf)) {
1279 b_free(&qcs->rx.app_buf);
1280 offer_buffers(NULL, 1);
1281 }
1282 }
1283
1284 if (ret)
1285 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
1286
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001287 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
1288
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001289 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001290}
1291
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001292static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf,
1293 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001294{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001295 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001296 size_t ret;
1297
1298 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001299
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001300 ret = qcs->qcc->app_ops->snd_buf(cs, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001301
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001302 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
1303
1304 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001305}
1306
1307/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1308 * event subscriber <es> is not allowed to change from a previous call as long
1309 * as at least one event is still subscribed. The <event_type> must only be a
1310 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1311 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001312static int qc_subscribe(struct conn_stream *cs, int event_type,
1313 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001314{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001315 return qcs_subscribe(__cs_mux(cs), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001316}
1317
1318/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1319 * The <es> pointer is not allowed to differ from the one passed to the
1320 * subscribe() call. It always returns zero.
1321 */
1322static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
1323{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001324 struct qcs *qcs = __cs_mux(cs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001325
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001326 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1327 BUG_ON(qcs->subs && qcs->subs != es);
1328
1329 es->events &= ~event_type;
1330 if (!es->events)
1331 qcs->subs = NULL;
1332
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001333 return 0;
1334}
1335
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001336/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Christopher Fauletb041b232022-03-24 10:27:02 +01001337 * report CS_EP_ERR_PENDING|CS_EP_ERROR on the attached conn-streams and wake
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001338 * them.
1339 */
1340static int qc_wake_some_streams(struct qcc *qcc)
1341{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001342 struct qcs *qcs;
1343 struct eb64_node *node;
1344
1345 for (node = eb64_first(&qcc->streams_by_id); node;
1346 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001347 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001348
1349 if (!qcs->cs)
1350 continue;
1351
1352 if (qcc->conn->flags & CO_FL_ERROR) {
Christopher Fauletb041b232022-03-24 10:27:02 +01001353 qcs->endp->flags |= CS_EP_ERR_PENDING;
1354 if (qcs->endp->flags & CS_EP_EOS)
1355 qcs->endp->flags |= CS_EP_ERROR;
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001356
1357 if (qcs->subs) {
1358 qcs_notify_recv(qcs);
1359 qcs_notify_send(qcs);
1360 }
1361 else if (qcs->cs->data_cb->wake) {
1362 qcs->cs->data_cb->wake(qcs->cs);
1363 }
1364 }
1365 }
1366
1367 return 0;
1368}
1369
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001370static int qc_wake(struct connection *conn)
1371{
1372 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02001373 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001374
1375 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001376
1377 /* Check if a soft-stop is in progress.
1378 * Release idling front connection if this is the case.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001379 *
1380 * TODO this is revelant for frontend connections only.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001381 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001382 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
1383 goto release;
1384
Willy Tarreau784b8682022-04-11 14:18:10 +02001385 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02001386 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
1387
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001388 qc_send(qcc);
1389
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001390 qc_wake_some_streams(qcc);
1391
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001392 if (qcc_is_dead(qcc))
1393 goto release;
1394
1395 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
1396
1397 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001398
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001399 release:
1400 qc_release(qcc);
1401 TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001402 return 1;
1403}
1404
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001405
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001406static void qmux_trace_frm(const struct quic_frame *frm)
1407{
1408 switch (frm->type) {
1409 case QUIC_FT_MAX_STREAMS_BIDI:
1410 chunk_appendf(&trace_buf, " max_streams=%lu",
1411 frm->max_streams_bidi.max_streams);
1412 break;
1413
1414 case QUIC_FT_MAX_STREAMS_UNI:
1415 chunk_appendf(&trace_buf, " max_streams=%lu",
1416 frm->max_streams_uni.max_streams);
1417 break;
1418
1419 default:
1420 break;
1421 }
1422}
1423
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001424/* quic-mux trace handler */
1425static void qmux_trace(enum trace_level level, uint64_t mask,
1426 const struct trace_source *src,
1427 const struct ist where, const struct ist func,
1428 const void *a1, const void *a2, const void *a3, const void *a4)
1429{
1430 const struct connection *conn = a1;
1431 const struct qcc *qcc = conn ? conn->ctx : NULL;
1432 const struct qcs *qcs = a2;
1433
1434 if (!qcc)
1435 return;
1436
1437 if (src->verbosity > QMUX_VERB_CLEAN) {
1438 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
1439
1440 if (qcs)
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001441 chunk_appendf(&trace_buf, " qcs=%p(%lu)", qcs, qcs->id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001442
1443 if (mask & QMUX_EV_QCC_NQCS) {
1444 const uint64_t *id = a3;
1445 chunk_appendf(&trace_buf, " id=%lu", *id);
1446 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001447
1448 if (mask & QMUX_EV_SEND_FRM)
1449 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001450
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001451 if (mask & QMUX_EV_QCS_XFER_DATA) {
1452 const struct qcs_xfer_data_trace_arg *arg = a3;
1453 chunk_appendf(&trace_buf, " prep=%lu xfer=%d",
1454 arg->prep, arg->xfer);
1455 }
1456
1457 if (mask & QMUX_EV_QCS_BUILD_STRM) {
1458 const struct qcs_build_stream_trace_arg *arg = a3;
1459 chunk_appendf(&trace_buf, " len=%lu fin=%d offset=%lu",
1460 arg->len, arg->fin, arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001461 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001462 }
1463}
1464
Amaury Denoyelle251eadf2022-03-24 17:14:52 +01001465/* Function to automatically activate QUIC MUX traces on stdout.
1466 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
1467 * Main use for now is in the docker image for QUIC interop testing.
1468 */
1469static void qmux_init_stdout_traces(void)
1470{
1471#ifdef ENABLE_QUIC_STDOUT_TRACES
1472 trace_qmux.sink = sink_find("stdout");
1473 trace_qmux.level = TRACE_LEVEL_DEVELOPER;
1474 trace_qmux.state = TRACE_STATE_RUNNING;
1475 trace_qmux.verbosity = QMUX_VERB_MINIMAL;
1476#endif
1477}
1478INITCALL0(STG_INIT, qmux_init_stdout_traces);
1479
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001480
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001481static const struct mux_ops qc_ops = {
1482 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001483 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001484 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001485 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001486 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001487 .subscribe = qc_subscribe,
1488 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001489 .wake = qc_wake,
Christopher Fauleta97cced2022-04-12 18:04:10 +02001490 .flags = MX_FL_HTX|MX_FL_NO_UPG,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02001491 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001492};
1493
1494static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001495 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001496
1497INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);