blob: 04ed1bac679982d853c133dd631ada67dc75a2e3 [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) {
142 if (qcc->app_ops->attach(qcs))
143 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 Denoyellea9773552022-05-16 14:38:25 +0200289/* Remove <bytes> from <qcs> Rx buffer. This must be called by transcoders
290 * after STREAM parsing. Flow-control for received offsets may be allocated for
291 * the peer if needed.
292 */
293void qcs_consume(struct qcs *qcs, uint64_t bytes)
294{
295 struct qcc *qcc = qcs->qcc;
296 struct quic_frame *frm;
Amaury Denoyelle8b87b152022-05-18 16:19:47 +0200297 struct ncbuf *buf = &qcs->rx.ncbuf;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200298 enum ncb_ret ret;
299
Amaury Denoyelle8b87b152022-05-18 16:19:47 +0200300 ret = ncb_advance(buf, bytes);
Amaury Denoyellea9773552022-05-16 14:38:25 +0200301 if (ret) {
302 ABORT_NOW(); /* should not happens because removal only in data */
303 }
304
Amaury Denoyelle8b87b152022-05-18 16:19:47 +0200305 if (ncb_is_empty(buf))
306 qc_free_ncbuf(qcs, buf);
307
Amaury Denoyellea9773552022-05-16 14:38:25 +0200308 qcs->rx.offset += bytes;
309 if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) {
310 frm = pool_zalloc(pool_head_quic_frame);
311 BUG_ON(!frm); /* TODO handle this properly */
312
313 qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init;
314
315 LIST_INIT(&frm->reflist);
316 frm->type = QUIC_FT_MAX_STREAM_DATA;
317 frm->max_stream_data.id = qcs->id;
318 frm->max_stream_data.max_stream_data = qcs->rx.msd;
319
320 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
321 tasklet_wakeup(qcc->wait_event.tasklet);
322 }
Amaury Denoyellec830e1e2022-05-16 16:19:59 +0200323
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200324 qcc->lfctl.offsets_consume += bytes;
325 if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) {
Amaury Denoyellec830e1e2022-05-16 16:19:59 +0200326 frm = pool_zalloc(pool_head_quic_frame);
327 BUG_ON(!frm); /* TODO handle this properly */
328
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200329 qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +0200330
331 LIST_INIT(&frm->reflist);
332 frm->type = QUIC_FT_MAX_DATA;
333 frm->max_data.max_data = qcc->lfctl.md;
334
335 LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list);
336 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
337 }
Amaury Denoyellea9773552022-05-16 14:38:25 +0200338}
339
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100340/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
341 * several streams, depending on the already open ones.
342 * Return this node if succeeded, NULL if not.
343 */
Amaury Denoyelle50742292022-03-29 14:57:19 +0200344struct qcs *qcc_get_qcs(struct qcc *qcc, uint64_t id)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100345{
346 unsigned int strm_type;
347 int64_t sub_id;
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200348 struct eb64_node *node;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200349 struct qcs *qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100350
351 strm_type = id & QCS_ID_TYPE_MASK;
352 sub_id = id >> QCS_ID_TYPE_SHIFT;
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200353 node = NULL;
Amaury Denoyelle0dc40f02022-02-07 11:44:17 +0100354 if (quic_stream_is_local(qcc, id)) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100355 /* Local streams: this stream must be already opened. */
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200356 node = eb64_lookup(&qcc->streams_by_id, id);
357 if (!node) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100358 /* unknown stream id */
359 goto out;
360 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200361 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100362 }
363 else {
364 /* Remote streams. */
365 struct eb_root *strms;
366 uint64_t largest_id;
367 enum qcs_type qcs_type;
368
369 strms = &qcc->streams_by_id;
370 qcs_type = qcs_id_type(id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100371
372 /* TODO also checks max-streams for uni streams */
373 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100374 if (sub_id + 1 > qcc->lfctl.ms_bidi) {
Amaury Denoyelle209404b2022-05-20 16:45:32 +0200375 /* RFC 9000 4.6. Controlling Concurrency
376 *
377 * An endpoint that receives a frame with a
378 * stream ID exceeding the limit it has sent
379 * MUST treat this as a connection error of
380 * type STREAM_LIMIT_ERROR
381 */
382 qcc_emit_cc(qcc, QC_ERR_STREAM_LIMIT_ERROR);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100383 goto out;
384 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100385 }
386
387 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
388 * correct value.
389 */
390 largest_id = qcc->strms[qcs_type].largest_id;
391 if (sub_id > (int64_t)largest_id) {
392 /* RFC: "A stream ID that is used out of order results in all streams
393 * of that type with lower-numbered stream IDs also being opened".
394 * So, let's "open" these streams.
395 */
396 int64_t i;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200397 struct qcs *tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100398
Amaury Denoyelle50742292022-03-29 14:57:19 +0200399 tmp_qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100400 for (i = largest_id + 1; i <= sub_id; i++) {
401 uint64_t id = (i << QCS_ID_TYPE_SHIFT) | strm_type;
402 enum qcs_type type = id & QCS_ID_DIR_BIT ? QCS_CLT_UNI : QCS_CLT_BIDI;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200403
Amaury Denoyelle50742292022-03-29 14:57:19 +0200404 tmp_qcs = qcs_new(qcc, id, type);
405 if (!tmp_qcs) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100406 /* allocation failure */
407 goto out;
408 }
409
410 qcc->strms[qcs_type].largest_id = i;
411 }
Amaury Denoyelle50742292022-03-29 14:57:19 +0200412 if (tmp_qcs)
413 qcs = tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100414 }
415 else {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200416 node = eb64_lookup(strms, id);
417 if (node)
418 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100419 }
420 }
421
Amaury Denoyelle50742292022-03-29 14:57:19 +0200422 return qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100423
424 out:
425 return NULL;
426}
427
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200428/* Decode the content of STREAM frames already received on the stream instance
429 * <qcs>.
430 *
431 * Returns 0 on success else non-zero.
432 */
433static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
434{
435 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
436
Amaury Denoyelle0ffd6e72022-05-24 11:07:28 +0200437 if (qcc->app_ops->decode_qcs(qcs, qcs->flags & QC_SF_FIN_RECV, qcc->ctx)) {
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200438 TRACE_DEVEL("leaving on decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
439 return 1;
440 }
441
442 qcs_notify_recv(qcs);
443
444 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
445
446 return 0;
447}
448
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200449/* Emit a CONNECTION_CLOSE_APP with error <err>. Reserved for application error
450 * code. This will interrupt all future send/receive operations.
451 */
452void qcc_emit_cc_app(struct qcc *qcc, int err)
453{
454 quic_set_connection_close(qcc->conn->handle.qc, err, 1);
455 qcc->flags |= QC_CF_CC_EMIT;
456 tasklet_wakeup(qcc->wait_event.tasklet);
457}
458
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200459/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
460 * <data> with length <len> and represents the offset <offset>. <fin> is set if
461 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100462 *
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200463 * Returns 0 on success else non-zero.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100464 */
465int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200466 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100467{
468 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200469 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100470
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100471 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
472
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200473 if (qcc->flags & QC_CF_CC_EMIT) {
474 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
475 return 0;
476 }
477
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200478 /* RFC 9000 19.8. STREAM Frames
479 *
480 * An endpoint MUST terminate the connection with error
481 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
482 * initiated stream that has not yet been created, or for a send-only
483 * stream.
484 */
485 if (quic_stream_is_local(qcc, id) && quic_stream_is_uni(id)) {
486 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
487 TRACE_DEVEL("leaving on invalid reception for a send-only stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
488 return 1;
489 }
490
Amaury Denoyelle50742292022-03-29 14:57:19 +0200491 qcs = qcc_get_qcs(qcc, id);
492 if (!qcs) {
Frédéric Lécailleb57de072022-05-02 18:58:27 +0200493 if ((id >> QCS_ID_TYPE_SHIFT) <= qcc->strms[qcs_id_type(id)].largest_id) {
494 TRACE_DEVEL("already released stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
495 return 0;
496 }
497 else {
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200498 /* RFC 9000 19.8. STREAM Frames
499 *
500 * An endpoint MUST terminate the connection with error
501 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
502 * initiated stream that has not yet been created, or for a send-only
503 * stream.
504 */
505 if (quic_stream_is_local(qcc, id)) {
506 TRACE_DEVEL("leaving on locally initiated stream not yet created", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
507 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
508 return 1;
509 }
510 else {
511 TRACE_DEVEL("leaving on stream not found", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
512 return 1;
513 }
Frédéric Lécailleb57de072022-05-02 18:58:27 +0200514 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100515 }
516
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100517 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100518 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 +0200519 return 0;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100520 }
521
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200522 /* TODO if last frame already received, stream size must not change.
523 * Else send FINAL_SIZE_ERROR.
524 */
525
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200526 if (offset + len > qcs->rx.offset_max) {
527 uint64_t diff = offset + len - qcs->rx.offset_max;
528 qcs->rx.offset_max = offset + len;
529 qcc->lfctl.offsets_recv += diff;
530
531 if (offset + len > qcs->rx.msd ||
532 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
533 /* RFC 9000 4.1. Data Flow Control
534 *
535 * A receiver MUST close the connection with an error
536 * of type FLOW_CONTROL_ERROR if the sender violates
537 * the advertised connection or stream data limits
538 */
539 TRACE_DEVEL("leaving on flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
540 qcc->conn, qcs);
541 qcc_emit_cc(qcc, QC_ERR_FLOW_CONTROL_ERROR);
542 return 1;
543 }
544 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100545
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200546 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100547 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200548 ABORT_NOW();
549 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100550 }
551
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100552 TRACE_DEVEL("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200553 if (offset < qcs->rx.offset) {
554 len -= qcs->rx.offset - offset;
555 offset = qcs->rx.offset;
556 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100557
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200558 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
559 if (ret != NCB_RET_OK) {
560 if (ret == NCB_RET_DATA_REJ) {
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200561 /* RFC 9000 2.2. Sending and Receiving Data
562 *
563 * An endpoint could receive data for a stream at the
564 * same stream offset multiple times. Data that has
565 * already been received can be discarded. The data at
566 * a given offset MUST NOT change if it is sent
567 * multiple times; an endpoint MAY treat receipt of
568 * different data at the same offset within a stream as
569 * a connection error of type PROTOCOL_VIOLATION.
570 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200571 TRACE_DEVEL("leaving on data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
572 qcc->conn, qcs);
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200573 qcc_emit_cc(qcc, QC_ERR_PROTOCOL_VIOLATION);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200574 }
575 else if (ret == NCB_RET_GAP_SIZE) {
576 TRACE_DEVEL("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
577 qcc->conn, qcs);
578 }
579 return 1;
580 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100581
582 if (fin)
583 qcs->flags |= QC_SF_FIN_RECV;
584
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200585 if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL))
586 qcc_decode_qcs(qcc, qcs);
587
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200588 if (qcs->flags & QC_SF_READ_ABORTED) {
589 /* TODO should send a STOP_SENDING */
590 qcs_free(qcs);
591 }
592
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100593 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100594 return 0;
595}
596
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100597/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
598 * the frame.
599 *
600 * Returns 0 on success else non-zero.
601 */
602int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
603{
604 if (qcc->rfctl.md < max) {
605 qcc->rfctl.md = max;
606
607 if (qcc->flags & QC_CF_BLK_MFCTL) {
608 qcc->flags &= ~QC_CF_BLK_MFCTL;
609 tasklet_wakeup(qcc->wait_event.tasklet);
610 }
611 }
612 return 0;
613}
614
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100615/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
616 * field of the frame and <id> is the identifier of the QUIC stream.
617 *
618 * Returns 0 on success else non-zero.
619 */
620int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
621{
622 struct qcs *qcs;
623 struct eb64_node *node;
624
625 node = eb64_lookup(&qcc->streams_by_id, id);
626 if (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200627 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100628 if (max > qcs->tx.msd) {
629 qcs->tx.msd = max;
630
631 if (qcs->flags & QC_SF_BLK_SFCTL) {
632 qcs->flags &= ~QC_SF_BLK_SFCTL;
633 tasklet_wakeup(qcc->wait_event.tasklet);
634 }
635 }
636 }
637
638 return 0;
639}
640
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200641/* Signal the closing of remote stream with id <id>. Flow-control for new
642 * streams may be allocated for the peer if needed.
643 */
644static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +0100645{
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200646 struct quic_frame *frm;
647
648 if (quic_stream_is_bidi(id)) {
649 ++qcc->lfctl.cl_bidi_r;
650 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
651 frm = pool_zalloc(pool_head_quic_frame);
652 BUG_ON(!frm); /* TODO handle this properly */
653
654 LIST_INIT(&frm->reflist);
655 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
656 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
657 qcc->lfctl.cl_bidi_r;
658 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
659 tasklet_wakeup(qcc->wait_event.tasklet);
660
661 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
662 qcc->lfctl.cl_bidi_r = 0;
663 }
664 }
665 else {
666 /* TODO */
667 }
668
669 return 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100670}
671
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500672/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100673static void qcs_destroy(struct qcs *qcs)
674{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100675 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200676 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100677
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100678 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100679
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200680 if (quic_stream_is_remote(qcs->qcc, id))
681 qcc_release_remote_stream(qcs->qcc, id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100682
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200683 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100684
685 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100686}
687
688static inline int qcc_is_dead(const struct qcc *qcc)
689{
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200690 if (qcc->app_ops && qcc->app_ops->is_active &&
691 qcc->app_ops->is_active(qcc, qcc->ctx))
692 return 0;
693
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200694 if ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task)
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100695 return 1;
696
697 return 0;
698}
699
700/* Return true if the mux timeout should be armed. */
701static inline int qcc_may_expire(struct qcc *qcc)
702{
Willy Tarreau3215e732022-05-27 10:09:11 +0200703 return !qcc->nb_sc;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100704}
705
706/* release function. This one should be called to free all resources allocated
707 * to the mux.
708 */
709static void qc_release(struct qcc *qcc)
710{
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200711 struct connection *conn = qcc->conn;
712 struct eb64_node *node;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100713
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100714 TRACE_ENTER(QMUX_EV_QCC_END);
715
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200716 if (qcc->app_ops && qcc->app_ops->release)
717 qcc->app_ops->release(qcc->ctx);
Amaury Denoyellef8909452022-03-30 11:51:56 +0200718
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200719 if (qcc->task) {
720 task_destroy(qcc->task);
721 qcc->task = NULL;
722 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100723
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200724 if (qcc->wait_event.tasklet)
725 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyellef3e03a42022-04-21 15:41:34 +0200726 if (conn && qcc->wait_event.events) {
727 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
728 qcc->wait_event.events,
729 &qcc->wait_event);
730 }
Amaury Denoyellef8909452022-03-30 11:51:56 +0200731
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200732 /* liberate remaining qcs instances */
733 node = eb64_first(&qcc->streams_by_id);
734 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200735 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200736 node = eb64_next(node);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200737 qcs_free(qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100738 }
739
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200740 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
741 struct quic_frame *frm = LIST_ELEM(&qcc->lfctl.frms, struct quic_frame *, list);
742 LIST_DELETE(&frm->list);
743 pool_free(pool_head_quic_frame, frm);
744 }
745
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200746 pool_free(pool_head_qcc, qcc);
747
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100748 if (conn) {
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100749 LIST_DEL_INIT(&conn->stopping_list);
750
Willy Tarreau784b8682022-04-11 14:18:10 +0200751 conn->handle.qc->conn = NULL;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100752 conn->mux = NULL;
753 conn->ctx = NULL;
754
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100755 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
756
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100757 conn_stop_tracking(conn);
758 conn_full_close(conn);
759 if (conn->destroy_cb)
760 conn->destroy_cb(conn);
761 conn_free(conn);
762 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100763
764 TRACE_LEAVE(QMUX_EV_QCC_END);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100765}
766
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200767/* Transfer as much as possible data on <qcs> from <in> to <out>. <max_data> is
768 * the current flow-control limit on the connection which must not be exceeded.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100769 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200770 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100771 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200772static int qcs_xfer_data(struct qcs *qcs, struct buffer *out,
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200773 struct buffer *in, uint64_t max_data)
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200774{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100775 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200776 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100777 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200778
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100779 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100780
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100781 qc_get_buf(qcs, out);
782
783 /*
784 * QCS out buffer diagram
785 * head left to_xfer
786 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100787 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100788 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100789 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100790 * ^ ack-off ^ sent-off ^ off
791 *
792 * STREAM frame
793 * ^ ^
794 * |xxxxxxxxxxxxxxxxx|
795 */
796
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200797 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100798 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
799
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100800 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200801 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100802
803 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
804 /* do not exceed flow control limit */
805 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
806 to_xfer = qcs->tx.msd - qcs->tx.offset;
807
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100808 BUG_ON_HOT(max_data > qcc->rfctl.md);
809 /* do not overcome flow control limit on connection */
810 if (max_data + to_xfer > qcc->rfctl.md)
811 to_xfer = qcc->rfctl.md - max_data;
812
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100813 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200814 goto out;
815
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200816 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200817
818 out:
819 {
820 struct qcs_xfer_data_trace_arg arg = {
821 .prep = b_data(out), .xfer = total,
822 };
823 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
824 qcc->conn, qcs, &arg);
825 }
826
827 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200828}
829
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200830/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
831 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
832 * stream frame.
833 *
834 * Returns the length of the STREAM frame or a negative error code.
835 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200836static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
837 struct list *frm_list)
838{
839 struct qcc *qcc = qcs->qcc;
840 struct quic_frame *frm;
841 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200842 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200843
844 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
845
Amaury Denoyellea4569202022-04-15 17:29:25 +0200846 /* if ack_offset < buf_offset, it points to an older buffer. */
847 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
848 BUG_ON(qcs->tx.sent_offset < base_off);
849
850 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200851 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200852 BUG_ON(total < 0);
853
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200854 if (!total) {
855 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
856 return 0;
857 }
Amaury Denoyellea4569202022-04-15 17:29:25 +0200858 BUG_ON(qcs->tx.sent_offset >= qcs->tx.offset);
859 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200860
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200861 frm = pool_zalloc(pool_head_quic_frame);
862 if (!frm)
863 goto err;
864
Frédéric Lécailleb9171912022-04-21 17:32:10 +0200865 LIST_INIT(&frm->reflist);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100866 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200867 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200868 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100869 frm->stream.buf = out;
870 frm->stream.data = (unsigned char *)b_peek(out, head);
871
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100872 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200873 if (fin)
874 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100875
876 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200877 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100878 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200879 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100880
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200881 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
882 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200883
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100884 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100885
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200886 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100887 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200888 struct qcs_build_stream_trace_arg arg = {
889 .len = frm->stream.len, .fin = fin,
890 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100891 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200892 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100893 qcc->conn, qcs, &arg);
894 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100895
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200896 return total;
897
898 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100899 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200900 return -1;
901}
902
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100903/* This function must be called by the upper layer to inform about the sending
904 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
905 * <offset>.
906 */
907void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
908{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100909 struct qcc *qcc = qcs->qcc;
910 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100911
912 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200913 BUG_ON(offset >= qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100914
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100915 /* check if the STREAM frame has already been notified. It can happen
916 * for retransmission.
917 */
918 if (offset + data <= qcs->tx.sent_offset)
919 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100920
921 diff = offset + data - qcs->tx.sent_offset;
922
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100923 /* increase offset sum on connection */
924 qcc->tx.sent_offsets += diff;
925 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
926 if (qcc->tx.sent_offsets == qcc->rfctl.md)
927 qcc->flags |= QC_CF_BLK_MFCTL;
928
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100929 /* increase offset on stream */
930 qcs->tx.sent_offset += diff;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100931 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200932 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100933 if (qcs->tx.sent_offset == qcs->tx.msd)
934 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200935
936 if (qcs->tx.offset == qcs->tx.sent_offset && b_full(&qcs->stream->buf->buf)) {
937 qc_stream_buf_release(qcs->stream);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200938 /* prepare qcs for immediate send retry if data to send */
939 if (b_data(&qcs->tx.buf))
940 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200941 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100942}
943
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100944/* Wrapper for send on transport layer. Send a list of frames <frms> for the
945 * connection <qcc>.
946 *
947 * Returns 0 if all data sent with success else non-zero.
948 */
949static int qc_send_frames(struct qcc *qcc, struct list *frms)
950{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100951 /* TODO implement an opportunistic retry mechanism. This is needed
952 * because qc_send_app_pkts is not completed. It will only prepare data
953 * up to its Tx buffer. The frames left are not send even if the Tx
954 * buffer is emptied by the sendto call.
955 *
956 * To overcome this, we call repeatedly qc_send_app_pkts until we
957 * detect that the transport layer has send nothing. This could happen
958 * on congestion or sendto syscall error.
959 *
960 * When qc_send_app_pkts is improved to handle retry by itself, we can
961 * remove the looping from the MUX.
962 */
963 struct quic_frame *first_frm;
964 uint64_t first_offset = 0;
965 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100966
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100967 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
968
969 if (LIST_ISEMPTY(frms)) {
970 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200971 return 1;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100972 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100973
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200974 LIST_INIT(&qcc->send_retry_list);
975
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100976 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100977 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
978 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
979 first_offset = first_frm->stream.offset.key;
980 first_stream_frame_type = 1;
981 }
982 else {
983 first_stream_frame_type = 0;
984 }
985
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100986 if (!LIST_ISEMPTY(frms))
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +0200987 qc_send_app_pkts(qcc->conn->handle.qc, 0, frms);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100988
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100989 /* If there is frames left, check if the transport layer has send some
990 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100991 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100992 if (!LIST_ISEMPTY(frms)) {
993 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
994 goto retry_send;
995
996 /* If the first frame is STREAM, check if its offset has
997 * changed.
998 */
999 if (first_stream_frame_type &&
1000 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
1001 goto retry_send;
1002 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001003 }
1004
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001005 /* If there is frames left at this stage, transport layer is blocked.
1006 * Subscribe on it to retry later.
1007 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001008 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001009 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001010 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1011 SUB_RETRY_SEND, &qcc->wait_event);
1012 return 1;
1013 }
1014
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001015 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1016
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001017 return 0;
1018}
1019
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001020/* Used internally by qc_send function. Proceed to send for <qcs>. This will
1021 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
1022 * is then generated and inserted in <frms> list. <qcc_max_data> is the current
1023 * flow-control max-data at the connection level which must not be surpassed.
1024 *
1025 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
1026 * be null if out buffer cannot be allocated.
1027 */
1028static int _qc_send_qcs(struct qcs *qcs, struct list *frms,
1029 uint64_t qcc_max_data)
1030{
1031 struct qcc *qcc = qcs->qcc;
1032 struct buffer *buf = &qcs->tx.buf;
1033 struct buffer *out = qc_stream_buf_get(qcs->stream);
1034 int xfer = 0;
1035
1036 /* Allocate <out> buffer if necessary. */
1037 if (!out) {
1038 if (qcc->flags & QC_CF_CONN_FULL)
1039 return 0;
1040
1041 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
1042 if (!out) {
1043 qcc->flags |= QC_CF_CONN_FULL;
1044 return 0;
1045 }
1046 }
1047
1048 /* Transfer data from <buf> to <out>. */
1049 if (b_data(buf)) {
1050 xfer = qcs_xfer_data(qcs, out, buf, qcc_max_data);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001051 if (xfer > 0) {
1052 qcs_notify_send(qcs);
1053 qcs->flags &= ~QC_SF_BLK_MROOM;
1054 }
1055
1056 qcs->tx.offset += xfer;
1057 }
1058
1059 /* out buffer cannot be emptied if qcs offsets differ. */
1060 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1061
1062 /* Build a new STREAM frame with <out> buffer. */
1063 if (qcs->tx.sent_offset != qcs->tx.offset) {
1064 int ret;
1065 char fin = !!(qcs->flags & QC_SF_FIN_STREAM);
1066
1067 /* FIN is set if all incoming data were transfered. */
1068 fin = !!(fin && !b_data(buf));
1069
1070 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +02001071 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001072 }
1073
1074 return xfer;
1075}
1076
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001077/* Proceed to sending. Loop through all available streams for the <qcc>
1078 * instance and try to send as much as possible.
1079 *
1080 * Returns the total of bytes sent to the transport layer.
1081 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001082static int qc_send(struct qcc *qcc)
1083{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001084 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001085 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001086 struct qcs *qcs, *qcs_tmp;
1087 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001088
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001089 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001090
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001091 if (qcc->conn->flags & CO_FL_SOCK_WR_SH || qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001092 qcc->conn->flags |= CO_FL_ERROR;
1093 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
1094 return 0;
1095 }
1096
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001097 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1098 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
1099 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
1100 goto out;
1101 }
1102 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02001103
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001104 if (qcc->flags & QC_CF_BLK_MFCTL)
1105 return 0;
1106
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001107 /* loop through all streams, construct STREAM frames if data available.
1108 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001109 */
1110 node = eb64_first(&qcc->streams_by_id);
1111 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001112 int ret;
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001113 uint64_t id;
1114
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001115 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001116 id = qcs->id;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001117
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001118 if (quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001119 node = eb64_next(node);
1120 continue;
1121 }
1122
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001123 if (qcs->flags & QC_SF_BLK_SFCTL) {
1124 node = eb64_next(node);
1125 continue;
1126 }
1127
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001128 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001129 node = eb64_next(node);
1130 continue;
1131 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001132
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001133 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + total);
1134 total += ret;
1135 node = eb64_next(node);
1136 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001137
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001138 if (qc_send_frames(qcc, &frms)) {
1139 /* data rejected by transport layer, do not retry. */
1140 goto out;
1141 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001142
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001143 retry:
1144 tmp_total = 0;
1145 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
1146 int ret;
1147 BUG_ON(!b_data(&qcs->tx.buf));
1148 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001149
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001150 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + tmp_total);
1151 tmp_total += ret;
1152 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001153 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001154
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001155 total += tmp_total;
1156 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
1157 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +01001158
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001159 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001160 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1161
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001162 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001163}
1164
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001165/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1166 * operation.
1167 *
1168 * Returns 0 on success else non-zero.
1169 */
1170static int qc_recv(struct qcc *qcc)
1171{
1172 struct eb64_node *node;
1173 struct qcs *qcs;
1174
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001175 TRACE_ENTER(QMUX_EV_QCC_RECV);
1176
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001177 if (qcc->flags & QC_CF_CC_EMIT) {
1178 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
1179 return 0;
1180 }
1181
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001182 node = eb64_first(&qcc->streams_by_id);
1183 while (node) {
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001184 uint64_t id;
1185
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001186 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001187 id = qcs->id;
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001188
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001189 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001190 node = eb64_next(node);
1191 continue;
1192 }
1193
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001194 if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001195 node = eb64_next(node);
1196 continue;
1197 }
1198
1199 qcc_decode_qcs(qcc, qcs);
1200 node = eb64_next(node);
Amaury Denoyelle849b24f2022-05-24 17:22:07 +02001201
1202 if (qcs->flags & QC_SF_READ_ABORTED) {
1203 /* TODO should send a STOP_SENDING */
1204 qcs_free(qcs);
1205 }
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001206 }
1207
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001208 TRACE_LEAVE(QMUX_EV_QCC_RECV);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001209 return 0;
1210}
1211
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001212/* Release all streams that are already marked as detached. This is only done
1213 * if their TX buffers are empty or if a CONNECTION_CLOSE has been received.
1214 *
1215 * Return the number of released stream.
1216 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001217static int qc_release_detached_streams(struct qcc *qcc)
1218{
1219 struct eb64_node *node;
1220 int release = 0;
1221
1222 node = eb64_first(&qcc->streams_by_id);
1223 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001224 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001225 node = eb64_next(node);
1226
1227 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001228 if (!b_data(&qcs->tx.buf) &&
1229 qcs->tx.offset == qcs->tx.sent_offset) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001230 qcs_destroy(qcs);
1231 release = 1;
1232 }
1233 else {
1234 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1235 SUB_RETRY_SEND, &qcc->wait_event);
1236 }
1237 }
1238 }
1239
1240 return release;
1241}
1242
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001243static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1244{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001245 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001246
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001247 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001248
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001249 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001250
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001251 if (qc_release_detached_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001252 if (qcc_is_dead(qcc)) {
1253 qc_release(qcc);
1254 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001255 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001256 if (qcc_may_expire(qcc))
1257 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1258 else
1259 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001260 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001261 }
1262 }
1263
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001264 qc_recv(qcc);
1265
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001266 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1267
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001268 return NULL;
1269}
1270
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001271static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1272{
1273 struct qcc *qcc = ctx;
1274 int expired = tick_is_expired(t->expire, now_ms);
1275
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001276 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001277
1278 if (qcc) {
1279 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001280 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001281 return t;
1282 }
1283
1284 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001285 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001286 t->expire = TICK_ETERNITY;
1287 return t;
1288 }
1289 }
1290
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001291 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001292
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001293 if (!qcc) {
1294 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001295 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001296 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001297
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001298 qcc->task = NULL;
1299
1300 if (qcc_is_dead(qcc))
1301 qc_release(qcc);
1302
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001303 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1304
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001305 return NULL;
1306}
1307
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001308static int qc_init(struct connection *conn, struct proxy *prx,
1309 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001310{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001311 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001312 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001313
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001314 qcc = pool_alloc(pool_head_qcc);
1315 if (!qcc)
1316 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001317
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001318 qcc->conn = conn;
1319 conn->ctx = qcc;
Willy Tarreau3215e732022-05-27 10:09:11 +02001320 qcc->nb_sc = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001321 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001322
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001323 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001324
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001325 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001326
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001327 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001328 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001329
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001330 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001331 qcc->tx.sent_offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001332
1333 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001334 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001335 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
1336 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001337 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001338 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001339
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001340 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001341 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
1342 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001343 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001344 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001345
1346 /* Server initiated streams must respect the server flow control. */
1347 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001348 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
1349 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001350 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001351 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1352
1353 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001354 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
1355 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001356 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001357 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001358
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001359 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001360 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02001361 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
1362 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001363 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001364
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001365 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001366 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001367
Willy Tarreau784b8682022-04-11 14:18:10 +02001368 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001369 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001370 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1371 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1372
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001373 qcc->wait_event.tasklet = tasklet_new();
1374 if (!qcc->wait_event.tasklet)
1375 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001376
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001377 LIST_INIT(&qcc->send_retry_list);
1378
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001379 qcc->subs = NULL;
1380 qcc->wait_event.tasklet->process = qc_io_cb;
1381 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001382 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001383
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001384 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001385 qcc->task = NULL;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001386 qcc->timeout = prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001387 if (tick_isset(qcc->timeout)) {
1388 qcc->task = task_new_here();
1389 if (!qcc->task)
1390 goto fail_no_timeout_task;
1391 qcc->task->process = qc_timeout_task;
1392 qcc->task->context = qcc;
1393 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1394 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001395
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001396 if (!conn_is_back(conn)) {
1397 if (!LIST_INLIST(&conn->stopping_list)) {
1398 LIST_APPEND(&mux_stopping_data[tid].list,
1399 &conn->stopping_list);
1400 }
1401 }
1402
Willy Tarreau784b8682022-04-11 14:18:10 +02001403 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001404 /* init read cycle */
1405 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001406
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001407 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001408
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001409 fail_no_timeout_task:
1410 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001411 fail_no_tasklet:
1412 pool_free(pool_head_qcc, qcc);
1413 fail_no_qcc:
1414 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001415}
1416
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001417static void qc_destroy(void *ctx)
1418{
1419 struct qcc *qcc = ctx;
1420
1421 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
1422 qc_release(qcc);
1423 TRACE_LEAVE(QMUX_EV_QCC_END);
1424}
1425
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001426static void qc_detach(struct sedesc *sd)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001427{
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001428 struct qcs *qcs = sd->se;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001429 struct qcc *qcc = qcs->qcc;
1430
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001431 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001432
Willy Tarreau3215e732022-05-27 10:09:11 +02001433 --qcc->nb_sc;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001434
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001435 if ((b_data(&qcs->tx.buf) || qcs->tx.offset > qcs->tx.sent_offset) &&
1436 !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001437 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001438 qcs->flags |= QC_SF_DETACH;
1439 return;
1440 }
1441
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001442 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001443
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001444 if (qcc_is_dead(qcc)) {
1445 qc_release(qcc);
1446 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001447 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001448 if (qcc_may_expire(qcc))
1449 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1450 else
1451 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001452 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001453 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001454
1455 TRACE_LEAVE(QMUX_EV_STRM_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001456}
1457
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001458/* Called from the upper layer, to receive data */
Willy Tarreau3215e732022-05-27 10:09:11 +02001459static size_t qc_rcv_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001460 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001461{
Willy Tarreau3215e732022-05-27 10:09:11 +02001462 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001463 struct htx *qcs_htx = NULL;
1464 struct htx *cs_htx = NULL;
1465 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001466 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001467
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001468 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001469
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001470 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
1471 if (htx_is_empty(qcs_htx)) {
1472 /* Set buffer data to 0 as HTX is empty. */
1473 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1474 goto end;
1475 }
1476
1477 ret = qcs_htx->data;
1478
1479 cs_htx = htx_from_buf(buf);
1480 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
1481 htx_to_buf(cs_htx, buf);
1482 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1483 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
1484 goto end;
1485 }
1486
1487 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
1488 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
1489
1490 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001491 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
1492 cs_htx->flags |= HTX_FL_EOM;
1493 fin = 1;
1494 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001495
1496 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
1497 htx_to_buf(cs_htx, buf);
1498 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1499 ret -= qcs_htx->data;
1500
1501 end:
1502 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001503 se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001504 }
1505 else {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001506 se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
1507 if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING))
1508 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001509
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001510 if (fin)
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001511 se_fl_set(qcs->sd, SE_FL_EOI);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001512
1513 if (b_size(&qcs->rx.app_buf)) {
1514 b_free(&qcs->rx.app_buf);
1515 offer_buffers(NULL, 1);
1516 }
1517 }
1518
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001519 if (ret) {
1520 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001521 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001522 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001523
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001524 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
1525
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001526 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001527}
1528
Willy Tarreau3215e732022-05-27 10:09:11 +02001529static size_t qc_snd_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001530 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001531{
Willy Tarreau3215e732022-05-27 10:09:11 +02001532 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001533 size_t ret;
1534
1535 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001536
Willy Tarreau3215e732022-05-27 10:09:11 +02001537 ret = qcs->qcc->app_ops->snd_buf(sc, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001538
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001539 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
1540
1541 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001542}
1543
1544/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1545 * event subscriber <es> is not allowed to change from a previous call as long
1546 * as at least one event is still subscribed. The <event_type> must only be a
1547 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1548 */
Willy Tarreau3215e732022-05-27 10:09:11 +02001549static int qc_subscribe(struct stconn *sc, int event_type,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001550 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001551{
Willy Tarreau3215e732022-05-27 10:09:11 +02001552 return qcs_subscribe(__sc_mux_strm(sc), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001553}
1554
1555/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1556 * The <es> pointer is not allowed to differ from the one passed to the
1557 * subscribe() call. It always returns zero.
1558 */
Willy Tarreau3215e732022-05-27 10:09:11 +02001559static int qc_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001560{
Willy Tarreau3215e732022-05-27 10:09:11 +02001561 struct qcs *qcs = __sc_mux_strm(sc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001562
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001563 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1564 BUG_ON(qcs->subs && qcs->subs != es);
1565
1566 es->events &= ~event_type;
1567 if (!es->events)
1568 qcs->subs = NULL;
1569
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001570 return 0;
1571}
1572
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001573/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Willy Tarreau4596fe22022-05-17 19:07:51 +02001574 * report SE_FL_ERR_PENDING|SE_FL_ERROR on the attached stream connectors and
1575 * wake them.
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001576 */
1577static int qc_wake_some_streams(struct qcc *qcc)
1578{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001579 struct qcs *qcs;
1580 struct eb64_node *node;
1581
1582 for (node = eb64_first(&qcc->streams_by_id); node;
1583 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001584 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001585
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001586 if (!qcs->sd || !qcs->sd->sc)
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001587 continue;
1588
1589 if (qcc->conn->flags & CO_FL_ERROR) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001590 se_fl_set(qcs->sd, SE_FL_ERR_PENDING);
1591 if (se_fl_test(qcs->sd, SE_FL_EOS))
1592 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001593
1594 if (qcs->subs) {
1595 qcs_notify_recv(qcs);
1596 qcs_notify_send(qcs);
1597 }
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001598 else if (qcs->sd->sc->app_ops->wake) {
1599 qcs->sd->sc->app_ops->wake(qcs->sd->sc);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001600 }
1601 }
1602 }
1603
1604 return 0;
1605}
1606
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001607static int qc_wake(struct connection *conn)
1608{
1609 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02001610 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001611
1612 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001613
1614 /* Check if a soft-stop is in progress.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001615 *
1616 * TODO this is revelant for frontend connections only.
Amaury Denoyelled0c62322022-05-23 08:52:58 +02001617 *
1618 * TODO Client should be notified with a H3 GOAWAY and then a
1619 * CONNECTION_CLOSE. However, quic-conn uses the listener socket for
1620 * sending which at this stage is already closed.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001621 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001622 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Amaury Denoyelled0c62322022-05-23 08:52:58 +02001623 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001624
Willy Tarreau784b8682022-04-11 14:18:10 +02001625 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02001626 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
1627
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001628 qc_send(qcc);
1629
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001630 qc_wake_some_streams(qcc);
1631
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001632 if (qcc_is_dead(qcc))
1633 goto release;
1634
1635 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
1636
1637 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001638
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001639 release:
1640 qc_release(qcc);
1641 TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001642 return 1;
1643}
1644
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001645
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001646static void qmux_trace_frm(const struct quic_frame *frm)
1647{
1648 switch (frm->type) {
1649 case QUIC_FT_MAX_STREAMS_BIDI:
1650 chunk_appendf(&trace_buf, " max_streams=%lu",
1651 frm->max_streams_bidi.max_streams);
1652 break;
1653
1654 case QUIC_FT_MAX_STREAMS_UNI:
1655 chunk_appendf(&trace_buf, " max_streams=%lu",
1656 frm->max_streams_uni.max_streams);
1657 break;
1658
1659 default:
1660 break;
1661 }
1662}
1663
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001664/* quic-mux trace handler */
1665static void qmux_trace(enum trace_level level, uint64_t mask,
1666 const struct trace_source *src,
1667 const struct ist where, const struct ist func,
1668 const void *a1, const void *a2, const void *a3, const void *a4)
1669{
1670 const struct connection *conn = a1;
1671 const struct qcc *qcc = conn ? conn->ctx : NULL;
1672 const struct qcs *qcs = a2;
1673
1674 if (!qcc)
1675 return;
1676
1677 if (src->verbosity > QMUX_VERB_CLEAN) {
1678 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
1679
1680 if (qcs)
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001681 chunk_appendf(&trace_buf, " qcs=%p(%lu)", qcs, qcs->id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001682
1683 if (mask & QMUX_EV_QCC_NQCS) {
1684 const uint64_t *id = a3;
1685 chunk_appendf(&trace_buf, " id=%lu", *id);
1686 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001687
1688 if (mask & QMUX_EV_SEND_FRM)
1689 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001690
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001691 if (mask & QMUX_EV_QCS_XFER_DATA) {
1692 const struct qcs_xfer_data_trace_arg *arg = a3;
1693 chunk_appendf(&trace_buf, " prep=%lu xfer=%d",
1694 arg->prep, arg->xfer);
1695 }
1696
1697 if (mask & QMUX_EV_QCS_BUILD_STRM) {
1698 const struct qcs_build_stream_trace_arg *arg = a3;
1699 chunk_appendf(&trace_buf, " len=%lu fin=%d offset=%lu",
1700 arg->len, arg->fin, arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001701 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001702 }
1703}
1704
1705
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001706static const struct mux_ops qc_ops = {
1707 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001708 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001709 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001710 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001711 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001712 .subscribe = qc_subscribe,
1713 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001714 .wake = qc_wake,
Willy Tarreaub5821e12022-04-26 11:54:08 +02001715 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02001716 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001717};
1718
1719static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001720 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001721
1722INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);