blob: bf91750c5eefe6c6b8568abfe1e75035313383a1 [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 Denoyelle3baab742022-08-11 18:35:55 +0200232 struct qcc *qcc = qcs->qcc;
233
234 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn, qcs);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200235
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200236 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200237 b_free(&qcs->tx.buf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200238
Amaury Denoyelle3baab742022-08-11 18:35:55 +0200239 BUG_ON(!qcc->strms[qcs_id_type(qcs->id)].nb_streams);
240 --qcc->strms[qcs_id_type(qcs->id)].nb_streams;
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200241
Amaury Denoyelle3baab742022-08-11 18:35:55 +0200242 if (qcs->ctx && qcc->app_ops->detach)
243 qcc->app_ops->detach(qcs);
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200244
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200245 qc_stream_desc_release(qcs->stream);
246
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +0200247 BUG_ON(qcs->sd && !se_fl_test(qcs->sd, SE_FL_ORPHAN));
248 sedesc_free(qcs->sd);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200249
250 eb64_delete(&qcs->by_id);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100251 pool_free(pool_head_qcs, qcs);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200252
Amaury Denoyelle3baab742022-08-11 18:35:55 +0200253 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100254}
255
Amaury Denoyelle3abeb572022-07-04 11:42:27 +0200256static forceinline struct stconn *qcs_sc(const struct qcs *qcs)
257{
258 return qcs->sd ? qcs->sd->sc : NULL;
259}
260
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200261/* Reset the <qcc> inactivity timeout for http-keep-alive timeout. */
262static forceinline void qcc_reset_idle_start(struct qcc *qcc)
263{
264 qcc->idle_start = now_ms;
265}
266
Amaury Denoyellec603de42022-07-25 11:21:46 +0200267/* Decrement <qcc> sc. */
268static forceinline void qcc_rm_sc(struct qcc *qcc)
269{
270 BUG_ON_HOT(!qcc->nb_sc);
271 --qcc->nb_sc;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200272
273 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
274 * refreshed after this on stream detach.
275 */
276 if (!qcc->nb_sc && !qcc->nb_hreq)
277 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200278}
279
280/* Decrement <qcc> hreq. */
281static forceinline void qcc_rm_hreq(struct qcc *qcc)
282{
283 BUG_ON_HOT(!qcc->nb_hreq);
284 --qcc->nb_hreq;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200285
286 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
287 * refreshed after this on I/O handler.
288 */
289 if (!qcc->nb_sc && !qcc->nb_hreq)
290 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200291}
292
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200293static inline int qcc_is_dead(const struct qcc *qcc)
294{
295 /* Mux connection is considered dead if :
296 * - all stream-desc are detached AND
297 * = connection is on error OR
298 * = mux timeout has already fired or is unset
299 */
300 if (!qcc->nb_sc && ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task))
301 return 1;
302
303 return 0;
304}
305
306/* Return true if the mux timeout should be armed. */
307static inline int qcc_may_expire(struct qcc *qcc)
308{
309 return !qcc->nb_sc;
310}
311
312/* Refresh the timeout on <qcc> if needed depending on its state. */
313static void qcc_refresh_timeout(struct qcc *qcc)
314{
315 const struct proxy *px = qcc->proxy;
316
317 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
318
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200319 if (!qcc->task) {
320 TRACE_DEVEL("already expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200321 goto leave;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200322 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200323
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200324 /* Check if upper layer is responsible of timeout management. */
325 if (!qcc_may_expire(qcc)) {
326 TRACE_DEVEL("not eligible for timeout", QMUX_EV_QCC_WAKE, qcc->conn);
327 qcc->task->expire = TICK_ETERNITY;
328 task_queue(qcc->task);
329 goto leave;
330 }
331
332 /* TODO if connection is idle on frontend and proxy is disabled, remove
333 * it with global close_spread delay applied.
334 */
335
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200336 /* TODO implement client/server-fin timeout for graceful shutdown */
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200337
338 /* Frontend timeout management
339 * - detached streams with data left to send -> default timeout
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200340 * - stream waiting on incomplete request or no stream yet activated -> timeout http-request
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200341 * - idle after stream processing -> timeout http-keep-alive
342 */
343 if (!conn_is_back(qcc->conn)) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200344 if (qcc->nb_hreq) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200345 TRACE_DEVEL("one or more requests still in progress", QMUX_EV_QCC_WAKE, qcc->conn);
346 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200347 task_queue(qcc->task);
348 goto leave;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200349 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200350
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200351 if (!LIST_ISEMPTY(&qcc->opening_list) || unlikely(!qcc->largest_bidi_r)) {
352 int timeout = px->timeout.httpreq;
353 struct qcs *qcs = NULL;
354 int base_time;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200355
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200356 /* Use start time of first stream waiting on HTTP or
357 * qcc idle if no stream not yet used.
358 */
359 if (likely(!LIST_ISEMPTY(&qcc->opening_list)))
360 qcs = LIST_ELEM(qcc->opening_list.n, struct qcs *, el_opening);
361 base_time = qcs ? qcs->start : qcc->idle_start;
362
363 TRACE_DEVEL("waiting on http request", QMUX_EV_QCC_WAKE, qcc->conn, qcs);
364 qcc->task->expire = tick_add_ifset(base_time, timeout);
365 }
366 else {
367 /* Use http-request timeout if keep-alive timeout not set */
368 int timeout = tick_isset(px->timeout.httpka) ?
369 px->timeout.httpka : px->timeout.httpreq;
370
371 TRACE_DEVEL("at least one request achieved but none currently in progress", QMUX_EV_QCC_WAKE, qcc->conn);
372 qcc->task->expire = tick_add_ifset(qcc->idle_start, timeout);
373 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200374 }
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200375
376 /* fallback to default timeout if frontend specific undefined or for
377 * backend connections.
378 */
379 if (!tick_isset(qcc->task->expire)) {
380 TRACE_DEVEL("fallback to default timeout", QMUX_EV_QCC_WAKE, qcc->conn);
381 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200382 }
383
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200384 task_queue(qcc->task);
385
386 leave:
387 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
388}
389
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200390/* Mark a stream as open if it was idle. This can be used on every
391 * successful emission/reception operation to update the stream state.
392 */
393static void qcs_idle_open(struct qcs *qcs)
394{
395 /* This operation must not be used if the stream is already closed. */
396 BUG_ON_HOT(qcs->st == QC_SS_CLO);
397
398 if (qcs->st == QC_SS_IDLE) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200399 TRACE_STATE("opening stream", QMUX_EV_QCS_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200400 qcs->st = QC_SS_OPEN;
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200401 }
402}
403
404/* Close the local channel of <qcs> instance. */
405static void qcs_close_local(struct qcs *qcs)
406{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200407 TRACE_STATE("closing stream locally", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
408
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200409 /* The stream must have already been opened. */
410 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
411
412 /* This operation cannot be used multiple times. */
413 BUG_ON_HOT(qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO);
414
415 if (quic_stream_is_bidi(qcs->id)) {
416 qcs->st = (qcs->st == QC_SS_HREM) ? QC_SS_CLO : QC_SS_HLOC;
Amaury Denoyellec603de42022-07-25 11:21:46 +0200417 qcc_rm_hreq(qcs->qcc);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200418 }
419 else {
420 /* Only local uni streams are valid for this operation. */
421 BUG_ON_HOT(quic_stream_is_remote(qcs->qcc, qcs->id));
422 qcs->st = QC_SS_CLO;
423 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200424}
425
426/* Close the remote channel of <qcs> instance. */
427static void qcs_close_remote(struct qcs *qcs)
428{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200429 TRACE_STATE("closing stream remotely", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
430
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200431 /* The stream must have already been opened. */
432 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
433
434 /* This operation cannot be used multiple times. */
435 BUG_ON_HOT(qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO);
436
437 if (quic_stream_is_bidi(qcs->id)) {
438 qcs->st = (qcs->st == QC_SS_HLOC) ? QC_SS_CLO : QC_SS_HREM;
439 }
440 else {
441 /* Only remote uni streams are valid for this operation. */
442 BUG_ON_HOT(quic_stream_is_local(qcs->qcc, qcs->id));
443 qcs->st = QC_SS_CLO;
444 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200445}
446
447static int qcs_is_close_local(struct qcs *qcs)
448{
449 return qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO;
450}
451
452static __maybe_unused int qcs_is_close_remote(struct qcs *qcs)
453{
454 return qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO;
455}
456
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100457struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100458{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100459 struct buffer *buf = b_alloc(bptr);
460 BUG_ON(!buf);
461 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100462}
463
Amaury Denoyellea441ec92022-07-04 15:48:57 +0200464static struct ncbuf *qc_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200465{
466 struct buffer buf = BUF_NULL;
467
468 if (ncb_is_null(ncbuf)) {
469 b_alloc(&buf);
470 BUG_ON(b_is_null(&buf));
471
472 *ncbuf = ncb_make(buf.area, buf.size, 0);
473 ncb_init(ncbuf, 0);
474 }
475
476 return ncbuf;
477}
478
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500479/* Notify an eventual subscriber on <qcs> or else wakeup up the stconn layer if
Amaury Denoyelle4561f842022-07-06 14:54:34 +0200480 * initialized.
481 */
482static void qcs_alert(struct qcs *qcs)
483{
484 if (qcs->subs) {
485 qcs_notify_recv(qcs);
486 qcs_notify_send(qcs);
487 }
488 else if (qcs_sc(qcs) && qcs->sd->sc->app_ops->wake) {
489 qcs->sd->sc->app_ops->wake(qcs->sd->sc);
490 }
491}
492
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100493int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
494{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100495 struct qcc *qcc = qcs->qcc;
496
497 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100498
499 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
500 BUG_ON(qcs->subs && qcs->subs != es);
501
502 es->events |= event_type;
503 qcs->subs = es;
504
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100505 if (event_type & SUB_RETRY_RECV)
506 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
507
508 if (event_type & SUB_RETRY_SEND)
509 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
510
511 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
512
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100513 return 0;
514}
515
516void qcs_notify_recv(struct qcs *qcs)
517{
518 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
519 tasklet_wakeup(qcs->subs->tasklet);
520 qcs->subs->events &= ~SUB_RETRY_RECV;
521 if (!qcs->subs->events)
522 qcs->subs = NULL;
523 }
524}
525
526void qcs_notify_send(struct qcs *qcs)
527{
528 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
529 tasklet_wakeup(qcs->subs->tasklet);
530 qcs->subs->events &= ~SUB_RETRY_SEND;
531 if (!qcs->subs->events)
532 qcs->subs = NULL;
533 }
534}
535
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200536/* Open a locally initiated stream for the connection <qcc>. Set <bidi> for a
537 * bidirectional stream, else an unidirectional stream is opened. The next
538 * available ID on the connection will be used according to the stream type.
539 *
540 * Returns the allocated stream instance or NULL on error.
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100541 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200542struct qcs *qcc_init_stream_local(struct qcc *qcc, int bidi)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100543{
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200544 struct qcs *qcs;
545 enum qcs_type type;
546 uint64_t *next;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100547
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200548 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
549
550 if (bidi) {
551 next = &qcc->next_bidi_l;
552 type = conn_is_back(qcc->conn) ? QCS_CLT_BIDI : QCS_SRV_BIDI;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100553 }
554 else {
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200555 next = &qcc->next_uni_l;
556 type = conn_is_back(qcc->conn) ? QCS_CLT_UNI : QCS_SRV_UNI;
557 }
558
559 /* TODO ensure that we won't overflow remote peer flow control limit on
560 * streams. Else, we should emit a STREAMS_BLOCKED frame.
561 */
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100562
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200563 qcs = qcs_new(qcc, *next, type);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200564 if (!qcs) {
565 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200566 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200567 }
Amaury Denoyellec055e302022-02-07 16:09:06 +0100568
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200569 TRACE_PROTO("opening local stream", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200570 *next += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100571
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200572 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200573 return qcs;
574}
575
576/* Open a remote initiated stream for the connection <qcc> with ID <id>. The
577 * caller is responsible to ensure that a stream with the same ID was not
578 * already opened. This function will also create all intermediaries streams
579 * with ID smaller than <id> not already opened before.
580 *
581 * Returns the allocated stream instance or NULL on error.
582 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200583static struct qcs *qcc_init_stream_remote(struct qcc *qcc, uint64_t id)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200584{
585 struct qcs *qcs = NULL;
586 enum qcs_type type;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200587 uint64_t *largest, max_id;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100588
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200589 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200590
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200591 BUG_ON_HOT(quic_stream_is_local(qcc, id));
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100592
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200593 if (quic_stream_is_bidi(id)) {
594 largest = &qcc->largest_bidi_r;
595 type = conn_is_back(qcc->conn) ? QCS_SRV_BIDI : QCS_CLT_BIDI;
596 }
597 else {
598 largest = &qcc->largest_uni_r;
599 type = conn_is_back(qcc->conn) ? QCS_SRV_UNI : QCS_CLT_UNI;
600 }
601
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200602 /* RFC 9000 4.6. Controlling Concurrency
603 *
604 * An endpoint that receives a frame with a stream ID exceeding the
605 * limit it has sent MUST treat this as a connection error of type
606 * STREAM_LIMIT_ERROR
607 */
608 max_id = quic_stream_is_bidi(id) ? qcc->lfctl.ms_bidi * 4 :
609 qcc->lfctl.ms_uni * 4;
610 if (id >= max_id) {
611 TRACE_ERROR("flow control error", QMUX_EV_QCS_NEW|QMUX_EV_PROTO_ERR, qcc->conn);
612 qcc_emit_cc(qcc, QC_ERR_STREAM_LIMIT_ERROR);
613 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200614 }
615
616 /* Only stream ID not already opened can be used. */
617 BUG_ON(id < *largest);
618
619 while (id >= *largest) {
Amaury Denoyellefd79ddb2022-08-16 11:13:45 +0200620 const char *str = *largest < id ? "initializing intermediary remote stream" :
621 "initializing remote stream";
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200622
623 qcs = qcs_new(qcc, *largest, type);
624 if (!qcs) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200625 /* TODO emit RESET_STREAM */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200626 TRACE_ERROR("stream fallocation failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200627 goto err;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100628 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200629
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200630 TRACE_PROTO(str, QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200631 *largest += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100632 }
633
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200634 out:
635 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle50742292022-03-29 14:57:19 +0200636 return qcs;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200637
638 err:
639 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
640 return NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200641}
642
643/* Use this function for a stream <id> which is not in <qcc> stream tree. It
644 * returns true if the associated stream is closed.
645 */
646static int qcc_stream_id_is_closed(struct qcc *qcc, uint64_t id)
647{
648 uint64_t *largest;
649
650 /* This function must only be used for stream not present in the stream tree. */
651 BUG_ON_HOT(eb64_lookup(&qcc->streams_by_id, id));
652
653 if (quic_stream_is_local(qcc, id)) {
654 largest = quic_stream_is_uni(id) ? &qcc->next_uni_l :
655 &qcc->next_bidi_l;
656 }
657 else {
658 largest = quic_stream_is_uni(id) ? &qcc->largest_uni_r :
659 &qcc->largest_bidi_r;
660 }
661
662 return id < *largest;
663}
664
665/* Retrieve the stream instance from <id> ID. This can be used when receiving
666 * STREAM, STREAM_DATA_BLOCKED, RESET_STREAM, MAX_STREAM_DATA or STOP_SENDING
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200667 * frames. Set to false <receive_only> or <send_only> if these particular types
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200668 * of streams are not allowed. If the stream instance is found, it is stored in
669 * <out>.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200670 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200671 * Returns 0 on success else non-zero. On error, a RESET_STREAM or a
672 * CONNECTION_CLOSE is automatically emitted. Beware that <out> may be NULL
673 * on success if the stream has already been closed.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200674 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200675int qcc_get_qcs(struct qcc *qcc, uint64_t id, int receive_only, int send_only,
676 struct qcs **out)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200677{
678 struct eb64_node *node;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200679
680 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200681 *out = NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200682
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200683 if (!receive_only && quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200684 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 +0200685 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200686 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200687 }
688
689 if (!send_only && quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200690 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 +0200691 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200692 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200693 }
694
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200695 /* Search the stream in the connection tree. */
696 node = eb64_lookup(&qcc->streams_by_id, id);
697 if (node) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200698 *out = eb64_entry(node, struct qcs, by_id);
699 TRACE_DEVEL("using stream from connection tree", QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200700 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200701 }
702
703 /* Check if stream is already closed. */
704 if (qcc_stream_id_is_closed(qcc, id)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200705 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200706 /* Consider this as a success even if <out> is left NULL. */
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200707 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200708 }
709
710 /* Create the stream. This is valid only for remote initiated one. A
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500711 * local stream must have already been explicitly created by the
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200712 * application protocol layer.
713 */
714 if (quic_stream_is_local(qcc, id)) {
715 /* RFC 9000 19.8. STREAM Frames
716 *
717 * An endpoint MUST terminate the connection with error
718 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
719 * initiated stream that has not yet been created, or for a send-only
720 * stream.
721 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200722 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 +0200723 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200724 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200725 }
726 else {
727 /* Remote stream not found - try to open it. */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200728 *out = qcc_init_stream_remote(qcc, id);
729 if (!*out) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200730 TRACE_ERROR("stream creation error", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200731 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200732 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200733 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100734
735 out:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200736 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200737 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200738
739 err:
740 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
741 return 1;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100742}
743
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200744/* Simple function to duplicate a buffer */
745static inline struct buffer qcs_b_dup(const struct ncbuf *b)
746{
747 return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0));
748}
749
Amaury Denoyelle36d4b5e2022-07-01 11:25:40 +0200750/* Remove <bytes> from <qcs> Rx buffer. Flow-control for received offsets may
751 * be allocated for the peer if needed.
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200752 */
753static void qcs_consume(struct qcs *qcs, uint64_t bytes)
754{
755 struct qcc *qcc = qcs->qcc;
756 struct quic_frame *frm;
757 struct ncbuf *buf = &qcs->rx.ncbuf;
758 enum ncb_ret ret;
759
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200760 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
761
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200762 ret = ncb_advance(buf, bytes);
763 if (ret) {
764 ABORT_NOW(); /* should not happens because removal only in data */
765 }
766
767 if (ncb_is_empty(buf))
768 qc_free_ncbuf(qcs, buf);
769
770 qcs->rx.offset += bytes;
771 if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200772 TRACE_DATA("increase stream credit via MAX_STREAM_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200773 frm = pool_zalloc(pool_head_quic_frame);
774 BUG_ON(!frm); /* TODO handle this properly */
775
776 qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init;
777
778 LIST_INIT(&frm->reflist);
779 frm->type = QUIC_FT_MAX_STREAM_DATA;
780 frm->max_stream_data.id = qcs->id;
781 frm->max_stream_data.max_stream_data = qcs->rx.msd;
782
783 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
784 tasklet_wakeup(qcc->wait_event.tasklet);
785 }
786
787 qcc->lfctl.offsets_consume += bytes;
788 if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200789 TRACE_DATA("increase conn credit via MAX_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200790 frm = pool_zalloc(pool_head_quic_frame);
791 BUG_ON(!frm); /* TODO handle this properly */
792
793 qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init;
794
795 LIST_INIT(&frm->reflist);
796 frm->type = QUIC_FT_MAX_DATA;
797 frm->max_data.max_data = qcc->lfctl.md;
798
799 LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list);
800 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
801 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200802
803 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200804}
805
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200806/* Decode the content of STREAM frames already received on the stream instance
807 * <qcs>.
808 *
809 * Returns 0 on success else non-zero.
810 */
811static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
812{
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200813 struct buffer b;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200814 ssize_t ret;
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200815 int fin = 0;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200816
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200817 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
818
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200819 b = qcs_b_dup(&qcs->rx.ncbuf);
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200820
821 /* Signal FIN to application if STREAM FIN received and there is no gap
822 * in the Rx buffer.
823 */
Amaury Denoyelle3f39b402022-07-01 16:11:03 +0200824 if (qcs->flags & QC_SF_SIZE_KNOWN && !ncb_is_fragmented(&qcs->rx.ncbuf))
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200825 fin = 1;
826
827 ret = qcc->app_ops->decode_qcs(qcs, &b, fin);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200828 if (ret < 0) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200829 TRACE_ERROR("decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200830 goto err;
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200831 }
832
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200833 if (ret) {
834 qcs_consume(qcs, ret);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200835 qcs_notify_recv(qcs);
836 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200837
838 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200839 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200840
841 err:
842 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
843 return 1;
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200844}
845
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200846/* Emit a CONNECTION_CLOSE_APP with error <err>. Reserved for application error
Amaury Denoyelled666d742022-07-13 15:15:58 +0200847 * code. To close the connection right away, set <immediate> : this is useful
848 * when dealing with a connection fatal error. Else a graceful shutdown will be
849 * conducted : the error-code is only registered. The lower layer is
850 * responsible to close the connection when deemed suitable. Note that in this
851 * case the error code might be overwritten if an immediate close is requested
852 * in the interval.
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200853 */
Amaury Denoyelled666d742022-07-13 15:15:58 +0200854void qcc_emit_cc_app(struct qcc *qcc, int err, int immediate)
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200855{
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200856 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
857
Amaury Denoyelled666d742022-07-13 15:15:58 +0200858 if (immediate) {
859 quic_set_connection_close(qcc->conn->handle.qc, quic_err_app(err));
860 qcc->flags |= QC_CF_CC_EMIT;
861 tasklet_wakeup(qcc->wait_event.tasklet);
862 }
863 else {
864 /* Only register the error code for graceful shutdown. */
865 qcc->conn->handle.qc->err = quic_err_app(err);
866 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200867
868 TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200869}
870
871/* Prepare for the emission of RESET_STREAM on <qcs> with error code <err>. */
872void qcc_reset_stream(struct qcs *qcs, int err)
873{
874 struct qcc *qcc = qcs->qcc;
875
876 if ((qcs->flags & QC_SF_TO_RESET) || qcs_is_close_local(qcs))
877 return;
878
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200879 TRACE_STATE("reset stream", QMUX_EV_QCS_END, qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200880 qcs->flags |= QC_SF_TO_RESET;
881 qcs->err = err;
882 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200883}
884
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200885/* Install the <app_ops> applicative layer of a QUIC connection on mux <qcc>.
886 * Returns 0 on success else non-zero.
887 */
888int qcc_install_app_ops(struct qcc *qcc, const struct qcc_app_ops *app_ops)
889{
890 TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn);
891
892 qcc->app_ops = app_ops;
893 if (qcc->app_ops->init && !qcc->app_ops->init(qcc)) {
894 TRACE_ERROR("app ops init error", QMUX_EV_QCC_NEW, qcc->conn);
895 goto err;
896 }
897
898 TRACE_PROTO("application layer initialized", QMUX_EV_QCC_NEW, qcc->conn);
899
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200900 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
901 return 0;
902
903 err:
904 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
905 return 1;
906}
907
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200908/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
909 * <data> with length <len> and represents the offset <offset>. <fin> is set if
910 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100911 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200912 * Returns 0 on success else non-zero. On error, the received frame should not
913 * be acknowledged.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100914 */
915int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200916 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100917{
918 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200919 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100920
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100921 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
922
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200923 if (qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200924 TRACE_DATA("connection closed", QMUX_EV_QCC_RECV, qcc->conn);
925 goto err;
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200926 }
927
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200928 /* RFC 9000 19.8. STREAM Frames
929 *
930 * An endpoint MUST terminate the connection with error
931 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
932 * initiated stream that has not yet been created, or for a send-only
933 * stream.
934 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200935 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200936 TRACE_DATA("qcs retrieval error", QMUX_EV_QCC_RECV, qcc->conn);
937 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200938 }
939
940 if (!qcs) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200941 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV, qcc->conn);
942 goto out;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200943 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100944
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200945 /* RFC 9000 4.5. Stream Final Size
946 *
947 * Once a final size for a stream is known, it cannot change. If a
948 * RESET_STREAM or STREAM frame is received indicating a change in the
949 * final size for the stream, an endpoint SHOULD respond with an error
950 * of type FINAL_SIZE_ERROR; see Section 11 for details on error
951 * handling.
952 */
953 if (qcs->flags & QC_SF_SIZE_KNOWN &&
954 (offset + len > qcs->rx.offset_max || (fin && offset + len < qcs->rx.offset_max))) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200955 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 +0200956 qcc_emit_cc(qcc, QC_ERR_FINAL_SIZE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200957 goto err;
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200958 }
959
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100960 if (offset + len <= qcs->rx.offset) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200961 TRACE_DATA("already received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
962 goto out;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100963 }
964
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200965 TRACE_PROTO("receiving STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200966 qcs_idle_open(qcs);
967
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200968 if (offset + len > qcs->rx.offset_max) {
969 uint64_t diff = offset + len - qcs->rx.offset_max;
970 qcs->rx.offset_max = offset + len;
971 qcc->lfctl.offsets_recv += diff;
972
973 if (offset + len > qcs->rx.msd ||
974 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
975 /* RFC 9000 4.1. Data Flow Control
976 *
977 * A receiver MUST close the connection with an error
978 * of type FLOW_CONTROL_ERROR if the sender violates
979 * the advertised connection or stream data limits
980 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200981 TRACE_ERROR("flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200982 qcc->conn, qcs);
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200983 qcc_emit_cc(qcc, QC_ERR_FLOW_CONTROL_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200984 goto err;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200985 }
986 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100987
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200988 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100989 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200990 ABORT_NOW();
991 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100992 }
993
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200994 TRACE_DATA("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200995 if (offset < qcs->rx.offset) {
Frédéric Lécaillea18c3332022-07-04 09:54:58 +0200996 size_t diff = qcs->rx.offset - offset;
997
998 len -= diff;
999 data += diff;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001000 offset = qcs->rx.offset;
1001 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001002
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001003 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
1004 if (ret != NCB_RET_OK) {
1005 if (ret == NCB_RET_DATA_REJ) {
Amaury Denoyellecc3d7162022-05-20 15:14:57 +02001006 /* RFC 9000 2.2. Sending and Receiving Data
1007 *
1008 * An endpoint could receive data for a stream at the
1009 * same stream offset multiple times. Data that has
1010 * already been received can be discarded. The data at
1011 * a given offset MUST NOT change if it is sent
1012 * multiple times; an endpoint MAY treat receipt of
1013 * different data at the same offset within a stream as
1014 * a connection error of type PROTOCOL_VIOLATION.
1015 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +02001016 TRACE_ERROR("overlapping data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001017 qcc->conn, qcs);
Amaury Denoyellecc3d7162022-05-20 15:14:57 +02001018 qcc_emit_cc(qcc, QC_ERR_PROTOCOL_VIOLATION);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001019 }
1020 else if (ret == NCB_RET_GAP_SIZE) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001021 TRACE_DATA("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
1022 qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02001023 }
1024 return 1;
1025 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001026
1027 if (fin)
Amaury Denoyelle3f39b402022-07-01 16:11:03 +02001028 qcs->flags |= QC_SF_SIZE_KNOWN;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001029
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001030 if (qcs->flags & QC_SF_SIZE_KNOWN && !ncb_is_fragmented(&qcs->rx.ncbuf))
1031 qcs_close_remote(qcs);
1032
Amaury Denoyelle418ba212022-08-02 15:57:16 +02001033 if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001034 qcc_decode_qcs(qcc, qcs);
Amaury Denoyelle418ba212022-08-02 15:57:16 +02001035 qcc_refresh_timeout(qcc);
1036 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +02001037
Amaury Denoyelle849b24f2022-05-24 17:22:07 +02001038 if (qcs->flags & QC_SF_READ_ABORTED) {
1039 /* TODO should send a STOP_SENDING */
1040 qcs_free(qcs);
1041 }
1042
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001043 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001044 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001045 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001046
1047 err:
1048 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1049 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001050}
1051
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001052/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
1053 * the frame.
1054 *
1055 * Returns 0 on success else non-zero.
1056 */
1057int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
1058{
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001059 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1060
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001061 TRACE_PROTO("receiving MAX_DATA", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001062 if (qcc->rfctl.md < max) {
1063 qcc->rfctl.md = max;
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001064 TRACE_DEVEL("increase remote max-data", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001065
1066 if (qcc->flags & QC_CF_BLK_MFCTL) {
1067 qcc->flags &= ~QC_CF_BLK_MFCTL;
1068 tasklet_wakeup(qcc->wait_event.tasklet);
1069 }
1070 }
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001071
1072 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001073 return 0;
1074}
1075
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001076/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
1077 * field of the frame and <id> is the identifier of the QUIC stream.
1078 *
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001079 * Returns 0 on success else non-zero. On error, the received frame should not
1080 * be acknowledged.
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001081 */
1082int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
1083{
1084 struct qcs *qcs;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001085
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001086 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1087
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001088 /* RFC 9000 19.10. MAX_STREAM_DATA Frames
1089 *
1090 * Receiving a MAX_STREAM_DATA frame for a locally
1091 * initiated stream that has not yet been created MUST be treated as a
1092 * connection error of type STREAM_STATE_ERROR. An endpoint that
1093 * receives a MAX_STREAM_DATA frame for a receive-only stream MUST
1094 * terminate the connection with error STREAM_STATE_ERROR.
1095 */
1096 if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) {
1097 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1098 return 1;
1099 }
1100
1101 if (qcs) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001102 TRACE_PROTO("receiving MAX_STREAM_DATA", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001103 if (max > qcs->tx.msd) {
1104 qcs->tx.msd = max;
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001105 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 +01001106
1107 if (qcs->flags & QC_SF_BLK_SFCTL) {
1108 qcs->flags &= ~QC_SF_BLK_SFCTL;
1109 tasklet_wakeup(qcc->wait_event.tasklet);
1110 }
1111 }
1112 }
1113
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001114 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1115 qcc_refresh_timeout(qcc);
1116
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001117 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001118 return 0;
1119}
1120
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001121/* Handle a new STOP_SENDING frame for stream ID <id>. The error code should be
1122 * specified in <err>.
1123 *
1124 * Returns 0 on success else non-zero. On error, the received frame should not
1125 * be acknowledged.
1126 */
1127int qcc_recv_stop_sending(struct qcc *qcc, uint64_t id, uint64_t err)
1128{
1129 struct qcs *qcs;
1130
1131 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1132
1133 /* RFC 9000 19.5. STOP_SENDING Frames
1134 *
1135 * Receiving a STOP_SENDING frame for a
1136 * locally initiated stream that has not yet been created MUST be
1137 * treated as a connection error of type STREAM_STATE_ERROR. An
1138 * endpoint that receives a STOP_SENDING frame for a receive-only stream
1139 * MUST terminate the connection with error STREAM_STATE_ERROR.
1140 */
1141 if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) {
1142 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1143 return 1;
1144 }
1145
1146 if (!qcs)
1147 goto out;
1148
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001149 TRACE_PROTO("receiving STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle96ca1b72022-08-09 17:36:38 +02001150 qcs_idle_open(qcs);
1151
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001152 /* RFC 9000 3.5. Solicited State Transitions
1153 *
1154 * An endpoint that receives a STOP_SENDING frame
1155 * MUST send a RESET_STREAM frame if the stream is in the "Ready" or
1156 * "Send" state. If the stream is in the "Data Sent" state, the
1157 * endpoint MAY defer sending the RESET_STREAM frame until the packets
1158 * containing outstanding data are acknowledged or declared lost. If
1159 * any outstanding data is declared lost, the endpoint SHOULD send a
1160 * RESET_STREAM frame instead of retransmitting the data.
1161 *
1162 * An endpoint SHOULD copy the error code from the STOP_SENDING frame to
1163 * the RESET_STREAM frame it sends, but it can use any application error
1164 * code.
1165 */
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001166 qcc_reset_stream(qcs, err);
1167
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001168 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1169 qcc_refresh_timeout(qcc);
1170
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001171 out:
1172 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1173 return 0;
1174}
1175
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001176/* Signal the closing of remote stream with id <id>. Flow-control for new
1177 * streams may be allocated for the peer if needed.
1178 */
1179static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +01001180{
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001181 struct quic_frame *frm;
1182
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001183 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn);
1184
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001185 if (quic_stream_is_bidi(id)) {
1186 ++qcc->lfctl.cl_bidi_r;
1187 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001188 TRACE_DATA("increase max stream limit with MAX_STREAMS_BIDI", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001189 frm = pool_zalloc(pool_head_quic_frame);
1190 BUG_ON(!frm); /* TODO handle this properly */
1191
1192 LIST_INIT(&frm->reflist);
1193 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
1194 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
1195 qcc->lfctl.cl_bidi_r;
1196 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
1197 tasklet_wakeup(qcc->wait_event.tasklet);
1198
1199 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
1200 qcc->lfctl.cl_bidi_r = 0;
1201 }
1202 }
1203 else {
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02001204 /* TODO in HTTP/3 unidirectional streams cannot be closed or a
1205 * H3_CLOSED_CRITICAL_STREAM will be triggered before
1206 * entering here. If a new application protocol is supported it
1207 * might be necessary to implement MAX_STREAMS_UNI emission.
1208 */
1209 ABORT_NOW();
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001210 }
1211
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001212 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
1213
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001214 return 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001215}
1216
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05001217/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001218static void qcs_destroy(struct qcs *qcs)
1219{
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001220 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001221 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001222
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001223 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001224
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001225 if (quic_stream_is_remote(qcs->qcc, id))
1226 qcc_release_remote_stream(qcs->qcc, id);
Amaury Denoyellec055e302022-02-07 16:09:06 +01001227
Amaury Denoyelledccbd732022-03-29 18:36:59 +02001228 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001229
1230 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001231}
1232
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001233/* Transfer as much as possible data on <qcs> from <in> to <out>. This is done
1234 * in respect with available flow-control at stream and connection level.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001235 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001236 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001237 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001238static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, struct buffer *in)
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001239{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001240 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001241 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001242 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001243
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001244 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001245
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001246 qc_get_buf(qcs, out);
1247
1248 /*
1249 * QCS out buffer diagram
1250 * head left to_xfer
1251 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001252 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001253 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001254 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001255 * ^ ack-off ^ sent-off ^ off
1256 *
1257 * STREAM frame
1258 * ^ ^
1259 * |xxxxxxxxxxxxxxxxx|
1260 */
1261
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001262 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001263 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001264 BUG_ON_HOT(qcc->tx.offsets < qcc->tx.sent_offsets);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001265
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001266 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001267 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001268
1269 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
1270 /* do not exceed flow control limit */
1271 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
1272 to_xfer = qcs->tx.msd - qcs->tx.offset;
1273
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001274 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001275 /* do not overcome flow control limit on connection */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001276 if (qcc->tx.offsets + to_xfer > qcc->rfctl.md)
1277 to_xfer = qcc->rfctl.md - qcc->tx.offsets;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001278
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001279 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001280 goto out;
1281
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001282 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001283
1284 out:
1285 {
1286 struct qcs_xfer_data_trace_arg arg = {
1287 .prep = b_data(out), .xfer = total,
1288 };
1289 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
1290 qcc->conn, qcs, &arg);
1291 }
1292
1293 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001294}
1295
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001296/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
1297 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
1298 * stream frame.
1299 *
1300 * Returns the length of the STREAM frame or a negative error code.
1301 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001302static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
1303 struct list *frm_list)
1304{
1305 struct qcc *qcc = qcs->qcc;
1306 struct quic_frame *frm;
1307 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001308 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001309
1310 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1311
Amaury Denoyellea4569202022-04-15 17:29:25 +02001312 /* if ack_offset < buf_offset, it points to an older buffer. */
1313 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
1314 BUG_ON(qcs->tx.sent_offset < base_off);
1315
1316 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001317 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001318 BUG_ON(total < 0);
1319
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001320 if (!total && !fin) {
1321 /* No need to send anything if total is NULL and no FIN to signal. */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001322 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1323 return 0;
1324 }
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001325 BUG_ON((!total && qcs->tx.sent_offset > qcs->tx.offset) ||
1326 (total && qcs->tx.sent_offset >= qcs->tx.offset));
Amaury Denoyellea4569202022-04-15 17:29:25 +02001327 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001328 BUG_ON(qcc->tx.sent_offsets + total > qcc->rfctl.md);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001329
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001330 TRACE_PROTO("sending STREAM frame", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001331 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001332 if (!frm) {
1333 TRACE_ERROR("frame alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001334 goto err;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001335 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001336
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001337 LIST_INIT(&frm->reflist);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001338 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001339 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001340 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001341 frm->stream.buf = out;
1342 frm->stream.data = (unsigned char *)b_peek(out, head);
1343
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +01001344 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001345 if (fin)
1346 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001347
1348 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001349 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001350 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001351 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001352
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001353 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
1354 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001355
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001356 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001357
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001358 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001359 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001360 struct qcs_build_stream_trace_arg arg = {
1361 .len = frm->stream.len, .fin = fin,
1362 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001363 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001364 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001365 qcc->conn, qcs, &arg);
1366 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001367
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001368 return total;
1369
1370 err:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001371 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001372 return -1;
1373}
1374
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001375/* Check after transferring data from qcs.tx.buf if FIN must be set on the next
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001376 * STREAM frame for <qcs>.
1377 *
1378 * Returns true if FIN must be set else false.
1379 */
1380static int qcs_stream_fin(struct qcs *qcs)
1381{
1382 return qcs->flags & QC_SF_FIN_STREAM && !b_data(&qcs->tx.buf);
1383}
1384
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001385/* This function must be called by the upper layer to inform about the sending
1386 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
1387 * <offset>.
1388 */
1389void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
1390{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001391 struct qcc *qcc = qcs->qcc;
1392 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001393
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001394 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1395
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001396 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001397 BUG_ON(offset + data > qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001398
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001399 /* check if the STREAM frame has already been notified. It can happen
1400 * for retransmission.
1401 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001402 if (offset + data < qcs->tx.sent_offset) {
1403 TRACE_DEVEL("offset already notified", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1404 goto out;
1405 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001406
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001407 qcs_idle_open(qcs);
1408
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001409 diff = offset + data - qcs->tx.sent_offset;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001410 if (diff) {
1411 /* increase offset sum on connection */
1412 qcc->tx.sent_offsets += diff;
1413 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
1414 if (qcc->tx.sent_offsets == qcc->rfctl.md)
1415 qcc->flags |= QC_CF_BLK_MFCTL;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001416
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001417 /* increase offset on stream */
1418 qcs->tx.sent_offset += diff;
1419 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
1420 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
1421 if (qcs->tx.sent_offset == qcs->tx.msd)
1422 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001423
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001424 if (qcs->tx.offset == qcs->tx.sent_offset &&
1425 b_full(&qcs->stream->buf->buf)) {
1426 qc_stream_buf_release(qcs->stream);
1427 /* prepare qcs for immediate send retry if data to send */
1428 if (b_data(&qcs->tx.buf))
1429 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
1430 }
1431 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001432
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001433 if (qcs->tx.offset == qcs->tx.sent_offset && qcs_stream_fin(qcs)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001434 /* Close stream locally. */
1435 qcs_close_local(qcs);
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001436 /* Reset flag to not emit multiple FIN STREAM frames. */
1437 qcs->flags &= ~QC_SF_FIN_STREAM;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001438 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001439
1440 out:
1441 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001442}
1443
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001444/* Wrapper for send on transport layer. Send a list of frames <frms> for the
1445 * connection <qcc>.
1446 *
1447 * Returns 0 if all data sent with success else non-zero.
1448 */
1449static int qc_send_frames(struct qcc *qcc, struct list *frms)
1450{
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001451 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
1452
1453 if (LIST_ISEMPTY(frms)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001454 TRACE_DEVEL("no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
1455 goto err;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001456 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +01001457
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001458 LIST_INIT(&qcc->send_retry_list);
1459
Amaury Denoyelle70467562022-08-17 16:33:13 +02001460 qc_send_mux(qcc->conn->handle.qc, frms);
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001461
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001462 /* If there is frames left at this stage, transport layer is blocked.
1463 * Subscribe on it to retry later.
1464 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001465 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001466 TRACE_DEVEL("remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001467 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1468 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001469 goto err;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001470 }
1471
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001472 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001473 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001474
1475 err:
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001476 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001477 return 1;
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001478}
1479
1480/* Emit a RESET_STREAM on <qcs>.
1481 *
1482 * Returns 0 if the frame has been successfully sent else non-zero.
1483 */
1484static int qcs_send_reset(struct qcs *qcs)
1485{
1486 struct list frms = LIST_HEAD_INIT(frms);
1487 struct quic_frame *frm;
1488
1489 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1490
1491 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001492 if (!frm) {
1493 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001494 return 1;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001495 }
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001496
1497 LIST_INIT(&frm->reflist);
1498 frm->type = QUIC_FT_RESET_STREAM;
1499 frm->reset_stream.id = qcs->id;
1500 frm->reset_stream.app_error_code = qcs->err;
1501 frm->reset_stream.final_size = qcs->tx.sent_offset;
1502
1503 LIST_APPEND(&frms, &frm->list);
1504 if (qc_send_frames(qcs->qcc, &frms)) {
1505 pool_free(pool_head_quic_frame, frm);
1506 TRACE_DEVEL("cannot send RESET_STREAM", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1507 return 1;
1508 }
1509
1510 if (qcs_sc(qcs)) {
1511 se_fl_set_error(qcs->sd);
1512 qcs_alert(qcs);
1513 }
1514
1515 qcs_close_local(qcs);
1516 qcs->flags &= ~QC_SF_TO_RESET;
1517
1518 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001519 return 0;
1520}
1521
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001522/* Used internally by qc_send function. Proceed to send for <qcs>. This will
1523 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001524 * is then generated and inserted in <frms> list.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001525 *
1526 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
1527 * be null if out buffer cannot be allocated.
1528 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001529static int _qc_send_qcs(struct qcs *qcs, struct list *frms)
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001530{
1531 struct qcc *qcc = qcs->qcc;
1532 struct buffer *buf = &qcs->tx.buf;
1533 struct buffer *out = qc_stream_buf_get(qcs->stream);
1534 int xfer = 0;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001535 char fin = 0;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001536
1537 /* Allocate <out> buffer if necessary. */
1538 if (!out) {
1539 if (qcc->flags & QC_CF_CONN_FULL)
1540 return 0;
1541
1542 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
1543 if (!out) {
1544 qcc->flags |= QC_CF_CONN_FULL;
1545 return 0;
1546 }
1547 }
1548
1549 /* Transfer data from <buf> to <out>. */
1550 if (b_data(buf)) {
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001551 xfer = qcs_xfer_data(qcs, out, buf);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001552 if (xfer > 0) {
1553 qcs_notify_send(qcs);
1554 qcs->flags &= ~QC_SF_BLK_MROOM;
1555 }
1556
1557 qcs->tx.offset += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001558 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001559 qcc->tx.offsets += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001560 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001561 }
1562
1563 /* out buffer cannot be emptied if qcs offsets differ. */
1564 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1565
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001566 /* FIN is set if all incoming data were transferred. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001567 fin = qcs_stream_fin(qcs);
1568
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001569 /* Build a new STREAM frame with <out> buffer. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001570 if (qcs->tx.sent_offset != qcs->tx.offset || fin) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001571 int ret;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001572 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +02001573 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001574 }
1575
1576 return xfer;
1577}
1578
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001579/* Proceed to sending. Loop through all available streams for the <qcc>
1580 * instance and try to send as much as possible.
1581 *
1582 * Returns the total of bytes sent to the transport layer.
1583 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001584static int qc_send(struct qcc *qcc)
1585{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001586 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001587 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001588 struct qcs *qcs, *qcs_tmp;
1589 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001590
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001591 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001592
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001593 if (qcc->conn->flags & CO_FL_SOCK_WR_SH || qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001594 qcc->conn->flags |= CO_FL_ERROR;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001595 TRACE_DEVEL("connection on error", QMUX_EV_QCC_SEND, qcc->conn);
1596 goto err;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001597 }
1598
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001599 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1600 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
1601 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
1602 goto out;
1603 }
1604 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02001605
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001606 if (qcc->flags & QC_CF_BLK_MFCTL)
1607 return 0;
1608
Frédéric Lécaille3dd79d32022-09-08 17:53:36 +02001609 if (!(qcc->flags & QC_CF_APP_FINAL) && !eb_is_empty(&qcc->streams_by_id) &&
1610 qcc->app_ops->finalize) {
1611 /* Finalize the application layer before sending any stream.
1612 * For h3 this consists in preparing the control stream data (SETTINGS h3).
1613 */
1614 qcc->app_ops->finalize(qcc->ctx);
1615 qcc->flags |= QC_CF_APP_FINAL;
1616 }
1617
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001618 /* loop through all streams, construct STREAM frames if data available.
1619 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001620 */
1621 node = eb64_first(&qcc->streams_by_id);
1622 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001623 int ret;
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001624 uint64_t id;
1625
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001626 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001627 id = qcs->id;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001628
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001629 if (quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001630 node = eb64_next(node);
1631 continue;
1632 }
1633
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001634 if (qcs->flags & QC_SF_TO_RESET) {
1635 qcs_send_reset(qcs);
1636 node = eb64_next(node);
1637 continue;
1638 }
1639
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001640 if (qcs_is_close_local(qcs)) {
1641 node = eb64_next(node);
1642 continue;
1643 }
1644
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001645 if (qcs->flags & QC_SF_BLK_SFCTL) {
1646 node = eb64_next(node);
1647 continue;
1648 }
1649
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001650 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001651 node = eb64_next(node);
1652 continue;
1653 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001654
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001655 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001656 total += ret;
1657 node = eb64_next(node);
1658 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001659
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001660 if (qc_send_frames(qcc, &frms)) {
1661 /* data rejected by transport layer, do not retry. */
1662 goto out;
1663 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001664
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001665 retry:
1666 tmp_total = 0;
1667 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
1668 int ret;
1669 BUG_ON(!b_data(&qcs->tx.buf));
1670 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001671
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001672 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001673 tmp_total += ret;
1674 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001675 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001676
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001677 total += tmp_total;
1678 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
1679 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +01001680
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001681 out:
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02001682 /* Deallocate frames that the transport layer has rejected. */
1683 if (!LIST_ISEMPTY(&frms)) {
1684 struct quic_frame *frm, *frm2;
1685 list_for_each_entry_safe(frm, frm2, &frms, list) {
1686 LIST_DELETE(&frm->list);
1687 pool_free(pool_head_quic_frame, frm);
1688 }
1689 }
1690
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001691 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001692 return total;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001693
1694 err:
1695 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
1696 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001697}
1698
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001699/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1700 * operation.
1701 *
1702 * Returns 0 on success else non-zero.
1703 */
1704static int qc_recv(struct qcc *qcc)
1705{
1706 struct eb64_node *node;
1707 struct qcs *qcs;
1708
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001709 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001710
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001711 if (qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001712 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001713 return 0;
1714 }
1715
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001716 node = eb64_first(&qcc->streams_by_id);
1717 while (node) {
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001718 uint64_t id;
1719
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001720 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001721 id = qcs->id;
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001722
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001723 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001724 node = eb64_next(node);
1725 continue;
1726 }
1727
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001728 if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001729 node = eb64_next(node);
1730 continue;
1731 }
1732
1733 qcc_decode_qcs(qcc, qcs);
1734 node = eb64_next(node);
Amaury Denoyelle849b24f2022-05-24 17:22:07 +02001735
1736 if (qcs->flags & QC_SF_READ_ABORTED) {
1737 /* TODO should send a STOP_SENDING */
1738 qcs_free(qcs);
1739 }
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001740 }
1741
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001742 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001743 return 0;
1744}
1745
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001746
1747/* Release all streams which have their transfer operation achieved.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001748 *
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001749 * Returns true if at least one stream is released.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001750 */
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001751static int qc_purge_streams(struct qcc *qcc)
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001752{
1753 struct eb64_node *node;
1754 int release = 0;
1755
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001756 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001757
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001758 node = eb64_first(&qcc->streams_by_id);
1759 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001760 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001761 node = eb64_next(node);
1762
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001763 /* Release not attached closed streams. */
1764 if (qcs->st == QC_SS_CLO && !qcs_sc(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001765 TRACE_STATE("purging closed stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001766 qcs_destroy(qcs);
1767 release = 1;
1768 continue;
1769 }
1770
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001771 /* Release detached streams with empty buffer. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001772 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02001773 if (qcs_is_close_local(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001774 TRACE_STATE("purging detached stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001775 qcs_destroy(qcs);
1776 release = 1;
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001777 continue;
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001778 }
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001779
1780 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1781 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001782 }
1783 }
1784
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001785 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001786 return release;
1787}
1788
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001789/* release function. This one should be called to free all resources allocated
1790 * to the mux.
1791 */
1792static void qc_release(struct qcc *qcc)
1793{
1794 struct connection *conn = qcc->conn;
1795 struct eb64_node *node;
1796
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001797 TRACE_ENTER(QMUX_EV_QCC_END, conn);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001798
1799 if (qcc->app_ops && qcc->app_ops->release) {
1800 /* Application protocol with dedicated connection closing
1801 * procedure.
1802 */
1803 qcc->app_ops->release(qcc->ctx);
Amaury Denoyellea154dc02022-06-13 17:09:01 +02001804
1805 /* useful if application protocol should emit some closing
1806 * frames. For example HTTP/3 GOAWAY frame.
1807 */
1808 qc_send(qcc);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001809 }
1810 else {
1811 qcc_emit_cc_app(qcc, QC_ERR_NO_ERROR, 0);
1812 }
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001813 TRACE_PROTO("application layer released", QMUX_EV_QCC_END, conn);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001814
1815 if (qcc->task) {
1816 task_destroy(qcc->task);
1817 qcc->task = NULL;
1818 }
1819
1820 if (qcc->wait_event.tasklet)
1821 tasklet_free(qcc->wait_event.tasklet);
1822 if (conn && qcc->wait_event.events) {
1823 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
1824 qcc->wait_event.events,
1825 &qcc->wait_event);
1826 }
1827
1828 /* liberate remaining qcs instances */
1829 node = eb64_first(&qcc->streams_by_id);
1830 while (node) {
1831 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
1832 node = eb64_next(node);
1833 qcs_free(qcs);
1834 }
1835
1836 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1837 struct quic_frame *frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list);
1838 LIST_DELETE(&frm->list);
1839 pool_free(pool_head_quic_frame, frm);
1840 }
1841
1842 pool_free(pool_head_qcc, qcc);
1843
1844 if (conn) {
1845 LIST_DEL_INIT(&conn->stopping_list);
1846
1847 conn->handle.qc->conn = NULL;
1848 conn->mux = NULL;
1849 conn->ctx = NULL;
1850
1851 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
1852
1853 conn_stop_tracking(conn);
1854 conn_full_close(conn);
1855 if (conn->destroy_cb)
1856 conn->destroy_cb(conn);
1857 conn_free(conn);
1858 }
1859
1860 TRACE_LEAVE(QMUX_EV_QCC_END);
1861}
1862
Willy Tarreau41e701e2022-09-08 15:12:59 +02001863struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001864{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001865 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001866
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001867 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001868
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001869 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001870
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001871 if (qc_purge_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001872 if (qcc_is_dead(qcc)) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001873 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001874 goto release;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001875 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001876 }
1877
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001878 qc_recv(qcc);
1879
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02001880 /* TODO check if qcc proxy is disabled. If yes, use graceful shutdown
1881 * to close the connection.
1882 */
1883
1884 qcc_refresh_timeout(qcc);
1885
Amaury Denoyelled3973852022-07-25 14:56:54 +02001886 end:
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001887 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
1888 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001889
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001890 release:
1891 qc_release(qcc);
1892 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001893 return NULL;
1894}
1895
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001896static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1897{
1898 struct qcc *qcc = ctx;
1899 int expired = tick_is_expired(t->expire, now_ms);
1900
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001901 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001902
1903 if (qcc) {
1904 if (!expired) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001905 TRACE_DEVEL("not expired", QMUX_EV_QCC_WAKE, qcc->conn);
1906 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001907 }
1908
1909 if (!qcc_may_expire(qcc)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001910 TRACE_DEVEL("cannot expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001911 t->expire = TICK_ETERNITY;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001912 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001913 }
1914 }
1915
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001916 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001917
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001918 if (!qcc) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001919 TRACE_DEVEL("no more qcc", QMUX_EV_QCC_WAKE);
1920 goto out;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001921 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001922
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001923 qcc->task = NULL;
1924
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02001925 /* TODO depending on the timeout condition, different shutdown mode
1926 * should be used. For http keep-alive or disabled proxy, a graceful
1927 * shutdown should occurs. For all other cases, an immediate close
1928 * seems legitimate.
1929 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001930 if (qcc_is_dead(qcc)) {
1931 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001932 qc_release(qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001933 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001934
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001935 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001936 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001937 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001938
1939 requeue:
1940 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1941 return t;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001942}
1943
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001944static int qc_init(struct connection *conn, struct proxy *prx,
1945 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001946{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001947 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001948 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001949
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001950 TRACE_ENTER(QMUX_EV_QCC_NEW);
1951
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001952 qcc = pool_alloc(pool_head_qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001953 if (!qcc) {
1954 TRACE_ERROR("alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001955 goto fail_no_qcc;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001956 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001957
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001958 qcc->conn = conn;
1959 conn->ctx = qcc;
Amaury Denoyellec603de42022-07-25 11:21:46 +02001960 qcc->nb_hreq = qcc->nb_sc = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001961 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001962
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001963 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001964
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001965 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001966
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001967 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001968 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001969
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001970 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001971 qcc->tx.sent_offsets = qcc->tx.offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001972
1973 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001974 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001975 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001976 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001977 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001978
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001979 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001980 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001981 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001982 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001983
1984 /* Server initiated streams must respect the server flow control. */
1985 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001986 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001987 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001988 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1989
1990 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001991 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001992 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001993 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001994
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001995 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001996 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02001997 qcc->lfctl.ms_uni = lparams->initial_max_streams_uni;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02001998 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
1999 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01002000 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01002001
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02002002 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02002003 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02002004
Willy Tarreau784b8682022-04-11 14:18:10 +02002005 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01002006 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01002007 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
2008 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
2009
Amaury Denoyellea509ffb2022-07-04 15:50:33 +02002010 if (conn_is_back(conn)) {
2011 qcc->next_bidi_l = 0x00;
2012 qcc->largest_bidi_r = 0x01;
2013 qcc->next_uni_l = 0x02;
2014 qcc->largest_uni_r = 0x03;
2015 }
2016 else {
2017 qcc->largest_bidi_r = 0x00;
2018 qcc->next_bidi_l = 0x01;
2019 qcc->largest_uni_r = 0x02;
2020 qcc->next_uni_l = 0x03;
2021 }
2022
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002023 qcc->wait_event.tasklet = tasklet_new();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002024 if (!qcc->wait_event.tasklet) {
2025 TRACE_ERROR("taslket alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002026 goto fail_no_tasklet;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002027 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002028
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02002029 LIST_INIT(&qcc->send_retry_list);
2030
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002031 qcc->subs = NULL;
2032 qcc->wait_event.tasklet->process = qc_io_cb;
2033 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01002034 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002035
Amaury Denoyelle07bf8f42022-07-22 16:16:03 +02002036 qcc->proxy = prx;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002037 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002038 qcc->task = NULL;
Amaury Denoyelleb6309452022-07-25 14:51:56 +02002039 qcc->timeout = conn_is_back(qcc->conn) ? prx->timeout.server :
2040 prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002041 if (tick_isset(qcc->timeout)) {
2042 qcc->task = task_new_here();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002043 if (!qcc->task) {
2044 TRACE_ERROR("timeout task alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002045 goto fail_no_timeout_task;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002046 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002047 qcc->task->process = qc_timeout_task;
2048 qcc->task->context = qcc;
2049 qcc->task->expire = tick_add(now_ms, qcc->timeout);
2050 }
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +02002051 qcc_reset_idle_start(qcc);
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02002052 LIST_INIT(&qcc->opening_list);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002053
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002054 if (!conn_is_back(conn)) {
2055 if (!LIST_INLIST(&conn->stopping_list)) {
2056 LIST_APPEND(&mux_stopping_data[tid].list,
2057 &conn->stopping_list);
2058 }
2059 }
2060
Willy Tarreau784b8682022-04-11 14:18:10 +02002061 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002062 /* init read cycle */
2063 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002064
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002065 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002066 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002067
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01002068 fail_no_timeout_task:
2069 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002070 fail_no_tasklet:
2071 pool_free(pool_head_qcc, qcc);
2072 fail_no_qcc:
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002073 TRACE_LEAVE(QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002074 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002075}
2076
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002077static void qc_destroy(void *ctx)
2078{
2079 struct qcc *qcc = ctx;
2080
2081 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
2082 qc_release(qcc);
2083 TRACE_LEAVE(QMUX_EV_QCC_END);
2084}
2085
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002086static void qc_detach(struct sedesc *sd)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002087{
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002088 struct qcs *qcs = sd->se;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002089 struct qcc *qcc = qcs->qcc;
2090
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002091 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002092
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002093 /* TODO this BUG_ON_HOT() is not correct as the stconn layer may detach
2094 * from the stream even if it is not closed remotely at the QUIC layer.
2095 * This happens for example when a stream must be closed due to a
2096 * rejected request. To better handle these cases, it will be required
2097 * to implement shutr/shutw MUX operations. Once this is done, this
2098 * BUG_ON_HOT() statement can be adjusted.
2099 */
2100 //BUG_ON_HOT(!qcs_is_close_remote(qcs));
Amaury Denoyellec603de42022-07-25 11:21:46 +02002101
2102 qcc_rm_sc(qcc);
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002103
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02002104 if (!qcs_is_close_local(qcs) && !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002105 TRACE_STATE("remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002106 qcs->flags |= QC_SF_DETACH;
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002107 qcc_refresh_timeout(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002108
2109 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002110 return;
2111 }
2112
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002113 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01002114
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002115 if (qcc_is_dead(qcc)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002116 TRACE_STATE("killing dead connection", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002117 goto release;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002118 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002119 else if (qcc->task) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002120 TRACE_DEVEL("refreshing connection's timeout", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002121 qcc_refresh_timeout(qcc);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002122 }
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002123 else {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002124 TRACE_DEVEL("completed", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002125 }
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002126
2127 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002128 return;
2129
2130 release:
2131 qc_release(qcc);
2132 TRACE_LEAVE(QMUX_EV_STRM_END);
2133 return;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002134}
2135
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002136/* Called from the upper layer, to receive data */
Willy Tarreau3215e732022-05-27 10:09:11 +02002137static size_t qc_rcv_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002138 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002139{
Willy Tarreau3215e732022-05-27 10:09:11 +02002140 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002141 struct htx *qcs_htx = NULL;
2142 struct htx *cs_htx = NULL;
2143 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002144 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002145
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002146 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002147
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002148 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
2149 if (htx_is_empty(qcs_htx)) {
2150 /* Set buffer data to 0 as HTX is empty. */
2151 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
2152 goto end;
2153 }
2154
2155 ret = qcs_htx->data;
2156
2157 cs_htx = htx_from_buf(buf);
2158 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
Amaury Denoyelle72a78e82022-07-29 15:34:12 +02002159 /* EOM will be copied to cs_htx via b_xfer(). */
2160 if (qcs_htx->flags & HTX_FL_EOM)
2161 fin = 1;
2162
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002163 htx_to_buf(cs_htx, buf);
2164 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
2165 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
2166 goto end;
2167 }
2168
2169 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
2170 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
2171
2172 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002173 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
2174 cs_htx->flags |= HTX_FL_EOM;
2175 fin = 1;
2176 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002177
2178 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
2179 htx_to_buf(cs_htx, buf);
2180 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
2181 ret -= qcs_htx->data;
2182
2183 end:
2184 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002185 se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002186 }
2187 else {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002188 se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
2189 if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING))
2190 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002191
Amaury Denoyelle72a78e82022-07-29 15:34:12 +02002192 /* Set end-of-input if FIN received and all data extracted. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002193 if (fin)
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002194 se_fl_set(qcs->sd, SE_FL_EOI);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002195
2196 if (b_size(&qcs->rx.app_buf)) {
2197 b_free(&qcs->rx.app_buf);
2198 offer_buffers(NULL, 1);
2199 }
2200 }
2201
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002202 if (ret) {
2203 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002204 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002205 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002206
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002207 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
2208
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002209 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002210}
2211
Willy Tarreau3215e732022-05-27 10:09:11 +02002212static size_t qc_snd_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002213 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002214{
Willy Tarreau3215e732022-05-27 10:09:11 +02002215 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002216 size_t ret;
2217
2218 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002219
Amaury Denoyelle843a1192022-07-04 11:44:38 +02002220 if (qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_RESET)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002221 ret = count;
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002222 goto end;
2223 }
2224
Willy Tarreau3215e732022-05-27 10:09:11 +02002225 ret = qcs->qcc->app_ops->snd_buf(sc, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002226
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002227 end:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002228 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
2229
2230 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002231}
2232
2233/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2234 * event subscriber <es> is not allowed to change from a previous call as long
2235 * as at least one event is still subscribed. The <event_type> must only be a
2236 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
2237 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002238static int qc_subscribe(struct stconn *sc, int event_type,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002239 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002240{
Willy Tarreau3215e732022-05-27 10:09:11 +02002241 return qcs_subscribe(__sc_mux_strm(sc), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002242}
2243
2244/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
2245 * The <es> pointer is not allowed to differ from the one passed to the
2246 * subscribe() call. It always returns zero.
2247 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002248static int qc_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002249{
Willy Tarreau3215e732022-05-27 10:09:11 +02002250 struct qcs *qcs = __sc_mux_strm(sc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002251
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002252 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
2253 BUG_ON(qcs->subs && qcs->subs != es);
2254
2255 es->events &= ~event_type;
2256 if (!es->events)
2257 qcs->subs = NULL;
2258
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002259 return 0;
2260}
2261
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002262/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Willy Tarreau4596fe22022-05-17 19:07:51 +02002263 * report SE_FL_ERR_PENDING|SE_FL_ERROR on the attached stream connectors and
2264 * wake them.
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002265 */
2266static int qc_wake_some_streams(struct qcc *qcc)
2267{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002268 struct qcs *qcs;
2269 struct eb64_node *node;
2270
2271 for (node = eb64_first(&qcc->streams_by_id); node;
2272 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02002273 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002274
Amaury Denoyelle3abeb572022-07-04 11:42:27 +02002275 if (!qcs_sc(qcs))
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002276 continue;
2277
2278 if (qcc->conn->flags & CO_FL_ERROR) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002279 se_fl_set(qcs->sd, SE_FL_ERR_PENDING);
2280 if (se_fl_test(qcs->sd, SE_FL_EOS))
2281 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002282
Amaury Denoyelle4561f842022-07-06 14:54:34 +02002283 qcs_alert(qcs);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002284 }
2285 }
2286
2287 return 0;
2288}
2289
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002290static int qc_wake(struct connection *conn)
2291{
2292 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02002293 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002294
2295 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002296
2297 /* Check if a soft-stop is in progress.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002298 *
2299 * TODO this is revelant for frontend connections only.
Amaury Denoyelled0c62322022-05-23 08:52:58 +02002300 *
2301 * TODO Client should be notified with a H3 GOAWAY and then a
2302 * CONNECTION_CLOSE. However, quic-conn uses the listener socket for
2303 * sending which at this stage is already closed.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002304 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002305 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Amaury Denoyelled0c62322022-05-23 08:52:58 +02002306 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002307
Willy Tarreau784b8682022-04-11 14:18:10 +02002308 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002309 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
2310
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002311 qc_send(qcc);
2312
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002313 qc_wake_some_streams(qcc);
2314
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002315 if (qcc_is_dead(qcc))
2316 goto release;
2317
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002318 qcc_refresh_timeout(qcc);
2319
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002320 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002321 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002322
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002323 release:
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002324 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002325 qc_release(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002326 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002327 return 1;
2328}
2329
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002330
2331static char *qcs_st_to_str(enum qcs_state st)
2332{
2333 switch (st) {
2334 case QC_SS_IDLE: return "IDL";
2335 case QC_SS_OPEN: return "OPN";
2336 case QC_SS_HLOC: return "HCL";
2337 case QC_SS_HREM: return "HCR";
2338 case QC_SS_CLO: return "CLO";
2339 default: return "???";
2340 }
2341}
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01002342
Willy Tarreaub4a4fee2022-09-02 16:00:40 +02002343/* for debugging with CLI's "show sess" command. May emit multiple lines, each
2344 * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single
2345 * line is used. Each field starts with a space so it's safe to print it after
2346 * existing fields.
2347 */
2348static int qc_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx)
2349{
2350 struct qcs *qcs = sd->se;
2351 struct qcc *qcc;
2352 int ret = 0;
2353
2354 if (!qcs)
2355 return ret;
2356
2357 chunk_appendf(msg, " qcs=%p .flg=%#x .id=%llu .st=%s .ctx=%p, .err=%#llx",
2358 qcs, qcs->flags, (ull)qcs->id, qcs_st_to_str(qcs->st), qcs->ctx, (ull)qcs->err);
2359
2360 if (pfx)
2361 chunk_appendf(msg, "\n%s", pfx);
2362
2363 qcc = qcs->qcc;
2364 chunk_appendf(msg, " qcc=%p .flg=%#x .nbsc=%llu .nbhreq=%llu, .task=%p",
2365 qcc, qcc->flags, (ull)qcc->nb_sc, (ull)qcc->nb_hreq, qcc->task);
2366 return ret;
2367}
2368
2369
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002370static void qmux_trace_frm(const struct quic_frame *frm)
2371{
2372 switch (frm->type) {
2373 case QUIC_FT_MAX_STREAMS_BIDI:
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002374 chunk_appendf(&trace_buf, " max_streams=%llu",
2375 (ull)frm->max_streams_bidi.max_streams);
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002376 break;
2377
2378 case QUIC_FT_MAX_STREAMS_UNI:
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002379 chunk_appendf(&trace_buf, " max_streams=%llu",
2380 (ull)frm->max_streams_uni.max_streams);
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002381 break;
2382
2383 default:
2384 break;
2385 }
2386}
2387
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01002388/* quic-mux trace handler */
2389static void qmux_trace(enum trace_level level, uint64_t mask,
2390 const struct trace_source *src,
2391 const struct ist where, const struct ist func,
2392 const void *a1, const void *a2, const void *a3, const void *a4)
2393{
2394 const struct connection *conn = a1;
2395 const struct qcc *qcc = conn ? conn->ctx : NULL;
2396 const struct qcs *qcs = a2;
2397
2398 if (!qcc)
2399 return;
2400
2401 if (src->verbosity > QMUX_VERB_CLEAN) {
2402 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
Frédéric Lécaillee4c30742022-08-19 12:02:29 +02002403 if (qcc->conn->handle.qc)
2404 chunk_appendf(&trace_buf, " qc=%p", qcc->conn->handle.qc);
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01002405
2406 if (qcs)
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002407 chunk_appendf(&trace_buf, " qcs=%p .id=%llu .st=%s",
2408 qcs, (ull)qcs->id,
2409 qcs_st_to_str(qcs->st));
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002410
2411 if (mask & QMUX_EV_QCC_NQCS) {
2412 const uint64_t *id = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002413 chunk_appendf(&trace_buf, " id=%llu", (ull)*id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002414 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002415
2416 if (mask & QMUX_EV_SEND_FRM)
2417 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01002418
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02002419 if (mask & QMUX_EV_QCS_XFER_DATA) {
2420 const struct qcs_xfer_data_trace_arg *arg = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002421 chunk_appendf(&trace_buf, " prep=%llu xfer=%d",
2422 (ull)arg->prep, arg->xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02002423 }
2424
2425 if (mask & QMUX_EV_QCS_BUILD_STRM) {
2426 const struct qcs_build_stream_trace_arg *arg = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002427 chunk_appendf(&trace_buf, " len=%llu fin=%d offset=%llu",
2428 (ull)arg->len, arg->fin, (ull)arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01002429 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01002430 }
2431}
2432
2433
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002434static const struct mux_ops qc_ops = {
2435 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002436 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002437 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002438 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002439 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002440 .subscribe = qc_subscribe,
2441 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002442 .wake = qc_wake,
Willy Tarreaub4a4fee2022-09-02 16:00:40 +02002443 .show_sd = qc_show_sd,
Willy Tarreaub5821e12022-04-26 11:54:08 +02002444 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02002445 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002446};
2447
2448static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002449 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002450
2451INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);