blob: 455b43e48e81955a6edb059a3a3bcbf1ee8b70f5 [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
Amaury Denoyelle36d4b5e2022-07-01 11:25:40 +0200383/* Remove <bytes> from <qcs> Rx buffer. Flow-control for received offsets may
384 * be allocated for the peer if needed.
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200385 */
386static void qcs_consume(struct qcs *qcs, uint64_t bytes)
387{
388 struct qcc *qcc = qcs->qcc;
389 struct quic_frame *frm;
390 struct ncbuf *buf = &qcs->rx.ncbuf;
391 enum ncb_ret ret;
392
393 ret = ncb_advance(buf, bytes);
394 if (ret) {
395 ABORT_NOW(); /* should not happens because removal only in data */
396 }
397
398 if (ncb_is_empty(buf))
399 qc_free_ncbuf(qcs, buf);
400
401 qcs->rx.offset += bytes;
402 if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) {
403 frm = pool_zalloc(pool_head_quic_frame);
404 BUG_ON(!frm); /* TODO handle this properly */
405
406 qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init;
407
408 LIST_INIT(&frm->reflist);
409 frm->type = QUIC_FT_MAX_STREAM_DATA;
410 frm->max_stream_data.id = qcs->id;
411 frm->max_stream_data.max_stream_data = qcs->rx.msd;
412
413 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
414 tasklet_wakeup(qcc->wait_event.tasklet);
415 }
416
417 qcc->lfctl.offsets_consume += bytes;
418 if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) {
419 frm = pool_zalloc(pool_head_quic_frame);
420 BUG_ON(!frm); /* TODO handle this properly */
421
422 qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init;
423
424 LIST_INIT(&frm->reflist);
425 frm->type = QUIC_FT_MAX_DATA;
426 frm->max_data.max_data = qcc->lfctl.md;
427
428 LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list);
429 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
430 }
431}
432
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200433/* Decode the content of STREAM frames already received on the stream instance
434 * <qcs>.
435 *
436 * Returns 0 on success else non-zero.
437 */
438static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
439{
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200440 struct buffer b;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200441 ssize_t ret;
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200442 int fin = 0;
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 Denoyelle6befccd2022-07-01 11:26:04 +0200447
448 /* Signal FIN to application if STREAM FIN received and there is no gap
449 * in the Rx buffer.
450 */
451 if (qcs->flags & QC_SF_FIN_RECV && !ncb_is_fragmented(&qcs->rx.ncbuf))
452 fin = 1;
453
454 ret = qcc->app_ops->decode_qcs(qcs, &b, fin);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200455 if (ret < 0) {
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200456 TRACE_DEVEL("leaving on decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
457 return 1;
458 }
459
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200460 if (ret) {
461 qcs_consume(qcs, ret);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200462 qcs_notify_recv(qcs);
463 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200464
465 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
466
467 return 0;
468}
469
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200470/* Emit a CONNECTION_CLOSE_APP with error <err>. Reserved for application error
471 * code. This will interrupt all future send/receive operations.
472 */
473void qcc_emit_cc_app(struct qcc *qcc, int err)
474{
475 quic_set_connection_close(qcc->conn->handle.qc, err, 1);
476 qcc->flags |= QC_CF_CC_EMIT;
477 tasklet_wakeup(qcc->wait_event.tasklet);
478}
479
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200480/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
481 * <data> with length <len> and represents the offset <offset>. <fin> is set if
482 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100483 *
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200484 * Returns 0 on success else non-zero.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100485 */
486int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200487 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100488{
489 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200490 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100491
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100492 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
493
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200494 if (qcc->flags & QC_CF_CC_EMIT) {
495 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
496 return 0;
497 }
498
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200499 /* RFC 9000 19.8. STREAM Frames
500 *
501 * An endpoint MUST terminate the connection with error
502 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
503 * initiated stream that has not yet been created, or for a send-only
504 * stream.
505 */
506 if (quic_stream_is_local(qcc, id) && quic_stream_is_uni(id)) {
507 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
508 TRACE_DEVEL("leaving on invalid reception for a send-only stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
509 return 1;
510 }
511
Amaury Denoyelle50742292022-03-29 14:57:19 +0200512 qcs = qcc_get_qcs(qcc, id);
513 if (!qcs) {
Frédéric Lécailleb57de072022-05-02 18:58:27 +0200514 if ((id >> QCS_ID_TYPE_SHIFT) <= qcc->strms[qcs_id_type(id)].largest_id) {
515 TRACE_DEVEL("already released stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
516 return 0;
517 }
518 else {
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200519 /* RFC 9000 19.8. STREAM Frames
520 *
521 * An endpoint MUST terminate the connection with error
522 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
523 * initiated stream that has not yet been created, or for a send-only
524 * stream.
525 */
526 if (quic_stream_is_local(qcc, id)) {
527 TRACE_DEVEL("leaving on locally initiated stream not yet created", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
528 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
529 return 1;
530 }
531 else {
532 TRACE_DEVEL("leaving on stream not found", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
533 return 1;
534 }
Frédéric Lécailleb57de072022-05-02 18:58:27 +0200535 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100536 }
537
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100538 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100539 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 +0200540 return 0;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100541 }
542
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200543 /* TODO if last frame already received, stream size must not change.
544 * Else send FINAL_SIZE_ERROR.
545 */
546
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200547 if (offset + len > qcs->rx.offset_max) {
548 uint64_t diff = offset + len - qcs->rx.offset_max;
549 qcs->rx.offset_max = offset + len;
550 qcc->lfctl.offsets_recv += diff;
551
552 if (offset + len > qcs->rx.msd ||
553 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
554 /* RFC 9000 4.1. Data Flow Control
555 *
556 * A receiver MUST close the connection with an error
557 * of type FLOW_CONTROL_ERROR if the sender violates
558 * the advertised connection or stream data limits
559 */
560 TRACE_DEVEL("leaving on flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
561 qcc->conn, qcs);
562 qcc_emit_cc(qcc, QC_ERR_FLOW_CONTROL_ERROR);
563 return 1;
564 }
565 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100566
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200567 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100568 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200569 ABORT_NOW();
570 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100571 }
572
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100573 TRACE_DEVEL("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200574 if (offset < qcs->rx.offset) {
575 len -= qcs->rx.offset - offset;
576 offset = qcs->rx.offset;
577 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100578
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200579 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
580 if (ret != NCB_RET_OK) {
581 if (ret == NCB_RET_DATA_REJ) {
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200582 /* RFC 9000 2.2. Sending and Receiving Data
583 *
584 * An endpoint could receive data for a stream at the
585 * same stream offset multiple times. Data that has
586 * already been received can be discarded. The data at
587 * a given offset MUST NOT change if it is sent
588 * multiple times; an endpoint MAY treat receipt of
589 * different data at the same offset within a stream as
590 * a connection error of type PROTOCOL_VIOLATION.
591 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200592 TRACE_DEVEL("leaving on data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
593 qcc->conn, qcs);
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200594 qcc_emit_cc(qcc, QC_ERR_PROTOCOL_VIOLATION);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200595 }
596 else if (ret == NCB_RET_GAP_SIZE) {
597 TRACE_DEVEL("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
598 qcc->conn, qcs);
599 }
600 return 1;
601 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100602
603 if (fin)
604 qcs->flags |= QC_SF_FIN_RECV;
605
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200606 if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL))
607 qcc_decode_qcs(qcc, qcs);
608
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200609 if (qcs->flags & QC_SF_READ_ABORTED) {
610 /* TODO should send a STOP_SENDING */
611 qcs_free(qcs);
612 }
613
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100614 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100615 return 0;
616}
617
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100618/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
619 * the frame.
620 *
621 * Returns 0 on success else non-zero.
622 */
623int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
624{
625 if (qcc->rfctl.md < max) {
626 qcc->rfctl.md = max;
627
628 if (qcc->flags & QC_CF_BLK_MFCTL) {
629 qcc->flags &= ~QC_CF_BLK_MFCTL;
630 tasklet_wakeup(qcc->wait_event.tasklet);
631 }
632 }
633 return 0;
634}
635
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100636/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
637 * field of the frame and <id> is the identifier of the QUIC stream.
638 *
639 * Returns 0 on success else non-zero.
640 */
641int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
642{
643 struct qcs *qcs;
644 struct eb64_node *node;
645
646 node = eb64_lookup(&qcc->streams_by_id, id);
647 if (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200648 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100649 if (max > qcs->tx.msd) {
650 qcs->tx.msd = max;
651
652 if (qcs->flags & QC_SF_BLK_SFCTL) {
653 qcs->flags &= ~QC_SF_BLK_SFCTL;
654 tasklet_wakeup(qcc->wait_event.tasklet);
655 }
656 }
657 }
658
659 return 0;
660}
661
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200662/* Signal the closing of remote stream with id <id>. Flow-control for new
663 * streams may be allocated for the peer if needed.
664 */
665static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +0100666{
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200667 struct quic_frame *frm;
668
669 if (quic_stream_is_bidi(id)) {
670 ++qcc->lfctl.cl_bidi_r;
671 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
672 frm = pool_zalloc(pool_head_quic_frame);
673 BUG_ON(!frm); /* TODO handle this properly */
674
675 LIST_INIT(&frm->reflist);
676 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
677 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
678 qcc->lfctl.cl_bidi_r;
679 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
680 tasklet_wakeup(qcc->wait_event.tasklet);
681
682 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
683 qcc->lfctl.cl_bidi_r = 0;
684 }
685 }
686 else {
687 /* TODO */
688 }
689
690 return 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100691}
692
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500693/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100694static void qcs_destroy(struct qcs *qcs)
695{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100696 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200697 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100698
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100699 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100700
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200701 if (quic_stream_is_remote(qcs->qcc, id))
702 qcc_release_remote_stream(qcs->qcc, id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100703
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200704 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100705
706 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100707}
708
709static inline int qcc_is_dead(const struct qcc *qcc)
710{
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200711 if (qcc->app_ops && qcc->app_ops->is_active &&
712 qcc->app_ops->is_active(qcc, qcc->ctx))
713 return 0;
714
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200715 if ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task)
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100716 return 1;
717
718 return 0;
719}
720
721/* Return true if the mux timeout should be armed. */
722static inline int qcc_may_expire(struct qcc *qcc)
723{
Willy Tarreau3215e732022-05-27 10:09:11 +0200724 return !qcc->nb_sc;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100725}
726
727/* release function. This one should be called to free all resources allocated
728 * to the mux.
729 */
730static void qc_release(struct qcc *qcc)
731{
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200732 struct connection *conn = qcc->conn;
733 struct eb64_node *node;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100734
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100735 TRACE_ENTER(QMUX_EV_QCC_END);
736
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200737 if (qcc->app_ops && qcc->app_ops->release)
738 qcc->app_ops->release(qcc->ctx);
Amaury Denoyellef8909452022-03-30 11:51:56 +0200739
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200740 if (qcc->task) {
741 task_destroy(qcc->task);
742 qcc->task = NULL;
743 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100744
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200745 if (qcc->wait_event.tasklet)
746 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyellef3e03a42022-04-21 15:41:34 +0200747 if (conn && qcc->wait_event.events) {
748 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
749 qcc->wait_event.events,
750 &qcc->wait_event);
751 }
Amaury Denoyellef8909452022-03-30 11:51:56 +0200752
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200753 /* liberate remaining qcs instances */
754 node = eb64_first(&qcc->streams_by_id);
755 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200756 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200757 node = eb64_next(node);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200758 qcs_free(qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100759 }
760
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200761 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
Amaury Denoyelle040955f2022-06-13 11:30:46 +0200762 struct quic_frame *frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list);
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200763 LIST_DELETE(&frm->list);
764 pool_free(pool_head_quic_frame, frm);
765 }
766
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200767 pool_free(pool_head_qcc, qcc);
768
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100769 if (conn) {
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100770 LIST_DEL_INIT(&conn->stopping_list);
771
Willy Tarreau784b8682022-04-11 14:18:10 +0200772 conn->handle.qc->conn = NULL;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100773 conn->mux = NULL;
774 conn->ctx = NULL;
775
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100776 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
777
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100778 conn_stop_tracking(conn);
779 conn_full_close(conn);
780 if (conn->destroy_cb)
781 conn->destroy_cb(conn);
782 conn_free(conn);
783 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100784
785 TRACE_LEAVE(QMUX_EV_QCC_END);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100786}
787
Amaury Denoyelleb9e06402022-06-10 15:16:21 +0200788/* Transfer as much as possible data on <qcs> from <in> to <out>. This is done
789 * in respect with available flow-control at stream and connection level.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100790 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200791 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100792 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +0200793static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, struct buffer *in)
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200794{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100795 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200796 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100797 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200798
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100799 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100800
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100801 qc_get_buf(qcs, out);
802
803 /*
804 * QCS out buffer diagram
805 * head left to_xfer
806 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100807 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100808 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100809 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100810 * ^ ack-off ^ sent-off ^ off
811 *
812 * STREAM frame
813 * ^ ^
814 * |xxxxxxxxxxxxxxxxx|
815 */
816
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200817 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100818 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +0200819 BUG_ON_HOT(qcc->tx.offsets < qcc->tx.sent_offsets);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100820
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100821 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200822 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100823
824 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
825 /* do not exceed flow control limit */
826 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
827 to_xfer = qcs->tx.msd - qcs->tx.offset;
828
Amaury Denoyelleb9e06402022-06-10 15:16:21 +0200829 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100830 /* do not overcome flow control limit on connection */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +0200831 if (qcc->tx.offsets + to_xfer > qcc->rfctl.md)
832 to_xfer = qcc->rfctl.md - qcc->tx.offsets;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100833
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100834 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200835 goto out;
836
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200837 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200838
839 out:
840 {
841 struct qcs_xfer_data_trace_arg arg = {
842 .prep = b_data(out), .xfer = total,
843 };
844 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
845 qcc->conn, qcs, &arg);
846 }
847
848 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200849}
850
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200851/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
852 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
853 * stream frame.
854 *
855 * Returns the length of the STREAM frame or a negative error code.
856 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200857static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
858 struct list *frm_list)
859{
860 struct qcc *qcc = qcs->qcc;
861 struct quic_frame *frm;
862 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200863 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200864
865 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
866
Amaury Denoyellea4569202022-04-15 17:29:25 +0200867 /* if ack_offset < buf_offset, it points to an older buffer. */
868 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
869 BUG_ON(qcs->tx.sent_offset < base_off);
870
871 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200872 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200873 BUG_ON(total < 0);
874
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200875 if (!total) {
876 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
877 return 0;
878 }
Amaury Denoyellea4569202022-04-15 17:29:25 +0200879 BUG_ON(qcs->tx.sent_offset >= qcs->tx.offset);
880 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +0200881 BUG_ON(qcc->tx.sent_offsets + total > qcc->rfctl.md);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200882
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200883 frm = pool_zalloc(pool_head_quic_frame);
884 if (!frm)
885 goto err;
886
Frédéric Lécailleb9171912022-04-21 17:32:10 +0200887 LIST_INIT(&frm->reflist);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100888 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200889 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200890 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100891 frm->stream.buf = out;
892 frm->stream.data = (unsigned char *)b_peek(out, head);
893
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100894 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200895 if (fin)
896 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100897
898 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200899 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100900 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200901 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100902
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200903 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
904 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200905
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100906 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100907
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200908 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100909 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200910 struct qcs_build_stream_trace_arg arg = {
911 .len = frm->stream.len, .fin = fin,
912 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100913 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200914 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100915 qcc->conn, qcs, &arg);
916 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100917
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200918 return total;
919
920 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100921 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200922 return -1;
923}
924
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100925/* This function must be called by the upper layer to inform about the sending
926 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
927 * <offset>.
928 */
929void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
930{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100931 struct qcc *qcc = qcs->qcc;
932 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100933
934 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +0200935 BUG_ON(offset + data > qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100936
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100937 /* check if the STREAM frame has already been notified. It can happen
938 * for retransmission.
939 */
940 if (offset + data <= qcs->tx.sent_offset)
941 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100942
943 diff = offset + data - qcs->tx.sent_offset;
944
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100945 /* increase offset sum on connection */
946 qcc->tx.sent_offsets += diff;
947 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
948 if (qcc->tx.sent_offsets == qcc->rfctl.md)
949 qcc->flags |= QC_CF_BLK_MFCTL;
950
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100951 /* increase offset on stream */
952 qcs->tx.sent_offset += diff;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100953 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200954 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100955 if (qcs->tx.sent_offset == qcs->tx.msd)
956 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200957
958 if (qcs->tx.offset == qcs->tx.sent_offset && b_full(&qcs->stream->buf->buf)) {
959 qc_stream_buf_release(qcs->stream);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200960 /* prepare qcs for immediate send retry if data to send */
961 if (b_data(&qcs->tx.buf))
962 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200963 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100964}
965
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100966/* Wrapper for send on transport layer. Send a list of frames <frms> for the
967 * connection <qcc>.
968 *
969 * Returns 0 if all data sent with success else non-zero.
970 */
971static int qc_send_frames(struct qcc *qcc, struct list *frms)
972{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100973 /* TODO implement an opportunistic retry mechanism. This is needed
974 * because qc_send_app_pkts is not completed. It will only prepare data
975 * up to its Tx buffer. The frames left are not send even if the Tx
976 * buffer is emptied by the sendto call.
977 *
978 * To overcome this, we call repeatedly qc_send_app_pkts until we
979 * detect that the transport layer has send nothing. This could happen
980 * on congestion or sendto syscall error.
981 *
982 * When qc_send_app_pkts is improved to handle retry by itself, we can
983 * remove the looping from the MUX.
984 */
985 struct quic_frame *first_frm;
986 uint64_t first_offset = 0;
987 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100988
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100989 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
990
991 if (LIST_ISEMPTY(frms)) {
992 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200993 return 1;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100994 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100995
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200996 LIST_INIT(&qcc->send_retry_list);
997
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100998 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100999 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
1000 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
1001 first_offset = first_frm->stream.offset.key;
1002 first_stream_frame_type = 1;
1003 }
1004 else {
1005 first_stream_frame_type = 0;
1006 }
1007
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001008 if (!LIST_ISEMPTY(frms))
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02001009 qc_send_app_pkts(qcc->conn->handle.qc, 0, frms);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001010
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001011 /* If there is frames left, check if the transport layer has send some
1012 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001013 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001014 if (!LIST_ISEMPTY(frms)) {
1015 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
1016 goto retry_send;
1017
1018 /* If the first frame is STREAM, check if its offset has
1019 * changed.
1020 */
1021 if (first_stream_frame_type &&
1022 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
1023 goto retry_send;
1024 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001025 }
1026
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001027 /* If there is frames left at this stage, transport layer is blocked.
1028 * Subscribe on it to retry later.
1029 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001030 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001031 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001032 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1033 SUB_RETRY_SEND, &qcc->wait_event);
1034 return 1;
1035 }
1036
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001037 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1038
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001039 return 0;
1040}
1041
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001042/* Used internally by qc_send function. Proceed to send for <qcs>. This will
1043 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001044 * is then generated and inserted in <frms> list.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001045 *
1046 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
1047 * be null if out buffer cannot be allocated.
1048 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001049static int _qc_send_qcs(struct qcs *qcs, struct list *frms)
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001050{
1051 struct qcc *qcc = qcs->qcc;
1052 struct buffer *buf = &qcs->tx.buf;
1053 struct buffer *out = qc_stream_buf_get(qcs->stream);
1054 int xfer = 0;
1055
1056 /* Allocate <out> buffer if necessary. */
1057 if (!out) {
1058 if (qcc->flags & QC_CF_CONN_FULL)
1059 return 0;
1060
1061 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
1062 if (!out) {
1063 qcc->flags |= QC_CF_CONN_FULL;
1064 return 0;
1065 }
1066 }
1067
1068 /* Transfer data from <buf> to <out>. */
1069 if (b_data(buf)) {
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001070 xfer = qcs_xfer_data(qcs, out, buf);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001071 if (xfer > 0) {
1072 qcs_notify_send(qcs);
1073 qcs->flags &= ~QC_SF_BLK_MROOM;
1074 }
1075
1076 qcs->tx.offset += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001077 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001078 qcc->tx.offsets += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001079 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001080 }
1081
1082 /* out buffer cannot be emptied if qcs offsets differ. */
1083 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1084
1085 /* Build a new STREAM frame with <out> buffer. */
1086 if (qcs->tx.sent_offset != qcs->tx.offset) {
1087 int ret;
1088 char fin = !!(qcs->flags & QC_SF_FIN_STREAM);
1089
1090 /* FIN is set if all incoming data were transfered. */
1091 fin = !!(fin && !b_data(buf));
1092
1093 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +02001094 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001095 }
1096
1097 return xfer;
1098}
1099
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001100/* Proceed to sending. Loop through all available streams for the <qcc>
1101 * instance and try to send as much as possible.
1102 *
1103 * Returns the total of bytes sent to the transport layer.
1104 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001105static int qc_send(struct qcc *qcc)
1106{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001107 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001108 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001109 struct qcs *qcs, *qcs_tmp;
1110 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001111
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001112 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001113
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001114 if (qcc->conn->flags & CO_FL_SOCK_WR_SH || qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001115 qcc->conn->flags |= CO_FL_ERROR;
1116 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
1117 return 0;
1118 }
1119
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001120 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1121 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
1122 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
1123 goto out;
1124 }
1125 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02001126
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001127 if (qcc->flags & QC_CF_BLK_MFCTL)
1128 return 0;
1129
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001130 /* loop through all streams, construct STREAM frames if data available.
1131 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001132 */
1133 node = eb64_first(&qcc->streams_by_id);
1134 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001135 int ret;
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001136 uint64_t id;
1137
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001138 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001139 id = qcs->id;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001140
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001141 if (quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001142 node = eb64_next(node);
1143 continue;
1144 }
1145
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001146 if (qcs->flags & QC_SF_BLK_SFCTL) {
1147 node = eb64_next(node);
1148 continue;
1149 }
1150
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001151 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001152 node = eb64_next(node);
1153 continue;
1154 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001155
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001156 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001157 total += ret;
1158 node = eb64_next(node);
1159 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001160
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001161 if (qc_send_frames(qcc, &frms)) {
1162 /* data rejected by transport layer, do not retry. */
1163 goto out;
1164 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001165
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001166 retry:
1167 tmp_total = 0;
1168 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
1169 int ret;
1170 BUG_ON(!b_data(&qcs->tx.buf));
1171 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001172
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001173 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001174 tmp_total += ret;
1175 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001176 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001177
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001178 total += tmp_total;
1179 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
1180 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +01001181
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001182 out:
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02001183 /* Deallocate frames that the transport layer has rejected. */
1184 if (!LIST_ISEMPTY(&frms)) {
1185 struct quic_frame *frm, *frm2;
1186 list_for_each_entry_safe(frm, frm2, &frms, list) {
1187 LIST_DELETE(&frm->list);
1188 pool_free(pool_head_quic_frame, frm);
1189 }
1190 }
1191
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001192 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1193
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001194 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001195}
1196
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001197/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1198 * operation.
1199 *
1200 * Returns 0 on success else non-zero.
1201 */
1202static int qc_recv(struct qcc *qcc)
1203{
1204 struct eb64_node *node;
1205 struct qcs *qcs;
1206
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001207 TRACE_ENTER(QMUX_EV_QCC_RECV);
1208
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001209 if (qcc->flags & QC_CF_CC_EMIT) {
1210 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
1211 return 0;
1212 }
1213
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001214 node = eb64_first(&qcc->streams_by_id);
1215 while (node) {
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001216 uint64_t id;
1217
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001218 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001219 id = qcs->id;
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001220
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001221 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001222 node = eb64_next(node);
1223 continue;
1224 }
1225
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001226 if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001227 node = eb64_next(node);
1228 continue;
1229 }
1230
1231 qcc_decode_qcs(qcc, qcs);
1232 node = eb64_next(node);
Amaury Denoyelle849b24f2022-05-24 17:22:07 +02001233
1234 if (qcs->flags & QC_SF_READ_ABORTED) {
1235 /* TODO should send a STOP_SENDING */
1236 qcs_free(qcs);
1237 }
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001238 }
1239
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001240 TRACE_LEAVE(QMUX_EV_QCC_RECV);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001241 return 0;
1242}
1243
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001244/* Release all streams that are already marked as detached. This is only done
1245 * if their TX buffers are empty or if a CONNECTION_CLOSE has been received.
1246 *
1247 * Return the number of released stream.
1248 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001249static int qc_release_detached_streams(struct qcc *qcc)
1250{
1251 struct eb64_node *node;
1252 int release = 0;
1253
1254 node = eb64_first(&qcc->streams_by_id);
1255 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001256 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001257 node = eb64_next(node);
1258
1259 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001260 if (!b_data(&qcs->tx.buf) &&
1261 qcs->tx.offset == qcs->tx.sent_offset) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001262 qcs_destroy(qcs);
1263 release = 1;
1264 }
1265 else {
1266 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1267 SUB_RETRY_SEND, &qcc->wait_event);
1268 }
1269 }
1270 }
1271
1272 return release;
1273}
1274
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001275static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1276{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001277 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001278
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001279 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001280
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001281 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001282
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001283 if (qc_release_detached_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001284 if (qcc_is_dead(qcc)) {
1285 qc_release(qcc);
1286 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001287 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001288 if (qcc_may_expire(qcc))
1289 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1290 else
1291 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001292 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001293 }
1294 }
1295
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001296 qc_recv(qcc);
1297
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001298 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1299
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001300 return NULL;
1301}
1302
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001303static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1304{
1305 struct qcc *qcc = ctx;
1306 int expired = tick_is_expired(t->expire, now_ms);
1307
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001308 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001309
1310 if (qcc) {
1311 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001312 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001313 return t;
1314 }
1315
1316 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001317 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001318 t->expire = TICK_ETERNITY;
1319 return t;
1320 }
1321 }
1322
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001323 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001324
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001325 if (!qcc) {
1326 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001327 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001328 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001329
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001330 qcc->task = NULL;
1331
1332 if (qcc_is_dead(qcc))
1333 qc_release(qcc);
1334
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001335 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1336
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001337 return NULL;
1338}
1339
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001340static int qc_init(struct connection *conn, struct proxy *prx,
1341 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001342{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001343 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001344 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001345
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001346 qcc = pool_alloc(pool_head_qcc);
1347 if (!qcc)
1348 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001349
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001350 qcc->conn = conn;
1351 conn->ctx = qcc;
Willy Tarreau3215e732022-05-27 10:09:11 +02001352 qcc->nb_sc = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001353 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001354
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001355 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001356
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001357 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001358
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001359 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001360 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001361
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001362 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001363 qcc->tx.sent_offsets = qcc->tx.offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001364
1365 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001366 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001367 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
1368 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001369 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001370 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001371
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001372 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001373 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
1374 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001375 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001376 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001377
1378 /* Server initiated streams must respect the server flow control. */
1379 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001380 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
1381 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001382 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001383 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1384
1385 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001386 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
1387 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001388 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001389 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001390
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001391 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001392 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02001393 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
1394 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001395 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001396
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001397 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001398 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001399
Willy Tarreau784b8682022-04-11 14:18:10 +02001400 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001401 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001402 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1403 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1404
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001405 qcc->wait_event.tasklet = tasklet_new();
1406 if (!qcc->wait_event.tasklet)
1407 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001408
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001409 LIST_INIT(&qcc->send_retry_list);
1410
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001411 qcc->subs = NULL;
1412 qcc->wait_event.tasklet->process = qc_io_cb;
1413 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001414 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001415
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001416 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001417 qcc->task = NULL;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001418 qcc->timeout = prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001419 if (tick_isset(qcc->timeout)) {
1420 qcc->task = task_new_here();
1421 if (!qcc->task)
1422 goto fail_no_timeout_task;
1423 qcc->task->process = qc_timeout_task;
1424 qcc->task->context = qcc;
1425 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1426 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001427
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001428 if (!conn_is_back(conn)) {
1429 if (!LIST_INLIST(&conn->stopping_list)) {
1430 LIST_APPEND(&mux_stopping_data[tid].list,
1431 &conn->stopping_list);
1432 }
1433 }
1434
Willy Tarreau784b8682022-04-11 14:18:10 +02001435 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001436 /* init read cycle */
1437 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001438
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001439 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001440
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001441 fail_no_timeout_task:
1442 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001443 fail_no_tasklet:
1444 pool_free(pool_head_qcc, qcc);
1445 fail_no_qcc:
1446 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001447}
1448
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001449static void qc_destroy(void *ctx)
1450{
1451 struct qcc *qcc = ctx;
1452
1453 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
1454 qc_release(qcc);
1455 TRACE_LEAVE(QMUX_EV_QCC_END);
1456}
1457
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001458static void qc_detach(struct sedesc *sd)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001459{
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001460 struct qcs *qcs = sd->se;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001461 struct qcc *qcc = qcs->qcc;
1462
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001463 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001464
Willy Tarreau3215e732022-05-27 10:09:11 +02001465 --qcc->nb_sc;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001466
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001467 if ((b_data(&qcs->tx.buf) || qcs->tx.offset > qcs->tx.sent_offset) &&
1468 !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001469 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001470 qcs->flags |= QC_SF_DETACH;
1471 return;
1472 }
1473
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001474 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001475
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001476 if (qcc_is_dead(qcc)) {
1477 qc_release(qcc);
1478 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001479 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001480 if (qcc_may_expire(qcc))
1481 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1482 else
1483 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001484 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001485 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001486
1487 TRACE_LEAVE(QMUX_EV_STRM_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001488}
1489
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001490/* Called from the upper layer, to receive data */
Willy Tarreau3215e732022-05-27 10:09:11 +02001491static size_t qc_rcv_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001492 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001493{
Willy Tarreau3215e732022-05-27 10:09:11 +02001494 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001495 struct htx *qcs_htx = NULL;
1496 struct htx *cs_htx = NULL;
1497 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001498 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001499
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001500 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001501
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001502 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
1503 if (htx_is_empty(qcs_htx)) {
1504 /* Set buffer data to 0 as HTX is empty. */
1505 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1506 goto end;
1507 }
1508
1509 ret = qcs_htx->data;
1510
1511 cs_htx = htx_from_buf(buf);
1512 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
1513 htx_to_buf(cs_htx, buf);
1514 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1515 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
1516 goto end;
1517 }
1518
1519 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
1520 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
1521
1522 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001523 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
1524 cs_htx->flags |= HTX_FL_EOM;
1525 fin = 1;
1526 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001527
1528 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
1529 htx_to_buf(cs_htx, buf);
1530 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1531 ret -= qcs_htx->data;
1532
1533 end:
1534 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001535 se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001536 }
1537 else {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001538 se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
1539 if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING))
1540 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001541
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001542 if (fin)
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001543 se_fl_set(qcs->sd, SE_FL_EOI);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001544
1545 if (b_size(&qcs->rx.app_buf)) {
1546 b_free(&qcs->rx.app_buf);
1547 offer_buffers(NULL, 1);
1548 }
1549 }
1550
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001551 if (ret) {
1552 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001553 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001554 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001555
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001556 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
1557
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001558 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001559}
1560
Willy Tarreau3215e732022-05-27 10:09:11 +02001561static size_t qc_snd_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001562 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001563{
Willy Tarreau3215e732022-05-27 10:09:11 +02001564 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001565 size_t ret;
1566
1567 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001568
Willy Tarreau3215e732022-05-27 10:09:11 +02001569 ret = qcs->qcc->app_ops->snd_buf(sc, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001570
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001571 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
1572
1573 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001574}
1575
1576/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1577 * event subscriber <es> is not allowed to change from a previous call as long
1578 * as at least one event is still subscribed. The <event_type> must only be a
1579 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1580 */
Willy Tarreau3215e732022-05-27 10:09:11 +02001581static int qc_subscribe(struct stconn *sc, int event_type,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001582 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001583{
Willy Tarreau3215e732022-05-27 10:09:11 +02001584 return qcs_subscribe(__sc_mux_strm(sc), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001585}
1586
1587/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1588 * The <es> pointer is not allowed to differ from the one passed to the
1589 * subscribe() call. It always returns zero.
1590 */
Willy Tarreau3215e732022-05-27 10:09:11 +02001591static int qc_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001592{
Willy Tarreau3215e732022-05-27 10:09:11 +02001593 struct qcs *qcs = __sc_mux_strm(sc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001594
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001595 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1596 BUG_ON(qcs->subs && qcs->subs != es);
1597
1598 es->events &= ~event_type;
1599 if (!es->events)
1600 qcs->subs = NULL;
1601
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001602 return 0;
1603}
1604
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001605/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Willy Tarreau4596fe22022-05-17 19:07:51 +02001606 * report SE_FL_ERR_PENDING|SE_FL_ERROR on the attached stream connectors and
1607 * wake them.
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001608 */
1609static int qc_wake_some_streams(struct qcc *qcc)
1610{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001611 struct qcs *qcs;
1612 struct eb64_node *node;
1613
1614 for (node = eb64_first(&qcc->streams_by_id); node;
1615 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001616 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001617
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001618 if (!qcs->sd || !qcs->sd->sc)
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001619 continue;
1620
1621 if (qcc->conn->flags & CO_FL_ERROR) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001622 se_fl_set(qcs->sd, SE_FL_ERR_PENDING);
1623 if (se_fl_test(qcs->sd, SE_FL_EOS))
1624 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001625
1626 if (qcs->subs) {
1627 qcs_notify_recv(qcs);
1628 qcs_notify_send(qcs);
1629 }
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001630 else if (qcs->sd->sc->app_ops->wake) {
1631 qcs->sd->sc->app_ops->wake(qcs->sd->sc);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001632 }
1633 }
1634 }
1635
1636 return 0;
1637}
1638
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001639static int qc_wake(struct connection *conn)
1640{
1641 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02001642 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001643
1644 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001645
1646 /* Check if a soft-stop is in progress.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001647 *
1648 * TODO this is revelant for frontend connections only.
Amaury Denoyelled0c62322022-05-23 08:52:58 +02001649 *
1650 * TODO Client should be notified with a H3 GOAWAY and then a
1651 * CONNECTION_CLOSE. However, quic-conn uses the listener socket for
1652 * sending which at this stage is already closed.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001653 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001654 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Amaury Denoyelled0c62322022-05-23 08:52:58 +02001655 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001656
Willy Tarreau784b8682022-04-11 14:18:10 +02001657 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02001658 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
1659
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001660 qc_send(qcc);
1661
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001662 qc_wake_some_streams(qcc);
1663
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001664 if (qcc_is_dead(qcc))
1665 goto release;
1666
1667 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
1668
1669 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001670
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001671 release:
1672 qc_release(qcc);
1673 TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001674 return 1;
1675}
1676
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001677
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001678static void qmux_trace_frm(const struct quic_frame *frm)
1679{
1680 switch (frm->type) {
1681 case QUIC_FT_MAX_STREAMS_BIDI:
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02001682 chunk_appendf(&trace_buf, " max_streams=%llu",
1683 (ull)frm->max_streams_bidi.max_streams);
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001684 break;
1685
1686 case QUIC_FT_MAX_STREAMS_UNI:
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02001687 chunk_appendf(&trace_buf, " max_streams=%llu",
1688 (ull)frm->max_streams_uni.max_streams);
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001689 break;
1690
1691 default:
1692 break;
1693 }
1694}
1695
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001696/* quic-mux trace handler */
1697static void qmux_trace(enum trace_level level, uint64_t mask,
1698 const struct trace_source *src,
1699 const struct ist where, const struct ist func,
1700 const void *a1, const void *a2, const void *a3, const void *a4)
1701{
1702 const struct connection *conn = a1;
1703 const struct qcc *qcc = conn ? conn->ctx : NULL;
1704 const struct qcs *qcs = a2;
1705
1706 if (!qcc)
1707 return;
1708
1709 if (src->verbosity > QMUX_VERB_CLEAN) {
1710 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
1711
1712 if (qcs)
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02001713 chunk_appendf(&trace_buf, " qcs=%p(%llu)", qcs, (ull)qcs->id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001714
1715 if (mask & QMUX_EV_QCC_NQCS) {
1716 const uint64_t *id = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02001717 chunk_appendf(&trace_buf, " id=%llu", (ull)*id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001718 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001719
1720 if (mask & QMUX_EV_SEND_FRM)
1721 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001722
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001723 if (mask & QMUX_EV_QCS_XFER_DATA) {
1724 const struct qcs_xfer_data_trace_arg *arg = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02001725 chunk_appendf(&trace_buf, " prep=%llu xfer=%d",
1726 (ull)arg->prep, arg->xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001727 }
1728
1729 if (mask & QMUX_EV_QCS_BUILD_STRM) {
1730 const struct qcs_build_stream_trace_arg *arg = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02001731 chunk_appendf(&trace_buf, " len=%llu fin=%d offset=%llu",
1732 (ull)arg->len, arg->fin, (ull)arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001733 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001734 }
1735}
1736
1737
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001738static const struct mux_ops qc_ops = {
1739 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001740 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001741 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001742 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001743 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001744 .subscribe = qc_subscribe,
1745 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001746 .wake = qc_wake,
Willy Tarreaub5821e12022-04-26 11:54:08 +02001747 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02001748 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001749};
1750
1751static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001752 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001753
1754INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);