blob: 8e9bda99b1fde2f0bdd5ba21ed2a20a931a28c5e [file] [log] [blame]
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001#include <haproxy/mux_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002
Amaury Denoyelleeb01f592021-10-07 16:44:05 +02003#include <import/eb64tree.h>
4
Frédéric Lécailledfbae762021-02-18 09:59:01 +01005#include <haproxy/api.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01006#include <haproxy/connection.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +01007#include <haproxy/conn_stream.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +01008#include <haproxy/dynbuf.h>
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01009#include <haproxy/htx.h>
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +020010#include <haproxy/list.h>
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +020011#include <haproxy/ncbuf.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +010012#include <haproxy/pool.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020013#include <haproxy/quic_stream.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020014#include <haproxy/ssl_sock-t.h>
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010015#include <haproxy/trace.h>
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +010016#include <haproxy/xprt_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010017
Amaury Denoyelledeed7772021-12-03 11:36:46 +010018DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010019DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
20
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010021/* trace source and events */
22static void qmux_trace(enum trace_level level, uint64_t mask,
23 const struct trace_source *src,
24 const struct ist where, const struct ist func,
25 const void *a1, const void *a2, const void *a3, const void *a4);
26
27static const struct trace_event qmux_trace_events[] = {
Amaury Denoyelle4f137572022-03-24 17:10:00 +010028#define QMUX_EV_QCC_RECV (1ULL << 1)
29 { .mask = QMUX_EV_QCC_RECV, .name = "qcc_recv", .desc = "Rx on QUIC connection" },
30#define QMUX_EV_QCC_SEND (1ULL << 2)
31 { .mask = QMUX_EV_QCC_SEND, .name = "qcc_send", .desc = "Tx on QUIC connection" },
32#define QMUX_EV_QCC_WAKE (1ULL << 3)
33 { .mask = QMUX_EV_QCC_WAKE, .name = "qcc_wake", .desc = "QUIC connection woken up" },
34#define QMUX_EV_QCC_END (1ULL << 4)
35 { .mask = QMUX_EV_QCC_END, .name = "qcc_end", .desc = "QUIC connection terminated" },
36#define QMUX_EV_QCC_NQCS (1ULL << 5)
37 { .mask = QMUX_EV_QCC_NQCS, .name = "qcc_no_qcs", .desc = "QUIC stream not found" },
38#define QMUX_EV_QCS_NEW (1ULL << 6)
39 { .mask = QMUX_EV_QCS_NEW, .name = "qcs_new", .desc = "new QUIC stream" },
40#define QMUX_EV_QCS_RECV (1ULL << 7)
41 { .mask = QMUX_EV_QCS_RECV, .name = "qcs_recv", .desc = "Rx on QUIC stream" },
42#define QMUX_EV_QCS_SEND (1ULL << 8)
43 { .mask = QMUX_EV_QCS_SEND, .name = "qcs_send", .desc = "Tx on QUIC stream" },
44#define QMUX_EV_QCS_END (1ULL << 9)
45 { .mask = QMUX_EV_QCS_END, .name = "qcs_end", .desc = "QUIC stream terminated" },
46#define QMUX_EV_STRM_RECV (1ULL << 10)
47 { .mask = QMUX_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
48#define QMUX_EV_STRM_SEND (1ULL << 11)
49 { .mask = QMUX_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
50#define QMUX_EV_STRM_END (1ULL << 12)
51 { .mask = QMUX_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
Amaury Denoyellefa29f332022-03-25 09:09:40 +010052#define QMUX_EV_SEND_FRM (1ULL << 13)
53 { .mask = QMUX_EV_SEND_FRM, .name = "send_frm", .desc = "sending QUIC frame" },
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020054/* special event dedicated to qcs_xfer_data */
55#define QMUX_EV_QCS_XFER_DATA (1ULL << 14)
56 { .mask = QMUX_EV_QCS_XFER_DATA, .name = "qcs_xfer_data", .desc = "qcs_xfer_data" },
57/* special event dedicated to qcs_build_stream_frm */
58#define QMUX_EV_QCS_BUILD_STRM (1ULL << 15)
59 { .mask = QMUX_EV_QCS_BUILD_STRM, .name = "qcs_build_stream_frm", .desc = "qcs_build_stream_frm" },
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010060 { }
61};
62
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020063/* custom arg for QMUX_EV_QCS_XFER_DATA */
64struct qcs_xfer_data_trace_arg {
65 size_t prep;
Amaury Denoyellefdcec362022-03-25 09:28:10 +010066 int xfer;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020067};
68/* custom arg for QMUX_EV_QCS_BUILD_STRM */
69struct qcs_build_stream_trace_arg {
70 size_t len;
Amaury Denoyellefdcec362022-03-25 09:28:10 +010071 char fin;
72 uint64_t offset;
73};
74
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010075static const struct name_desc qmux_trace_lockon_args[4] = {
76 /* arg1 */ { /* already used by the connection */ },
77 /* arg2 */ { .name="qcs", .desc="QUIC stream" },
78 /* arg3 */ { },
79 /* arg4 */ { }
80};
81
82static const struct name_desc qmux_trace_decoding[] = {
83#define QMUX_VERB_CLEAN 1
84 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
85#define QMUX_VERB_MINIMAL 2
86 { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" },
87 { /* end */ }
88};
89
90struct trace_source trace_qmux = {
91 .name = IST("qmux"),
92 .desc = "QUIC multiplexer",
93 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
94 .default_cb = qmux_trace,
95 .known_events = qmux_trace_events,
96 .lockon_args = qmux_trace_lockon_args,
97 .decoding = qmux_trace_decoding,
98 .report_events = ~0, /* report everything by default */
99};
100
101#define TRACE_SOURCE &trace_qmux
102INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
103
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200104/* Emit a CONNECTION_CLOSE with error <err>. This will interrupt all future
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200105 * send/receive operations.
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200106 */
107static void qcc_emit_cc(struct qcc *qcc, int err)
108{
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200109 quic_set_connection_close(qcc->conn->handle.qc, err, 0);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200110 qcc->flags |= QC_CF_CC_EMIT;
111 tasklet_wakeup(qcc->wait_event.tasklet);
112}
113
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100114/* Allocate a new QUIC streams with id <id> and type <type>. */
115struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100116{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100117 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100118
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100119 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
120
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100121 qcs = pool_alloc(pool_head_qcs);
122 if (!qcs)
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200123 return NULL;
124
125 qcs->stream = NULL;
126 qcs->qcc = qcc;
Willy Tarreau01c2a4a2022-05-10 15:46:10 +0200127 qcs->endp = NULL;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200128 qcs->flags = QC_SF_NONE;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200129 qcs->ctx = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100130
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200131 /* Allocate transport layer stream descriptor. Only needed for TX. */
132 if (!quic_stream_is_uni(id) || !quic_stream_is_remote(qcc, id)) {
133 struct quic_conn *qc = qcc->conn->handle.qc;
134 qcs->stream = qc_stream_desc_new(id, type, qcs, qc);
135 if (!qcs->stream)
136 goto err;
137 }
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200138
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +0200139 qcs->id = qcs->by_id.key = id;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200140 if (qcc->app_ops->attach) {
141 if (qcc->app_ops->attach(qcs))
142 goto err;
143 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100144
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200145 /* store transport layer stream descriptor in qcc tree */
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200146 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100147
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100148 qcc->strms[type].nb_streams++;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100149
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100150 /* If stream is local, use peer remote-limit, or else the opposite. */
151 /* TODO use uni limit for unidirectional streams */
152 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
153 qcc->rfctl.msd_bidi_l;
154
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200155 qcs->rx.ncbuf = NCBUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100156 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200157 qcs->rx.offset = qcs->rx.offset_max = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100158
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200159 /* TODO use uni limit for unidirectional streams */
160 qcs->rx.msd = quic_stream_is_local(qcc, id) ? qcc->lfctl.msd_bidi_l :
161 qcc->lfctl.msd_bidi_r;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200162 qcs->rx.msd_init = qcs->rx.msd;
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200163
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100164 qcs->tx.buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100165 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100166 qcs->tx.sent_offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100167
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100168 qcs->wait_event.tasklet = NULL;
169 qcs->wait_event.events = 0;
170 qcs->subs = NULL;
171
172 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100173 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100174 return qcs;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200175
176 err:
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200177 if (qcs->ctx && qcc->app_ops->detach)
178 qcc->app_ops->detach(qcs);
179
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200180 qc_stream_desc_release(qcs->stream);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200181 pool_free(pool_head_qcs, qcs);
182 return NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100183}
184
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200185static void qc_free_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
186{
187 struct buffer buf;
188
Amaury Denoyelle7313f5e2022-05-17 18:53:21 +0200189 if (ncb_is_null(ncbuf))
190 return;
191
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200192 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
193 b_free(&buf);
Amaury Denoyelle7313f5e2022-05-17 18:53:21 +0200194 offer_buffers(NULL, 1);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200195
196 *ncbuf = NCBUF_NULL;
197}
198
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200199/* Free a qcs. This function must only be done to remove a stream on allocation
200 * error or connection shutdown. Else use qcs_destroy which handle all the
201 * QUIC connection mechanism.
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100202 */
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200203void qcs_free(struct qcs *qcs)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100204{
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200205 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200206 b_free(&qcs->tx.buf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200207
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200208 BUG_ON(!qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams);
209 --qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams;
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200210
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200211 if (qcs->ctx && qcs->qcc->app_ops->detach)
212 qcs->qcc->app_ops->detach(qcs);
213
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200214 qc_stream_desc_release(qcs->stream);
215
Willy Tarreaub605c422022-05-17 17:04:55 +0200216 BUG_ON(qcs->endp && !se_fl_test(qcs->endp, SE_FL_ORPHAN));
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100217 cs_endpoint_free(qcs->endp);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200218
219 eb64_delete(&qcs->by_id);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100220 pool_free(pool_head_qcs, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100221}
222
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100223struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100224{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100225 struct buffer *buf = b_alloc(bptr);
226 BUG_ON(!buf);
227 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100228}
229
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200230struct ncbuf *qc_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
231{
232 struct buffer buf = BUF_NULL;
233
234 if (ncb_is_null(ncbuf)) {
235 b_alloc(&buf);
236 BUG_ON(b_is_null(&buf));
237
238 *ncbuf = ncb_make(buf.area, buf.size, 0);
239 ncb_init(ncbuf, 0);
240 }
241
242 return ncbuf;
243}
244
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100245int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
246{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100247 struct qcc *qcc = qcs->qcc;
248
249 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100250
251 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
252 BUG_ON(qcs->subs && qcs->subs != es);
253
254 es->events |= event_type;
255 qcs->subs = es;
256
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100257 if (event_type & SUB_RETRY_RECV)
258 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
259
260 if (event_type & SUB_RETRY_SEND)
261 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
262
263 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
264
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100265 return 0;
266}
267
268void qcs_notify_recv(struct qcs *qcs)
269{
270 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
271 tasklet_wakeup(qcs->subs->tasklet);
272 qcs->subs->events &= ~SUB_RETRY_RECV;
273 if (!qcs->subs->events)
274 qcs->subs = NULL;
275 }
276}
277
278void qcs_notify_send(struct qcs *qcs)
279{
280 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
281 tasklet_wakeup(qcs->subs->tasklet);
282 qcs->subs->events &= ~SUB_RETRY_SEND;
283 if (!qcs->subs->events)
284 qcs->subs = NULL;
285 }
286}
287
Amaury Denoyellea9773552022-05-16 14:38:25 +0200288/* Remove <bytes> from <qcs> Rx buffer. This must be called by transcoders
289 * after STREAM parsing. Flow-control for received offsets may be allocated for
290 * the peer if needed.
291 */
292void qcs_consume(struct qcs *qcs, uint64_t bytes)
293{
294 struct qcc *qcc = qcs->qcc;
295 struct quic_frame *frm;
Amaury Denoyelle8b87b152022-05-18 16:19:47 +0200296 struct ncbuf *buf = &qcs->rx.ncbuf;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200297 enum ncb_ret ret;
298
Amaury Denoyelle8b87b152022-05-18 16:19:47 +0200299 ret = ncb_advance(buf, bytes);
Amaury Denoyellea9773552022-05-16 14:38:25 +0200300 if (ret) {
301 ABORT_NOW(); /* should not happens because removal only in data */
302 }
303
Amaury Denoyelle8b87b152022-05-18 16:19:47 +0200304 if (ncb_is_empty(buf))
305 qc_free_ncbuf(qcs, buf);
306
Amaury Denoyellea9773552022-05-16 14:38:25 +0200307 qcs->rx.offset += bytes;
308 if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) {
309 frm = pool_zalloc(pool_head_quic_frame);
310 BUG_ON(!frm); /* TODO handle this properly */
311
312 qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init;
313
314 LIST_INIT(&frm->reflist);
315 frm->type = QUIC_FT_MAX_STREAM_DATA;
316 frm->max_stream_data.id = qcs->id;
317 frm->max_stream_data.max_stream_data = qcs->rx.msd;
318
319 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
320 tasklet_wakeup(qcc->wait_event.tasklet);
321 }
Amaury Denoyellec830e1e2022-05-16 16:19:59 +0200322
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200323 qcc->lfctl.offsets_consume += bytes;
324 if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) {
Amaury Denoyellec830e1e2022-05-16 16:19:59 +0200325 frm = pool_zalloc(pool_head_quic_frame);
326 BUG_ON(!frm); /* TODO handle this properly */
327
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200328 qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +0200329
330 LIST_INIT(&frm->reflist);
331 frm->type = QUIC_FT_MAX_DATA;
332 frm->max_data.max_data = qcc->lfctl.md;
333
334 LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list);
335 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
336 }
Amaury Denoyellea9773552022-05-16 14:38:25 +0200337}
338
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100339/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
340 * several streams, depending on the already open ones.
341 * Return this node if succeeded, NULL if not.
342 */
Amaury Denoyelle50742292022-03-29 14:57:19 +0200343struct qcs *qcc_get_qcs(struct qcc *qcc, uint64_t id)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100344{
345 unsigned int strm_type;
346 int64_t sub_id;
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200347 struct eb64_node *node;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200348 struct qcs *qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100349
350 strm_type = id & QCS_ID_TYPE_MASK;
351 sub_id = id >> QCS_ID_TYPE_SHIFT;
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200352 node = NULL;
Amaury Denoyelle0dc40f02022-02-07 11:44:17 +0100353 if (quic_stream_is_local(qcc, id)) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100354 /* Local streams: this stream must be already opened. */
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200355 node = eb64_lookup(&qcc->streams_by_id, id);
356 if (!node) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100357 /* unknown stream id */
358 goto out;
359 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200360 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100361 }
362 else {
363 /* Remote streams. */
364 struct eb_root *strms;
365 uint64_t largest_id;
366 enum qcs_type qcs_type;
367
368 strms = &qcc->streams_by_id;
369 qcs_type = qcs_id_type(id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100370
371 /* TODO also checks max-streams for uni streams */
372 if (quic_stream_is_bidi(id)) {
Amaury Denoyelle78396e52022-03-21 17:13:32 +0100373 if (sub_id + 1 > qcc->lfctl.ms_bidi) {
Amaury Denoyelle209404b2022-05-20 16:45:32 +0200374 /* RFC 9000 4.6. Controlling Concurrency
375 *
376 * An endpoint that receives a frame with a
377 * stream ID exceeding the limit it has sent
378 * MUST treat this as a connection error of
379 * type STREAM_LIMIT_ERROR
380 */
381 qcc_emit_cc(qcc, QC_ERR_STREAM_LIMIT_ERROR);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100382 goto out;
383 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100384 }
385
386 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
387 * correct value.
388 */
389 largest_id = qcc->strms[qcs_type].largest_id;
390 if (sub_id > (int64_t)largest_id) {
391 /* RFC: "A stream ID that is used out of order results in all streams
392 * of that type with lower-numbered stream IDs also being opened".
393 * So, let's "open" these streams.
394 */
395 int64_t i;
Amaury Denoyelle50742292022-03-29 14:57:19 +0200396 struct qcs *tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100397
Amaury Denoyelle50742292022-03-29 14:57:19 +0200398 tmp_qcs = NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100399 for (i = largest_id + 1; i <= sub_id; i++) {
400 uint64_t id = (i << QCS_ID_TYPE_SHIFT) | strm_type;
401 enum qcs_type type = id & QCS_ID_DIR_BIT ? QCS_CLT_UNI : QCS_CLT_BIDI;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200402
Amaury Denoyelle50742292022-03-29 14:57:19 +0200403 tmp_qcs = qcs_new(qcc, id, type);
404 if (!tmp_qcs) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100405 /* allocation failure */
406 goto out;
407 }
408
409 qcc->strms[qcs_type].largest_id = i;
410 }
Amaury Denoyelle50742292022-03-29 14:57:19 +0200411 if (tmp_qcs)
412 qcs = tmp_qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100413 }
414 else {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200415 node = eb64_lookup(strms, id);
416 if (node)
417 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100418 }
419 }
420
Amaury Denoyelle50742292022-03-29 14:57:19 +0200421 return qcs;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100422
423 out:
424 return NULL;
425}
426
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200427/* Decode the content of STREAM frames already received on the stream instance
428 * <qcs>.
429 *
430 * Returns 0 on success else non-zero.
431 */
432static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
433{
434 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
435
Amaury Denoyelle0ffd6e72022-05-24 11:07:28 +0200436 if (qcc->app_ops->decode_qcs(qcs, qcs->flags & QC_SF_FIN_RECV, qcc->ctx)) {
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200437 TRACE_DEVEL("leaving on decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
438 return 1;
439 }
440
441 qcs_notify_recv(qcs);
442
443 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
444
445 return 0;
446}
447
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200448/* Emit a CONNECTION_CLOSE_APP with error <err>. Reserved for application error
449 * code. This will interrupt all future send/receive operations.
450 */
451void qcc_emit_cc_app(struct qcc *qcc, int err)
452{
453 quic_set_connection_close(qcc->conn->handle.qc, err, 1);
454 qcc->flags |= QC_CF_CC_EMIT;
455 tasklet_wakeup(qcc->wait_event.tasklet);
456}
457
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200458/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
459 * <data> with length <len> and represents the offset <offset>. <fin> is set if
460 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100461 *
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200462 * Returns 0 on success else non-zero.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100463 */
464int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200465 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100466{
467 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200468 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100469
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100470 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
471
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200472 if (qcc->flags & QC_CF_CC_EMIT) {
473 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
474 return 0;
475 }
476
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200477 /* RFC 9000 19.8. STREAM Frames
478 *
479 * An endpoint MUST terminate the connection with error
480 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
481 * initiated stream that has not yet been created, or for a send-only
482 * stream.
483 */
484 if (quic_stream_is_local(qcc, id) && quic_stream_is_uni(id)) {
485 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
486 TRACE_DEVEL("leaving on invalid reception for a send-only stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
487 return 1;
488 }
489
Amaury Denoyelle50742292022-03-29 14:57:19 +0200490 qcs = qcc_get_qcs(qcc, id);
491 if (!qcs) {
Frédéric Lécailleb57de072022-05-02 18:58:27 +0200492 if ((id >> QCS_ID_TYPE_SHIFT) <= qcc->strms[qcs_id_type(id)].largest_id) {
493 TRACE_DEVEL("already released stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
494 return 0;
495 }
496 else {
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200497 /* RFC 9000 19.8. STREAM Frames
498 *
499 * An endpoint MUST terminate the connection with error
500 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
501 * initiated stream that has not yet been created, or for a send-only
502 * stream.
503 */
504 if (quic_stream_is_local(qcc, id)) {
505 TRACE_DEVEL("leaving on locally initiated stream not yet created", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
506 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
507 return 1;
508 }
509 else {
510 TRACE_DEVEL("leaving on stream not found", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
511 return 1;
512 }
Frédéric Lécailleb57de072022-05-02 18:58:27 +0200513 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100514 }
515
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100516 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100517 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 +0200518 return 0;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100519 }
520
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200521 /* TODO if last frame already received, stream size must not change.
522 * Else send FINAL_SIZE_ERROR.
523 */
524
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200525 if (offset + len > qcs->rx.offset_max) {
526 uint64_t diff = offset + len - qcs->rx.offset_max;
527 qcs->rx.offset_max = offset + len;
528 qcc->lfctl.offsets_recv += diff;
529
530 if (offset + len > qcs->rx.msd ||
531 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
532 /* RFC 9000 4.1. Data Flow Control
533 *
534 * A receiver MUST close the connection with an error
535 * of type FLOW_CONTROL_ERROR if the sender violates
536 * the advertised connection or stream data limits
537 */
538 TRACE_DEVEL("leaving on flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
539 qcc->conn, qcs);
540 qcc_emit_cc(qcc, QC_ERR_FLOW_CONTROL_ERROR);
541 return 1;
542 }
543 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100544
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200545 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100546 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200547 ABORT_NOW();
548 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100549 }
550
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100551 TRACE_DEVEL("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200552 if (offset < qcs->rx.offset) {
553 len -= qcs->rx.offset - offset;
554 offset = qcs->rx.offset;
555 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100556
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200557 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
558 if (ret != NCB_RET_OK) {
559 if (ret == NCB_RET_DATA_REJ) {
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200560 /* RFC 9000 2.2. Sending and Receiving Data
561 *
562 * An endpoint could receive data for a stream at the
563 * same stream offset multiple times. Data that has
564 * already been received can be discarded. The data at
565 * a given offset MUST NOT change if it is sent
566 * multiple times; an endpoint MAY treat receipt of
567 * different data at the same offset within a stream as
568 * a connection error of type PROTOCOL_VIOLATION.
569 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200570 TRACE_DEVEL("leaving on data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
571 qcc->conn, qcs);
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200572 qcc_emit_cc(qcc, QC_ERR_PROTOCOL_VIOLATION);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200573 }
574 else if (ret == NCB_RET_GAP_SIZE) {
575 TRACE_DEVEL("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
576 qcc->conn, qcs);
577 }
578 return 1;
579 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100580
581 if (fin)
582 qcs->flags |= QC_SF_FIN_RECV;
583
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200584 if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL))
585 qcc_decode_qcs(qcc, qcs);
586
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200587 if (qcs->flags & QC_SF_READ_ABORTED) {
588 /* TODO should send a STOP_SENDING */
589 qcs_free(qcs);
590 }
591
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100592 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100593 return 0;
594}
595
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100596/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
597 * the frame.
598 *
599 * Returns 0 on success else non-zero.
600 */
601int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
602{
603 if (qcc->rfctl.md < max) {
604 qcc->rfctl.md = max;
605
606 if (qcc->flags & QC_CF_BLK_MFCTL) {
607 qcc->flags &= ~QC_CF_BLK_MFCTL;
608 tasklet_wakeup(qcc->wait_event.tasklet);
609 }
610 }
611 return 0;
612}
613
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100614/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
615 * field of the frame and <id> is the identifier of the QUIC stream.
616 *
617 * Returns 0 on success else non-zero.
618 */
619int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
620{
621 struct qcs *qcs;
622 struct eb64_node *node;
623
624 node = eb64_lookup(&qcc->streams_by_id, id);
625 if (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200626 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100627 if (max > qcs->tx.msd) {
628 qcs->tx.msd = max;
629
630 if (qcs->flags & QC_SF_BLK_SFCTL) {
631 qcs->flags &= ~QC_SF_BLK_SFCTL;
632 tasklet_wakeup(qcc->wait_event.tasklet);
633 }
634 }
635 }
636
637 return 0;
638}
639
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200640/* Signal the closing of remote stream with id <id>. Flow-control for new
641 * streams may be allocated for the peer if needed.
642 */
643static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +0100644{
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200645 struct quic_frame *frm;
646
647 if (quic_stream_is_bidi(id)) {
648 ++qcc->lfctl.cl_bidi_r;
649 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
650 frm = pool_zalloc(pool_head_quic_frame);
651 BUG_ON(!frm); /* TODO handle this properly */
652
653 LIST_INIT(&frm->reflist);
654 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
655 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
656 qcc->lfctl.cl_bidi_r;
657 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
658 tasklet_wakeup(qcc->wait_event.tasklet);
659
660 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
661 qcc->lfctl.cl_bidi_r = 0;
662 }
663 }
664 else {
665 /* TODO */
666 }
667
668 return 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100669}
670
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500671/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100672static void qcs_destroy(struct qcs *qcs)
673{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100674 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200675 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100676
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100677 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100678
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200679 if (quic_stream_is_remote(qcs->qcc, id))
680 qcc_release_remote_stream(qcs->qcc, id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100681
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200682 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100683
684 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100685}
686
687static inline int qcc_is_dead(const struct qcc *qcc)
688{
Amaury Denoyelle198d35f2022-04-01 17:56:58 +0200689 if (qcc->app_ops && qcc->app_ops->is_active &&
690 qcc->app_ops->is_active(qcc, qcc->ctx))
691 return 0;
692
Amaury Denoyelled97fc802022-04-06 16:13:09 +0200693 if ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task)
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100694 return 1;
695
696 return 0;
697}
698
699/* Return true if the mux timeout should be armed. */
700static inline int qcc_may_expire(struct qcc *qcc)
701{
Amaury Denoyelle06890aa2022-04-04 16:15:06 +0200702 return !qcc->nb_cs;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100703}
704
705/* release function. This one should be called to free all resources allocated
706 * to the mux.
707 */
708static void qc_release(struct qcc *qcc)
709{
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200710 struct connection *conn = qcc->conn;
711 struct eb64_node *node;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100712
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100713 TRACE_ENTER(QMUX_EV_QCC_END);
714
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200715 if (qcc->app_ops && qcc->app_ops->release)
716 qcc->app_ops->release(qcc->ctx);
Amaury Denoyellef8909452022-03-30 11:51:56 +0200717
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200718 if (qcc->task) {
719 task_destroy(qcc->task);
720 qcc->task = NULL;
721 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100722
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200723 if (qcc->wait_event.tasklet)
724 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyellef3e03a42022-04-21 15:41:34 +0200725 if (conn && qcc->wait_event.events) {
726 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
727 qcc->wait_event.events,
728 &qcc->wait_event);
729 }
Amaury Denoyellef8909452022-03-30 11:51:56 +0200730
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200731 /* liberate remaining qcs instances */
732 node = eb64_first(&qcc->streams_by_id);
733 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200734 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200735 node = eb64_next(node);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200736 qcs_free(qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100737 }
738
Amaury Denoyellec985cb12022-05-16 14:29:59 +0200739 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
740 struct quic_frame *frm = LIST_ELEM(&qcc->lfctl.frms, struct quic_frame *, list);
741 LIST_DELETE(&frm->list);
742 pool_free(pool_head_quic_frame, frm);
743 }
744
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200745 pool_free(pool_head_qcc, qcc);
746
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100747 if (conn) {
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100748 LIST_DEL_INIT(&conn->stopping_list);
749
Willy Tarreau784b8682022-04-11 14:18:10 +0200750 conn->handle.qc->conn = NULL;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100751 conn->mux = NULL;
752 conn->ctx = NULL;
753
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100754 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
755
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100756 conn_stop_tracking(conn);
757 conn_full_close(conn);
758 if (conn->destroy_cb)
759 conn->destroy_cb(conn);
760 conn_free(conn);
761 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100762
763 TRACE_LEAVE(QMUX_EV_QCC_END);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100764}
765
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200766/* Transfer as much as possible data on <qcs> from <in> to <out>. <max_data> is
767 * the current flow-control limit on the connection which must not be exceeded.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100768 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200769 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +0100770 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200771static int qcs_xfer_data(struct qcs *qcs, struct buffer *out,
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200772 struct buffer *in, uint64_t max_data)
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200773{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100774 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200775 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100776 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200777
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100778 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100779
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100780 qc_get_buf(qcs, out);
781
782 /*
783 * QCS out buffer diagram
784 * head left to_xfer
785 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100786 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100787 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +0100788 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100789 * ^ ack-off ^ sent-off ^ off
790 *
791 * STREAM frame
792 * ^ ^
793 * |xxxxxxxxxxxxxxxxx|
794 */
795
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200796 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100797 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
798
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100799 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200800 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100801
802 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
803 /* do not exceed flow control limit */
804 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
805 to_xfer = qcs->tx.msd - qcs->tx.offset;
806
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100807 BUG_ON_HOT(max_data > qcc->rfctl.md);
808 /* do not overcome flow control limit on connection */
809 if (max_data + to_xfer > qcc->rfctl.md)
810 to_xfer = qcc->rfctl.md - max_data;
811
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100812 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200813 goto out;
814
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200815 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200816
817 out:
818 {
819 struct qcs_xfer_data_trace_arg arg = {
820 .prep = b_data(out), .xfer = total,
821 };
822 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
823 qcc->conn, qcs, &arg);
824 }
825
826 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200827}
828
Amaury Denoyellefe8f5552022-04-27 16:44:49 +0200829/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
830 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
831 * stream frame.
832 *
833 * Returns the length of the STREAM frame or a negative error code.
834 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200835static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
836 struct list *frm_list)
837{
838 struct qcc *qcc = qcs->qcc;
839 struct quic_frame *frm;
840 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200841 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200842
843 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
844
Amaury Denoyellea4569202022-04-15 17:29:25 +0200845 /* if ack_offset < buf_offset, it points to an older buffer. */
846 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
847 BUG_ON(qcs->tx.sent_offset < base_off);
848
849 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200850 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200851 BUG_ON(total < 0);
852
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200853 if (!total) {
854 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
855 return 0;
856 }
Amaury Denoyellea4569202022-04-15 17:29:25 +0200857 BUG_ON(qcs->tx.sent_offset >= qcs->tx.offset);
858 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200859
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200860 frm = pool_zalloc(pool_head_quic_frame);
861 if (!frm)
862 goto err;
863
Frédéric Lécailleb9171912022-04-21 17:32:10 +0200864 LIST_INIT(&frm->reflist);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100865 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200866 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200867 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100868 frm->stream.buf = out;
869 frm->stream.data = (unsigned char *)b_peek(out, head);
870
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100871 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200872 if (fin)
873 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100874
875 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200876 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100877 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200878 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100879
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200880 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
881 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200882
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100883 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100884
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200885 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100886 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200887 struct qcs_build_stream_trace_arg arg = {
888 .len = frm->stream.len, .fin = fin,
889 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100890 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +0200891 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +0100892 qcc->conn, qcs, &arg);
893 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100894
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200895 return total;
896
897 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100898 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200899 return -1;
900}
901
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100902/* This function must be called by the upper layer to inform about the sending
903 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
904 * <offset>.
905 */
906void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
907{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100908 struct qcc *qcc = qcs->qcc;
909 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100910
911 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200912 BUG_ON(offset >= qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100913
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100914 /* check if the STREAM frame has already been notified. It can happen
915 * for retransmission.
916 */
917 if (offset + data <= qcs->tx.sent_offset)
918 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100919
920 diff = offset + data - qcs->tx.sent_offset;
921
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +0100922 /* increase offset sum on connection */
923 qcc->tx.sent_offsets += diff;
924 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
925 if (qcc->tx.sent_offsets == qcc->rfctl.md)
926 qcc->flags |= QC_CF_BLK_MFCTL;
927
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100928 /* increase offset on stream */
929 qcs->tx.sent_offset += diff;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100930 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200931 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100932 if (qcs->tx.sent_offset == qcs->tx.msd)
933 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyellea4569202022-04-15 17:29:25 +0200934
935 if (qcs->tx.offset == qcs->tx.sent_offset && b_full(&qcs->stream->buf->buf)) {
936 qc_stream_buf_release(qcs->stream);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200937 /* prepare qcs for immediate send retry if data to send */
938 if (b_data(&qcs->tx.buf))
939 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
Amaury Denoyellea4569202022-04-15 17:29:25 +0200940 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +0100941}
942
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100943/* Wrapper for send on transport layer. Send a list of frames <frms> for the
944 * connection <qcc>.
945 *
946 * Returns 0 if all data sent with success else non-zero.
947 */
948static int qc_send_frames(struct qcc *qcc, struct list *frms)
949{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100950 /* TODO implement an opportunistic retry mechanism. This is needed
951 * because qc_send_app_pkts is not completed. It will only prepare data
952 * up to its Tx buffer. The frames left are not send even if the Tx
953 * buffer is emptied by the sendto call.
954 *
955 * To overcome this, we call repeatedly qc_send_app_pkts until we
956 * detect that the transport layer has send nothing. This could happen
957 * on congestion or sendto syscall error.
958 *
959 * When qc_send_app_pkts is improved to handle retry by itself, we can
960 * remove the looping from the MUX.
961 */
962 struct quic_frame *first_frm;
963 uint64_t first_offset = 0;
964 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100965
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100966 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
967
968 if (LIST_ISEMPTY(frms)) {
969 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200970 return 1;
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100971 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +0100972
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +0200973 LIST_INIT(&qcc->send_retry_list);
974
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100975 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100976 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
977 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
978 first_offset = first_frm->stream.offset.key;
979 first_stream_frame_type = 1;
980 }
981 else {
982 first_stream_frame_type = 0;
983 }
984
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100985 if (!LIST_ISEMPTY(frms))
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +0200986 qc_send_app_pkts(qcc->conn->handle.qc, 0, frms);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100987
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100988 /* If there is frames left, check if the transport layer has send some
989 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100990 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +0100991 if (!LIST_ISEMPTY(frms)) {
992 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
993 goto retry_send;
994
995 /* If the first frame is STREAM, check if its offset has
996 * changed.
997 */
998 if (first_stream_frame_type &&
999 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
1000 goto retry_send;
1001 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001002 }
1003
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001004 /* If there is frames left at this stage, transport layer is blocked.
1005 * Subscribe on it to retry later.
1006 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001007 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001008 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001009 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1010 SUB_RETRY_SEND, &qcc->wait_event);
1011 return 1;
1012 }
1013
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001014 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1015
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001016 return 0;
1017}
1018
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001019/* Used internally by qc_send function. Proceed to send for <qcs>. This will
1020 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
1021 * is then generated and inserted in <frms> list. <qcc_max_data> is the current
1022 * flow-control max-data at the connection level which must not be surpassed.
1023 *
1024 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
1025 * be null if out buffer cannot be allocated.
1026 */
1027static int _qc_send_qcs(struct qcs *qcs, struct list *frms,
1028 uint64_t qcc_max_data)
1029{
1030 struct qcc *qcc = qcs->qcc;
1031 struct buffer *buf = &qcs->tx.buf;
1032 struct buffer *out = qc_stream_buf_get(qcs->stream);
1033 int xfer = 0;
1034
1035 /* Allocate <out> buffer if necessary. */
1036 if (!out) {
1037 if (qcc->flags & QC_CF_CONN_FULL)
1038 return 0;
1039
1040 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
1041 if (!out) {
1042 qcc->flags |= QC_CF_CONN_FULL;
1043 return 0;
1044 }
1045 }
1046
1047 /* Transfer data from <buf> to <out>. */
1048 if (b_data(buf)) {
1049 xfer = qcs_xfer_data(qcs, out, buf, qcc_max_data);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001050 if (xfer > 0) {
1051 qcs_notify_send(qcs);
1052 qcs->flags &= ~QC_SF_BLK_MROOM;
1053 }
1054
1055 qcs->tx.offset += xfer;
1056 }
1057
1058 /* out buffer cannot be emptied if qcs offsets differ. */
1059 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1060
1061 /* Build a new STREAM frame with <out> buffer. */
1062 if (qcs->tx.sent_offset != qcs->tx.offset) {
1063 int ret;
1064 char fin = !!(qcs->flags & QC_SF_FIN_STREAM);
1065
1066 /* FIN is set if all incoming data were transfered. */
1067 fin = !!(fin && !b_data(buf));
1068
1069 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +02001070 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001071 }
1072
1073 return xfer;
1074}
1075
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001076/* Proceed to sending. Loop through all available streams for the <qcc>
1077 * instance and try to send as much as possible.
1078 *
1079 * Returns the total of bytes sent to the transport layer.
1080 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001081static int qc_send(struct qcc *qcc)
1082{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001083 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001084 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001085 struct qcs *qcs, *qcs_tmp;
1086 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001087
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001088 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001089
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001090 if (qcc->conn->flags & CO_FL_SOCK_WR_SH || qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001091 qcc->conn->flags |= CO_FL_ERROR;
1092 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
1093 return 0;
1094 }
1095
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001096 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1097 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
1098 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
1099 goto out;
1100 }
1101 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02001102
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001103 if (qcc->flags & QC_CF_BLK_MFCTL)
1104 return 0;
1105
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001106 /* loop through all streams, construct STREAM frames if data available.
1107 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001108 */
1109 node = eb64_first(&qcc->streams_by_id);
1110 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001111 int ret;
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001112 uint64_t id;
1113
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001114 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001115 id = qcs->id;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001116
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001117 if (quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001118 node = eb64_next(node);
1119 continue;
1120 }
1121
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001122 if (qcs->flags & QC_SF_BLK_SFCTL) {
1123 node = eb64_next(node);
1124 continue;
1125 }
1126
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001127 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001128 node = eb64_next(node);
1129 continue;
1130 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001131
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001132 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + total);
1133 total += ret;
1134 node = eb64_next(node);
1135 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001136
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001137 if (qc_send_frames(qcc, &frms)) {
1138 /* data rejected by transport layer, do not retry. */
1139 goto out;
1140 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001141
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001142 retry:
1143 tmp_total = 0;
1144 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
1145 int ret;
1146 BUG_ON(!b_data(&qcs->tx.buf));
1147 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001148
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001149 ret = _qc_send_qcs(qcs, &frms, qcc->tx.sent_offsets + tmp_total);
1150 tmp_total += ret;
1151 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001152 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001153
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001154 total += tmp_total;
1155 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
1156 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +01001157
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001158 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001159 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1160
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001161 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001162}
1163
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001164/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1165 * operation.
1166 *
1167 * Returns 0 on success else non-zero.
1168 */
1169static int qc_recv(struct qcc *qcc)
1170{
1171 struct eb64_node *node;
1172 struct qcs *qcs;
1173
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001174 TRACE_ENTER(QMUX_EV_QCC_RECV);
1175
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001176 if (qcc->flags & QC_CF_CC_EMIT) {
1177 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
1178 return 0;
1179 }
1180
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001181 node = eb64_first(&qcc->streams_by_id);
1182 while (node) {
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001183 uint64_t id;
1184
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001185 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001186 id = qcs->id;
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001187
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001188 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001189 node = eb64_next(node);
1190 continue;
1191 }
1192
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001193 if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001194 node = eb64_next(node);
1195 continue;
1196 }
1197
1198 qcc_decode_qcs(qcc, qcs);
1199 node = eb64_next(node);
Amaury Denoyelle849b24f2022-05-24 17:22:07 +02001200
1201 if (qcs->flags & QC_SF_READ_ABORTED) {
1202 /* TODO should send a STOP_SENDING */
1203 qcs_free(qcs);
1204 }
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001205 }
1206
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001207 TRACE_LEAVE(QMUX_EV_QCC_RECV);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001208 return 0;
1209}
1210
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001211/* Release all streams that are already marked as detached. This is only done
1212 * if their TX buffers are empty or if a CONNECTION_CLOSE has been received.
1213 *
1214 * Return the number of released stream.
1215 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001216static int qc_release_detached_streams(struct qcc *qcc)
1217{
1218 struct eb64_node *node;
1219 int release = 0;
1220
1221 node = eb64_first(&qcc->streams_by_id);
1222 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001223 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001224 node = eb64_next(node);
1225
1226 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001227 if (!b_data(&qcs->tx.buf) &&
1228 qcs->tx.offset == qcs->tx.sent_offset) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001229 qcs_destroy(qcs);
1230 release = 1;
1231 }
1232 else {
1233 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1234 SUB_RETRY_SEND, &qcc->wait_event);
1235 }
1236 }
1237 }
1238
1239 return release;
1240}
1241
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001242static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1243{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001244 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001245
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001246 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001247
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001248 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001249
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001250 if (qc_release_detached_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001251 if (qcc_is_dead(qcc)) {
1252 qc_release(qcc);
1253 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001254 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001255 if (qcc_may_expire(qcc))
1256 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1257 else
1258 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001259 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001260 }
1261 }
1262
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001263 qc_recv(qcc);
1264
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001265 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1266
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001267 return NULL;
1268}
1269
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001270static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1271{
1272 struct qcc *qcc = ctx;
1273 int expired = tick_is_expired(t->expire, now_ms);
1274
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001275 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001276
1277 if (qcc) {
1278 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001279 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001280 return t;
1281 }
1282
1283 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001284 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001285 t->expire = TICK_ETERNITY;
1286 return t;
1287 }
1288 }
1289
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001290 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001291
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001292 if (!qcc) {
1293 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001294 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001295 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001296
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001297 qcc->task = NULL;
1298
1299 if (qcc_is_dead(qcc))
1300 qc_release(qcc);
1301
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001302 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1303
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001304 return NULL;
1305}
1306
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001307static int qc_init(struct connection *conn, struct proxy *prx,
1308 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001309{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001310 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001311 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001312
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001313 qcc = pool_alloc(pool_head_qcc);
1314 if (!qcc)
1315 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001316
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001317 qcc->conn = conn;
1318 conn->ctx = qcc;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001319 qcc->nb_cs = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001320 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001321
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001322 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001323
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001324 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001325
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001326 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001327 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001328
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001329 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001330 qcc->tx.sent_offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001331
1332 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001333 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001334 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
1335 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001336 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001337 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001338
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001339 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001340 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
1341 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001342 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001343 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001344
1345 /* Server initiated streams must respect the server flow control. */
1346 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001347 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
1348 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001349 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001350 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1351
1352 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001353 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
1354 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001355 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001356 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001357
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001358 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001359 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02001360 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
1361 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001362 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001363
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001364 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001365 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001366
Willy Tarreau784b8682022-04-11 14:18:10 +02001367 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001368 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001369 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1370 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1371
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001372 qcc->wait_event.tasklet = tasklet_new();
1373 if (!qcc->wait_event.tasklet)
1374 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001375
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001376 LIST_INIT(&qcc->send_retry_list);
1377
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001378 qcc->subs = NULL;
1379 qcc->wait_event.tasklet->process = qc_io_cb;
1380 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001381 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001382
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001383 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001384 qcc->task = NULL;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001385 qcc->timeout = prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001386 if (tick_isset(qcc->timeout)) {
1387 qcc->task = task_new_here();
1388 if (!qcc->task)
1389 goto fail_no_timeout_task;
1390 qcc->task->process = qc_timeout_task;
1391 qcc->task->context = qcc;
1392 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1393 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001394
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001395 if (!conn_is_back(conn)) {
1396 if (!LIST_INLIST(&conn->stopping_list)) {
1397 LIST_APPEND(&mux_stopping_data[tid].list,
1398 &conn->stopping_list);
1399 }
1400 }
1401
Willy Tarreau784b8682022-04-11 14:18:10 +02001402 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001403 /* init read cycle */
1404 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001405
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001406 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001407
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001408 fail_no_timeout_task:
1409 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001410 fail_no_tasklet:
1411 pool_free(pool_head_qcc, qcc);
1412 fail_no_qcc:
1413 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001414}
1415
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001416static void qc_destroy(void *ctx)
1417{
1418 struct qcc *qcc = ctx;
1419
1420 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
1421 qc_release(qcc);
1422 TRACE_LEAVE(QMUX_EV_QCC_END);
1423}
1424
Willy Tarreau4201ab72022-05-10 19:18:52 +02001425static void qc_detach(struct cs_endpoint *endp)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001426{
Willy Tarreau65d05972022-05-16 17:29:42 +02001427 struct qcs *qcs = endp->se;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001428 struct qcc *qcc = qcs->qcc;
1429
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001430 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001431
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001432 --qcc->nb_cs;
1433
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001434 if ((b_data(&qcs->tx.buf) || qcs->tx.offset > qcs->tx.sent_offset) &&
1435 !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001436 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001437 qcs->flags |= QC_SF_DETACH;
1438 return;
1439 }
1440
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001441 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001442
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001443 if (qcc_is_dead(qcc)) {
1444 qc_release(qcc);
1445 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001446 else if (qcc->task) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001447 if (qcc_may_expire(qcc))
1448 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1449 else
1450 qcc->task->expire = TICK_ETERNITY;
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001451 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001452 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001453
1454 TRACE_LEAVE(QMUX_EV_STRM_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001455}
1456
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001457/* Called from the upper layer, to receive data */
1458static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf,
1459 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001460{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001461 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001462 struct htx *qcs_htx = NULL;
1463 struct htx *cs_htx = NULL;
1464 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001465 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001466
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001467 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001468
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001469 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
1470 if (htx_is_empty(qcs_htx)) {
1471 /* Set buffer data to 0 as HTX is empty. */
1472 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1473 goto end;
1474 }
1475
1476 ret = qcs_htx->data;
1477
1478 cs_htx = htx_from_buf(buf);
1479 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
1480 htx_to_buf(cs_htx, buf);
1481 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1482 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
1483 goto end;
1484 }
1485
1486 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
1487 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
1488
1489 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001490 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
1491 cs_htx->flags |= HTX_FL_EOM;
1492 fin = 1;
1493 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001494
1495 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
1496 htx_to_buf(cs_htx, buf);
1497 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
1498 ret -= qcs_htx->data;
1499
1500 end:
1501 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreaub605c422022-05-17 17:04:55 +02001502 se_fl_set(qcs->endp, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001503 }
1504 else {
Willy Tarreaub605c422022-05-17 17:04:55 +02001505 se_fl_clr(qcs->endp, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
1506 if (se_fl_test(qcs->endp, SE_FL_ERR_PENDING))
1507 se_fl_set(qcs->endp, SE_FL_ERROR);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001508
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01001509 if (fin)
Willy Tarreaub605c422022-05-17 17:04:55 +02001510 se_fl_set(qcs->endp, SE_FL_EOI);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001511
1512 if (b_size(&qcs->rx.app_buf)) {
1513 b_free(&qcs->rx.app_buf);
1514 offer_buffers(NULL, 1);
1515 }
1516 }
1517
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001518 if (ret) {
1519 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001520 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02001521 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001522
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001523 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
1524
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01001525 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001526}
1527
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001528static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf,
1529 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001530{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001531 struct qcs *qcs = __cs_mux(cs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001532 size_t ret;
1533
1534 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001535
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001536 ret = qcs->qcc->app_ops->snd_buf(cs, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001537
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001538 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
1539
1540 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001541}
1542
1543/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1544 * event subscriber <es> is not allowed to change from a previous call as long
1545 * as at least one event is still subscribed. The <event_type> must only be a
1546 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1547 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001548static int qc_subscribe(struct conn_stream *cs, int event_type,
1549 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001550{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001551 return qcs_subscribe(__cs_mux(cs), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001552}
1553
1554/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1555 * The <es> pointer is not allowed to differ from the one passed to the
1556 * subscribe() call. It always returns zero.
1557 */
1558static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
1559{
Christopher Fauletdb90f2a2022-03-22 16:06:25 +01001560 struct qcs *qcs = __cs_mux(cs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001561
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001562 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1563 BUG_ON(qcs->subs && qcs->subs != es);
1564
1565 es->events &= ~event_type;
1566 if (!es->events)
1567 qcs->subs = NULL;
1568
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001569 return 0;
1570}
1571
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001572/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Willy Tarreaub605c422022-05-17 17:04:55 +02001573 * report SE_FL_ERR_PENDING|SE_FL_ERROR on the attached conn-streams and wake
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001574 * them.
1575 */
1576static int qc_wake_some_streams(struct qcc *qcc)
1577{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001578 struct qcs *qcs;
1579 struct eb64_node *node;
1580
1581 for (node = eb64_first(&qcc->streams_by_id); node;
1582 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001583 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001584
Amaury Denoyelle1c25b182022-05-24 16:53:56 +02001585 if (!qcs->endp || !qcs->endp->cs)
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001586 continue;
1587
1588 if (qcc->conn->flags & CO_FL_ERROR) {
Willy Tarreaub605c422022-05-17 17:04:55 +02001589 se_fl_set(qcs->endp, SE_FL_ERR_PENDING);
1590 if (se_fl_test(qcs->endp, SE_FL_EOS))
1591 se_fl_set(qcs->endp, SE_FL_ERROR);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001592
1593 if (qcs->subs) {
1594 qcs_notify_recv(qcs);
1595 qcs_notify_send(qcs);
1596 }
Willy Tarreau01c2a4a2022-05-10 15:46:10 +02001597 else if (qcs->endp->cs->data_cb->wake) {
1598 qcs->endp->cs->data_cb->wake(qcs->endp->cs);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001599 }
1600 }
1601 }
1602
1603 return 0;
1604}
1605
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001606static int qc_wake(struct connection *conn)
1607{
1608 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02001609 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001610
1611 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001612
1613 /* Check if a soft-stop is in progress.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001614 *
1615 * TODO this is revelant for frontend connections only.
Amaury Denoyelled0c62322022-05-23 08:52:58 +02001616 *
1617 * TODO Client should be notified with a H3 GOAWAY and then a
1618 * CONNECTION_CLOSE. However, quic-conn uses the listener socket for
1619 * sending which at this stage is already closed.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001620 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001621 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Amaury Denoyelled0c62322022-05-23 08:52:58 +02001622 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001623
Willy Tarreau784b8682022-04-11 14:18:10 +02001624 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02001625 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
1626
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001627 qc_send(qcc);
1628
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02001629 qc_wake_some_streams(qcc);
1630
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001631 if (qcc_is_dead(qcc))
1632 goto release;
1633
1634 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
1635
1636 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001637
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001638 release:
1639 qc_release(qcc);
1640 TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001641 return 1;
1642}
1643
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001644
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001645static void qmux_trace_frm(const struct quic_frame *frm)
1646{
1647 switch (frm->type) {
1648 case QUIC_FT_MAX_STREAMS_BIDI:
1649 chunk_appendf(&trace_buf, " max_streams=%lu",
1650 frm->max_streams_bidi.max_streams);
1651 break;
1652
1653 case QUIC_FT_MAX_STREAMS_UNI:
1654 chunk_appendf(&trace_buf, " max_streams=%lu",
1655 frm->max_streams_uni.max_streams);
1656 break;
1657
1658 default:
1659 break;
1660 }
1661}
1662
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001663/* quic-mux trace handler */
1664static void qmux_trace(enum trace_level level, uint64_t mask,
1665 const struct trace_source *src,
1666 const struct ist where, const struct ist func,
1667 const void *a1, const void *a2, const void *a3, const void *a4)
1668{
1669 const struct connection *conn = a1;
1670 const struct qcc *qcc = conn ? conn->ctx : NULL;
1671 const struct qcs *qcs = a2;
1672
1673 if (!qcc)
1674 return;
1675
1676 if (src->verbosity > QMUX_VERB_CLEAN) {
1677 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
1678
1679 if (qcs)
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001680 chunk_appendf(&trace_buf, " qcs=%p(%lu)", qcs, qcs->id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001681
1682 if (mask & QMUX_EV_QCC_NQCS) {
1683 const uint64_t *id = a3;
1684 chunk_appendf(&trace_buf, " id=%lu", *id);
1685 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01001686
1687 if (mask & QMUX_EV_SEND_FRM)
1688 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001689
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001690 if (mask & QMUX_EV_QCS_XFER_DATA) {
1691 const struct qcs_xfer_data_trace_arg *arg = a3;
1692 chunk_appendf(&trace_buf, " prep=%lu xfer=%d",
1693 arg->prep, arg->xfer);
1694 }
1695
1696 if (mask & QMUX_EV_QCS_BUILD_STRM) {
1697 const struct qcs_build_stream_trace_arg *arg = a3;
1698 chunk_appendf(&trace_buf, " len=%lu fin=%d offset=%lu",
1699 arg->len, arg->fin, arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001700 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01001701 }
1702}
1703
1704
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001705static const struct mux_ops qc_ops = {
1706 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001707 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001708 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001709 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001710 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001711 .subscribe = qc_subscribe,
1712 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001713 .wake = qc_wake,
Willy Tarreaub5821e12022-04-26 11:54:08 +02001714 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02001715 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001716};
1717
1718static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001719 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001720
1721INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);