blob: 22a0566b7b3a8a88d7ec69b30a9a5a158374179d [file] [log] [blame]
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001#include <haproxy/mux_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002
Amaury Denoyelleeb01f592021-10-07 16:44:05 +02003#include <import/eb64tree.h>
4
Frédéric Lécailledfbae762021-02-18 09:59:01 +01005#include <haproxy/api.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01006#include <haproxy/connection.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +01007#include <haproxy/dynbuf.h>
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01008#include <haproxy/htx.h>
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02009#include <haproxy/list.h>
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +020010#include <haproxy/ncbuf.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +010011#include <haproxy/pool.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020012#include <haproxy/quic_stream.h>
Frédéric Lécaille748ece62022-05-21 23:58:40 +020013#include <haproxy/quic_tp-t.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020014#include <haproxy/ssl_sock-t.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020015#include <haproxy/stconn.h>
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010016#include <haproxy/trace.h>
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +010017#include <haproxy/xprt_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010018
Amaury Denoyelledeed7772021-12-03 11:36:46 +010019DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010020DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
21
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010022/* trace source and events */
23static void qmux_trace(enum trace_level level, uint64_t mask,
24 const struct trace_source *src,
25 const struct ist where, const struct ist func,
26 const void *a1, const void *a2, const void *a3, const void *a4);
27
28static const struct trace_event qmux_trace_events[] = {
Amaury Denoyelle4f137572022-03-24 17:10:00 +010029#define QMUX_EV_QCC_RECV (1ULL << 1)
30 { .mask = QMUX_EV_QCC_RECV, .name = "qcc_recv", .desc = "Rx on QUIC connection" },
31#define QMUX_EV_QCC_SEND (1ULL << 2)
32 { .mask = QMUX_EV_QCC_SEND, .name = "qcc_send", .desc = "Tx on QUIC connection" },
33#define QMUX_EV_QCC_WAKE (1ULL << 3)
34 { .mask = QMUX_EV_QCC_WAKE, .name = "qcc_wake", .desc = "QUIC connection woken up" },
35#define QMUX_EV_QCC_END (1ULL << 4)
36 { .mask = QMUX_EV_QCC_END, .name = "qcc_end", .desc = "QUIC connection terminated" },
37#define QMUX_EV_QCC_NQCS (1ULL << 5)
38 { .mask = QMUX_EV_QCC_NQCS, .name = "qcc_no_qcs", .desc = "QUIC stream not found" },
39#define QMUX_EV_QCS_NEW (1ULL << 6)
40 { .mask = QMUX_EV_QCS_NEW, .name = "qcs_new", .desc = "new QUIC stream" },
41#define QMUX_EV_QCS_RECV (1ULL << 7)
42 { .mask = QMUX_EV_QCS_RECV, .name = "qcs_recv", .desc = "Rx on QUIC stream" },
43#define QMUX_EV_QCS_SEND (1ULL << 8)
44 { .mask = QMUX_EV_QCS_SEND, .name = "qcs_send", .desc = "Tx on QUIC stream" },
45#define QMUX_EV_QCS_END (1ULL << 9)
46 { .mask = QMUX_EV_QCS_END, .name = "qcs_end", .desc = "QUIC stream terminated" },
47#define QMUX_EV_STRM_RECV (1ULL << 10)
48 { .mask = QMUX_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
49#define QMUX_EV_STRM_SEND (1ULL << 11)
50 { .mask = QMUX_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
51#define QMUX_EV_STRM_END (1ULL << 12)
52 { .mask = QMUX_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
Amaury Denoyellefa29f332022-03-25 09:09:40 +010053#define QMUX_EV_SEND_FRM (1ULL << 13)
54 { .mask = QMUX_EV_SEND_FRM, .name = "send_frm", .desc = "sending QUIC frame" },
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020055/* special event dedicated to qcs_xfer_data */
56#define QMUX_EV_QCS_XFER_DATA (1ULL << 14)
57 { .mask = QMUX_EV_QCS_XFER_DATA, .name = "qcs_xfer_data", .desc = "qcs_xfer_data" },
58/* special event dedicated to qcs_build_stream_frm */
59#define QMUX_EV_QCS_BUILD_STRM (1ULL << 15)
60 { .mask = QMUX_EV_QCS_BUILD_STRM, .name = "qcs_build_stream_frm", .desc = "qcs_build_stream_frm" },
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +020061#define QMUX_EV_PROTO_ERR (1ULL << 16)
62 { .mask = QMUX_EV_PROTO_ERR, .name = "proto_err", .desc = "protocol error" },
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010063 { }
64};
65
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020066/* custom arg for QMUX_EV_QCS_XFER_DATA */
67struct qcs_xfer_data_trace_arg {
68 size_t prep;
Amaury Denoyellefdcec362022-03-25 09:28:10 +010069 int xfer;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020070};
71/* custom arg for QMUX_EV_QCS_BUILD_STRM */
72struct qcs_build_stream_trace_arg {
73 size_t len;
Amaury Denoyellefdcec362022-03-25 09:28:10 +010074 char fin;
75 uint64_t offset;
76};
77
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010078static const struct name_desc qmux_trace_lockon_args[4] = {
79 /* arg1 */ { /* already used by the connection */ },
80 /* arg2 */ { .name="qcs", .desc="QUIC stream" },
81 /* arg3 */ { },
82 /* arg4 */ { }
83};
84
85static const struct name_desc qmux_trace_decoding[] = {
86#define QMUX_VERB_CLEAN 1
87 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
88#define QMUX_VERB_MINIMAL 2
89 { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" },
90 { /* end */ }
91};
92
93struct trace_source trace_qmux = {
94 .name = IST("qmux"),
95 .desc = "QUIC multiplexer",
96 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
97 .default_cb = qmux_trace,
98 .known_events = qmux_trace_events,
99 .lockon_args = qmux_trace_lockon_args,
100 .decoding = qmux_trace_decoding,
101 .report_events = ~0, /* report everything by default */
102};
103
104#define TRACE_SOURCE &trace_qmux
105INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
106
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200107/* Emit a CONNECTION_CLOSE with error <err>. This will interrupt all future
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200108 * send/receive operations.
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200109 */
110static void qcc_emit_cc(struct qcc *qcc, int err)
111{
Amaury Denoyelle57e6db72022-07-13 15:07:56 +0200112 quic_set_connection_close(qcc->conn->handle.qc, quic_err_transport(err));
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200113 qcc->flags |= QC_CF_CC_EMIT;
114 tasklet_wakeup(qcc->wait_event.tasklet);
115}
116
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100117/* Allocate a new QUIC streams with id <id> and type <type>. */
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200118static struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100119{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100120 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100121
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100122 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
123
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100124 qcs = pool_alloc(pool_head_qcs);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200125 if (!qcs) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200126 TRACE_ERROR("alloc failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200127 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200128 }
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200129
130 qcs->stream = NULL;
131 qcs->qcc = qcc;
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +0200132 qcs->sd = NULL;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200133 qcs->flags = QC_SF_NONE;
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200134 qcs->st = QC_SS_IDLE;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200135 qcs->ctx = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100136
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200137 /* App callback attach may register the stream for http-request wait.
138 * These fields must be initialed before.
139 */
140 LIST_INIT(&qcs->el_opening);
141 qcs->start = TICK_ETERNITY;
142
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200143 /* Allocate transport layer stream descriptor. Only needed for TX. */
144 if (!quic_stream_is_uni(id) || !quic_stream_is_remote(qcc, id)) {
145 struct quic_conn *qc = qcc->conn->handle.qc;
146 qcs->stream = qc_stream_desc_new(id, type, qcs, qc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200147 if (!qcs->stream) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200148 TRACE_ERROR("qc_stream_desc alloc failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200149 goto err;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200150 }
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200151 }
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200152
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +0200153 qcs->id = qcs->by_id.key = id;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200154 if (qcc->app_ops->attach) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200155 if (qcc->app_ops->attach(qcs, qcc->ctx)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200156 TRACE_ERROR("app proto failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200157 goto err;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200158 }
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200159 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100160
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200161 /* store transport layer stream descriptor in qcc tree */
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200162 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100163
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100164 qcc->strms[type].nb_streams++;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100165
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100166 /* If stream is local, use peer remote-limit, or else the opposite. */
167 /* TODO use uni limit for unidirectional streams */
168 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
169 qcc->rfctl.msd_bidi_l;
170
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200171 qcs->rx.ncbuf = NCBUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100172 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200173 qcs->rx.offset = qcs->rx.offset_max = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100174
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200175 /* TODO use uni limit for unidirectional streams */
176 qcs->rx.msd = quic_stream_is_local(qcc, id) ? qcc->lfctl.msd_bidi_l :
177 qcc->lfctl.msd_bidi_r;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200178 qcs->rx.msd_init = qcs->rx.msd;
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200179
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100180 qcs->tx.buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100181 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100182 qcs->tx.sent_offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100183
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100184 qcs->wait_event.tasklet = NULL;
185 qcs->wait_event.events = 0;
186 qcs->subs = NULL;
187
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200188 qcs->err = 0;
189
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100190 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100191 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100192 return qcs;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200193
194 err:
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200195 if (qcs->ctx && qcc->app_ops->detach)
196 qcc->app_ops->detach(qcs);
197
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200198 qc_stream_desc_release(qcs->stream);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200199 pool_free(pool_head_qcs, qcs);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200200
201 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200202 return NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100203}
204
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200205static void qc_free_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
206{
207 struct buffer buf;
208
Amaury Denoyelle7313f5e2022-05-17 18:53:21 +0200209 if (ncb_is_null(ncbuf))
210 return;
211
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200212 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
213 b_free(&buf);
Amaury Denoyelle7313f5e2022-05-17 18:53:21 +0200214 offer_buffers(NULL, 1);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200215
216 *ncbuf = NCBUF_NULL;
217}
218
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200219/* Free a qcs. This function must only be done to remove a stream on allocation
220 * error or connection shutdown. Else use qcs_destroy which handle all the
221 * QUIC connection mechanism.
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100222 */
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200223static void qcs_free(struct qcs *qcs)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100224{
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200225 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200226 b_free(&qcs->tx.buf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200227
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200228 BUG_ON(!qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams);
229 --qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams;
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200230
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200231 if (qcs->ctx && qcs->qcc->app_ops->detach)
232 qcs->qcc->app_ops->detach(qcs);
233
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200234 qc_stream_desc_release(qcs->stream);
235
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +0200236 BUG_ON(qcs->sd && !se_fl_test(qcs->sd, SE_FL_ORPHAN));
237 sedesc_free(qcs->sd);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200238
239 eb64_delete(&qcs->by_id);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100240 pool_free(pool_head_qcs, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100241}
242
Amaury Denoyelle3abeb572022-07-04 11:42:27 +0200243static forceinline struct stconn *qcs_sc(const struct qcs *qcs)
244{
245 return qcs->sd ? qcs->sd->sc : NULL;
246}
247
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200248/* Reset the <qcc> inactivity timeout for http-keep-alive timeout. */
249static forceinline void qcc_reset_idle_start(struct qcc *qcc)
250{
251 qcc->idle_start = now_ms;
252}
253
Amaury Denoyellec603de42022-07-25 11:21:46 +0200254/* Decrement <qcc> sc. */
255static forceinline void qcc_rm_sc(struct qcc *qcc)
256{
257 BUG_ON_HOT(!qcc->nb_sc);
258 --qcc->nb_sc;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200259
260 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
261 * refreshed after this on stream detach.
262 */
263 if (!qcc->nb_sc && !qcc->nb_hreq)
264 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200265}
266
267/* Decrement <qcc> hreq. */
268static forceinline void qcc_rm_hreq(struct qcc *qcc)
269{
270 BUG_ON_HOT(!qcc->nb_hreq);
271 --qcc->nb_hreq;
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 I/O handler.
275 */
276 if (!qcc->nb_sc && !qcc->nb_hreq)
277 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200278}
279
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200280static inline int qcc_is_dead(const struct qcc *qcc)
281{
282 /* Mux connection is considered dead if :
283 * - all stream-desc are detached AND
284 * = connection is on error OR
285 * = mux timeout has already fired or is unset
286 */
287 if (!qcc->nb_sc && ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task))
288 return 1;
289
290 return 0;
291}
292
293/* Return true if the mux timeout should be armed. */
294static inline int qcc_may_expire(struct qcc *qcc)
295{
296 return !qcc->nb_sc;
297}
298
299/* Refresh the timeout on <qcc> if needed depending on its state. */
300static void qcc_refresh_timeout(struct qcc *qcc)
301{
302 const struct proxy *px = qcc->proxy;
303
304 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
305
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200306 if (!qcc->task) {
307 TRACE_DEVEL("already expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200308 goto leave;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200309 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200310
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200311 /* Check if upper layer is responsible of timeout management. */
312 if (!qcc_may_expire(qcc)) {
313 TRACE_DEVEL("not eligible for timeout", QMUX_EV_QCC_WAKE, qcc->conn);
314 qcc->task->expire = TICK_ETERNITY;
315 task_queue(qcc->task);
316 goto leave;
317 }
318
319 /* TODO if connection is idle on frontend and proxy is disabled, remove
320 * it with global close_spread delay applied.
321 */
322
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200323 /* TODO implement client/server-fin timeout for graceful shutdown */
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200324
325 /* Frontend timeout management
326 * - detached streams with data left to send -> default timeout
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200327 * - stream waiting on incomplete request or no stream yet activated -> timeout http-request
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200328 * - idle after stream processing -> timeout http-keep-alive
329 */
330 if (!conn_is_back(qcc->conn)) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200331 if (qcc->nb_hreq) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200332 TRACE_DEVEL("one or more requests still in progress", QMUX_EV_QCC_WAKE, qcc->conn);
333 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200334 task_queue(qcc->task);
335 goto leave;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200336 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200337
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200338 if (!LIST_ISEMPTY(&qcc->opening_list) || unlikely(!qcc->largest_bidi_r)) {
339 int timeout = px->timeout.httpreq;
340 struct qcs *qcs = NULL;
341 int base_time;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200342
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200343 /* Use start time of first stream waiting on HTTP or
344 * qcc idle if no stream not yet used.
345 */
346 if (likely(!LIST_ISEMPTY(&qcc->opening_list)))
347 qcs = LIST_ELEM(qcc->opening_list.n, struct qcs *, el_opening);
348 base_time = qcs ? qcs->start : qcc->idle_start;
349
350 TRACE_DEVEL("waiting on http request", QMUX_EV_QCC_WAKE, qcc->conn, qcs);
351 qcc->task->expire = tick_add_ifset(base_time, timeout);
352 }
353 else {
354 /* Use http-request timeout if keep-alive timeout not set */
355 int timeout = tick_isset(px->timeout.httpka) ?
356 px->timeout.httpka : px->timeout.httpreq;
357
358 TRACE_DEVEL("at least one request achieved but none currently in progress", QMUX_EV_QCC_WAKE, qcc->conn);
359 qcc->task->expire = tick_add_ifset(qcc->idle_start, timeout);
360 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200361 }
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200362
363 /* fallback to default timeout if frontend specific undefined or for
364 * backend connections.
365 */
366 if (!tick_isset(qcc->task->expire)) {
367 TRACE_DEVEL("fallback to default timeout", QMUX_EV_QCC_WAKE, qcc->conn);
368 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200369 }
370
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200371 task_queue(qcc->task);
372
373 leave:
374 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
375}
376
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200377/* Mark a stream as open if it was idle. This can be used on every
378 * successful emission/reception operation to update the stream state.
379 */
380static void qcs_idle_open(struct qcs *qcs)
381{
382 /* This operation must not be used if the stream is already closed. */
383 BUG_ON_HOT(qcs->st == QC_SS_CLO);
384
385 if (qcs->st == QC_SS_IDLE) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200386 TRACE_STATE("opening stream", QMUX_EV_QCS_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200387 qcs->st = QC_SS_OPEN;
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200388 }
389}
390
391/* Close the local channel of <qcs> instance. */
392static void qcs_close_local(struct qcs *qcs)
393{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200394 TRACE_STATE("closing stream locally", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
395
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200396 /* The stream must have already been opened. */
397 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
398
399 /* This operation cannot be used multiple times. */
400 BUG_ON_HOT(qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO);
401
402 if (quic_stream_is_bidi(qcs->id)) {
403 qcs->st = (qcs->st == QC_SS_HREM) ? QC_SS_CLO : QC_SS_HLOC;
Amaury Denoyellec603de42022-07-25 11:21:46 +0200404 qcc_rm_hreq(qcs->qcc);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200405 }
406 else {
407 /* Only local uni streams are valid for this operation. */
408 BUG_ON_HOT(quic_stream_is_remote(qcs->qcc, qcs->id));
409 qcs->st = QC_SS_CLO;
410 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200411}
412
413/* Close the remote channel of <qcs> instance. */
414static void qcs_close_remote(struct qcs *qcs)
415{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200416 TRACE_STATE("closing stream remotely", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
417
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200418 /* The stream must have already been opened. */
419 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
420
421 /* This operation cannot be used multiple times. */
422 BUG_ON_HOT(qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO);
423
424 if (quic_stream_is_bidi(qcs->id)) {
425 qcs->st = (qcs->st == QC_SS_HLOC) ? QC_SS_CLO : QC_SS_HREM;
426 }
427 else {
428 /* Only remote uni streams are valid for this operation. */
429 BUG_ON_HOT(quic_stream_is_local(qcs->qcc, qcs->id));
430 qcs->st = QC_SS_CLO;
431 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200432}
433
434static int qcs_is_close_local(struct qcs *qcs)
435{
436 return qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO;
437}
438
439static __maybe_unused int qcs_is_close_remote(struct qcs *qcs)
440{
441 return qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO;
442}
443
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100444struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100445{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100446 struct buffer *buf = b_alloc(bptr);
447 BUG_ON(!buf);
448 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100449}
450
Amaury Denoyellea441ec92022-07-04 15:48:57 +0200451static struct ncbuf *qc_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200452{
453 struct buffer buf = BUF_NULL;
454
455 if (ncb_is_null(ncbuf)) {
456 b_alloc(&buf);
457 BUG_ON(b_is_null(&buf));
458
459 *ncbuf = ncb_make(buf.area, buf.size, 0);
460 ncb_init(ncbuf, 0);
461 }
462
463 return ncbuf;
464}
465
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500466/* Notify an eventual subscriber on <qcs> or else wakeup up the stconn layer if
Amaury Denoyelle4561f842022-07-06 14:54:34 +0200467 * initialized.
468 */
469static void qcs_alert(struct qcs *qcs)
470{
471 if (qcs->subs) {
472 qcs_notify_recv(qcs);
473 qcs_notify_send(qcs);
474 }
475 else if (qcs_sc(qcs) && qcs->sd->sc->app_ops->wake) {
476 qcs->sd->sc->app_ops->wake(qcs->sd->sc);
477 }
478}
479
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100480int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
481{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100482 struct qcc *qcc = qcs->qcc;
483
484 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100485
486 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
487 BUG_ON(qcs->subs && qcs->subs != es);
488
489 es->events |= event_type;
490 qcs->subs = es;
491
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100492 if (event_type & SUB_RETRY_RECV)
493 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
494
495 if (event_type & SUB_RETRY_SEND)
496 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
497
498 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
499
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100500 return 0;
501}
502
503void qcs_notify_recv(struct qcs *qcs)
504{
505 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
506 tasklet_wakeup(qcs->subs->tasklet);
507 qcs->subs->events &= ~SUB_RETRY_RECV;
508 if (!qcs->subs->events)
509 qcs->subs = NULL;
510 }
511}
512
513void qcs_notify_send(struct qcs *qcs)
514{
515 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
516 tasklet_wakeup(qcs->subs->tasklet);
517 qcs->subs->events &= ~SUB_RETRY_SEND;
518 if (!qcs->subs->events)
519 qcs->subs = NULL;
520 }
521}
522
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200523/* Open a locally initiated stream for the connection <qcc>. Set <bidi> for a
524 * bidirectional stream, else an unidirectional stream is opened. The next
525 * available ID on the connection will be used according to the stream type.
526 *
527 * Returns the allocated stream instance or NULL on error.
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100528 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200529struct qcs *qcc_init_stream_local(struct qcc *qcc, int bidi)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100530{
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200531 struct qcs *qcs;
532 enum qcs_type type;
533 uint64_t *next;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100534
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200535 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
536
537 if (bidi) {
538 next = &qcc->next_bidi_l;
539 type = conn_is_back(qcc->conn) ? QCS_CLT_BIDI : QCS_SRV_BIDI;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100540 }
541 else {
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200542 next = &qcc->next_uni_l;
543 type = conn_is_back(qcc->conn) ? QCS_CLT_UNI : QCS_SRV_UNI;
544 }
545
546 /* TODO ensure that we won't overflow remote peer flow control limit on
547 * streams. Else, we should emit a STREAMS_BLOCKED frame.
548 */
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100549
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200550 qcs = qcs_new(qcc, *next, type);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200551 if (!qcs) {
552 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200553 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200554 }
Amaury Denoyellec055e302022-02-07 16:09:06 +0100555
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200556 TRACE_PROTO("opening local stream", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200557 *next += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100558
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200559 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200560 return qcs;
561}
562
563/* Open a remote initiated stream for the connection <qcc> with ID <id>. The
564 * caller is responsible to ensure that a stream with the same ID was not
565 * already opened. This function will also create all intermediaries streams
566 * with ID smaller than <id> not already opened before.
567 *
568 * Returns the allocated stream instance or NULL on error.
569 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200570static struct qcs *qcc_init_stream_remote(struct qcc *qcc, uint64_t id)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200571{
572 struct qcs *qcs = NULL;
573 enum qcs_type type;
574 uint64_t *largest;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100575
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200576 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200577
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200578 BUG_ON_HOT(quic_stream_is_local(qcc, id));
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100579
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200580 if (quic_stream_is_bidi(id)) {
581 largest = &qcc->largest_bidi_r;
582 type = conn_is_back(qcc->conn) ? QCS_SRV_BIDI : QCS_CLT_BIDI;
583 }
584 else {
585 largest = &qcc->largest_uni_r;
586 type = conn_is_back(qcc->conn) ? QCS_SRV_UNI : QCS_CLT_UNI;
587 }
588
589 /* TODO also checks max-streams for uni streams */
590 if (quic_stream_is_bidi(id)) {
591 if (id >= qcc->lfctl.ms_bidi * 4) {
592 /* RFC 9000 4.6. Controlling Concurrency
593 *
594 * An endpoint that receives a frame with a
595 * stream ID exceeding the limit it has sent
596 * MUST treat this as a connection error of
597 * type STREAM_LIMIT_ERROR
598 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200599 TRACE_ERROR("flow control error", QMUX_EV_QCS_NEW|QMUX_EV_PROTO_ERR, qcc->conn);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200600 qcc_emit_cc(qcc, QC_ERR_STREAM_LIMIT_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200601 goto err;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100602 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200603 }
604
605 /* Only stream ID not already opened can be used. */
606 BUG_ON(id < *largest);
607
608 while (id >= *largest) {
609 const char *str = *largest < id ? "opening intermediary stream" :
610 "opening remote stream";
611
612 qcs = qcs_new(qcc, *largest, type);
613 if (!qcs) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200614 /* TODO emit RESET_STREAM */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200615 TRACE_ERROR("stream fallocation failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200616 goto err;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100617 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200618
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200619 TRACE_PROTO(str, QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200620 *largest += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100621 }
622
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200623 out:
624 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle50742292022-03-29 14:57:19 +0200625 return qcs;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200626
627 err:
628 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
629 return NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200630}
631
632/* Use this function for a stream <id> which is not in <qcc> stream tree. It
633 * returns true if the associated stream is closed.
634 */
635static int qcc_stream_id_is_closed(struct qcc *qcc, uint64_t id)
636{
637 uint64_t *largest;
638
639 /* This function must only be used for stream not present in the stream tree. */
640 BUG_ON_HOT(eb64_lookup(&qcc->streams_by_id, id));
641
642 if (quic_stream_is_local(qcc, id)) {
643 largest = quic_stream_is_uni(id) ? &qcc->next_uni_l :
644 &qcc->next_bidi_l;
645 }
646 else {
647 largest = quic_stream_is_uni(id) ? &qcc->largest_uni_r :
648 &qcc->largest_bidi_r;
649 }
650
651 return id < *largest;
652}
653
654/* Retrieve the stream instance from <id> ID. This can be used when receiving
655 * STREAM, STREAM_DATA_BLOCKED, RESET_STREAM, MAX_STREAM_DATA or STOP_SENDING
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200656 * frames. Set to false <receive_only> or <send_only> if these particular types
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200657 * of streams are not allowed. If the stream instance is found, it is stored in
658 * <out>.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200659 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200660 * Returns 0 on success else non-zero. On error, a RESET_STREAM or a
661 * CONNECTION_CLOSE is automatically emitted. Beware that <out> may be NULL
662 * on success if the stream has already been closed.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200663 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200664int qcc_get_qcs(struct qcc *qcc, uint64_t id, int receive_only, int send_only,
665 struct qcs **out)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200666{
667 struct eb64_node *node;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200668
669 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200670 *out = NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200671
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200672 if (!receive_only && quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200673 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 +0200674 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200675 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200676 }
677
678 if (!send_only && quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200679 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 +0200680 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200681 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200682 }
683
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200684 /* Search the stream in the connection tree. */
685 node = eb64_lookup(&qcc->streams_by_id, id);
686 if (node) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200687 *out = eb64_entry(node, struct qcs, by_id);
688 TRACE_DEVEL("using stream from connection tree", QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200689 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200690 }
691
692 /* Check if stream is already closed. */
693 if (qcc_stream_id_is_closed(qcc, id)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200694 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200695 /* Consider this as a success even if <out> is left NULL. */
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200696 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200697 }
698
699 /* Create the stream. This is valid only for remote initiated one. A
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500700 * local stream must have already been explicitly created by the
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200701 * application protocol layer.
702 */
703 if (quic_stream_is_local(qcc, id)) {
704 /* RFC 9000 19.8. STREAM Frames
705 *
706 * An endpoint MUST terminate the connection with error
707 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
708 * initiated stream that has not yet been created, or for a send-only
709 * stream.
710 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200711 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 +0200712 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200713 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200714 }
715 else {
716 /* Remote stream not found - try to open it. */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200717 *out = qcc_init_stream_remote(qcc, id);
718 if (!*out) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200719 TRACE_ERROR("stream creation error", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200720 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200721 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200722 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100723
724 out:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200725 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200726 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200727
728 err:
729 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
730 return 1;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100731}
732
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200733/* Simple function to duplicate a buffer */
734static inline struct buffer qcs_b_dup(const struct ncbuf *b)
735{
736 return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0));
737}
738
Amaury Denoyelle36d4b5e2022-07-01 11:25:40 +0200739/* Remove <bytes> from <qcs> Rx buffer. Flow-control for received offsets may
740 * be allocated for the peer if needed.
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200741 */
742static void qcs_consume(struct qcs *qcs, uint64_t bytes)
743{
744 struct qcc *qcc = qcs->qcc;
745 struct quic_frame *frm;
746 struct ncbuf *buf = &qcs->rx.ncbuf;
747 enum ncb_ret ret;
748
749 ret = ncb_advance(buf, bytes);
750 if (ret) {
751 ABORT_NOW(); /* should not happens because removal only in data */
752 }
753
754 if (ncb_is_empty(buf))
755 qc_free_ncbuf(qcs, buf);
756
757 qcs->rx.offset += bytes;
758 if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) {
759 frm = pool_zalloc(pool_head_quic_frame);
760 BUG_ON(!frm); /* TODO handle this properly */
761
762 qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init;
763
764 LIST_INIT(&frm->reflist);
765 frm->type = QUIC_FT_MAX_STREAM_DATA;
766 frm->max_stream_data.id = qcs->id;
767 frm->max_stream_data.max_stream_data = qcs->rx.msd;
768
769 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
770 tasklet_wakeup(qcc->wait_event.tasklet);
771 }
772
773 qcc->lfctl.offsets_consume += bytes;
774 if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) {
775 frm = pool_zalloc(pool_head_quic_frame);
776 BUG_ON(!frm); /* TODO handle this properly */
777
778 qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init;
779
780 LIST_INIT(&frm->reflist);
781 frm->type = QUIC_FT_MAX_DATA;
782 frm->max_data.max_data = qcc->lfctl.md;
783
784 LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list);
785 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
786 }
787}
788
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200789/* Decode the content of STREAM frames already received on the stream instance
790 * <qcs>.
791 *
792 * Returns 0 on success else non-zero.
793 */
794static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
795{
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200796 struct buffer b;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200797 ssize_t ret;
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200798 int fin = 0;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200799
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200800 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
801
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200802 b = qcs_b_dup(&qcs->rx.ncbuf);
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200803
804 /* Signal FIN to application if STREAM FIN received and there is no gap
805 * in the Rx buffer.
806 */
Amaury Denoyelle3f39b402022-07-01 16:11:03 +0200807 if (qcs->flags & QC_SF_SIZE_KNOWN && !ncb_is_fragmented(&qcs->rx.ncbuf))
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200808 fin = 1;
809
810 ret = qcc->app_ops->decode_qcs(qcs, &b, fin);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200811 if (ret < 0) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200812 TRACE_ERROR("decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200813 goto err;
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200814 }
815
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200816 if (ret) {
817 qcs_consume(qcs, ret);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200818 qcs_notify_recv(qcs);
819 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200820
821 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200822 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200823
824 err:
825 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
826 return 1;
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200827}
828
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200829/* Emit a CONNECTION_CLOSE_APP with error <err>. Reserved for application error
Amaury Denoyelled666d742022-07-13 15:15:58 +0200830 * code. To close the connection right away, set <immediate> : this is useful
831 * when dealing with a connection fatal error. Else a graceful shutdown will be
832 * conducted : the error-code is only registered. The lower layer is
833 * responsible to close the connection when deemed suitable. Note that in this
834 * case the error code might be overwritten if an immediate close is requested
835 * in the interval.
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200836 */
Amaury Denoyelled666d742022-07-13 15:15:58 +0200837void qcc_emit_cc_app(struct qcc *qcc, int err, int immediate)
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200838{
Amaury Denoyelled666d742022-07-13 15:15:58 +0200839 if (immediate) {
840 quic_set_connection_close(qcc->conn->handle.qc, quic_err_app(err));
841 qcc->flags |= QC_CF_CC_EMIT;
842 tasklet_wakeup(qcc->wait_event.tasklet);
843 }
844 else {
845 /* Only register the error code for graceful shutdown. */
846 qcc->conn->handle.qc->err = quic_err_app(err);
847 }
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200848}
849
850/* Prepare for the emission of RESET_STREAM on <qcs> with error code <err>. */
851void qcc_reset_stream(struct qcs *qcs, int err)
852{
853 struct qcc *qcc = qcs->qcc;
854
855 if ((qcs->flags & QC_SF_TO_RESET) || qcs_is_close_local(qcs))
856 return;
857
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200858 TRACE_STATE("reset stream", QMUX_EV_QCS_END, qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200859 qcs->flags |= QC_SF_TO_RESET;
860 qcs->err = err;
861 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200862}
863
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200864/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
865 * <data> with length <len> and represents the offset <offset>. <fin> is set if
866 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100867 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200868 * Returns 0 on success else non-zero. On error, the received frame should not
869 * be acknowledged.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100870 */
871int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200872 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100873{
874 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200875 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100876
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100877 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
878
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200879 if (qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200880 TRACE_DATA("connection closed", QMUX_EV_QCC_RECV, qcc->conn);
881 goto err;
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200882 }
883
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200884 /* RFC 9000 19.8. STREAM Frames
885 *
886 * An endpoint MUST terminate the connection with error
887 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
888 * initiated stream that has not yet been created, or for a send-only
889 * stream.
890 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200891 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200892 TRACE_DATA("qcs retrieval error", QMUX_EV_QCC_RECV, qcc->conn);
893 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200894 }
895
896 if (!qcs) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200897 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV, qcc->conn);
898 goto out;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200899 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100900
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200901 /* RFC 9000 4.5. Stream Final Size
902 *
903 * Once a final size for a stream is known, it cannot change. If a
904 * RESET_STREAM or STREAM frame is received indicating a change in the
905 * final size for the stream, an endpoint SHOULD respond with an error
906 * of type FINAL_SIZE_ERROR; see Section 11 for details on error
907 * handling.
908 */
909 if (qcs->flags & QC_SF_SIZE_KNOWN &&
910 (offset + len > qcs->rx.offset_max || (fin && offset + len < qcs->rx.offset_max))) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200911 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 +0200912 qcc_emit_cc(qcc, QC_ERR_FINAL_SIZE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200913 goto err;
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200914 }
915
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100916 if (offset + len <= qcs->rx.offset) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200917 TRACE_DATA("already received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
918 goto out;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100919 }
920
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200921 qcs_idle_open(qcs);
922
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200923 if (offset + len > qcs->rx.offset_max) {
924 uint64_t diff = offset + len - qcs->rx.offset_max;
925 qcs->rx.offset_max = offset + len;
926 qcc->lfctl.offsets_recv += diff;
927
928 if (offset + len > qcs->rx.msd ||
929 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
930 /* RFC 9000 4.1. Data Flow Control
931 *
932 * A receiver MUST close the connection with an error
933 * of type FLOW_CONTROL_ERROR if the sender violates
934 * the advertised connection or stream data limits
935 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200936 TRACE_ERROR("flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200937 qcc->conn, qcs);
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200938 qcc_emit_cc(qcc, QC_ERR_FLOW_CONTROL_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200939 goto err;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200940 }
941 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100942
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200943 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100944 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200945 ABORT_NOW();
946 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100947 }
948
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200949 TRACE_DATA("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200950 if (offset < qcs->rx.offset) {
Frédéric Lécaillea18c3332022-07-04 09:54:58 +0200951 size_t diff = qcs->rx.offset - offset;
952
953 len -= diff;
954 data += diff;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200955 offset = qcs->rx.offset;
956 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100957
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200958 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
959 if (ret != NCB_RET_OK) {
960 if (ret == NCB_RET_DATA_REJ) {
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200961 /* RFC 9000 2.2. Sending and Receiving Data
962 *
963 * An endpoint could receive data for a stream at the
964 * same stream offset multiple times. Data that has
965 * already been received can be discarded. The data at
966 * a given offset MUST NOT change if it is sent
967 * multiple times; an endpoint MAY treat receipt of
968 * different data at the same offset within a stream as
969 * a connection error of type PROTOCOL_VIOLATION.
970 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200971 TRACE_ERROR("overlapping data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200972 qcc->conn, qcs);
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200973 qcc_emit_cc(qcc, QC_ERR_PROTOCOL_VIOLATION);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200974 }
975 else if (ret == NCB_RET_GAP_SIZE) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200976 TRACE_DATA("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
977 qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200978 }
979 return 1;
980 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100981
982 if (fin)
Amaury Denoyelle3f39b402022-07-01 16:11:03 +0200983 qcs->flags |= QC_SF_SIZE_KNOWN;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100984
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200985 if (qcs->flags & QC_SF_SIZE_KNOWN && !ncb_is_fragmented(&qcs->rx.ncbuf))
986 qcs_close_remote(qcs);
987
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200988 if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200989 qcc_decode_qcs(qcc, qcs);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200990 qcc_refresh_timeout(qcc);
991 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200992
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200993 if (qcs->flags & QC_SF_READ_ABORTED) {
994 /* TODO should send a STOP_SENDING */
995 qcs_free(qcs);
996 }
997
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200998 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100999 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001000 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001001
1002 err:
1003 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1004 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01001005}
1006
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001007/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
1008 * the frame.
1009 *
1010 * Returns 0 on success else non-zero.
1011 */
1012int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
1013{
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001014 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1015
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001016 if (qcc->rfctl.md < max) {
1017 qcc->rfctl.md = max;
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001018 TRACE_DEVEL("increase remote max-data", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001019
1020 if (qcc->flags & QC_CF_BLK_MFCTL) {
1021 qcc->flags &= ~QC_CF_BLK_MFCTL;
1022 tasklet_wakeup(qcc->wait_event.tasklet);
1023 }
1024 }
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001025
1026 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01001027 return 0;
1028}
1029
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001030/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
1031 * field of the frame and <id> is the identifier of the QUIC stream.
1032 *
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001033 * Returns 0 on success else non-zero. On error, the received frame should not
1034 * be acknowledged.
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001035 */
1036int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
1037{
1038 struct qcs *qcs;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001039
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001040 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1041
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001042 /* RFC 9000 19.10. MAX_STREAM_DATA Frames
1043 *
1044 * Receiving a MAX_STREAM_DATA frame for a locally
1045 * initiated stream that has not yet been created MUST be treated as a
1046 * connection error of type STREAM_STATE_ERROR. An endpoint that
1047 * receives a MAX_STREAM_DATA frame for a receive-only stream MUST
1048 * terminate the connection with error STREAM_STATE_ERROR.
1049 */
1050 if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) {
1051 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1052 return 1;
1053 }
1054
1055 if (qcs) {
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001056 if (max > qcs->tx.msd) {
1057 qcs->tx.msd = max;
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001058 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 +01001059
1060 if (qcs->flags & QC_SF_BLK_SFCTL) {
1061 qcs->flags &= ~QC_SF_BLK_SFCTL;
1062 tasklet_wakeup(qcc->wait_event.tasklet);
1063 }
1064 }
1065 }
1066
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001067 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1068 qcc_refresh_timeout(qcc);
1069
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001070 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001071 return 0;
1072}
1073
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001074/* Handle a new STOP_SENDING frame for stream ID <id>. The error code should be
1075 * specified in <err>.
1076 *
1077 * Returns 0 on success else non-zero. On error, the received frame should not
1078 * be acknowledged.
1079 */
1080int qcc_recv_stop_sending(struct qcc *qcc, uint64_t id, uint64_t err)
1081{
1082 struct qcs *qcs;
1083
1084 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1085
1086 /* RFC 9000 19.5. STOP_SENDING Frames
1087 *
1088 * Receiving a STOP_SENDING frame for a
1089 * locally initiated stream that has not yet been created MUST be
1090 * treated as a connection error of type STREAM_STATE_ERROR. An
1091 * endpoint that receives a STOP_SENDING frame for a receive-only stream
1092 * MUST terminate the connection with error STREAM_STATE_ERROR.
1093 */
1094 if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) {
1095 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1096 return 1;
1097 }
1098
1099 if (!qcs)
1100 goto out;
1101
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001102 TRACE_PROTO("receiving STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle96ca1b72022-08-09 17:36:38 +02001103 qcs_idle_open(qcs);
1104
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001105 /* RFC 9000 3.5. Solicited State Transitions
1106 *
1107 * An endpoint that receives a STOP_SENDING frame
1108 * MUST send a RESET_STREAM frame if the stream is in the "Ready" or
1109 * "Send" state. If the stream is in the "Data Sent" state, the
1110 * endpoint MAY defer sending the RESET_STREAM frame until the packets
1111 * containing outstanding data are acknowledged or declared lost. If
1112 * any outstanding data is declared lost, the endpoint SHOULD send a
1113 * RESET_STREAM frame instead of retransmitting the data.
1114 *
1115 * An endpoint SHOULD copy the error code from the STOP_SENDING frame to
1116 * the RESET_STREAM frame it sends, but it can use any application error
1117 * code.
1118 */
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001119 qcc_reset_stream(qcs, err);
1120
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001121 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1122 qcc_refresh_timeout(qcc);
1123
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001124 out:
1125 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1126 return 0;
1127}
1128
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001129/* Signal the closing of remote stream with id <id>. Flow-control for new
1130 * streams may be allocated for the peer if needed.
1131 */
1132static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +01001133{
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001134 struct quic_frame *frm;
1135
1136 if (quic_stream_is_bidi(id)) {
1137 ++qcc->lfctl.cl_bidi_r;
1138 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
1139 frm = pool_zalloc(pool_head_quic_frame);
1140 BUG_ON(!frm); /* TODO handle this properly */
1141
1142 LIST_INIT(&frm->reflist);
1143 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
1144 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
1145 qcc->lfctl.cl_bidi_r;
1146 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
1147 tasklet_wakeup(qcc->wait_event.tasklet);
1148
1149 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
1150 qcc->lfctl.cl_bidi_r = 0;
1151 }
1152 }
1153 else {
1154 /* TODO */
1155 }
1156
1157 return 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001158}
1159
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05001160/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001161static void qcs_destroy(struct qcs *qcs)
1162{
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001163 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001164 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001165
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001166 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001167
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001168 if (quic_stream_is_remote(qcs->qcc, id))
1169 qcc_release_remote_stream(qcs->qcc, id);
Amaury Denoyellec055e302022-02-07 16:09:06 +01001170
Amaury Denoyelledccbd732022-03-29 18:36:59 +02001171 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001172
1173 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001174}
1175
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001176/* Transfer as much as possible data on <qcs> from <in> to <out>. This is done
1177 * in respect with available flow-control at stream and connection level.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001178 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001179 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001180 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001181static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, struct buffer *in)
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001182{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001183 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001184 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001185 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001186
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001187 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001188
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001189 qc_get_buf(qcs, out);
1190
1191 /*
1192 * QCS out buffer diagram
1193 * head left to_xfer
1194 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001195 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001196 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001197 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001198 * ^ ack-off ^ sent-off ^ off
1199 *
1200 * STREAM frame
1201 * ^ ^
1202 * |xxxxxxxxxxxxxxxxx|
1203 */
1204
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001205 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001206 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001207 BUG_ON_HOT(qcc->tx.offsets < qcc->tx.sent_offsets);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001208
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001209 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001210 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001211
1212 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
1213 /* do not exceed flow control limit */
1214 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
1215 to_xfer = qcs->tx.msd - qcs->tx.offset;
1216
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001217 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001218 /* do not overcome flow control limit on connection */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001219 if (qcc->tx.offsets + to_xfer > qcc->rfctl.md)
1220 to_xfer = qcc->rfctl.md - qcc->tx.offsets;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001221
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001222 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001223 goto out;
1224
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001225 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001226
1227 out:
1228 {
1229 struct qcs_xfer_data_trace_arg arg = {
1230 .prep = b_data(out), .xfer = total,
1231 };
1232 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
1233 qcc->conn, qcs, &arg);
1234 }
1235
1236 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001237}
1238
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001239/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
1240 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
1241 * stream frame.
1242 *
1243 * Returns the length of the STREAM frame or a negative error code.
1244 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001245static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
1246 struct list *frm_list)
1247{
1248 struct qcc *qcc = qcs->qcc;
1249 struct quic_frame *frm;
1250 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001251 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001252
1253 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1254
Amaury Denoyellea4569202022-04-15 17:29:25 +02001255 /* if ack_offset < buf_offset, it points to an older buffer. */
1256 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
1257 BUG_ON(qcs->tx.sent_offset < base_off);
1258
1259 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001260 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001261 BUG_ON(total < 0);
1262
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001263 if (!total && !fin) {
1264 /* No need to send anything if total is NULL and no FIN to signal. */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001265 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1266 return 0;
1267 }
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001268 BUG_ON((!total && qcs->tx.sent_offset > qcs->tx.offset) ||
1269 (total && qcs->tx.sent_offset >= qcs->tx.offset));
Amaury Denoyellea4569202022-04-15 17:29:25 +02001270 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001271 BUG_ON(qcc->tx.sent_offsets + total > qcc->rfctl.md);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001272
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001273 frm = pool_zalloc(pool_head_quic_frame);
1274 if (!frm)
1275 goto err;
1276
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001277 LIST_INIT(&frm->reflist);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001278 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001279 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001280 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001281 frm->stream.buf = out;
1282 frm->stream.data = (unsigned char *)b_peek(out, head);
1283
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +01001284 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001285 if (fin)
1286 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001287
1288 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001289 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001290 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001291 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001292
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001293 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
1294 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001295
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001296 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001297
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001298 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001299 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001300 struct qcs_build_stream_trace_arg arg = {
1301 .len = frm->stream.len, .fin = fin,
1302 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001303 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001304 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001305 qcc->conn, qcs, &arg);
1306 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001307
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001308 return total;
1309
1310 err:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001311 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001312 return -1;
1313}
1314
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001315/* Check after transferring data from qcs.tx.buf if FIN must be set on the next
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001316 * STREAM frame for <qcs>.
1317 *
1318 * Returns true if FIN must be set else false.
1319 */
1320static int qcs_stream_fin(struct qcs *qcs)
1321{
1322 return qcs->flags & QC_SF_FIN_STREAM && !b_data(&qcs->tx.buf);
1323}
1324
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001325/* This function must be called by the upper layer to inform about the sending
1326 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
1327 * <offset>.
1328 */
1329void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
1330{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001331 struct qcc *qcc = qcs->qcc;
1332 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001333
1334 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001335 BUG_ON(offset + data > qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001336
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001337 /* check if the STREAM frame has already been notified. It can happen
1338 * for retransmission.
1339 */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001340 if (offset + data < qcs->tx.sent_offset)
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001341 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001342
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001343 qcs_idle_open(qcs);
1344
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001345 diff = offset + data - qcs->tx.sent_offset;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001346 if (diff) {
1347 /* increase offset sum on connection */
1348 qcc->tx.sent_offsets += diff;
1349 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
1350 if (qcc->tx.sent_offsets == qcc->rfctl.md)
1351 qcc->flags |= QC_CF_BLK_MFCTL;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001352
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001353 /* increase offset on stream */
1354 qcs->tx.sent_offset += diff;
1355 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
1356 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
1357 if (qcs->tx.sent_offset == qcs->tx.msd)
1358 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001359
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001360 if (qcs->tx.offset == qcs->tx.sent_offset &&
1361 b_full(&qcs->stream->buf->buf)) {
1362 qc_stream_buf_release(qcs->stream);
1363 /* prepare qcs for immediate send retry if data to send */
1364 if (b_data(&qcs->tx.buf))
1365 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
1366 }
1367 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001368
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001369 if (qcs->tx.offset == qcs->tx.sent_offset && qcs_stream_fin(qcs)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001370 /* Close stream locally. */
1371 qcs_close_local(qcs);
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001372 /* Reset flag to not emit multiple FIN STREAM frames. */
1373 qcs->flags &= ~QC_SF_FIN_STREAM;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001374 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001375}
1376
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001377/* Wrapper for send on transport layer. Send a list of frames <frms> for the
1378 * connection <qcc>.
1379 *
1380 * Returns 0 if all data sent with success else non-zero.
1381 */
1382static int qc_send_frames(struct qcc *qcc, struct list *frms)
1383{
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001384 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
1385
1386 if (LIST_ISEMPTY(frms)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001387 TRACE_DEVEL("no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
1388 goto err;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001389 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +01001390
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001391 LIST_INIT(&qcc->send_retry_list);
1392
Amaury Denoyelle56c61542022-08-04 10:11:12 +02001393 qc_send_app_pkts(qcc->conn->handle.qc, 0, frms);
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001394
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001395 /* If there is frames left at this stage, transport layer is blocked.
1396 * Subscribe on it to retry later.
1397 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001398 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001399 TRACE_DEVEL("remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001400 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1401 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001402 goto err;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001403 }
1404
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001405 TRACE_LEAVE(QMUX_EV_QCC_SEND);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001406 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001407
1408 err:
1409 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1410 return 1;
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001411}
1412
1413/* Emit a RESET_STREAM on <qcs>.
1414 *
1415 * Returns 0 if the frame has been successfully sent else non-zero.
1416 */
1417static int qcs_send_reset(struct qcs *qcs)
1418{
1419 struct list frms = LIST_HEAD_INIT(frms);
1420 struct quic_frame *frm;
1421
1422 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1423
1424 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001425 if (!frm) {
1426 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001427 return 1;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001428 }
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001429
1430 LIST_INIT(&frm->reflist);
1431 frm->type = QUIC_FT_RESET_STREAM;
1432 frm->reset_stream.id = qcs->id;
1433 frm->reset_stream.app_error_code = qcs->err;
1434 frm->reset_stream.final_size = qcs->tx.sent_offset;
1435
1436 LIST_APPEND(&frms, &frm->list);
1437 if (qc_send_frames(qcs->qcc, &frms)) {
1438 pool_free(pool_head_quic_frame, frm);
1439 TRACE_DEVEL("cannot send RESET_STREAM", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1440 return 1;
1441 }
1442
1443 if (qcs_sc(qcs)) {
1444 se_fl_set_error(qcs->sd);
1445 qcs_alert(qcs);
1446 }
1447
1448 qcs_close_local(qcs);
1449 qcs->flags &= ~QC_SF_TO_RESET;
1450
1451 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001452 return 0;
1453}
1454
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001455/* Used internally by qc_send function. Proceed to send for <qcs>. This will
1456 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001457 * is then generated and inserted in <frms> list.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001458 *
1459 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
1460 * be null if out buffer cannot be allocated.
1461 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001462static int _qc_send_qcs(struct qcs *qcs, struct list *frms)
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001463{
1464 struct qcc *qcc = qcs->qcc;
1465 struct buffer *buf = &qcs->tx.buf;
1466 struct buffer *out = qc_stream_buf_get(qcs->stream);
1467 int xfer = 0;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001468 char fin = 0;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001469
1470 /* Allocate <out> buffer if necessary. */
1471 if (!out) {
1472 if (qcc->flags & QC_CF_CONN_FULL)
1473 return 0;
1474
1475 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
1476 if (!out) {
1477 qcc->flags |= QC_CF_CONN_FULL;
1478 return 0;
1479 }
1480 }
1481
1482 /* Transfer data from <buf> to <out>. */
1483 if (b_data(buf)) {
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001484 xfer = qcs_xfer_data(qcs, out, buf);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001485 if (xfer > 0) {
1486 qcs_notify_send(qcs);
1487 qcs->flags &= ~QC_SF_BLK_MROOM;
1488 }
1489
1490 qcs->tx.offset += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001491 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001492 qcc->tx.offsets += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001493 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001494 }
1495
1496 /* out buffer cannot be emptied if qcs offsets differ. */
1497 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1498
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001499 /* FIN is set if all incoming data were transferred. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001500 fin = qcs_stream_fin(qcs);
1501
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001502 /* Build a new STREAM frame with <out> buffer. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001503 if (qcs->tx.sent_offset != qcs->tx.offset || fin) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001504 int ret;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001505 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +02001506 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001507 }
1508
1509 return xfer;
1510}
1511
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001512/* Proceed to sending. Loop through all available streams for the <qcc>
1513 * instance and try to send as much as possible.
1514 *
1515 * Returns the total of bytes sent to the transport layer.
1516 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001517static int qc_send(struct qcc *qcc)
1518{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001519 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001520 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001521 struct qcs *qcs, *qcs_tmp;
1522 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001523
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001524 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001525
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001526 if (qcc->conn->flags & CO_FL_SOCK_WR_SH || qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001527 qcc->conn->flags |= CO_FL_ERROR;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001528 TRACE_DEVEL("connection on error", QMUX_EV_QCC_SEND, qcc->conn);
1529 goto err;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001530 }
1531
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001532 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1533 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
1534 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
1535 goto out;
1536 }
1537 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02001538
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001539 if (qcc->flags & QC_CF_BLK_MFCTL)
1540 return 0;
1541
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001542 /* loop through all streams, construct STREAM frames if data available.
1543 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001544 */
1545 node = eb64_first(&qcc->streams_by_id);
1546 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001547 int ret;
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001548 uint64_t id;
1549
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001550 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001551 id = qcs->id;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001552
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001553 if (quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001554 node = eb64_next(node);
1555 continue;
1556 }
1557
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001558 if (qcs->flags & QC_SF_TO_RESET) {
1559 qcs_send_reset(qcs);
1560 node = eb64_next(node);
1561 continue;
1562 }
1563
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001564 if (qcs_is_close_local(qcs)) {
1565 node = eb64_next(node);
1566 continue;
1567 }
1568
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001569 if (qcs->flags & QC_SF_BLK_SFCTL) {
1570 node = eb64_next(node);
1571 continue;
1572 }
1573
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001574 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001575 node = eb64_next(node);
1576 continue;
1577 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001578
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001579 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001580 total += ret;
1581 node = eb64_next(node);
1582 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001583
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001584 if (qc_send_frames(qcc, &frms)) {
1585 /* data rejected by transport layer, do not retry. */
1586 goto out;
1587 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001588
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001589 retry:
1590 tmp_total = 0;
1591 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
1592 int ret;
1593 BUG_ON(!b_data(&qcs->tx.buf));
1594 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001595
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001596 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001597 tmp_total += ret;
1598 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001599 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001600
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001601 total += tmp_total;
1602 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
1603 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +01001604
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001605 out:
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02001606 /* Deallocate frames that the transport layer has rejected. */
1607 if (!LIST_ISEMPTY(&frms)) {
1608 struct quic_frame *frm, *frm2;
1609 list_for_each_entry_safe(frm, frm2, &frms, list) {
1610 LIST_DELETE(&frm->list);
1611 pool_free(pool_head_quic_frame, frm);
1612 }
1613 }
1614
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001615 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001616 return total;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001617
1618 err:
1619 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
1620 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001621}
1622
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001623/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1624 * operation.
1625 *
1626 * Returns 0 on success else non-zero.
1627 */
1628static int qc_recv(struct qcc *qcc)
1629{
1630 struct eb64_node *node;
1631 struct qcs *qcs;
1632
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001633 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001634
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001635 if (qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001636 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001637 return 0;
1638 }
1639
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001640 node = eb64_first(&qcc->streams_by_id);
1641 while (node) {
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001642 uint64_t id;
1643
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001644 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001645 id = qcs->id;
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001646
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001647 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001648 node = eb64_next(node);
1649 continue;
1650 }
1651
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001652 if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001653 node = eb64_next(node);
1654 continue;
1655 }
1656
1657 qcc_decode_qcs(qcc, qcs);
1658 node = eb64_next(node);
Amaury Denoyelle849b24f2022-05-24 17:22:07 +02001659
1660 if (qcs->flags & QC_SF_READ_ABORTED) {
1661 /* TODO should send a STOP_SENDING */
1662 qcs_free(qcs);
1663 }
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001664 }
1665
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001666 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001667 return 0;
1668}
1669
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001670
1671/* Release all streams which have their transfer operation achieved.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001672 *
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001673 * Returns true if at least one stream is released.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001674 */
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001675static int qc_purge_streams(struct qcc *qcc)
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001676{
1677 struct eb64_node *node;
1678 int release = 0;
1679
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001680 TRACE_ENTER(QMUX_EV_QCC_WAKE);
1681
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001682 node = eb64_first(&qcc->streams_by_id);
1683 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001684 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001685 node = eb64_next(node);
1686
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001687 /* Release not attached closed streams. */
1688 if (qcs->st == QC_SS_CLO && !qcs_sc(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001689 TRACE_STATE("purging closed stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001690 qcs_destroy(qcs);
1691 release = 1;
1692 continue;
1693 }
1694
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001695 /* Release detached streams with empty buffer. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001696 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02001697 if (qcs_is_close_local(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001698 TRACE_STATE("purging detached stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001699 qcs_destroy(qcs);
1700 release = 1;
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001701 continue;
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001702 }
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001703
1704 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1705 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001706 }
1707 }
1708
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001709 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001710 return release;
1711}
1712
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001713/* release function. This one should be called to free all resources allocated
1714 * to the mux.
1715 */
1716static void qc_release(struct qcc *qcc)
1717{
1718 struct connection *conn = qcc->conn;
1719 struct eb64_node *node;
1720
1721 TRACE_ENTER(QMUX_EV_QCC_END);
1722
1723 if (qcc->app_ops && qcc->app_ops->release) {
1724 /* Application protocol with dedicated connection closing
1725 * procedure.
1726 */
1727 qcc->app_ops->release(qcc->ctx);
Amaury Denoyellea154dc02022-06-13 17:09:01 +02001728
1729 /* useful if application protocol should emit some closing
1730 * frames. For example HTTP/3 GOAWAY frame.
1731 */
1732 qc_send(qcc);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001733 }
1734 else {
1735 qcc_emit_cc_app(qcc, QC_ERR_NO_ERROR, 0);
1736 }
1737
1738 if (qcc->task) {
1739 task_destroy(qcc->task);
1740 qcc->task = NULL;
1741 }
1742
1743 if (qcc->wait_event.tasklet)
1744 tasklet_free(qcc->wait_event.tasklet);
1745 if (conn && qcc->wait_event.events) {
1746 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
1747 qcc->wait_event.events,
1748 &qcc->wait_event);
1749 }
1750
1751 /* liberate remaining qcs instances */
1752 node = eb64_first(&qcc->streams_by_id);
1753 while (node) {
1754 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
1755 node = eb64_next(node);
1756 qcs_free(qcs);
1757 }
1758
1759 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1760 struct quic_frame *frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list);
1761 LIST_DELETE(&frm->list);
1762 pool_free(pool_head_quic_frame, frm);
1763 }
1764
1765 pool_free(pool_head_qcc, qcc);
1766
1767 if (conn) {
1768 LIST_DEL_INIT(&conn->stopping_list);
1769
1770 conn->handle.qc->conn = NULL;
1771 conn->mux = NULL;
1772 conn->ctx = NULL;
1773
1774 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
1775
1776 conn_stop_tracking(conn);
1777 conn_full_close(conn);
1778 if (conn->destroy_cb)
1779 conn->destroy_cb(conn);
1780 conn_free(conn);
1781 }
1782
1783 TRACE_LEAVE(QMUX_EV_QCC_END);
1784}
1785
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001786static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1787{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001788 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001789
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001790 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001791
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001792 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001793
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001794 if (qc_purge_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001795 if (qcc_is_dead(qcc)) {
1796 qc_release(qcc);
Amaury Denoyelled3973852022-07-25 14:56:54 +02001797 goto end;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001798 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001799 }
1800
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001801 qc_recv(qcc);
1802
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02001803 /* TODO check if qcc proxy is disabled. If yes, use graceful shutdown
1804 * to close the connection.
1805 */
1806
1807 qcc_refresh_timeout(qcc);
1808
Amaury Denoyelled3973852022-07-25 14:56:54 +02001809 end:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001810 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1811
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001812 return NULL;
1813}
1814
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001815static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1816{
1817 struct qcc *qcc = ctx;
1818 int expired = tick_is_expired(t->expire, now_ms);
1819
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001820 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001821
1822 if (qcc) {
1823 if (!expired) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001824 TRACE_DEVEL("not expired", QMUX_EV_QCC_WAKE, qcc->conn);
1825 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001826 }
1827
1828 if (!qcc_may_expire(qcc)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001829 TRACE_DEVEL("cannot expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001830 t->expire = TICK_ETERNITY;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001831 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001832 }
1833 }
1834
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001835 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001836
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001837 if (!qcc) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001838 TRACE_DEVEL("no more qcc", QMUX_EV_QCC_WAKE);
1839 goto out;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001840 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001841
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001842 qcc->task = NULL;
1843
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02001844 /* TODO depending on the timeout condition, different shutdown mode
1845 * should be used. For http keep-alive or disabled proxy, a graceful
1846 * shutdown should occurs. For all other cases, an immediate close
1847 * seems legitimate.
1848 */
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001849 if (qcc_is_dead(qcc))
1850 qc_release(qcc);
1851
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001852 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001853 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001854 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001855
1856 requeue:
1857 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1858 return t;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001859}
1860
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001861static int qc_init(struct connection *conn, struct proxy *prx,
1862 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001863{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001864 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001865 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001866
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001867 qcc = pool_alloc(pool_head_qcc);
1868 if (!qcc)
1869 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001870
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001871 qcc->conn = conn;
1872 conn->ctx = qcc;
Amaury Denoyellec603de42022-07-25 11:21:46 +02001873 qcc->nb_hreq = qcc->nb_sc = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001874 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001875
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001876 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001877
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001878 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001879
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001880 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001881 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001882
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001883 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001884 qcc->tx.sent_offsets = qcc->tx.offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001885
1886 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001887 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001888 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001889 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001890 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001891
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001892 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001893 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001894 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001895 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001896
1897 /* Server initiated streams must respect the server flow control. */
1898 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001899 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001900 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001901 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1902
1903 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001904 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001905 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001906 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001907
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001908 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001909 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02001910 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
1911 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001912 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001913
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001914 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001915 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001916
Willy Tarreau784b8682022-04-11 14:18:10 +02001917 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001918 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001919 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1920 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1921
Amaury Denoyellea509ffb2022-07-04 15:50:33 +02001922 if (conn_is_back(conn)) {
1923 qcc->next_bidi_l = 0x00;
1924 qcc->largest_bidi_r = 0x01;
1925 qcc->next_uni_l = 0x02;
1926 qcc->largest_uni_r = 0x03;
1927 }
1928 else {
1929 qcc->largest_bidi_r = 0x00;
1930 qcc->next_bidi_l = 0x01;
1931 qcc->largest_uni_r = 0x02;
1932 qcc->next_uni_l = 0x03;
1933 }
1934
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001935 qcc->wait_event.tasklet = tasklet_new();
1936 if (!qcc->wait_event.tasklet)
1937 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001938
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001939 LIST_INIT(&qcc->send_retry_list);
1940
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001941 qcc->subs = NULL;
1942 qcc->wait_event.tasklet->process = qc_io_cb;
1943 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001944 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001945
Amaury Denoyelle07bf8f42022-07-22 16:16:03 +02001946 qcc->proxy = prx;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001947 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001948 qcc->task = NULL;
Amaury Denoyelleb6309452022-07-25 14:51:56 +02001949 qcc->timeout = conn_is_back(qcc->conn) ? prx->timeout.server :
1950 prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001951 if (tick_isset(qcc->timeout)) {
1952 qcc->task = task_new_here();
1953 if (!qcc->task)
1954 goto fail_no_timeout_task;
1955 qcc->task->process = qc_timeout_task;
1956 qcc->task->context = qcc;
1957 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1958 }
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +02001959 qcc_reset_idle_start(qcc);
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001960 LIST_INIT(&qcc->opening_list);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001961
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001962 if (!conn_is_back(conn)) {
1963 if (!LIST_INLIST(&conn->stopping_list)) {
1964 LIST_APPEND(&mux_stopping_data[tid].list,
1965 &conn->stopping_list);
1966 }
1967 }
1968
Willy Tarreau784b8682022-04-11 14:18:10 +02001969 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001970 /* init read cycle */
1971 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001972
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001973 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001974
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001975 fail_no_timeout_task:
1976 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001977 fail_no_tasklet:
1978 pool_free(pool_head_qcc, qcc);
1979 fail_no_qcc:
1980 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001981}
1982
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001983static void qc_destroy(void *ctx)
1984{
1985 struct qcc *qcc = ctx;
1986
1987 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
1988 qc_release(qcc);
1989 TRACE_LEAVE(QMUX_EV_QCC_END);
1990}
1991
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001992static void qc_detach(struct sedesc *sd)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001993{
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001994 struct qcs *qcs = sd->se;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001995 struct qcc *qcc = qcs->qcc;
1996
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001997 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001998
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001999 /* TODO this BUG_ON_HOT() is not correct as the stconn layer may detach
2000 * from the stream even if it is not closed remotely at the QUIC layer.
2001 * This happens for example when a stream must be closed due to a
2002 * rejected request. To better handle these cases, it will be required
2003 * to implement shutr/shutw MUX operations. Once this is done, this
2004 * BUG_ON_HOT() statement can be adjusted.
2005 */
2006 //BUG_ON_HOT(!qcs_is_close_remote(qcs));
Amaury Denoyellec603de42022-07-25 11:21:46 +02002007
2008 qcc_rm_sc(qcc);
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002009
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02002010 if (!qcs_is_close_local(qcs) && !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002011 TRACE_STATE("remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002012 qcs->flags |= QC_SF_DETACH;
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002013 qcc_refresh_timeout(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002014
2015 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002016 return;
2017 }
2018
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002019 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01002020
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002021 if (qcc_is_dead(qcc)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002022 TRACE_STATE("killing dead connection", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002023 qc_release(qcc);
2024 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002025 else if (qcc->task) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002026 TRACE_DEVEL("refreshing connection's timeout", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002027 qcc_refresh_timeout(qcc);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002028 }
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002029 else {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002030 TRACE_DEVEL("completed", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002031 }
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002032
2033 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002034}
2035
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002036/* Called from the upper layer, to receive data */
Willy Tarreau3215e732022-05-27 10:09:11 +02002037static size_t qc_rcv_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002038 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002039{
Willy Tarreau3215e732022-05-27 10:09:11 +02002040 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002041 struct htx *qcs_htx = NULL;
2042 struct htx *cs_htx = NULL;
2043 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002044 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002045
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002046 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002047
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002048 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
2049 if (htx_is_empty(qcs_htx)) {
2050 /* Set buffer data to 0 as HTX is empty. */
2051 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
2052 goto end;
2053 }
2054
2055 ret = qcs_htx->data;
2056
2057 cs_htx = htx_from_buf(buf);
2058 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
Amaury Denoyelle72a78e82022-07-29 15:34:12 +02002059 /* EOM will be copied to cs_htx via b_xfer(). */
2060 if (qcs_htx->flags & HTX_FL_EOM)
2061 fin = 1;
2062
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002063 htx_to_buf(cs_htx, buf);
2064 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
2065 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
2066 goto end;
2067 }
2068
2069 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
2070 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
2071
2072 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002073 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
2074 cs_htx->flags |= HTX_FL_EOM;
2075 fin = 1;
2076 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002077
2078 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
2079 htx_to_buf(cs_htx, buf);
2080 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
2081 ret -= qcs_htx->data;
2082
2083 end:
2084 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002085 se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002086 }
2087 else {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002088 se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
2089 if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING))
2090 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002091
Amaury Denoyelle72a78e82022-07-29 15:34:12 +02002092 /* Set end-of-input if FIN received and all data extracted. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002093 if (fin)
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002094 se_fl_set(qcs->sd, SE_FL_EOI);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002095
2096 if (b_size(&qcs->rx.app_buf)) {
2097 b_free(&qcs->rx.app_buf);
2098 offer_buffers(NULL, 1);
2099 }
2100 }
2101
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002102 if (ret) {
2103 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002104 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002105 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002106
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002107 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
2108
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002109 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002110}
2111
Willy Tarreau3215e732022-05-27 10:09:11 +02002112static size_t qc_snd_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002113 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002114{
Willy Tarreau3215e732022-05-27 10:09:11 +02002115 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002116 size_t ret;
2117
2118 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002119
Amaury Denoyelle843a1192022-07-04 11:44:38 +02002120 if (qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_RESET)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002121 ret = count;
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002122 goto end;
2123 }
2124
Willy Tarreau3215e732022-05-27 10:09:11 +02002125 ret = qcs->qcc->app_ops->snd_buf(sc, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002126
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002127 end:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002128 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
2129
2130 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002131}
2132
2133/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2134 * event subscriber <es> is not allowed to change from a previous call as long
2135 * as at least one event is still subscribed. The <event_type> must only be a
2136 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
2137 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002138static int qc_subscribe(struct stconn *sc, int event_type,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002139 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002140{
Willy Tarreau3215e732022-05-27 10:09:11 +02002141 return qcs_subscribe(__sc_mux_strm(sc), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002142}
2143
2144/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
2145 * The <es> pointer is not allowed to differ from the one passed to the
2146 * subscribe() call. It always returns zero.
2147 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002148static int qc_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002149{
Willy Tarreau3215e732022-05-27 10:09:11 +02002150 struct qcs *qcs = __sc_mux_strm(sc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002151
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002152 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
2153 BUG_ON(qcs->subs && qcs->subs != es);
2154
2155 es->events &= ~event_type;
2156 if (!es->events)
2157 qcs->subs = NULL;
2158
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002159 return 0;
2160}
2161
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002162/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Willy Tarreau4596fe22022-05-17 19:07:51 +02002163 * report SE_FL_ERR_PENDING|SE_FL_ERROR on the attached stream connectors and
2164 * wake them.
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002165 */
2166static int qc_wake_some_streams(struct qcc *qcc)
2167{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002168 struct qcs *qcs;
2169 struct eb64_node *node;
2170
2171 for (node = eb64_first(&qcc->streams_by_id); node;
2172 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02002173 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002174
Amaury Denoyelle3abeb572022-07-04 11:42:27 +02002175 if (!qcs_sc(qcs))
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002176 continue;
2177
2178 if (qcc->conn->flags & CO_FL_ERROR) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002179 se_fl_set(qcs->sd, SE_FL_ERR_PENDING);
2180 if (se_fl_test(qcs->sd, SE_FL_EOS))
2181 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002182
Amaury Denoyelle4561f842022-07-06 14:54:34 +02002183 qcs_alert(qcs);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002184 }
2185 }
2186
2187 return 0;
2188}
2189
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002190static int qc_wake(struct connection *conn)
2191{
2192 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02002193 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002194
2195 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002196
2197 /* Check if a soft-stop is in progress.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002198 *
2199 * TODO this is revelant for frontend connections only.
Amaury Denoyelled0c62322022-05-23 08:52:58 +02002200 *
2201 * TODO Client should be notified with a H3 GOAWAY and then a
2202 * CONNECTION_CLOSE. However, quic-conn uses the listener socket for
2203 * sending which at this stage is already closed.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002204 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002205 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Amaury Denoyelled0c62322022-05-23 08:52:58 +02002206 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002207
Willy Tarreau784b8682022-04-11 14:18:10 +02002208 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002209 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
2210
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002211 qc_send(qcc);
2212
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002213 qc_wake_some_streams(qcc);
2214
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002215 if (qcc_is_dead(qcc))
2216 goto release;
2217
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002218 qcc_refresh_timeout(qcc);
2219
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002220 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002221 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002222
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002223 release:
2224 qc_release(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002225 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002226 return 1;
2227}
2228
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002229
2230static char *qcs_st_to_str(enum qcs_state st)
2231{
2232 switch (st) {
2233 case QC_SS_IDLE: return "IDL";
2234 case QC_SS_OPEN: return "OPN";
2235 case QC_SS_HLOC: return "HCL";
2236 case QC_SS_HREM: return "HCR";
2237 case QC_SS_CLO: return "CLO";
2238 default: return "???";
2239 }
2240}
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01002241
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002242static void qmux_trace_frm(const struct quic_frame *frm)
2243{
2244 switch (frm->type) {
2245 case QUIC_FT_MAX_STREAMS_BIDI:
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002246 chunk_appendf(&trace_buf, " max_streams=%llu",
2247 (ull)frm->max_streams_bidi.max_streams);
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002248 break;
2249
2250 case QUIC_FT_MAX_STREAMS_UNI:
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002251 chunk_appendf(&trace_buf, " max_streams=%llu",
2252 (ull)frm->max_streams_uni.max_streams);
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002253 break;
2254
2255 default:
2256 break;
2257 }
2258}
2259
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01002260/* quic-mux trace handler */
2261static void qmux_trace(enum trace_level level, uint64_t mask,
2262 const struct trace_source *src,
2263 const struct ist where, const struct ist func,
2264 const void *a1, const void *a2, const void *a3, const void *a4)
2265{
2266 const struct connection *conn = a1;
2267 const struct qcc *qcc = conn ? conn->ctx : NULL;
2268 const struct qcs *qcs = a2;
2269
2270 if (!qcc)
2271 return;
2272
2273 if (src->verbosity > QMUX_VERB_CLEAN) {
2274 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
2275
2276 if (qcs)
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002277 chunk_appendf(&trace_buf, " qcs=%p .id=%llu .st=%s",
2278 qcs, (ull)qcs->id,
2279 qcs_st_to_str(qcs->st));
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002280
2281 if (mask & QMUX_EV_QCC_NQCS) {
2282 const uint64_t *id = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002283 chunk_appendf(&trace_buf, " id=%llu", (ull)*id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002284 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002285
2286 if (mask & QMUX_EV_SEND_FRM)
2287 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01002288
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02002289 if (mask & QMUX_EV_QCS_XFER_DATA) {
2290 const struct qcs_xfer_data_trace_arg *arg = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002291 chunk_appendf(&trace_buf, " prep=%llu xfer=%d",
2292 (ull)arg->prep, arg->xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02002293 }
2294
2295 if (mask & QMUX_EV_QCS_BUILD_STRM) {
2296 const struct qcs_build_stream_trace_arg *arg = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002297 chunk_appendf(&trace_buf, " len=%llu fin=%d offset=%llu",
2298 (ull)arg->len, arg->fin, (ull)arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01002299 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01002300 }
2301}
2302
2303
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002304static const struct mux_ops qc_ops = {
2305 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002306 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002307 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002308 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002309 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002310 .subscribe = qc_subscribe,
2311 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002312 .wake = qc_wake,
Willy Tarreaub5821e12022-04-26 11:54:08 +02002313 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02002314 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002315};
2316
2317static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002318 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002319
2320INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);