blob: 2051bff105e79a7fd524bbf8cd45bb159d0b9aed [file] [log] [blame]
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001#include <haproxy/mux_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002
Amaury Denoyelleeb01f592021-10-07 16:44:05 +02003#include <import/eb64tree.h>
4
Frédéric Lécailledfbae762021-02-18 09:59:01 +01005#include <haproxy/api.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01006#include <haproxy/connection.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +01007#include <haproxy/conn_stream.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +01008#include <haproxy/dynbuf.h>
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01009#include <haproxy/htx.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +010010#include <haproxy/pool.h>
Amaury Denoyelle251eadf2022-03-24 17:14:52 +010011#include <haproxy/sink.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020012#include <haproxy/ssl_sock-t.h>
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010013#include <haproxy/trace.h>
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +010014#include <haproxy/xprt_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010015
Amaury Denoyelledeed7772021-12-03 11:36:46 +010016DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010017DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
18
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010019/* trace source and events */
20static void qmux_trace(enum trace_level level, uint64_t mask,
21 const struct trace_source *src,
22 const struct ist where, const struct ist func,
23 const void *a1, const void *a2, const void *a3, const void *a4);
24
25static const struct trace_event qmux_trace_events[] = {
Amaury Denoyelle4f137572022-03-24 17:10:00 +010026#define QMUX_EV_QCC_RECV (1ULL << 1)
27 { .mask = QMUX_EV_QCC_RECV, .name = "qcc_recv", .desc = "Rx on QUIC connection" },
28#define QMUX_EV_QCC_SEND (1ULL << 2)
29 { .mask = QMUX_EV_QCC_SEND, .name = "qcc_send", .desc = "Tx on QUIC connection" },
30#define QMUX_EV_QCC_WAKE (1ULL << 3)
31 { .mask = QMUX_EV_QCC_WAKE, .name = "qcc_wake", .desc = "QUIC connection woken up" },
32#define QMUX_EV_QCC_END (1ULL << 4)
33 { .mask = QMUX_EV_QCC_END, .name = "qcc_end", .desc = "QUIC connection terminated" },
34#define QMUX_EV_QCC_NQCS (1ULL << 5)
35 { .mask = QMUX_EV_QCC_NQCS, .name = "qcc_no_qcs", .desc = "QUIC stream not found" },
36#define QMUX_EV_QCS_NEW (1ULL << 6)
37 { .mask = QMUX_EV_QCS_NEW, .name = "qcs_new", .desc = "new QUIC stream" },
38#define QMUX_EV_QCS_RECV (1ULL << 7)
39 { .mask = QMUX_EV_QCS_RECV, .name = "qcs_recv", .desc = "Rx on QUIC stream" },
40#define QMUX_EV_QCS_SEND (1ULL << 8)
41 { .mask = QMUX_EV_QCS_SEND, .name = "qcs_send", .desc = "Tx on QUIC stream" },
42#define QMUX_EV_QCS_END (1ULL << 9)
43 { .mask = QMUX_EV_QCS_END, .name = "qcs_end", .desc = "QUIC stream terminated" },
44#define QMUX_EV_STRM_RECV (1ULL << 10)
45 { .mask = QMUX_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
46#define QMUX_EV_STRM_SEND (1ULL << 11)
47 { .mask = QMUX_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
48#define QMUX_EV_STRM_END (1ULL << 12)
49 { .mask = QMUX_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
Amaury Denoyellefa29f332022-03-25 09:09:40 +010050#define QMUX_EV_SEND_FRM (1ULL << 13)
51 { .mask = QMUX_EV_SEND_FRM, .name = "send_frm", .desc = "sending QUIC frame" },
Amaury Denoyellefdcec362022-03-25 09:28:10 +010052/* special event dedicated to qcs_push_frame */
53#define QMUX_EV_QCS_PUSH_FRM (1ULL << 14)
54 { .mask = QMUX_EV_QCS_PUSH_FRM, .name = "qcs_push_frm", .desc = "qcs_push_frame" },
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010055 { }
56};
57
Amaury Denoyellefdcec362022-03-25 09:28:10 +010058/* custom arg for QMUX_EV_QCS_PUSH_FRM */
59struct qcs_push_frm_trace_arg {
60 size_t sent;
61 int xfer;
62 char fin;
63 uint64_t offset;
64};
65
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010066static const struct name_desc qmux_trace_lockon_args[4] = {
67 /* arg1 */ { /* already used by the connection */ },
68 /* arg2 */ { .name="qcs", .desc="QUIC stream" },
69 /* arg3 */ { },
70 /* arg4 */ { }
71};
72
73static const struct name_desc qmux_trace_decoding[] = {
74#define QMUX_VERB_CLEAN 1
75 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
76#define QMUX_VERB_MINIMAL 2
77 { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" },
78 { /* end */ }
79};
80
81struct trace_source trace_qmux = {
82 .name = IST("qmux"),
83 .desc = "QUIC multiplexer",
84 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
85 .default_cb = qmux_trace,
86 .known_events = qmux_trace_events,
87 .lockon_args = qmux_trace_lockon_args,
88 .decoding = qmux_trace_decoding,
89 .report_events = ~0, /* report everything by default */
90};
91
92#define TRACE_SOURCE &trace_qmux
93INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
94
Amaury Denoyelledeed7772021-12-03 11:36:46 +010095/* Allocate a new QUIC streams with id <id> and type <type>. */
96struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +010097{
Amaury Denoyelledeed7772021-12-03 11:36:46 +010098 struct qcs *qcs;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +020099 struct qc_stream_desc *stream;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100100
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100101 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
102
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100103 qcs = pool_alloc(pool_head_qcs);
104 if (!qcs)
105 goto out;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100106
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200107 /* allocate transport layer stream descriptor */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200108 stream = qc_stream_desc_new(id, qcs);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200109 if (!stream) {
110 pool_free(pool_head_qcs, qcs);
111 qcs = NULL;
112 goto out;
113 }
114
115 qcs->stream = stream;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100116 qcs->qcc = qcc;
117 qcs->cs = NULL;
118 qcs->flags = QC_SF_NONE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100119
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100120 qcs->endp = cs_endpoint_new();
121 if (!qcs->endp) {
122 pool_free(pool_head_qcs, qcs);
123 return NULL;
124 }
125 qcs->endp->target = qcs;
126 qcs->endp->ctx = qcc->conn;
127 qcs->endp->flags |= (CS_EP_T_MUX|CS_EP_ORPHAN|CS_EP_NOT_FIRST);
128
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200129 qcs->id = id;
130 /* store transport layer stream descriptor in qcc tree */
131 eb64_insert(&qcc->streams_by_id, &stream->by_id);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100132
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100133 qcc->strms[type].nb_streams++;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100134
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100135 /* If stream is local, use peer remote-limit, or else the opposite. */
136 /* TODO use uni limit for unidirectional streams */
137 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
138 qcc->rfctl.msd_bidi_l;
139
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100140 qcs->rx.buf = BUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100141 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100142 qcs->rx.offset = 0;
143 qcs->rx.frms = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100144
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100145 qcs->tx.buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100146 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100147 qcs->tx.sent_offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100148
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100149 qcs->wait_event.tasklet = NULL;
150 qcs->wait_event.events = 0;
151 qcs->subs = NULL;
152
153 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100154 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100155 return qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100156}
157
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200158/* Free a qcs. This function must only be done to remove a stream on allocation
159 * error or connection shutdown. Else use qcs_destroy which handle all the
160 * QUIC connection mechanism.
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100161 */
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200162void qcs_free(struct qcs *qcs)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100163{
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200164 b_free(&qcs->rx.buf);
165 b_free(&qcs->tx.buf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200166
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200167 BUG_ON(!qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams);
168 --qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams;
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200169
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200170 /* stream desc must be removed from MUX tree before release it */
171 eb64_delete(&qcs->stream->by_id);
Willy Tarreau784b8682022-04-11 14:18:10 +0200172 qc_stream_desc_release(qcs->stream, qcs->qcc->conn->handle.qc);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100173 BUG_ON(qcs->endp && !(qcs->endp->flags & CS_EP_ORPHAN));
174 cs_endpoint_free(qcs->endp);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100175 pool_free(pool_head_qcs, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100176}
177
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100178struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100179{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100180 struct buffer *buf = b_alloc(bptr);
181 BUG_ON(!buf);
182 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100183}
184
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100185int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
186{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100187 struct qcc *qcc = qcs->qcc;
188
189 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100190
191 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
192 BUG_ON(qcs->subs && qcs->subs != es);
193
194 es->events |= event_type;
195 qcs->subs = es;
196
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100197 if (event_type & SUB_RETRY_RECV)
198 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
199
200 if (event_type & SUB_RETRY_SEND)
201 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
202
203 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
204
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100205 return 0;
206}
207
208void qcs_notify_recv(struct qcs *qcs)
209{
210 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
211 tasklet_wakeup(qcs->subs->tasklet);
212 qcs->subs->events &= ~SUB_RETRY_RECV;
213 if (!qcs->subs->events)
214 qcs->subs = NULL;
215 }
216}
217
218void qcs_notify_send(struct qcs *qcs)
219{
220 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
221 tasklet_wakeup(qcs->subs->tasklet);
222 qcs->subs->events &= ~SUB_RETRY_SEND;
223 if (!qcs->subs->events)
224 qcs->subs = NULL;
225 }
226}
227
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100228/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
229 * several streams, depending on the already open ones.
230 * Return this node if succeeded, NULL if not.
231 */
Amaury Denoyelle50742292022-03-29 14:57:19 +0200232struct qcs *qcc_get_qcs(struct qcc *qcc, uint64_t id)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100233{
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200234 struct qc_stream_desc *stream;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100235 unsigned int strm_type;
236 int64_t sub_id;
237 struct eb64_node *strm_node;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200238 struct qcs *qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100239
240 strm_type = id & QCS_ID_TYPE_MASK;
241 sub_id = id >> QCS_ID_TYPE_SHIFT;
242 strm_node = NULL;
Amaury Denoyelle0dc40f02022-02-07 11:44:17 +0100243 if (quic_stream_is_local(qcc, id)) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100244 /* Local streams: this stream must be already opened. */
245 strm_node = eb64_lookup(&qcc->streams_by_id, id);
246 if (!strm_node) {
247 /* unknown stream id */
248 goto out;
249 }
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200250 stream = eb64_entry(strm_node, struct qc_stream_desc, by_id);
251 qcs = stream->ctx;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100252 }
253 else {
254 /* Remote streams. */
255 struct eb_root *strms;
256 uint64_t largest_id;
257 enum qcs_type qcs_type;
258
259 strms = &qcc->streams_by_id;
260 qcs_type = qcs_id_type(id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100261
262 /* TODO also checks max-streams for uni streams */
263 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100264 if (sub_id + 1 > qcc->lfctl.ms_bidi) {
Amaury Denoyellec055e302022-02-07 16:09:06 +0100265 /* streams limit reached */
266 goto out;
267 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100268 }
269
270 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
271 * correct value.
272 */
273 largest_id = qcc->strms[qcs_type].largest_id;
274 if (sub_id > (int64_t)largest_id) {
275 /* RFC: "A stream ID that is used out of order results in all streams
276 * of that type with lower-numbered stream IDs also being opened".
277 * So, let's "open" these streams.
278 */
279 int64_t i;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200280 struct qcs *tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100281
Amaury Denoyelle50742292022-03-29 14:57:19 +0200282 tmp_qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100283 for (i = largest_id + 1; i <= sub_id; i++) {
284 uint64_t id = (i << QCS_ID_TYPE_SHIFT) | strm_type;
285 enum qcs_type type = id & QCS_ID_DIR_BIT ? QCS_CLT_UNI : QCS_CLT_BIDI;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200286
Amaury Denoyelle50742292022-03-29 14:57:19 +0200287 tmp_qcs = qcs_new(qcc, id, type);
288 if (!tmp_qcs) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100289 /* allocation failure */
290 goto out;
291 }
292
293 qcc->strms[qcs_type].largest_id = i;
294 }
Amaury Denoyelle50742292022-03-29 14:57:19 +0200295 if (tmp_qcs)
296 qcs = tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100297 }
298 else {
299 strm_node = eb64_lookup(strms, id);
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200300 if (strm_node) {
301 stream = eb64_entry(strm_node, struct qc_stream_desc, by_id);
302 qcs = stream->ctx;
303 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100304 }
305 }
306
Amaury Denoyelle50742292022-03-29 14:57:19 +0200307 return qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100308
309 out:
310 return NULL;
311}
312
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100313/* Handle a new STREAM frame <strm_frm>. The frame content will be copied in
314 * the buffer of the stream instance. The stream instance will be stored in
315 * <out_qcs>. In case of success, the caller can immediatly call qcc_decode_qcs
316 * to process the frame content.
317 *
318 * Returns 0 on success. On errors, two codes are present.
319 * - 1 is returned if the frame cannot be decoded and must be discarded.
320 * - 2 is returned if the stream cannot decode at the moment the frame. The
321 * frame should be buffered to be handled later.
322 */
323int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
324 char fin, char *data, struct qcs **out_qcs)
325{
326 struct qcs *qcs;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100327 size_t total, diff;
328
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100329 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
330
Amaury Denoyelle50742292022-03-29 14:57:19 +0200331 qcs = qcc_get_qcs(qcc, id);
332 if (!qcs) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100333 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 +0100334 return 1;
335 }
336
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100337 *out_qcs = qcs;
338
339 if (offset > qcs->rx.offset)
340 return 2;
341
342 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100343 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 +0100344 return 1;
345 }
346
347 /* Last frame already handled for this stream. */
348 BUG_ON(qcs->flags & QC_SF_FIN_RECV);
349
350 if (!qc_get_buf(qcs, &qcs->rx.buf)) {
351 /* TODO should mark qcs as full */
352 return 2;
353 }
354
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100355 TRACE_DEVEL("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100356 diff = qcs->rx.offset - offset;
357
358 /* TODO do not partially copy a frame if not enough size left. Maybe
359 * this can be optimized.
360 */
361 if (len > b_room(&qcs->rx.buf)) {
362 /* TODO handle STREAM frames larger than RX buffer. */
363 BUG_ON(len > b_size(&qcs->rx.buf));
364 return 2;
365 }
366
367 len -= diff;
368 data += diff;
369
370 total = b_putblk(&qcs->rx.buf, data, len);
371 /* TODO handle partial copy of a STREAM frame. */
372 BUG_ON(len != total);
373
374 qcs->rx.offset += total;
375
376 if (fin)
377 qcs->flags |= QC_SF_FIN_RECV;
378
379 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100380 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100381 return 0;
382}
383
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100384/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
385 * the frame.
386 *
387 * Returns 0 on success else non-zero.
388 */
389int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
390{
391 if (qcc->rfctl.md < max) {
392 qcc->rfctl.md = max;
393
394 if (qcc->flags & QC_CF_BLK_MFCTL) {
395 qcc->flags &= ~QC_CF_BLK_MFCTL;
396 tasklet_wakeup(qcc->wait_event.tasklet);
397 }
398 }
399 return 0;
400}
401
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100402/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
403 * field of the frame and <id> is the identifier of the QUIC stream.
404 *
405 * Returns 0 on success else non-zero.
406 */
407int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
408{
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200409 struct qc_stream_desc *stream;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100410 struct qcs *qcs;
411 struct eb64_node *node;
412
413 node = eb64_lookup(&qcc->streams_by_id, id);
414 if (node) {
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200415 stream = eb64_entry(node, struct qc_stream_desc, by_id);
416 qcs = stream->ctx;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100417 if (max > qcs->tx.msd) {
418 qcs->tx.msd = max;
419
420 if (qcs->flags & QC_SF_BLK_SFCTL) {
421 qcs->flags &= ~QC_SF_BLK_SFCTL;
422 tasklet_wakeup(qcc->wait_event.tasklet);
423 }
424 }
425 }
426
427 return 0;
428}
429
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100430/* Decode the content of STREAM frames already received on the stream instance
431 * <qcs>.
432 *
433 * Returns 0 on success else non-zero.
434 */
435int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
436{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100437 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
438
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100439 if (qcc->app_ops->decode_qcs(qcs, qcs->flags & QC_SF_FIN_RECV, qcc->ctx) < 0) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100440 TRACE_DEVEL("leaving on decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100441 return 1;
442 }
443
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100444 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
445
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100446 return 0;
447}
448
Amaury Denoyellec055e302022-02-07 16:09:06 +0100449static int qc_is_max_streams_needed(struct qcc *qcc)
450{
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100451 return qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100452}
453
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500454/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100455static void qcs_destroy(struct qcs *qcs)
456{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100457 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200458 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100459
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100460 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100461
Amaury Denoyellec055e302022-02-07 16:09:06 +0100462 if (quic_stream_is_remote(qcs->qcc, id)) {
463 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100464 ++qcs->qcc->lfctl.cl_bidi_r;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100465 if (qc_is_max_streams_needed(qcs->qcc))
466 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
467 }
468 }
469
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200470 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100471
472 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100473}
474
475static inline int qcc_is_dead(const struct qcc *qcc)
476{
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200477 if (qcc->app_ops && qcc->app_ops->is_active &&
478 qcc->app_ops->is_active(qcc, qcc->ctx))
479 return 0;
480
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200481 if ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task)
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100482 return 1;
483
484 return 0;
485}
486
487/* Return true if the mux timeout should be armed. */
488static inline int qcc_may_expire(struct qcc *qcc)
489{
Amaury Denoyelle06890aa2022-04-04 16:15:06 +0200490 return !qcc->nb_cs;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100491}
492
493/* release function. This one should be called to free all resources allocated
494 * to the mux.
495 */
496static void qc_release(struct qcc *qcc)
497{
498 struct connection *conn = NULL;
499
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100500 TRACE_ENTER(QMUX_EV_QCC_END);
501
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100502 if (qcc) {
Amaury Denoyellef8909452022-03-30 11:51:56 +0200503 struct eb64_node *node;
504
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100505 /* The connection must be aattached to this mux to be released */
506 if (qcc->conn && qcc->conn->ctx == qcc)
507 conn = qcc->conn;
508
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100509 TRACE_DEVEL("freeing qcc", QMUX_EV_QCC_END, conn);
510
Amaury Denoyellecbc13b72022-03-29 14:46:38 +0200511 if (qcc->app_ops && qcc->app_ops->release)
512 qcc->app_ops->release(qcc->ctx);
513
Amaury Denoyelle06890aa2022-04-04 16:15:06 +0200514 if (qcc->task) {
515 task_destroy(qcc->task);
516 qcc->task = NULL;
517 }
518
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100519 if (qcc->wait_event.tasklet)
520 tasklet_free(qcc->wait_event.tasklet);
521
Amaury Denoyellef8909452022-03-30 11:51:56 +0200522 /* liberate remaining qcs instances */
523 node = eb64_first(&qcc->streams_by_id);
524 while (node) {
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200525 struct qc_stream_desc *stream = eb64_entry(node, struct qc_stream_desc, by_id);
Amaury Denoyellef8909452022-03-30 11:51:56 +0200526 node = eb64_next(node);
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200527 qcs_free(stream->ctx);
Amaury Denoyellef8909452022-03-30 11:51:56 +0200528 }
529
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100530 pool_free(pool_head_qcc, qcc);
531 }
532
533 if (conn) {
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100534 LIST_DEL_INIT(&conn->stopping_list);
535
Willy Tarreau784b8682022-04-11 14:18:10 +0200536 conn->handle.qc->conn = NULL;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100537 conn->mux = NULL;
538 conn->ctx = NULL;
539
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100540 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
541
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100542 conn_stop_tracking(conn);
543 conn_full_close(conn);
544 if (conn->destroy_cb)
545 conn->destroy_cb(conn);
546 conn_free(conn);
547 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100548
549 TRACE_LEAVE(QMUX_EV_QCC_END);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100550}
551
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100552/* Prepare a STREAM frame for <qcs> instance. First, transfer data from
553 * <payload> to <out> buffer. The STREAM frame payload points to the <out>
554 * buffer. The frame is then pushed to <frm_list>. If <fin> is set, and the
555 * <payload> buf is emptied after transfer, FIN bit is set on the STREAM frame.
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100556 * Transfer is automatically adjusted to not exceed the stream flow-control
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100557 * limit. <max_data> must contains the current sum offsets for the connection.
558 * This is useful to not exceed the connection flow-control limit when using
559 * repeatdly this function on multiple streams before passing the data to the
560 * lower layer.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100561 *
562 * Returns the total bytes of newly transferred data or a negative error code.
563 */
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100564static int qcs_push_frame(struct qcs *qcs, struct buffer *out,
565 struct buffer *payload, int fin,
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100566 struct list *frm_list, uint64_t max_data)
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200567{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100568 struct qcc *qcc = qcs->qcc;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200569 struct quic_frame *frm;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100570 int head, left, to_xfer;
571 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200572
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100573 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100574
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100575 qc_get_buf(qcs, out);
576
577 /*
578 * QCS out buffer diagram
579 * head left to_xfer
580 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100581 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100582 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100583 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100584 * ^ ack-off ^ sent-off ^ off
585 *
586 * STREAM frame
587 * ^ ^
588 * |xxxxxxxxxxxxxxxxx|
589 */
590
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200591 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100592 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
593
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200594 head = qcs->tx.sent_offset - qcs->stream->ack_offset;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100595 left = qcs->tx.offset - qcs->tx.sent_offset;
596 to_xfer = QUIC_MIN(b_data(payload), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100597
598 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
599 /* do not exceed flow control limit */
600 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
601 to_xfer = qcs->tx.msd - qcs->tx.offset;
602
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100603 BUG_ON_HOT(max_data > qcc->rfctl.md);
604 /* do not overcome flow control limit on connection */
605 if (max_data + to_xfer > qcc->rfctl.md)
606 to_xfer = qcc->rfctl.md - max_data;
607
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100608 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200609 goto out;
610
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200611 frm = pool_zalloc(pool_head_quic_frame);
612 if (!frm)
613 goto err;
614
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100615 total = b_force_xfer(out, payload, to_xfer);
616
617 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200618 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200619 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100620 frm->stream.buf = out;
621 frm->stream.data = (unsigned char *)b_peek(out, head);
622
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100623 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200624 fin = fin && !b_data(payload);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200625 if (fin)
626 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100627
628 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200629 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100630 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200631 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100632
633 if (left + total) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200634 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100635 frm->stream.len = left + total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200636 }
637
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100638 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100639
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200640 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100641 {
642 struct qcs_push_frm_trace_arg arg = {
643 .sent = b_data(out), .xfer = total, .fin = fin,
644 .offset = qcs->tx.sent_offset
645 };
646 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_PUSH_FRM,
647 qcc->conn, qcs, &arg);
648 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100649
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200650 return total;
651
652 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100653 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200654 return -1;
655}
656
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100657/* This function must be called by the upper layer to inform about the sending
658 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
659 * <offset>.
660 */
661void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
662{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100663 struct qcc *qcc = qcs->qcc;
664 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100665
666 BUG_ON(offset > qcs->tx.sent_offset);
667
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100668 /* check if the STREAM frame has already been notified. It can happen
669 * for retransmission.
670 */
671 if (offset + data <= qcs->tx.sent_offset)
672 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100673
674 diff = offset + data - qcs->tx.sent_offset;
675
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100676 /* increase offset sum on connection */
677 qcc->tx.sent_offsets += diff;
678 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
679 if (qcc->tx.sent_offsets == qcc->rfctl.md)
680 qcc->flags |= QC_CF_BLK_MFCTL;
681
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100682 /* increase offset on stream */
683 qcs->tx.sent_offset += diff;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100684 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
685 if (qcs->tx.sent_offset == qcs->tx.msd)
686 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100687}
688
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100689/* Wrapper for send on transport layer. Send a list of frames <frms> for the
690 * connection <qcc>.
691 *
692 * Returns 0 if all data sent with success else non-zero.
693 */
694static int qc_send_frames(struct qcc *qcc, struct list *frms)
695{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100696 /* TODO implement an opportunistic retry mechanism. This is needed
697 * because qc_send_app_pkts is not completed. It will only prepare data
698 * up to its Tx buffer. The frames left are not send even if the Tx
699 * buffer is emptied by the sendto call.
700 *
701 * To overcome this, we call repeatedly qc_send_app_pkts until we
702 * detect that the transport layer has send nothing. This could happen
703 * on congestion or sendto syscall error.
704 *
705 * When qc_send_app_pkts is improved to handle retry by itself, we can
706 * remove the looping from the MUX.
707 */
708 struct quic_frame *first_frm;
709 uint64_t first_offset = 0;
710 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100711
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100712 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
713
714 if (LIST_ISEMPTY(frms)) {
715 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100716 return 0;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100717 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100718
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100719 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100720 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
721 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
722 first_offset = first_frm->stream.offset.key;
723 first_stream_frame_type = 1;
724 }
725 else {
726 first_stream_frame_type = 0;
727 }
728
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100729 if (!LIST_ISEMPTY(frms))
Willy Tarreau784b8682022-04-11 14:18:10 +0200730 qc_send_app_pkts(qcc->conn->handle.qc, frms);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100731
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100732 /* If there is frames left, check if the transport layer has send some
733 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100734 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100735 if (!LIST_ISEMPTY(frms)) {
736 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
737 goto retry_send;
738
739 /* If the first frame is STREAM, check if its offset has
740 * changed.
741 */
742 if (first_stream_frame_type &&
743 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
744 goto retry_send;
745 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100746 }
747
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100748 /* If there is frames left at this stage, transport layer is blocked.
749 * Subscribe on it to retry later.
750 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100751 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100752 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100753 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
754 SUB_RETRY_SEND, &qcc->wait_event);
755 return 1;
756 }
757
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100758 TRACE_LEAVE(QMUX_EV_QCC_SEND);
759
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100760 return 0;
761}
762
Amaury Denoyellec9337802022-04-04 16:36:34 +0200763/* Send a MAX_STREAM_BIDI frame to update the limit of bidirectional streams
764 * allowed to be opened by the peer. The caller should have first checked if
765 * this is required with qc_is_max_streams_needed.
766 *
767 * Returns 0 on success else non-zero.
768 */
769static int qc_send_max_streams(struct qcc *qcc)
770{
771 struct list frms = LIST_HEAD_INIT(frms);
772 struct quic_frame *frm;
773
774 frm = pool_zalloc(pool_head_quic_frame);
775 BUG_ON(!frm); /* TODO handle this properly */
776
777 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
778 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
779 qcc->lfctl.cl_bidi_r;
780 TRACE_DEVEL("sending MAX_STREAMS frame", QMUX_EV_SEND_FRM, qcc->conn, NULL, frm);
781 LIST_APPEND(&frms, &frm->list);
782
783 if (qc_send_frames(qcc, &frms))
784 return 1;
785
786 /* save the new limit if the frame has been send. */
787 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
788 qcc->lfctl.cl_bidi_r = 0;
789
790 return 0;
791}
792
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100793/* Proceed to sending. Loop through all available streams for the <qcc>
794 * instance and try to send as much as possible.
795 *
796 * Returns the total of bytes sent to the transport layer.
797 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100798static int qc_send(struct qcc *qcc)
799{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100800 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200801 struct eb64_node *node;
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100802 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200803
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100804 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200805
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200806 if (qcc->conn->flags & CO_FL_SOCK_WR_SH) {
807 qcc->conn->flags |= CO_FL_ERROR;
808 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
809 return 0;
810 }
811
Amaury Denoyellec9337802022-04-04 16:36:34 +0200812 if (qc_is_max_streams_needed(qcc))
813 qc_send_max_streams(qcc);
814
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100815 if (qcc->flags & QC_CF_BLK_MFCTL)
816 return 0;
817
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100818 /* loop through all streams, construct STREAM frames if data available.
819 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200820 */
821 node = eb64_first(&qcc->streams_by_id);
822 while (node) {
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200823 struct qc_stream_desc *stream = eb64_entry(node, struct qc_stream_desc, by_id);
824 struct qcs *qcs = stream->ctx;
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200825 struct buffer *buf = &qcs->tx.buf;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200826 struct buffer *out = &qcs->stream->buf;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100827
Amaury Denoyellee2ec9422022-03-10 16:46:18 +0100828 /* TODO
829 * for the moment, unidirectional streams have their own
830 * mechanism for sending. This should be unified in the future,
831 * in this case the next check will be removed.
832 */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200833 if (quic_stream_is_uni(qcs->id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +0100834 node = eb64_next(node);
835 continue;
836 }
837
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100838 if (qcs->flags & QC_SF_BLK_SFCTL) {
839 node = eb64_next(node);
840 continue;
841 }
842
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100843 if (b_data(buf) || b_data(out)) {
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100844 int ret;
Amaury Denoyelle92960912022-03-24 18:23:29 +0100845 char fin = !!(qcs->flags & QC_SF_FIN_STREAM);
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100846
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100847 ret = qcs_push_frame(qcs, out, buf, fin, &frms,
848 qcc->tx.sent_offsets + total);
Amaury Denoyelle14551132022-03-04 16:51:20 +0100849 BUG_ON(ret < 0); /* TODO handle this properly */
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200850
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100851 if (ret > 0) {
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100852 qcs_notify_send(qcs);
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100853 if (qcs->flags & QC_SF_BLK_MROOM)
854 qcs->flags &= ~QC_SF_BLK_MROOM;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100855 }
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200856
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200857 qcs->tx.offset += ret;
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100858 total += ret;
Amaury Denoyellea2c58a72021-12-03 14:38:31 +0100859
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100860 /* Subscribe if not all data can be send. */
Amaury Denoyellea2c58a72021-12-03 14:38:31 +0100861 if (b_data(buf)) {
862 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
863 SUB_RETRY_SEND, &qcc->wait_event);
864 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200865 }
866 node = eb64_next(node);
867 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200868
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100869 qc_send_frames(qcc, &frms);
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100870
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100871 TRACE_LEAVE(QMUX_EV_QCC_SEND);
872
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100873 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100874}
875
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +0100876/* Release all streams that are already marked as detached. This is only done
877 * if their TX buffers are empty or if a CONNECTION_CLOSE has been received.
878 *
879 * Return the number of released stream.
880 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100881static int qc_release_detached_streams(struct qcc *qcc)
882{
883 struct eb64_node *node;
884 int release = 0;
885
886 node = eb64_first(&qcc->streams_by_id);
887 while (node) {
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200888 struct qc_stream_desc *stream = eb64_entry(node, struct qc_stream_desc, by_id);
889 struct qcs *qcs = stream->ctx;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100890 node = eb64_next(node);
891
892 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200893 if (!b_data(&qcs->tx.buf) &&
894 qcs->tx.offset == qcs->tx.sent_offset) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100895 qcs_destroy(qcs);
896 release = 1;
897 }
898 else {
899 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
900 SUB_RETRY_SEND, &qcc->wait_event);
901 }
902 }
903 }
904
905 return release;
906}
907
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100908static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
909{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +0200910 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100911
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100912 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100913
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100914 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100915
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100916 if (qc_release_detached_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +0200917 if (qcc_is_dead(qcc)) {
918 qc_release(qcc);
919 }
920 else {
921 if (qcc_may_expire(qcc))
922 qcc->task->expire = tick_add(now_ms, qcc->timeout);
923 else
924 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +0100925 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100926 }
927 }
928
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100929 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
930
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100931 return NULL;
932}
933
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100934static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
935{
936 struct qcc *qcc = ctx;
937 int expired = tick_is_expired(t->expire, now_ms);
938
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100939 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100940
941 if (qcc) {
942 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100943 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100944 return t;
945 }
946
947 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100948 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100949 t->expire = TICK_ETERNITY;
950 return t;
951 }
952 }
953
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100954 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +0100955
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100956 if (!qcc) {
957 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +0100958 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100959 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +0100960
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100961 qcc->task = NULL;
962
963 if (qcc_is_dead(qcc))
964 qc_release(qcc);
965
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100966 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
967
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100968 return NULL;
969}
970
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100971static int qc_init(struct connection *conn, struct proxy *prx,
972 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100973{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100974 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100975 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100976
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100977 qcc = pool_alloc(pool_head_qcc);
978 if (!qcc)
979 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100980
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100981 qcc->conn = conn;
982 conn->ctx = qcc;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +0200983 qcc->nb_cs = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +0100984 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100985
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100986 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100987
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100988 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100989
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100990 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +0200991 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100992
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100993 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100994 qcc->tx.sent_offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100995
996 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100997 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100998 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
999 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001000 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001001 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001002
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001003 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001004 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
1005 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001006 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001007 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001008
1009 /* Server initiated streams must respect the server flow control. */
1010 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001011 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
1012 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001013 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001014 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1015
1016 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001017 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
1018 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001019 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001020 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001021
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001022 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
1023 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001024
Willy Tarreau784b8682022-04-11 14:18:10 +02001025 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001026 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001027 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1028 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1029
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001030 qcc->wait_event.tasklet = tasklet_new();
1031 if (!qcc->wait_event.tasklet)
1032 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001033
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001034 qcc->subs = NULL;
1035 qcc->wait_event.tasklet->process = qc_io_cb;
1036 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001037 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001038
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001039 /* haproxy timeouts */
1040 qcc->timeout = prx->timeout.client;
1041 qcc->task = task_new_here();
1042 if (!qcc->task)
1043 goto fail_no_timeout_task;
1044 qcc->task->process = qc_timeout_task;
1045 qcc->task->context = qcc;
1046 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1047
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001048 if (!conn_is_back(conn)) {
1049 if (!LIST_INLIST(&conn->stopping_list)) {
1050 LIST_APPEND(&mux_stopping_data[tid].list,
1051 &conn->stopping_list);
1052 }
1053 }
1054
Willy Tarreau784b8682022-04-11 14:18:10 +02001055 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001056 /* init read cycle */
1057 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001058
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001059 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001060
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001061 fail_no_timeout_task:
1062 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001063 fail_no_tasklet:
1064 pool_free(pool_head_qcc, qcc);
1065 fail_no_qcc:
1066 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001067}
1068
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001069static void qc_detach(struct conn_stream *cs)
1070{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001071 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001072 struct qcc *qcc = qcs->qcc;
1073
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001074 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001075
Amaury Denoyelle80388212022-04-08 12:00:12 +02001076 qcs->cs = NULL;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001077 --qcc->nb_cs;
1078
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001079 if ((b_data(&qcs->tx.buf) || qcs->tx.offset > qcs->tx.sent_offset) &&
1080 !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001081 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001082 qcs->flags |= QC_SF_DETACH;
1083 return;
1084 }
1085
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001086 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001087
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001088 if (qcc_is_dead(qcc)) {
1089 qc_release(qcc);
1090 }
1091 else {
1092 if (qcc_may_expire(qcc))
1093 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1094 else
1095 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001096 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001097 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001098
1099 TRACE_LEAVE(QMUX_EV_STRM_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001100}
1101
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001102/* Called from the upper layer, to receive data */
1103static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf,
1104 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001105{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001106 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001107 struct htx *qcs_htx = NULL;
1108 struct htx *cs_htx = NULL;
1109 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001110 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001111
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001112 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001113
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001114 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
1115 if (htx_is_empty(qcs_htx)) {
1116 /* Set buffer data to 0 as HTX is empty. */
1117 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1118 goto end;
1119 }
1120
1121 ret = qcs_htx->data;
1122
1123 cs_htx = htx_from_buf(buf);
1124 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
1125 htx_to_buf(cs_htx, buf);
1126 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1127 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
1128 goto end;
1129 }
1130
1131 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
1132 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
1133
1134 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001135 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
1136 cs_htx->flags |= HTX_FL_EOM;
1137 fin = 1;
1138 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001139
1140 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
1141 htx_to_buf(cs_htx, buf);
1142 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1143 ret -= qcs_htx->data;
1144
1145 end:
1146 if (b_data(&qcs->rx.app_buf)) {
Christopher Fauletb041b232022-03-24 10:27:02 +01001147 cs->endp->flags |= (CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001148 }
1149 else {
Christopher Fauletb041b232022-03-24 10:27:02 +01001150 cs->endp->flags &= ~(CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
1151 if (cs->endp->flags & CS_EP_ERR_PENDING)
1152 cs->endp->flags |= CS_EP_ERROR;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001153
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001154 if (fin)
Christopher Fauletb041b232022-03-24 10:27:02 +01001155 cs->endp->flags |= (CS_EP_EOI|CS_EP_EOS);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001156
1157 if (b_size(&qcs->rx.app_buf)) {
1158 b_free(&qcs->rx.app_buf);
1159 offer_buffers(NULL, 1);
1160 }
1161 }
1162
1163 if (ret)
1164 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
1165
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001166 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
1167
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001168 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001169}
1170
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001171static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf,
1172 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001173{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001174 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001175 size_t ret;
1176
1177 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001178
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001179 ret = qcs->qcc->app_ops->snd_buf(cs, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001180
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001181 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
1182
1183 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001184}
1185
1186/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1187 * event subscriber <es> is not allowed to change from a previous call as long
1188 * as at least one event is still subscribed. The <event_type> must only be a
1189 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1190 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001191static int qc_subscribe(struct conn_stream *cs, int event_type,
1192 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001193{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001194 return qcs_subscribe(__cs_mux(cs), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001195}
1196
1197/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1198 * The <es> pointer is not allowed to differ from the one passed to the
1199 * subscribe() call. It always returns zero.
1200 */
1201static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
1202{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001203 struct qcs *qcs = __cs_mux(cs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001204
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001205 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1206 BUG_ON(qcs->subs && qcs->subs != es);
1207
1208 es->events &= ~event_type;
1209 if (!es->events)
1210 qcs->subs = NULL;
1211
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001212 return 0;
1213}
1214
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001215/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Christopher Fauletb041b232022-03-24 10:27:02 +01001216 * report CS_EP_ERR_PENDING|CS_EP_ERROR on the attached conn-streams and wake
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001217 * them.
1218 */
1219static int qc_wake_some_streams(struct qcc *qcc)
1220{
1221 struct qc_stream_desc *stream;
1222 struct qcs *qcs;
1223 struct eb64_node *node;
1224
1225 for (node = eb64_first(&qcc->streams_by_id); node;
1226 node = eb64_next(node)) {
1227 stream = eb64_entry(node, struct qc_stream_desc, by_id);
1228 qcs = stream->ctx;
1229
1230 if (!qcs->cs)
1231 continue;
1232
1233 if (qcc->conn->flags & CO_FL_ERROR) {
Christopher Fauletb041b232022-03-24 10:27:02 +01001234 qcs->endp->flags |= CS_EP_ERR_PENDING;
1235 if (qcs->endp->flags & CS_EP_EOS)
1236 qcs->endp->flags |= CS_EP_ERROR;
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001237
1238 if (qcs->subs) {
1239 qcs_notify_recv(qcs);
1240 qcs_notify_send(qcs);
1241 }
1242 else if (qcs->cs->data_cb->wake) {
1243 qcs->cs->data_cb->wake(qcs->cs);
1244 }
1245 }
1246 }
1247
1248 return 0;
1249}
1250
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001251static int qc_wake(struct connection *conn)
1252{
1253 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02001254 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001255
1256 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001257
1258 /* Check if a soft-stop is in progress.
1259 * Release idling front connection if this is the case.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001260 *
1261 * TODO this is revelant for frontend connections only.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001262 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001263 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
1264 goto release;
1265
Willy Tarreau784b8682022-04-11 14:18:10 +02001266 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02001267 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
1268
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001269 qc_send(qcc);
1270
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001271 qc_wake_some_streams(qcc);
1272
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001273 if (qcc_is_dead(qcc))
1274 goto release;
1275
1276 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
1277
1278 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001279
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001280 release:
1281 qc_release(qcc);
1282 TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001283 return 1;
1284}
1285
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001286
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001287static void qmux_trace_frm(const struct quic_frame *frm)
1288{
1289 switch (frm->type) {
1290 case QUIC_FT_MAX_STREAMS_BIDI:
1291 chunk_appendf(&trace_buf, " max_streams=%lu",
1292 frm->max_streams_bidi.max_streams);
1293 break;
1294
1295 case QUIC_FT_MAX_STREAMS_UNI:
1296 chunk_appendf(&trace_buf, " max_streams=%lu",
1297 frm->max_streams_uni.max_streams);
1298 break;
1299
1300 default:
1301 break;
1302 }
1303}
1304
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001305/* quic-mux trace handler */
1306static void qmux_trace(enum trace_level level, uint64_t mask,
1307 const struct trace_source *src,
1308 const struct ist where, const struct ist func,
1309 const void *a1, const void *a2, const void *a3, const void *a4)
1310{
1311 const struct connection *conn = a1;
1312 const struct qcc *qcc = conn ? conn->ctx : NULL;
1313 const struct qcs *qcs = a2;
1314
1315 if (!qcc)
1316 return;
1317
1318 if (src->verbosity > QMUX_VERB_CLEAN) {
1319 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
1320
1321 if (qcs)
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001322 chunk_appendf(&trace_buf, " qcs=%p(%lu)", qcs, qcs->id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001323
1324 if (mask & QMUX_EV_QCC_NQCS) {
1325 const uint64_t *id = a3;
1326 chunk_appendf(&trace_buf, " id=%lu", *id);
1327 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001328
1329 if (mask & QMUX_EV_SEND_FRM)
1330 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001331
1332 if (mask & QMUX_EV_QCS_PUSH_FRM) {
1333 const struct qcs_push_frm_trace_arg *arg = a3;
1334 chunk_appendf(&trace_buf, " sent=%lu xfer=%d fin=%d offset=%lu",
1335 arg->sent, arg->xfer, arg->fin, arg->offset);
1336 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001337 }
1338}
1339
Amaury Denoyelle251eadf2022-03-24 17:14:52 +01001340/* Function to automatically activate QUIC MUX traces on stdout.
1341 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
1342 * Main use for now is in the docker image for QUIC interop testing.
1343 */
1344static void qmux_init_stdout_traces(void)
1345{
1346#ifdef ENABLE_QUIC_STDOUT_TRACES
1347 trace_qmux.sink = sink_find("stdout");
1348 trace_qmux.level = TRACE_LEVEL_DEVELOPER;
1349 trace_qmux.state = TRACE_STATE_RUNNING;
1350 trace_qmux.verbosity = QMUX_VERB_MINIMAL;
1351#endif
1352}
1353INITCALL0(STG_INIT, qmux_init_stdout_traces);
1354
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001355
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001356static const struct mux_ops qc_ops = {
1357 .init = qc_init,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001358 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001359 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001360 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001361 .subscribe = qc_subscribe,
1362 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001363 .wake = qc_wake,
Christopher Fauleta97cced2022-04-12 18:04:10 +02001364 .flags = MX_FL_HTX|MX_FL_NO_UPG,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02001365 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001366};
1367
1368static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001369 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001370
1371INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);