blob: 7b71ddb2c1a274d6d273749231133859464aeaa5 [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 Denoyelle4c9a1642022-08-10 16:58:01 +020029#define QMUX_EV_QCC_NEW (1ULL << 0)
30 { .mask = QMUX_EV_QCC_NEW , .name = "qcc_new", .desc = "new QUIC connection" },
Amaury Denoyelle4f137572022-03-24 17:10:00 +010031#define QMUX_EV_QCC_RECV (1ULL << 1)
32 { .mask = QMUX_EV_QCC_RECV, .name = "qcc_recv", .desc = "Rx on QUIC connection" },
33#define QMUX_EV_QCC_SEND (1ULL << 2)
34 { .mask = QMUX_EV_QCC_SEND, .name = "qcc_send", .desc = "Tx on QUIC connection" },
35#define QMUX_EV_QCC_WAKE (1ULL << 3)
36 { .mask = QMUX_EV_QCC_WAKE, .name = "qcc_wake", .desc = "QUIC connection woken up" },
37#define QMUX_EV_QCC_END (1ULL << 4)
38 { .mask = QMUX_EV_QCC_END, .name = "qcc_end", .desc = "QUIC connection terminated" },
39#define QMUX_EV_QCC_NQCS (1ULL << 5)
40 { .mask = QMUX_EV_QCC_NQCS, .name = "qcc_no_qcs", .desc = "QUIC stream not found" },
41#define QMUX_EV_QCS_NEW (1ULL << 6)
42 { .mask = QMUX_EV_QCS_NEW, .name = "qcs_new", .desc = "new QUIC stream" },
43#define QMUX_EV_QCS_RECV (1ULL << 7)
44 { .mask = QMUX_EV_QCS_RECV, .name = "qcs_recv", .desc = "Rx on QUIC stream" },
45#define QMUX_EV_QCS_SEND (1ULL << 8)
46 { .mask = QMUX_EV_QCS_SEND, .name = "qcs_send", .desc = "Tx on QUIC stream" },
47#define QMUX_EV_QCS_END (1ULL << 9)
48 { .mask = QMUX_EV_QCS_END, .name = "qcs_end", .desc = "QUIC stream terminated" },
49#define QMUX_EV_STRM_RECV (1ULL << 10)
50 { .mask = QMUX_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
51#define QMUX_EV_STRM_SEND (1ULL << 11)
52 { .mask = QMUX_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
53#define QMUX_EV_STRM_END (1ULL << 12)
54 { .mask = QMUX_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
Amaury Denoyellefa29f332022-03-25 09:09:40 +010055#define QMUX_EV_SEND_FRM (1ULL << 13)
56 { .mask = QMUX_EV_SEND_FRM, .name = "send_frm", .desc = "sending QUIC frame" },
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020057/* special event dedicated to qcs_xfer_data */
58#define QMUX_EV_QCS_XFER_DATA (1ULL << 14)
59 { .mask = QMUX_EV_QCS_XFER_DATA, .name = "qcs_xfer_data", .desc = "qcs_xfer_data" },
60/* special event dedicated to qcs_build_stream_frm */
61#define QMUX_EV_QCS_BUILD_STRM (1ULL << 15)
62 { .mask = QMUX_EV_QCS_BUILD_STRM, .name = "qcs_build_stream_frm", .desc = "qcs_build_stream_frm" },
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +020063#define QMUX_EV_PROTO_ERR (1ULL << 16)
64 { .mask = QMUX_EV_PROTO_ERR, .name = "proto_err", .desc = "protocol error" },
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010065 { }
66};
67
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020068/* custom arg for QMUX_EV_QCS_XFER_DATA */
69struct qcs_xfer_data_trace_arg {
70 size_t prep;
Amaury Denoyellefdcec362022-03-25 09:28:10 +010071 int xfer;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020072};
73/* custom arg for QMUX_EV_QCS_BUILD_STRM */
74struct qcs_build_stream_trace_arg {
75 size_t len;
Amaury Denoyellefdcec362022-03-25 09:28:10 +010076 char fin;
77 uint64_t offset;
78};
79
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010080static const struct name_desc qmux_trace_lockon_args[4] = {
81 /* arg1 */ { /* already used by the connection */ },
82 /* arg2 */ { .name="qcs", .desc="QUIC stream" },
83 /* arg3 */ { },
84 /* arg4 */ { }
85};
86
87static const struct name_desc qmux_trace_decoding[] = {
88#define QMUX_VERB_CLEAN 1
89 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
90#define QMUX_VERB_MINIMAL 2
91 { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" },
92 { /* end */ }
93};
94
95struct trace_source trace_qmux = {
96 .name = IST("qmux"),
97 .desc = "QUIC multiplexer",
98 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
99 .default_cb = qmux_trace,
100 .known_events = qmux_trace_events,
101 .lockon_args = qmux_trace_lockon_args,
102 .decoding = qmux_trace_decoding,
103 .report_events = ~0, /* report everything by default */
104};
105
106#define TRACE_SOURCE &trace_qmux
107INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
108
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200109/* Emit a CONNECTION_CLOSE with error <err>. This will interrupt all future
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200110 * send/receive operations.
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200111 */
112static void qcc_emit_cc(struct qcc *qcc, int err)
113{
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200114 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
115
116 TRACE_STATE("set CONNECTION_CLOSE on quic-conn", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle57e6db72022-07-13 15:07:56 +0200117 quic_set_connection_close(qcc->conn->handle.qc, quic_err_transport(err));
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200118 qcc->flags |= QC_CF_CC_EMIT;
119 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200120
121 TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200122}
123
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100124/* Allocate a new QUIC streams with id <id> and type <type>. */
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200125static struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100126{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100127 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100128
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100129 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
130
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100131 qcs = pool_alloc(pool_head_qcs);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200132 if (!qcs) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200133 TRACE_ERROR("alloc failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200134 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200135 }
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200136
137 qcs->stream = NULL;
138 qcs->qcc = qcc;
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +0200139 qcs->sd = NULL;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200140 qcs->flags = QC_SF_NONE;
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200141 qcs->st = QC_SS_IDLE;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200142 qcs->ctx = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100143
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200144 /* App callback attach may register the stream for http-request wait.
145 * These fields must be initialed before.
146 */
147 LIST_INIT(&qcs->el_opening);
148 qcs->start = TICK_ETERNITY;
149
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200150 /* Allocate transport layer stream descriptor. Only needed for TX. */
151 if (!quic_stream_is_uni(id) || !quic_stream_is_remote(qcc, id)) {
152 struct quic_conn *qc = qcc->conn->handle.qc;
153 qcs->stream = qc_stream_desc_new(id, type, qcs, qc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200154 if (!qcs->stream) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200155 TRACE_ERROR("qc_stream_desc alloc failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200156 goto err;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200157 }
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200158 }
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200159
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +0200160 qcs->id = qcs->by_id.key = id;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200161 if (qcc->app_ops->attach) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200162 if (qcc->app_ops->attach(qcs, qcc->ctx)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200163 TRACE_ERROR("app proto failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200164 goto err;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200165 }
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200166 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100167
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200168 /* store transport layer stream descriptor in qcc tree */
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200169 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100170
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100171 qcc->strms[type].nb_streams++;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100172
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100173 /* If stream is local, use peer remote-limit, or else the opposite. */
174 /* TODO use uni limit for unidirectional streams */
175 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
176 qcc->rfctl.msd_bidi_l;
177
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200178 qcs->rx.ncbuf = NCBUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100179 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200180 qcs->rx.offset = qcs->rx.offset_max = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100181
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200182 /* TODO use uni limit for unidirectional streams */
183 qcs->rx.msd = quic_stream_is_local(qcc, id) ? qcc->lfctl.msd_bidi_l :
184 qcc->lfctl.msd_bidi_r;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200185 qcs->rx.msd_init = qcs->rx.msd;
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200186
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100187 qcs->tx.buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100188 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100189 qcs->tx.sent_offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100190
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100191 qcs->wait_event.tasklet = NULL;
192 qcs->wait_event.events = 0;
193 qcs->subs = NULL;
194
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200195 qcs->err = 0;
196
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100197 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100198 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100199 return qcs;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200200
201 err:
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200202 if (qcs->ctx && qcc->app_ops->detach)
203 qcc->app_ops->detach(qcs);
204
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200205 qc_stream_desc_release(qcs->stream);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200206 pool_free(pool_head_qcs, qcs);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200207
208 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200209 return NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100210}
211
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200212static void qc_free_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
213{
214 struct buffer buf;
215
Amaury Denoyelle7313f5e2022-05-17 18:53:21 +0200216 if (ncb_is_null(ncbuf))
217 return;
218
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200219 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
220 b_free(&buf);
Amaury Denoyelle7313f5e2022-05-17 18:53:21 +0200221 offer_buffers(NULL, 1);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200222
223 *ncbuf = NCBUF_NULL;
224}
225
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200226/* Free a qcs. This function must only be done to remove a stream on allocation
227 * error or connection shutdown. Else use qcs_destroy which handle all the
228 * QUIC connection mechanism.
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100229 */
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200230static void qcs_free(struct qcs *qcs)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100231{
Amaury Denoyelle6bdf9362022-08-11 18:22:22 +0200232 TRACE_ENTER(QMUX_EV_QCS_END, qcs->qcc->conn, qcs);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200233
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200234 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200235 b_free(&qcs->tx.buf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200236
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200237 BUG_ON(!qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams);
238 --qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams;
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200239
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200240 if (qcs->ctx && qcs->qcc->app_ops->detach)
241 qcs->qcc->app_ops->detach(qcs);
242
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200243 qc_stream_desc_release(qcs->stream);
244
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +0200245 BUG_ON(qcs->sd && !se_fl_test(qcs->sd, SE_FL_ORPHAN));
246 sedesc_free(qcs->sd);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200247
248 eb64_delete(&qcs->by_id);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100249 pool_free(pool_head_qcs, qcs);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200250
251 TRACE_LEAVE(QMUX_EV_QCS_END);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100252}
253
Amaury Denoyelle3abeb572022-07-04 11:42:27 +0200254static forceinline struct stconn *qcs_sc(const struct qcs *qcs)
255{
256 return qcs->sd ? qcs->sd->sc : NULL;
257}
258
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200259/* Reset the <qcc> inactivity timeout for http-keep-alive timeout. */
260static forceinline void qcc_reset_idle_start(struct qcc *qcc)
261{
262 qcc->idle_start = now_ms;
263}
264
Amaury Denoyellec603de42022-07-25 11:21:46 +0200265/* Decrement <qcc> sc. */
266static forceinline void qcc_rm_sc(struct qcc *qcc)
267{
268 BUG_ON_HOT(!qcc->nb_sc);
269 --qcc->nb_sc;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200270
271 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
272 * refreshed after this on stream detach.
273 */
274 if (!qcc->nb_sc && !qcc->nb_hreq)
275 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200276}
277
278/* Decrement <qcc> hreq. */
279static forceinline void qcc_rm_hreq(struct qcc *qcc)
280{
281 BUG_ON_HOT(!qcc->nb_hreq);
282 --qcc->nb_hreq;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200283
284 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
285 * refreshed after this on I/O handler.
286 */
287 if (!qcc->nb_sc && !qcc->nb_hreq)
288 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200289}
290
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200291static inline int qcc_is_dead(const struct qcc *qcc)
292{
293 /* Mux connection is considered dead if :
294 * - all stream-desc are detached AND
295 * = connection is on error OR
296 * = mux timeout has already fired or is unset
297 */
298 if (!qcc->nb_sc && ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task))
299 return 1;
300
301 return 0;
302}
303
304/* Return true if the mux timeout should be armed. */
305static inline int qcc_may_expire(struct qcc *qcc)
306{
307 return !qcc->nb_sc;
308}
309
310/* Refresh the timeout on <qcc> if needed depending on its state. */
311static void qcc_refresh_timeout(struct qcc *qcc)
312{
313 const struct proxy *px = qcc->proxy;
314
315 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
316
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200317 if (!qcc->task) {
318 TRACE_DEVEL("already expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200319 goto leave;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200320 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200321
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200322 /* Check if upper layer is responsible of timeout management. */
323 if (!qcc_may_expire(qcc)) {
324 TRACE_DEVEL("not eligible for timeout", QMUX_EV_QCC_WAKE, qcc->conn);
325 qcc->task->expire = TICK_ETERNITY;
326 task_queue(qcc->task);
327 goto leave;
328 }
329
330 /* TODO if connection is idle on frontend and proxy is disabled, remove
331 * it with global close_spread delay applied.
332 */
333
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200334 /* TODO implement client/server-fin timeout for graceful shutdown */
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200335
336 /* Frontend timeout management
337 * - detached streams with data left to send -> default timeout
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200338 * - stream waiting on incomplete request or no stream yet activated -> timeout http-request
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200339 * - idle after stream processing -> timeout http-keep-alive
340 */
341 if (!conn_is_back(qcc->conn)) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200342 if (qcc->nb_hreq) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200343 TRACE_DEVEL("one or more requests still in progress", QMUX_EV_QCC_WAKE, qcc->conn);
344 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200345 task_queue(qcc->task);
346 goto leave;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200347 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200348
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200349 if (!LIST_ISEMPTY(&qcc->opening_list) || unlikely(!qcc->largest_bidi_r)) {
350 int timeout = px->timeout.httpreq;
351 struct qcs *qcs = NULL;
352 int base_time;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200353
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200354 /* Use start time of first stream waiting on HTTP or
355 * qcc idle if no stream not yet used.
356 */
357 if (likely(!LIST_ISEMPTY(&qcc->opening_list)))
358 qcs = LIST_ELEM(qcc->opening_list.n, struct qcs *, el_opening);
359 base_time = qcs ? qcs->start : qcc->idle_start;
360
361 TRACE_DEVEL("waiting on http request", QMUX_EV_QCC_WAKE, qcc->conn, qcs);
362 qcc->task->expire = tick_add_ifset(base_time, timeout);
363 }
364 else {
365 /* Use http-request timeout if keep-alive timeout not set */
366 int timeout = tick_isset(px->timeout.httpka) ?
367 px->timeout.httpka : px->timeout.httpreq;
368
369 TRACE_DEVEL("at least one request achieved but none currently in progress", QMUX_EV_QCC_WAKE, qcc->conn);
370 qcc->task->expire = tick_add_ifset(qcc->idle_start, timeout);
371 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200372 }
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200373
374 /* fallback to default timeout if frontend specific undefined or for
375 * backend connections.
376 */
377 if (!tick_isset(qcc->task->expire)) {
378 TRACE_DEVEL("fallback to default timeout", QMUX_EV_QCC_WAKE, qcc->conn);
379 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200380 }
381
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200382 task_queue(qcc->task);
383
384 leave:
385 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
386}
387
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200388/* Mark a stream as open if it was idle. This can be used on every
389 * successful emission/reception operation to update the stream state.
390 */
391static void qcs_idle_open(struct qcs *qcs)
392{
393 /* This operation must not be used if the stream is already closed. */
394 BUG_ON_HOT(qcs->st == QC_SS_CLO);
395
396 if (qcs->st == QC_SS_IDLE) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200397 TRACE_STATE("opening stream", QMUX_EV_QCS_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200398 qcs->st = QC_SS_OPEN;
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200399 }
400}
401
402/* Close the local channel of <qcs> instance. */
403static void qcs_close_local(struct qcs *qcs)
404{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200405 TRACE_STATE("closing stream locally", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
406
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200407 /* The stream must have already been opened. */
408 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
409
410 /* This operation cannot be used multiple times. */
411 BUG_ON_HOT(qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO);
412
413 if (quic_stream_is_bidi(qcs->id)) {
414 qcs->st = (qcs->st == QC_SS_HREM) ? QC_SS_CLO : QC_SS_HLOC;
Amaury Denoyellec603de42022-07-25 11:21:46 +0200415 qcc_rm_hreq(qcs->qcc);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200416 }
417 else {
418 /* Only local uni streams are valid for this operation. */
419 BUG_ON_HOT(quic_stream_is_remote(qcs->qcc, qcs->id));
420 qcs->st = QC_SS_CLO;
421 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200422}
423
424/* Close the remote channel of <qcs> instance. */
425static void qcs_close_remote(struct qcs *qcs)
426{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200427 TRACE_STATE("closing stream remotely", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
428
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200429 /* The stream must have already been opened. */
430 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
431
432 /* This operation cannot be used multiple times. */
433 BUG_ON_HOT(qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO);
434
435 if (quic_stream_is_bidi(qcs->id)) {
436 qcs->st = (qcs->st == QC_SS_HLOC) ? QC_SS_CLO : QC_SS_HREM;
437 }
438 else {
439 /* Only remote uni streams are valid for this operation. */
440 BUG_ON_HOT(quic_stream_is_local(qcs->qcc, qcs->id));
441 qcs->st = QC_SS_CLO;
442 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200443}
444
445static int qcs_is_close_local(struct qcs *qcs)
446{
447 return qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO;
448}
449
450static __maybe_unused int qcs_is_close_remote(struct qcs *qcs)
451{
452 return qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO;
453}
454
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100455struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100456{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100457 struct buffer *buf = b_alloc(bptr);
458 BUG_ON(!buf);
459 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100460}
461
Amaury Denoyellea441ec92022-07-04 15:48:57 +0200462static struct ncbuf *qc_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200463{
464 struct buffer buf = BUF_NULL;
465
466 if (ncb_is_null(ncbuf)) {
467 b_alloc(&buf);
468 BUG_ON(b_is_null(&buf));
469
470 *ncbuf = ncb_make(buf.area, buf.size, 0);
471 ncb_init(ncbuf, 0);
472 }
473
474 return ncbuf;
475}
476
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500477/* Notify an eventual subscriber on <qcs> or else wakeup up the stconn layer if
Amaury Denoyelle4561f842022-07-06 14:54:34 +0200478 * initialized.
479 */
480static void qcs_alert(struct qcs *qcs)
481{
482 if (qcs->subs) {
483 qcs_notify_recv(qcs);
484 qcs_notify_send(qcs);
485 }
486 else if (qcs_sc(qcs) && qcs->sd->sc->app_ops->wake) {
487 qcs->sd->sc->app_ops->wake(qcs->sd->sc);
488 }
489}
490
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100491int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
492{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100493 struct qcc *qcc = qcs->qcc;
494
495 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100496
497 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
498 BUG_ON(qcs->subs && qcs->subs != es);
499
500 es->events |= event_type;
501 qcs->subs = es;
502
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100503 if (event_type & SUB_RETRY_RECV)
504 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
505
506 if (event_type & SUB_RETRY_SEND)
507 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
508
509 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
510
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100511 return 0;
512}
513
514void qcs_notify_recv(struct qcs *qcs)
515{
516 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
517 tasklet_wakeup(qcs->subs->tasklet);
518 qcs->subs->events &= ~SUB_RETRY_RECV;
519 if (!qcs->subs->events)
520 qcs->subs = NULL;
521 }
522}
523
524void qcs_notify_send(struct qcs *qcs)
525{
526 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
527 tasklet_wakeup(qcs->subs->tasklet);
528 qcs->subs->events &= ~SUB_RETRY_SEND;
529 if (!qcs->subs->events)
530 qcs->subs = NULL;
531 }
532}
533
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200534/* Open a locally initiated stream for the connection <qcc>. Set <bidi> for a
535 * bidirectional stream, else an unidirectional stream is opened. The next
536 * available ID on the connection will be used according to the stream type.
537 *
538 * Returns the allocated stream instance or NULL on error.
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100539 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200540struct qcs *qcc_init_stream_local(struct qcc *qcc, int bidi)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100541{
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200542 struct qcs *qcs;
543 enum qcs_type type;
544 uint64_t *next;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100545
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200546 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
547
548 if (bidi) {
549 next = &qcc->next_bidi_l;
550 type = conn_is_back(qcc->conn) ? QCS_CLT_BIDI : QCS_SRV_BIDI;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100551 }
552 else {
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200553 next = &qcc->next_uni_l;
554 type = conn_is_back(qcc->conn) ? QCS_CLT_UNI : QCS_SRV_UNI;
555 }
556
557 /* TODO ensure that we won't overflow remote peer flow control limit on
558 * streams. Else, we should emit a STREAMS_BLOCKED frame.
559 */
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100560
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200561 qcs = qcs_new(qcc, *next, type);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200562 if (!qcs) {
563 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200564 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200565 }
Amaury Denoyellec055e302022-02-07 16:09:06 +0100566
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200567 TRACE_PROTO("opening local stream", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200568 *next += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100569
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200570 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200571 return qcs;
572}
573
574/* Open a remote initiated stream for the connection <qcc> with ID <id>. The
575 * caller is responsible to ensure that a stream with the same ID was not
576 * already opened. This function will also create all intermediaries streams
577 * with ID smaller than <id> not already opened before.
578 *
579 * Returns the allocated stream instance or NULL on error.
580 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200581static struct qcs *qcc_init_stream_remote(struct qcc *qcc, uint64_t id)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200582{
583 struct qcs *qcs = NULL;
584 enum qcs_type type;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200585 uint64_t *largest, max_id;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100586
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200587 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200588
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200589 BUG_ON_HOT(quic_stream_is_local(qcc, id));
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100590
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200591 if (quic_stream_is_bidi(id)) {
592 largest = &qcc->largest_bidi_r;
593 type = conn_is_back(qcc->conn) ? QCS_SRV_BIDI : QCS_CLT_BIDI;
594 }
595 else {
596 largest = &qcc->largest_uni_r;
597 type = conn_is_back(qcc->conn) ? QCS_SRV_UNI : QCS_CLT_UNI;
598 }
599
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200600 /* RFC 9000 4.6. Controlling Concurrency
601 *
602 * An endpoint that receives a frame with a stream ID exceeding the
603 * limit it has sent MUST treat this as a connection error of type
604 * STREAM_LIMIT_ERROR
605 */
606 max_id = quic_stream_is_bidi(id) ? qcc->lfctl.ms_bidi * 4 :
607 qcc->lfctl.ms_uni * 4;
608 if (id >= max_id) {
609 TRACE_ERROR("flow control error", QMUX_EV_QCS_NEW|QMUX_EV_PROTO_ERR, qcc->conn);
610 qcc_emit_cc(qcc, QC_ERR_STREAM_LIMIT_ERROR);
611 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200612 }
613
614 /* Only stream ID not already opened can be used. */
615 BUG_ON(id < *largest);
616
617 while (id >= *largest) {
618 const char *str = *largest < id ? "opening intermediary stream" :
619 "opening remote stream";
620
621 qcs = qcs_new(qcc, *largest, type);
622 if (!qcs) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200623 /* TODO emit RESET_STREAM */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200624 TRACE_ERROR("stream fallocation failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200625 goto err;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100626 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200627
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200628 TRACE_PROTO(str, QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200629 *largest += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100630 }
631
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200632 out:
633 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle50742292022-03-29 14:57:19 +0200634 return qcs;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200635
636 err:
637 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
638 return NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200639}
640
641/* Use this function for a stream <id> which is not in <qcc> stream tree. It
642 * returns true if the associated stream is closed.
643 */
644static int qcc_stream_id_is_closed(struct qcc *qcc, uint64_t id)
645{
646 uint64_t *largest;
647
648 /* This function must only be used for stream not present in the stream tree. */
649 BUG_ON_HOT(eb64_lookup(&qcc->streams_by_id, id));
650
651 if (quic_stream_is_local(qcc, id)) {
652 largest = quic_stream_is_uni(id) ? &qcc->next_uni_l :
653 &qcc->next_bidi_l;
654 }
655 else {
656 largest = quic_stream_is_uni(id) ? &qcc->largest_uni_r :
657 &qcc->largest_bidi_r;
658 }
659
660 return id < *largest;
661}
662
663/* Retrieve the stream instance from <id> ID. This can be used when receiving
664 * STREAM, STREAM_DATA_BLOCKED, RESET_STREAM, MAX_STREAM_DATA or STOP_SENDING
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200665 * frames. Set to false <receive_only> or <send_only> if these particular types
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200666 * of streams are not allowed. If the stream instance is found, it is stored in
667 * <out>.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200668 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200669 * Returns 0 on success else non-zero. On error, a RESET_STREAM or a
670 * CONNECTION_CLOSE is automatically emitted. Beware that <out> may be NULL
671 * on success if the stream has already been closed.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200672 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200673int qcc_get_qcs(struct qcc *qcc, uint64_t id, int receive_only, int send_only,
674 struct qcs **out)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200675{
676 struct eb64_node *node;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200677
678 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200679 *out = NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200680
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200681 if (!receive_only && quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200682 TRACE_ERROR("receive-only stream not allowed", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id);
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200683 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200684 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200685 }
686
687 if (!send_only && quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200688 TRACE_ERROR("send-only stream not allowed", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id);
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200689 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200690 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200691 }
692
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200693 /* Search the stream in the connection tree. */
694 node = eb64_lookup(&qcc->streams_by_id, id);
695 if (node) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200696 *out = eb64_entry(node, struct qcs, by_id);
697 TRACE_DEVEL("using stream from connection tree", QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200698 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200699 }
700
701 /* Check if stream is already closed. */
702 if (qcc_stream_id_is_closed(qcc, id)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200703 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200704 /* Consider this as a success even if <out> is left NULL. */
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200705 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200706 }
707
708 /* Create the stream. This is valid only for remote initiated one. A
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500709 * local stream must have already been explicitly created by the
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200710 * application protocol layer.
711 */
712 if (quic_stream_is_local(qcc, id)) {
713 /* RFC 9000 19.8. STREAM Frames
714 *
715 * An endpoint MUST terminate the connection with error
716 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
717 * initiated stream that has not yet been created, or for a send-only
718 * stream.
719 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200720 TRACE_ERROR("locally initiated stream not yet created", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200721 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200722 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200723 }
724 else {
725 /* Remote stream not found - try to open it. */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200726 *out = qcc_init_stream_remote(qcc, id);
727 if (!*out) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200728 TRACE_ERROR("stream creation error", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200729 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200730 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200731 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100732
733 out:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200734 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200735 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200736
737 err:
738 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
739 return 1;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100740}
741
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200742/* Simple function to duplicate a buffer */
743static inline struct buffer qcs_b_dup(const struct ncbuf *b)
744{
745 return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0));
746}
747
Amaury Denoyelle36d4b5e2022-07-01 11:25:40 +0200748/* Remove <bytes> from <qcs> Rx buffer. Flow-control for received offsets may
749 * be allocated for the peer if needed.
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200750 */
751static void qcs_consume(struct qcs *qcs, uint64_t bytes)
752{
753 struct qcc *qcc = qcs->qcc;
754 struct quic_frame *frm;
755 struct ncbuf *buf = &qcs->rx.ncbuf;
756 enum ncb_ret ret;
757
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200758 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
759
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200760 ret = ncb_advance(buf, bytes);
761 if (ret) {
762 ABORT_NOW(); /* should not happens because removal only in data */
763 }
764
765 if (ncb_is_empty(buf))
766 qc_free_ncbuf(qcs, buf);
767
768 qcs->rx.offset += bytes;
769 if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200770 TRACE_DATA("increase stream credit via MAX_STREAM_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200771 frm = pool_zalloc(pool_head_quic_frame);
772 BUG_ON(!frm); /* TODO handle this properly */
773
774 qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init;
775
776 LIST_INIT(&frm->reflist);
777 frm->type = QUIC_FT_MAX_STREAM_DATA;
778 frm->max_stream_data.id = qcs->id;
779 frm->max_stream_data.max_stream_data = qcs->rx.msd;
780
781 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
782 tasklet_wakeup(qcc->wait_event.tasklet);
783 }
784
785 qcc->lfctl.offsets_consume += bytes;
786 if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200787 TRACE_DATA("increase conn credit via MAX_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200788 frm = pool_zalloc(pool_head_quic_frame);
789 BUG_ON(!frm); /* TODO handle this properly */
790
791 qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init;
792
793 LIST_INIT(&frm->reflist);
794 frm->type = QUIC_FT_MAX_DATA;
795 frm->max_data.max_data = qcc->lfctl.md;
796
797 LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list);
798 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
799 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200800
801 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200802}
803
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200804/* Decode the content of STREAM frames already received on the stream instance
805 * <qcs>.
806 *
807 * Returns 0 on success else non-zero.
808 */
809static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
810{
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200811 struct buffer b;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200812 ssize_t ret;
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200813 int fin = 0;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200814
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200815 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
816
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200817 b = qcs_b_dup(&qcs->rx.ncbuf);
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200818
819 /* Signal FIN to application if STREAM FIN received and there is no gap
820 * in the Rx buffer.
821 */
Amaury Denoyelle3f39b402022-07-01 16:11:03 +0200822 if (qcs->flags & QC_SF_SIZE_KNOWN && !ncb_is_fragmented(&qcs->rx.ncbuf))
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200823 fin = 1;
824
825 ret = qcc->app_ops->decode_qcs(qcs, &b, fin);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200826 if (ret < 0) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200827 TRACE_ERROR("decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200828 goto err;
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200829 }
830
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200831 if (ret) {
832 qcs_consume(qcs, ret);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200833 qcs_notify_recv(qcs);
834 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200835
836 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200837 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200838
839 err:
840 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
841 return 1;
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200842}
843
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200844/* Emit a CONNECTION_CLOSE_APP with error <err>. Reserved for application error
Amaury Denoyelled666d742022-07-13 15:15:58 +0200845 * code. To close the connection right away, set <immediate> : this is useful
846 * when dealing with a connection fatal error. Else a graceful shutdown will be
847 * conducted : the error-code is only registered. The lower layer is
848 * responsible to close the connection when deemed suitable. Note that in this
849 * case the error code might be overwritten if an immediate close is requested
850 * in the interval.
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200851 */
Amaury Denoyelled666d742022-07-13 15:15:58 +0200852void qcc_emit_cc_app(struct qcc *qcc, int err, int immediate)
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200853{
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200854 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
855
Amaury Denoyelled666d742022-07-13 15:15:58 +0200856 if (immediate) {
857 quic_set_connection_close(qcc->conn->handle.qc, quic_err_app(err));
858 qcc->flags |= QC_CF_CC_EMIT;
859 tasklet_wakeup(qcc->wait_event.tasklet);
860 }
861 else {
862 /* Only register the error code for graceful shutdown. */
863 qcc->conn->handle.qc->err = quic_err_app(err);
864 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200865
866 TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200867}
868
869/* Prepare for the emission of RESET_STREAM on <qcs> with error code <err>. */
870void qcc_reset_stream(struct qcs *qcs, int err)
871{
872 struct qcc *qcc = qcs->qcc;
873
874 if ((qcs->flags & QC_SF_TO_RESET) || qcs_is_close_local(qcs))
875 return;
876
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200877 TRACE_STATE("reset stream", QMUX_EV_QCS_END, qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200878 qcs->flags |= QC_SF_TO_RESET;
879 qcs->err = err;
880 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200881}
882
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200883/* Install the <app_ops> applicative layer of a QUIC connection on mux <qcc>.
884 * Returns 0 on success else non-zero.
885 */
886int qcc_install_app_ops(struct qcc *qcc, const struct qcc_app_ops *app_ops)
887{
888 TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn);
889
890 qcc->app_ops = app_ops;
891 if (qcc->app_ops->init && !qcc->app_ops->init(qcc)) {
892 TRACE_ERROR("app ops init error", QMUX_EV_QCC_NEW, qcc->conn);
893 goto err;
894 }
895
896 TRACE_PROTO("application layer initialized", QMUX_EV_QCC_NEW, qcc->conn);
897
898 if (qcc->app_ops->finalize)
899 qcc->app_ops->finalize(qcc->ctx);
900
901 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
902 return 0;
903
904 err:
905 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
906 return 1;
907}
908
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200909/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
910 * <data> with length <len> and represents the offset <offset>. <fin> is set if
911 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100912 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200913 * Returns 0 on success else non-zero. On error, the received frame should not
914 * be acknowledged.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100915 */
916int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200917 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100918{
919 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200920 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100921
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100922 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
923
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200924 if (qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200925 TRACE_DATA("connection closed", QMUX_EV_QCC_RECV, qcc->conn);
926 goto err;
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200927 }
928
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200929 /* RFC 9000 19.8. STREAM Frames
930 *
931 * An endpoint MUST terminate the connection with error
932 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
933 * initiated stream that has not yet been created, or for a send-only
934 * stream.
935 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200936 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200937 TRACE_DATA("qcs retrieval error", QMUX_EV_QCC_RECV, qcc->conn);
938 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200939 }
940
941 if (!qcs) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200942 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV, qcc->conn);
943 goto out;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200944 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100945
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200946 /* RFC 9000 4.5. Stream Final Size
947 *
948 * Once a final size for a stream is known, it cannot change. If a
949 * RESET_STREAM or STREAM frame is received indicating a change in the
950 * final size for the stream, an endpoint SHOULD respond with an error
951 * of type FINAL_SIZE_ERROR; see Section 11 for details on error
952 * handling.
953 */
954 if (qcs->flags & QC_SF_SIZE_KNOWN &&
955 (offset + len > qcs->rx.offset_max || (fin && offset + len < qcs->rx.offset_max))) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200956 TRACE_ERROR("final size error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR, qcc->conn, qcs);
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200957 qcc_emit_cc(qcc, QC_ERR_FINAL_SIZE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200958 goto err;
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200959 }
960
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100961 if (offset + len <= qcs->rx.offset) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200962 TRACE_DATA("already received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
963 goto out;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100964 }
965
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200966 TRACE_PROTO("receiving STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200967 qcs_idle_open(qcs);
968
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200969 if (offset + len > qcs->rx.offset_max) {
970 uint64_t diff = offset + len - qcs->rx.offset_max;
971 qcs->rx.offset_max = offset + len;
972 qcc->lfctl.offsets_recv += diff;
973
974 if (offset + len > qcs->rx.msd ||
975 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
976 /* RFC 9000 4.1. Data Flow Control
977 *
978 * A receiver MUST close the connection with an error
979 * of type FLOW_CONTROL_ERROR if the sender violates
980 * the advertised connection or stream data limits
981 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200982 TRACE_ERROR("flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200983 qcc->conn, qcs);
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200984 qcc_emit_cc(qcc, QC_ERR_FLOW_CONTROL_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200985 goto err;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200986 }
987 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100988
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200989 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100990 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200991 ABORT_NOW();
992 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100993 }
994
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200995 TRACE_DATA("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200996 if (offset < qcs->rx.offset) {
Frédéric Lécaillea18c3332022-07-04 09:54:58 +0200997 size_t diff = qcs->rx.offset - offset;
998
999 len -= diff;
1000 data += diff;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001001 offset = qcs->rx.offset;
1002 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001003
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001004 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
1005 if (ret != NCB_RET_OK) {
1006 if (ret == NCB_RET_DATA_REJ) {
Amaury Denoyellecc3d7162022-05-20 15:14:57 +02001007 /* RFC 9000 2.2. Sending and Receiving Data
1008 *
1009 * An endpoint could receive data for a stream at the
1010 * same stream offset multiple times. Data that has
1011 * already been received can be discarded. The data at
1012 * a given offset MUST NOT change if it is sent
1013 * multiple times; an endpoint MAY treat receipt of
1014 * different data at the same offset within a stream as
1015 * a connection error of type PROTOCOL_VIOLATION.
1016 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +02001017 TRACE_ERROR("overlapping data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001018 qcc->conn, qcs);
Amaury Denoyellecc3d7162022-05-20 15:14:57 +02001019 qcc_emit_cc(qcc, QC_ERR_PROTOCOL_VIOLATION);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001020 }
1021 else if (ret == NCB_RET_GAP_SIZE) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001022 TRACE_DATA("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
1023 qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001024 }
1025 return 1;
1026 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001027
1028 if (fin)
Amaury Denoyelle3f39b402022-07-01 16:11:03 +02001029 qcs->flags |= QC_SF_SIZE_KNOWN;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001030
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001031 if (qcs->flags & QC_SF_SIZE_KNOWN && !ncb_is_fragmented(&qcs->rx.ncbuf))
1032 qcs_close_remote(qcs);
1033
Amaury Denoyelle418ba212022-08-02 15:57:16 +02001034 if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001035 qcc_decode_qcs(qcc, qcs);
Amaury Denoyelle418ba212022-08-02 15:57:16 +02001036 qcc_refresh_timeout(qcc);
1037 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001038
Amaury Denoyelle849b24f2022-05-24 17:22:07 +02001039 if (qcs->flags & QC_SF_READ_ABORTED) {
1040 /* TODO should send a STOP_SENDING */
1041 qcs_free(qcs);
1042 }
1043
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001044 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001045 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001046 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001047
1048 err:
1049 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1050 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001051}
1052
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001053/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
1054 * the frame.
1055 *
1056 * Returns 0 on success else non-zero.
1057 */
1058int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
1059{
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001060 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1061
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001062 TRACE_PROTO("receiving MAX_DATA", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001063 if (qcc->rfctl.md < max) {
1064 qcc->rfctl.md = max;
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001065 TRACE_DEVEL("increase remote max-data", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001066
1067 if (qcc->flags & QC_CF_BLK_MFCTL) {
1068 qcc->flags &= ~QC_CF_BLK_MFCTL;
1069 tasklet_wakeup(qcc->wait_event.tasklet);
1070 }
1071 }
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001072
1073 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001074 return 0;
1075}
1076
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001077/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
1078 * field of the frame and <id> is the identifier of the QUIC stream.
1079 *
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001080 * Returns 0 on success else non-zero. On error, the received frame should not
1081 * be acknowledged.
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001082 */
1083int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
1084{
1085 struct qcs *qcs;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001086
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001087 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1088
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001089 /* RFC 9000 19.10. MAX_STREAM_DATA Frames
1090 *
1091 * Receiving a MAX_STREAM_DATA frame for a locally
1092 * initiated stream that has not yet been created MUST be treated as a
1093 * connection error of type STREAM_STATE_ERROR. An endpoint that
1094 * receives a MAX_STREAM_DATA frame for a receive-only stream MUST
1095 * terminate the connection with error STREAM_STATE_ERROR.
1096 */
1097 if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) {
1098 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1099 return 1;
1100 }
1101
1102 if (qcs) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001103 TRACE_PROTO("receiving MAX_STREAM_DATA", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001104 if (max > qcs->tx.msd) {
1105 qcs->tx.msd = max;
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001106 TRACE_DEVEL("increase remote max-stream-data", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001107
1108 if (qcs->flags & QC_SF_BLK_SFCTL) {
1109 qcs->flags &= ~QC_SF_BLK_SFCTL;
1110 tasklet_wakeup(qcc->wait_event.tasklet);
1111 }
1112 }
1113 }
1114
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001115 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1116 qcc_refresh_timeout(qcc);
1117
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001118 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001119 return 0;
1120}
1121
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001122/* Handle a new STOP_SENDING frame for stream ID <id>. The error code should be
1123 * specified in <err>.
1124 *
1125 * Returns 0 on success else non-zero. On error, the received frame should not
1126 * be acknowledged.
1127 */
1128int qcc_recv_stop_sending(struct qcc *qcc, uint64_t id, uint64_t err)
1129{
1130 struct qcs *qcs;
1131
1132 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1133
1134 /* RFC 9000 19.5. STOP_SENDING Frames
1135 *
1136 * Receiving a STOP_SENDING frame for a
1137 * locally initiated stream that has not yet been created MUST be
1138 * treated as a connection error of type STREAM_STATE_ERROR. An
1139 * endpoint that receives a STOP_SENDING frame for a receive-only stream
1140 * MUST terminate the connection with error STREAM_STATE_ERROR.
1141 */
1142 if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) {
1143 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1144 return 1;
1145 }
1146
1147 if (!qcs)
1148 goto out;
1149
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001150 TRACE_PROTO("receiving STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle96ca1b72022-08-09 17:36:38 +02001151 qcs_idle_open(qcs);
1152
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001153 /* RFC 9000 3.5. Solicited State Transitions
1154 *
1155 * An endpoint that receives a STOP_SENDING frame
1156 * MUST send a RESET_STREAM frame if the stream is in the "Ready" or
1157 * "Send" state. If the stream is in the "Data Sent" state, the
1158 * endpoint MAY defer sending the RESET_STREAM frame until the packets
1159 * containing outstanding data are acknowledged or declared lost. If
1160 * any outstanding data is declared lost, the endpoint SHOULD send a
1161 * RESET_STREAM frame instead of retransmitting the data.
1162 *
1163 * An endpoint SHOULD copy the error code from the STOP_SENDING frame to
1164 * the RESET_STREAM frame it sends, but it can use any application error
1165 * code.
1166 */
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001167 qcc_reset_stream(qcs, err);
1168
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001169 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1170 qcc_refresh_timeout(qcc);
1171
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001172 out:
1173 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1174 return 0;
1175}
1176
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001177/* Signal the closing of remote stream with id <id>. Flow-control for new
1178 * streams may be allocated for the peer if needed.
1179 */
1180static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +01001181{
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001182 struct quic_frame *frm;
1183
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001184 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn);
1185
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001186 if (quic_stream_is_bidi(id)) {
1187 ++qcc->lfctl.cl_bidi_r;
1188 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001189 TRACE_DATA("increase max stream limit with MAX_STREAMS_BIDI", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001190 frm = pool_zalloc(pool_head_quic_frame);
1191 BUG_ON(!frm); /* TODO handle this properly */
1192
1193 LIST_INIT(&frm->reflist);
1194 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
1195 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
1196 qcc->lfctl.cl_bidi_r;
1197 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
1198 tasklet_wakeup(qcc->wait_event.tasklet);
1199
1200 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
1201 qcc->lfctl.cl_bidi_r = 0;
1202 }
1203 }
1204 else {
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02001205 /* TODO in HTTP/3 unidirectional streams cannot be closed or a
1206 * H3_CLOSED_CRITICAL_STREAM will be triggered before
1207 * entering here. If a new application protocol is supported it
1208 * might be necessary to implement MAX_STREAMS_UNI emission.
1209 */
1210 ABORT_NOW();
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001211 }
1212
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001213 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
1214
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001215 return 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001216}
1217
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05001218/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001219static void qcs_destroy(struct qcs *qcs)
1220{
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001221 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001222 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001223
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001224 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001225
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001226 if (quic_stream_is_remote(qcs->qcc, id))
1227 qcc_release_remote_stream(qcs->qcc, id);
Amaury Denoyellec055e302022-02-07 16:09:06 +01001228
Amaury Denoyelledccbd732022-03-29 18:36:59 +02001229 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001230
1231 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001232}
1233
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001234/* Transfer as much as possible data on <qcs> from <in> to <out>. This is done
1235 * in respect with available flow-control at stream and connection level.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001236 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001237 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001238 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001239static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, struct buffer *in)
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001240{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001241 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001242 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001243 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001244
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001245 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001246
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001247 qc_get_buf(qcs, out);
1248
1249 /*
1250 * QCS out buffer diagram
1251 * head left to_xfer
1252 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001253 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001254 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001255 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001256 * ^ ack-off ^ sent-off ^ off
1257 *
1258 * STREAM frame
1259 * ^ ^
1260 * |xxxxxxxxxxxxxxxxx|
1261 */
1262
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001263 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001264 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001265 BUG_ON_HOT(qcc->tx.offsets < qcc->tx.sent_offsets);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001266
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001267 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001268 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001269
1270 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
1271 /* do not exceed flow control limit */
1272 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
1273 to_xfer = qcs->tx.msd - qcs->tx.offset;
1274
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001275 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001276 /* do not overcome flow control limit on connection */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001277 if (qcc->tx.offsets + to_xfer > qcc->rfctl.md)
1278 to_xfer = qcc->rfctl.md - qcc->tx.offsets;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001279
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001280 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001281 goto out;
1282
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001283 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001284
1285 out:
1286 {
1287 struct qcs_xfer_data_trace_arg arg = {
1288 .prep = b_data(out), .xfer = total,
1289 };
1290 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
1291 qcc->conn, qcs, &arg);
1292 }
1293
1294 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001295}
1296
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001297/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
1298 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
1299 * stream frame.
1300 *
1301 * Returns the length of the STREAM frame or a negative error code.
1302 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001303static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
1304 struct list *frm_list)
1305{
1306 struct qcc *qcc = qcs->qcc;
1307 struct quic_frame *frm;
1308 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001309 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001310
1311 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1312
Amaury Denoyellea4569202022-04-15 17:29:25 +02001313 /* if ack_offset < buf_offset, it points to an older buffer. */
1314 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
1315 BUG_ON(qcs->tx.sent_offset < base_off);
1316
1317 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001318 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001319 BUG_ON(total < 0);
1320
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001321 if (!total && !fin) {
1322 /* No need to send anything if total is NULL and no FIN to signal. */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001323 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1324 return 0;
1325 }
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001326 BUG_ON((!total && qcs->tx.sent_offset > qcs->tx.offset) ||
1327 (total && qcs->tx.sent_offset >= qcs->tx.offset));
Amaury Denoyellea4569202022-04-15 17:29:25 +02001328 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001329 BUG_ON(qcc->tx.sent_offsets + total > qcc->rfctl.md);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001330
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001331 TRACE_PROTO("sending STREAM frame", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001332 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001333 if (!frm) {
1334 TRACE_ERROR("frame alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001335 goto err;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001336 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001337
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001338 LIST_INIT(&frm->reflist);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001339 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001340 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001341 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001342 frm->stream.buf = out;
1343 frm->stream.data = (unsigned char *)b_peek(out, head);
1344
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +01001345 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001346 if (fin)
1347 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001348
1349 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001350 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001351 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001352 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001353
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001354 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
1355 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001356
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001357 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001358
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001359 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001360 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001361 struct qcs_build_stream_trace_arg arg = {
1362 .len = frm->stream.len, .fin = fin,
1363 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001364 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001365 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001366 qcc->conn, qcs, &arg);
1367 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001368
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001369 return total;
1370
1371 err:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001372 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001373 return -1;
1374}
1375
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001376/* Check after transferring data from qcs.tx.buf if FIN must be set on the next
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001377 * STREAM frame for <qcs>.
1378 *
1379 * Returns true if FIN must be set else false.
1380 */
1381static int qcs_stream_fin(struct qcs *qcs)
1382{
1383 return qcs->flags & QC_SF_FIN_STREAM && !b_data(&qcs->tx.buf);
1384}
1385
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001386/* This function must be called by the upper layer to inform about the sending
1387 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
1388 * <offset>.
1389 */
1390void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
1391{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001392 struct qcc *qcc = qcs->qcc;
1393 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001394
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001395 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1396
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001397 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001398 BUG_ON(offset + data > qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001399
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001400 /* check if the STREAM frame has already been notified. It can happen
1401 * for retransmission.
1402 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001403 if (offset + data < qcs->tx.sent_offset) {
1404 TRACE_DEVEL("offset already notified", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1405 goto out;
1406 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001407
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001408 qcs_idle_open(qcs);
1409
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001410 diff = offset + data - qcs->tx.sent_offset;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001411 if (diff) {
1412 /* increase offset sum on connection */
1413 qcc->tx.sent_offsets += diff;
1414 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
1415 if (qcc->tx.sent_offsets == qcc->rfctl.md)
1416 qcc->flags |= QC_CF_BLK_MFCTL;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001417
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001418 /* increase offset on stream */
1419 qcs->tx.sent_offset += diff;
1420 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
1421 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
1422 if (qcs->tx.sent_offset == qcs->tx.msd)
1423 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001424
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001425 if (qcs->tx.offset == qcs->tx.sent_offset &&
1426 b_full(&qcs->stream->buf->buf)) {
1427 qc_stream_buf_release(qcs->stream);
1428 /* prepare qcs for immediate send retry if data to send */
1429 if (b_data(&qcs->tx.buf))
1430 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
1431 }
1432 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001433
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001434 if (qcs->tx.offset == qcs->tx.sent_offset && qcs_stream_fin(qcs)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001435 /* Close stream locally. */
1436 qcs_close_local(qcs);
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001437 /* Reset flag to not emit multiple FIN STREAM frames. */
1438 qcs->flags &= ~QC_SF_FIN_STREAM;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001439 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001440
1441 out:
1442 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001443}
1444
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001445/* Wrapper for send on transport layer. Send a list of frames <frms> for the
1446 * connection <qcc>.
1447 *
1448 * Returns 0 if all data sent with success else non-zero.
1449 */
1450static int qc_send_frames(struct qcc *qcc, struct list *frms)
1451{
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001452 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
1453
1454 if (LIST_ISEMPTY(frms)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001455 TRACE_DEVEL("no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
1456 goto err;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001457 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +01001458
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001459 LIST_INIT(&qcc->send_retry_list);
1460
Amaury Denoyelle56c61542022-08-04 10:11:12 +02001461 qc_send_app_pkts(qcc->conn->handle.qc, 0, frms);
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001462
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001463 /* If there is frames left at this stage, transport layer is blocked.
1464 * Subscribe on it to retry later.
1465 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001466 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001467 TRACE_DEVEL("remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001468 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1469 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001470 goto err;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001471 }
1472
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001473 TRACE_LEAVE(QMUX_EV_QCC_SEND);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001474 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001475
1476 err:
1477 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1478 return 1;
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001479}
1480
1481/* Emit a RESET_STREAM on <qcs>.
1482 *
1483 * Returns 0 if the frame has been successfully sent else non-zero.
1484 */
1485static int qcs_send_reset(struct qcs *qcs)
1486{
1487 struct list frms = LIST_HEAD_INIT(frms);
1488 struct quic_frame *frm;
1489
1490 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1491
1492 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001493 if (!frm) {
1494 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001495 return 1;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001496 }
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001497
1498 LIST_INIT(&frm->reflist);
1499 frm->type = QUIC_FT_RESET_STREAM;
1500 frm->reset_stream.id = qcs->id;
1501 frm->reset_stream.app_error_code = qcs->err;
1502 frm->reset_stream.final_size = qcs->tx.sent_offset;
1503
1504 LIST_APPEND(&frms, &frm->list);
1505 if (qc_send_frames(qcs->qcc, &frms)) {
1506 pool_free(pool_head_quic_frame, frm);
1507 TRACE_DEVEL("cannot send RESET_STREAM", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1508 return 1;
1509 }
1510
1511 if (qcs_sc(qcs)) {
1512 se_fl_set_error(qcs->sd);
1513 qcs_alert(qcs);
1514 }
1515
1516 qcs_close_local(qcs);
1517 qcs->flags &= ~QC_SF_TO_RESET;
1518
1519 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001520 return 0;
1521}
1522
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001523/* Used internally by qc_send function. Proceed to send for <qcs>. This will
1524 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001525 * is then generated and inserted in <frms> list.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001526 *
1527 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
1528 * be null if out buffer cannot be allocated.
1529 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001530static int _qc_send_qcs(struct qcs *qcs, struct list *frms)
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001531{
1532 struct qcc *qcc = qcs->qcc;
1533 struct buffer *buf = &qcs->tx.buf;
1534 struct buffer *out = qc_stream_buf_get(qcs->stream);
1535 int xfer = 0;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001536 char fin = 0;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001537
1538 /* Allocate <out> buffer if necessary. */
1539 if (!out) {
1540 if (qcc->flags & QC_CF_CONN_FULL)
1541 return 0;
1542
1543 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
1544 if (!out) {
1545 qcc->flags |= QC_CF_CONN_FULL;
1546 return 0;
1547 }
1548 }
1549
1550 /* Transfer data from <buf> to <out>. */
1551 if (b_data(buf)) {
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001552 xfer = qcs_xfer_data(qcs, out, buf);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001553 if (xfer > 0) {
1554 qcs_notify_send(qcs);
1555 qcs->flags &= ~QC_SF_BLK_MROOM;
1556 }
1557
1558 qcs->tx.offset += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001559 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001560 qcc->tx.offsets += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001561 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001562 }
1563
1564 /* out buffer cannot be emptied if qcs offsets differ. */
1565 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1566
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001567 /* FIN is set if all incoming data were transferred. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001568 fin = qcs_stream_fin(qcs);
1569
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001570 /* Build a new STREAM frame with <out> buffer. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001571 if (qcs->tx.sent_offset != qcs->tx.offset || fin) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001572 int ret;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001573 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +02001574 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001575 }
1576
1577 return xfer;
1578}
1579
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001580/* Proceed to sending. Loop through all available streams for the <qcc>
1581 * instance and try to send as much as possible.
1582 *
1583 * Returns the total of bytes sent to the transport layer.
1584 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001585static int qc_send(struct qcc *qcc)
1586{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001587 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001588 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001589 struct qcs *qcs, *qcs_tmp;
1590 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001591
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001592 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001593
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001594 if (qcc->conn->flags & CO_FL_SOCK_WR_SH || qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001595 qcc->conn->flags |= CO_FL_ERROR;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001596 TRACE_DEVEL("connection on error", QMUX_EV_QCC_SEND, qcc->conn);
1597 goto err;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001598 }
1599
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001600 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1601 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
1602 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
1603 goto out;
1604 }
1605 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02001606
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001607 if (qcc->flags & QC_CF_BLK_MFCTL)
1608 return 0;
1609
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001610 /* loop through all streams, construct STREAM frames if data available.
1611 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001612 */
1613 node = eb64_first(&qcc->streams_by_id);
1614 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001615 int ret;
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001616 uint64_t id;
1617
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001618 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001619 id = qcs->id;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001620
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001621 if (quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001622 node = eb64_next(node);
1623 continue;
1624 }
1625
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001626 if (qcs->flags & QC_SF_TO_RESET) {
1627 qcs_send_reset(qcs);
1628 node = eb64_next(node);
1629 continue;
1630 }
1631
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001632 if (qcs_is_close_local(qcs)) {
1633 node = eb64_next(node);
1634 continue;
1635 }
1636
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001637 if (qcs->flags & QC_SF_BLK_SFCTL) {
1638 node = eb64_next(node);
1639 continue;
1640 }
1641
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001642 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001643 node = eb64_next(node);
1644 continue;
1645 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001646
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001647 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001648 total += ret;
1649 node = eb64_next(node);
1650 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001651
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001652 if (qc_send_frames(qcc, &frms)) {
1653 /* data rejected by transport layer, do not retry. */
1654 goto out;
1655 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001656
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001657 retry:
1658 tmp_total = 0;
1659 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
1660 int ret;
1661 BUG_ON(!b_data(&qcs->tx.buf));
1662 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001663
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001664 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001665 tmp_total += ret;
1666 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001667 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001668
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001669 total += tmp_total;
1670 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
1671 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +01001672
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001673 out:
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02001674 /* Deallocate frames that the transport layer has rejected. */
1675 if (!LIST_ISEMPTY(&frms)) {
1676 struct quic_frame *frm, *frm2;
1677 list_for_each_entry_safe(frm, frm2, &frms, list) {
1678 LIST_DELETE(&frm->list);
1679 pool_free(pool_head_quic_frame, frm);
1680 }
1681 }
1682
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001683 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001684 return total;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001685
1686 err:
1687 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
1688 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001689}
1690
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001691/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1692 * operation.
1693 *
1694 * Returns 0 on success else non-zero.
1695 */
1696static int qc_recv(struct qcc *qcc)
1697{
1698 struct eb64_node *node;
1699 struct qcs *qcs;
1700
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001701 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001702
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001703 if (qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001704 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001705 return 0;
1706 }
1707
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001708 node = eb64_first(&qcc->streams_by_id);
1709 while (node) {
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001710 uint64_t id;
1711
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001712 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001713 id = qcs->id;
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001714
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001715 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001716 node = eb64_next(node);
1717 continue;
1718 }
1719
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001720 if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001721 node = eb64_next(node);
1722 continue;
1723 }
1724
1725 qcc_decode_qcs(qcc, qcs);
1726 node = eb64_next(node);
Amaury Denoyelle849b24f2022-05-24 17:22:07 +02001727
1728 if (qcs->flags & QC_SF_READ_ABORTED) {
1729 /* TODO should send a STOP_SENDING */
1730 qcs_free(qcs);
1731 }
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001732 }
1733
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001734 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001735 return 0;
1736}
1737
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001738
1739/* Release all streams which have their transfer operation achieved.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001740 *
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001741 * Returns true if at least one stream is released.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001742 */
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001743static int qc_purge_streams(struct qcc *qcc)
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001744{
1745 struct eb64_node *node;
1746 int release = 0;
1747
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001748 TRACE_ENTER(QMUX_EV_QCC_WAKE);
1749
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001750 node = eb64_first(&qcc->streams_by_id);
1751 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001752 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001753 node = eb64_next(node);
1754
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001755 /* Release not attached closed streams. */
1756 if (qcs->st == QC_SS_CLO && !qcs_sc(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001757 TRACE_STATE("purging closed stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001758 qcs_destroy(qcs);
1759 release = 1;
1760 continue;
1761 }
1762
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001763 /* Release detached streams with empty buffer. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001764 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02001765 if (qcs_is_close_local(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001766 TRACE_STATE("purging detached stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001767 qcs_destroy(qcs);
1768 release = 1;
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001769 continue;
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001770 }
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001771
1772 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1773 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001774 }
1775 }
1776
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001777 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001778 return release;
1779}
1780
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001781/* release function. This one should be called to free all resources allocated
1782 * to the mux.
1783 */
1784static void qc_release(struct qcc *qcc)
1785{
1786 struct connection *conn = qcc->conn;
1787 struct eb64_node *node;
1788
1789 TRACE_ENTER(QMUX_EV_QCC_END);
1790
1791 if (qcc->app_ops && qcc->app_ops->release) {
1792 /* Application protocol with dedicated connection closing
1793 * procedure.
1794 */
1795 qcc->app_ops->release(qcc->ctx);
Amaury Denoyellea154dc02022-06-13 17:09:01 +02001796
1797 /* useful if application protocol should emit some closing
1798 * frames. For example HTTP/3 GOAWAY frame.
1799 */
1800 qc_send(qcc);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001801 }
1802 else {
1803 qcc_emit_cc_app(qcc, QC_ERR_NO_ERROR, 0);
1804 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001805 TRACE_PROTO("application layer released", QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001806
1807 if (qcc->task) {
1808 task_destroy(qcc->task);
1809 qcc->task = NULL;
1810 }
1811
1812 if (qcc->wait_event.tasklet)
1813 tasklet_free(qcc->wait_event.tasklet);
1814 if (conn && qcc->wait_event.events) {
1815 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
1816 qcc->wait_event.events,
1817 &qcc->wait_event);
1818 }
1819
1820 /* liberate remaining qcs instances */
1821 node = eb64_first(&qcc->streams_by_id);
1822 while (node) {
1823 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
1824 node = eb64_next(node);
1825 qcs_free(qcs);
1826 }
1827
1828 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1829 struct quic_frame *frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list);
1830 LIST_DELETE(&frm->list);
1831 pool_free(pool_head_quic_frame, frm);
1832 }
1833
1834 pool_free(pool_head_qcc, qcc);
1835
1836 if (conn) {
1837 LIST_DEL_INIT(&conn->stopping_list);
1838
1839 conn->handle.qc->conn = NULL;
1840 conn->mux = NULL;
1841 conn->ctx = NULL;
1842
1843 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
1844
1845 conn_stop_tracking(conn);
1846 conn_full_close(conn);
1847 if (conn->destroy_cb)
1848 conn->destroy_cb(conn);
1849 conn_free(conn);
1850 }
1851
1852 TRACE_LEAVE(QMUX_EV_QCC_END);
1853}
1854
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001855static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1856{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001857 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001858
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001859 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001860
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001861 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001862
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001863 if (qc_purge_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001864 if (qcc_is_dead(qcc)) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001865 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001866 qc_release(qcc);
Amaury Denoyelled3973852022-07-25 14:56:54 +02001867 goto end;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001868 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001869 }
1870
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001871 qc_recv(qcc);
1872
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02001873 /* TODO check if qcc proxy is disabled. If yes, use graceful shutdown
1874 * to close the connection.
1875 */
1876
1877 qcc_refresh_timeout(qcc);
1878
Amaury Denoyelled3973852022-07-25 14:56:54 +02001879 end:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001880 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1881
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001882 return NULL;
1883}
1884
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001885static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1886{
1887 struct qcc *qcc = ctx;
1888 int expired = tick_is_expired(t->expire, now_ms);
1889
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001890 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001891
1892 if (qcc) {
1893 if (!expired) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001894 TRACE_DEVEL("not expired", QMUX_EV_QCC_WAKE, qcc->conn);
1895 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001896 }
1897
1898 if (!qcc_may_expire(qcc)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001899 TRACE_DEVEL("cannot expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001900 t->expire = TICK_ETERNITY;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001901 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001902 }
1903 }
1904
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001905 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001906
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001907 if (!qcc) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001908 TRACE_DEVEL("no more qcc", QMUX_EV_QCC_WAKE);
1909 goto out;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001910 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001911
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001912 qcc->task = NULL;
1913
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02001914 /* TODO depending on the timeout condition, different shutdown mode
1915 * should be used. For http keep-alive or disabled proxy, a graceful
1916 * shutdown should occurs. For all other cases, an immediate close
1917 * seems legitimate.
1918 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001919 if (qcc_is_dead(qcc)) {
1920 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001921 qc_release(qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001922 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001923
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001924 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001925 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001926 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001927
1928 requeue:
1929 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1930 return t;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001931}
1932
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001933static int qc_init(struct connection *conn, struct proxy *prx,
1934 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001935{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001936 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001937 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001938
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001939 TRACE_ENTER(QMUX_EV_QCC_NEW);
1940
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001941 qcc = pool_alloc(pool_head_qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001942 if (!qcc) {
1943 TRACE_ERROR("alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001944 goto fail_no_qcc;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001945 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001946
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001947 qcc->conn = conn;
1948 conn->ctx = qcc;
Amaury Denoyellec603de42022-07-25 11:21:46 +02001949 qcc->nb_hreq = qcc->nb_sc = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001950 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001951
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001952 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001953
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001954 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001955
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001956 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001957 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001958
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001959 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001960 qcc->tx.sent_offsets = qcc->tx.offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001961
1962 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001963 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001964 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001965 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001966 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001967
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001968 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001969 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001970 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001971 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001972
1973 /* Server initiated streams must respect the server flow control. */
1974 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001975 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001976 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001977 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1978
1979 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001980 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001981 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001982 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001983
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001984 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001985 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02001986 qcc->lfctl.ms_uni = lparams->initial_max_streams_uni;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02001987 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
1988 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001989 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001990
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001991 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001992 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001993
Willy Tarreau784b8682022-04-11 14:18:10 +02001994 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001995 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001996 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1997 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1998
Amaury Denoyellea509ffb2022-07-04 15:50:33 +02001999 if (conn_is_back(conn)) {
2000 qcc->next_bidi_l = 0x00;
2001 qcc->largest_bidi_r = 0x01;
2002 qcc->next_uni_l = 0x02;
2003 qcc->largest_uni_r = 0x03;
2004 }
2005 else {
2006 qcc->largest_bidi_r = 0x00;
2007 qcc->next_bidi_l = 0x01;
2008 qcc->largest_uni_r = 0x02;
2009 qcc->next_uni_l = 0x03;
2010 }
2011
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002012 qcc->wait_event.tasklet = tasklet_new();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002013 if (!qcc->wait_event.tasklet) {
2014 TRACE_ERROR("taslket alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002015 goto fail_no_tasklet;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002016 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002017
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02002018 LIST_INIT(&qcc->send_retry_list);
2019
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002020 qcc->subs = NULL;
2021 qcc->wait_event.tasklet->process = qc_io_cb;
2022 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01002023 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002024
Amaury Denoyelle07bf8f42022-07-22 16:16:03 +02002025 qcc->proxy = prx;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002026 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002027 qcc->task = NULL;
Amaury Denoyelleb6309452022-07-25 14:51:56 +02002028 qcc->timeout = conn_is_back(qcc->conn) ? prx->timeout.server :
2029 prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002030 if (tick_isset(qcc->timeout)) {
2031 qcc->task = task_new_here();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002032 if (!qcc->task) {
2033 TRACE_ERROR("timeout task alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002034 goto fail_no_timeout_task;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002035 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002036 qcc->task->process = qc_timeout_task;
2037 qcc->task->context = qcc;
2038 qcc->task->expire = tick_add(now_ms, qcc->timeout);
2039 }
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +02002040 qcc_reset_idle_start(qcc);
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02002041 LIST_INIT(&qcc->opening_list);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002042
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002043 if (!conn_is_back(conn)) {
2044 if (!LIST_INLIST(&conn->stopping_list)) {
2045 LIST_APPEND(&mux_stopping_data[tid].list,
2046 &conn->stopping_list);
2047 }
2048 }
2049
Willy Tarreau784b8682022-04-11 14:18:10 +02002050 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002051 /* init read cycle */
2052 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002053
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002054 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002055 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002056
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002057 fail_no_timeout_task:
2058 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002059 fail_no_tasklet:
2060 pool_free(pool_head_qcc, qcc);
2061 fail_no_qcc:
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002062 TRACE_LEAVE(QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002063 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002064}
2065
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002066static void qc_destroy(void *ctx)
2067{
2068 struct qcc *qcc = ctx;
2069
2070 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
2071 qc_release(qcc);
2072 TRACE_LEAVE(QMUX_EV_QCC_END);
2073}
2074
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002075static void qc_detach(struct sedesc *sd)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002076{
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002077 struct qcs *qcs = sd->se;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002078 struct qcc *qcc = qcs->qcc;
2079
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002080 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002081
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002082 /* TODO this BUG_ON_HOT() is not correct as the stconn layer may detach
2083 * from the stream even if it is not closed remotely at the QUIC layer.
2084 * This happens for example when a stream must be closed due to a
2085 * rejected request. To better handle these cases, it will be required
2086 * to implement shutr/shutw MUX operations. Once this is done, this
2087 * BUG_ON_HOT() statement can be adjusted.
2088 */
2089 //BUG_ON_HOT(!qcs_is_close_remote(qcs));
Amaury Denoyellec603de42022-07-25 11:21:46 +02002090
2091 qcc_rm_sc(qcc);
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002092
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02002093 if (!qcs_is_close_local(qcs) && !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002094 TRACE_STATE("remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002095 qcs->flags |= QC_SF_DETACH;
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002096 qcc_refresh_timeout(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002097
2098 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002099 return;
2100 }
2101
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002102 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01002103
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002104 if (qcc_is_dead(qcc)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002105 TRACE_STATE("killing dead connection", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002106 goto release;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002107 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002108 else if (qcc->task) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002109 TRACE_DEVEL("refreshing connection's timeout", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002110 qcc_refresh_timeout(qcc);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002111 }
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002112 else {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002113 TRACE_DEVEL("completed", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002114 }
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002115
2116 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002117 return;
2118
2119 release:
2120 qc_release(qcc);
2121 TRACE_LEAVE(QMUX_EV_STRM_END);
2122 return;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002123}
2124
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002125/* Called from the upper layer, to receive data */
Willy Tarreau3215e732022-05-27 10:09:11 +02002126static size_t qc_rcv_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002127 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002128{
Willy Tarreau3215e732022-05-27 10:09:11 +02002129 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002130 struct htx *qcs_htx = NULL;
2131 struct htx *cs_htx = NULL;
2132 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002133 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002134
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002135 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002136
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002137 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
2138 if (htx_is_empty(qcs_htx)) {
2139 /* Set buffer data to 0 as HTX is empty. */
2140 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
2141 goto end;
2142 }
2143
2144 ret = qcs_htx->data;
2145
2146 cs_htx = htx_from_buf(buf);
2147 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
Amaury Denoyelle72a78e82022-07-29 15:34:12 +02002148 /* EOM will be copied to cs_htx via b_xfer(). */
2149 if (qcs_htx->flags & HTX_FL_EOM)
2150 fin = 1;
2151
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002152 htx_to_buf(cs_htx, buf);
2153 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
2154 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
2155 goto end;
2156 }
2157
2158 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
2159 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
2160
2161 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002162 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
2163 cs_htx->flags |= HTX_FL_EOM;
2164 fin = 1;
2165 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002166
2167 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
2168 htx_to_buf(cs_htx, buf);
2169 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
2170 ret -= qcs_htx->data;
2171
2172 end:
2173 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002174 se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002175 }
2176 else {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002177 se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
2178 if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING))
2179 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002180
Amaury Denoyelle72a78e82022-07-29 15:34:12 +02002181 /* Set end-of-input if FIN received and all data extracted. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002182 if (fin)
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002183 se_fl_set(qcs->sd, SE_FL_EOI);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002184
2185 if (b_size(&qcs->rx.app_buf)) {
2186 b_free(&qcs->rx.app_buf);
2187 offer_buffers(NULL, 1);
2188 }
2189 }
2190
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002191 if (ret) {
2192 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002193 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002194 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002195
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002196 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
2197
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002198 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002199}
2200
Willy Tarreau3215e732022-05-27 10:09:11 +02002201static size_t qc_snd_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002202 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002203{
Willy Tarreau3215e732022-05-27 10:09:11 +02002204 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002205 size_t ret;
2206
2207 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002208
Amaury Denoyelle843a1192022-07-04 11:44:38 +02002209 if (qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_RESET)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002210 ret = count;
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002211 goto end;
2212 }
2213
Willy Tarreau3215e732022-05-27 10:09:11 +02002214 ret = qcs->qcc->app_ops->snd_buf(sc, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002215
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002216 end:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002217 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
2218
2219 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002220}
2221
2222/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2223 * event subscriber <es> is not allowed to change from a previous call as long
2224 * as at least one event is still subscribed. The <event_type> must only be a
2225 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
2226 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002227static int qc_subscribe(struct stconn *sc, int event_type,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002228 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002229{
Willy Tarreau3215e732022-05-27 10:09:11 +02002230 return qcs_subscribe(__sc_mux_strm(sc), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002231}
2232
2233/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
2234 * The <es> pointer is not allowed to differ from the one passed to the
2235 * subscribe() call. It always returns zero.
2236 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002237static int qc_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002238{
Willy Tarreau3215e732022-05-27 10:09:11 +02002239 struct qcs *qcs = __sc_mux_strm(sc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002240
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002241 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
2242 BUG_ON(qcs->subs && qcs->subs != es);
2243
2244 es->events &= ~event_type;
2245 if (!es->events)
2246 qcs->subs = NULL;
2247
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002248 return 0;
2249}
2250
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002251/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Willy Tarreau4596fe22022-05-17 19:07:51 +02002252 * report SE_FL_ERR_PENDING|SE_FL_ERROR on the attached stream connectors and
2253 * wake them.
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002254 */
2255static int qc_wake_some_streams(struct qcc *qcc)
2256{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002257 struct qcs *qcs;
2258 struct eb64_node *node;
2259
2260 for (node = eb64_first(&qcc->streams_by_id); node;
2261 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02002262 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002263
Amaury Denoyelle3abeb572022-07-04 11:42:27 +02002264 if (!qcs_sc(qcs))
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002265 continue;
2266
2267 if (qcc->conn->flags & CO_FL_ERROR) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002268 se_fl_set(qcs->sd, SE_FL_ERR_PENDING);
2269 if (se_fl_test(qcs->sd, SE_FL_EOS))
2270 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002271
Amaury Denoyelle4561f842022-07-06 14:54:34 +02002272 qcs_alert(qcs);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002273 }
2274 }
2275
2276 return 0;
2277}
2278
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002279static int qc_wake(struct connection *conn)
2280{
2281 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02002282 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002283
2284 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002285
2286 /* Check if a soft-stop is in progress.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002287 *
2288 * TODO this is revelant for frontend connections only.
Amaury Denoyelled0c62322022-05-23 08:52:58 +02002289 *
2290 * TODO Client should be notified with a H3 GOAWAY and then a
2291 * CONNECTION_CLOSE. However, quic-conn uses the listener socket for
2292 * sending which at this stage is already closed.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002293 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002294 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Amaury Denoyelled0c62322022-05-23 08:52:58 +02002295 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002296
Willy Tarreau784b8682022-04-11 14:18:10 +02002297 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002298 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
2299
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002300 qc_send(qcc);
2301
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002302 qc_wake_some_streams(qcc);
2303
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002304 if (qcc_is_dead(qcc))
2305 goto release;
2306
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002307 qcc_refresh_timeout(qcc);
2308
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002309 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002310 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002311
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002312 release:
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002313 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002314 qc_release(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002315 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002316 return 1;
2317}
2318
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002319
2320static char *qcs_st_to_str(enum qcs_state st)
2321{
2322 switch (st) {
2323 case QC_SS_IDLE: return "IDL";
2324 case QC_SS_OPEN: return "OPN";
2325 case QC_SS_HLOC: return "HCL";
2326 case QC_SS_HREM: return "HCR";
2327 case QC_SS_CLO: return "CLO";
2328 default: return "???";
2329 }
2330}
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01002331
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002332static void qmux_trace_frm(const struct quic_frame *frm)
2333{
2334 switch (frm->type) {
2335 case QUIC_FT_MAX_STREAMS_BIDI:
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002336 chunk_appendf(&trace_buf, " max_streams=%llu",
2337 (ull)frm->max_streams_bidi.max_streams);
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002338 break;
2339
2340 case QUIC_FT_MAX_STREAMS_UNI:
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002341 chunk_appendf(&trace_buf, " max_streams=%llu",
2342 (ull)frm->max_streams_uni.max_streams);
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002343 break;
2344
2345 default:
2346 break;
2347 }
2348}
2349
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01002350/* quic-mux trace handler */
2351static void qmux_trace(enum trace_level level, uint64_t mask,
2352 const struct trace_source *src,
2353 const struct ist where, const struct ist func,
2354 const void *a1, const void *a2, const void *a3, const void *a4)
2355{
2356 const struct connection *conn = a1;
2357 const struct qcc *qcc = conn ? conn->ctx : NULL;
2358 const struct qcs *qcs = a2;
2359
2360 if (!qcc)
2361 return;
2362
2363 if (src->verbosity > QMUX_VERB_CLEAN) {
2364 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
2365
2366 if (qcs)
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002367 chunk_appendf(&trace_buf, " qcs=%p .id=%llu .st=%s",
2368 qcs, (ull)qcs->id,
2369 qcs_st_to_str(qcs->st));
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002370
2371 if (mask & QMUX_EV_QCC_NQCS) {
2372 const uint64_t *id = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002373 chunk_appendf(&trace_buf, " id=%llu", (ull)*id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002374 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002375
2376 if (mask & QMUX_EV_SEND_FRM)
2377 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01002378
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02002379 if (mask & QMUX_EV_QCS_XFER_DATA) {
2380 const struct qcs_xfer_data_trace_arg *arg = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002381 chunk_appendf(&trace_buf, " prep=%llu xfer=%d",
2382 (ull)arg->prep, arg->xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02002383 }
2384
2385 if (mask & QMUX_EV_QCS_BUILD_STRM) {
2386 const struct qcs_build_stream_trace_arg *arg = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002387 chunk_appendf(&trace_buf, " len=%llu fin=%d offset=%llu",
2388 (ull)arg->len, arg->fin, (ull)arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01002389 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01002390 }
2391}
2392
2393
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002394static const struct mux_ops qc_ops = {
2395 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002396 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002397 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002398 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002399 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002400 .subscribe = qc_subscribe,
2401 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002402 .wake = qc_wake,
Willy Tarreaub5821e12022-04-26 11:54:08 +02002403 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02002404 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002405};
2406
2407static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002408 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002409
2410INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);