blob: 9449b86feee5cd08dcb289945a350a20ee5c64e5 [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>
Amaury Denoyelledeed7772021-12-03 11:36:46 +01007#include <haproxy/dynbuf.h>
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01008#include <haproxy/htx.h>
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02009#include <haproxy/list.h>
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +020010#include <haproxy/ncbuf.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +010011#include <haproxy/pool.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020012#include <haproxy/quic_stream.h>
Frédéric Lécaille748ece62022-05-21 23:58:40 +020013#include <haproxy/quic_tp-t.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020014#include <haproxy/ssl_sock-t.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020015#include <haproxy/stconn.h>
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010016#include <haproxy/trace.h>
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +010017#include <haproxy/xprt_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010018
Amaury Denoyelledeed7772021-12-03 11:36:46 +010019DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010020DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
21
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010022/* trace source and events */
23static void qmux_trace(enum trace_level level, uint64_t mask,
24 const struct trace_source *src,
25 const struct ist where, const struct ist func,
26 const void *a1, const void *a2, const void *a3, const void *a4);
27
28static const struct trace_event qmux_trace_events[] = {
Amaury Denoyelle4f137572022-03-24 17:10:00 +010029#define QMUX_EV_QCC_RECV (1ULL << 1)
30 { .mask = QMUX_EV_QCC_RECV, .name = "qcc_recv", .desc = "Rx on QUIC connection" },
31#define QMUX_EV_QCC_SEND (1ULL << 2)
32 { .mask = QMUX_EV_QCC_SEND, .name = "qcc_send", .desc = "Tx on QUIC connection" },
33#define QMUX_EV_QCC_WAKE (1ULL << 3)
34 { .mask = QMUX_EV_QCC_WAKE, .name = "qcc_wake", .desc = "QUIC connection woken up" },
35#define QMUX_EV_QCC_END (1ULL << 4)
36 { .mask = QMUX_EV_QCC_END, .name = "qcc_end", .desc = "QUIC connection terminated" },
37#define QMUX_EV_QCC_NQCS (1ULL << 5)
38 { .mask = QMUX_EV_QCC_NQCS, .name = "qcc_no_qcs", .desc = "QUIC stream not found" },
39#define QMUX_EV_QCS_NEW (1ULL << 6)
40 { .mask = QMUX_EV_QCS_NEW, .name = "qcs_new", .desc = "new QUIC stream" },
41#define QMUX_EV_QCS_RECV (1ULL << 7)
42 { .mask = QMUX_EV_QCS_RECV, .name = "qcs_recv", .desc = "Rx on QUIC stream" },
43#define QMUX_EV_QCS_SEND (1ULL << 8)
44 { .mask = QMUX_EV_QCS_SEND, .name = "qcs_send", .desc = "Tx on QUIC stream" },
45#define QMUX_EV_QCS_END (1ULL << 9)
46 { .mask = QMUX_EV_QCS_END, .name = "qcs_end", .desc = "QUIC stream terminated" },
47#define QMUX_EV_STRM_RECV (1ULL << 10)
48 { .mask = QMUX_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
49#define QMUX_EV_STRM_SEND (1ULL << 11)
50 { .mask = QMUX_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
51#define QMUX_EV_STRM_END (1ULL << 12)
52 { .mask = QMUX_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
Amaury Denoyellefa29f332022-03-25 09:09:40 +010053#define QMUX_EV_SEND_FRM (1ULL << 13)
54 { .mask = QMUX_EV_SEND_FRM, .name = "send_frm", .desc = "sending QUIC frame" },
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020055/* special event dedicated to qcs_xfer_data */
56#define QMUX_EV_QCS_XFER_DATA (1ULL << 14)
57 { .mask = QMUX_EV_QCS_XFER_DATA, .name = "qcs_xfer_data", .desc = "qcs_xfer_data" },
58/* special event dedicated to qcs_build_stream_frm */
59#define QMUX_EV_QCS_BUILD_STRM (1ULL << 15)
60 { .mask = QMUX_EV_QCS_BUILD_STRM, .name = "qcs_build_stream_frm", .desc = "qcs_build_stream_frm" },
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010061 { }
62};
63
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020064/* custom arg for QMUX_EV_QCS_XFER_DATA */
65struct qcs_xfer_data_trace_arg {
66 size_t prep;
Amaury Denoyellefdcec362022-03-25 09:28:10 +010067 int xfer;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020068};
69/* custom arg for QMUX_EV_QCS_BUILD_STRM */
70struct qcs_build_stream_trace_arg {
71 size_t len;
Amaury Denoyellefdcec362022-03-25 09:28:10 +010072 char fin;
73 uint64_t offset;
74};
75
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010076static const struct name_desc qmux_trace_lockon_args[4] = {
77 /* arg1 */ { /* already used by the connection */ },
78 /* arg2 */ { .name="qcs", .desc="QUIC stream" },
79 /* arg3 */ { },
80 /* arg4 */ { }
81};
82
83static const struct name_desc qmux_trace_decoding[] = {
84#define QMUX_VERB_CLEAN 1
85 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
86#define QMUX_VERB_MINIMAL 2
87 { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" },
88 { /* end */ }
89};
90
91struct trace_source trace_qmux = {
92 .name = IST("qmux"),
93 .desc = "QUIC multiplexer",
94 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
95 .default_cb = qmux_trace,
96 .known_events = qmux_trace_events,
97 .lockon_args = qmux_trace_lockon_args,
98 .decoding = qmux_trace_decoding,
99 .report_events = ~0, /* report everything by default */
100};
101
102#define TRACE_SOURCE &trace_qmux
103INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
104
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200105/* Emit a CONNECTION_CLOSE with error <err>. This will interrupt all future
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200106 * send/receive operations.
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200107 */
108static void qcc_emit_cc(struct qcc *qcc, int err)
109{
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200110 quic_set_connection_close(qcc->conn->handle.qc, err, 0);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200111 qcc->flags |= QC_CF_CC_EMIT;
112 tasklet_wakeup(qcc->wait_event.tasklet);
113}
114
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100115/* Allocate a new QUIC streams with id <id> and type <type>. */
116struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100117{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100118 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100119
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100120 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
121
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100122 qcs = pool_alloc(pool_head_qcs);
123 if (!qcs)
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200124 return NULL;
125
126 qcs->stream = NULL;
127 qcs->qcc = qcc;
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +0200128 qcs->sd = NULL;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200129 qcs->flags = QC_SF_NONE;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200130 qcs->ctx = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100131
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200132 /* Allocate transport layer stream descriptor. Only needed for TX. */
133 if (!quic_stream_is_uni(id) || !quic_stream_is_remote(qcc, id)) {
134 struct quic_conn *qc = qcc->conn->handle.qc;
135 qcs->stream = qc_stream_desc_new(id, type, qcs, qc);
136 if (!qcs->stream)
137 goto err;
138 }
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200139
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +0200140 qcs->id = qcs->by_id.key = id;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200141 if (qcc->app_ops->attach) {
Amaury Denoyellec0156792022-06-03 15:29:07 +0200142 if (qcc->app_ops->attach(qcs, qcc->ctx))
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200143 goto err;
144 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100145
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200146 /* store transport layer stream descriptor in qcc tree */
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200147 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100148
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100149 qcc->strms[type].nb_streams++;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100150
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100151 /* If stream is local, use peer remote-limit, or else the opposite. */
152 /* TODO use uni limit for unidirectional streams */
153 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
154 qcc->rfctl.msd_bidi_l;
155
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200156 qcs->rx.ncbuf = NCBUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100157 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200158 qcs->rx.offset = qcs->rx.offset_max = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100159
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200160 /* TODO use uni limit for unidirectional streams */
161 qcs->rx.msd = quic_stream_is_local(qcc, id) ? qcc->lfctl.msd_bidi_l :
162 qcc->lfctl.msd_bidi_r;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200163 qcs->rx.msd_init = qcs->rx.msd;
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200164
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100165 qcs->tx.buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100166 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100167 qcs->tx.sent_offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100168
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100169 qcs->wait_event.tasklet = NULL;
170 qcs->wait_event.events = 0;
171 qcs->subs = NULL;
172
173 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100174 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100175 return qcs;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200176
177 err:
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200178 if (qcs->ctx && qcc->app_ops->detach)
179 qcc->app_ops->detach(qcs);
180
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200181 qc_stream_desc_release(qcs->stream);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200182 pool_free(pool_head_qcs, qcs);
183 return NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100184}
185
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200186static void qc_free_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
187{
188 struct buffer buf;
189
Amaury Denoyelle7313f5e2022-05-17 18:53:21 +0200190 if (ncb_is_null(ncbuf))
191 return;
192
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200193 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
194 b_free(&buf);
Amaury Denoyelle7313f5e2022-05-17 18:53:21 +0200195 offer_buffers(NULL, 1);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200196
197 *ncbuf = NCBUF_NULL;
198}
199
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200200/* Free a qcs. This function must only be done to remove a stream on allocation
201 * error or connection shutdown. Else use qcs_destroy which handle all the
202 * QUIC connection mechanism.
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100203 */
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200204void qcs_free(struct qcs *qcs)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100205{
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200206 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200207 b_free(&qcs->tx.buf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200208
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200209 BUG_ON(!qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams);
210 --qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams;
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200211
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200212 if (qcs->ctx && qcs->qcc->app_ops->detach)
213 qcs->qcc->app_ops->detach(qcs);
214
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200215 qc_stream_desc_release(qcs->stream);
216
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +0200217 BUG_ON(qcs->sd && !se_fl_test(qcs->sd, SE_FL_ORPHAN));
218 sedesc_free(qcs->sd);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200219
220 eb64_delete(&qcs->by_id);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100221 pool_free(pool_head_qcs, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100222}
223
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100224struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100225{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100226 struct buffer *buf = b_alloc(bptr);
227 BUG_ON(!buf);
228 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100229}
230
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200231struct ncbuf *qc_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
232{
233 struct buffer buf = BUF_NULL;
234
235 if (ncb_is_null(ncbuf)) {
236 b_alloc(&buf);
237 BUG_ON(b_is_null(&buf));
238
239 *ncbuf = ncb_make(buf.area, buf.size, 0);
240 ncb_init(ncbuf, 0);
241 }
242
243 return ncbuf;
244}
245
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100246int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
247{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100248 struct qcc *qcc = qcs->qcc;
249
250 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100251
252 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
253 BUG_ON(qcs->subs && qcs->subs != es);
254
255 es->events |= event_type;
256 qcs->subs = es;
257
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100258 if (event_type & SUB_RETRY_RECV)
259 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
260
261 if (event_type & SUB_RETRY_SEND)
262 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
263
264 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
265
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100266 return 0;
267}
268
269void qcs_notify_recv(struct qcs *qcs)
270{
271 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
272 tasklet_wakeup(qcs->subs->tasklet);
273 qcs->subs->events &= ~SUB_RETRY_RECV;
274 if (!qcs->subs->events)
275 qcs->subs = NULL;
276 }
277}
278
279void qcs_notify_send(struct qcs *qcs)
280{
281 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
282 tasklet_wakeup(qcs->subs->tasklet);
283 qcs->subs->events &= ~SUB_RETRY_SEND;
284 if (!qcs->subs->events)
285 qcs->subs = NULL;
286 }
287}
288
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100289/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
290 * several streams, depending on the already open ones.
291 * Return this node if succeeded, NULL if not.
292 */
Amaury Denoyelle50742292022-03-29 14:57:19 +0200293struct qcs *qcc_get_qcs(struct qcc *qcc, uint64_t id)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100294{
295 unsigned int strm_type;
296 int64_t sub_id;
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200297 struct eb64_node *node;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200298 struct qcs *qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100299
300 strm_type = id & QCS_ID_TYPE_MASK;
301 sub_id = id >> QCS_ID_TYPE_SHIFT;
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200302 node = NULL;
Amaury Denoyelle0dc40f02022-02-07 11:44:17 +0100303 if (quic_stream_is_local(qcc, id)) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100304 /* Local streams: this stream must be already opened. */
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200305 node = eb64_lookup(&qcc->streams_by_id, id);
306 if (!node) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100307 /* unknown stream id */
308 goto out;
309 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200310 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100311 }
312 else {
313 /* Remote streams. */
314 struct eb_root *strms;
315 uint64_t largest_id;
316 enum qcs_type qcs_type;
317
318 strms = &qcc->streams_by_id;
319 qcs_type = qcs_id_type(id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100320
321 /* TODO also checks max-streams for uni streams */
322 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100323 if (sub_id + 1 > qcc->lfctl.ms_bidi) {
Amaury Denoyelle209404b2022-05-20 16:45:32 +0200324 /* RFC 9000 4.6. Controlling Concurrency
325 *
326 * An endpoint that receives a frame with a
327 * stream ID exceeding the limit it has sent
328 * MUST treat this as a connection error of
329 * type STREAM_LIMIT_ERROR
330 */
331 qcc_emit_cc(qcc, QC_ERR_STREAM_LIMIT_ERROR);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100332 goto out;
333 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100334 }
335
336 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
337 * correct value.
338 */
339 largest_id = qcc->strms[qcs_type].largest_id;
340 if (sub_id > (int64_t)largest_id) {
341 /* RFC: "A stream ID that is used out of order results in all streams
342 * of that type with lower-numbered stream IDs also being opened".
343 * So, let's "open" these streams.
344 */
345 int64_t i;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200346 struct qcs *tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100347
Amaury Denoyelle50742292022-03-29 14:57:19 +0200348 tmp_qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100349 for (i = largest_id + 1; i <= sub_id; i++) {
350 uint64_t id = (i << QCS_ID_TYPE_SHIFT) | strm_type;
351 enum qcs_type type = id & QCS_ID_DIR_BIT ? QCS_CLT_UNI : QCS_CLT_BIDI;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200352
Amaury Denoyelle50742292022-03-29 14:57:19 +0200353 tmp_qcs = qcs_new(qcc, id, type);
354 if (!tmp_qcs) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100355 /* allocation failure */
356 goto out;
357 }
358
359 qcc->strms[qcs_type].largest_id = i;
360 }
Amaury Denoyelle50742292022-03-29 14:57:19 +0200361 if (tmp_qcs)
362 qcs = tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100363 }
364 else {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200365 node = eb64_lookup(strms, id);
366 if (node)
367 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100368 }
369 }
370
Amaury Denoyelle50742292022-03-29 14:57:19 +0200371 return qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100372
373 out:
374 return NULL;
375}
376
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200377/* Simple function to duplicate a buffer */
378static inline struct buffer qcs_b_dup(const struct ncbuf *b)
379{
380 return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0));
381}
382
383/* Remove <bytes> from <qcs> Rx buffer. This must be called by transcoders
384 * after STREAM parsing. Flow-control for received offsets may be allocated for
385 * the peer if needed.
386 */
387static void qcs_consume(struct qcs *qcs, uint64_t bytes)
388{
389 struct qcc *qcc = qcs->qcc;
390 struct quic_frame *frm;
391 struct ncbuf *buf = &qcs->rx.ncbuf;
392 enum ncb_ret ret;
393
394 ret = ncb_advance(buf, bytes);
395 if (ret) {
396 ABORT_NOW(); /* should not happens because removal only in data */
397 }
398
399 if (ncb_is_empty(buf))
400 qc_free_ncbuf(qcs, buf);
401
402 qcs->rx.offset += bytes;
403 if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) {
404 frm = pool_zalloc(pool_head_quic_frame);
405 BUG_ON(!frm); /* TODO handle this properly */
406
407 qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init;
408
409 LIST_INIT(&frm->reflist);
410 frm->type = QUIC_FT_MAX_STREAM_DATA;
411 frm->max_stream_data.id = qcs->id;
412 frm->max_stream_data.max_stream_data = qcs->rx.msd;
413
414 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
415 tasklet_wakeup(qcc->wait_event.tasklet);
416 }
417
418 qcc->lfctl.offsets_consume += bytes;
419 if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) {
420 frm = pool_zalloc(pool_head_quic_frame);
421 BUG_ON(!frm); /* TODO handle this properly */
422
423 qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init;
424
425 LIST_INIT(&frm->reflist);
426 frm->type = QUIC_FT_MAX_DATA;
427 frm->max_data.max_data = qcc->lfctl.md;
428
429 LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list);
430 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
431 }
432}
433
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200434/* Decode the content of STREAM frames already received on the stream instance
435 * <qcs>.
436 *
437 * Returns 0 on success else non-zero.
438 */
439static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
440{
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200441 struct buffer b;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200442 ssize_t ret;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200443
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200444 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
445
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200446 b = qcs_b_dup(&qcs->rx.ncbuf);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200447 ret = qcc->app_ops->decode_qcs(qcs, &b, qcs->flags & QC_SF_FIN_RECV);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200448 if (ret < 0) {
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200449 TRACE_DEVEL("leaving on decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
450 return 1;
451 }
452
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200453 if (ret) {
454 qcs_consume(qcs, ret);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200455 qcs_notify_recv(qcs);
456 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200457
458 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
459
460 return 0;
461}
462
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200463/* Emit a CONNECTION_CLOSE_APP with error <err>. Reserved for application error
464 * code. This will interrupt all future send/receive operations.
465 */
466void qcc_emit_cc_app(struct qcc *qcc, int err)
467{
468 quic_set_connection_close(qcc->conn->handle.qc, err, 1);
469 qcc->flags |= QC_CF_CC_EMIT;
470 tasklet_wakeup(qcc->wait_event.tasklet);
471}
472
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200473/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
474 * <data> with length <len> and represents the offset <offset>. <fin> is set if
475 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100476 *
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200477 * Returns 0 on success else non-zero.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100478 */
479int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200480 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100481{
482 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200483 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100484
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100485 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
486
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200487 if (qcc->flags & QC_CF_CC_EMIT) {
488 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
489 return 0;
490 }
491
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200492 /* RFC 9000 19.8. STREAM Frames
493 *
494 * An endpoint MUST terminate the connection with error
495 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
496 * initiated stream that has not yet been created, or for a send-only
497 * stream.
498 */
499 if (quic_stream_is_local(qcc, id) && quic_stream_is_uni(id)) {
500 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
501 TRACE_DEVEL("leaving on invalid reception for a send-only stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
502 return 1;
503 }
504
Amaury Denoyelle50742292022-03-29 14:57:19 +0200505 qcs = qcc_get_qcs(qcc, id);
506 if (!qcs) {
Frédéric Lécailleb57de072022-05-02 18:58:27 +0200507 if ((id >> QCS_ID_TYPE_SHIFT) <= qcc->strms[qcs_id_type(id)].largest_id) {
508 TRACE_DEVEL("already released stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
509 return 0;
510 }
511 else {
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200512 /* RFC 9000 19.8. STREAM Frames
513 *
514 * An endpoint MUST terminate the connection with error
515 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
516 * initiated stream that has not yet been created, or for a send-only
517 * stream.
518 */
519 if (quic_stream_is_local(qcc, id)) {
520 TRACE_DEVEL("leaving on locally initiated stream not yet created", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
521 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
522 return 1;
523 }
524 else {
525 TRACE_DEVEL("leaving on stream not found", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
526 return 1;
527 }
Frédéric Lécailleb57de072022-05-02 18:58:27 +0200528 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100529 }
530
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100531 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100532 TRACE_DEVEL("leaving on already received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle74cf2372022-04-29 15:58:22 +0200533 return 0;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100534 }
535
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200536 /* TODO if last frame already received, stream size must not change.
537 * Else send FINAL_SIZE_ERROR.
538 */
539
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200540 if (offset + len > qcs->rx.offset_max) {
541 uint64_t diff = offset + len - qcs->rx.offset_max;
542 qcs->rx.offset_max = offset + len;
543 qcc->lfctl.offsets_recv += diff;
544
545 if (offset + len > qcs->rx.msd ||
546 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
547 /* RFC 9000 4.1. Data Flow Control
548 *
549 * A receiver MUST close the connection with an error
550 * of type FLOW_CONTROL_ERROR if the sender violates
551 * the advertised connection or stream data limits
552 */
553 TRACE_DEVEL("leaving on flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
554 qcc->conn, qcs);
555 qcc_emit_cc(qcc, QC_ERR_FLOW_CONTROL_ERROR);
556 return 1;
557 }
558 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100559
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200560 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100561 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200562 ABORT_NOW();
563 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100564 }
565
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100566 TRACE_DEVEL("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200567 if (offset < qcs->rx.offset) {
568 len -= qcs->rx.offset - offset;
569 offset = qcs->rx.offset;
570 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100571
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200572 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
573 if (ret != NCB_RET_OK) {
574 if (ret == NCB_RET_DATA_REJ) {
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200575 /* RFC 9000 2.2. Sending and Receiving Data
576 *
577 * An endpoint could receive data for a stream at the
578 * same stream offset multiple times. Data that has
579 * already been received can be discarded. The data at
580 * a given offset MUST NOT change if it is sent
581 * multiple times; an endpoint MAY treat receipt of
582 * different data at the same offset within a stream as
583 * a connection error of type PROTOCOL_VIOLATION.
584 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200585 TRACE_DEVEL("leaving on data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
586 qcc->conn, qcs);
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200587 qcc_emit_cc(qcc, QC_ERR_PROTOCOL_VIOLATION);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200588 }
589 else if (ret == NCB_RET_GAP_SIZE) {
590 TRACE_DEVEL("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
591 qcc->conn, qcs);
592 }
593 return 1;
594 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100595
596 if (fin)
597 qcs->flags |= QC_SF_FIN_RECV;
598
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200599 if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL))
600 qcc_decode_qcs(qcc, qcs);
601
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200602 if (qcs->flags & QC_SF_READ_ABORTED) {
603 /* TODO should send a STOP_SENDING */
604 qcs_free(qcs);
605 }
606
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100607 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100608 return 0;
609}
610
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100611/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
612 * the frame.
613 *
614 * Returns 0 on success else non-zero.
615 */
616int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
617{
618 if (qcc->rfctl.md < max) {
619 qcc->rfctl.md = max;
620
621 if (qcc->flags & QC_CF_BLK_MFCTL) {
622 qcc->flags &= ~QC_CF_BLK_MFCTL;
623 tasklet_wakeup(qcc->wait_event.tasklet);
624 }
625 }
626 return 0;
627}
628
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100629/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
630 * field of the frame and <id> is the identifier of the QUIC stream.
631 *
632 * Returns 0 on success else non-zero.
633 */
634int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
635{
636 struct qcs *qcs;
637 struct eb64_node *node;
638
639 node = eb64_lookup(&qcc->streams_by_id, id);
640 if (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200641 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100642 if (max > qcs->tx.msd) {
643 qcs->tx.msd = max;
644
645 if (qcs->flags & QC_SF_BLK_SFCTL) {
646 qcs->flags &= ~QC_SF_BLK_SFCTL;
647 tasklet_wakeup(qcc->wait_event.tasklet);
648 }
649 }
650 }
651
652 return 0;
653}
654
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200655/* Signal the closing of remote stream with id <id>. Flow-control for new
656 * streams may be allocated for the peer if needed.
657 */
658static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +0100659{
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200660 struct quic_frame *frm;
661
662 if (quic_stream_is_bidi(id)) {
663 ++qcc->lfctl.cl_bidi_r;
664 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
665 frm = pool_zalloc(pool_head_quic_frame);
666 BUG_ON(!frm); /* TODO handle this properly */
667
668 LIST_INIT(&frm->reflist);
669 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
670 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
671 qcc->lfctl.cl_bidi_r;
672 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
673 tasklet_wakeup(qcc->wait_event.tasklet);
674
675 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
676 qcc->lfctl.cl_bidi_r = 0;
677 }
678 }
679 else {
680 /* TODO */
681 }
682
683 return 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100684}
685
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500686/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100687static void qcs_destroy(struct qcs *qcs)
688{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100689 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200690 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100691
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100692 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100693
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200694 if (quic_stream_is_remote(qcs->qcc, id))
695 qcc_release_remote_stream(qcs->qcc, id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100696
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200697 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100698
699 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100700}
701
702static inline int qcc_is_dead(const struct qcc *qcc)
703{
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200704 if (qcc->app_ops && qcc->app_ops->is_active &&
705 qcc->app_ops->is_active(qcc, qcc->ctx))
706 return 0;
707
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200708 if ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task)
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100709 return 1;
710
711 return 0;
712}
713
714/* Return true if the mux timeout should be armed. */
715static inline int qcc_may_expire(struct qcc *qcc)
716{
Willy Tarreau3215e732022-05-27 10:09:11 +0200717 return !qcc->nb_sc;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100718}
719
720/* release function. This one should be called to free all resources allocated
721 * to the mux.
722 */
723static void qc_release(struct qcc *qcc)
724{
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200725 struct connection *conn = qcc->conn;
726 struct eb64_node *node;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100727
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100728 TRACE_ENTER(QMUX_EV_QCC_END);
729
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200730 if (qcc->app_ops && qcc->app_ops->release)
731 qcc->app_ops->release(qcc->ctx);
Amaury Denoyellef8909452022-03-30 11:51:56 +0200732
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200733 if (qcc->task) {
734 task_destroy(qcc->task);
735 qcc->task = NULL;
736 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100737
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200738 if (qcc->wait_event.tasklet)
739 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyellef3e03a42022-04-21 15:41:34 +0200740 if (conn && qcc->wait_event.events) {
741 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
742 qcc->wait_event.events,
743 &qcc->wait_event);
744 }
Amaury Denoyellef8909452022-03-30 11:51:56 +0200745
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200746 /* liberate remaining qcs instances */
747 node = eb64_first(&qcc->streams_by_id);
748 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200749 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200750 node = eb64_next(node);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200751 qcs_free(qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100752 }
753
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200754 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
Amaury Denoyelle040955f2022-06-13 11:30:46 +0200755 struct quic_frame *frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list);
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200756 LIST_DELETE(&frm->list);
757 pool_free(pool_head_quic_frame, frm);
758 }
759
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200760 pool_free(pool_head_qcc, qcc);
761
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100762 if (conn) {
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100763 LIST_DEL_INIT(&conn->stopping_list);
764
Willy Tarreau784b8682022-04-11 14:18:10 +0200765 conn->handle.qc->conn = NULL;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100766 conn->mux = NULL;
767 conn->ctx = NULL;
768
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100769 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
770
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100771 conn_stop_tracking(conn);
772 conn_full_close(conn);
773 if (conn->destroy_cb)
774 conn->destroy_cb(conn);
775 conn_free(conn);
776 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100777
778 TRACE_LEAVE(QMUX_EV_QCC_END);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100779}
780
Amaury Denoyelleb9e06402022-06-10 15:16:21 +0200781/* Transfer as much as possible data on <qcs> from <in> to <out>. This is done
782 * in respect with available flow-control at stream and connection level.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100783 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200784 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100785 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +0200786static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, struct buffer *in)
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200787{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100788 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200789 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +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_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100793
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100794 qc_get_buf(qcs, out);
795
796 /*
797 * QCS out buffer diagram
798 * head left to_xfer
799 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100800 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100801 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100802 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100803 * ^ ack-off ^ sent-off ^ off
804 *
805 * STREAM frame
806 * ^ ^
807 * |xxxxxxxxxxxxxxxxx|
808 */
809
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200810 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100811 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +0200812 BUG_ON_HOT(qcc->tx.offsets < qcc->tx.sent_offsets);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100813
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100814 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200815 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100816
817 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
818 /* do not exceed flow control limit */
819 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
820 to_xfer = qcs->tx.msd - qcs->tx.offset;
821
Amaury Denoyelleb9e06402022-06-10 15:16:21 +0200822 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100823 /* do not overcome flow control limit on connection */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +0200824 if (qcc->tx.offsets + to_xfer > qcc->rfctl.md)
825 to_xfer = qcc->rfctl.md - qcc->tx.offsets;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100826
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100827 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200828 goto out;
829
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200830 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200831
832 out:
833 {
834 struct qcs_xfer_data_trace_arg arg = {
835 .prep = b_data(out), .xfer = total,
836 };
837 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
838 qcc->conn, qcs, &arg);
839 }
840
841 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200842}
843
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200844/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
845 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
846 * stream frame.
847 *
848 * Returns the length of the STREAM frame or a negative error code.
849 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200850static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
851 struct list *frm_list)
852{
853 struct qcc *qcc = qcs->qcc;
854 struct quic_frame *frm;
855 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200856 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200857
858 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
859
Amaury Denoyellea4569202022-04-15 17:29:25 +0200860 /* if ack_offset < buf_offset, it points to an older buffer. */
861 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
862 BUG_ON(qcs->tx.sent_offset < base_off);
863
864 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200865 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200866 BUG_ON(total < 0);
867
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200868 if (!total) {
869 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
870 return 0;
871 }
Amaury Denoyellea4569202022-04-15 17:29:25 +0200872 BUG_ON(qcs->tx.sent_offset >= qcs->tx.offset);
873 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +0200874 BUG_ON(qcc->tx.sent_offsets + total > qcc->rfctl.md);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200875
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200876 frm = pool_zalloc(pool_head_quic_frame);
877 if (!frm)
878 goto err;
879
Frédéric Lécailleb9171912022-04-21 17:32:10 +0200880 LIST_INIT(&frm->reflist);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100881 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200882 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200883 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100884 frm->stream.buf = out;
885 frm->stream.data = (unsigned char *)b_peek(out, head);
886
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100887 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200888 if (fin)
889 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100890
891 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200892 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100893 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200894 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100895
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200896 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
897 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200898
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100899 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100900
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200901 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100902 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200903 struct qcs_build_stream_trace_arg arg = {
904 .len = frm->stream.len, .fin = fin,
905 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100906 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200907 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100908 qcc->conn, qcs, &arg);
909 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100910
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200911 return total;
912
913 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100914 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200915 return -1;
916}
917
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100918/* This function must be called by the upper layer to inform about the sending
919 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
920 * <offset>.
921 */
922void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
923{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100924 struct qcc *qcc = qcs->qcc;
925 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100926
927 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +0200928 BUG_ON(offset + data > qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100929
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100930 /* check if the STREAM frame has already been notified. It can happen
931 * for retransmission.
932 */
933 if (offset + data <= qcs->tx.sent_offset)
934 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100935
936 diff = offset + data - qcs->tx.sent_offset;
937
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100938 /* increase offset sum on connection */
939 qcc->tx.sent_offsets += diff;
940 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
941 if (qcc->tx.sent_offsets == qcc->rfctl.md)
942 qcc->flags |= QC_CF_BLK_MFCTL;
943
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100944 /* increase offset on stream */
945 qcs->tx.sent_offset += diff;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100946 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200947 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100948 if (qcs->tx.sent_offset == qcs->tx.msd)
949 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200950
951 if (qcs->tx.offset == qcs->tx.sent_offset && b_full(&qcs->stream->buf->buf)) {
952 qc_stream_buf_release(qcs->stream);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200953 /* prepare qcs for immediate send retry if data to send */
954 if (b_data(&qcs->tx.buf))
955 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200956 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100957}
958
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100959/* Wrapper for send on transport layer. Send a list of frames <frms> for the
960 * connection <qcc>.
961 *
962 * Returns 0 if all data sent with success else non-zero.
963 */
964static int qc_send_frames(struct qcc *qcc, struct list *frms)
965{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100966 /* TODO implement an opportunistic retry mechanism. This is needed
967 * because qc_send_app_pkts is not completed. It will only prepare data
968 * up to its Tx buffer. The frames left are not send even if the Tx
969 * buffer is emptied by the sendto call.
970 *
971 * To overcome this, we call repeatedly qc_send_app_pkts until we
972 * detect that the transport layer has send nothing. This could happen
973 * on congestion or sendto syscall error.
974 *
975 * When qc_send_app_pkts is improved to handle retry by itself, we can
976 * remove the looping from the MUX.
977 */
978 struct quic_frame *first_frm;
979 uint64_t first_offset = 0;
980 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100981
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100982 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
983
984 if (LIST_ISEMPTY(frms)) {
985 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200986 return 1;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100987 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100988
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200989 LIST_INIT(&qcc->send_retry_list);
990
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100991 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100992 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
993 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
994 first_offset = first_frm->stream.offset.key;
995 first_stream_frame_type = 1;
996 }
997 else {
998 first_stream_frame_type = 0;
999 }
1000
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001001 if (!LIST_ISEMPTY(frms))
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02001002 qc_send_app_pkts(qcc->conn->handle.qc, 0, frms);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001003
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001004 /* If there is frames left, check if the transport layer has send some
1005 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001006 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001007 if (!LIST_ISEMPTY(frms)) {
1008 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
1009 goto retry_send;
1010
1011 /* If the first frame is STREAM, check if its offset has
1012 * changed.
1013 */
1014 if (first_stream_frame_type &&
1015 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
1016 goto retry_send;
1017 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001018 }
1019
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001020 /* If there is frames left at this stage, transport layer is blocked.
1021 * Subscribe on it to retry later.
1022 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001023 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001024 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001025 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1026 SUB_RETRY_SEND, &qcc->wait_event);
1027 return 1;
1028 }
1029
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001030 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1031
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001032 return 0;
1033}
1034
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001035/* Used internally by qc_send function. Proceed to send for <qcs>. This will
1036 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001037 * is then generated and inserted in <frms> list.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001038 *
1039 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
1040 * be null if out buffer cannot be allocated.
1041 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001042static int _qc_send_qcs(struct qcs *qcs, struct list *frms)
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001043{
1044 struct qcc *qcc = qcs->qcc;
1045 struct buffer *buf = &qcs->tx.buf;
1046 struct buffer *out = qc_stream_buf_get(qcs->stream);
1047 int xfer = 0;
1048
1049 /* Allocate <out> buffer if necessary. */
1050 if (!out) {
1051 if (qcc->flags & QC_CF_CONN_FULL)
1052 return 0;
1053
1054 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
1055 if (!out) {
1056 qcc->flags |= QC_CF_CONN_FULL;
1057 return 0;
1058 }
1059 }
1060
1061 /* Transfer data from <buf> to <out>. */
1062 if (b_data(buf)) {
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001063 xfer = qcs_xfer_data(qcs, out, buf);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001064 if (xfer > 0) {
1065 qcs_notify_send(qcs);
1066 qcs->flags &= ~QC_SF_BLK_MROOM;
1067 }
1068
1069 qcs->tx.offset += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001070 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001071 qcc->tx.offsets += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001072 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001073 }
1074
1075 /* out buffer cannot be emptied if qcs offsets differ. */
1076 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1077
1078 /* Build a new STREAM frame with <out> buffer. */
1079 if (qcs->tx.sent_offset != qcs->tx.offset) {
1080 int ret;
1081 char fin = !!(qcs->flags & QC_SF_FIN_STREAM);
1082
1083 /* FIN is set if all incoming data were transfered. */
1084 fin = !!(fin && !b_data(buf));
1085
1086 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +02001087 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001088 }
1089
1090 return xfer;
1091}
1092
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001093/* Proceed to sending. Loop through all available streams for the <qcc>
1094 * instance and try to send as much as possible.
1095 *
1096 * Returns the total of bytes sent to the transport layer.
1097 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001098static int qc_send(struct qcc *qcc)
1099{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001100 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001101 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001102 struct qcs *qcs, *qcs_tmp;
1103 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001104
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001105 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001106
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001107 if (qcc->conn->flags & CO_FL_SOCK_WR_SH || qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001108 qcc->conn->flags |= CO_FL_ERROR;
1109 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
1110 return 0;
1111 }
1112
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001113 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1114 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
1115 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
1116 goto out;
1117 }
1118 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02001119
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001120 if (qcc->flags & QC_CF_BLK_MFCTL)
1121 return 0;
1122
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001123 /* loop through all streams, construct STREAM frames if data available.
1124 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001125 */
1126 node = eb64_first(&qcc->streams_by_id);
1127 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001128 int ret;
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001129 uint64_t id;
1130
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001131 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001132 id = qcs->id;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001133
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001134 if (quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001135 node = eb64_next(node);
1136 continue;
1137 }
1138
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001139 if (qcs->flags & QC_SF_BLK_SFCTL) {
1140 node = eb64_next(node);
1141 continue;
1142 }
1143
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001144 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001145 node = eb64_next(node);
1146 continue;
1147 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001148
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001149 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001150 total += ret;
1151 node = eb64_next(node);
1152 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001153
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001154 if (qc_send_frames(qcc, &frms)) {
1155 /* data rejected by transport layer, do not retry. */
1156 goto out;
1157 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001158
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001159 retry:
1160 tmp_total = 0;
1161 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
1162 int ret;
1163 BUG_ON(!b_data(&qcs->tx.buf));
1164 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001165
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001166 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001167 tmp_total += ret;
1168 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001169 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001170
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001171 total += tmp_total;
1172 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
1173 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +01001174
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001175 out:
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02001176 /* Deallocate frames that the transport layer has rejected. */
1177 if (!LIST_ISEMPTY(&frms)) {
1178 struct quic_frame *frm, *frm2;
1179 list_for_each_entry_safe(frm, frm2, &frms, list) {
1180 LIST_DELETE(&frm->list);
1181 pool_free(pool_head_quic_frame, frm);
1182 }
1183 }
1184
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001185 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1186
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001187 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001188}
1189
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001190/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1191 * operation.
1192 *
1193 * Returns 0 on success else non-zero.
1194 */
1195static int qc_recv(struct qcc *qcc)
1196{
1197 struct eb64_node *node;
1198 struct qcs *qcs;
1199
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001200 TRACE_ENTER(QMUX_EV_QCC_RECV);
1201
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001202 if (qcc->flags & QC_CF_CC_EMIT) {
1203 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
1204 return 0;
1205 }
1206
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001207 node = eb64_first(&qcc->streams_by_id);
1208 while (node) {
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001209 uint64_t id;
1210
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001211 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001212 id = qcs->id;
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001213
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001214 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001215 node = eb64_next(node);
1216 continue;
1217 }
1218
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001219 if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001220 node = eb64_next(node);
1221 continue;
1222 }
1223
1224 qcc_decode_qcs(qcc, qcs);
1225 node = eb64_next(node);
Amaury Denoyelle849b24f2022-05-24 17:22:07 +02001226
1227 if (qcs->flags & QC_SF_READ_ABORTED) {
1228 /* TODO should send a STOP_SENDING */
1229 qcs_free(qcs);
1230 }
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001231 }
1232
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001233 TRACE_LEAVE(QMUX_EV_QCC_RECV);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001234 return 0;
1235}
1236
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001237/* Release all streams that are already marked as detached. This is only done
1238 * if their TX buffers are empty or if a CONNECTION_CLOSE has been received.
1239 *
1240 * Return the number of released stream.
1241 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001242static int qc_release_detached_streams(struct qcc *qcc)
1243{
1244 struct eb64_node *node;
1245 int release = 0;
1246
1247 node = eb64_first(&qcc->streams_by_id);
1248 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001249 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001250 node = eb64_next(node);
1251
1252 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001253 if (!b_data(&qcs->tx.buf) &&
1254 qcs->tx.offset == qcs->tx.sent_offset) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001255 qcs_destroy(qcs);
1256 release = 1;
1257 }
1258 else {
1259 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1260 SUB_RETRY_SEND, &qcc->wait_event);
1261 }
1262 }
1263 }
1264
1265 return release;
1266}
1267
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001268static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1269{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001270 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001271
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001272 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001273
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001274 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001275
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001276 if (qc_release_detached_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001277 if (qcc_is_dead(qcc)) {
1278 qc_release(qcc);
1279 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001280 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001281 if (qcc_may_expire(qcc))
1282 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1283 else
1284 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001285 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001286 }
1287 }
1288
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001289 qc_recv(qcc);
1290
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001291 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1292
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001293 return NULL;
1294}
1295
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001296static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1297{
1298 struct qcc *qcc = ctx;
1299 int expired = tick_is_expired(t->expire, now_ms);
1300
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001301 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001302
1303 if (qcc) {
1304 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001305 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001306 return t;
1307 }
1308
1309 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001310 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001311 t->expire = TICK_ETERNITY;
1312 return t;
1313 }
1314 }
1315
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001316 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001317
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001318 if (!qcc) {
1319 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001320 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001321 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001322
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001323 qcc->task = NULL;
1324
1325 if (qcc_is_dead(qcc))
1326 qc_release(qcc);
1327
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001328 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1329
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001330 return NULL;
1331}
1332
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001333static int qc_init(struct connection *conn, struct proxy *prx,
1334 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001335{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001336 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001337 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001338
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001339 qcc = pool_alloc(pool_head_qcc);
1340 if (!qcc)
1341 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001342
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001343 qcc->conn = conn;
1344 conn->ctx = qcc;
Willy Tarreau3215e732022-05-27 10:09:11 +02001345 qcc->nb_sc = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001346 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001347
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001348 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001349
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001350 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001351
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001352 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001353 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001354
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001355 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001356 qcc->tx.sent_offsets = qcc->tx.offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001357
1358 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001359 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001360 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
1361 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001362 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001363 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001364
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001365 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001366 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
1367 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001368 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001369 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001370
1371 /* Server initiated streams must respect the server flow control. */
1372 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001373 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
1374 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001375 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001376 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1377
1378 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001379 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
1380 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001381 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001382 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001383
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001384 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001385 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02001386 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
1387 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001388 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001389
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001390 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001391 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001392
Willy Tarreau784b8682022-04-11 14:18:10 +02001393 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001394 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001395 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1396 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1397
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001398 qcc->wait_event.tasklet = tasklet_new();
1399 if (!qcc->wait_event.tasklet)
1400 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001401
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001402 LIST_INIT(&qcc->send_retry_list);
1403
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001404 qcc->subs = NULL;
1405 qcc->wait_event.tasklet->process = qc_io_cb;
1406 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001407 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001408
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001409 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001410 qcc->task = NULL;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001411 qcc->timeout = prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001412 if (tick_isset(qcc->timeout)) {
1413 qcc->task = task_new_here();
1414 if (!qcc->task)
1415 goto fail_no_timeout_task;
1416 qcc->task->process = qc_timeout_task;
1417 qcc->task->context = qcc;
1418 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1419 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001420
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001421 if (!conn_is_back(conn)) {
1422 if (!LIST_INLIST(&conn->stopping_list)) {
1423 LIST_APPEND(&mux_stopping_data[tid].list,
1424 &conn->stopping_list);
1425 }
1426 }
1427
Willy Tarreau784b8682022-04-11 14:18:10 +02001428 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001429 /* init read cycle */
1430 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001431
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001432 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001433
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001434 fail_no_timeout_task:
1435 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001436 fail_no_tasklet:
1437 pool_free(pool_head_qcc, qcc);
1438 fail_no_qcc:
1439 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001440}
1441
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001442static void qc_destroy(void *ctx)
1443{
1444 struct qcc *qcc = ctx;
1445
1446 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
1447 qc_release(qcc);
1448 TRACE_LEAVE(QMUX_EV_QCC_END);
1449}
1450
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001451static void qc_detach(struct sedesc *sd)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001452{
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001453 struct qcs *qcs = sd->se;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001454 struct qcc *qcc = qcs->qcc;
1455
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001456 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001457
Willy Tarreau3215e732022-05-27 10:09:11 +02001458 --qcc->nb_sc;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001459
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001460 if ((b_data(&qcs->tx.buf) || qcs->tx.offset > qcs->tx.sent_offset) &&
1461 !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001462 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001463 qcs->flags |= QC_SF_DETACH;
1464 return;
1465 }
1466
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001467 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001468
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001469 if (qcc_is_dead(qcc)) {
1470 qc_release(qcc);
1471 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001472 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001473 if (qcc_may_expire(qcc))
1474 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1475 else
1476 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001477 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001478 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001479
1480 TRACE_LEAVE(QMUX_EV_STRM_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001481}
1482
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001483/* Called from the upper layer, to receive data */
Willy Tarreau3215e732022-05-27 10:09:11 +02001484static size_t qc_rcv_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001485 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001486{
Willy Tarreau3215e732022-05-27 10:09:11 +02001487 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001488 struct htx *qcs_htx = NULL;
1489 struct htx *cs_htx = NULL;
1490 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001491 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001492
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001493 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001494
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001495 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
1496 if (htx_is_empty(qcs_htx)) {
1497 /* Set buffer data to 0 as HTX is empty. */
1498 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1499 goto end;
1500 }
1501
1502 ret = qcs_htx->data;
1503
1504 cs_htx = htx_from_buf(buf);
1505 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
1506 htx_to_buf(cs_htx, buf);
1507 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1508 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
1509 goto end;
1510 }
1511
1512 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
1513 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
1514
1515 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001516 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
1517 cs_htx->flags |= HTX_FL_EOM;
1518 fin = 1;
1519 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001520
1521 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
1522 htx_to_buf(cs_htx, buf);
1523 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1524 ret -= qcs_htx->data;
1525
1526 end:
1527 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001528 se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001529 }
1530 else {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001531 se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
1532 if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING))
1533 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001534
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001535 if (fin)
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001536 se_fl_set(qcs->sd, SE_FL_EOI);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001537
1538 if (b_size(&qcs->rx.app_buf)) {
1539 b_free(&qcs->rx.app_buf);
1540 offer_buffers(NULL, 1);
1541 }
1542 }
1543
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001544 if (ret) {
1545 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001546 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001547 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001548
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001549 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
1550
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001551 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001552}
1553
Willy Tarreau3215e732022-05-27 10:09:11 +02001554static size_t qc_snd_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001555 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001556{
Willy Tarreau3215e732022-05-27 10:09:11 +02001557 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001558 size_t ret;
1559
1560 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001561
Willy Tarreau3215e732022-05-27 10:09:11 +02001562 ret = qcs->qcc->app_ops->snd_buf(sc, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001563
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001564 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
1565
1566 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001567}
1568
1569/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1570 * event subscriber <es> is not allowed to change from a previous call as long
1571 * as at least one event is still subscribed. The <event_type> must only be a
1572 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1573 */
Willy Tarreau3215e732022-05-27 10:09:11 +02001574static int qc_subscribe(struct stconn *sc, int event_type,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001575 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001576{
Willy Tarreau3215e732022-05-27 10:09:11 +02001577 return qcs_subscribe(__sc_mux_strm(sc), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001578}
1579
1580/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1581 * The <es> pointer is not allowed to differ from the one passed to the
1582 * subscribe() call. It always returns zero.
1583 */
Willy Tarreau3215e732022-05-27 10:09:11 +02001584static int qc_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001585{
Willy Tarreau3215e732022-05-27 10:09:11 +02001586 struct qcs *qcs = __sc_mux_strm(sc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001587
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001588 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1589 BUG_ON(qcs->subs && qcs->subs != es);
1590
1591 es->events &= ~event_type;
1592 if (!es->events)
1593 qcs->subs = NULL;
1594
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001595 return 0;
1596}
1597
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001598/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Willy Tarreau4596fe22022-05-17 19:07:51 +02001599 * report SE_FL_ERR_PENDING|SE_FL_ERROR on the attached stream connectors and
1600 * wake them.
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001601 */
1602static int qc_wake_some_streams(struct qcc *qcc)
1603{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001604 struct qcs *qcs;
1605 struct eb64_node *node;
1606
1607 for (node = eb64_first(&qcc->streams_by_id); node;
1608 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001609 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001610
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001611 if (!qcs->sd || !qcs->sd->sc)
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001612 continue;
1613
1614 if (qcc->conn->flags & CO_FL_ERROR) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001615 se_fl_set(qcs->sd, SE_FL_ERR_PENDING);
1616 if (se_fl_test(qcs->sd, SE_FL_EOS))
1617 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001618
1619 if (qcs->subs) {
1620 qcs_notify_recv(qcs);
1621 qcs_notify_send(qcs);
1622 }
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001623 else if (qcs->sd->sc->app_ops->wake) {
1624 qcs->sd->sc->app_ops->wake(qcs->sd->sc);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001625 }
1626 }
1627 }
1628
1629 return 0;
1630}
1631
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001632static int qc_wake(struct connection *conn)
1633{
1634 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02001635 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001636
1637 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001638
1639 /* Check if a soft-stop is in progress.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001640 *
1641 * TODO this is revelant for frontend connections only.
Amaury Denoyelled0c62322022-05-23 08:52:58 +02001642 *
1643 * TODO Client should be notified with a H3 GOAWAY and then a
1644 * CONNECTION_CLOSE. However, quic-conn uses the listener socket for
1645 * sending which at this stage is already closed.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001646 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001647 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Amaury Denoyelled0c62322022-05-23 08:52:58 +02001648 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001649
Willy Tarreau784b8682022-04-11 14:18:10 +02001650 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02001651 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
1652
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001653 qc_send(qcc);
1654
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001655 qc_wake_some_streams(qcc);
1656
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001657 if (qcc_is_dead(qcc))
1658 goto release;
1659
1660 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
1661
1662 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001663
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001664 release:
1665 qc_release(qcc);
1666 TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001667 return 1;
1668}
1669
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001670
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001671static void qmux_trace_frm(const struct quic_frame *frm)
1672{
1673 switch (frm->type) {
1674 case QUIC_FT_MAX_STREAMS_BIDI:
1675 chunk_appendf(&trace_buf, " max_streams=%lu",
1676 frm->max_streams_bidi.max_streams);
1677 break;
1678
1679 case QUIC_FT_MAX_STREAMS_UNI:
1680 chunk_appendf(&trace_buf, " max_streams=%lu",
1681 frm->max_streams_uni.max_streams);
1682 break;
1683
1684 default:
1685 break;
1686 }
1687}
1688
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001689/* quic-mux trace handler */
1690static void qmux_trace(enum trace_level level, uint64_t mask,
1691 const struct trace_source *src,
1692 const struct ist where, const struct ist func,
1693 const void *a1, const void *a2, const void *a3, const void *a4)
1694{
1695 const struct connection *conn = a1;
1696 const struct qcc *qcc = conn ? conn->ctx : NULL;
1697 const struct qcs *qcs = a2;
1698
1699 if (!qcc)
1700 return;
1701
1702 if (src->verbosity > QMUX_VERB_CLEAN) {
1703 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
1704
1705 if (qcs)
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001706 chunk_appendf(&trace_buf, " qcs=%p(%lu)", qcs, qcs->id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001707
1708 if (mask & QMUX_EV_QCC_NQCS) {
1709 const uint64_t *id = a3;
1710 chunk_appendf(&trace_buf, " id=%lu", *id);
1711 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001712
1713 if (mask & QMUX_EV_SEND_FRM)
1714 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001715
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001716 if (mask & QMUX_EV_QCS_XFER_DATA) {
1717 const struct qcs_xfer_data_trace_arg *arg = a3;
1718 chunk_appendf(&trace_buf, " prep=%lu xfer=%d",
1719 arg->prep, arg->xfer);
1720 }
1721
1722 if (mask & QMUX_EV_QCS_BUILD_STRM) {
1723 const struct qcs_build_stream_trace_arg *arg = a3;
1724 chunk_appendf(&trace_buf, " len=%lu fin=%d offset=%lu",
1725 arg->len, arg->fin, arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001726 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001727 }
1728}
1729
1730
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001731static const struct mux_ops qc_ops = {
1732 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001733 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001734 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001735 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001736 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001737 .subscribe = qc_subscribe,
1738 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001739 .wake = qc_wake,
Willy Tarreaub5821e12022-04-26 11:54:08 +02001740 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02001741 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001742};
1743
1744static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001745 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001746
1747INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);