blob: da322c060f23928b2d445397bba3062ac0352841 [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 Denoyellef3e03a42022-04-21 15:41:34 +0200521 if (conn && qcc->wait_event.events) {
522 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
523 qcc->wait_event.events,
524 &qcc->wait_event);
525 }
Amaury Denoyellef8909452022-03-30 11:51:56 +0200526
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200527 /* liberate remaining qcs instances */
528 node = eb64_first(&qcc->streams_by_id);
529 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200530 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200531 node = eb64_next(node);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200532 qcs_free(qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100533 }
534
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200535 pool_free(pool_head_qcc, qcc);
536
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100537 if (conn) {
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100538 LIST_DEL_INIT(&conn->stopping_list);
539
Willy Tarreau784b8682022-04-11 14:18:10 +0200540 conn->handle.qc->conn = NULL;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100541 conn->mux = NULL;
542 conn->ctx = NULL;
543
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100544 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
545
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100546 conn_stop_tracking(conn);
547 conn_full_close(conn);
548 if (conn->destroy_cb)
549 conn->destroy_cb(conn);
550 conn_free(conn);
551 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100552
553 TRACE_LEAVE(QMUX_EV_QCC_END);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100554}
555
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100556/* Prepare a STREAM frame for <qcs> instance. First, transfer data from
557 * <payload> to <out> buffer. The STREAM frame payload points to the <out>
558 * buffer. The frame is then pushed to <frm_list>. If <fin> is set, and the
559 * <payload> buf is emptied after transfer, FIN bit is set on the STREAM frame.
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100560 * Transfer is automatically adjusted to not exceed the stream flow-control
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100561 * limit. <max_data> must contains the current sum offsets for the connection.
562 * This is useful to not exceed the connection flow-control limit when using
563 * repeatdly this function on multiple streams before passing the data to the
564 * lower layer.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100565 *
Amaury Denoyelle3eb892f2022-04-22 09:47:58 +0200566 * Returns the total bytes of newly transferred data. It may be 0 if none.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100567 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200568static int qcs_xfer_data(struct qcs *qcs, struct buffer *out,
569 struct buffer *payload, uint64_t max_data)
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200570{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100571 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200572 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100573 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200574
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100575 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100576
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100577 qc_get_buf(qcs, out);
578
579 /*
580 * QCS out buffer diagram
581 * head left to_xfer
582 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100583 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100584 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100585 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100586 * ^ ack-off ^ sent-off ^ off
587 *
588 * STREAM frame
589 * ^ ^
590 * |xxxxxxxxxxxxxxxxx|
591 */
592
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200593 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100594 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
595
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100596 left = qcs->tx.offset - qcs->tx.sent_offset;
597 to_xfer = QUIC_MIN(b_data(payload), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100598
599 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
600 /* do not exceed flow control limit */
601 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
602 to_xfer = qcs->tx.msd - qcs->tx.offset;
603
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100604 BUG_ON_HOT(max_data > qcc->rfctl.md);
605 /* do not overcome flow control limit on connection */
606 if (max_data + to_xfer > qcc->rfctl.md)
607 to_xfer = qcc->rfctl.md - max_data;
608
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100609 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200610 goto out;
611
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200612 total = b_force_xfer(out, payload, to_xfer);
613
614 out:
615 {
616 struct qcs_xfer_data_trace_arg arg = {
617 .prep = b_data(out), .xfer = total,
618 };
619 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
620 qcc->conn, qcs, &arg);
621 }
622
623 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200624}
625
626static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
627 struct list *frm_list)
628{
629 struct qcc *qcc = qcs->qcc;
630 struct quic_frame *frm;
631 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200632 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200633
634 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
635
Amaury Denoyellea4569202022-04-15 17:29:25 +0200636 /* if ack_offset < buf_offset, it points to an older buffer. */
637 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
638 BUG_ON(qcs->tx.sent_offset < base_off);
639
640 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200641 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200642 BUG_ON(total < 0);
643
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200644 if (!total) {
645 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
646 return 0;
647 }
Amaury Denoyellea4569202022-04-15 17:29:25 +0200648 BUG_ON(qcs->tx.sent_offset >= qcs->tx.offset);
649 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200650
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200651 frm = pool_zalloc(pool_head_quic_frame);
652 if (!frm)
653 goto err;
654
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100655 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200656 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200657 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100658 frm->stream.buf = out;
659 frm->stream.data = (unsigned char *)b_peek(out, head);
660
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100661 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200662 if (fin)
663 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100664
665 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200666 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100667 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200668 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100669
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200670 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
671 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200672
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100673 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100674
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200675 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100676 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200677 struct qcs_build_stream_trace_arg arg = {
678 .len = frm->stream.len, .fin = fin,
679 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100680 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200681 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100682 qcc->conn, qcs, &arg);
683 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100684
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200685 return total;
686
687 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100688 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200689 return -1;
690}
691
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100692/* This function must be called by the upper layer to inform about the sending
693 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
694 * <offset>.
695 */
696void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
697{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100698 struct qcc *qcc = qcs->qcc;
699 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100700
701 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200702 BUG_ON(offset >= qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100703
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100704 /* check if the STREAM frame has already been notified. It can happen
705 * for retransmission.
706 */
707 if (offset + data <= qcs->tx.sent_offset)
708 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100709
710 diff = offset + data - qcs->tx.sent_offset;
711
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100712 /* increase offset sum on connection */
713 qcc->tx.sent_offsets += diff;
714 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
715 if (qcc->tx.sent_offsets == qcc->rfctl.md)
716 qcc->flags |= QC_CF_BLK_MFCTL;
717
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100718 /* increase offset on stream */
719 qcs->tx.sent_offset += diff;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100720 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200721 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100722 if (qcs->tx.sent_offset == qcs->tx.msd)
723 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200724
725 if (qcs->tx.offset == qcs->tx.sent_offset && b_full(&qcs->stream->buf->buf)) {
726 qc_stream_buf_release(qcs->stream);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200727 /* prepare qcs for immediate send retry if data to send */
728 if (b_data(&qcs->tx.buf))
729 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200730 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100731}
732
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100733/* Wrapper for send on transport layer. Send a list of frames <frms> for the
734 * connection <qcc>.
735 *
736 * Returns 0 if all data sent with success else non-zero.
737 */
738static int qc_send_frames(struct qcc *qcc, struct list *frms)
739{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100740 /* TODO implement an opportunistic retry mechanism. This is needed
741 * because qc_send_app_pkts is not completed. It will only prepare data
742 * up to its Tx buffer. The frames left are not send even if the Tx
743 * buffer is emptied by the sendto call.
744 *
745 * To overcome this, we call repeatedly qc_send_app_pkts until we
746 * detect that the transport layer has send nothing. This could happen
747 * on congestion or sendto syscall error.
748 *
749 * When qc_send_app_pkts is improved to handle retry by itself, we can
750 * remove the looping from the MUX.
751 */
752 struct quic_frame *first_frm;
753 uint64_t first_offset = 0;
754 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100755
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100756 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
757
758 if (LIST_ISEMPTY(frms)) {
759 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200760 return 1;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100761 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100762
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200763 LIST_INIT(&qcc->send_retry_list);
764
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100765 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100766 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
767 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
768 first_offset = first_frm->stream.offset.key;
769 first_stream_frame_type = 1;
770 }
771 else {
772 first_stream_frame_type = 0;
773 }
774
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100775 if (!LIST_ISEMPTY(frms))
Willy Tarreau784b8682022-04-11 14:18:10 +0200776 qc_send_app_pkts(qcc->conn->handle.qc, frms);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100777
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100778 /* If there is frames left, check if the transport layer has send some
779 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100780 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100781 if (!LIST_ISEMPTY(frms)) {
782 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
783 goto retry_send;
784
785 /* If the first frame is STREAM, check if its offset has
786 * changed.
787 */
788 if (first_stream_frame_type &&
789 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
790 goto retry_send;
791 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100792 }
793
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100794 /* If there is frames left at this stage, transport layer is blocked.
795 * Subscribe on it to retry later.
796 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100797 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100798 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100799 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
800 SUB_RETRY_SEND, &qcc->wait_event);
801 return 1;
802 }
803
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100804 TRACE_LEAVE(QMUX_EV_QCC_SEND);
805
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100806 return 0;
807}
808
Amaury Denoyellec9337802022-04-04 16:36:34 +0200809/* Send a MAX_STREAM_BIDI frame to update the limit of bidirectional streams
810 * allowed to be opened by the peer. The caller should have first checked if
811 * this is required with qc_is_max_streams_needed.
812 *
813 * Returns 0 on success else non-zero.
814 */
815static int qc_send_max_streams(struct qcc *qcc)
816{
817 struct list frms = LIST_HEAD_INIT(frms);
818 struct quic_frame *frm;
819
820 frm = pool_zalloc(pool_head_quic_frame);
821 BUG_ON(!frm); /* TODO handle this properly */
822
823 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
824 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
825 qcc->lfctl.cl_bidi_r;
826 TRACE_DEVEL("sending MAX_STREAMS frame", QMUX_EV_SEND_FRM, qcc->conn, NULL, frm);
827 LIST_APPEND(&frms, &frm->list);
828
829 if (qc_send_frames(qcc, &frms))
830 return 1;
831
832 /* save the new limit if the frame has been send. */
833 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
834 qcc->lfctl.cl_bidi_r = 0;
835
836 return 0;
837}
838
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200839/* Used internally by qc_send function. Proceed to send for <qcs>. This will
840 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
841 * is then generated and inserted in <frms> list. <qcc_max_data> is the current
842 * flow-control max-data at the connection level which must not be surpassed.
843 *
844 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
845 * be null if out buffer cannot be allocated.
846 */
847static int _qc_send_qcs(struct qcs *qcs, struct list *frms,
848 uint64_t qcc_max_data)
849{
850 struct qcc *qcc = qcs->qcc;
851 struct buffer *buf = &qcs->tx.buf;
852 struct buffer *out = qc_stream_buf_get(qcs->stream);
853 int xfer = 0;
854
855 /* Allocate <out> buffer if necessary. */
856 if (!out) {
857 if (qcc->flags & QC_CF_CONN_FULL)
858 return 0;
859
860 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
861 if (!out) {
862 qcc->flags |= QC_CF_CONN_FULL;
863 return 0;
864 }
865 }
866
867 /* Transfer data from <buf> to <out>. */
868 if (b_data(buf)) {
869 xfer = qcs_xfer_data(qcs, out, buf, qcc_max_data);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200870 if (xfer > 0) {
871 qcs_notify_send(qcs);
872 qcs->flags &= ~QC_SF_BLK_MROOM;
873 }
874
875 qcs->tx.offset += xfer;
876 }
877
878 /* out buffer cannot be emptied if qcs offsets differ. */
879 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
880
881 /* Build a new STREAM frame with <out> buffer. */
882 if (qcs->tx.sent_offset != qcs->tx.offset) {
883 int ret;
884 char fin = !!(qcs->flags & QC_SF_FIN_STREAM);
885
886 /* FIN is set if all incoming data were transfered. */
887 fin = !!(fin && !b_data(buf));
888
889 ret = qcs_build_stream_frm(qcs, out, fin, frms);
890 BUG_ON(ret < 0); /* TODO handle this properly */
891 }
892
893 return xfer;
894}
895
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100896/* Proceed to sending. Loop through all available streams for the <qcc>
897 * instance and try to send as much as possible.
898 *
899 * Returns the total of bytes sent to the transport layer.
900 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100901static int qc_send(struct qcc *qcc)
902{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100903 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200904 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200905 struct qcs *qcs, *qcs_tmp;
906 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200907
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100908 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200909
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200910 if (qcc->conn->flags & CO_FL_SOCK_WR_SH) {
911 qcc->conn->flags |= CO_FL_ERROR;
912 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
913 return 0;
914 }
915
Amaury Denoyellec9337802022-04-04 16:36:34 +0200916 if (qc_is_max_streams_needed(qcc))
917 qc_send_max_streams(qcc);
918
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100919 if (qcc->flags & QC_CF_BLK_MFCTL)
920 return 0;
921
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100922 /* loop through all streams, construct STREAM frames if data available.
923 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200924 */
925 node = eb64_first(&qcc->streams_by_id);
926 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200927 int ret;
928 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100929
Amaury Denoyellee2ec9422022-03-10 16:46:18 +0100930 /* TODO
931 * for the moment, unidirectional streams have their own
932 * mechanism for sending. This should be unified in the future,
933 * in this case the next check will be removed.
934 */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200935 if (quic_stream_is_uni(qcs->id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +0100936 node = eb64_next(node);
937 continue;
938 }
939
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100940 if (qcs->flags & QC_SF_BLK_SFCTL) {
941 node = eb64_next(node);
942 continue;
943 }
944
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200945 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +0200946 node = eb64_next(node);
947 continue;
948 }
Amaury Denoyellea4569202022-04-15 17:29:25 +0200949
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200950 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + total);
951 total += ret;
952 node = eb64_next(node);
953 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200954
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200955 if (qc_send_frames(qcc, &frms)) {
956 /* data rejected by transport layer, do not retry. */
957 goto out;
958 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200959
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200960 retry:
961 tmp_total = 0;
962 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
963 int ret;
964 BUG_ON(!b_data(&qcs->tx.buf));
965 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200966
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200967 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + tmp_total);
968 tmp_total += ret;
969 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200970 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200971
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200972 total += tmp_total;
973 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
974 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100975
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200976 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100977 TRACE_LEAVE(QMUX_EV_QCC_SEND);
978
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100979 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100980}
981
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +0100982/* Release all streams that are already marked as detached. This is only done
983 * if their TX buffers are empty or if a CONNECTION_CLOSE has been received.
984 *
985 * Return the number of released stream.
986 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100987static int qc_release_detached_streams(struct qcc *qcc)
988{
989 struct eb64_node *node;
990 int release = 0;
991
992 node = eb64_first(&qcc->streams_by_id);
993 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200994 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100995 node = eb64_next(node);
996
997 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200998 if (!b_data(&qcs->tx.buf) &&
999 qcs->tx.offset == qcs->tx.sent_offset) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001000 qcs_destroy(qcs);
1001 release = 1;
1002 }
1003 else {
1004 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1005 SUB_RETRY_SEND, &qcc->wait_event);
1006 }
1007 }
1008 }
1009
1010 return release;
1011}
1012
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001013static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1014{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001015 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001016
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001017 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001018
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001019 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001020
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001021 if (qc_release_detached_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001022 if (qcc_is_dead(qcc)) {
1023 qc_release(qcc);
1024 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001025 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001026 if (qcc_may_expire(qcc))
1027 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1028 else
1029 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001030 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001031 }
1032 }
1033
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001034 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1035
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001036 return NULL;
1037}
1038
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001039static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1040{
1041 struct qcc *qcc = ctx;
1042 int expired = tick_is_expired(t->expire, now_ms);
1043
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001044 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001045
1046 if (qcc) {
1047 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001048 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001049 return t;
1050 }
1051
1052 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001053 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001054 t->expire = TICK_ETERNITY;
1055 return t;
1056 }
1057 }
1058
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001059 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001060
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001061 if (!qcc) {
1062 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001063 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001064 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001065
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001066 qcc->task = NULL;
1067
1068 if (qcc_is_dead(qcc))
1069 qc_release(qcc);
1070
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001071 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1072
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001073 return NULL;
1074}
1075
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001076static int qc_init(struct connection *conn, struct proxy *prx,
1077 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001078{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001079 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001080 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001081
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001082 qcc = pool_alloc(pool_head_qcc);
1083 if (!qcc)
1084 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001085
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001086 qcc->conn = conn;
1087 conn->ctx = qcc;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001088 qcc->nb_cs = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001089 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001090
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001091 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001092
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001093 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001094
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001095 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001096 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001097
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001098 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001099 qcc->tx.sent_offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001100
1101 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001102 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001103 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
1104 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001105 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001106 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001107
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001108 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001109 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
1110 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001111 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001112 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001113
1114 /* Server initiated streams must respect the server flow control. */
1115 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001116 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
1117 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001118 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001119 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1120
1121 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001122 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
1123 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001124 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001125 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001126
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001127 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
1128 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001129
Willy Tarreau784b8682022-04-11 14:18:10 +02001130 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001131 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001132 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1133 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1134
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001135 qcc->wait_event.tasklet = tasklet_new();
1136 if (!qcc->wait_event.tasklet)
1137 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001138
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001139 LIST_INIT(&qcc->send_retry_list);
1140
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001141 qcc->subs = NULL;
1142 qcc->wait_event.tasklet->process = qc_io_cb;
1143 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001144 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001145
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001146 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001147 qcc->task = NULL;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001148 qcc->timeout = prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001149 if (tick_isset(qcc->timeout)) {
1150 qcc->task = task_new_here();
1151 if (!qcc->task)
1152 goto fail_no_timeout_task;
1153 qcc->task->process = qc_timeout_task;
1154 qcc->task->context = qcc;
1155 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1156 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001157
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001158 if (!conn_is_back(conn)) {
1159 if (!LIST_INLIST(&conn->stopping_list)) {
1160 LIST_APPEND(&mux_stopping_data[tid].list,
1161 &conn->stopping_list);
1162 }
1163 }
1164
Willy Tarreau784b8682022-04-11 14:18:10 +02001165 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001166 /* init read cycle */
1167 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001168
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001169 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001170
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001171 fail_no_timeout_task:
1172 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001173 fail_no_tasklet:
1174 pool_free(pool_head_qcc, qcc);
1175 fail_no_qcc:
1176 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001177}
1178
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001179static void qc_destroy(void *ctx)
1180{
1181 struct qcc *qcc = ctx;
1182
1183 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
1184 qc_release(qcc);
1185 TRACE_LEAVE(QMUX_EV_QCC_END);
1186}
1187
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001188static void qc_detach(struct conn_stream *cs)
1189{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001190 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001191 struct qcc *qcc = qcs->qcc;
1192
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001193 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001194
Amaury Denoyelle80388212022-04-08 12:00:12 +02001195 qcs->cs = NULL;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001196 --qcc->nb_cs;
1197
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001198 if ((b_data(&qcs->tx.buf) || qcs->tx.offset > qcs->tx.sent_offset) &&
1199 !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001200 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001201 qcs->flags |= QC_SF_DETACH;
1202 return;
1203 }
1204
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001205 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001206
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001207 if (qcc_is_dead(qcc)) {
1208 qc_release(qcc);
1209 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001210 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001211 if (qcc_may_expire(qcc))
1212 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1213 else
1214 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001215 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001216 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001217
1218 TRACE_LEAVE(QMUX_EV_STRM_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001219}
1220
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001221/* Called from the upper layer, to receive data */
1222static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf,
1223 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001224{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001225 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001226 struct htx *qcs_htx = NULL;
1227 struct htx *cs_htx = NULL;
1228 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001229 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001230
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001231 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001232
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001233 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
1234 if (htx_is_empty(qcs_htx)) {
1235 /* Set buffer data to 0 as HTX is empty. */
1236 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1237 goto end;
1238 }
1239
1240 ret = qcs_htx->data;
1241
1242 cs_htx = htx_from_buf(buf);
1243 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
1244 htx_to_buf(cs_htx, buf);
1245 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1246 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
1247 goto end;
1248 }
1249
1250 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
1251 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
1252
1253 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001254 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
1255 cs_htx->flags |= HTX_FL_EOM;
1256 fin = 1;
1257 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001258
1259 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
1260 htx_to_buf(cs_htx, buf);
1261 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1262 ret -= qcs_htx->data;
1263
1264 end:
1265 if (b_data(&qcs->rx.app_buf)) {
Christopher Fauletb041b232022-03-24 10:27:02 +01001266 cs->endp->flags |= (CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001267 }
1268 else {
Christopher Fauletb041b232022-03-24 10:27:02 +01001269 cs->endp->flags &= ~(CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
1270 if (cs->endp->flags & CS_EP_ERR_PENDING)
1271 cs->endp->flags |= CS_EP_ERROR;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001272
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001273 if (fin)
Christopher Fauletb041b232022-03-24 10:27:02 +01001274 cs->endp->flags |= (CS_EP_EOI|CS_EP_EOS);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001275
1276 if (b_size(&qcs->rx.app_buf)) {
1277 b_free(&qcs->rx.app_buf);
1278 offer_buffers(NULL, 1);
1279 }
1280 }
1281
1282 if (ret)
1283 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
1284
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001285 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
1286
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001287 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001288}
1289
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001290static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf,
1291 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001292{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001293 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001294 size_t ret;
1295
1296 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001297
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001298 ret = qcs->qcc->app_ops->snd_buf(cs, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001299
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001300 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
1301
1302 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001303}
1304
1305/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1306 * event subscriber <es> is not allowed to change from a previous call as long
1307 * as at least one event is still subscribed. The <event_type> must only be a
1308 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1309 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001310static int qc_subscribe(struct conn_stream *cs, int event_type,
1311 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001312{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001313 return qcs_subscribe(__cs_mux(cs), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001314}
1315
1316/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1317 * The <es> pointer is not allowed to differ from the one passed to the
1318 * subscribe() call. It always returns zero.
1319 */
1320static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
1321{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001322 struct qcs *qcs = __cs_mux(cs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001323
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001324 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1325 BUG_ON(qcs->subs && qcs->subs != es);
1326
1327 es->events &= ~event_type;
1328 if (!es->events)
1329 qcs->subs = NULL;
1330
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001331 return 0;
1332}
1333
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001334/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Christopher Fauletb041b232022-03-24 10:27:02 +01001335 * report CS_EP_ERR_PENDING|CS_EP_ERROR on the attached conn-streams and wake
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001336 * them.
1337 */
1338static int qc_wake_some_streams(struct qcc *qcc)
1339{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001340 struct qcs *qcs;
1341 struct eb64_node *node;
1342
1343 for (node = eb64_first(&qcc->streams_by_id); node;
1344 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001345 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001346
1347 if (!qcs->cs)
1348 continue;
1349
1350 if (qcc->conn->flags & CO_FL_ERROR) {
Christopher Fauletb041b232022-03-24 10:27:02 +01001351 qcs->endp->flags |= CS_EP_ERR_PENDING;
1352 if (qcs->endp->flags & CS_EP_EOS)
1353 qcs->endp->flags |= CS_EP_ERROR;
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001354
1355 if (qcs->subs) {
1356 qcs_notify_recv(qcs);
1357 qcs_notify_send(qcs);
1358 }
1359 else if (qcs->cs->data_cb->wake) {
1360 qcs->cs->data_cb->wake(qcs->cs);
1361 }
1362 }
1363 }
1364
1365 return 0;
1366}
1367
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001368static int qc_wake(struct connection *conn)
1369{
1370 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02001371 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001372
1373 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001374
1375 /* Check if a soft-stop is in progress.
1376 * Release idling front connection if this is the case.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001377 *
1378 * TODO this is revelant for frontend connections only.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001379 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001380 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
1381 goto release;
1382
Willy Tarreau784b8682022-04-11 14:18:10 +02001383 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02001384 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
1385
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001386 qc_send(qcc);
1387
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001388 qc_wake_some_streams(qcc);
1389
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001390 if (qcc_is_dead(qcc))
1391 goto release;
1392
1393 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
1394
1395 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001396
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001397 release:
1398 qc_release(qcc);
1399 TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001400 return 1;
1401}
1402
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001403
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001404static void qmux_trace_frm(const struct quic_frame *frm)
1405{
1406 switch (frm->type) {
1407 case QUIC_FT_MAX_STREAMS_BIDI:
1408 chunk_appendf(&trace_buf, " max_streams=%lu",
1409 frm->max_streams_bidi.max_streams);
1410 break;
1411
1412 case QUIC_FT_MAX_STREAMS_UNI:
1413 chunk_appendf(&trace_buf, " max_streams=%lu",
1414 frm->max_streams_uni.max_streams);
1415 break;
1416
1417 default:
1418 break;
1419 }
1420}
1421
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001422/* quic-mux trace handler */
1423static void qmux_trace(enum trace_level level, uint64_t mask,
1424 const struct trace_source *src,
1425 const struct ist where, const struct ist func,
1426 const void *a1, const void *a2, const void *a3, const void *a4)
1427{
1428 const struct connection *conn = a1;
1429 const struct qcc *qcc = conn ? conn->ctx : NULL;
1430 const struct qcs *qcs = a2;
1431
1432 if (!qcc)
1433 return;
1434
1435 if (src->verbosity > QMUX_VERB_CLEAN) {
1436 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
1437
1438 if (qcs)
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001439 chunk_appendf(&trace_buf, " qcs=%p(%lu)", qcs, qcs->id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001440
1441 if (mask & QMUX_EV_QCC_NQCS) {
1442 const uint64_t *id = a3;
1443 chunk_appendf(&trace_buf, " id=%lu", *id);
1444 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001445
1446 if (mask & QMUX_EV_SEND_FRM)
1447 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001448
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001449 if (mask & QMUX_EV_QCS_XFER_DATA) {
1450 const struct qcs_xfer_data_trace_arg *arg = a3;
1451 chunk_appendf(&trace_buf, " prep=%lu xfer=%d",
1452 arg->prep, arg->xfer);
1453 }
1454
1455 if (mask & QMUX_EV_QCS_BUILD_STRM) {
1456 const struct qcs_build_stream_trace_arg *arg = a3;
1457 chunk_appendf(&trace_buf, " len=%lu fin=%d offset=%lu",
1458 arg->len, arg->fin, arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001459 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001460 }
1461}
1462
Amaury Denoyelle251eadf2022-03-24 17:14:52 +01001463/* Function to automatically activate QUIC MUX traces on stdout.
1464 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
1465 * Main use for now is in the docker image for QUIC interop testing.
1466 */
1467static void qmux_init_stdout_traces(void)
1468{
1469#ifdef ENABLE_QUIC_STDOUT_TRACES
1470 trace_qmux.sink = sink_find("stdout");
1471 trace_qmux.level = TRACE_LEVEL_DEVELOPER;
1472 trace_qmux.state = TRACE_STATE_RUNNING;
1473 trace_qmux.verbosity = QMUX_VERB_MINIMAL;
1474#endif
1475}
1476INITCALL0(STG_INIT, qmux_init_stdout_traces);
1477
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001478
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001479static const struct mux_ops qc_ops = {
1480 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001481 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001482 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001483 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001484 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001485 .subscribe = qc_subscribe,
1486 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001487 .wake = qc_wake,
Christopher Fauleta97cced2022-04-12 18:04:10 +02001488 .flags = MX_FL_HTX|MX_FL_NO_UPG,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02001489 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001490};
1491
1492static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001493 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001494
1495INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);