blob: 275b92a9df7fa057e22bb3ae97bf94396fe51a03 [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
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200120 qcs->id = id;
121 /* store transport layer stream descriptor in qcc tree */
122 eb64_insert(&qcc->streams_by_id, &stream->by_id);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100123 qcc->strms[type].nb_streams++;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100124
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100125 /* If stream is local, use peer remote-limit, or else the opposite. */
126 /* TODO use uni limit for unidirectional streams */
127 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
128 qcc->rfctl.msd_bidi_l;
129
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100130 qcs->rx.buf = BUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100131 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100132 qcs->rx.offset = 0;
133 qcs->rx.frms = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100134
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100135 qcs->tx.buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100136 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100137 qcs->tx.sent_offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100138
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100139 qcs->wait_event.tasklet = NULL;
140 qcs->wait_event.events = 0;
141 qcs->subs = NULL;
142
143 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100144 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100145 return qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100146}
147
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200148/* Free a qcs. This function must only be done to remove a stream on allocation
149 * error or connection shutdown. Else use qcs_destroy which handle all the
150 * QUIC connection mechanism.
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100151 */
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200152void qcs_free(struct qcs *qcs)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100153{
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200154 b_free(&qcs->rx.buf);
155 b_free(&qcs->tx.buf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200156
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200157 BUG_ON(!qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams);
158 --qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams;
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200159
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200160 /* stream desc must be removed from MUX tree before release it */
161 eb64_delete(&qcs->stream->by_id);
Willy Tarreau784b8682022-04-11 14:18:10 +0200162 qc_stream_desc_release(qcs->stream, qcs->qcc->conn->handle.qc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100163 pool_free(pool_head_qcs, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100164}
165
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100166struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100167{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100168 struct buffer *buf = b_alloc(bptr);
169 BUG_ON(!buf);
170 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100171}
172
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100173int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
174{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100175 struct qcc *qcc = qcs->qcc;
176
177 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100178
179 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
180 BUG_ON(qcs->subs && qcs->subs != es);
181
182 es->events |= event_type;
183 qcs->subs = es;
184
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100185 if (event_type & SUB_RETRY_RECV)
186 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
187
188 if (event_type & SUB_RETRY_SEND)
189 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
190
191 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
192
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100193 return 0;
194}
195
196void qcs_notify_recv(struct qcs *qcs)
197{
198 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
199 tasklet_wakeup(qcs->subs->tasklet);
200 qcs->subs->events &= ~SUB_RETRY_RECV;
201 if (!qcs->subs->events)
202 qcs->subs = NULL;
203 }
204}
205
206void qcs_notify_send(struct qcs *qcs)
207{
208 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
209 tasklet_wakeup(qcs->subs->tasklet);
210 qcs->subs->events &= ~SUB_RETRY_SEND;
211 if (!qcs->subs->events)
212 qcs->subs = NULL;
213 }
214}
215
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100216/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
217 * several streams, depending on the already open ones.
218 * Return this node if succeeded, NULL if not.
219 */
Amaury Denoyelle50742292022-03-29 14:57:19 +0200220struct qcs *qcc_get_qcs(struct qcc *qcc, uint64_t id)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100221{
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200222 struct qc_stream_desc *stream;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100223 unsigned int strm_type;
224 int64_t sub_id;
225 struct eb64_node *strm_node;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200226 struct qcs *qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100227
228 strm_type = id & QCS_ID_TYPE_MASK;
229 sub_id = id >> QCS_ID_TYPE_SHIFT;
230 strm_node = NULL;
Amaury Denoyelle0dc40f02022-02-07 11:44:17 +0100231 if (quic_stream_is_local(qcc, id)) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100232 /* Local streams: this stream must be already opened. */
233 strm_node = eb64_lookup(&qcc->streams_by_id, id);
234 if (!strm_node) {
235 /* unknown stream id */
236 goto out;
237 }
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200238 stream = eb64_entry(strm_node, struct qc_stream_desc, by_id);
239 qcs = stream->ctx;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100240 }
241 else {
242 /* Remote streams. */
243 struct eb_root *strms;
244 uint64_t largest_id;
245 enum qcs_type qcs_type;
246
247 strms = &qcc->streams_by_id;
248 qcs_type = qcs_id_type(id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100249
250 /* TODO also checks max-streams for uni streams */
251 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100252 if (sub_id + 1 > qcc->lfctl.ms_bidi) {
Amaury Denoyellec055e302022-02-07 16:09:06 +0100253 /* streams limit reached */
254 goto out;
255 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100256 }
257
258 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
259 * correct value.
260 */
261 largest_id = qcc->strms[qcs_type].largest_id;
262 if (sub_id > (int64_t)largest_id) {
263 /* RFC: "A stream ID that is used out of order results in all streams
264 * of that type with lower-numbered stream IDs also being opened".
265 * So, let's "open" these streams.
266 */
267 int64_t i;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200268 struct qcs *tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100269
Amaury Denoyelle50742292022-03-29 14:57:19 +0200270 tmp_qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100271 for (i = largest_id + 1; i <= sub_id; i++) {
272 uint64_t id = (i << QCS_ID_TYPE_SHIFT) | strm_type;
273 enum qcs_type type = id & QCS_ID_DIR_BIT ? QCS_CLT_UNI : QCS_CLT_BIDI;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200274
Amaury Denoyelle50742292022-03-29 14:57:19 +0200275 tmp_qcs = qcs_new(qcc, id, type);
276 if (!tmp_qcs) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100277 /* allocation failure */
278 goto out;
279 }
280
281 qcc->strms[qcs_type].largest_id = i;
282 }
Amaury Denoyelle50742292022-03-29 14:57:19 +0200283 if (tmp_qcs)
284 qcs = tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100285 }
286 else {
287 strm_node = eb64_lookup(strms, id);
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200288 if (strm_node) {
289 stream = eb64_entry(strm_node, struct qc_stream_desc, by_id);
290 qcs = stream->ctx;
291 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100292 }
293 }
294
Amaury Denoyelle50742292022-03-29 14:57:19 +0200295 return qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100296
297 out:
298 return NULL;
299}
300
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100301/* Handle a new STREAM frame <strm_frm>. The frame content will be copied in
302 * the buffer of the stream instance. The stream instance will be stored in
303 * <out_qcs>. In case of success, the caller can immediatly call qcc_decode_qcs
304 * to process the frame content.
305 *
306 * Returns 0 on success. On errors, two codes are present.
307 * - 1 is returned if the frame cannot be decoded and must be discarded.
308 * - 2 is returned if the stream cannot decode at the moment the frame. The
309 * frame should be buffered to be handled later.
310 */
311int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
312 char fin, char *data, struct qcs **out_qcs)
313{
314 struct qcs *qcs;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100315 size_t total, diff;
316
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100317 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
318
Amaury Denoyelle50742292022-03-29 14:57:19 +0200319 qcs = qcc_get_qcs(qcc, id);
320 if (!qcs) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100321 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 +0100322 return 1;
323 }
324
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100325 *out_qcs = qcs;
326
327 if (offset > qcs->rx.offset)
328 return 2;
329
330 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100331 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 +0100332 return 1;
333 }
334
335 /* Last frame already handled for this stream. */
336 BUG_ON(qcs->flags & QC_SF_FIN_RECV);
337
338 if (!qc_get_buf(qcs, &qcs->rx.buf)) {
339 /* TODO should mark qcs as full */
340 return 2;
341 }
342
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100343 TRACE_DEVEL("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100344 diff = qcs->rx.offset - offset;
345
346 /* TODO do not partially copy a frame if not enough size left. Maybe
347 * this can be optimized.
348 */
349 if (len > b_room(&qcs->rx.buf)) {
350 /* TODO handle STREAM frames larger than RX buffer. */
351 BUG_ON(len > b_size(&qcs->rx.buf));
352 return 2;
353 }
354
355 len -= diff;
356 data += diff;
357
358 total = b_putblk(&qcs->rx.buf, data, len);
359 /* TODO handle partial copy of a STREAM frame. */
360 BUG_ON(len != total);
361
362 qcs->rx.offset += total;
363
364 if (fin)
365 qcs->flags |= QC_SF_FIN_RECV;
366
367 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100368 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100369 return 0;
370}
371
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100372/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
373 * the frame.
374 *
375 * Returns 0 on success else non-zero.
376 */
377int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
378{
379 if (qcc->rfctl.md < max) {
380 qcc->rfctl.md = max;
381
382 if (qcc->flags & QC_CF_BLK_MFCTL) {
383 qcc->flags &= ~QC_CF_BLK_MFCTL;
384 tasklet_wakeup(qcc->wait_event.tasklet);
385 }
386 }
387 return 0;
388}
389
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100390/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
391 * field of the frame and <id> is the identifier of the QUIC stream.
392 *
393 * Returns 0 on success else non-zero.
394 */
395int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
396{
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200397 struct qc_stream_desc *stream;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100398 struct qcs *qcs;
399 struct eb64_node *node;
400
401 node = eb64_lookup(&qcc->streams_by_id, id);
402 if (node) {
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200403 stream = eb64_entry(node, struct qc_stream_desc, by_id);
404 qcs = stream->ctx;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100405 if (max > qcs->tx.msd) {
406 qcs->tx.msd = max;
407
408 if (qcs->flags & QC_SF_BLK_SFCTL) {
409 qcs->flags &= ~QC_SF_BLK_SFCTL;
410 tasklet_wakeup(qcc->wait_event.tasklet);
411 }
412 }
413 }
414
415 return 0;
416}
417
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100418/* Decode the content of STREAM frames already received on the stream instance
419 * <qcs>.
420 *
421 * Returns 0 on success else non-zero.
422 */
423int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
424{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100425 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
426
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100427 if (qcc->app_ops->decode_qcs(qcs, qcs->flags & QC_SF_FIN_RECV, qcc->ctx) < 0) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100428 TRACE_DEVEL("leaving on decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100429 return 1;
430 }
431
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100432 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
433
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100434 return 0;
435}
436
Amaury Denoyellec055e302022-02-07 16:09:06 +0100437static int qc_is_max_streams_needed(struct qcc *qcc)
438{
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100439 return qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100440}
441
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500442/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100443static void qcs_destroy(struct qcs *qcs)
444{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100445 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200446 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100447
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100448 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100449
Amaury Denoyellec055e302022-02-07 16:09:06 +0100450 if (quic_stream_is_remote(qcs->qcc, id)) {
451 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100452 ++qcs->qcc->lfctl.cl_bidi_r;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100453 if (qc_is_max_streams_needed(qcs->qcc))
454 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
455 }
456 }
457
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200458 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100459
460 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100461}
462
463static inline int qcc_is_dead(const struct qcc *qcc)
464{
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200465 if (qcc->app_ops && qcc->app_ops->is_active &&
466 qcc->app_ops->is_active(qcc, qcc->ctx))
467 return 0;
468
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200469 if ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task)
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100470 return 1;
471
472 return 0;
473}
474
475/* Return true if the mux timeout should be armed. */
476static inline int qcc_may_expire(struct qcc *qcc)
477{
Amaury Denoyelle06890aa2022-04-04 16:15:06 +0200478 return !qcc->nb_cs;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100479}
480
481/* release function. This one should be called to free all resources allocated
482 * to the mux.
483 */
484static void qc_release(struct qcc *qcc)
485{
486 struct connection *conn = NULL;
487
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100488 TRACE_ENTER(QMUX_EV_QCC_END);
489
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100490 if (qcc) {
Amaury Denoyellef8909452022-03-30 11:51:56 +0200491 struct eb64_node *node;
492
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100493 /* The connection must be aattached to this mux to be released */
494 if (qcc->conn && qcc->conn->ctx == qcc)
495 conn = qcc->conn;
496
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100497 TRACE_DEVEL("freeing qcc", QMUX_EV_QCC_END, conn);
498
Amaury Denoyellecbc13b72022-03-29 14:46:38 +0200499 if (qcc->app_ops && qcc->app_ops->release)
500 qcc->app_ops->release(qcc->ctx);
501
Amaury Denoyelle06890aa2022-04-04 16:15:06 +0200502 if (qcc->task) {
503 task_destroy(qcc->task);
504 qcc->task = NULL;
505 }
506
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100507 if (qcc->wait_event.tasklet)
508 tasklet_free(qcc->wait_event.tasklet);
509
Amaury Denoyellef8909452022-03-30 11:51:56 +0200510 /* liberate remaining qcs instances */
511 node = eb64_first(&qcc->streams_by_id);
512 while (node) {
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200513 struct qc_stream_desc *stream = eb64_entry(node, struct qc_stream_desc, by_id);
Amaury Denoyellef8909452022-03-30 11:51:56 +0200514 node = eb64_next(node);
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200515 qcs_free(stream->ctx);
Amaury Denoyellef8909452022-03-30 11:51:56 +0200516 }
517
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100518 pool_free(pool_head_qcc, qcc);
519 }
520
521 if (conn) {
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100522 LIST_DEL_INIT(&conn->stopping_list);
523
Willy Tarreau784b8682022-04-11 14:18:10 +0200524 conn->handle.qc->conn = NULL;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100525 conn->mux = NULL;
526 conn->ctx = NULL;
527
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100528 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
529
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100530 conn_stop_tracking(conn);
531 conn_full_close(conn);
532 if (conn->destroy_cb)
533 conn->destroy_cb(conn);
534 conn_free(conn);
535 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100536
537 TRACE_LEAVE(QMUX_EV_QCC_END);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100538}
539
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100540/* Prepare a STREAM frame for <qcs> instance. First, transfer data from
541 * <payload> to <out> buffer. The STREAM frame payload points to the <out>
542 * buffer. The frame is then pushed to <frm_list>. If <fin> is set, and the
543 * <payload> buf is emptied after transfer, FIN bit is set on the STREAM frame.
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100544 * Transfer is automatically adjusted to not exceed the stream flow-control
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100545 * limit. <max_data> must contains the current sum offsets for the connection.
546 * This is useful to not exceed the connection flow-control limit when using
547 * repeatdly this function on multiple streams before passing the data to the
548 * lower layer.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100549 *
550 * Returns the total bytes of newly transferred data or a negative error code.
551 */
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100552static int qcs_push_frame(struct qcs *qcs, struct buffer *out,
553 struct buffer *payload, int fin,
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100554 struct list *frm_list, uint64_t max_data)
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200555{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100556 struct qcc *qcc = qcs->qcc;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200557 struct quic_frame *frm;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100558 int head, left, to_xfer;
559 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200560
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100561 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100562
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100563 qc_get_buf(qcs, out);
564
565 /*
566 * QCS out buffer diagram
567 * head left to_xfer
568 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100569 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100570 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100571 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100572 * ^ ack-off ^ sent-off ^ off
573 *
574 * STREAM frame
575 * ^ ^
576 * |xxxxxxxxxxxxxxxxx|
577 */
578
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200579 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100580 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
581
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200582 head = qcs->tx.sent_offset - qcs->stream->ack_offset;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100583 left = qcs->tx.offset - qcs->tx.sent_offset;
584 to_xfer = QUIC_MIN(b_data(payload), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100585
586 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
587 /* do not exceed flow control limit */
588 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
589 to_xfer = qcs->tx.msd - qcs->tx.offset;
590
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100591 BUG_ON_HOT(max_data > qcc->rfctl.md);
592 /* do not overcome flow control limit on connection */
593 if (max_data + to_xfer > qcc->rfctl.md)
594 to_xfer = qcc->rfctl.md - max_data;
595
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100596 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200597 goto out;
598
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200599 frm = pool_zalloc(pool_head_quic_frame);
600 if (!frm)
601 goto err;
602
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100603 total = b_force_xfer(out, payload, to_xfer);
604
605 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200606 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200607 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100608 frm->stream.buf = out;
609 frm->stream.data = (unsigned char *)b_peek(out, head);
610
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100611 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200612 fin = fin && !b_data(payload);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200613 if (fin)
614 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100615
616 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200617 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100618 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200619 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100620
621 if (left + total) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200622 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100623 frm->stream.len = left + total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200624 }
625
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100626 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100627
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200628 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100629 {
630 struct qcs_push_frm_trace_arg arg = {
631 .sent = b_data(out), .xfer = total, .fin = fin,
632 .offset = qcs->tx.sent_offset
633 };
634 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_PUSH_FRM,
635 qcc->conn, qcs, &arg);
636 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100637
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200638 return total;
639
640 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100641 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200642 return -1;
643}
644
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100645/* This function must be called by the upper layer to inform about the sending
646 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
647 * <offset>.
648 */
649void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
650{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100651 struct qcc *qcc = qcs->qcc;
652 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100653
654 BUG_ON(offset > qcs->tx.sent_offset);
655
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100656 /* check if the STREAM frame has already been notified. It can happen
657 * for retransmission.
658 */
659 if (offset + data <= qcs->tx.sent_offset)
660 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100661
662 diff = offset + data - qcs->tx.sent_offset;
663
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100664 /* increase offset sum on connection */
665 qcc->tx.sent_offsets += diff;
666 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
667 if (qcc->tx.sent_offsets == qcc->rfctl.md)
668 qcc->flags |= QC_CF_BLK_MFCTL;
669
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100670 /* increase offset on stream */
671 qcs->tx.sent_offset += diff;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100672 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
673 if (qcs->tx.sent_offset == qcs->tx.msd)
674 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100675}
676
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100677/* Wrapper for send on transport layer. Send a list of frames <frms> for the
678 * connection <qcc>.
679 *
680 * Returns 0 if all data sent with success else non-zero.
681 */
682static int qc_send_frames(struct qcc *qcc, struct list *frms)
683{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100684 /* TODO implement an opportunistic retry mechanism. This is needed
685 * because qc_send_app_pkts is not completed. It will only prepare data
686 * up to its Tx buffer. The frames left are not send even if the Tx
687 * buffer is emptied by the sendto call.
688 *
689 * To overcome this, we call repeatedly qc_send_app_pkts until we
690 * detect that the transport layer has send nothing. This could happen
691 * on congestion or sendto syscall error.
692 *
693 * When qc_send_app_pkts is improved to handle retry by itself, we can
694 * remove the looping from the MUX.
695 */
696 struct quic_frame *first_frm;
697 uint64_t first_offset = 0;
698 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100699
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100700 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
701
702 if (LIST_ISEMPTY(frms)) {
703 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100704 return 0;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100705 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100706
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100707 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100708 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
709 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
710 first_offset = first_frm->stream.offset.key;
711 first_stream_frame_type = 1;
712 }
713 else {
714 first_stream_frame_type = 0;
715 }
716
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100717 if (!LIST_ISEMPTY(frms))
Willy Tarreau784b8682022-04-11 14:18:10 +0200718 qc_send_app_pkts(qcc->conn->handle.qc, frms);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100719
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100720 /* If there is frames left, check if the transport layer has send some
721 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100722 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100723 if (!LIST_ISEMPTY(frms)) {
724 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
725 goto retry_send;
726
727 /* If the first frame is STREAM, check if its offset has
728 * changed.
729 */
730 if (first_stream_frame_type &&
731 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
732 goto retry_send;
733 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100734 }
735
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100736 /* If there is frames left at this stage, transport layer is blocked.
737 * Subscribe on it to retry later.
738 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100739 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100740 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100741 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
742 SUB_RETRY_SEND, &qcc->wait_event);
743 return 1;
744 }
745
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100746 TRACE_LEAVE(QMUX_EV_QCC_SEND);
747
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100748 return 0;
749}
750
Amaury Denoyellec9337802022-04-04 16:36:34 +0200751/* Send a MAX_STREAM_BIDI frame to update the limit of bidirectional streams
752 * allowed to be opened by the peer. The caller should have first checked if
753 * this is required with qc_is_max_streams_needed.
754 *
755 * Returns 0 on success else non-zero.
756 */
757static int qc_send_max_streams(struct qcc *qcc)
758{
759 struct list frms = LIST_HEAD_INIT(frms);
760 struct quic_frame *frm;
761
762 frm = pool_zalloc(pool_head_quic_frame);
763 BUG_ON(!frm); /* TODO handle this properly */
764
765 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
766 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
767 qcc->lfctl.cl_bidi_r;
768 TRACE_DEVEL("sending MAX_STREAMS frame", QMUX_EV_SEND_FRM, qcc->conn, NULL, frm);
769 LIST_APPEND(&frms, &frm->list);
770
771 if (qc_send_frames(qcc, &frms))
772 return 1;
773
774 /* save the new limit if the frame has been send. */
775 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
776 qcc->lfctl.cl_bidi_r = 0;
777
778 return 0;
779}
780
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100781/* Proceed to sending. Loop through all available streams for the <qcc>
782 * instance and try to send as much as possible.
783 *
784 * Returns the total of bytes sent to the transport layer.
785 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100786static int qc_send(struct qcc *qcc)
787{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100788 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200789 struct eb64_node *node;
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100790 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200791
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100792 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200793
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200794 if (qcc->conn->flags & CO_FL_SOCK_WR_SH) {
795 qcc->conn->flags |= CO_FL_ERROR;
796 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
797 return 0;
798 }
799
Amaury Denoyellec9337802022-04-04 16:36:34 +0200800 if (qc_is_max_streams_needed(qcc))
801 qc_send_max_streams(qcc);
802
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100803 if (qcc->flags & QC_CF_BLK_MFCTL)
804 return 0;
805
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100806 /* loop through all streams, construct STREAM frames if data available.
807 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200808 */
809 node = eb64_first(&qcc->streams_by_id);
810 while (node) {
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200811 struct qc_stream_desc *stream = eb64_entry(node, struct qc_stream_desc, by_id);
812 struct qcs *qcs = stream->ctx;
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200813 struct buffer *buf = &qcs->tx.buf;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200814 struct buffer *out = &qcs->stream->buf;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100815
Amaury Denoyellee2ec9422022-03-10 16:46:18 +0100816 /* TODO
817 * for the moment, unidirectional streams have their own
818 * mechanism for sending. This should be unified in the future,
819 * in this case the next check will be removed.
820 */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200821 if (quic_stream_is_uni(qcs->id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +0100822 node = eb64_next(node);
823 continue;
824 }
825
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100826 if (qcs->flags & QC_SF_BLK_SFCTL) {
827 node = eb64_next(node);
828 continue;
829 }
830
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100831 if (b_data(buf) || b_data(out)) {
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100832 int ret;
Amaury Denoyelle92960912022-03-24 18:23:29 +0100833 char fin = !!(qcs->flags & QC_SF_FIN_STREAM);
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100834
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100835 ret = qcs_push_frame(qcs, out, buf, fin, &frms,
836 qcc->tx.sent_offsets + total);
Amaury Denoyelle14551132022-03-04 16:51:20 +0100837 BUG_ON(ret < 0); /* TODO handle this properly */
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200838
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100839 if (ret > 0) {
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100840 qcs_notify_send(qcs);
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100841 if (qcs->flags & QC_SF_BLK_MROOM)
842 qcs->flags &= ~QC_SF_BLK_MROOM;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100843 }
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200844
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200845 qcs->tx.offset += ret;
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100846 total += ret;
Amaury Denoyellea2c58a72021-12-03 14:38:31 +0100847
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100848 /* Subscribe if not all data can be send. */
Amaury Denoyellea2c58a72021-12-03 14:38:31 +0100849 if (b_data(buf)) {
850 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
851 SUB_RETRY_SEND, &qcc->wait_event);
852 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200853 }
854 node = eb64_next(node);
855 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200856
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100857 qc_send_frames(qcc, &frms);
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100858
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100859 TRACE_LEAVE(QMUX_EV_QCC_SEND);
860
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100861 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100862}
863
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +0100864/* Release all streams that are already marked as detached. This is only done
865 * if their TX buffers are empty or if a CONNECTION_CLOSE has been received.
866 *
867 * Return the number of released stream.
868 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100869static int qc_release_detached_streams(struct qcc *qcc)
870{
871 struct eb64_node *node;
872 int release = 0;
873
874 node = eb64_first(&qcc->streams_by_id);
875 while (node) {
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200876 struct qc_stream_desc *stream = eb64_entry(node, struct qc_stream_desc, by_id);
877 struct qcs *qcs = stream->ctx;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100878 node = eb64_next(node);
879
880 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200881 if (!b_data(&qcs->tx.buf) &&
882 qcs->tx.offset == qcs->tx.sent_offset) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100883 qcs_destroy(qcs);
884 release = 1;
885 }
886 else {
887 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
888 SUB_RETRY_SEND, &qcc->wait_event);
889 }
890 }
891 }
892
893 return release;
894}
895
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100896static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
897{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +0200898 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100899
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100900 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100901
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100902 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100903
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100904 if (qc_release_detached_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +0200905 if (qcc_is_dead(qcc)) {
906 qc_release(qcc);
907 }
908 else {
909 if (qcc_may_expire(qcc))
910 qcc->task->expire = tick_add(now_ms, qcc->timeout);
911 else
912 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +0100913 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100914 }
915 }
916
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100917 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
918
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100919 return NULL;
920}
921
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100922static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
923{
924 struct qcc *qcc = ctx;
925 int expired = tick_is_expired(t->expire, now_ms);
926
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100927 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100928
929 if (qcc) {
930 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100931 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100932 return t;
933 }
934
935 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100936 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100937 t->expire = TICK_ETERNITY;
938 return t;
939 }
940 }
941
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100942 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +0100943
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100944 if (!qcc) {
945 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +0100946 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100947 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +0100948
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100949 qcc->task = NULL;
950
951 if (qcc_is_dead(qcc))
952 qc_release(qcc);
953
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100954 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
955
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100956 return NULL;
957}
958
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100959static int qc_init(struct connection *conn, struct proxy *prx,
960 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100961{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100962 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100963 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100964
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100965 qcc = pool_alloc(pool_head_qcc);
966 if (!qcc)
967 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100968
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100969 qcc->conn = conn;
970 conn->ctx = qcc;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +0200971 qcc->nb_cs = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +0100972 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100973
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100974 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100975
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100976 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100977
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100978 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +0200979 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100980
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100981 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100982 qcc->tx.sent_offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100983
984 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100985 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100986 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
987 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100988 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100989 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100990
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100991 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100992 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
993 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100994 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100995 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100996
997 /* Server initiated streams must respect the server flow control. */
998 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100999 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
1000 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001001 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001002 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1003
1004 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001005 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
1006 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001007 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001008 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001009
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001010 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
1011 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001012
Willy Tarreau784b8682022-04-11 14:18:10 +02001013 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001014 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001015 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1016 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1017
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001018 qcc->wait_event.tasklet = tasklet_new();
1019 if (!qcc->wait_event.tasklet)
1020 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001021
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001022 qcc->subs = NULL;
1023 qcc->wait_event.tasklet->process = qc_io_cb;
1024 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001025 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001026
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001027 /* haproxy timeouts */
1028 qcc->timeout = prx->timeout.client;
1029 qcc->task = task_new_here();
1030 if (!qcc->task)
1031 goto fail_no_timeout_task;
1032 qcc->task->process = qc_timeout_task;
1033 qcc->task->context = qcc;
1034 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1035
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001036 if (!conn_is_back(conn)) {
1037 if (!LIST_INLIST(&conn->stopping_list)) {
1038 LIST_APPEND(&mux_stopping_data[tid].list,
1039 &conn->stopping_list);
1040 }
1041 }
1042
Willy Tarreau784b8682022-04-11 14:18:10 +02001043 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001044 /* init read cycle */
1045 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001046
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001047 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001048
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001049 fail_no_timeout_task:
1050 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001051 fail_no_tasklet:
1052 pool_free(pool_head_qcc, qcc);
1053 fail_no_qcc:
1054 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001055}
1056
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001057static void qc_detach(struct conn_stream *cs)
1058{
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001059 struct qcs *qcs = cs->ctx;
1060 struct qcc *qcc = qcs->qcc;
1061
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001062 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001063
Amaury Denoyelle80388212022-04-08 12:00:12 +02001064 cs->ctx = NULL;
1065 qcs->cs = NULL;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001066 --qcc->nb_cs;
1067
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001068 if ((b_data(&qcs->tx.buf) || qcs->tx.offset > qcs->tx.sent_offset) &&
1069 !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001070 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001071 qcs->flags |= QC_SF_DETACH;
1072 return;
1073 }
1074
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001075 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001076
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001077 if (qcc_is_dead(qcc)) {
1078 qc_release(qcc);
1079 }
1080 else {
1081 if (qcc_may_expire(qcc))
1082 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1083 else
1084 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001085 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001086 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001087
1088 TRACE_LEAVE(QMUX_EV_STRM_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001089}
1090
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001091/* Called from the upper layer, to receive data */
1092static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf,
1093 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001094{
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001095 struct qcs *qcs = cs->ctx;
1096 struct htx *qcs_htx = NULL;
1097 struct htx *cs_htx = NULL;
1098 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001099 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001100
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001101 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001102
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001103 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
1104 if (htx_is_empty(qcs_htx)) {
1105 /* Set buffer data to 0 as HTX is empty. */
1106 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1107 goto end;
1108 }
1109
1110 ret = qcs_htx->data;
1111
1112 cs_htx = htx_from_buf(buf);
1113 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
1114 htx_to_buf(cs_htx, buf);
1115 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1116 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
1117 goto end;
1118 }
1119
1120 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
1121 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
1122
1123 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001124 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
1125 cs_htx->flags |= HTX_FL_EOM;
1126 fin = 1;
1127 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001128
1129 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
1130 htx_to_buf(cs_htx, buf);
1131 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1132 ret -= qcs_htx->data;
1133
1134 end:
1135 if (b_data(&qcs->rx.app_buf)) {
1136 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1137 }
1138 else {
1139 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
1140 if (cs->flags & CS_FL_ERR_PENDING)
1141 cs->flags |= CS_FL_ERROR;
1142
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001143 if (fin)
1144 cs->flags |= (CS_FL_EOI|CS_FL_EOS);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001145
1146 if (b_size(&qcs->rx.app_buf)) {
1147 b_free(&qcs->rx.app_buf);
1148 offer_buffers(NULL, 1);
1149 }
1150 }
1151
1152 if (ret)
1153 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
1154
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001155 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
1156
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001157 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001158}
1159
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001160static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf,
1161 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001162{
1163 struct qcs *qcs = cs->ctx;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001164 size_t ret;
1165
1166 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001167
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001168 ret = qcs->qcc->app_ops->snd_buf(cs, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001169
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001170 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
1171
1172 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001173}
1174
1175/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1176 * event subscriber <es> is not allowed to change from a previous call as long
1177 * as at least one event is still subscribed. The <event_type> must only be a
1178 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1179 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001180static int qc_subscribe(struct conn_stream *cs, int event_type,
1181 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001182{
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01001183 return qcs_subscribe(cs->ctx, event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001184}
1185
1186/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1187 * The <es> pointer is not allowed to differ from the one passed to the
1188 * subscribe() call. It always returns zero.
1189 */
1190static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
1191{
1192 struct qcs *qcs = cs->ctx;
1193
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001194 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1195 BUG_ON(qcs->subs && qcs->subs != es);
1196
1197 es->events &= ~event_type;
1198 if (!es->events)
1199 qcs->subs = NULL;
1200
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001201 return 0;
1202}
1203
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001204/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
1205 * report CS_FL_ERR_PENDING|CS_FL_ERROR on the attached conn-streams and wake
1206 * them.
1207 */
1208static int qc_wake_some_streams(struct qcc *qcc)
1209{
1210 struct qc_stream_desc *stream;
1211 struct qcs *qcs;
1212 struct eb64_node *node;
1213
1214 for (node = eb64_first(&qcc->streams_by_id); node;
1215 node = eb64_next(node)) {
1216 stream = eb64_entry(node, struct qc_stream_desc, by_id);
1217 qcs = stream->ctx;
1218
1219 if (!qcs->cs)
1220 continue;
1221
1222 if (qcc->conn->flags & CO_FL_ERROR) {
1223 qcs->cs->flags |= CS_FL_ERR_PENDING;
1224 if (qcs->cs->flags & CS_FL_EOS)
1225 qcs->cs->flags |= CS_FL_ERROR;
1226
1227 if (qcs->subs) {
1228 qcs_notify_recv(qcs);
1229 qcs_notify_send(qcs);
1230 }
1231 else if (qcs->cs->data_cb->wake) {
1232 qcs->cs->data_cb->wake(qcs->cs);
1233 }
1234 }
1235 }
1236
1237 return 0;
1238}
1239
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001240static int qc_wake(struct connection *conn)
1241{
1242 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02001243 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001244
1245 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001246
1247 /* Check if a soft-stop is in progress.
1248 * Release idling front connection if this is the case.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001249 *
1250 * TODO this is revelant for frontend connections only.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001251 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001252 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
1253 goto release;
1254
Willy Tarreau784b8682022-04-11 14:18:10 +02001255 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02001256 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
1257
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001258 qc_send(qcc);
1259
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001260 qc_wake_some_streams(qcc);
1261
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001262 if (qcc_is_dead(qcc))
1263 goto release;
1264
1265 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
1266
1267 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001268
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001269 release:
1270 qc_release(qcc);
1271 TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001272 return 1;
1273}
1274
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001275
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001276static void qmux_trace_frm(const struct quic_frame *frm)
1277{
1278 switch (frm->type) {
1279 case QUIC_FT_MAX_STREAMS_BIDI:
1280 chunk_appendf(&trace_buf, " max_streams=%lu",
1281 frm->max_streams_bidi.max_streams);
1282 break;
1283
1284 case QUIC_FT_MAX_STREAMS_UNI:
1285 chunk_appendf(&trace_buf, " max_streams=%lu",
1286 frm->max_streams_uni.max_streams);
1287 break;
1288
1289 default:
1290 break;
1291 }
1292}
1293
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001294/* quic-mux trace handler */
1295static void qmux_trace(enum trace_level level, uint64_t mask,
1296 const struct trace_source *src,
1297 const struct ist where, const struct ist func,
1298 const void *a1, const void *a2, const void *a3, const void *a4)
1299{
1300 const struct connection *conn = a1;
1301 const struct qcc *qcc = conn ? conn->ctx : NULL;
1302 const struct qcs *qcs = a2;
1303
1304 if (!qcc)
1305 return;
1306
1307 if (src->verbosity > QMUX_VERB_CLEAN) {
1308 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
1309
1310 if (qcs)
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001311 chunk_appendf(&trace_buf, " qcs=%p(%lu)", qcs, qcs->id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001312
1313 if (mask & QMUX_EV_QCC_NQCS) {
1314 const uint64_t *id = a3;
1315 chunk_appendf(&trace_buf, " id=%lu", *id);
1316 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001317
1318 if (mask & QMUX_EV_SEND_FRM)
1319 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001320
1321 if (mask & QMUX_EV_QCS_PUSH_FRM) {
1322 const struct qcs_push_frm_trace_arg *arg = a3;
1323 chunk_appendf(&trace_buf, " sent=%lu xfer=%d fin=%d offset=%lu",
1324 arg->sent, arg->xfer, arg->fin, arg->offset);
1325 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001326 }
1327}
1328
Amaury Denoyelle251eadf2022-03-24 17:14:52 +01001329/* Function to automatically activate QUIC MUX traces on stdout.
1330 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
1331 * Main use for now is in the docker image for QUIC interop testing.
1332 */
1333static void qmux_init_stdout_traces(void)
1334{
1335#ifdef ENABLE_QUIC_STDOUT_TRACES
1336 trace_qmux.sink = sink_find("stdout");
1337 trace_qmux.level = TRACE_LEVEL_DEVELOPER;
1338 trace_qmux.state = TRACE_STATE_RUNNING;
1339 trace_qmux.verbosity = QMUX_VERB_MINIMAL;
1340#endif
1341}
1342INITCALL0(STG_INIT, qmux_init_stdout_traces);
1343
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001344
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001345static const struct mux_ops qc_ops = {
1346 .init = qc_init,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001347 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001348 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001349 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001350 .subscribe = qc_subscribe,
1351 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001352 .wake = qc_wake,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02001353 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX|MX_FL_NO_UPG,
1354 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001355};
1356
1357static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001358 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001359
1360INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);