blob: 85fb620bfc52a14fcaeec266afb91096cdb3022a [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 Denoyelle62eef852022-06-03 16:40:34 +0200442
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200443 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
444
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200445 b = qcs_b_dup(&qcs->rx.ncbuf);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200446 ret = qcc->app_ops->decode_qcs(qcs, &b, qcs->flags & QC_SF_FIN_RECV);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200447 if (ret < 0) {
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200448 TRACE_DEVEL("leaving on decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
449 return 1;
450 }
451
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200452 if (ret) {
453 qcs_consume(qcs, ret);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200454 qcs_notify_recv(qcs);
455 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200456
457 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
458
459 return 0;
460}
461
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200462/* Emit a CONNECTION_CLOSE_APP with error <err>. Reserved for application error
463 * code. This will interrupt all future send/receive operations.
464 */
465void qcc_emit_cc_app(struct qcc *qcc, int err)
466{
467 quic_set_connection_close(qcc->conn->handle.qc, err, 1);
468 qcc->flags |= QC_CF_CC_EMIT;
469 tasklet_wakeup(qcc->wait_event.tasklet);
470}
471
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200472/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
473 * <data> with length <len> and represents the offset <offset>. <fin> is set if
474 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100475 *
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200476 * Returns 0 on success else non-zero.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100477 */
478int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200479 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100480{
481 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200482 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100483
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100484 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
485
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200486 if (qcc->flags & QC_CF_CC_EMIT) {
487 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
488 return 0;
489 }
490
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200491 /* RFC 9000 19.8. STREAM Frames
492 *
493 * An endpoint MUST terminate the connection with error
494 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
495 * initiated stream that has not yet been created, or for a send-only
496 * stream.
497 */
498 if (quic_stream_is_local(qcc, id) && quic_stream_is_uni(id)) {
499 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
500 TRACE_DEVEL("leaving on invalid reception for a send-only stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
501 return 1;
502 }
503
Amaury Denoyelle50742292022-03-29 14:57:19 +0200504 qcs = qcc_get_qcs(qcc, id);
505 if (!qcs) {
Frédéric Lécailleb57de072022-05-02 18:58:27 +0200506 if ((id >> QCS_ID_TYPE_SHIFT) <= qcc->strms[qcs_id_type(id)].largest_id) {
507 TRACE_DEVEL("already released stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
508 return 0;
509 }
510 else {
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200511 /* RFC 9000 19.8. STREAM Frames
512 *
513 * An endpoint MUST terminate the connection with error
514 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
515 * initiated stream that has not yet been created, or for a send-only
516 * stream.
517 */
518 if (quic_stream_is_local(qcc, id)) {
519 TRACE_DEVEL("leaving on locally initiated stream not yet created", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
520 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
521 return 1;
522 }
523 else {
524 TRACE_DEVEL("leaving on stream not found", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
525 return 1;
526 }
Frédéric Lécailleb57de072022-05-02 18:58:27 +0200527 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100528 }
529
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100530 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100531 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 +0200532 return 0;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100533 }
534
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200535 /* TODO if last frame already received, stream size must not change.
536 * Else send FINAL_SIZE_ERROR.
537 */
538
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200539 if (offset + len > qcs->rx.offset_max) {
540 uint64_t diff = offset + len - qcs->rx.offset_max;
541 qcs->rx.offset_max = offset + len;
542 qcc->lfctl.offsets_recv += diff;
543
544 if (offset + len > qcs->rx.msd ||
545 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
546 /* RFC 9000 4.1. Data Flow Control
547 *
548 * A receiver MUST close the connection with an error
549 * of type FLOW_CONTROL_ERROR if the sender violates
550 * the advertised connection or stream data limits
551 */
552 TRACE_DEVEL("leaving on flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
553 qcc->conn, qcs);
554 qcc_emit_cc(qcc, QC_ERR_FLOW_CONTROL_ERROR);
555 return 1;
556 }
557 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100558
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200559 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100560 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200561 ABORT_NOW();
562 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100563 }
564
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100565 TRACE_DEVEL("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200566 if (offset < qcs->rx.offset) {
567 len -= qcs->rx.offset - offset;
568 offset = qcs->rx.offset;
569 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100570
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200571 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
572 if (ret != NCB_RET_OK) {
573 if (ret == NCB_RET_DATA_REJ) {
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200574 /* RFC 9000 2.2. Sending and Receiving Data
575 *
576 * An endpoint could receive data for a stream at the
577 * same stream offset multiple times. Data that has
578 * already been received can be discarded. The data at
579 * a given offset MUST NOT change if it is sent
580 * multiple times; an endpoint MAY treat receipt of
581 * different data at the same offset within a stream as
582 * a connection error of type PROTOCOL_VIOLATION.
583 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200584 TRACE_DEVEL("leaving on data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
585 qcc->conn, qcs);
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200586 qcc_emit_cc(qcc, QC_ERR_PROTOCOL_VIOLATION);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200587 }
588 else if (ret == NCB_RET_GAP_SIZE) {
589 TRACE_DEVEL("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
590 qcc->conn, qcs);
591 }
592 return 1;
593 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100594
595 if (fin)
596 qcs->flags |= QC_SF_FIN_RECV;
597
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200598 if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL))
599 qcc_decode_qcs(qcc, qcs);
600
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200601 if (qcs->flags & QC_SF_READ_ABORTED) {
602 /* TODO should send a STOP_SENDING */
603 qcs_free(qcs);
604 }
605
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100606 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100607 return 0;
608}
609
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100610/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
611 * the frame.
612 *
613 * Returns 0 on success else non-zero.
614 */
615int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
616{
617 if (qcc->rfctl.md < max) {
618 qcc->rfctl.md = max;
619
620 if (qcc->flags & QC_CF_BLK_MFCTL) {
621 qcc->flags &= ~QC_CF_BLK_MFCTL;
622 tasklet_wakeup(qcc->wait_event.tasklet);
623 }
624 }
625 return 0;
626}
627
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100628/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
629 * field of the frame and <id> is the identifier of the QUIC stream.
630 *
631 * Returns 0 on success else non-zero.
632 */
633int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
634{
635 struct qcs *qcs;
636 struct eb64_node *node;
637
638 node = eb64_lookup(&qcc->streams_by_id, id);
639 if (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200640 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100641 if (max > qcs->tx.msd) {
642 qcs->tx.msd = max;
643
644 if (qcs->flags & QC_SF_BLK_SFCTL) {
645 qcs->flags &= ~QC_SF_BLK_SFCTL;
646 tasklet_wakeup(qcc->wait_event.tasklet);
647 }
648 }
649 }
650
651 return 0;
652}
653
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200654/* Signal the closing of remote stream with id <id>. Flow-control for new
655 * streams may be allocated for the peer if needed.
656 */
657static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +0100658{
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200659 struct quic_frame *frm;
660
661 if (quic_stream_is_bidi(id)) {
662 ++qcc->lfctl.cl_bidi_r;
663 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
664 frm = pool_zalloc(pool_head_quic_frame);
665 BUG_ON(!frm); /* TODO handle this properly */
666
667 LIST_INIT(&frm->reflist);
668 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
669 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
670 qcc->lfctl.cl_bidi_r;
671 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
672 tasklet_wakeup(qcc->wait_event.tasklet);
673
674 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
675 qcc->lfctl.cl_bidi_r = 0;
676 }
677 }
678 else {
679 /* TODO */
680 }
681
682 return 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100683}
684
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500685/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100686static void qcs_destroy(struct qcs *qcs)
687{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100688 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200689 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100690
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100691 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100692
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200693 if (quic_stream_is_remote(qcs->qcc, id))
694 qcc_release_remote_stream(qcs->qcc, id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100695
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200696 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100697
698 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100699}
700
701static inline int qcc_is_dead(const struct qcc *qcc)
702{
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200703 if (qcc->app_ops && qcc->app_ops->is_active &&
704 qcc->app_ops->is_active(qcc, qcc->ctx))
705 return 0;
706
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200707 if ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task)
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100708 return 1;
709
710 return 0;
711}
712
713/* Return true if the mux timeout should be armed. */
714static inline int qcc_may_expire(struct qcc *qcc)
715{
Willy Tarreau3215e732022-05-27 10:09:11 +0200716 return !qcc->nb_sc;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100717}
718
719/* release function. This one should be called to free all resources allocated
720 * to the mux.
721 */
722static void qc_release(struct qcc *qcc)
723{
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200724 struct connection *conn = qcc->conn;
725 struct eb64_node *node;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100726
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100727 TRACE_ENTER(QMUX_EV_QCC_END);
728
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200729 if (qcc->app_ops && qcc->app_ops->release)
730 qcc->app_ops->release(qcc->ctx);
Amaury Denoyellef8909452022-03-30 11:51:56 +0200731
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200732 if (qcc->task) {
733 task_destroy(qcc->task);
734 qcc->task = NULL;
735 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100736
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200737 if (qcc->wait_event.tasklet)
738 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyellef3e03a42022-04-21 15:41:34 +0200739 if (conn && qcc->wait_event.events) {
740 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
741 qcc->wait_event.events,
742 &qcc->wait_event);
743 }
Amaury Denoyellef8909452022-03-30 11:51:56 +0200744
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200745 /* liberate remaining qcs instances */
746 node = eb64_first(&qcc->streams_by_id);
747 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200748 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200749 node = eb64_next(node);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200750 qcs_free(qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100751 }
752
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200753 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
Amaury Denoyelle040955f2022-06-13 11:30:46 +0200754 struct quic_frame *frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list);
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200755 LIST_DELETE(&frm->list);
756 pool_free(pool_head_quic_frame, frm);
757 }
758
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200759 pool_free(pool_head_qcc, qcc);
760
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100761 if (conn) {
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100762 LIST_DEL_INIT(&conn->stopping_list);
763
Willy Tarreau784b8682022-04-11 14:18:10 +0200764 conn->handle.qc->conn = NULL;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100765 conn->mux = NULL;
766 conn->ctx = NULL;
767
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100768 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
769
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100770 conn_stop_tracking(conn);
771 conn_full_close(conn);
772 if (conn->destroy_cb)
773 conn->destroy_cb(conn);
774 conn_free(conn);
775 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100776
777 TRACE_LEAVE(QMUX_EV_QCC_END);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100778}
779
Amaury Denoyelleb9e06402022-06-10 15:16:21 +0200780/* Transfer as much as possible data on <qcs> from <in> to <out>. This is done
781 * in respect with available flow-control at stream and connection level.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100782 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200783 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100784 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +0200785static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, struct buffer *in)
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200786{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100787 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200788 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100789 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200790
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100791 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100792
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100793 qc_get_buf(qcs, out);
794
795 /*
796 * QCS out buffer diagram
797 * head left to_xfer
798 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100799 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100800 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100801 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100802 * ^ ack-off ^ sent-off ^ off
803 *
804 * STREAM frame
805 * ^ ^
806 * |xxxxxxxxxxxxxxxxx|
807 */
808
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200809 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100810 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +0200811 BUG_ON_HOT(qcc->tx.offsets < qcc->tx.sent_offsets);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100812
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100813 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200814 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100815
816 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
817 /* do not exceed flow control limit */
818 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
819 to_xfer = qcs->tx.msd - qcs->tx.offset;
820
Amaury Denoyelleb9e06402022-06-10 15:16:21 +0200821 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100822 /* do not overcome flow control limit on connection */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +0200823 if (qcc->tx.offsets + to_xfer > qcc->rfctl.md)
824 to_xfer = qcc->rfctl.md - qcc->tx.offsets;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100825
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100826 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200827 goto out;
828
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200829 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200830
831 out:
832 {
833 struct qcs_xfer_data_trace_arg arg = {
834 .prep = b_data(out), .xfer = total,
835 };
836 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
837 qcc->conn, qcs, &arg);
838 }
839
840 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200841}
842
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200843/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
844 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
845 * stream frame.
846 *
847 * Returns the length of the STREAM frame or a negative error code.
848 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200849static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
850 struct list *frm_list)
851{
852 struct qcc *qcc = qcs->qcc;
853 struct quic_frame *frm;
854 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200855 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200856
857 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
858
Amaury Denoyellea4569202022-04-15 17:29:25 +0200859 /* if ack_offset < buf_offset, it points to an older buffer. */
860 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
861 BUG_ON(qcs->tx.sent_offset < base_off);
862
863 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200864 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200865 BUG_ON(total < 0);
866
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200867 if (!total) {
868 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
869 return 0;
870 }
Amaury Denoyellea4569202022-04-15 17:29:25 +0200871 BUG_ON(qcs->tx.sent_offset >= qcs->tx.offset);
872 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +0200873 BUG_ON(qcc->tx.sent_offsets + total > qcc->rfctl.md);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200874
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200875 frm = pool_zalloc(pool_head_quic_frame);
876 if (!frm)
877 goto err;
878
Frédéric Lécailleb9171912022-04-21 17:32:10 +0200879 LIST_INIT(&frm->reflist);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100880 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200881 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200882 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100883 frm->stream.buf = out;
884 frm->stream.data = (unsigned char *)b_peek(out, head);
885
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100886 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200887 if (fin)
888 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100889
890 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200891 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100892 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200893 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100894
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200895 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
896 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200897
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100898 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100899
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200900 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100901 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200902 struct qcs_build_stream_trace_arg arg = {
903 .len = frm->stream.len, .fin = fin,
904 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100905 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200906 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100907 qcc->conn, qcs, &arg);
908 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100909
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200910 return total;
911
912 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100913 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200914 return -1;
915}
916
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100917/* This function must be called by the upper layer to inform about the sending
918 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
919 * <offset>.
920 */
921void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
922{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100923 struct qcc *qcc = qcs->qcc;
924 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100925
926 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +0200927 BUG_ON(offset + data > qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100928
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100929 /* check if the STREAM frame has already been notified. It can happen
930 * for retransmission.
931 */
932 if (offset + data <= qcs->tx.sent_offset)
933 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100934
935 diff = offset + data - qcs->tx.sent_offset;
936
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100937 /* increase offset sum on connection */
938 qcc->tx.sent_offsets += diff;
939 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
940 if (qcc->tx.sent_offsets == qcc->rfctl.md)
941 qcc->flags |= QC_CF_BLK_MFCTL;
942
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100943 /* increase offset on stream */
944 qcs->tx.sent_offset += diff;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100945 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200946 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100947 if (qcs->tx.sent_offset == qcs->tx.msd)
948 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200949
950 if (qcs->tx.offset == qcs->tx.sent_offset && b_full(&qcs->stream->buf->buf)) {
951 qc_stream_buf_release(qcs->stream);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200952 /* prepare qcs for immediate send retry if data to send */
953 if (b_data(&qcs->tx.buf))
954 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200955 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100956}
957
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100958/* Wrapper for send on transport layer. Send a list of frames <frms> for the
959 * connection <qcc>.
960 *
961 * Returns 0 if all data sent with success else non-zero.
962 */
963static int qc_send_frames(struct qcc *qcc, struct list *frms)
964{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100965 /* TODO implement an opportunistic retry mechanism. This is needed
966 * because qc_send_app_pkts is not completed. It will only prepare data
967 * up to its Tx buffer. The frames left are not send even if the Tx
968 * buffer is emptied by the sendto call.
969 *
970 * To overcome this, we call repeatedly qc_send_app_pkts until we
971 * detect that the transport layer has send nothing. This could happen
972 * on congestion or sendto syscall error.
973 *
974 * When qc_send_app_pkts is improved to handle retry by itself, we can
975 * remove the looping from the MUX.
976 */
977 struct quic_frame *first_frm;
978 uint64_t first_offset = 0;
979 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100980
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100981 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
982
983 if (LIST_ISEMPTY(frms)) {
984 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200985 return 1;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100986 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100987
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200988 LIST_INIT(&qcc->send_retry_list);
989
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100990 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100991 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
992 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
993 first_offset = first_frm->stream.offset.key;
994 first_stream_frame_type = 1;
995 }
996 else {
997 first_stream_frame_type = 0;
998 }
999
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001000 if (!LIST_ISEMPTY(frms))
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02001001 qc_send_app_pkts(qcc->conn->handle.qc, 0, frms);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001002
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001003 /* If there is frames left, check if the transport layer has send some
1004 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001005 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001006 if (!LIST_ISEMPTY(frms)) {
1007 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
1008 goto retry_send;
1009
1010 /* If the first frame is STREAM, check if its offset has
1011 * changed.
1012 */
1013 if (first_stream_frame_type &&
1014 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
1015 goto retry_send;
1016 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001017 }
1018
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001019 /* If there is frames left at this stage, transport layer is blocked.
1020 * Subscribe on it to retry later.
1021 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001022 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001023 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001024 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1025 SUB_RETRY_SEND, &qcc->wait_event);
1026 return 1;
1027 }
1028
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001029 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1030
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001031 return 0;
1032}
1033
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001034/* Used internally by qc_send function. Proceed to send for <qcs>. This will
1035 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001036 * is then generated and inserted in <frms> list.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001037 *
1038 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
1039 * be null if out buffer cannot be allocated.
1040 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001041static int _qc_send_qcs(struct qcs *qcs, struct list *frms)
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001042{
1043 struct qcc *qcc = qcs->qcc;
1044 struct buffer *buf = &qcs->tx.buf;
1045 struct buffer *out = qc_stream_buf_get(qcs->stream);
1046 int xfer = 0;
1047
1048 /* Allocate <out> buffer if necessary. */
1049 if (!out) {
1050 if (qcc->flags & QC_CF_CONN_FULL)
1051 return 0;
1052
1053 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
1054 if (!out) {
1055 qcc->flags |= QC_CF_CONN_FULL;
1056 return 0;
1057 }
1058 }
1059
1060 /* Transfer data from <buf> to <out>. */
1061 if (b_data(buf)) {
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001062 xfer = qcs_xfer_data(qcs, out, buf);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001063 if (xfer > 0) {
1064 qcs_notify_send(qcs);
1065 qcs->flags &= ~QC_SF_BLK_MROOM;
1066 }
1067
1068 qcs->tx.offset += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001069 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001070 qcc->tx.offsets += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001071 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001072 }
1073
1074 /* out buffer cannot be emptied if qcs offsets differ. */
1075 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1076
1077 /* Build a new STREAM frame with <out> buffer. */
1078 if (qcs->tx.sent_offset != qcs->tx.offset) {
1079 int ret;
1080 char fin = !!(qcs->flags & QC_SF_FIN_STREAM);
1081
1082 /* FIN is set if all incoming data were transfered. */
1083 fin = !!(fin && !b_data(buf));
1084
1085 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +02001086 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001087 }
1088
1089 return xfer;
1090}
1091
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001092/* Proceed to sending. Loop through all available streams for the <qcc>
1093 * instance and try to send as much as possible.
1094 *
1095 * Returns the total of bytes sent to the transport layer.
1096 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001097static int qc_send(struct qcc *qcc)
1098{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001099 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001100 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001101 struct qcs *qcs, *qcs_tmp;
1102 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001103
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001104 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001105
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001106 if (qcc->conn->flags & CO_FL_SOCK_WR_SH || qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001107 qcc->conn->flags |= CO_FL_ERROR;
1108 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
1109 return 0;
1110 }
1111
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001112 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1113 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
1114 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
1115 goto out;
1116 }
1117 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02001118
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001119 if (qcc->flags & QC_CF_BLK_MFCTL)
1120 return 0;
1121
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001122 /* loop through all streams, construct STREAM frames if data available.
1123 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001124 */
1125 node = eb64_first(&qcc->streams_by_id);
1126 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001127 int ret;
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001128 uint64_t id;
1129
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001130 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001131 id = qcs->id;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001132
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001133 if (quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001134 node = eb64_next(node);
1135 continue;
1136 }
1137
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001138 if (qcs->flags & QC_SF_BLK_SFCTL) {
1139 node = eb64_next(node);
1140 continue;
1141 }
1142
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001143 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001144 node = eb64_next(node);
1145 continue;
1146 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001147
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001148 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001149 total += ret;
1150 node = eb64_next(node);
1151 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001152
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001153 if (qc_send_frames(qcc, &frms)) {
1154 /* data rejected by transport layer, do not retry. */
1155 goto out;
1156 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001157
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001158 retry:
1159 tmp_total = 0;
1160 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
1161 int ret;
1162 BUG_ON(!b_data(&qcs->tx.buf));
1163 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001164
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001165 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001166 tmp_total += ret;
1167 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001168 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001169
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001170 total += tmp_total;
1171 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
1172 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +01001173
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001174 out:
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02001175 /* Deallocate frames that the transport layer has rejected. */
1176 if (!LIST_ISEMPTY(&frms)) {
1177 struct quic_frame *frm, *frm2;
1178 list_for_each_entry_safe(frm, frm2, &frms, list) {
1179 LIST_DELETE(&frm->list);
1180 pool_free(pool_head_quic_frame, frm);
1181 }
1182 }
1183
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001184 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1185
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001186 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001187}
1188
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001189/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1190 * operation.
1191 *
1192 * Returns 0 on success else non-zero.
1193 */
1194static int qc_recv(struct qcc *qcc)
1195{
1196 struct eb64_node *node;
1197 struct qcs *qcs;
1198
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001199 TRACE_ENTER(QMUX_EV_QCC_RECV);
1200
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001201 if (qcc->flags & QC_CF_CC_EMIT) {
1202 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
1203 return 0;
1204 }
1205
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001206 node = eb64_first(&qcc->streams_by_id);
1207 while (node) {
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001208 uint64_t id;
1209
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001210 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001211 id = qcs->id;
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001212
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001213 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001214 node = eb64_next(node);
1215 continue;
1216 }
1217
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001218 if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001219 node = eb64_next(node);
1220 continue;
1221 }
1222
1223 qcc_decode_qcs(qcc, qcs);
1224 node = eb64_next(node);
Amaury Denoyelle849b24f2022-05-24 17:22:07 +02001225
1226 if (qcs->flags & QC_SF_READ_ABORTED) {
1227 /* TODO should send a STOP_SENDING */
1228 qcs_free(qcs);
1229 }
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001230 }
1231
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001232 TRACE_LEAVE(QMUX_EV_QCC_RECV);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001233 return 0;
1234}
1235
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001236/* Release all streams that are already marked as detached. This is only done
1237 * if their TX buffers are empty or if a CONNECTION_CLOSE has been received.
1238 *
1239 * Return the number of released stream.
1240 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001241static int qc_release_detached_streams(struct qcc *qcc)
1242{
1243 struct eb64_node *node;
1244 int release = 0;
1245
1246 node = eb64_first(&qcc->streams_by_id);
1247 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001248 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001249 node = eb64_next(node);
1250
1251 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001252 if (!b_data(&qcs->tx.buf) &&
1253 qcs->tx.offset == qcs->tx.sent_offset) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001254 qcs_destroy(qcs);
1255 release = 1;
1256 }
1257 else {
1258 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1259 SUB_RETRY_SEND, &qcc->wait_event);
1260 }
1261 }
1262 }
1263
1264 return release;
1265}
1266
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001267static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1268{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001269 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001270
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001271 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001272
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001273 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001274
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001275 if (qc_release_detached_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001276 if (qcc_is_dead(qcc)) {
1277 qc_release(qcc);
1278 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001279 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001280 if (qcc_may_expire(qcc))
1281 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1282 else
1283 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001284 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001285 }
1286 }
1287
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001288 qc_recv(qcc);
1289
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001290 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1291
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001292 return NULL;
1293}
1294
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001295static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1296{
1297 struct qcc *qcc = ctx;
1298 int expired = tick_is_expired(t->expire, now_ms);
1299
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001300 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001301
1302 if (qcc) {
1303 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001304 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001305 return t;
1306 }
1307
1308 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001309 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001310 t->expire = TICK_ETERNITY;
1311 return t;
1312 }
1313 }
1314
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001315 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001316
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001317 if (!qcc) {
1318 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001319 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001320 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001321
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001322 qcc->task = NULL;
1323
1324 if (qcc_is_dead(qcc))
1325 qc_release(qcc);
1326
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001327 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1328
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001329 return NULL;
1330}
1331
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001332static int qc_init(struct connection *conn, struct proxy *prx,
1333 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001334{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001335 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001336 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001337
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001338 qcc = pool_alloc(pool_head_qcc);
1339 if (!qcc)
1340 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001341
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001342 qcc->conn = conn;
1343 conn->ctx = qcc;
Willy Tarreau3215e732022-05-27 10:09:11 +02001344 qcc->nb_sc = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001345 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001346
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001347 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001348
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001349 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001350
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001351 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001352 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001353
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001354 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001355 qcc->tx.sent_offsets = qcc->tx.offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001356
1357 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001358 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001359 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
1360 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001361 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001362 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001363
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001364 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001365 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
1366 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001367 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001368 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001369
1370 /* Server initiated streams must respect the server flow control. */
1371 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001372 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
1373 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001374 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001375 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1376
1377 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001378 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
1379 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001380 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001381 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001382
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001383 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001384 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02001385 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
1386 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001387 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001388
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001389 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001390 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001391
Willy Tarreau784b8682022-04-11 14:18:10 +02001392 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001393 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001394 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1395 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1396
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001397 qcc->wait_event.tasklet = tasklet_new();
1398 if (!qcc->wait_event.tasklet)
1399 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001400
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001401 LIST_INIT(&qcc->send_retry_list);
1402
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001403 qcc->subs = NULL;
1404 qcc->wait_event.tasklet->process = qc_io_cb;
1405 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001406 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001407
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001408 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001409 qcc->task = NULL;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001410 qcc->timeout = prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001411 if (tick_isset(qcc->timeout)) {
1412 qcc->task = task_new_here();
1413 if (!qcc->task)
1414 goto fail_no_timeout_task;
1415 qcc->task->process = qc_timeout_task;
1416 qcc->task->context = qcc;
1417 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1418 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001419
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001420 if (!conn_is_back(conn)) {
1421 if (!LIST_INLIST(&conn->stopping_list)) {
1422 LIST_APPEND(&mux_stopping_data[tid].list,
1423 &conn->stopping_list);
1424 }
1425 }
1426
Willy Tarreau784b8682022-04-11 14:18:10 +02001427 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001428 /* init read cycle */
1429 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001430
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001431 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001432
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001433 fail_no_timeout_task:
1434 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001435 fail_no_tasklet:
1436 pool_free(pool_head_qcc, qcc);
1437 fail_no_qcc:
1438 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001439}
1440
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001441static void qc_destroy(void *ctx)
1442{
1443 struct qcc *qcc = ctx;
1444
1445 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
1446 qc_release(qcc);
1447 TRACE_LEAVE(QMUX_EV_QCC_END);
1448}
1449
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001450static void qc_detach(struct sedesc *sd)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001451{
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001452 struct qcs *qcs = sd->se;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001453 struct qcc *qcc = qcs->qcc;
1454
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001455 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001456
Willy Tarreau3215e732022-05-27 10:09:11 +02001457 --qcc->nb_sc;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001458
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001459 if ((b_data(&qcs->tx.buf) || qcs->tx.offset > qcs->tx.sent_offset) &&
1460 !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001461 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001462 qcs->flags |= QC_SF_DETACH;
1463 return;
1464 }
1465
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001466 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001467
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001468 if (qcc_is_dead(qcc)) {
1469 qc_release(qcc);
1470 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001471 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001472 if (qcc_may_expire(qcc))
1473 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1474 else
1475 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001476 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001477 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001478
1479 TRACE_LEAVE(QMUX_EV_STRM_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001480}
1481
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001482/* Called from the upper layer, to receive data */
Willy Tarreau3215e732022-05-27 10:09:11 +02001483static size_t qc_rcv_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001484 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001485{
Willy Tarreau3215e732022-05-27 10:09:11 +02001486 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001487 struct htx *qcs_htx = NULL;
1488 struct htx *cs_htx = NULL;
1489 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001490 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001491
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001492 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001493
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001494 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
1495 if (htx_is_empty(qcs_htx)) {
1496 /* Set buffer data to 0 as HTX is empty. */
1497 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1498 goto end;
1499 }
1500
1501 ret = qcs_htx->data;
1502
1503 cs_htx = htx_from_buf(buf);
1504 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
1505 htx_to_buf(cs_htx, buf);
1506 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1507 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
1508 goto end;
1509 }
1510
1511 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
1512 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
1513
1514 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001515 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
1516 cs_htx->flags |= HTX_FL_EOM;
1517 fin = 1;
1518 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001519
1520 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
1521 htx_to_buf(cs_htx, buf);
1522 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1523 ret -= qcs_htx->data;
1524
1525 end:
1526 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001527 se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001528 }
1529 else {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001530 se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
1531 if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING))
1532 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001533
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001534 if (fin)
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001535 se_fl_set(qcs->sd, SE_FL_EOI);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001536
1537 if (b_size(&qcs->rx.app_buf)) {
1538 b_free(&qcs->rx.app_buf);
1539 offer_buffers(NULL, 1);
1540 }
1541 }
1542
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001543 if (ret) {
1544 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001545 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001546 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001547
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001548 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
1549
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001550 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001551}
1552
Willy Tarreau3215e732022-05-27 10:09:11 +02001553static size_t qc_snd_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001554 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001555{
Willy Tarreau3215e732022-05-27 10:09:11 +02001556 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001557 size_t ret;
1558
1559 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001560
Willy Tarreau3215e732022-05-27 10:09:11 +02001561 ret = qcs->qcc->app_ops->snd_buf(sc, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001562
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001563 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
1564
1565 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001566}
1567
1568/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1569 * event subscriber <es> is not allowed to change from a previous call as long
1570 * as at least one event is still subscribed. The <event_type> must only be a
1571 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1572 */
Willy Tarreau3215e732022-05-27 10:09:11 +02001573static int qc_subscribe(struct stconn *sc, int event_type,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001574 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001575{
Willy Tarreau3215e732022-05-27 10:09:11 +02001576 return qcs_subscribe(__sc_mux_strm(sc), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001577}
1578
1579/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1580 * The <es> pointer is not allowed to differ from the one passed to the
1581 * subscribe() call. It always returns zero.
1582 */
Willy Tarreau3215e732022-05-27 10:09:11 +02001583static int qc_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001584{
Willy Tarreau3215e732022-05-27 10:09:11 +02001585 struct qcs *qcs = __sc_mux_strm(sc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001586
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001587 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1588 BUG_ON(qcs->subs && qcs->subs != es);
1589
1590 es->events &= ~event_type;
1591 if (!es->events)
1592 qcs->subs = NULL;
1593
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001594 return 0;
1595}
1596
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001597/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Willy Tarreau4596fe22022-05-17 19:07:51 +02001598 * report SE_FL_ERR_PENDING|SE_FL_ERROR on the attached stream connectors and
1599 * wake them.
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001600 */
1601static int qc_wake_some_streams(struct qcc *qcc)
1602{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001603 struct qcs *qcs;
1604 struct eb64_node *node;
1605
1606 for (node = eb64_first(&qcc->streams_by_id); node;
1607 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001608 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001609
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001610 if (!qcs->sd || !qcs->sd->sc)
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001611 continue;
1612
1613 if (qcc->conn->flags & CO_FL_ERROR) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001614 se_fl_set(qcs->sd, SE_FL_ERR_PENDING);
1615 if (se_fl_test(qcs->sd, SE_FL_EOS))
1616 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001617
1618 if (qcs->subs) {
1619 qcs_notify_recv(qcs);
1620 qcs_notify_send(qcs);
1621 }
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001622 else if (qcs->sd->sc->app_ops->wake) {
1623 qcs->sd->sc->app_ops->wake(qcs->sd->sc);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001624 }
1625 }
1626 }
1627
1628 return 0;
1629}
1630
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001631static int qc_wake(struct connection *conn)
1632{
1633 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02001634 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001635
1636 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001637
1638 /* Check if a soft-stop is in progress.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001639 *
1640 * TODO this is revelant for frontend connections only.
Amaury Denoyelled0c62322022-05-23 08:52:58 +02001641 *
1642 * TODO Client should be notified with a H3 GOAWAY and then a
1643 * CONNECTION_CLOSE. However, quic-conn uses the listener socket for
1644 * sending which at this stage is already closed.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001645 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001646 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Amaury Denoyelled0c62322022-05-23 08:52:58 +02001647 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001648
Willy Tarreau784b8682022-04-11 14:18:10 +02001649 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02001650 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
1651
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001652 qc_send(qcc);
1653
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001654 qc_wake_some_streams(qcc);
1655
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001656 if (qcc_is_dead(qcc))
1657 goto release;
1658
1659 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
1660
1661 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001662
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001663 release:
1664 qc_release(qcc);
1665 TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001666 return 1;
1667}
1668
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001669
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001670static void qmux_trace_frm(const struct quic_frame *frm)
1671{
1672 switch (frm->type) {
1673 case QUIC_FT_MAX_STREAMS_BIDI:
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02001674 chunk_appendf(&trace_buf, " max_streams=%llu",
1675 (ull)frm->max_streams_bidi.max_streams);
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001676 break;
1677
1678 case QUIC_FT_MAX_STREAMS_UNI:
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02001679 chunk_appendf(&trace_buf, " max_streams=%llu",
1680 (ull)frm->max_streams_uni.max_streams);
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001681 break;
1682
1683 default:
1684 break;
1685 }
1686}
1687
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001688/* quic-mux trace handler */
1689static void qmux_trace(enum trace_level level, uint64_t mask,
1690 const struct trace_source *src,
1691 const struct ist where, const struct ist func,
1692 const void *a1, const void *a2, const void *a3, const void *a4)
1693{
1694 const struct connection *conn = a1;
1695 const struct qcc *qcc = conn ? conn->ctx : NULL;
1696 const struct qcs *qcs = a2;
1697
1698 if (!qcc)
1699 return;
1700
1701 if (src->verbosity > QMUX_VERB_CLEAN) {
1702 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
1703
1704 if (qcs)
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02001705 chunk_appendf(&trace_buf, " qcs=%p(%llu)", qcs, (ull)qcs->id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001706
1707 if (mask & QMUX_EV_QCC_NQCS) {
1708 const uint64_t *id = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02001709 chunk_appendf(&trace_buf, " id=%llu", (ull)*id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001710 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001711
1712 if (mask & QMUX_EV_SEND_FRM)
1713 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001714
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001715 if (mask & QMUX_EV_QCS_XFER_DATA) {
1716 const struct qcs_xfer_data_trace_arg *arg = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02001717 chunk_appendf(&trace_buf, " prep=%llu xfer=%d",
1718 (ull)arg->prep, arg->xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001719 }
1720
1721 if (mask & QMUX_EV_QCS_BUILD_STRM) {
1722 const struct qcs_build_stream_trace_arg *arg = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02001723 chunk_appendf(&trace_buf, " len=%llu fin=%d offset=%llu",
1724 (ull)arg->len, arg->fin, (ull)arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001725 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001726 }
1727}
1728
1729
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001730static const struct mux_ops qc_ops = {
1731 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001732 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001733 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001734 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001735 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001736 .subscribe = qc_subscribe,
1737 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001738 .wake = qc_wake,
Willy Tarreaub5821e12022-04-26 11:54:08 +02001739 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02001740 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001741};
1742
1743static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001744 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001745
1746INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);