blob: df6e304d43416edf84f827f8a1b0d2b46f8e2c02 [file] [log] [blame]
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001#include <haproxy/mux_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002
Amaury Denoyelleeb01f592021-10-07 16:44:05 +02003#include <import/eb64tree.h>
4
Frédéric Lécailledfbae762021-02-18 09:59:01 +01005#include <haproxy/api.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01006#include <haproxy/connection.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +01007#include <haproxy/dynbuf.h>
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01008#include <haproxy/htx.h>
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02009#include <haproxy/list.h>
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +020010#include <haproxy/ncbuf.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +010011#include <haproxy/pool.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020012#include <haproxy/quic_stream.h>
Frédéric Lécaille748ece62022-05-21 23:58:40 +020013#include <haproxy/quic_tp-t.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020014#include <haproxy/ssl_sock-t.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020015#include <haproxy/stconn.h>
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010016#include <haproxy/trace.h>
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +010017#include <haproxy/xprt_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010018
Amaury Denoyelledeed7772021-12-03 11:36:46 +010019DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010020DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
21
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010022/* trace source and events */
23static void qmux_trace(enum trace_level level, uint64_t mask,
24 const struct trace_source *src,
25 const struct ist where, const struct ist func,
26 const void *a1, const void *a2, const void *a3, const void *a4);
27
28static const struct trace_event qmux_trace_events[] = {
Amaury Denoyelle4f137572022-03-24 17:10:00 +010029#define QMUX_EV_QCC_RECV (1ULL << 1)
30 { .mask = QMUX_EV_QCC_RECV, .name = "qcc_recv", .desc = "Rx on QUIC connection" },
31#define QMUX_EV_QCC_SEND (1ULL << 2)
32 { .mask = QMUX_EV_QCC_SEND, .name = "qcc_send", .desc = "Tx on QUIC connection" },
33#define QMUX_EV_QCC_WAKE (1ULL << 3)
34 { .mask = QMUX_EV_QCC_WAKE, .name = "qcc_wake", .desc = "QUIC connection woken up" },
35#define QMUX_EV_QCC_END (1ULL << 4)
36 { .mask = QMUX_EV_QCC_END, .name = "qcc_end", .desc = "QUIC connection terminated" },
37#define QMUX_EV_QCC_NQCS (1ULL << 5)
38 { .mask = QMUX_EV_QCC_NQCS, .name = "qcc_no_qcs", .desc = "QUIC stream not found" },
39#define QMUX_EV_QCS_NEW (1ULL << 6)
40 { .mask = QMUX_EV_QCS_NEW, .name = "qcs_new", .desc = "new QUIC stream" },
41#define QMUX_EV_QCS_RECV (1ULL << 7)
42 { .mask = QMUX_EV_QCS_RECV, .name = "qcs_recv", .desc = "Rx on QUIC stream" },
43#define QMUX_EV_QCS_SEND (1ULL << 8)
44 { .mask = QMUX_EV_QCS_SEND, .name = "qcs_send", .desc = "Tx on QUIC stream" },
45#define QMUX_EV_QCS_END (1ULL << 9)
46 { .mask = QMUX_EV_QCS_END, .name = "qcs_end", .desc = "QUIC stream terminated" },
47#define QMUX_EV_STRM_RECV (1ULL << 10)
48 { .mask = QMUX_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
49#define QMUX_EV_STRM_SEND (1ULL << 11)
50 { .mask = QMUX_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
51#define QMUX_EV_STRM_END (1ULL << 12)
52 { .mask = QMUX_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
Amaury Denoyellefa29f332022-03-25 09:09:40 +010053#define QMUX_EV_SEND_FRM (1ULL << 13)
54 { .mask = QMUX_EV_SEND_FRM, .name = "send_frm", .desc = "sending QUIC frame" },
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020055/* special event dedicated to qcs_xfer_data */
56#define QMUX_EV_QCS_XFER_DATA (1ULL << 14)
57 { .mask = QMUX_EV_QCS_XFER_DATA, .name = "qcs_xfer_data", .desc = "qcs_xfer_data" },
58/* special event dedicated to qcs_build_stream_frm */
59#define QMUX_EV_QCS_BUILD_STRM (1ULL << 15)
60 { .mask = QMUX_EV_QCS_BUILD_STRM, .name = "qcs_build_stream_frm", .desc = "qcs_build_stream_frm" },
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010061 { }
62};
63
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020064/* custom arg for QMUX_EV_QCS_XFER_DATA */
65struct qcs_xfer_data_trace_arg {
66 size_t prep;
Amaury Denoyellefdcec362022-03-25 09:28:10 +010067 int xfer;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +020068};
69/* custom arg for QMUX_EV_QCS_BUILD_STRM */
70struct qcs_build_stream_trace_arg {
71 size_t len;
Amaury Denoyellefdcec362022-03-25 09:28:10 +010072 char fin;
73 uint64_t offset;
74};
75
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010076static const struct name_desc qmux_trace_lockon_args[4] = {
77 /* arg1 */ { /* already used by the connection */ },
78 /* arg2 */ { .name="qcs", .desc="QUIC stream" },
79 /* arg3 */ { },
80 /* arg4 */ { }
81};
82
83static const struct name_desc qmux_trace_decoding[] = {
84#define QMUX_VERB_CLEAN 1
85 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
86#define QMUX_VERB_MINIMAL 2
87 { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" },
88 { /* end */ }
89};
90
91struct trace_source trace_qmux = {
92 .name = IST("qmux"),
93 .desc = "QUIC multiplexer",
94 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
95 .default_cb = qmux_trace,
96 .known_events = qmux_trace_events,
97 .lockon_args = qmux_trace_lockon_args,
98 .decoding = qmux_trace_decoding,
99 .report_events = ~0, /* report everything by default */
100};
101
102#define TRACE_SOURCE &trace_qmux
103INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
104
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200105/* Emit a CONNECTION_CLOSE with error <err>. This will interrupt all future
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200106 * send/receive operations.
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200107 */
108static void qcc_emit_cc(struct qcc *qcc, int err)
109{
Amaury Denoyelle57e6db72022-07-13 15:07:56 +0200110 quic_set_connection_close(qcc->conn->handle.qc, quic_err_transport(err));
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +0200111 qcc->flags |= QC_CF_CC_EMIT;
112 tasklet_wakeup(qcc->wait_event.tasklet);
113}
114
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100115/* Allocate a new QUIC streams with id <id> and type <type>. */
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200116static struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100117{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100118 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100119
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100120 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
121
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100122 qcs = pool_alloc(pool_head_qcs);
123 if (!qcs)
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200124 return NULL;
125
126 qcs->stream = NULL;
127 qcs->qcc = qcc;
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +0200128 qcs->sd = NULL;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200129 qcs->flags = QC_SF_NONE;
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200130 qcs->st = QC_SS_IDLE;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200131 qcs->ctx = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100132
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200133 /* Allocate transport layer stream descriptor. Only needed for TX. */
134 if (!quic_stream_is_uni(id) || !quic_stream_is_remote(qcc, id)) {
135 struct quic_conn *qc = qcc->conn->handle.qc;
136 qcs->stream = qc_stream_desc_new(id, type, qcs, qc);
137 if (!qcs->stream)
138 goto err;
139 }
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200140
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +0200141 qcs->id = qcs->by_id.key = id;
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200142 if (qcc->app_ops->attach) {
Amaury Denoyellec0156792022-06-03 15:29:07 +0200143 if (qcc->app_ops->attach(qcs, qcc->ctx))
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200144 goto err;
145 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100146
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200147 /* store transport layer stream descriptor in qcc tree */
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200148 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100149
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100150 qcc->strms[type].nb_streams++;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100151
Amaury Denoyelle6ea78192022-03-07 15:47:02 +0100152 /* If stream is local, use peer remote-limit, or else the opposite. */
153 /* TODO use uni limit for unidirectional streams */
154 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
155 qcc->rfctl.msd_bidi_l;
156
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200157 qcs->rx.ncbuf = NCBUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100158 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200159 qcs->rx.offset = qcs->rx.offset_max = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100160
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200161 /* TODO use uni limit for unidirectional streams */
162 qcs->rx.msd = quic_stream_is_local(qcc, id) ? qcc->lfctl.msd_bidi_l :
163 qcc->lfctl.msd_bidi_r;
Amaury Denoyellea9773552022-05-16 14:38:25 +0200164 qcs->rx.msd_init = qcs->rx.msd;
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200165
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100166 qcs->tx.buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100167 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100168 qcs->tx.sent_offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100169
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100170 qcs->wait_event.tasklet = NULL;
171 qcs->wait_event.events = 0;
172 qcs->subs = NULL;
173
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200174 qcs->err = 0;
175
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100176 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100177 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100178 return qcs;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200179
180 err:
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200181 if (qcs->ctx && qcc->app_ops->detach)
182 qcc->app_ops->detach(qcs);
183
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200184 qc_stream_desc_release(qcs->stream);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200185 pool_free(pool_head_qcs, qcs);
186 return NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100187}
188
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200189static void qc_free_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
190{
191 struct buffer buf;
192
Amaury Denoyelle7313f5e2022-05-17 18:53:21 +0200193 if (ncb_is_null(ncbuf))
194 return;
195
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200196 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
197 b_free(&buf);
Amaury Denoyelle7313f5e2022-05-17 18:53:21 +0200198 offer_buffers(NULL, 1);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200199
200 *ncbuf = NCBUF_NULL;
201}
202
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200203/* Free a qcs. This function must only be done to remove a stream on allocation
204 * error or connection shutdown. Else use qcs_destroy which handle all the
205 * QUIC connection mechanism.
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100206 */
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200207static void qcs_free(struct qcs *qcs)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100208{
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200209 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200210 b_free(&qcs->tx.buf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200211
Amaury Denoyelled8e680c2022-03-29 15:18:44 +0200212 BUG_ON(!qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams);
213 --qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams;
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200214
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200215 if (qcs->ctx && qcs->qcc->app_ops->detach)
216 qcs->qcc->app_ops->detach(qcs);
217
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200218 qc_stream_desc_release(qcs->stream);
219
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +0200220 BUG_ON(qcs->sd && !se_fl_test(qcs->sd, SE_FL_ORPHAN));
221 sedesc_free(qcs->sd);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200222
223 eb64_delete(&qcs->by_id);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100224 pool_free(pool_head_qcs, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100225}
226
Amaury Denoyelle3abeb572022-07-04 11:42:27 +0200227static forceinline struct stconn *qcs_sc(const struct qcs *qcs)
228{
229 return qcs->sd ? qcs->sd->sc : NULL;
230}
231
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200232/* Reset the <qcc> inactivity timeout for http-keep-alive timeout. */
233static forceinline void qcc_reset_idle_start(struct qcc *qcc)
234{
235 qcc->idle_start = now_ms;
236}
237
Amaury Denoyellec603de42022-07-25 11:21:46 +0200238/* Decrement <qcc> sc. */
239static forceinline void qcc_rm_sc(struct qcc *qcc)
240{
241 BUG_ON_HOT(!qcc->nb_sc);
242 --qcc->nb_sc;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200243
244 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
245 * refreshed after this on stream detach.
246 */
247 if (!qcc->nb_sc && !qcc->nb_hreq)
248 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200249}
250
251/* Decrement <qcc> hreq. */
252static forceinline void qcc_rm_hreq(struct qcc *qcc)
253{
254 BUG_ON_HOT(!qcc->nb_hreq);
255 --qcc->nb_hreq;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200256
257 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
258 * refreshed after this on I/O handler.
259 */
260 if (!qcc->nb_sc && !qcc->nb_hreq)
261 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200262}
263
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200264static inline int qcc_is_dead(const struct qcc *qcc)
265{
266 /* Mux connection is considered dead if :
267 * - all stream-desc are detached AND
268 * = connection is on error OR
269 * = mux timeout has already fired or is unset
270 */
271 if (!qcc->nb_sc && ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task))
272 return 1;
273
274 return 0;
275}
276
277/* Return true if the mux timeout should be armed. */
278static inline int qcc_may_expire(struct qcc *qcc)
279{
280 return !qcc->nb_sc;
281}
282
283/* Refresh the timeout on <qcc> if needed depending on its state. */
284static void qcc_refresh_timeout(struct qcc *qcc)
285{
286 const struct proxy *px = qcc->proxy;
287
288 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
289
290 if (!qcc->task)
291 goto leave;
292
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200293 /* Check if upper layer is responsible of timeout management. */
294 if (!qcc_may_expire(qcc)) {
295 TRACE_DEVEL("not eligible for timeout", QMUX_EV_QCC_WAKE, qcc->conn);
296 qcc->task->expire = TICK_ETERNITY;
297 task_queue(qcc->task);
298 goto leave;
299 }
300
301 /* TODO if connection is idle on frontend and proxy is disabled, remove
302 * it with global close_spread delay applied.
303 */
304
305 /* TODO implement specific timeouts
306 * - http-requset for waiting on incomplete streams
307 * - client-fin for graceful shutdown
308 */
309
310 /* Frontend timeout management
311 * - detached streams with data left to send -> default timeout
312 * - idle after stream processing -> timeout http-keep-alive
313 */
314 if (!conn_is_back(qcc->conn)) {
315 int timeout;
316
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200317 if (qcc->nb_hreq) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200318 TRACE_DEVEL("one or more requests still in progress", QMUX_EV_QCC_WAKE, qcc->conn);
319 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200320 task_queue(qcc->task);
321 goto leave;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200322 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200323
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200324 /* Use http-request timeout if keep-alive timeout not set */
325 timeout = tick_isset(px->timeout.httpka) ?
326 px->timeout.httpka : px->timeout.httpreq;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200327
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200328 TRACE_DEVEL("at least one request achieved but none currently in progress", QMUX_EV_QCC_WAKE, qcc->conn);
329 qcc->task->expire = tick_add_ifset(qcc->idle_start, timeout);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200330 }
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200331
332 /* fallback to default timeout if frontend specific undefined or for
333 * backend connections.
334 */
335 if (!tick_isset(qcc->task->expire)) {
336 TRACE_DEVEL("fallback to default timeout", QMUX_EV_QCC_WAKE, qcc->conn);
337 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200338 }
339
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200340 task_queue(qcc->task);
341
342 leave:
343 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
344}
345
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200346/* Mark a stream as open if it was idle. This can be used on every
347 * successful emission/reception operation to update the stream state.
348 */
349static void qcs_idle_open(struct qcs *qcs)
350{
351 /* This operation must not be used if the stream is already closed. */
352 BUG_ON_HOT(qcs->st == QC_SS_CLO);
353
354 if (qcs->st == QC_SS_IDLE) {
355 qcs->st = QC_SS_OPEN;
356 TRACE_DEVEL("opening stream", QMUX_EV_QCS_NEW, qcs->qcc->conn, qcs);
357 }
358}
359
360/* Close the local channel of <qcs> instance. */
361static void qcs_close_local(struct qcs *qcs)
362{
363 /* The stream must have already been opened. */
364 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
365
366 /* This operation cannot be used multiple times. */
367 BUG_ON_HOT(qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO);
368
369 if (quic_stream_is_bidi(qcs->id)) {
370 qcs->st = (qcs->st == QC_SS_HREM) ? QC_SS_CLO : QC_SS_HLOC;
Amaury Denoyellec603de42022-07-25 11:21:46 +0200371 qcc_rm_hreq(qcs->qcc);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200372 }
373 else {
374 /* Only local uni streams are valid for this operation. */
375 BUG_ON_HOT(quic_stream_is_remote(qcs->qcc, qcs->id));
376 qcs->st = QC_SS_CLO;
377 }
378
379 TRACE_DEVEL("closing stream locally", QMUX_EV_QCS_END, qcs->qcc->conn, qcs);
380}
381
382/* Close the remote channel of <qcs> instance. */
383static void qcs_close_remote(struct qcs *qcs)
384{
385 /* The stream must have already been opened. */
386 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
387
388 /* This operation cannot be used multiple times. */
389 BUG_ON_HOT(qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO);
390
391 if (quic_stream_is_bidi(qcs->id)) {
392 qcs->st = (qcs->st == QC_SS_HLOC) ? QC_SS_CLO : QC_SS_HREM;
393 }
394 else {
395 /* Only remote uni streams are valid for this operation. */
396 BUG_ON_HOT(quic_stream_is_local(qcs->qcc, qcs->id));
397 qcs->st = QC_SS_CLO;
398 }
399
400 TRACE_DEVEL("closing stream remotely", QMUX_EV_QCS_END, qcs->qcc->conn, qcs);
401}
402
403static int qcs_is_close_local(struct qcs *qcs)
404{
405 return qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO;
406}
407
408static __maybe_unused int qcs_is_close_remote(struct qcs *qcs)
409{
410 return qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO;
411}
412
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100413struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100414{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100415 struct buffer *buf = b_alloc(bptr);
416 BUG_ON(!buf);
417 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100418}
419
Amaury Denoyellea441ec92022-07-04 15:48:57 +0200420static struct ncbuf *qc_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200421{
422 struct buffer buf = BUF_NULL;
423
424 if (ncb_is_null(ncbuf)) {
425 b_alloc(&buf);
426 BUG_ON(b_is_null(&buf));
427
428 *ncbuf = ncb_make(buf.area, buf.size, 0);
429 ncb_init(ncbuf, 0);
430 }
431
432 return ncbuf;
433}
434
Amaury Denoyelle4561f842022-07-06 14:54:34 +0200435/* Notify an eventual subscriber on <qcs> or else wakup up the stconn layer if
436 * initialized.
437 */
438static void qcs_alert(struct qcs *qcs)
439{
440 if (qcs->subs) {
441 qcs_notify_recv(qcs);
442 qcs_notify_send(qcs);
443 }
444 else if (qcs_sc(qcs) && qcs->sd->sc->app_ops->wake) {
445 qcs->sd->sc->app_ops->wake(qcs->sd->sc);
446 }
447}
448
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100449int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
450{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100451 struct qcc *qcc = qcs->qcc;
452
453 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100454
455 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
456 BUG_ON(qcs->subs && qcs->subs != es);
457
458 es->events |= event_type;
459 qcs->subs = es;
460
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100461 if (event_type & SUB_RETRY_RECV)
462 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
463
464 if (event_type & SUB_RETRY_SEND)
465 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
466
467 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
468
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100469 return 0;
470}
471
472void qcs_notify_recv(struct qcs *qcs)
473{
474 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
475 tasklet_wakeup(qcs->subs->tasklet);
476 qcs->subs->events &= ~SUB_RETRY_RECV;
477 if (!qcs->subs->events)
478 qcs->subs = NULL;
479 }
480}
481
482void qcs_notify_send(struct qcs *qcs)
483{
484 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
485 tasklet_wakeup(qcs->subs->tasklet);
486 qcs->subs->events &= ~SUB_RETRY_SEND;
487 if (!qcs->subs->events)
488 qcs->subs = NULL;
489 }
490}
491
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200492/* Open a locally initiated stream for the connection <qcc>. Set <bidi> for a
493 * bidirectional stream, else an unidirectional stream is opened. The next
494 * available ID on the connection will be used according to the stream type.
495 *
496 * Returns the allocated stream instance or NULL on error.
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100497 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200498struct qcs *qcc_init_stream_local(struct qcc *qcc, int bidi)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100499{
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200500 struct qcs *qcs;
501 enum qcs_type type;
502 uint64_t *next;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100503
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200504 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
505
506 if (bidi) {
507 next = &qcc->next_bidi_l;
508 type = conn_is_back(qcc->conn) ? QCS_CLT_BIDI : QCS_SRV_BIDI;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100509 }
510 else {
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200511 next = &qcc->next_uni_l;
512 type = conn_is_back(qcc->conn) ? QCS_CLT_UNI : QCS_SRV_UNI;
513 }
514
515 /* TODO ensure that we won't overflow remote peer flow control limit on
516 * streams. Else, we should emit a STREAMS_BLOCKED frame.
517 */
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100518
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200519 qcs = qcs_new(qcc, *next, type);
520 if (!qcs)
521 return NULL;
Amaury Denoyellec055e302022-02-07 16:09:06 +0100522
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200523 TRACE_DEVEL("opening local stream", QMUX_EV_QCS_NEW, qcc->conn, qcs);
524 *next += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100525
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200526 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
527 return qcs;
528}
529
530/* Open a remote initiated stream for the connection <qcc> with ID <id>. The
531 * caller is responsible to ensure that a stream with the same ID was not
532 * already opened. This function will also create all intermediaries streams
533 * with ID smaller than <id> not already opened before.
534 *
535 * Returns the allocated stream instance or NULL on error.
536 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200537static struct qcs *qcc_init_stream_remote(struct qcc *qcc, uint64_t id)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200538{
539 struct qcs *qcs = NULL;
540 enum qcs_type type;
541 uint64_t *largest;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100542
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200543 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200544
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200545 BUG_ON_HOT(quic_stream_is_local(qcc, id));
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100546
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200547 if (quic_stream_is_bidi(id)) {
548 largest = &qcc->largest_bidi_r;
549 type = conn_is_back(qcc->conn) ? QCS_SRV_BIDI : QCS_CLT_BIDI;
550 }
551 else {
552 largest = &qcc->largest_uni_r;
553 type = conn_is_back(qcc->conn) ? QCS_SRV_UNI : QCS_CLT_UNI;
554 }
555
556 /* TODO also checks max-streams for uni streams */
557 if (quic_stream_is_bidi(id)) {
558 if (id >= qcc->lfctl.ms_bidi * 4) {
559 /* RFC 9000 4.6. Controlling Concurrency
560 *
561 * An endpoint that receives a frame with a
562 * stream ID exceeding the limit it has sent
563 * MUST treat this as a connection error of
564 * type STREAM_LIMIT_ERROR
565 */
566 TRACE_DEVEL("leaving on flow control error", QMUX_EV_QCS_NEW, qcc->conn);
567 qcc_emit_cc(qcc, QC_ERR_STREAM_LIMIT_ERROR);
568 return NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100569 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200570 }
571
572 /* Only stream ID not already opened can be used. */
573 BUG_ON(id < *largest);
574
575 while (id >= *largest) {
576 const char *str = *largest < id ? "opening intermediary stream" :
577 "opening remote stream";
578
579 qcs = qcs_new(qcc, *largest, type);
580 if (!qcs) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200581 /* TODO emit RESET_STREAM */
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200582 TRACE_DEVEL("leaving on stream fallocation failure", QMUX_EV_QCS_NEW, qcc->conn);
583 return NULL;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100584 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200585
586 TRACE_DEVEL(str, QMUX_EV_QCS_NEW, qcc->conn, qcs);
587 *largest += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100588 }
589
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200590 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle50742292022-03-29 14:57:19 +0200591 return qcs;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200592}
593
594/* Use this function for a stream <id> which is not in <qcc> stream tree. It
595 * returns true if the associated stream is closed.
596 */
597static int qcc_stream_id_is_closed(struct qcc *qcc, uint64_t id)
598{
599 uint64_t *largest;
600
601 /* This function must only be used for stream not present in the stream tree. */
602 BUG_ON_HOT(eb64_lookup(&qcc->streams_by_id, id));
603
604 if (quic_stream_is_local(qcc, id)) {
605 largest = quic_stream_is_uni(id) ? &qcc->next_uni_l :
606 &qcc->next_bidi_l;
607 }
608 else {
609 largest = quic_stream_is_uni(id) ? &qcc->largest_uni_r :
610 &qcc->largest_bidi_r;
611 }
612
613 return id < *largest;
614}
615
616/* Retrieve the stream instance from <id> ID. This can be used when receiving
617 * STREAM, STREAM_DATA_BLOCKED, RESET_STREAM, MAX_STREAM_DATA or STOP_SENDING
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200618 * frames. Set to false <receive_only> or <send_only> if these particular types
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200619 * of streams are not allowed. If the stream instance is found, it is stored in
620 * <out>.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200621 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200622 * Returns 0 on success else non-zero. On error, a RESET_STREAM or a
623 * CONNECTION_CLOSE is automatically emitted. Beware that <out> may be NULL
624 * on success if the stream has already been closed.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200625 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200626int qcc_get_qcs(struct qcc *qcc, uint64_t id, int receive_only, int send_only,
627 struct qcs **out)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200628{
629 struct eb64_node *node;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200630
631 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200632 *out = NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200633
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200634 if (!receive_only && quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
635 TRACE_DEVEL("leaving on receive-only stream not allowed", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
636 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200637 return 1;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200638 }
639
640 if (!send_only && quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
641 TRACE_DEVEL("leaving on send-only stream not allowed", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
642 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200643 return 1;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200644 }
645
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200646 /* Search the stream in the connection tree. */
647 node = eb64_lookup(&qcc->streams_by_id, id);
648 if (node) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200649 *out = eb64_entry(node, struct qcs, by_id);
650 TRACE_DEVEL("using stream from connection tree", QMUX_EV_QCC_RECV, qcc->conn, *out);
651 return 0;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200652 }
653
654 /* Check if stream is already closed. */
655 if (qcc_stream_id_is_closed(qcc, id)) {
656 TRACE_DEVEL("already released stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200657 /* Consider this as a success even if <out> is left NULL. */
658 return 0;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200659 }
660
661 /* Create the stream. This is valid only for remote initiated one. A
662 * local stream must have already been explicitely created by the
663 * application protocol layer.
664 */
665 if (quic_stream_is_local(qcc, id)) {
666 /* RFC 9000 19.8. STREAM Frames
667 *
668 * An endpoint MUST terminate the connection with error
669 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
670 * initiated stream that has not yet been created, or for a send-only
671 * stream.
672 */
673 TRACE_DEVEL("leaving on locally initiated stream not yet created", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
674 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200675 return 1;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200676 }
677 else {
678 /* Remote stream not found - try to open it. */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200679 *out = qcc_init_stream_remote(qcc, id);
680 if (!*out) {
681 TRACE_DEVEL("leaving on stream creation error", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
682 return 1;
683 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200684 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100685
686 out:
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200687 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200688 return 0;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100689}
690
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200691/* Simple function to duplicate a buffer */
692static inline struct buffer qcs_b_dup(const struct ncbuf *b)
693{
694 return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0));
695}
696
Amaury Denoyelle36d4b5e2022-07-01 11:25:40 +0200697/* Remove <bytes> from <qcs> Rx buffer. Flow-control for received offsets may
698 * be allocated for the peer if needed.
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200699 */
700static void qcs_consume(struct qcs *qcs, uint64_t bytes)
701{
702 struct qcc *qcc = qcs->qcc;
703 struct quic_frame *frm;
704 struct ncbuf *buf = &qcs->rx.ncbuf;
705 enum ncb_ret ret;
706
707 ret = ncb_advance(buf, bytes);
708 if (ret) {
709 ABORT_NOW(); /* should not happens because removal only in data */
710 }
711
712 if (ncb_is_empty(buf))
713 qc_free_ncbuf(qcs, buf);
714
715 qcs->rx.offset += bytes;
716 if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) {
717 frm = pool_zalloc(pool_head_quic_frame);
718 BUG_ON(!frm); /* TODO handle this properly */
719
720 qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init;
721
722 LIST_INIT(&frm->reflist);
723 frm->type = QUIC_FT_MAX_STREAM_DATA;
724 frm->max_stream_data.id = qcs->id;
725 frm->max_stream_data.max_stream_data = qcs->rx.msd;
726
727 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
728 tasklet_wakeup(qcc->wait_event.tasklet);
729 }
730
731 qcc->lfctl.offsets_consume += bytes;
732 if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) {
733 frm = pool_zalloc(pool_head_quic_frame);
734 BUG_ON(!frm); /* TODO handle this properly */
735
736 qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init;
737
738 LIST_INIT(&frm->reflist);
739 frm->type = QUIC_FT_MAX_DATA;
740 frm->max_data.max_data = qcc->lfctl.md;
741
742 LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list);
743 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
744 }
745}
746
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200747/* Decode the content of STREAM frames already received on the stream instance
748 * <qcs>.
749 *
750 * Returns 0 on success else non-zero.
751 */
752static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
753{
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200754 struct buffer b;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200755 ssize_t ret;
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200756 int fin = 0;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200757
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200758 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
759
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200760 b = qcs_b_dup(&qcs->rx.ncbuf);
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200761
762 /* Signal FIN to application if STREAM FIN received and there is no gap
763 * in the Rx buffer.
764 */
Amaury Denoyelle3f39b402022-07-01 16:11:03 +0200765 if (qcs->flags & QC_SF_SIZE_KNOWN && !ncb_is_fragmented(&qcs->rx.ncbuf))
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200766 fin = 1;
767
768 ret = qcc->app_ops->decode_qcs(qcs, &b, fin);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200769 if (ret < 0) {
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200770 TRACE_DEVEL("leaving on decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
771 return 1;
772 }
773
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200774 if (ret) {
775 qcs_consume(qcs, ret);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200776 qcs_notify_recv(qcs);
777 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200778
779 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
780
781 return 0;
782}
783
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200784/* Emit a CONNECTION_CLOSE_APP with error <err>. Reserved for application error
Amaury Denoyelled666d742022-07-13 15:15:58 +0200785 * code. To close the connection right away, set <immediate> : this is useful
786 * when dealing with a connection fatal error. Else a graceful shutdown will be
787 * conducted : the error-code is only registered. The lower layer is
788 * responsible to close the connection when deemed suitable. Note that in this
789 * case the error code might be overwritten if an immediate close is requested
790 * in the interval.
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200791 */
Amaury Denoyelled666d742022-07-13 15:15:58 +0200792void qcc_emit_cc_app(struct qcc *qcc, int err, int immediate)
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200793{
Amaury Denoyelled666d742022-07-13 15:15:58 +0200794 if (immediate) {
795 quic_set_connection_close(qcc->conn->handle.qc, quic_err_app(err));
796 qcc->flags |= QC_CF_CC_EMIT;
797 tasklet_wakeup(qcc->wait_event.tasklet);
798 }
799 else {
800 /* Only register the error code for graceful shutdown. */
801 qcc->conn->handle.qc->err = quic_err_app(err);
802 }
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200803}
804
805/* Prepare for the emission of RESET_STREAM on <qcs> with error code <err>. */
806void qcc_reset_stream(struct qcs *qcs, int err)
807{
808 struct qcc *qcc = qcs->qcc;
809
810 if ((qcs->flags & QC_SF_TO_RESET) || qcs_is_close_local(qcs))
811 return;
812
813 qcs->flags |= QC_SF_TO_RESET;
814 qcs->err = err;
815 tasklet_wakeup(qcc->wait_event.tasklet);
816 TRACE_DEVEL("reset stream", QMUX_EV_QCS_END, qcc->conn, qcs);
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200817}
818
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200819/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
820 * <data> with length <len> and represents the offset <offset>. <fin> is set if
821 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100822 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200823 * Returns 0 on success else non-zero. On error, the received frame should not
824 * be acknowledged.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100825 */
826int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200827 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100828{
829 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200830 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100831
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100832 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
833
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200834 if (qcc->flags & QC_CF_CC_EMIT) {
835 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200836 return 1;
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200837 }
838
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200839 /* RFC 9000 19.8. STREAM Frames
840 *
841 * An endpoint MUST terminate the connection with error
842 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
843 * initiated stream that has not yet been created, or for a send-only
844 * stream.
845 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200846 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
847 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
848 return 1;
849 }
850
851 if (!qcs) {
852 /* Already closed stream. */
853 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200854 return 0;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200855 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100856
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200857 /* RFC 9000 4.5. Stream Final Size
858 *
859 * Once a final size for a stream is known, it cannot change. If a
860 * RESET_STREAM or STREAM frame is received indicating a change in the
861 * final size for the stream, an endpoint SHOULD respond with an error
862 * of type FINAL_SIZE_ERROR; see Section 11 for details on error
863 * handling.
864 */
865 if (qcs->flags & QC_SF_SIZE_KNOWN &&
866 (offset + len > qcs->rx.offset_max || (fin && offset + len < qcs->rx.offset_max))) {
867 TRACE_DEVEL("leaving on final size error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
868 qcc_emit_cc(qcc, QC_ERR_FINAL_SIZE_ERROR);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200869 return 1;
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200870 }
871
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100872 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100873 TRACE_DEVEL("leaving on already received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle74cf2372022-04-29 15:58:22 +0200874 return 0;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100875 }
876
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200877 qcs_idle_open(qcs);
878
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200879 if (offset + len > qcs->rx.offset_max) {
880 uint64_t diff = offset + len - qcs->rx.offset_max;
881 qcs->rx.offset_max = offset + len;
882 qcc->lfctl.offsets_recv += diff;
883
884 if (offset + len > qcs->rx.msd ||
885 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
886 /* RFC 9000 4.1. Data Flow Control
887 *
888 * A receiver MUST close the connection with an error
889 * of type FLOW_CONTROL_ERROR if the sender violates
890 * the advertised connection or stream data limits
891 */
892 TRACE_DEVEL("leaving on flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
893 qcc->conn, qcs);
894 qcc_emit_cc(qcc, QC_ERR_FLOW_CONTROL_ERROR);
895 return 1;
896 }
897 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100898
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200899 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100900 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200901 ABORT_NOW();
902 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100903 }
904
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100905 TRACE_DEVEL("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200906 if (offset < qcs->rx.offset) {
Frédéric Lécaillea18c3332022-07-04 09:54:58 +0200907 size_t diff = qcs->rx.offset - offset;
908
909 len -= diff;
910 data += diff;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200911 offset = qcs->rx.offset;
912 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100913
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200914 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
915 if (ret != NCB_RET_OK) {
916 if (ret == NCB_RET_DATA_REJ) {
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200917 /* RFC 9000 2.2. Sending and Receiving Data
918 *
919 * An endpoint could receive data for a stream at the
920 * same stream offset multiple times. Data that has
921 * already been received can be discarded. The data at
922 * a given offset MUST NOT change if it is sent
923 * multiple times; an endpoint MAY treat receipt of
924 * different data at the same offset within a stream as
925 * a connection error of type PROTOCOL_VIOLATION.
926 */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200927 TRACE_DEVEL("leaving on data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
928 qcc->conn, qcs);
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200929 qcc_emit_cc(qcc, QC_ERR_PROTOCOL_VIOLATION);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200930 }
931 else if (ret == NCB_RET_GAP_SIZE) {
932 TRACE_DEVEL("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
933 qcc->conn, qcs);
934 }
935 return 1;
936 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100937
938 if (fin)
Amaury Denoyelle3f39b402022-07-01 16:11:03 +0200939 qcs->flags |= QC_SF_SIZE_KNOWN;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100940
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200941 if (qcs->flags & QC_SF_SIZE_KNOWN && !ncb_is_fragmented(&qcs->rx.ncbuf))
942 qcs_close_remote(qcs);
943
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200944 if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200945 qcc_decode_qcs(qcc, qcs);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200946 qcc_refresh_timeout(qcc);
947 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200948
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200949 if (qcs->flags & QC_SF_READ_ABORTED) {
950 /* TODO should send a STOP_SENDING */
951 qcs_free(qcs);
952 }
953
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100954 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100955 return 0;
956}
957
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100958/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
959 * the frame.
960 *
961 * Returns 0 on success else non-zero.
962 */
963int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
964{
Amaury Denoyelle392e94e2022-07-06 15:44:16 +0200965 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
966
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100967 if (qcc->rfctl.md < max) {
968 qcc->rfctl.md = max;
Amaury Denoyelle392e94e2022-07-06 15:44:16 +0200969 TRACE_DEVEL("increase remote max-data", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100970
971 if (qcc->flags & QC_CF_BLK_MFCTL) {
972 qcc->flags &= ~QC_CF_BLK_MFCTL;
973 tasklet_wakeup(qcc->wait_event.tasklet);
974 }
975 }
Amaury Denoyelle392e94e2022-07-06 15:44:16 +0200976
977 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100978 return 0;
979}
980
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100981/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
982 * field of the frame and <id> is the identifier of the QUIC stream.
983 *
Amaury Denoyelleb68559a2022-07-06 15:45:20 +0200984 * Returns 0 on success else non-zero. On error, the received frame should not
985 * be acknowledged.
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100986 */
987int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
988{
989 struct qcs *qcs;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100990
Amaury Denoyelle392e94e2022-07-06 15:44:16 +0200991 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
992
Amaury Denoyelleb68559a2022-07-06 15:45:20 +0200993 /* RFC 9000 19.10. MAX_STREAM_DATA Frames
994 *
995 * Receiving a MAX_STREAM_DATA frame for a locally
996 * initiated stream that has not yet been created MUST be treated as a
997 * connection error of type STREAM_STATE_ERROR. An endpoint that
998 * receives a MAX_STREAM_DATA frame for a receive-only stream MUST
999 * terminate the connection with error STREAM_STATE_ERROR.
1000 */
1001 if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) {
1002 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1003 return 1;
1004 }
1005
1006 if (qcs) {
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001007 if (max > qcs->tx.msd) {
1008 qcs->tx.msd = max;
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001009 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 +01001010
1011 if (qcs->flags & QC_SF_BLK_SFCTL) {
1012 qcs->flags &= ~QC_SF_BLK_SFCTL;
1013 tasklet_wakeup(qcc->wait_event.tasklet);
1014 }
1015 }
1016 }
1017
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001018 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001019 return 0;
1020}
1021
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001022/* Handle a new STOP_SENDING frame for stream ID <id>. The error code should be
1023 * specified in <err>.
1024 *
1025 * Returns 0 on success else non-zero. On error, the received frame should not
1026 * be acknowledged.
1027 */
1028int qcc_recv_stop_sending(struct qcc *qcc, uint64_t id, uint64_t err)
1029{
1030 struct qcs *qcs;
1031
1032 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1033
1034 /* RFC 9000 19.5. STOP_SENDING Frames
1035 *
1036 * Receiving a STOP_SENDING frame for a
1037 * locally initiated stream that has not yet been created MUST be
1038 * treated as a connection error of type STREAM_STATE_ERROR. An
1039 * endpoint that receives a STOP_SENDING frame for a receive-only stream
1040 * MUST terminate the connection with error STREAM_STATE_ERROR.
1041 */
1042 if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) {
1043 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1044 return 1;
1045 }
1046
1047 if (!qcs)
1048 goto out;
1049
1050 /* RFC 9000 3.5. Solicited State Transitions
1051 *
1052 * An endpoint that receives a STOP_SENDING frame
1053 * MUST send a RESET_STREAM frame if the stream is in the "Ready" or
1054 * "Send" state. If the stream is in the "Data Sent" state, the
1055 * endpoint MAY defer sending the RESET_STREAM frame until the packets
1056 * containing outstanding data are acknowledged or declared lost. If
1057 * any outstanding data is declared lost, the endpoint SHOULD send a
1058 * RESET_STREAM frame instead of retransmitting the data.
1059 *
1060 * An endpoint SHOULD copy the error code from the STOP_SENDING frame to
1061 * the RESET_STREAM frame it sends, but it can use any application error
1062 * code.
1063 */
1064 TRACE_DEVEL("receiving STOP_SENDING on stream", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
1065 qcc_reset_stream(qcs, err);
1066
1067 out:
1068 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1069 return 0;
1070}
1071
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001072/* Signal the closing of remote stream with id <id>. Flow-control for new
1073 * streams may be allocated for the peer if needed.
1074 */
1075static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +01001076{
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001077 struct quic_frame *frm;
1078
1079 if (quic_stream_is_bidi(id)) {
1080 ++qcc->lfctl.cl_bidi_r;
1081 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
1082 frm = pool_zalloc(pool_head_quic_frame);
1083 BUG_ON(!frm); /* TODO handle this properly */
1084
1085 LIST_INIT(&frm->reflist);
1086 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
1087 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
1088 qcc->lfctl.cl_bidi_r;
1089 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
1090 tasklet_wakeup(qcc->wait_event.tasklet);
1091
1092 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
1093 qcc->lfctl.cl_bidi_r = 0;
1094 }
1095 }
1096 else {
1097 /* TODO */
1098 }
1099
1100 return 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001101}
1102
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05001103/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001104static void qcs_destroy(struct qcs *qcs)
1105{
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001106 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001107 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001108
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001109 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001110
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001111 if (quic_stream_is_remote(qcs->qcc, id))
1112 qcc_release_remote_stream(qcs->qcc, id);
Amaury Denoyellec055e302022-02-07 16:09:06 +01001113
Amaury Denoyelledccbd732022-03-29 18:36:59 +02001114 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001115
1116 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001117}
1118
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001119/* Transfer as much as possible data on <qcs> from <in> to <out>. This is done
1120 * in respect with available flow-control at stream and connection level.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001121 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001122 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001123 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001124static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, struct buffer *in)
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001125{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001126 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001127 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001128 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001129
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001130 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001131
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001132 qc_get_buf(qcs, out);
1133
1134 /*
1135 * QCS out buffer diagram
1136 * head left to_xfer
1137 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001138 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001139 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001140 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001141 * ^ ack-off ^ sent-off ^ off
1142 *
1143 * STREAM frame
1144 * ^ ^
1145 * |xxxxxxxxxxxxxxxxx|
1146 */
1147
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001148 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001149 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001150 BUG_ON_HOT(qcc->tx.offsets < qcc->tx.sent_offsets);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001151
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001152 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001153 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001154
1155 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
1156 /* do not exceed flow control limit */
1157 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
1158 to_xfer = qcs->tx.msd - qcs->tx.offset;
1159
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001160 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001161 /* do not overcome flow control limit on connection */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001162 if (qcc->tx.offsets + to_xfer > qcc->rfctl.md)
1163 to_xfer = qcc->rfctl.md - qcc->tx.offsets;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001164
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001165 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001166 goto out;
1167
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001168 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001169
1170 out:
1171 {
1172 struct qcs_xfer_data_trace_arg arg = {
1173 .prep = b_data(out), .xfer = total,
1174 };
1175 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
1176 qcc->conn, qcs, &arg);
1177 }
1178
1179 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001180}
1181
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001182/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
1183 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
1184 * stream frame.
1185 *
1186 * Returns the length of the STREAM frame or a negative error code.
1187 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001188static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
1189 struct list *frm_list)
1190{
1191 struct qcc *qcc = qcs->qcc;
1192 struct quic_frame *frm;
1193 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001194 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001195
1196 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1197
Amaury Denoyellea4569202022-04-15 17:29:25 +02001198 /* if ack_offset < buf_offset, it points to an older buffer. */
1199 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
1200 BUG_ON(qcs->tx.sent_offset < base_off);
1201
1202 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001203 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001204 BUG_ON(total < 0);
1205
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001206 if (!total && !fin) {
1207 /* No need to send anything if total is NULL and no FIN to signal. */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001208 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1209 return 0;
1210 }
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001211 BUG_ON((!total && qcs->tx.sent_offset > qcs->tx.offset) ||
1212 (total && qcs->tx.sent_offset >= qcs->tx.offset));
Amaury Denoyellea4569202022-04-15 17:29:25 +02001213 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001214 BUG_ON(qcc->tx.sent_offsets + total > qcc->rfctl.md);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001215
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001216 frm = pool_zalloc(pool_head_quic_frame);
1217 if (!frm)
1218 goto err;
1219
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001220 LIST_INIT(&frm->reflist);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001221 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001222 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001223 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001224 frm->stream.buf = out;
1225 frm->stream.data = (unsigned char *)b_peek(out, head);
1226
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +01001227 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001228 if (fin)
1229 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001230
1231 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001232 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001233 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001234 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001235
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001236 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
1237 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001238
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001239 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001240
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001241 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001242 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001243 struct qcs_build_stream_trace_arg arg = {
1244 .len = frm->stream.len, .fin = fin,
1245 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001246 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001247 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001248 qcc->conn, qcs, &arg);
1249 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001250
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001251 return total;
1252
1253 err:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001254 TRACE_DEVEL("leaving in error", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001255 return -1;
1256}
1257
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001258/* Check after transfering data from qcs.tx.buf if FIN must be set on the next
1259 * STREAM frame for <qcs>.
1260 *
1261 * Returns true if FIN must be set else false.
1262 */
1263static int qcs_stream_fin(struct qcs *qcs)
1264{
1265 return qcs->flags & QC_SF_FIN_STREAM && !b_data(&qcs->tx.buf);
1266}
1267
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001268/* This function must be called by the upper layer to inform about the sending
1269 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
1270 * <offset>.
1271 */
1272void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
1273{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001274 struct qcc *qcc = qcs->qcc;
1275 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001276
1277 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001278 BUG_ON(offset + data > qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001279
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001280 /* check if the STREAM frame has already been notified. It can happen
1281 * for retransmission.
1282 */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001283 if (offset + data < qcs->tx.sent_offset)
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001284 return;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001285
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001286 qcs_idle_open(qcs);
1287
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001288 diff = offset + data - qcs->tx.sent_offset;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001289 if (diff) {
1290 /* increase offset sum on connection */
1291 qcc->tx.sent_offsets += diff;
1292 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
1293 if (qcc->tx.sent_offsets == qcc->rfctl.md)
1294 qcc->flags |= QC_CF_BLK_MFCTL;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001295
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001296 /* increase offset on stream */
1297 qcs->tx.sent_offset += diff;
1298 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
1299 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
1300 if (qcs->tx.sent_offset == qcs->tx.msd)
1301 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001302
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001303 if (qcs->tx.offset == qcs->tx.sent_offset &&
1304 b_full(&qcs->stream->buf->buf)) {
1305 qc_stream_buf_release(qcs->stream);
1306 /* prepare qcs for immediate send retry if data to send */
1307 if (b_data(&qcs->tx.buf))
1308 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
1309 }
1310 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001311
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001312 if (qcs->tx.offset == qcs->tx.sent_offset && qcs_stream_fin(qcs)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001313 /* Close stream locally. */
1314 qcs_close_local(qcs);
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001315 /* Reset flag to not emit multiple FIN STREAM frames. */
1316 qcs->flags &= ~QC_SF_FIN_STREAM;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001317 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001318}
1319
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001320/* Wrapper for send on transport layer. Send a list of frames <frms> for the
1321 * connection <qcc>.
1322 *
1323 * Returns 0 if all data sent with success else non-zero.
1324 */
1325static int qc_send_frames(struct qcc *qcc, struct list *frms)
1326{
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001327 /* TODO implement an opportunistic retry mechanism. This is needed
1328 * because qc_send_app_pkts is not completed. It will only prepare data
1329 * up to its Tx buffer. The frames left are not send even if the Tx
1330 * buffer is emptied by the sendto call.
1331 *
1332 * To overcome this, we call repeatedly qc_send_app_pkts until we
1333 * detect that the transport layer has send nothing. This could happen
1334 * on congestion or sendto syscall error.
1335 *
1336 * When qc_send_app_pkts is improved to handle retry by itself, we can
1337 * remove the looping from the MUX.
1338 */
1339 struct quic_frame *first_frm;
1340 uint64_t first_offset = 0;
1341 char first_stream_frame_type;
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001342
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001343 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
1344
1345 if (LIST_ISEMPTY(frms)) {
1346 TRACE_DEVEL("leaving with no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001347 return 1;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001348 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +01001349
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001350 LIST_INIT(&qcc->send_retry_list);
1351
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001352 retry_send:
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001353 first_frm = LIST_ELEM(frms->n, struct quic_frame *, list);
1354 if ((first_frm->type & QUIC_FT_STREAM_8) == QUIC_FT_STREAM_8) {
1355 first_offset = first_frm->stream.offset.key;
1356 first_stream_frame_type = 1;
1357 }
1358 else {
1359 first_stream_frame_type = 0;
1360 }
1361
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001362 if (!LIST_ISEMPTY(frms))
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02001363 qc_send_app_pkts(qcc->conn->handle.qc, 0, frms);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001364
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001365 /* If there is frames left, check if the transport layer has send some
1366 * data or is blocked.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001367 */
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001368 if (!LIST_ISEMPTY(frms)) {
1369 if (first_frm != LIST_ELEM(frms->n, struct quic_frame *, list))
1370 goto retry_send;
1371
1372 /* If the first frame is STREAM, check if its offset has
1373 * changed.
1374 */
1375 if (first_stream_frame_type &&
1376 first_offset != LIST_ELEM(frms->n, struct quic_frame *, list)->stream.offset.key) {
1377 goto retry_send;
1378 }
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001379 }
1380
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001381 /* If there is frames left at this stage, transport layer is blocked.
1382 * Subscribe on it to retry later.
1383 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001384 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001385 TRACE_DEVEL("leaving with remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001386 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1387 SUB_RETRY_SEND, &qcc->wait_event);
1388 return 1;
1389 }
1390
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001391 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1392
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001393 return 0;
1394}
1395
1396/* Emit a RESET_STREAM on <qcs>.
1397 *
1398 * Returns 0 if the frame has been successfully sent else non-zero.
1399 */
1400static int qcs_send_reset(struct qcs *qcs)
1401{
1402 struct list frms = LIST_HEAD_INIT(frms);
1403 struct quic_frame *frm;
1404
1405 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1406
1407 frm = pool_zalloc(pool_head_quic_frame);
1408 if (!frm)
1409 return 1;
1410
1411 LIST_INIT(&frm->reflist);
1412 frm->type = QUIC_FT_RESET_STREAM;
1413 frm->reset_stream.id = qcs->id;
1414 frm->reset_stream.app_error_code = qcs->err;
1415 frm->reset_stream.final_size = qcs->tx.sent_offset;
1416
1417 LIST_APPEND(&frms, &frm->list);
1418 if (qc_send_frames(qcs->qcc, &frms)) {
1419 pool_free(pool_head_quic_frame, frm);
1420 TRACE_DEVEL("cannot send RESET_STREAM", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1421 return 1;
1422 }
1423
1424 if (qcs_sc(qcs)) {
1425 se_fl_set_error(qcs->sd);
1426 qcs_alert(qcs);
1427 }
1428
1429 qcs_close_local(qcs);
1430 qcs->flags &= ~QC_SF_TO_RESET;
1431
1432 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001433 return 0;
1434}
1435
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001436/* Used internally by qc_send function. Proceed to send for <qcs>. This will
1437 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001438 * is then generated and inserted in <frms> list.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001439 *
1440 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
1441 * be null if out buffer cannot be allocated.
1442 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001443static int _qc_send_qcs(struct qcs *qcs, struct list *frms)
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001444{
1445 struct qcc *qcc = qcs->qcc;
1446 struct buffer *buf = &qcs->tx.buf;
1447 struct buffer *out = qc_stream_buf_get(qcs->stream);
1448 int xfer = 0;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001449 char fin = 0;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001450
1451 /* Allocate <out> buffer if necessary. */
1452 if (!out) {
1453 if (qcc->flags & QC_CF_CONN_FULL)
1454 return 0;
1455
1456 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
1457 if (!out) {
1458 qcc->flags |= QC_CF_CONN_FULL;
1459 return 0;
1460 }
1461 }
1462
1463 /* Transfer data from <buf> to <out>. */
1464 if (b_data(buf)) {
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001465 xfer = qcs_xfer_data(qcs, out, buf);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001466 if (xfer > 0) {
1467 qcs_notify_send(qcs);
1468 qcs->flags &= ~QC_SF_BLK_MROOM;
1469 }
1470
1471 qcs->tx.offset += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001472 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001473 qcc->tx.offsets += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001474 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001475 }
1476
1477 /* out buffer cannot be emptied if qcs offsets differ. */
1478 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1479
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001480 /* FIN is set if all incoming data were transfered. */
1481 fin = qcs_stream_fin(qcs);
1482
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001483 /* Build a new STREAM frame with <out> buffer. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001484 if (qcs->tx.sent_offset != qcs->tx.offset || fin) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001485 int ret;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001486 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +02001487 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001488 }
1489
1490 return xfer;
1491}
1492
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001493/* Proceed to sending. Loop through all available streams for the <qcc>
1494 * instance and try to send as much as possible.
1495 *
1496 * Returns the total of bytes sent to the transport layer.
1497 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001498static int qc_send(struct qcc *qcc)
1499{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001500 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001501 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001502 struct qcs *qcs, *qcs_tmp;
1503 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001504
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001505 TRACE_ENTER(QMUX_EV_QCC_SEND);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001506
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001507 if (qcc->conn->flags & CO_FL_SOCK_WR_SH || qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001508 qcc->conn->flags |= CO_FL_ERROR;
1509 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_SEND, qcc->conn);
1510 return 0;
1511 }
1512
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001513 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1514 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
1515 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
1516 goto out;
1517 }
1518 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02001519
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001520 if (qcc->flags & QC_CF_BLK_MFCTL)
1521 return 0;
1522
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001523 /* loop through all streams, construct STREAM frames if data available.
1524 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001525 */
1526 node = eb64_first(&qcc->streams_by_id);
1527 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001528 int ret;
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001529 uint64_t id;
1530
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001531 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001532 id = qcs->id;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001533
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001534 if (quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001535 node = eb64_next(node);
1536 continue;
1537 }
1538
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001539 if (qcs->flags & QC_SF_TO_RESET) {
1540 qcs_send_reset(qcs);
1541 node = eb64_next(node);
1542 continue;
1543 }
1544
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001545 if (qcs_is_close_local(qcs)) {
1546 node = eb64_next(node);
1547 continue;
1548 }
1549
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001550 if (qcs->flags & QC_SF_BLK_SFCTL) {
1551 node = eb64_next(node);
1552 continue;
1553 }
1554
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001555 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001556 node = eb64_next(node);
1557 continue;
1558 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001559
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001560 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001561 total += ret;
1562 node = eb64_next(node);
1563 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001564
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001565 if (qc_send_frames(qcc, &frms)) {
1566 /* data rejected by transport layer, do not retry. */
1567 goto out;
1568 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001569
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001570 retry:
1571 tmp_total = 0;
1572 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
1573 int ret;
1574 BUG_ON(!b_data(&qcs->tx.buf));
1575 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001576
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001577 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001578 tmp_total += ret;
1579 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001580 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001581
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001582 total += tmp_total;
1583 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
1584 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +01001585
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001586 out:
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02001587 /* Deallocate frames that the transport layer has rejected. */
1588 if (!LIST_ISEMPTY(&frms)) {
1589 struct quic_frame *frm, *frm2;
1590 list_for_each_entry_safe(frm, frm2, &frms, list) {
1591 LIST_DELETE(&frm->list);
1592 pool_free(pool_head_quic_frame, frm);
1593 }
1594 }
1595
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001596 TRACE_LEAVE(QMUX_EV_QCC_SEND);
1597
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001598 return total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001599}
1600
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001601/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1602 * operation.
1603 *
1604 * Returns 0 on success else non-zero.
1605 */
1606static int qc_recv(struct qcc *qcc)
1607{
1608 struct eb64_node *node;
1609 struct qcs *qcs;
1610
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001611 TRACE_ENTER(QMUX_EV_QCC_RECV);
1612
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001613 if (qcc->flags & QC_CF_CC_EMIT) {
1614 TRACE_DEVEL("leaving on error", QMUX_EV_QCC_RECV, qcc->conn);
1615 return 0;
1616 }
1617
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001618 node = eb64_first(&qcc->streams_by_id);
1619 while (node) {
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001620 uint64_t id;
1621
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001622 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001623 id = qcs->id;
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001624
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001625 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001626 node = eb64_next(node);
1627 continue;
1628 }
1629
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001630 if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001631 node = eb64_next(node);
1632 continue;
1633 }
1634
1635 qcc_decode_qcs(qcc, qcs);
1636 node = eb64_next(node);
Amaury Denoyelle849b24f2022-05-24 17:22:07 +02001637
1638 if (qcs->flags & QC_SF_READ_ABORTED) {
1639 /* TODO should send a STOP_SENDING */
1640 qcs_free(qcs);
1641 }
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001642 }
1643
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001644 TRACE_LEAVE(QMUX_EV_QCC_RECV);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001645 return 0;
1646}
1647
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001648
1649/* Release all streams which have their transfer operation achieved.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001650 *
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001651 * Returns true if at least one stream is released.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001652 */
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001653static int qc_purge_streams(struct qcc *qcc)
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001654{
1655 struct eb64_node *node;
1656 int release = 0;
1657
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001658 TRACE_ENTER(QMUX_EV_QCC_WAKE);
1659
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001660 node = eb64_first(&qcc->streams_by_id);
1661 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001662 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001663 node = eb64_next(node);
1664
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001665 /* Release not attached closed streams. */
1666 if (qcs->st == QC_SS_CLO && !qcs_sc(qcs)) {
1667 TRACE_DEVEL("purging closed stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
1668 qcs_destroy(qcs);
1669 release = 1;
1670 continue;
1671 }
1672
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001673 /* Release detached streams with empty buffer. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001674 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02001675 if (qcs_is_close_local(qcs)) {
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001676 TRACE_DEVEL("purging detached stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001677 qcs_destroy(qcs);
1678 release = 1;
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001679 continue;
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001680 }
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001681
1682 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1683 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001684 }
1685 }
1686
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001687 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001688 return release;
1689}
1690
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001691/* release function. This one should be called to free all resources allocated
1692 * to the mux.
1693 */
1694static void qc_release(struct qcc *qcc)
1695{
1696 struct connection *conn = qcc->conn;
1697 struct eb64_node *node;
1698
1699 TRACE_ENTER(QMUX_EV_QCC_END);
1700
1701 if (qcc->app_ops && qcc->app_ops->release) {
1702 /* Application protocol with dedicated connection closing
1703 * procedure.
1704 */
1705 qcc->app_ops->release(qcc->ctx);
Amaury Denoyellea154dc02022-06-13 17:09:01 +02001706
1707 /* useful if application protocol should emit some closing
1708 * frames. For example HTTP/3 GOAWAY frame.
1709 */
1710 qc_send(qcc);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001711 }
1712 else {
1713 qcc_emit_cc_app(qcc, QC_ERR_NO_ERROR, 0);
1714 }
1715
1716 if (qcc->task) {
1717 task_destroy(qcc->task);
1718 qcc->task = NULL;
1719 }
1720
1721 if (qcc->wait_event.tasklet)
1722 tasklet_free(qcc->wait_event.tasklet);
1723 if (conn && qcc->wait_event.events) {
1724 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
1725 qcc->wait_event.events,
1726 &qcc->wait_event);
1727 }
1728
1729 /* liberate remaining qcs instances */
1730 node = eb64_first(&qcc->streams_by_id);
1731 while (node) {
1732 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
1733 node = eb64_next(node);
1734 qcs_free(qcs);
1735 }
1736
1737 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1738 struct quic_frame *frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list);
1739 LIST_DELETE(&frm->list);
1740 pool_free(pool_head_quic_frame, frm);
1741 }
1742
1743 pool_free(pool_head_qcc, qcc);
1744
1745 if (conn) {
1746 LIST_DEL_INIT(&conn->stopping_list);
1747
1748 conn->handle.qc->conn = NULL;
1749 conn->mux = NULL;
1750 conn->ctx = NULL;
1751
1752 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
1753
1754 conn_stop_tracking(conn);
1755 conn_full_close(conn);
1756 if (conn->destroy_cb)
1757 conn->destroy_cb(conn);
1758 conn_free(conn);
1759 }
1760
1761 TRACE_LEAVE(QMUX_EV_QCC_END);
1762}
1763
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001764static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1765{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001766 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001767
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001768 TRACE_ENTER(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001769
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001770 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001771
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001772 if (qc_purge_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001773 if (qcc_is_dead(qcc)) {
1774 qc_release(qcc);
Amaury Denoyelled3973852022-07-25 14:56:54 +02001775 goto end;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001776 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001777 }
1778
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001779 qc_recv(qcc);
1780
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02001781 /* TODO check if qcc proxy is disabled. If yes, use graceful shutdown
1782 * to close the connection.
1783 */
1784
1785 qcc_refresh_timeout(qcc);
1786
Amaury Denoyelled3973852022-07-25 14:56:54 +02001787 end:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001788 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1789
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001790 return NULL;
1791}
1792
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001793static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1794{
1795 struct qcc *qcc = ctx;
1796 int expired = tick_is_expired(t->expire, now_ms);
1797
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001798 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001799
1800 if (qcc) {
1801 if (!expired) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001802 TRACE_DEVEL("leaving (not expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001803 return t;
1804 }
1805
1806 if (!qcc_may_expire(qcc)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001807 TRACE_DEVEL("leaving (cannot expired)", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001808 t->expire = TICK_ETERNITY;
1809 return t;
1810 }
1811 }
1812
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001813 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001814
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001815 if (!qcc) {
1816 TRACE_DEVEL("leaving (not more qcc)", QMUX_EV_QCC_WAKE);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001817 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001818 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001819
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001820 qcc->task = NULL;
1821
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02001822 /* TODO depending on the timeout condition, different shutdown mode
1823 * should be used. For http keep-alive or disabled proxy, a graceful
1824 * shutdown should occurs. For all other cases, an immediate close
1825 * seems legitimate.
1826 */
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001827 if (qcc_is_dead(qcc))
1828 qc_release(qcc);
1829
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001830 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1831
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001832 return NULL;
1833}
1834
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001835static int qc_init(struct connection *conn, struct proxy *prx,
1836 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001837{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001838 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001839 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001840
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001841 qcc = pool_alloc(pool_head_qcc);
1842 if (!qcc)
1843 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001844
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001845 qcc->conn = conn;
1846 conn->ctx = qcc;
Amaury Denoyellec603de42022-07-25 11:21:46 +02001847 qcc->nb_hreq = qcc->nb_sc = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001848 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001849
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001850 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001851
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001852 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001853
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001854 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001855 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001856
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001857 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001858 qcc->tx.sent_offsets = qcc->tx.offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001859
1860 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001861 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001862 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001863 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001864 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001865
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001866 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001867 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001868 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001869 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001870
1871 /* Server initiated streams must respect the server flow control. */
1872 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001873 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001874 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001875 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1876
1877 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001878 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001879 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001880 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001881
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001882 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001883 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02001884 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
1885 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001886 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001887
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001888 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001889 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001890
Willy Tarreau784b8682022-04-11 14:18:10 +02001891 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001892 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001893 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1894 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1895
Amaury Denoyellea509ffb2022-07-04 15:50:33 +02001896 if (conn_is_back(conn)) {
1897 qcc->next_bidi_l = 0x00;
1898 qcc->largest_bidi_r = 0x01;
1899 qcc->next_uni_l = 0x02;
1900 qcc->largest_uni_r = 0x03;
1901 }
1902 else {
1903 qcc->largest_bidi_r = 0x00;
1904 qcc->next_bidi_l = 0x01;
1905 qcc->largest_uni_r = 0x02;
1906 qcc->next_uni_l = 0x03;
1907 }
1908
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001909 qcc->wait_event.tasklet = tasklet_new();
1910 if (!qcc->wait_event.tasklet)
1911 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001912
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001913 LIST_INIT(&qcc->send_retry_list);
1914
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001915 qcc->subs = NULL;
1916 qcc->wait_event.tasklet->process = qc_io_cb;
1917 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001918 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001919
Amaury Denoyelle07bf8f42022-07-22 16:16:03 +02001920 qcc->proxy = prx;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001921 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001922 qcc->task = NULL;
Amaury Denoyelleb6309452022-07-25 14:51:56 +02001923 qcc->timeout = conn_is_back(qcc->conn) ? prx->timeout.server :
1924 prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001925 if (tick_isset(qcc->timeout)) {
1926 qcc->task = task_new_here();
1927 if (!qcc->task)
1928 goto fail_no_timeout_task;
1929 qcc->task->process = qc_timeout_task;
1930 qcc->task->context = qcc;
1931 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1932 }
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +02001933 qcc_reset_idle_start(qcc);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001934
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001935 if (!conn_is_back(conn)) {
1936 if (!LIST_INLIST(&conn->stopping_list)) {
1937 LIST_APPEND(&mux_stopping_data[tid].list,
1938 &conn->stopping_list);
1939 }
1940 }
1941
Willy Tarreau784b8682022-04-11 14:18:10 +02001942 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001943 /* init read cycle */
1944 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001945
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001946 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001947
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001948 fail_no_timeout_task:
1949 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001950 fail_no_tasklet:
1951 pool_free(pool_head_qcc, qcc);
1952 fail_no_qcc:
1953 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001954}
1955
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02001956static void qc_destroy(void *ctx)
1957{
1958 struct qcc *qcc = ctx;
1959
1960 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
1961 qc_release(qcc);
1962 TRACE_LEAVE(QMUX_EV_QCC_END);
1963}
1964
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001965static void qc_detach(struct sedesc *sd)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001966{
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02001967 struct qcs *qcs = sd->se;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001968 struct qcc *qcc = qcs->qcc;
1969
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001970 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001971
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001972 /* TODO this BUG_ON_HOT() is not correct as the stconn layer may detach
1973 * from the stream even if it is not closed remotely at the QUIC layer.
1974 * This happens for example when a stream must be closed due to a
1975 * rejected request. To better handle these cases, it will be required
1976 * to implement shutr/shutw MUX operations. Once this is done, this
1977 * BUG_ON_HOT() statement can be adjusted.
1978 */
1979 //BUG_ON_HOT(!qcs_is_close_remote(qcs));
Amaury Denoyellec603de42022-07-25 11:21:46 +02001980
1981 qcc_rm_sc(qcc);
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001982
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02001983 if (!qcs_is_close_local(qcs) && !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001984 TRACE_DEVEL("leaving with remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001985 qcs->flags |= QC_SF_DETACH;
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02001986 qcc_refresh_timeout(qcc);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001987 return;
1988 }
1989
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001990 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01001991
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001992 if (qcc_is_dead(qcc)) {
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02001993 TRACE_DEVEL("leaving and killing dead connection", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001994 qc_release(qcc);
1995 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001996 else if (qcc->task) {
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02001997 TRACE_DEVEL("leaving, refreshing connection's timeout", QMUX_EV_STRM_END, qcc->conn);
1998 qcc_refresh_timeout(qcc);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01001999 }
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002000 else {
2001 TRACE_DEVEL("leaving", QMUX_EV_STRM_END, qcc->conn);
2002 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002003}
2004
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002005/* Called from the upper layer, to receive data */
Willy Tarreau3215e732022-05-27 10:09:11 +02002006static size_t qc_rcv_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002007 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002008{
Willy Tarreau3215e732022-05-27 10:09:11 +02002009 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002010 struct htx *qcs_htx = NULL;
2011 struct htx *cs_htx = NULL;
2012 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002013 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002014
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002015 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002016
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002017 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
2018 if (htx_is_empty(qcs_htx)) {
2019 /* Set buffer data to 0 as HTX is empty. */
2020 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
2021 goto end;
2022 }
2023
2024 ret = qcs_htx->data;
2025
2026 cs_htx = htx_from_buf(buf);
2027 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
Amaury Denoyelle72a78e82022-07-29 15:34:12 +02002028 /* EOM will be copied to cs_htx via b_xfer(). */
2029 if (qcs_htx->flags & HTX_FL_EOM)
2030 fin = 1;
2031
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002032 htx_to_buf(cs_htx, buf);
2033 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
2034 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
2035 goto end;
2036 }
2037
2038 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
2039 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
2040
2041 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002042 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
2043 cs_htx->flags |= HTX_FL_EOM;
2044 fin = 1;
2045 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002046
2047 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
2048 htx_to_buf(cs_htx, buf);
2049 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
2050 ret -= qcs_htx->data;
2051
2052 end:
2053 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002054 se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002055 }
2056 else {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002057 se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
2058 if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING))
2059 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002060
Amaury Denoyelle72a78e82022-07-29 15:34:12 +02002061 /* Set end-of-input if FIN received and all data extracted. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002062 if (fin)
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002063 se_fl_set(qcs->sd, SE_FL_EOI);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002064
2065 if (b_size(&qcs->rx.app_buf)) {
2066 b_free(&qcs->rx.app_buf);
2067 offer_buffers(NULL, 1);
2068 }
2069 }
2070
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002071 if (ret) {
2072 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002073 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002074 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002075
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002076 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
2077
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002078 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002079}
2080
Willy Tarreau3215e732022-05-27 10:09:11 +02002081static size_t qc_snd_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002082 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002083{
Willy Tarreau3215e732022-05-27 10:09:11 +02002084 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002085 size_t ret;
2086
2087 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002088
Amaury Denoyelle843a1192022-07-04 11:44:38 +02002089 if (qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_RESET)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002090 ret = count;
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002091 goto end;
2092 }
2093
Willy Tarreau3215e732022-05-27 10:09:11 +02002094 ret = qcs->qcc->app_ops->snd_buf(sc, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002095
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002096 end:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002097 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
2098
2099 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002100}
2101
2102/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2103 * event subscriber <es> is not allowed to change from a previous call as long
2104 * as at least one event is still subscribed. The <event_type> must only be a
2105 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
2106 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002107static int qc_subscribe(struct stconn *sc, int event_type,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002108 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002109{
Willy Tarreau3215e732022-05-27 10:09:11 +02002110 return qcs_subscribe(__sc_mux_strm(sc), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002111}
2112
2113/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
2114 * The <es> pointer is not allowed to differ from the one passed to the
2115 * subscribe() call. It always returns zero.
2116 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002117static int qc_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002118{
Willy Tarreau3215e732022-05-27 10:09:11 +02002119 struct qcs *qcs = __sc_mux_strm(sc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002120
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002121 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
2122 BUG_ON(qcs->subs && qcs->subs != es);
2123
2124 es->events &= ~event_type;
2125 if (!es->events)
2126 qcs->subs = NULL;
2127
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002128 return 0;
2129}
2130
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002131/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Willy Tarreau4596fe22022-05-17 19:07:51 +02002132 * report SE_FL_ERR_PENDING|SE_FL_ERROR on the attached stream connectors and
2133 * wake them.
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002134 */
2135static int qc_wake_some_streams(struct qcc *qcc)
2136{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002137 struct qcs *qcs;
2138 struct eb64_node *node;
2139
2140 for (node = eb64_first(&qcc->streams_by_id); node;
2141 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02002142 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002143
Amaury Denoyelle3abeb572022-07-04 11:42:27 +02002144 if (!qcs_sc(qcs))
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002145 continue;
2146
2147 if (qcc->conn->flags & CO_FL_ERROR) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002148 se_fl_set(qcs->sd, SE_FL_ERR_PENDING);
2149 if (se_fl_test(qcs->sd, SE_FL_EOS))
2150 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002151
Amaury Denoyelle4561f842022-07-06 14:54:34 +02002152 qcs_alert(qcs);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002153 }
2154 }
2155
2156 return 0;
2157}
2158
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002159static int qc_wake(struct connection *conn)
2160{
2161 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02002162 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002163
2164 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002165
2166 /* Check if a soft-stop is in progress.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002167 *
2168 * TODO this is revelant for frontend connections only.
Amaury Denoyelled0c62322022-05-23 08:52:58 +02002169 *
2170 * TODO Client should be notified with a H3 GOAWAY and then a
2171 * CONNECTION_CLOSE. However, quic-conn uses the listener socket for
2172 * sending which at this stage is already closed.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002173 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002174 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Amaury Denoyelled0c62322022-05-23 08:52:58 +02002175 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002176
Willy Tarreau784b8682022-04-11 14:18:10 +02002177 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002178 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
2179
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002180 qc_send(qcc);
2181
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002182 qc_wake_some_streams(qcc);
2183
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002184 if (qcc_is_dead(qcc))
2185 goto release;
2186
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002187 qcc_refresh_timeout(qcc);
2188
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002189 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
2190
2191 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002192
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002193 release:
2194 qc_release(qcc);
2195 TRACE_DEVEL("leaving after releasing the connection", QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002196 return 1;
2197}
2198
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002199
2200static char *qcs_st_to_str(enum qcs_state st)
2201{
2202 switch (st) {
2203 case QC_SS_IDLE: return "IDL";
2204 case QC_SS_OPEN: return "OPN";
2205 case QC_SS_HLOC: return "HCL";
2206 case QC_SS_HREM: return "HCR";
2207 case QC_SS_CLO: return "CLO";
2208 default: return "???";
2209 }
2210}
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01002211
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002212static void qmux_trace_frm(const struct quic_frame *frm)
2213{
2214 switch (frm->type) {
2215 case QUIC_FT_MAX_STREAMS_BIDI:
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002216 chunk_appendf(&trace_buf, " max_streams=%llu",
2217 (ull)frm->max_streams_bidi.max_streams);
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002218 break;
2219
2220 case QUIC_FT_MAX_STREAMS_UNI:
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002221 chunk_appendf(&trace_buf, " max_streams=%llu",
2222 (ull)frm->max_streams_uni.max_streams);
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002223 break;
2224
2225 default:
2226 break;
2227 }
2228}
2229
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01002230/* quic-mux trace handler */
2231static void qmux_trace(enum trace_level level, uint64_t mask,
2232 const struct trace_source *src,
2233 const struct ist where, const struct ist func,
2234 const void *a1, const void *a2, const void *a3, const void *a4)
2235{
2236 const struct connection *conn = a1;
2237 const struct qcc *qcc = conn ? conn->ctx : NULL;
2238 const struct qcs *qcs = a2;
2239
2240 if (!qcc)
2241 return;
2242
2243 if (src->verbosity > QMUX_VERB_CLEAN) {
2244 chunk_appendf(&trace_buf, " : qcc=%p(F)", qcc);
2245
2246 if (qcs)
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002247 chunk_appendf(&trace_buf, " qcs=%p .id=%llu .st=%s",
2248 qcs, (ull)qcs->id,
2249 qcs_st_to_str(qcs->st));
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002250
2251 if (mask & QMUX_EV_QCC_NQCS) {
2252 const uint64_t *id = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002253 chunk_appendf(&trace_buf, " id=%llu", (ull)*id);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002254 }
Amaury Denoyellefa29f332022-03-25 09:09:40 +01002255
2256 if (mask & QMUX_EV_SEND_FRM)
2257 qmux_trace_frm(a3);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01002258
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02002259 if (mask & QMUX_EV_QCS_XFER_DATA) {
2260 const struct qcs_xfer_data_trace_arg *arg = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002261 chunk_appendf(&trace_buf, " prep=%llu xfer=%d",
2262 (ull)arg->prep, arg->xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02002263 }
2264
2265 if (mask & QMUX_EV_QCS_BUILD_STRM) {
2266 const struct qcs_build_stream_trace_arg *arg = a3;
Frédéric Lécaille628e89c2022-06-24 12:13:53 +02002267 chunk_appendf(&trace_buf, " len=%llu fin=%d offset=%llu",
2268 (ull)arg->len, arg->fin, (ull)arg->offset);
Amaury Denoyellefdcec362022-03-25 09:28:10 +01002269 }
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +01002270 }
2271}
2272
2273
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002274static const struct mux_ops qc_ops = {
2275 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002276 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002277 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002278 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002279 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002280 .subscribe = qc_subscribe,
2281 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002282 .wake = qc_wake,
Willy Tarreaub5821e12022-04-26 11:54:08 +02002283 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02002284 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002285};
2286
2287static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002288 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002289
2290INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);