blob: 63a75e294f2aca55a1e2b650fca341995791c82f [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 Denoyelle0cc02a32022-04-19 17:21:11 +020011#include <haproxy/quic_stream.h>
Amaury Denoyelle251eadf2022-03-24 17:14:52 +010012#include <haproxy/sink.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020013#include <haproxy/ssl_sock-t.h>
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010014#include <haproxy/trace.h>
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +010015#include <haproxy/xprt_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010016
Amaury Denoyelledeed7772021-12-03 11:36:46 +010017DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010018DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
19
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010020/* trace source and events */
21static void qmux_trace(enum trace_level level, uint64_t mask,
22 const struct trace_source *src,
23 const struct ist where, const struct ist func,
24 const void *a1, const void *a2, const void *a3, const void *a4);
25
26static const struct trace_event qmux_trace_events[] = {
Amaury Denoyelle4f137572022-03-24 17:10:00 +010027#define QMUX_EV_QCC_RECV (1ULL << 1)
28 { .mask = QMUX_EV_QCC_RECV, .name = "qcc_recv", .desc = "Rx on QUIC connection" },
29#define QMUX_EV_QCC_SEND (1ULL << 2)
30 { .mask = QMUX_EV_QCC_SEND, .name = "qcc_send", .desc = "Tx on QUIC connection" },
31#define QMUX_EV_QCC_WAKE (1ULL << 3)
32 { .mask = QMUX_EV_QCC_WAKE, .name = "qcc_wake", .desc = "QUIC connection woken up" },
33#define QMUX_EV_QCC_END (1ULL << 4)
34 { .mask = QMUX_EV_QCC_END, .name = "qcc_end", .desc = "QUIC connection terminated" },
35#define QMUX_EV_QCC_NQCS (1ULL << 5)
36 { .mask = QMUX_EV_QCC_NQCS, .name = "qcc_no_qcs", .desc = "QUIC stream not found" },
37#define QMUX_EV_QCS_NEW (1ULL << 6)
38 { .mask = QMUX_EV_QCS_NEW, .name = "qcs_new", .desc = "new QUIC stream" },
39#define QMUX_EV_QCS_RECV (1ULL << 7)
40 { .mask = QMUX_EV_QCS_RECV, .name = "qcs_recv", .desc = "Rx on QUIC stream" },
41#define QMUX_EV_QCS_SEND (1ULL << 8)
42 { .mask = QMUX_EV_QCS_SEND, .name = "qcs_send", .desc = "Tx on QUIC stream" },
43#define QMUX_EV_QCS_END (1ULL << 9)
44 { .mask = QMUX_EV_QCS_END, .name = "qcs_end", .desc = "QUIC stream terminated" },
45#define QMUX_EV_STRM_RECV (1ULL << 10)
46 { .mask = QMUX_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
47#define QMUX_EV_STRM_SEND (1ULL << 11)
48 { .mask = QMUX_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
49#define QMUX_EV_STRM_END (1ULL << 12)
50 { .mask = QMUX_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
Amaury Denoyellefa29f332022-03-25 09:09:40 +010051#define QMUX_EV_SEND_FRM (1ULL << 13)
52 { .mask = QMUX_EV_SEND_FRM, .name = "send_frm", .desc = "sending QUIC frame" },
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020053/* special event dedicated to qcs_xfer_data */
54#define QMUX_EV_QCS_XFER_DATA (1ULL << 14)
55 { .mask = QMUX_EV_QCS_XFER_DATA, .name = "qcs_xfer_data", .desc = "qcs_xfer_data" },
56/* special event dedicated to qcs_build_stream_frm */
57#define QMUX_EV_QCS_BUILD_STRM (1ULL << 15)
58 { .mask = QMUX_EV_QCS_BUILD_STRM, .name = "qcs_build_stream_frm", .desc = "qcs_build_stream_frm" },
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010059 { }
60};
61
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020062/* custom arg for QMUX_EV_QCS_XFER_DATA */
63struct qcs_xfer_data_trace_arg {
64 size_t prep;
Amaury Denoyellefdcec362022-03-25 09:28:10 +010065 int xfer;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020066};
67/* custom arg for QMUX_EV_QCS_BUILD_STRM */
68struct qcs_build_stream_trace_arg {
69 size_t len;
Amaury Denoyellefdcec362022-03-25 09:28:10 +010070 char fin;
71 uint64_t offset;
72};
73
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010074static const struct name_desc qmux_trace_lockon_args[4] = {
75 /* arg1 */ { /* already used by the connection */ },
76 /* arg2 */ { .name="qcs", .desc="QUIC stream" },
77 /* arg3 */ { },
78 /* arg4 */ { }
79};
80
81static const struct name_desc qmux_trace_decoding[] = {
82#define QMUX_VERB_CLEAN 1
83 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
84#define QMUX_VERB_MINIMAL 2
85 { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" },
86 { /* end */ }
87};
88
89struct trace_source trace_qmux = {
90 .name = IST("qmux"),
91 .desc = "QUIC multiplexer",
92 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
93 .default_cb = qmux_trace,
94 .known_events = qmux_trace_events,
95 .lockon_args = qmux_trace_lockon_args,
96 .decoding = qmux_trace_decoding,
97 .report_events = ~0, /* report everything by default */
98};
99
100#define TRACE_SOURCE &trace_qmux
101INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
102
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100103/* Allocate a new QUIC streams with id <id> and type <type>. */
104struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100105{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100106 struct qcs *qcs;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200107 struct qc_stream_desc *stream;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100108
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100109 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
110
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100111 qcs = pool_alloc(pool_head_qcs);
112 if (!qcs)
113 goto out;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100114
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200115 /* allocate transport layer stream descriptor
116 *
117 * TODO qc_stream_desc is only useful for Tx buffering. It should not
118 * be required for unidirectional remote streams.
119 */
120 stream = qc_stream_desc_new(id, qcs, qcc->conn->handle.qc);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200121 if (!stream) {
122 pool_free(pool_head_qcs, qcs);
123 qcs = NULL;
124 goto out;
125 }
126
127 qcs->stream = stream;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100128 qcs->qcc = qcc;
129 qcs->cs = NULL;
130 qcs->flags = QC_SF_NONE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100131
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100132 qcs->endp = cs_endpoint_new();
133 if (!qcs->endp) {
134 pool_free(pool_head_qcs, qcs);
135 return NULL;
136 }
137 qcs->endp->target = qcs;
138 qcs->endp->ctx = qcc->conn;
139 qcs->endp->flags |= (CS_EP_T_MUX|CS_EP_ORPHAN|CS_EP_NOT_FIRST);
140
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200141 qcs->id = qcs->by_id.key = id;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200142 /* store transport layer stream descriptor in qcc tree */
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200143 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100144
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100145 qcc->strms[type].nb_streams++;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100146
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100147 /* If stream is local, use peer remote-limit, or else the opposite. */
148 /* TODO use uni limit for unidirectional streams */
149 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
150 qcc->rfctl.msd_bidi_l;
151
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100152 qcs->rx.buf = BUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100153 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100154 qcs->rx.offset = 0;
155 qcs->rx.frms = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100156
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100157 qcs->tx.buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100158 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100159 qcs->tx.sent_offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100160
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100161 qcs->wait_event.tasklet = NULL;
162 qcs->wait_event.events = 0;
163 qcs->subs = NULL;
164
165 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100166 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100167 return qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100168}
169
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200170/* Free a qcs. This function must only be done to remove a stream on allocation
171 * error or connection shutdown. Else use qcs_destroy which handle all the
172 * QUIC connection mechanism.
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100173 */
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200174void qcs_free(struct qcs *qcs)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100175{
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200176 b_free(&qcs->rx.buf);
177 b_free(&qcs->tx.buf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200178
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200179 BUG_ON(!qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams);
180 --qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams;
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200181
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200182 qc_stream_desc_release(qcs->stream);
183
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100184 BUG_ON(qcs->endp && !(qcs->endp->flags & CS_EP_ORPHAN));
185 cs_endpoint_free(qcs->endp);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200186
187 eb64_delete(&qcs->by_id);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100188 pool_free(pool_head_qcs, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100189}
190
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100191struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100192{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100193 struct buffer *buf = b_alloc(bptr);
194 BUG_ON(!buf);
195 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100196}
197
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100198int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
199{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100200 struct qcc *qcc = qcs->qcc;
201
202 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100203
204 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
205 BUG_ON(qcs->subs && qcs->subs != es);
206
207 es->events |= event_type;
208 qcs->subs = es;
209
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100210 if (event_type & SUB_RETRY_RECV)
211 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
212
213 if (event_type & SUB_RETRY_SEND)
214 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
215
216 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
217
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100218 return 0;
219}
220
221void qcs_notify_recv(struct qcs *qcs)
222{
223 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
224 tasklet_wakeup(qcs->subs->tasklet);
225 qcs->subs->events &= ~SUB_RETRY_RECV;
226 if (!qcs->subs->events)
227 qcs->subs = NULL;
228 }
229}
230
231void qcs_notify_send(struct qcs *qcs)
232{
233 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
234 tasklet_wakeup(qcs->subs->tasklet);
235 qcs->subs->events &= ~SUB_RETRY_SEND;
236 if (!qcs->subs->events)
237 qcs->subs = NULL;
238 }
239}
240
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100241/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
242 * several streams, depending on the already open ones.
243 * Return this node if succeeded, NULL if not.
244 */
Amaury Denoyelle50742292022-03-29 14:57:19 +0200245struct qcs *qcc_get_qcs(struct qcc *qcc, uint64_t id)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100246{
247 unsigned int strm_type;
248 int64_t sub_id;
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200249 struct eb64_node *node;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200250 struct qcs *qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100251
252 strm_type = id & QCS_ID_TYPE_MASK;
253 sub_id = id >> QCS_ID_TYPE_SHIFT;
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200254 node = NULL;
Amaury Denoyelle0dc40f02022-02-07 11:44:17 +0100255 if (quic_stream_is_local(qcc, id)) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100256 /* Local streams: this stream must be already opened. */
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200257 node = eb64_lookup(&qcc->streams_by_id, id);
258 if (!node) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100259 /* unknown stream id */
260 goto out;
261 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200262 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100263 }
264 else {
265 /* Remote streams. */
266 struct eb_root *strms;
267 uint64_t largest_id;
268 enum qcs_type qcs_type;
269
270 strms = &qcc->streams_by_id;
271 qcs_type = qcs_id_type(id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100272
273 /* TODO also checks max-streams for uni streams */
274 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100275 if (sub_id + 1 > qcc->lfctl.ms_bidi) {
Amaury Denoyellec055e302022-02-07 16:09:06 +0100276 /* streams limit reached */
277 goto out;
278 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100279 }
280
281 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
282 * correct value.
283 */
284 largest_id = qcc->strms[qcs_type].largest_id;
285 if (sub_id > (int64_t)largest_id) {
286 /* RFC: "A stream ID that is used out of order results in all streams
287 * of that type with lower-numbered stream IDs also being opened".
288 * So, let's "open" these streams.
289 */
290 int64_t i;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200291 struct qcs *tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100292
Amaury Denoyelle50742292022-03-29 14:57:19 +0200293 tmp_qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100294 for (i = largest_id + 1; i <= sub_id; i++) {
295 uint64_t id = (i << QCS_ID_TYPE_SHIFT) | strm_type;
296 enum qcs_type type = id & QCS_ID_DIR_BIT ? QCS_CLT_UNI : QCS_CLT_BIDI;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200297
Amaury Denoyelle50742292022-03-29 14:57:19 +0200298 tmp_qcs = qcs_new(qcc, id, type);
299 if (!tmp_qcs) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100300 /* allocation failure */
301 goto out;
302 }
303
304 qcc->strms[qcs_type].largest_id = i;
305 }
Amaury Denoyelle50742292022-03-29 14:57:19 +0200306 if (tmp_qcs)
307 qcs = tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100308 }
309 else {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200310 node = eb64_lookup(strms, id);
311 if (node)
312 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100313 }
314 }
315
Amaury Denoyelle50742292022-03-29 14:57:19 +0200316 return qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100317
318 out:
319 return NULL;
320}
321
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100322/* Handle a new STREAM frame <strm_frm>. The frame content will be copied in
323 * the buffer of the stream instance. The stream instance will be stored in
324 * <out_qcs>. In case of success, the caller can immediatly call qcc_decode_qcs
325 * to process the frame content.
326 *
327 * Returns 0 on success. On errors, two codes are present.
328 * - 1 is returned if the frame cannot be decoded and must be discarded.
329 * - 2 is returned if the stream cannot decode at the moment the frame. The
330 * frame should be buffered to be handled later.
331 */
332int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
333 char fin, char *data, struct qcs **out_qcs)
334{
335 struct qcs *qcs;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100336 size_t total, diff;
337
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100338 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
339
Amaury Denoyelle50742292022-03-29 14:57:19 +0200340 qcs = qcc_get_qcs(qcc, id);
341 if (!qcs) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100342 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 +0100343 return 1;
344 }
345
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100346 *out_qcs = qcs;
347
348 if (offset > qcs->rx.offset)
349 return 2;
350
351 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100352 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 +0100353 return 1;
354 }
355
356 /* Last frame already handled for this stream. */
357 BUG_ON(qcs->flags & QC_SF_FIN_RECV);
358
359 if (!qc_get_buf(qcs, &qcs->rx.buf)) {
360 /* TODO should mark qcs as full */
361 return 2;
362 }
363
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100364 TRACE_DEVEL("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100365 diff = qcs->rx.offset - offset;
366
367 /* TODO do not partially copy a frame if not enough size left. Maybe
368 * this can be optimized.
369 */
370 if (len > b_room(&qcs->rx.buf)) {
371 /* TODO handle STREAM frames larger than RX buffer. */
372 BUG_ON(len > b_size(&qcs->rx.buf));
373 return 2;
374 }
375
376 len -= diff;
377 data += diff;
378
379 total = b_putblk(&qcs->rx.buf, data, len);
380 /* TODO handle partial copy of a STREAM frame. */
381 BUG_ON(len != total);
382
383 qcs->rx.offset += total;
384
385 if (fin)
386 qcs->flags |= QC_SF_FIN_RECV;
387
388 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100389 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100390 return 0;
391}
392
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100393/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
394 * the frame.
395 *
396 * Returns 0 on success else non-zero.
397 */
398int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
399{
400 if (qcc->rfctl.md < max) {
401 qcc->rfctl.md = max;
402
403 if (qcc->flags & QC_CF_BLK_MFCTL) {
404 qcc->flags &= ~QC_CF_BLK_MFCTL;
405 tasklet_wakeup(qcc->wait_event.tasklet);
406 }
407 }
408 return 0;
409}
410
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100411/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
412 * field of the frame and <id> is the identifier of the QUIC stream.
413 *
414 * Returns 0 on success else non-zero.
415 */
416int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
417{
418 struct qcs *qcs;
419 struct eb64_node *node;
420
421 node = eb64_lookup(&qcc->streams_by_id, id);
422 if (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200423 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100424 if (max > qcs->tx.msd) {
425 qcs->tx.msd = max;
426
427 if (qcs->flags & QC_SF_BLK_SFCTL) {
428 qcs->flags &= ~QC_SF_BLK_SFCTL;
429 tasklet_wakeup(qcc->wait_event.tasklet);
430 }
431 }
432 }
433
434 return 0;
435}
436
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100437/* Decode the content of STREAM frames already received on the stream instance
438 * <qcs>.
439 *
440 * Returns 0 on success else non-zero.
441 */
442int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
443{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100444 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
445
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100446 if (qcc->app_ops->decode_qcs(qcs, qcs->flags & QC_SF_FIN_RECV, qcc->ctx) < 0) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100447 TRACE_DEVEL("leaving on decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100448 return 1;
449 }
450
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100451 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
452
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100453 return 0;
454}
455
Amaury Denoyellec055e302022-02-07 16:09:06 +0100456static int qc_is_max_streams_needed(struct qcc *qcc)
457{
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100458 return qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100459}
460
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500461/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100462static void qcs_destroy(struct qcs *qcs)
463{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100464 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200465 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100466
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100467 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100468
Amaury Denoyellec055e302022-02-07 16:09:06 +0100469 if (quic_stream_is_remote(qcs->qcc, id)) {
470 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100471 ++qcs->qcc->lfctl.cl_bidi_r;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100472 if (qc_is_max_streams_needed(qcs->qcc))
473 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
474 }
475 }
476
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200477 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100478
479 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100480}
481
482static inline int qcc_is_dead(const struct qcc *qcc)
483{
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200484 if (qcc->app_ops && qcc->app_ops->is_active &&
485 qcc->app_ops->is_active(qcc, qcc->ctx))
486 return 0;
487
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200488 if ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task)
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100489 return 1;
490
491 return 0;
492}
493
494/* Return true if the mux timeout should be armed. */
495static inline int qcc_may_expire(struct qcc *qcc)
496{
Amaury Denoyelle06890aa2022-04-04 16:15:06 +0200497 return !qcc->nb_cs;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100498}
499
500/* release function. This one should be called to free all resources allocated
501 * to the mux.
502 */
503static void qc_release(struct qcc *qcc)
504{
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200505 struct connection *conn = qcc->conn;
506 struct eb64_node *node;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100507
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100508 TRACE_ENTER(QMUX_EV_QCC_END);
509
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200510 if (qcc->app_ops && qcc->app_ops->release)
511 qcc->app_ops->release(qcc->ctx);
Amaury Denoyellef8909452022-03-30 11:51:56 +0200512
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200513 if (qcc->task) {
514 task_destroy(qcc->task);
515 qcc->task = NULL;
516 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100517
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200518 if (qcc->wait_event.tasklet)
519 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyellef8909452022-03-30 11:51:56 +0200520
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200521 /* liberate remaining qcs instances */
522 node = eb64_first(&qcc->streams_by_id);
523 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200524 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200525 node = eb64_next(node);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200526 qcs_free(qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100527 }
528
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200529 pool_free(pool_head_qcc, qcc);
530
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100531 if (conn) {
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100532 LIST_DEL_INIT(&conn->stopping_list);
533
Willy Tarreau784b8682022-04-11 14:18:10 +0200534 conn->handle.qc->conn = NULL;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100535 conn->mux = NULL;
536 conn->ctx = NULL;
537
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100538 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
539
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100540 conn_stop_tracking(conn);
541 conn_full_close(conn);
542 if (conn->destroy_cb)
543 conn->destroy_cb(conn);
544 conn_free(conn);
545 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100546
547 TRACE_LEAVE(QMUX_EV_QCC_END);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100548}
549
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100550/* Prepare a STREAM frame for <qcs> instance. First, transfer data from
551 * <payload> to <out> buffer. The STREAM frame payload points to the <out>
552 * buffer. The frame is then pushed to <frm_list>. If <fin> is set, and the
553 * <payload> buf is emptied after transfer, FIN bit is set on the STREAM frame.
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100554 * Transfer is automatically adjusted to not exceed the stream flow-control
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100555 * limit. <max_data> must contains the current sum offsets for the connection.
556 * This is useful to not exceed the connection flow-control limit when using
557 * repeatdly this function on multiple streams before passing the data to the
558 * lower layer.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100559 *
560 * Returns the total bytes of newly transferred data or a negative error code.
561 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200562static int qcs_xfer_data(struct qcs *qcs, struct buffer *out,
563 struct buffer *payload, uint64_t max_data)
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200564{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100565 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200566 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100567 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200568
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100569 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100570
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100571 qc_get_buf(qcs, out);
572
573 /*
574 * QCS out buffer diagram
575 * head left to_xfer
576 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100577 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100578 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100579 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100580 * ^ ack-off ^ sent-off ^ off
581 *
582 * STREAM frame
583 * ^ ^
584 * |xxxxxxxxxxxxxxxxx|
585 */
586
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200587 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100588 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
589
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100590 left = qcs->tx.offset - qcs->tx.sent_offset;
591 to_xfer = QUIC_MIN(b_data(payload), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100592
593 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
594 /* do not exceed flow control limit */
595 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
596 to_xfer = qcs->tx.msd - qcs->tx.offset;
597
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100598 BUG_ON_HOT(max_data > qcc->rfctl.md);
599 /* do not overcome flow control limit on connection */
600 if (max_data + to_xfer > qcc->rfctl.md)
601 to_xfer = qcc->rfctl.md - max_data;
602
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100603 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200604 goto out;
605
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200606 total = b_force_xfer(out, payload, to_xfer);
607
608 out:
609 {
610 struct qcs_xfer_data_trace_arg arg = {
611 .prep = b_data(out), .xfer = total,
612 };
613 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
614 qcc->conn, qcs, &arg);
615 }
616
617 return total;
618
619 err:
620 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
621 return -1;
622}
623
624static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
625 struct list *frm_list)
626{
627 struct qcc *qcc = qcs->qcc;
628 struct quic_frame *frm;
629 int head, total;
630
631 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
632
633 /* cf buffer schema in qcs_xfer_data */
634 head = qcs->tx.sent_offset - qcs->stream->ack_offset;
635 total = b_data(out) - head;
636 if (!total) {
637 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
638 return 0;
639 }
640
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200641 frm = pool_zalloc(pool_head_quic_frame);
642 if (!frm)
643 goto err;
644
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100645 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200646 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200647 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100648 frm->stream.buf = out;
649 frm->stream.data = (unsigned char *)b_peek(out, head);
650
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100651 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200652 if (fin)
653 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100654
655 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200656 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100657 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200658 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100659
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200660 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
661 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200662
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100663 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100664
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200665 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100666 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200667 struct qcs_build_stream_trace_arg arg = {
668 .len = frm->stream.len, .fin = fin,
669 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100670 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200671 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100672 qcc->conn, qcs, &arg);
673 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100674
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200675 return total;
676
677 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100678 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200679 return -1;
680}
681
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100682/* This function must be called by the upper layer to inform about the sending
683 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
684 * <offset>.
685 */
686void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
687{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100688 struct qcc *qcc = qcs->qcc;
689 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100690
691 BUG_ON(offset > qcs->tx.sent_offset);
692
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100693 /* check if the STREAM frame has already been notified. It can happen
694 * for retransmission.
695 */
696 if (offset + data <= qcs->tx.sent_offset)
697 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100698
699 diff = offset + data - qcs->tx.sent_offset;
700
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100701 /* increase offset sum on connection */
702 qcc->tx.sent_offsets += diff;
703 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
704 if (qcc->tx.sent_offsets == qcc->rfctl.md)
705 qcc->flags |= QC_CF_BLK_MFCTL;
706
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100707 /* increase offset on stream */
708 qcs->tx.sent_offset += diff;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100709 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
710 if (qcs->tx.sent_offset == qcs->tx.msd)
711 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100712}
713
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100714/* Wrapper for send on transport layer. Send a list of frames <frms> for the
715 * connection <qcc>.
716 *
717 * Returns 0 if all data sent with success else non-zero.
718 */
719static int qc_send_frames(struct qcc *qcc, struct list *frms)
720{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100721 /* TODO implement an opportunistic retry mechanism. This is needed
722 * because qc_send_app_pkts is not completed. It will only prepare data
723 * up to its Tx buffer. The frames left are not send even if the Tx
724 * buffer is emptied by the sendto call.
725 *
726 * To overcome this, we call repeatedly qc_send_app_pkts until we
727 * detect that the transport layer has send nothing. This could happen
728 * on congestion or sendto syscall error.
729 *
730 * When qc_send_app_pkts is improved to handle retry by itself, we can
731 * remove the looping from the MUX.
732 */
733 struct quic_frame *first_frm;
734 uint64_t first_offset = 0;
735 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100736
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100737 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
738
739 if (LIST_ISEMPTY(frms)) {
740 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100741 return 0;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100742 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100743
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100744 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100745 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
746 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
747 first_offset = first_frm->stream.offset.key;
748 first_stream_frame_type = 1;
749 }
750 else {
751 first_stream_frame_type = 0;
752 }
753
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100754 if (!LIST_ISEMPTY(frms))
Willy Tarreau784b8682022-04-11 14:18:10 +0200755 qc_send_app_pkts(qcc->conn->handle.qc, frms);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100756
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100757 /* If there is frames left, check if the transport layer has send some
758 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100759 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100760 if (!LIST_ISEMPTY(frms)) {
761 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
762 goto retry_send;
763
764 /* If the first frame is STREAM, check if its offset has
765 * changed.
766 */
767 if (first_stream_frame_type &&
768 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
769 goto retry_send;
770 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100771 }
772
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100773 /* If there is frames left at this stage, transport layer is blocked.
774 * Subscribe on it to retry later.
775 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100776 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100777 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100778 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
779 SUB_RETRY_SEND, &qcc->wait_event);
780 return 1;
781 }
782
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100783 TRACE_LEAVE(QMUX_EV_QCC_SEND);
784
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100785 return 0;
786}
787
Amaury Denoyellec9337802022-04-04 16:36:34 +0200788/* Send a MAX_STREAM_BIDI frame to update the limit of bidirectional streams
789 * allowed to be opened by the peer. The caller should have first checked if
790 * this is required with qc_is_max_streams_needed.
791 *
792 * Returns 0 on success else non-zero.
793 */
794static int qc_send_max_streams(struct qcc *qcc)
795{
796 struct list frms = LIST_HEAD_INIT(frms);
797 struct quic_frame *frm;
798
799 frm = pool_zalloc(pool_head_quic_frame);
800 BUG_ON(!frm); /* TODO handle this properly */
801
802 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
803 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
804 qcc->lfctl.cl_bidi_r;
805 TRACE_DEVEL("sending MAX_STREAMS frame", QMUX_EV_SEND_FRM, qcc->conn, NULL, frm);
806 LIST_APPEND(&frms, &frm->list);
807
808 if (qc_send_frames(qcc, &frms))
809 return 1;
810
811 /* save the new limit if the frame has been send. */
812 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
813 qcc->lfctl.cl_bidi_r = 0;
814
815 return 0;
816}
817
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100818/* Proceed to sending. Loop through all available streams for the <qcc>
819 * instance and try to send as much as possible.
820 *
821 * Returns the total of bytes sent to the transport layer.
822 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100823static int qc_send(struct qcc *qcc)
824{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100825 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200826 struct eb64_node *node;
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100827 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200828
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100829 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200830
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200831 if (qcc->conn->flags & CO_FL_SOCK_WR_SH) {
832 qcc->conn->flags |= CO_FL_ERROR;
833 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
834 return 0;
835 }
836
Amaury Denoyellec9337802022-04-04 16:36:34 +0200837 if (qc_is_max_streams_needed(qcc))
838 qc_send_max_streams(qcc);
839
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100840 if (qcc->flags & QC_CF_BLK_MFCTL)
841 return 0;
842
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100843 /* loop through all streams, construct STREAM frames if data available.
844 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200845 */
846 node = eb64_first(&qcc->streams_by_id);
847 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200848 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200849 struct buffer *buf = &qcs->tx.buf;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200850 struct buffer *out = &qcs->stream->buf;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100851
Amaury Denoyellee2ec9422022-03-10 16:46:18 +0100852 /* TODO
853 * for the moment, unidirectional streams have their own
854 * mechanism for sending. This should be unified in the future,
855 * in this case the next check will be removed.
856 */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200857 if (quic_stream_is_uni(qcs->id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +0100858 node = eb64_next(node);
859 continue;
860 }
861
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100862 if (qcs->flags & QC_SF_BLK_SFCTL) {
863 node = eb64_next(node);
864 continue;
865 }
866
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200867 /* Prepare <out> buffer with data from <buf>. */
868 if (b_data(buf)) {
869 int ret = qcs_xfer_data(qcs, out, buf,
870 qcc->tx.sent_offsets + total);
Amaury Denoyelle14551132022-03-04 16:51:20 +0100871 BUG_ON(ret < 0); /* TODO handle this properly */
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200872
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100873 if (ret > 0) {
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100874 qcs_notify_send(qcs);
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100875 if (qcs->flags & QC_SF_BLK_MROOM)
876 qcs->flags &= ~QC_SF_BLK_MROOM;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100877 }
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200878
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200879 qcs->tx.offset += ret;
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100880 total += ret;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200881 }
Amaury Denoyellea2c58a72021-12-03 14:38:31 +0100882
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200883 /* Subscribe if not all data can be transfered. */
884 if (b_data(buf)) {
885 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
886 SUB_RETRY_SEND, &qcc->wait_event);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200887 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200888
889 /* Build a new STREAM frame with <out> buffer. */
890 if (b_data(out)) {
891 int ret;
892 char fin = !!(qcs->flags & QC_SF_FIN_STREAM);
893
894 /* FIN is set if all incoming data were transfered. */
895 fin = !!(fin && !b_data(buf));
896 ret = qcs_build_stream_frm(qcs, out, fin, &frms);
897 BUG_ON(ret < 0); /* TODO handle this properly */
898 }
899
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200900 node = eb64_next(node);
901 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200902
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100903 qc_send_frames(qcc, &frms);
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100904
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100905 TRACE_LEAVE(QMUX_EV_QCC_SEND);
906
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100907 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100908}
909
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +0100910/* Release all streams that are already marked as detached. This is only done
911 * if their TX buffers are empty or if a CONNECTION_CLOSE has been received.
912 *
913 * Return the number of released stream.
914 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100915static int qc_release_detached_streams(struct qcc *qcc)
916{
917 struct eb64_node *node;
918 int release = 0;
919
920 node = eb64_first(&qcc->streams_by_id);
921 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200922 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100923 node = eb64_next(node);
924
925 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200926 if (!b_data(&qcs->tx.buf) &&
927 qcs->tx.offset == qcs->tx.sent_offset) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100928 qcs_destroy(qcs);
929 release = 1;
930 }
931 else {
932 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
933 SUB_RETRY_SEND, &qcc->wait_event);
934 }
935 }
936 }
937
938 return release;
939}
940
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100941static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
942{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +0200943 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100944
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100945 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100946
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100947 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100948
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100949 if (qc_release_detached_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +0200950 if (qcc_is_dead(qcc)) {
951 qc_release(qcc);
952 }
953 else {
954 if (qcc_may_expire(qcc))
955 qcc->task->expire = tick_add(now_ms, qcc->timeout);
956 else
957 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +0100958 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100959 }
960 }
961
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100962 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
963
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100964 return NULL;
965}
966
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100967static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
968{
969 struct qcc *qcc = ctx;
970 int expired = tick_is_expired(t->expire, now_ms);
971
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100972 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100973
974 if (qcc) {
975 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100976 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100977 return t;
978 }
979
980 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100981 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100982 t->expire = TICK_ETERNITY;
983 return t;
984 }
985 }
986
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100987 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +0100988
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100989 if (!qcc) {
990 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +0100991 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100992 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +0100993
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100994 qcc->task = NULL;
995
996 if (qcc_is_dead(qcc))
997 qc_release(qcc);
998
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100999 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1000
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001001 return NULL;
1002}
1003
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001004static int qc_init(struct connection *conn, struct proxy *prx,
1005 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001006{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001007 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001008 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001009
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001010 qcc = pool_alloc(pool_head_qcc);
1011 if (!qcc)
1012 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001013
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001014 qcc->conn = conn;
1015 conn->ctx = qcc;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001016 qcc->nb_cs = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001017 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001018
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001019 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001020
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001021 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001022
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001023 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001024 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001025
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001026 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001027 qcc->tx.sent_offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001028
1029 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001030 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001031 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
1032 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001033 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001034 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001035
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001036 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001037 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
1038 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001039 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001040 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001041
1042 /* Server initiated streams must respect the server flow control. */
1043 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001044 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
1045 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001046 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001047 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1048
1049 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001050 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
1051 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001052 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001053 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001054
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001055 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
1056 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001057
Willy Tarreau784b8682022-04-11 14:18:10 +02001058 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001059 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001060 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1061 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1062
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001063 qcc->wait_event.tasklet = tasklet_new();
1064 if (!qcc->wait_event.tasklet)
1065 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001066
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001067 qcc->subs = NULL;
1068 qcc->wait_event.tasklet->process = qc_io_cb;
1069 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001070 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001071
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001072 /* haproxy timeouts */
1073 qcc->timeout = prx->timeout.client;
1074 qcc->task = task_new_here();
1075 if (!qcc->task)
1076 goto fail_no_timeout_task;
1077 qcc->task->process = qc_timeout_task;
1078 qcc->task->context = qcc;
1079 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1080
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001081 if (!conn_is_back(conn)) {
1082 if (!LIST_INLIST(&conn->stopping_list)) {
1083 LIST_APPEND(&mux_stopping_data[tid].list,
1084 &conn->stopping_list);
1085 }
1086 }
1087
Willy Tarreau784b8682022-04-11 14:18:10 +02001088 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001089 /* init read cycle */
1090 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001091
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001092 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001093
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001094 fail_no_timeout_task:
1095 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001096 fail_no_tasklet:
1097 pool_free(pool_head_qcc, qcc);
1098 fail_no_qcc:
1099 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001100}
1101
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001102static void qc_destroy(void *ctx)
1103{
1104 struct qcc *qcc = ctx;
1105
1106 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
1107 qc_release(qcc);
1108 TRACE_LEAVE(QMUX_EV_QCC_END);
1109}
1110
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001111static void qc_detach(struct conn_stream *cs)
1112{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001113 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001114 struct qcc *qcc = qcs->qcc;
1115
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001116 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001117
Amaury Denoyelle80388212022-04-08 12:00:12 +02001118 qcs->cs = NULL;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001119 --qcc->nb_cs;
1120
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001121 if ((b_data(&qcs->tx.buf) || qcs->tx.offset > qcs->tx.sent_offset) &&
1122 !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001123 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001124 qcs->flags |= QC_SF_DETACH;
1125 return;
1126 }
1127
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001128 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001129
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001130 if (qcc_is_dead(qcc)) {
1131 qc_release(qcc);
1132 }
1133 else {
1134 if (qcc_may_expire(qcc))
1135 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1136 else
1137 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001138 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001139 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001140
1141 TRACE_LEAVE(QMUX_EV_STRM_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001142}
1143
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001144/* Called from the upper layer, to receive data */
1145static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf,
1146 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001147{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001148 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001149 struct htx *qcs_htx = NULL;
1150 struct htx *cs_htx = NULL;
1151 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001152 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001153
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001154 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001155
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001156 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
1157 if (htx_is_empty(qcs_htx)) {
1158 /* Set buffer data to 0 as HTX is empty. */
1159 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1160 goto end;
1161 }
1162
1163 ret = qcs_htx->data;
1164
1165 cs_htx = htx_from_buf(buf);
1166 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
1167 htx_to_buf(cs_htx, buf);
1168 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1169 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
1170 goto end;
1171 }
1172
1173 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
1174 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
1175
1176 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001177 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
1178 cs_htx->flags |= HTX_FL_EOM;
1179 fin = 1;
1180 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001181
1182 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
1183 htx_to_buf(cs_htx, buf);
1184 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1185 ret -= qcs_htx->data;
1186
1187 end:
1188 if (b_data(&qcs->rx.app_buf)) {
Christopher Fauletb041b232022-03-24 10:27:02 +01001189 cs->endp->flags |= (CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001190 }
1191 else {
Christopher Fauletb041b232022-03-24 10:27:02 +01001192 cs->endp->flags &= ~(CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
1193 if (cs->endp->flags & CS_EP_ERR_PENDING)
1194 cs->endp->flags |= CS_EP_ERROR;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001195
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001196 if (fin)
Christopher Fauletb041b232022-03-24 10:27:02 +01001197 cs->endp->flags |= (CS_EP_EOI|CS_EP_EOS);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001198
1199 if (b_size(&qcs->rx.app_buf)) {
1200 b_free(&qcs->rx.app_buf);
1201 offer_buffers(NULL, 1);
1202 }
1203 }
1204
1205 if (ret)
1206 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
1207
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001208 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
1209
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001210 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001211}
1212
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001213static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf,
1214 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001215{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001216 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001217 size_t ret;
1218
1219 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001220
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001221 ret = qcs->qcc->app_ops->snd_buf(cs, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001222
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001223 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
1224
1225 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001226}
1227
1228/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1229 * event subscriber <es> is not allowed to change from a previous call as long
1230 * as at least one event is still subscribed. The <event_type> must only be a
1231 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1232 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001233static int qc_subscribe(struct conn_stream *cs, int event_type,
1234 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001235{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001236 return qcs_subscribe(__cs_mux(cs), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001237}
1238
1239/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1240 * The <es> pointer is not allowed to differ from the one passed to the
1241 * subscribe() call. It always returns zero.
1242 */
1243static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
1244{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001245 struct qcs *qcs = __cs_mux(cs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001246
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001247 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1248 BUG_ON(qcs->subs && qcs->subs != es);
1249
1250 es->events &= ~event_type;
1251 if (!es->events)
1252 qcs->subs = NULL;
1253
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001254 return 0;
1255}
1256
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001257/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Christopher Fauletb041b232022-03-24 10:27:02 +01001258 * report CS_EP_ERR_PENDING|CS_EP_ERROR on the attached conn-streams and wake
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001259 * them.
1260 */
1261static int qc_wake_some_streams(struct qcc *qcc)
1262{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001263 struct qcs *qcs;
1264 struct eb64_node *node;
1265
1266 for (node = eb64_first(&qcc->streams_by_id); node;
1267 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001268 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001269
1270 if (!qcs->cs)
1271 continue;
1272
1273 if (qcc->conn->flags & CO_FL_ERROR) {
Christopher Fauletb041b232022-03-24 10:27:02 +01001274 qcs->endp->flags |= CS_EP_ERR_PENDING;
1275 if (qcs->endp->flags & CS_EP_EOS)
1276 qcs->endp->flags |= CS_EP_ERROR;
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001277
1278 if (qcs->subs) {
1279 qcs_notify_recv(qcs);
1280 qcs_notify_send(qcs);
1281 }
1282 else if (qcs->cs->data_cb->wake) {
1283 qcs->cs->data_cb->wake(qcs->cs);
1284 }
1285 }
1286 }
1287
1288 return 0;
1289}
1290
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001291static int qc_wake(struct connection *conn)
1292{
1293 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02001294 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001295
1296 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001297
1298 /* Check if a soft-stop is in progress.
1299 * Release idling front connection if this is the case.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001300 *
1301 * TODO this is revelant for frontend connections only.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001302 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001303 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
1304 goto release;
1305
Willy Tarreau784b8682022-04-11 14:18:10 +02001306 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02001307 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
1308
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001309 qc_send(qcc);
1310
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001311 qc_wake_some_streams(qcc);
1312
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001313 if (qcc_is_dead(qcc))
1314 goto release;
1315
1316 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
1317
1318 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001319
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001320 release:
1321 qc_release(qcc);
1322 TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001323 return 1;
1324}
1325
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001326
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001327static void qmux_trace_frm(const struct quic_frame *frm)
1328{
1329 switch (frm->type) {
1330 case QUIC_FT_MAX_STREAMS_BIDI:
1331 chunk_appendf(&trace_buf, " max_streams=%lu",
1332 frm->max_streams_bidi.max_streams);
1333 break;
1334
1335 case QUIC_FT_MAX_STREAMS_UNI:
1336 chunk_appendf(&trace_buf, " max_streams=%lu",
1337 frm->max_streams_uni.max_streams);
1338 break;
1339
1340 default:
1341 break;
1342 }
1343}
1344
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001345/* quic-mux trace handler */
1346static void qmux_trace(enum trace_level level, uint64_t mask,
1347 const struct trace_source *src,
1348 const struct ist where, const struct ist func,
1349 const void *a1, const void *a2, const void *a3, const void *a4)
1350{
1351 const struct connection *conn = a1;
1352 const struct qcc *qcc = conn ? conn->ctx : NULL;
1353 const struct qcs *qcs = a2;
1354
1355 if (!qcc)
1356 return;
1357
1358 if (src->verbosity > QMUX_VERB_CLEAN) {
1359 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
1360
1361 if (qcs)
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001362 chunk_appendf(&trace_buf, " qcs=%p(%lu)", qcs, qcs->id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001363
1364 if (mask & QMUX_EV_QCC_NQCS) {
1365 const uint64_t *id = a3;
1366 chunk_appendf(&trace_buf, " id=%lu", *id);
1367 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001368
1369 if (mask & QMUX_EV_SEND_FRM)
1370 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001371
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001372 if (mask & QMUX_EV_QCS_XFER_DATA) {
1373 const struct qcs_xfer_data_trace_arg *arg = a3;
1374 chunk_appendf(&trace_buf, " prep=%lu xfer=%d",
1375 arg->prep, arg->xfer);
1376 }
1377
1378 if (mask & QMUX_EV_QCS_BUILD_STRM) {
1379 const struct qcs_build_stream_trace_arg *arg = a3;
1380 chunk_appendf(&trace_buf, " len=%lu fin=%d offset=%lu",
1381 arg->len, arg->fin, arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001382 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001383 }
1384}
1385
Amaury Denoyelle251eadf2022-03-24 17:14:52 +01001386/* Function to automatically activate QUIC MUX traces on stdout.
1387 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
1388 * Main use for now is in the docker image for QUIC interop testing.
1389 */
1390static void qmux_init_stdout_traces(void)
1391{
1392#ifdef ENABLE_QUIC_STDOUT_TRACES
1393 trace_qmux.sink = sink_find("stdout");
1394 trace_qmux.level = TRACE_LEVEL_DEVELOPER;
1395 trace_qmux.state = TRACE_STATE_RUNNING;
1396 trace_qmux.verbosity = QMUX_VERB_MINIMAL;
1397#endif
1398}
1399INITCALL0(STG_INIT, qmux_init_stdout_traces);
1400
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001401
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001402static const struct mux_ops qc_ops = {
1403 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001404 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001405 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001406 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001407 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001408 .subscribe = qc_subscribe,
1409 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001410 .wake = qc_wake,
Christopher Fauleta97cced2022-04-12 18:04:10 +02001411 .flags = MX_FL_HTX|MX_FL_NO_UPG,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02001412 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001413};
1414
1415static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001416 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001417
1418INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);