blob: 1c615f4e1fb38d13953e2c30212ef3b3899452d4 [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 Denoyelle1b2dba52022-04-15 17:32:04 +02008#include <haproxy/list.h>
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02009#include <haproxy/ncbuf.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +010010#include <haproxy/pool.h>
Amaury Denoyelled80fbca2022-09-19 17:02:28 +020011#include <haproxy/qmux_http.h>
Amaury Denoyelle36d50bf2022-09-19 16:12:38 +020012#include <haproxy/qmux_trace.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020013#include <haproxy/quic_stream.h>
Frédéric Lécaille748ece62022-05-21 23:58:40 +020014#include <haproxy/quic_tp-t.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020015#include <haproxy/ssl_sock-t.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020016#include <haproxy/stconn.h>
Amaury Denoyelledd4fbfb2022-03-24 16:09:16 +010017#include <haproxy/trace.h>
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +010018#include <haproxy/xprt_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010019
Amaury Denoyelledeed7772021-12-03 11:36:46 +010020DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010021DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
22
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +020023/* Emit a CONNECTION_CLOSE with error <err>. This will interrupt all future
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +020024 * send/receive operations.
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +020025 */
26static void qcc_emit_cc(struct qcc *qcc, int err)
27{
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +020028 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
29
30 TRACE_STATE("set CONNECTION_CLOSE on quic-conn", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle57e6db72022-07-13 15:07:56 +020031 quic_set_connection_close(qcc->conn->handle.qc, quic_err_transport(err));
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +020032 qcc->flags |= QC_CF_CC_EMIT;
33 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +020034
35 TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +020036}
37
Amaury Denoyelledeed7772021-12-03 11:36:46 +010038/* Allocate a new QUIC streams with id <id> and type <type>. */
Amaury Denoyellea509ffb2022-07-04 15:50:33 +020039static struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +010040{
Amaury Denoyelledeed7772021-12-03 11:36:46 +010041 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010042
Amaury Denoyelle4f137572022-03-24 17:10:00 +010043 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
44
Amaury Denoyelledeed7772021-12-03 11:36:46 +010045 qcs = pool_alloc(pool_head_qcs);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +020046 if (!qcs) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +020047 TRACE_ERROR("alloc failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle17014a62022-04-27 15:09:27 +020048 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +020049 }
Amaury Denoyelle17014a62022-04-27 15:09:27 +020050
51 qcs->stream = NULL;
52 qcs->qcc = qcc;
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +020053 qcs->sd = NULL;
Amaury Denoyelle17014a62022-04-27 15:09:27 +020054 qcs->flags = QC_SF_NONE;
Amaury Denoyelle38e60062022-07-01 16:48:42 +020055 qcs->st = QC_SS_IDLE;
Amaury Denoyelle47447af2022-04-27 15:17:11 +020056 qcs->ctx = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010057
Amaury Denoyelle30e260e2022-08-03 11:17:57 +020058 /* App callback attach may register the stream for http-request wait.
59 * These fields must be initialed before.
60 */
61 LIST_INIT(&qcs->el_opening);
62 qcs->start = TICK_ETERNITY;
63
Amaury Denoyelle93fba322022-05-24 16:53:14 +020064 /* Allocate transport layer stream descriptor. Only needed for TX. */
65 if (!quic_stream_is_uni(id) || !quic_stream_is_remote(qcc, id)) {
66 struct quic_conn *qc = qcc->conn->handle.qc;
67 qcs->stream = qc_stream_desc_new(id, type, qcs, qc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +020068 if (!qcs->stream) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +020069 TRACE_ERROR("qc_stream_desc alloc failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle93fba322022-05-24 16:53:14 +020070 goto err;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +020071 }
Amaury Denoyelle93fba322022-05-24 16:53:14 +020072 }
Amaury Denoyelle7272cd72022-03-29 15:15:54 +020073
Amaury Denoyelle3236a8e2022-05-24 15:24:03 +020074 qcs->id = qcs->by_id.key = id;
Amaury Denoyelle47447af2022-04-27 15:17:11 +020075 if (qcc->app_ops->attach) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +020076 if (qcc->app_ops->attach(qcs, qcc->ctx)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +020077 TRACE_ERROR("app proto failure", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle47447af2022-04-27 15:17:11 +020078 goto err;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +020079 }
Amaury Denoyelle47447af2022-04-27 15:17:11 +020080 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +010081
Amaury Denoyelled8e680c2022-03-29 15:18:44 +020082 /* store transport layer stream descriptor in qcc tree */
Amaury Denoyellee4301da2022-04-19 17:59:50 +020083 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +010084
Amaury Denoyelledeed7772021-12-03 11:36:46 +010085 qcc->strms[type].nb_streams++;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010086
Amaury Denoyelle6ea78192022-03-07 15:47:02 +010087 /* If stream is local, use peer remote-limit, or else the opposite. */
88 /* TODO use uni limit for unidirectional streams */
89 qcs->tx.msd = quic_stream_is_local(qcc, id) ? qcc->rfctl.msd_bidi_r :
90 qcc->rfctl.msd_bidi_l;
91
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +020092 qcs->rx.ncbuf = NCBUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +010093 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +020094 qcs->rx.offset = qcs->rx.offset_max = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010095
Amaury Denoyelle44d09122022-04-26 11:21:10 +020096 /* TODO use uni limit for unidirectional streams */
97 qcs->rx.msd = quic_stream_is_local(qcc, id) ? qcc->lfctl.msd_bidi_l :
98 qcc->lfctl.msd_bidi_r;
Amaury Denoyellea9773552022-05-16 14:38:25 +020099 qcs->rx.msd_init = qcs->rx.msd;
Amaury Denoyelle44d09122022-04-26 11:21:10 +0200100
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100101 qcs->tx.buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100102 qcs->tx.offset = 0;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +0100103 qcs->tx.sent_offset = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100104
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100105 qcs->wait_event.tasklet = NULL;
106 qcs->wait_event.events = 0;
107 qcs->subs = NULL;
108
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200109 qcs->err = 0;
110
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100111 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100112 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100113 return qcs;
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200114
115 err:
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200116 if (qcs->ctx && qcc->app_ops->detach)
117 qcc->app_ops->detach(qcs);
118
Amaury Denoyelle93fba322022-05-24 16:53:14 +0200119 qc_stream_desc_release(qcs->stream);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200120 pool_free(pool_head_qcs, qcs);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200121
122 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle17014a62022-04-27 15:09:27 +0200123 return NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100124}
125
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200126static void qc_free_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
127{
128 struct buffer buf;
129
Amaury Denoyelle7313f5e2022-05-17 18:53:21 +0200130 if (ncb_is_null(ncbuf))
131 return;
132
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200133 buf = b_make(ncbuf->area, ncbuf->size, 0, 0);
134 b_free(&buf);
Amaury Denoyelle7313f5e2022-05-17 18:53:21 +0200135 offer_buffers(NULL, 1);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200136
137 *ncbuf = NCBUF_NULL;
138}
139
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200140/* Free a qcs. This function must only be done to remove a stream on allocation
141 * error or connection shutdown. Else use qcs_destroy which handle all the
142 * QUIC connection mechanism.
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100143 */
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200144static void qcs_free(struct qcs *qcs)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100145{
Amaury Denoyelle3baab742022-08-11 18:35:55 +0200146 struct qcc *qcc = qcs->qcc;
147
148 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn, qcs);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200149
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200150 qc_free_ncbuf(qcs, &qcs->rx.ncbuf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200151 b_free(&qcs->tx.buf);
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200152
Amaury Denoyelle3baab742022-08-11 18:35:55 +0200153 BUG_ON(!qcc->strms[qcs_id_type(qcs->id)].nb_streams);
154 --qcc->strms[qcs_id_type(qcs->id)].nb_streams;
Amaury Denoyelledccbd732022-03-29 18:36:59 +0200155
Amaury Denoyelle3baab742022-08-11 18:35:55 +0200156 if (qcs->ctx && qcc->app_ops->detach)
157 qcc->app_ops->detach(qcs);
Amaury Denoyelle47447af2022-04-27 15:17:11 +0200158
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200159 qc_stream_desc_release(qcs->stream);
160
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +0200161 BUG_ON(qcs->sd && !se_fl_test(qcs->sd, SE_FL_ORPHAN));
162 sedesc_free(qcs->sd);
Amaury Denoyellee4301da2022-04-19 17:59:50 +0200163
164 eb64_delete(&qcs->by_id);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100165 pool_free(pool_head_qcs, qcs);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200166
Amaury Denoyelle3baab742022-08-11 18:35:55 +0200167 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100168}
169
Amaury Denoyelle3abeb572022-07-04 11:42:27 +0200170static forceinline struct stconn *qcs_sc(const struct qcs *qcs)
171{
172 return qcs->sd ? qcs->sd->sc : NULL;
173}
174
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200175/* Reset the <qcc> inactivity timeout for http-keep-alive timeout. */
176static forceinline void qcc_reset_idle_start(struct qcc *qcc)
177{
178 qcc->idle_start = now_ms;
179}
180
Amaury Denoyellec603de42022-07-25 11:21:46 +0200181/* Decrement <qcc> sc. */
182static forceinline void qcc_rm_sc(struct qcc *qcc)
183{
184 BUG_ON_HOT(!qcc->nb_sc);
185 --qcc->nb_sc;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200186
187 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
188 * refreshed after this on stream detach.
189 */
190 if (!qcc->nb_sc && !qcc->nb_hreq)
191 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200192}
193
194/* Decrement <qcc> hreq. */
195static forceinline void qcc_rm_hreq(struct qcc *qcc)
196{
197 BUG_ON_HOT(!qcc->nb_hreq);
198 --qcc->nb_hreq;
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +0200199
200 /* Reset qcc idle start for http-keep-alive timeout. Timeout will be
201 * refreshed after this on I/O handler.
202 */
203 if (!qcc->nb_sc && !qcc->nb_hreq)
204 qcc_reset_idle_start(qcc);
Amaury Denoyellec603de42022-07-25 11:21:46 +0200205}
206
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200207static inline int qcc_is_dead(const struct qcc *qcc)
208{
209 /* Mux connection is considered dead if :
210 * - all stream-desc are detached AND
211 * = connection is on error OR
212 * = mux timeout has already fired or is unset
213 */
214 if (!qcc->nb_sc && ((qcc->conn->flags & CO_FL_ERROR) || !qcc->task))
215 return 1;
216
217 return 0;
218}
219
220/* Return true if the mux timeout should be armed. */
221static inline int qcc_may_expire(struct qcc *qcc)
222{
223 return !qcc->nb_sc;
224}
225
226/* Refresh the timeout on <qcc> if needed depending on its state. */
227static void qcc_refresh_timeout(struct qcc *qcc)
228{
229 const struct proxy *px = qcc->proxy;
230
231 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
232
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200233 if (!qcc->task) {
234 TRACE_DEVEL("already expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200235 goto leave;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200236 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200237
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200238 /* Check if upper layer is responsible of timeout management. */
239 if (!qcc_may_expire(qcc)) {
240 TRACE_DEVEL("not eligible for timeout", QMUX_EV_QCC_WAKE, qcc->conn);
241 qcc->task->expire = TICK_ETERNITY;
242 task_queue(qcc->task);
243 goto leave;
244 }
245
246 /* TODO if connection is idle on frontend and proxy is disabled, remove
247 * it with global close_spread delay applied.
248 */
249
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200250 /* TODO implement client/server-fin timeout for graceful shutdown */
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200251
252 /* Frontend timeout management
253 * - detached streams with data left to send -> default timeout
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200254 * - stream waiting on incomplete request or no stream yet activated -> timeout http-request
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200255 * - idle after stream processing -> timeout http-keep-alive
256 */
257 if (!conn_is_back(qcc->conn)) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200258 if (qcc->nb_hreq) {
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200259 TRACE_DEVEL("one or more requests still in progress", QMUX_EV_QCC_WAKE, qcc->conn);
260 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200261 task_queue(qcc->task);
262 goto leave;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200263 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200264
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200265 if (!LIST_ISEMPTY(&qcc->opening_list) || unlikely(!qcc->largest_bidi_r)) {
266 int timeout = px->timeout.httpreq;
267 struct qcs *qcs = NULL;
268 int base_time;
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200269
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200270 /* Use start time of first stream waiting on HTTP or
271 * qcc idle if no stream not yet used.
272 */
273 if (likely(!LIST_ISEMPTY(&qcc->opening_list)))
274 qcs = LIST_ELEM(qcc->opening_list.n, struct qcs *, el_opening);
275 base_time = qcs ? qcs->start : qcc->idle_start;
276
277 TRACE_DEVEL("waiting on http request", QMUX_EV_QCC_WAKE, qcc->conn, qcs);
278 qcc->task->expire = tick_add_ifset(base_time, timeout);
279 }
280 else {
281 /* Use http-request timeout if keep-alive timeout not set */
282 int timeout = tick_isset(px->timeout.httpka) ?
283 px->timeout.httpka : px->timeout.httpreq;
284
285 TRACE_DEVEL("at least one request achieved but none currently in progress", QMUX_EV_QCC_WAKE, qcc->conn);
286 qcc->task->expire = tick_add_ifset(qcc->idle_start, timeout);
287 }
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200288 }
Amaury Denoyelle6ec98372022-08-01 17:59:38 +0200289
290 /* fallback to default timeout if frontend specific undefined or for
291 * backend connections.
292 */
293 if (!tick_isset(qcc->task->expire)) {
294 TRACE_DEVEL("fallback to default timeout", QMUX_EV_QCC_WAKE, qcc->conn);
295 qcc->task->expire = tick_add_ifset(now_ms, qcc->timeout);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200296 }
297
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200298 task_queue(qcc->task);
299
300 leave:
301 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
302}
303
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200304/* Mark a stream as open if it was idle. This can be used on every
305 * successful emission/reception operation to update the stream state.
306 */
307static void qcs_idle_open(struct qcs *qcs)
308{
309 /* This operation must not be used if the stream is already closed. */
310 BUG_ON_HOT(qcs->st == QC_SS_CLO);
311
312 if (qcs->st == QC_SS_IDLE) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200313 TRACE_STATE("opening stream", QMUX_EV_QCS_NEW, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200314 qcs->st = QC_SS_OPEN;
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200315 }
316}
317
318/* Close the local channel of <qcs> instance. */
319static void qcs_close_local(struct qcs *qcs)
320{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200321 TRACE_STATE("closing stream locally", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
322
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200323 /* The stream must have already been opened. */
324 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
325
326 /* This operation cannot be used multiple times. */
327 BUG_ON_HOT(qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO);
328
329 if (quic_stream_is_bidi(qcs->id)) {
330 qcs->st = (qcs->st == QC_SS_HREM) ? QC_SS_CLO : QC_SS_HLOC;
Amaury Denoyelleafb7b9d2022-09-19 11:58:24 +0200331
332 if (qcs->flags & QC_SF_HREQ_RECV)
333 qcc_rm_hreq(qcs->qcc);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200334 }
335 else {
336 /* Only local uni streams are valid for this operation. */
337 BUG_ON_HOT(quic_stream_is_remote(qcs->qcc, qcs->id));
338 qcs->st = QC_SS_CLO;
339 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200340}
341
342/* Close the remote channel of <qcs> instance. */
343static void qcs_close_remote(struct qcs *qcs)
344{
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200345 TRACE_STATE("closing stream remotely", QMUX_EV_QCS_RECV, qcs->qcc->conn, qcs);
346
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200347 /* The stream must have already been opened. */
348 BUG_ON_HOT(qcs->st == QC_SS_IDLE);
349
350 /* This operation cannot be used multiple times. */
351 BUG_ON_HOT(qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO);
352
353 if (quic_stream_is_bidi(qcs->id)) {
354 qcs->st = (qcs->st == QC_SS_HLOC) ? QC_SS_CLO : QC_SS_HREM;
355 }
356 else {
357 /* Only remote uni streams are valid for this operation. */
358 BUG_ON_HOT(quic_stream_is_local(qcs->qcc, qcs->id));
359 qcs->st = QC_SS_CLO;
360 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200361}
362
363static int qcs_is_close_local(struct qcs *qcs)
364{
365 return qcs->st == QC_SS_HLOC || qcs->st == QC_SS_CLO;
366}
367
368static __maybe_unused int qcs_is_close_remote(struct qcs *qcs)
369{
370 return qcs->st == QC_SS_HREM || qcs->st == QC_SS_CLO;
371}
372
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100373struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100374{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100375 struct buffer *buf = b_alloc(bptr);
376 BUG_ON(!buf);
377 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100378}
379
Amaury Denoyellea441ec92022-07-04 15:48:57 +0200380static struct ncbuf *qc_get_ncbuf(struct qcs *qcs, struct ncbuf *ncbuf)
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200381{
382 struct buffer buf = BUF_NULL;
383
384 if (ncb_is_null(ncbuf)) {
385 b_alloc(&buf);
386 BUG_ON(b_is_null(&buf));
387
388 *ncbuf = ncb_make(buf.area, buf.size, 0);
389 ncb_init(ncbuf, 0);
390 }
391
392 return ncbuf;
393}
394
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500395/* Notify an eventual subscriber on <qcs> or else wakeup up the stconn layer if
Amaury Denoyelle4561f842022-07-06 14:54:34 +0200396 * initialized.
397 */
398static void qcs_alert(struct qcs *qcs)
399{
400 if (qcs->subs) {
401 qcs_notify_recv(qcs);
402 qcs_notify_send(qcs);
403 }
404 else if (qcs_sc(qcs) && qcs->sd->sc->app_ops->wake) {
405 qcs->sd->sc->app_ops->wake(qcs->sd->sc);
406 }
407}
408
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100409int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
410{
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100411 struct qcc *qcc = qcs->qcc;
412
413 TRACE_ENTER(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100414
415 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
416 BUG_ON(qcs->subs && qcs->subs != es);
417
418 es->events |= event_type;
419 qcs->subs = es;
420
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100421 if (event_type & SUB_RETRY_RECV)
422 TRACE_DEVEL("subscribe(recv)", QMUX_EV_STRM_RECV, qcc->conn, qcs);
423
424 if (event_type & SUB_RETRY_SEND)
425 TRACE_DEVEL("subscribe(send)", QMUX_EV_STRM_SEND, qcc->conn, qcs);
426
427 TRACE_LEAVE(QMUX_EV_STRM_SEND|QMUX_EV_STRM_RECV, qcc->conn, qcs);
428
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100429 return 0;
430}
431
432void qcs_notify_recv(struct qcs *qcs)
433{
434 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
435 tasklet_wakeup(qcs->subs->tasklet);
436 qcs->subs->events &= ~SUB_RETRY_RECV;
437 if (!qcs->subs->events)
438 qcs->subs = NULL;
439 }
440}
441
442void qcs_notify_send(struct qcs *qcs)
443{
444 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
445 tasklet_wakeup(qcs->subs->tasklet);
446 qcs->subs->events &= ~SUB_RETRY_SEND;
447 if (!qcs->subs->events)
448 qcs->subs = NULL;
449 }
450}
451
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200452/* Open a locally initiated stream for the connection <qcc>. Set <bidi> for a
453 * bidirectional stream, else an unidirectional stream is opened. The next
454 * available ID on the connection will be used according to the stream type.
455 *
456 * Returns the allocated stream instance or NULL on error.
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100457 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200458struct qcs *qcc_init_stream_local(struct qcc *qcc, int bidi)
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100459{
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200460 struct qcs *qcs;
461 enum qcs_type type;
462 uint64_t *next;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100463
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200464 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
465
466 if (bidi) {
467 next = &qcc->next_bidi_l;
468 type = conn_is_back(qcc->conn) ? QCS_CLT_BIDI : QCS_SRV_BIDI;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100469 }
470 else {
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200471 next = &qcc->next_uni_l;
472 type = conn_is_back(qcc->conn) ? QCS_CLT_UNI : QCS_SRV_UNI;
473 }
474
475 /* TODO ensure that we won't overflow remote peer flow control limit on
476 * streams. Else, we should emit a STREAMS_BLOCKED frame.
477 */
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100478
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200479 qcs = qcs_new(qcc, *next, type);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200480 if (!qcs) {
481 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200482 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200483 }
Amaury Denoyellec055e302022-02-07 16:09:06 +0100484
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200485 TRACE_PROTO("opening local stream", QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200486 *next += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100487
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200488 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200489 return qcs;
490}
491
492/* Open a remote initiated stream for the connection <qcc> with ID <id>. The
493 * caller is responsible to ensure that a stream with the same ID was not
494 * already opened. This function will also create all intermediaries streams
495 * with ID smaller than <id> not already opened before.
496 *
497 * Returns the allocated stream instance or NULL on error.
498 */
Amaury Denoyelleb1437232022-07-08 11:53:22 +0200499static struct qcs *qcc_init_stream_remote(struct qcc *qcc, uint64_t id)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200500{
501 struct qcs *qcs = NULL;
502 enum qcs_type type;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200503 uint64_t *largest, max_id;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100504
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200505 TRACE_ENTER(QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200506
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200507 BUG_ON_HOT(quic_stream_is_local(qcc, id));
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100508
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200509 if (quic_stream_is_bidi(id)) {
510 largest = &qcc->largest_bidi_r;
511 type = conn_is_back(qcc->conn) ? QCS_SRV_BIDI : QCS_CLT_BIDI;
512 }
513 else {
514 largest = &qcc->largest_uni_r;
515 type = conn_is_back(qcc->conn) ? QCS_SRV_UNI : QCS_CLT_UNI;
516 }
517
Amaury Denoyellebf3c2082022-08-16 11:29:08 +0200518 /* RFC 9000 4.6. Controlling Concurrency
519 *
520 * An endpoint that receives a frame with a stream ID exceeding the
521 * limit it has sent MUST treat this as a connection error of type
522 * STREAM_LIMIT_ERROR
523 */
524 max_id = quic_stream_is_bidi(id) ? qcc->lfctl.ms_bidi * 4 :
525 qcc->lfctl.ms_uni * 4;
526 if (id >= max_id) {
527 TRACE_ERROR("flow control error", QMUX_EV_QCS_NEW|QMUX_EV_PROTO_ERR, qcc->conn);
528 qcc_emit_cc(qcc, QC_ERR_STREAM_LIMIT_ERROR);
529 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200530 }
531
532 /* Only stream ID not already opened can be used. */
533 BUG_ON(id < *largest);
534
535 while (id >= *largest) {
Amaury Denoyellefd79ddb2022-08-16 11:13:45 +0200536 const char *str = *largest < id ? "initializing intermediary remote stream" :
537 "initializing remote stream";
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200538
539 qcs = qcs_new(qcc, *largest, type);
540 if (!qcs) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200541 /* TODO emit RESET_STREAM */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200542 TRACE_ERROR("stream fallocation failure", QMUX_EV_QCS_NEW, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200543 goto err;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100544 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200545
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200546 TRACE_PROTO(str, QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200547 *largest += 4;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100548 }
549
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200550 out:
551 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn, qcs);
Amaury Denoyelle50742292022-03-29 14:57:19 +0200552 return qcs;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200553
554 err:
555 TRACE_LEAVE(QMUX_EV_QCS_NEW, qcc->conn);
556 return NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200557}
558
559/* Use this function for a stream <id> which is not in <qcc> stream tree. It
560 * returns true if the associated stream is closed.
561 */
562static int qcc_stream_id_is_closed(struct qcc *qcc, uint64_t id)
563{
564 uint64_t *largest;
565
566 /* This function must only be used for stream not present in the stream tree. */
567 BUG_ON_HOT(eb64_lookup(&qcc->streams_by_id, id));
568
569 if (quic_stream_is_local(qcc, id)) {
570 largest = quic_stream_is_uni(id) ? &qcc->next_uni_l :
571 &qcc->next_bidi_l;
572 }
573 else {
574 largest = quic_stream_is_uni(id) ? &qcc->largest_uni_r :
575 &qcc->largest_bidi_r;
576 }
577
578 return id < *largest;
579}
580
581/* Retrieve the stream instance from <id> ID. This can be used when receiving
582 * STREAM, STREAM_DATA_BLOCKED, RESET_STREAM, MAX_STREAM_DATA or STOP_SENDING
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200583 * frames. Set to false <receive_only> or <send_only> if these particular types
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200584 * of streams are not allowed. If the stream instance is found, it is stored in
585 * <out>.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200586 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200587 * Returns 0 on success else non-zero. On error, a RESET_STREAM or a
588 * CONNECTION_CLOSE is automatically emitted. Beware that <out> may be NULL
589 * on success if the stream has already been closed.
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200590 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200591int qcc_get_qcs(struct qcc *qcc, uint64_t id, int receive_only, int send_only,
592 struct qcs **out)
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200593{
594 struct eb64_node *node;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200595
596 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200597 *out = NULL;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200598
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200599 if (!receive_only && quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200600 TRACE_ERROR("receive-only stream not allowed", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id);
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200601 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200602 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200603 }
604
605 if (!send_only && quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200606 TRACE_ERROR("send-only stream not allowed", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id);
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200607 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200608 goto err;
Amaury Denoyelle5fbb8692022-07-06 15:43:21 +0200609 }
610
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200611 /* Search the stream in the connection tree. */
612 node = eb64_lookup(&qcc->streams_by_id, id);
613 if (node) {
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200614 *out = eb64_entry(node, struct qcs, by_id);
615 TRACE_DEVEL("using stream from connection tree", QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200616 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200617 }
618
619 /* Check if stream is already closed. */
620 if (qcc_stream_id_is_closed(qcc, id)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200621 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200622 /* Consider this as a success even if <out> is left NULL. */
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200623 goto out;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200624 }
625
626 /* Create the stream. This is valid only for remote initiated one. A
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500627 * local stream must have already been explicitly created by the
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200628 * application protocol layer.
629 */
630 if (quic_stream_is_local(qcc, id)) {
631 /* RFC 9000 19.8. STREAM Frames
632 *
633 * An endpoint MUST terminate the connection with error
634 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
635 * initiated stream that has not yet been created, or for a send-only
636 * stream.
637 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200638 TRACE_ERROR("locally initiated stream not yet created", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS|QMUX_EV_PROTO_ERR, qcc->conn, NULL, &id);
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200639 qcc_emit_cc(qcc, QC_ERR_STREAM_STATE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200640 goto err;
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200641 }
642 else {
643 /* Remote stream not found - try to open it. */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200644 *out = qcc_init_stream_remote(qcc, id);
645 if (!*out) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200646 TRACE_ERROR("stream creation error", QMUX_EV_QCC_RECV|QMUX_EV_QCC_NQCS, qcc->conn, NULL, &id);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200647 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200648 }
Amaury Denoyellea509ffb2022-07-04 15:50:33 +0200649 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100650
651 out:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200652 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn, *out);
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200653 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200654
655 err:
656 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
657 return 1;
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100658}
659
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200660/* Simple function to duplicate a buffer */
661static inline struct buffer qcs_b_dup(const struct ncbuf *b)
662{
663 return b_make(ncb_orig(b), b->size, b->head, ncb_data(b, 0));
664}
665
Amaury Denoyelle36d4b5e2022-07-01 11:25:40 +0200666/* Remove <bytes> from <qcs> Rx buffer. Flow-control for received offsets may
667 * be allocated for the peer if needed.
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200668 */
669static void qcs_consume(struct qcs *qcs, uint64_t bytes)
670{
671 struct qcc *qcc = qcs->qcc;
672 struct quic_frame *frm;
673 struct ncbuf *buf = &qcs->rx.ncbuf;
674 enum ncb_ret ret;
675
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200676 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
677
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200678 ret = ncb_advance(buf, bytes);
679 if (ret) {
680 ABORT_NOW(); /* should not happens because removal only in data */
681 }
682
683 if (ncb_is_empty(buf))
684 qc_free_ncbuf(qcs, buf);
685
686 qcs->rx.offset += bytes;
687 if (qcs->rx.msd - qcs->rx.offset < qcs->rx.msd_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200688 TRACE_DATA("increase stream credit via MAX_STREAM_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200689 frm = pool_zalloc(pool_head_quic_frame);
690 BUG_ON(!frm); /* TODO handle this properly */
691
692 qcs->rx.msd = qcs->rx.offset + qcs->rx.msd_init;
693
694 LIST_INIT(&frm->reflist);
695 frm->type = QUIC_FT_MAX_STREAM_DATA;
696 frm->max_stream_data.id = qcs->id;
697 frm->max_stream_data.max_stream_data = qcs->rx.msd;
698
699 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
700 tasklet_wakeup(qcc->wait_event.tasklet);
701 }
702
703 qcc->lfctl.offsets_consume += bytes;
704 if (qcc->lfctl.md - qcc->lfctl.offsets_consume < qcc->lfctl.md_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200705 TRACE_DATA("increase conn credit via MAX_DATA", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200706 frm = pool_zalloc(pool_head_quic_frame);
707 BUG_ON(!frm); /* TODO handle this properly */
708
709 qcc->lfctl.md = qcc->lfctl.offsets_consume + qcc->lfctl.md_init;
710
711 LIST_INIT(&frm->reflist);
712 frm->type = QUIC_FT_MAX_DATA;
713 frm->max_data.max_data = qcc->lfctl.md;
714
715 LIST_APPEND(&qcs->qcc->lfctl.frms, &frm->list);
716 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
717 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200718
719 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200720}
721
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200722/* Decode the content of STREAM frames already received on the stream instance
723 * <qcs>.
724 *
725 * Returns 0 on success else non-zero.
726 */
727static int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
728{
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200729 struct buffer b;
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200730 ssize_t ret;
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200731 int fin = 0;
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200732
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200733 TRACE_ENTER(QMUX_EV_QCS_RECV, qcc->conn, qcs);
734
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200735 b = qcs_b_dup(&qcs->rx.ncbuf);
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200736
Amaury Denoyelled1310f82022-09-16 13:30:59 +0200737 /* Signal FIN to application if STREAM FIN received with all data. */
738 if (qcs_is_close_remote(qcs))
Amaury Denoyelle6befccd2022-07-01 11:26:04 +0200739 fin = 1;
740
741 ret = qcc->app_ops->decode_qcs(qcs, &b, fin);
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200742 if (ret < 0) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200743 TRACE_ERROR("decoding error", QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200744 goto err;
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200745 }
746
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +0200747 if (ret) {
748 qcs_consume(qcs, ret);
Amaury Denoyelle62eef852022-06-03 16:40:34 +0200749 qcs_notify_recv(qcs);
750 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200751
752 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200753 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200754
755 err:
756 TRACE_LEAVE(QMUX_EV_QCS_RECV, qcc->conn, qcs);
757 return 1;
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200758}
759
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200760/* Emit a CONNECTION_CLOSE_APP with error <err>. Reserved for application error
Amaury Denoyelled666d742022-07-13 15:15:58 +0200761 * code. To close the connection right away, set <immediate> : this is useful
762 * when dealing with a connection fatal error. Else a graceful shutdown will be
763 * conducted : the error-code is only registered. The lower layer is
764 * responsible to close the connection when deemed suitable. Note that in this
765 * case the error code might be overwritten if an immediate close is requested
766 * in the interval.
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200767 */
Amaury Denoyelled666d742022-07-13 15:15:58 +0200768void qcc_emit_cc_app(struct qcc *qcc, int err, int immediate)
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200769{
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200770 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
771
Amaury Denoyelled666d742022-07-13 15:15:58 +0200772 if (immediate) {
773 quic_set_connection_close(qcc->conn->handle.qc, quic_err_app(err));
774 qcc->flags |= QC_CF_CC_EMIT;
775 tasklet_wakeup(qcc->wait_event.tasklet);
776 }
777 else {
778 /* Only register the error code for graceful shutdown. */
779 qcc->conn->handle.qc->err = quic_err_app(err);
780 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200781
782 TRACE_LEAVE(QMUX_EV_QCC_END, qcc->conn);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200783}
784
785/* Prepare for the emission of RESET_STREAM on <qcs> with error code <err>. */
786void qcc_reset_stream(struct qcs *qcs, int err)
787{
788 struct qcc *qcc = qcs->qcc;
789
790 if ((qcs->flags & QC_SF_TO_RESET) || qcs_is_close_local(qcs))
791 return;
792
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200793 TRACE_STATE("reset stream", QMUX_EV_QCS_END, qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +0200794 qcs->flags |= QC_SF_TO_RESET;
795 qcs->err = err;
796 tasklet_wakeup(qcc->wait_event.tasklet);
Amaury Denoyellef9e190e2022-05-23 16:12:15 +0200797}
798
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200799/* Install the <app_ops> applicative layer of a QUIC connection on mux <qcc>.
800 * Returns 0 on success else non-zero.
801 */
802int qcc_install_app_ops(struct qcc *qcc, const struct qcc_app_ops *app_ops)
803{
804 TRACE_ENTER(QMUX_EV_QCC_NEW, qcc->conn);
805
806 qcc->app_ops = app_ops;
807 if (qcc->app_ops->init && !qcc->app_ops->init(qcc)) {
808 TRACE_ERROR("app ops init error", QMUX_EV_QCC_NEW, qcc->conn);
809 goto err;
810 }
811
812 TRACE_PROTO("application layer initialized", QMUX_EV_QCC_NEW, qcc->conn);
813
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200814 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
815 return 0;
816
817 err:
818 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
819 return 1;
820}
821
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200822/* Handle a new STREAM frame for stream with id <id>. Payload is pointed by
823 * <data> with length <len> and represents the offset <offset>. <fin> is set if
824 * the QUIC frame FIN bit is set.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100825 *
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200826 * Returns 0 on success else non-zero. On error, the received frame should not
827 * be acknowledged.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100828 */
829int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200830 char fin, char *data)
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100831{
832 struct qcs *qcs;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200833 enum ncb_ret ret;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100834
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100835 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
836
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200837 if (qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200838 TRACE_DATA("connection closed", QMUX_EV_QCC_RECV, qcc->conn);
839 goto err;
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +0200840 }
841
Amaury Denoyelle6754d7e2022-05-23 16:12:49 +0200842 /* RFC 9000 19.8. STREAM Frames
843 *
844 * An endpoint MUST terminate the connection with error
845 * STREAM_STATE_ERROR if it receives a STREAM frame for a locally
846 * initiated stream that has not yet been created, or for a send-only
847 * stream.
848 */
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200849 if (qcc_get_qcs(qcc, id, 1, 0, &qcs)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200850 TRACE_DATA("qcs retrieval error", QMUX_EV_QCC_RECV, qcc->conn);
851 goto err;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200852 }
853
854 if (!qcs) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200855 TRACE_DATA("already closed stream", QMUX_EV_QCC_RECV, qcc->conn);
856 goto out;
Amaury Denoyelle57161b72022-07-07 15:02:32 +0200857 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100858
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200859 /* RFC 9000 4.5. Stream Final Size
860 *
861 * Once a final size for a stream is known, it cannot change. If a
862 * RESET_STREAM or STREAM frame is received indicating a change in the
863 * final size for the stream, an endpoint SHOULD respond with an error
864 * of type FINAL_SIZE_ERROR; see Section 11 for details on error
865 * handling.
866 */
867 if (qcs->flags & QC_SF_SIZE_KNOWN &&
868 (offset + len > qcs->rx.offset_max || (fin && offset + len < qcs->rx.offset_max))) {
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200869 TRACE_ERROR("final size error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR, qcc->conn, qcs);
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200870 qcc_emit_cc(qcc, QC_ERR_FINAL_SIZE_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200871 goto err;
Amaury Denoyellebf91e392022-07-04 10:02:04 +0200872 }
873
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100874 if (offset + len <= qcs->rx.offset) {
Amaury Denoyelled1310f82022-09-16 13:30:59 +0200875 /* TODO offset may have been received without FIN first and now
876 * with it. In this case, it must be notified to be able to
877 * close the stream.
878 */
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200879 TRACE_DATA("already received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
880 goto out;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100881 }
882
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200883 TRACE_PROTO("receiving STREAM", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200884 qcs_idle_open(qcs);
885
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200886 if (offset + len > qcs->rx.offset_max) {
887 uint64_t diff = offset + len - qcs->rx.offset_max;
888 qcs->rx.offset_max = offset + len;
889 qcc->lfctl.offsets_recv += diff;
890
891 if (offset + len > qcs->rx.msd ||
892 qcc->lfctl.offsets_recv > qcc->lfctl.md) {
893 /* RFC 9000 4.1. Data Flow Control
894 *
895 * A receiver MUST close the connection with an error
896 * of type FLOW_CONTROL_ERROR if the sender violates
897 * the advertised connection or stream data limits
898 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200899 TRACE_ERROR("flow control error", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200900 qcc->conn, qcs);
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200901 qcc_emit_cc(qcc, QC_ERR_FLOW_CONTROL_ERROR);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200902 goto err;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +0200903 }
904 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100905
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200906 if (!qc_get_ncbuf(qcs, &qcs->rx.ncbuf) || ncb_is_null(&qcs->rx.ncbuf)) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100907 /* TODO should mark qcs as full */
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200908 ABORT_NOW();
909 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100910 }
911
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200912 TRACE_DATA("newly received offset", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200913 if (offset < qcs->rx.offset) {
Frédéric Lécaillea18c3332022-07-04 09:54:58 +0200914 size_t diff = qcs->rx.offset - offset;
915
916 len -= diff;
917 data += diff;
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200918 offset = qcs->rx.offset;
919 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100920
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200921 ret = ncb_add(&qcs->rx.ncbuf, offset - qcs->rx.offset, data, len, NCB_ADD_COMPARE);
922 if (ret != NCB_RET_OK) {
923 if (ret == NCB_RET_DATA_REJ) {
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200924 /* RFC 9000 2.2. Sending and Receiving Data
925 *
926 * An endpoint could receive data for a stream at the
927 * same stream offset multiple times. Data that has
928 * already been received can be discarded. The data at
929 * a given offset MUST NOT change if it is sent
930 * multiple times; an endpoint MAY treat receipt of
931 * different data at the same offset within a stream as
932 * a connection error of type PROTOCOL_VIOLATION.
933 */
Amaury Denoyellec7fb0d22022-08-10 16:39:54 +0200934 TRACE_ERROR("overlapping data rejected", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV|QMUX_EV_PROTO_ERR,
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200935 qcc->conn, qcs);
Amaury Denoyellecc3d7162022-05-20 15:14:57 +0200936 qcc_emit_cc(qcc, QC_ERR_PROTOCOL_VIOLATION);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200937 }
938 else if (ret == NCB_RET_GAP_SIZE) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +0200939 TRACE_DATA("cannot bufferize frame due to gap size limit", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV,
940 qcc->conn, qcs);
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +0200941 }
942 return 1;
943 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100944
945 if (fin)
Amaury Denoyelle3f39b402022-07-01 16:11:03 +0200946 qcs->flags |= QC_SF_SIZE_KNOWN;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100947
Amaury Denoyelled1310f82022-09-16 13:30:59 +0200948 if (qcs->flags & QC_SF_SIZE_KNOWN &&
949 qcs->rx.offset_max == qcs->rx.offset + ncb_data(&qcs->rx.ncbuf, 0)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200950 qcs_close_remote(qcs);
Amaury Denoyelled1310f82022-09-16 13:30:59 +0200951 }
Amaury Denoyelle38e60062022-07-01 16:48:42 +0200952
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200953 if (ncb_data(&qcs->rx.ncbuf, 0) && !(qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200954 qcc_decode_qcs(qcc, qcs);
Amaury Denoyelle418ba212022-08-02 15:57:16 +0200955 qcc_refresh_timeout(qcc);
956 }
Amaury Denoyelle3a086402022-05-18 11:38:22 +0200957
Amaury Denoyelle849b24f2022-05-24 17:22:07 +0200958 if (qcs->flags & QC_SF_READ_ABORTED) {
959 /* TODO should send a STOP_SENDING */
960 qcs_free(qcs);
961 }
962
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200963 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +0100964 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100965 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +0200966
967 err:
968 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
969 return 1;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100970}
971
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100972/* Handle a new MAX_DATA frame. <max> must contains the maximum data field of
973 * the frame.
974 *
975 * Returns 0 on success else non-zero.
976 */
977int qcc_recv_max_data(struct qcc *qcc, uint64_t max)
978{
Amaury Denoyelle392e94e2022-07-06 15:44:16 +0200979 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
980
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +0200981 TRACE_PROTO("receiving MAX_DATA", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100982 if (qcc->rfctl.md < max) {
983 qcc->rfctl.md = max;
Amaury Denoyelle392e94e2022-07-06 15:44:16 +0200984 TRACE_DEVEL("increase remote max-data", QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100985
986 if (qcc->flags & QC_CF_BLK_MFCTL) {
987 qcc->flags &= ~QC_CF_BLK_MFCTL;
988 tasklet_wakeup(qcc->wait_event.tasklet);
989 }
990 }
Amaury Denoyelle392e94e2022-07-06 15:44:16 +0200991
992 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +0100993 return 0;
994}
995
Amaury Denoyelle8727ff42022-03-08 10:39:55 +0100996/* Handle a new MAX_STREAM_DATA frame. <max> must contains the maximum data
997 * field of the frame and <id> is the identifier of the QUIC stream.
998 *
Amaury Denoyelleb68559a2022-07-06 15:45:20 +0200999 * Returns 0 on success else non-zero. On error, the received frame should not
1000 * be acknowledged.
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001001 */
1002int qcc_recv_max_stream_data(struct qcc *qcc, uint64_t id, uint64_t max)
1003{
1004 struct qcs *qcs;
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001005
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001006 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1007
Amaury Denoyelleb68559a2022-07-06 15:45:20 +02001008 /* RFC 9000 19.10. MAX_STREAM_DATA Frames
1009 *
1010 * Receiving a MAX_STREAM_DATA frame for a locally
1011 * initiated stream that has not yet been created MUST be treated as a
1012 * connection error of type STREAM_STATE_ERROR. An endpoint that
1013 * receives a MAX_STREAM_DATA frame for a receive-only stream MUST
1014 * terminate the connection with error STREAM_STATE_ERROR.
1015 */
1016 if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) {
1017 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1018 return 1;
1019 }
1020
1021 if (qcs) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001022 TRACE_PROTO("receiving MAX_STREAM_DATA", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001023 if (max > qcs->tx.msd) {
1024 qcs->tx.msd = max;
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001025 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 +01001026
1027 if (qcs->flags & QC_SF_BLK_SFCTL) {
1028 qcs->flags &= ~QC_SF_BLK_SFCTL;
1029 tasklet_wakeup(qcc->wait_event.tasklet);
1030 }
1031 }
1032 }
1033
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001034 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1035 qcc_refresh_timeout(qcc);
1036
Amaury Denoyelle392e94e2022-07-06 15:44:16 +02001037 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01001038 return 0;
1039}
1040
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001041/* Handle a new STOP_SENDING frame for stream ID <id>. The error code should be
1042 * specified in <err>.
1043 *
1044 * Returns 0 on success else non-zero. On error, the received frame should not
1045 * be acknowledged.
1046 */
1047int qcc_recv_stop_sending(struct qcc *qcc, uint64_t id, uint64_t err)
1048{
1049 struct qcs *qcs;
1050
1051 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
1052
1053 /* RFC 9000 19.5. STOP_SENDING Frames
1054 *
1055 * Receiving a STOP_SENDING frame for a
1056 * locally initiated stream that has not yet been created MUST be
1057 * treated as a connection error of type STREAM_STATE_ERROR. An
1058 * endpoint that receives a STOP_SENDING frame for a receive-only stream
1059 * MUST terminate the connection with error STREAM_STATE_ERROR.
1060 */
1061 if (qcc_get_qcs(qcc, id, 0, 1, &qcs)) {
1062 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1063 return 1;
1064 }
1065
1066 if (!qcs)
1067 goto out;
1068
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001069 TRACE_PROTO("receiving STOP_SENDING", QMUX_EV_QCC_RECV|QMUX_EV_QCS_RECV, qcc->conn, qcs);
Amaury Denoyelle96ca1b72022-08-09 17:36:38 +02001070 qcs_idle_open(qcs);
1071
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001072 /* RFC 9000 3.5. Solicited State Transitions
1073 *
1074 * An endpoint that receives a STOP_SENDING frame
1075 * MUST send a RESET_STREAM frame if the stream is in the "Ready" or
1076 * "Send" state. If the stream is in the "Data Sent" state, the
1077 * endpoint MAY defer sending the RESET_STREAM frame until the packets
1078 * containing outstanding data are acknowledged or declared lost. If
1079 * any outstanding data is declared lost, the endpoint SHOULD send a
1080 * RESET_STREAM frame instead of retransmitting the data.
1081 *
1082 * An endpoint SHOULD copy the error code from the STOP_SENDING frame to
1083 * the RESET_STREAM frame it sends, but it can use any application error
1084 * code.
1085 */
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001086 qcc_reset_stream(qcs, err);
1087
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001088 if (qcc_may_expire(qcc) && !qcc->nb_hreq)
1089 qcc_refresh_timeout(qcc);
1090
Amaury Denoyellea5b50752022-07-04 11:44:53 +02001091 out:
1092 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
1093 return 0;
1094}
1095
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001096/* Signal the closing of remote stream with id <id>. Flow-control for new
1097 * streams may be allocated for the peer if needed.
1098 */
1099static int qcc_release_remote_stream(struct qcc *qcc, uint64_t id)
Amaury Denoyellec055e302022-02-07 16:09:06 +01001100{
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001101 struct quic_frame *frm;
1102
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001103 TRACE_ENTER(QMUX_EV_QCS_END, qcc->conn);
1104
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001105 if (quic_stream_is_bidi(id)) {
1106 ++qcc->lfctl.cl_bidi_r;
1107 if (qcc->lfctl.cl_bidi_r > qcc->lfctl.ms_bidi_init / 2) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001108 TRACE_DATA("increase max stream limit with MAX_STREAMS_BIDI", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001109 frm = pool_zalloc(pool_head_quic_frame);
1110 BUG_ON(!frm); /* TODO handle this properly */
1111
1112 LIST_INIT(&frm->reflist);
1113 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
1114 frm->max_streams_bidi.max_streams = qcc->lfctl.ms_bidi +
1115 qcc->lfctl.cl_bidi_r;
1116 LIST_APPEND(&qcc->lfctl.frms, &frm->list);
1117 tasklet_wakeup(qcc->wait_event.tasklet);
1118
1119 qcc->lfctl.ms_bidi += qcc->lfctl.cl_bidi_r;
1120 qcc->lfctl.cl_bidi_r = 0;
1121 }
1122 }
1123 else {
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02001124 /* TODO in HTTP/3 unidirectional streams cannot be closed or a
1125 * H3_CLOSED_CRITICAL_STREAM will be triggered before
1126 * entering here. If a new application protocol is supported it
1127 * might be necessary to implement MAX_STREAMS_UNI emission.
1128 */
1129 ABORT_NOW();
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001130 }
1131
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001132 TRACE_LEAVE(QMUX_EV_QCS_END, qcc->conn);
1133
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001134 return 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001135}
1136
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05001137/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001138static void qcs_destroy(struct qcs *qcs)
1139{
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001140 struct connection *conn = qcs->qcc->conn;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001141 const uint64_t id = qcs->id;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001142
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001143 TRACE_ENTER(QMUX_EV_QCS_END, conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001144
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001145 if (quic_stream_is_remote(qcs->qcc, id))
1146 qcc_release_remote_stream(qcs->qcc, id);
Amaury Denoyellec055e302022-02-07 16:09:06 +01001147
Amaury Denoyelledccbd732022-03-29 18:36:59 +02001148 qcs_free(qcs);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001149
1150 TRACE_LEAVE(QMUX_EV_QCS_END, conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001151}
1152
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001153/* Transfer as much as possible data on <qcs> from <in> to <out>. This is done
1154 * in respect with available flow-control at stream and connection level.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001155 *
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001156 * Returns the total bytes of transferred data.
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001157 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001158static int qcs_xfer_data(struct qcs *qcs, struct buffer *out, struct buffer *in)
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001159{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001160 struct qcc *qcc = qcs->qcc;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001161 int left, to_xfer;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001162 int total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001163
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001164 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001165
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001166 qc_get_buf(qcs, out);
1167
1168 /*
1169 * QCS out buffer diagram
1170 * head left to_xfer
1171 * -------------> ----------> ----->
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001172 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001173 * |...............|xxxxxxxxxxx|<<<<<
Amaury Denoyellee0320b82022-03-11 19:12:23 +01001174 * --------------------------------------------------
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001175 * ^ ack-off ^ sent-off ^ off
1176 *
1177 * STREAM frame
1178 * ^ ^
1179 * |xxxxxxxxxxxxxxxxx|
1180 */
1181
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001182 BUG_ON_HOT(qcs->tx.sent_offset < qcs->stream->ack_offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001183 BUG_ON_HOT(qcs->tx.offset < qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001184 BUG_ON_HOT(qcc->tx.offsets < qcc->tx.sent_offsets);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001185
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001186 left = qcs->tx.offset - qcs->tx.sent_offset;
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001187 to_xfer = QUIC_MIN(b_data(in), b_room(out));
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001188
1189 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
1190 /* do not exceed flow control limit */
1191 if (qcs->tx.offset + to_xfer > qcs->tx.msd)
1192 to_xfer = qcs->tx.msd - qcs->tx.offset;
1193
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001194 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001195 /* do not overcome flow control limit on connection */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001196 if (qcc->tx.offsets + to_xfer > qcc->rfctl.md)
1197 to_xfer = qcc->rfctl.md - qcc->tx.offsets;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001198
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001199 if (!left && !to_xfer)
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001200 goto out;
1201
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001202 total = b_force_xfer(out, in, to_xfer);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001203
1204 out:
1205 {
1206 struct qcs_xfer_data_trace_arg arg = {
1207 .prep = b_data(out), .xfer = total,
1208 };
1209 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_XFER_DATA,
1210 qcc->conn, qcs, &arg);
1211 }
1212
1213 return total;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001214}
1215
Amaury Denoyellefe8f5552022-04-27 16:44:49 +02001216/* Prepare a STREAM frame for <qcs> instance using <out> as payload. The frame
1217 * is appended in <frm_list>. Set <fin> if this is supposed to be the last
1218 * stream frame.
1219 *
1220 * Returns the length of the STREAM frame or a negative error code.
1221 */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001222static int qcs_build_stream_frm(struct qcs *qcs, struct buffer *out, char fin,
1223 struct list *frm_list)
1224{
1225 struct qcc *qcc = qcs->qcc;
1226 struct quic_frame *frm;
1227 int head, total;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001228 uint64_t base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001229
1230 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1231
Amaury Denoyellea4569202022-04-15 17:29:25 +02001232 /* if ack_offset < buf_offset, it points to an older buffer. */
1233 base_off = MAX(qcs->stream->buf_offset, qcs->stream->ack_offset);
1234 BUG_ON(qcs->tx.sent_offset < base_off);
1235
1236 head = qcs->tx.sent_offset - base_off;
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001237 total = b_data(out) - head;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001238 BUG_ON(total < 0);
1239
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001240 if (!total && !fin) {
1241 /* No need to send anything if total is NULL and no FIN to signal. */
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001242 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1243 return 0;
1244 }
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001245 BUG_ON((!total && qcs->tx.sent_offset > qcs->tx.offset) ||
1246 (total && qcs->tx.sent_offset >= qcs->tx.offset));
Amaury Denoyellea4569202022-04-15 17:29:25 +02001247 BUG_ON(qcs->tx.sent_offset + total > qcs->tx.offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001248 BUG_ON(qcc->tx.sent_offsets + total > qcc->rfctl.md);
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001249
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001250 TRACE_PROTO("sending STREAM frame", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001251 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001252 if (!frm) {
1253 TRACE_ERROR("frame alloc failure", QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001254 goto err;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001255 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001256
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001257 LIST_INIT(&frm->reflist);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001258 frm->type = QUIC_FT_STREAM_8;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001259 frm->stream.stream = qcs->stream;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001260 frm->stream.id = qcs->id;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001261 frm->stream.buf = out;
1262 frm->stream.data = (unsigned char *)b_peek(out, head);
1263
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +01001264 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001265 if (fin)
1266 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001267
1268 if (qcs->tx.sent_offset) {
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001269 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001270 frm->stream.offset.key = qcs->tx.sent_offset;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001271 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001272
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001273 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
1274 frm->stream.len = total;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001275
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001276 LIST_APPEND(frm_list, &frm->list);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001277
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001278 out:
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001279 {
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001280 struct qcs_build_stream_trace_arg arg = {
1281 .len = frm->stream.len, .fin = fin,
1282 .offset = frm->stream.offset.key,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001283 };
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001284 TRACE_LEAVE(QMUX_EV_QCS_SEND|QMUX_EV_QCS_BUILD_STRM,
Amaury Denoyellefdcec362022-03-25 09:28:10 +01001285 qcc->conn, qcs, &arg);
1286 }
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001287
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001288 return total;
1289
1290 err:
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001291 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001292 return -1;
1293}
1294
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001295/* Check after transferring data from qcs.tx.buf if FIN must be set on the next
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001296 * STREAM frame for <qcs>.
1297 *
1298 * Returns true if FIN must be set else false.
1299 */
1300static int qcs_stream_fin(struct qcs *qcs)
1301{
1302 return qcs->flags & QC_SF_FIN_STREAM && !b_data(&qcs->tx.buf);
1303}
1304
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001305/* This function must be called by the upper layer to inform about the sending
1306 * of a STREAM frame for <qcs> instance. The frame is of <data> length and on
1307 * <offset>.
1308 */
1309void qcc_streams_sent_done(struct qcs *qcs, uint64_t data, uint64_t offset)
1310{
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001311 struct qcc *qcc = qcs->qcc;
1312 uint64_t diff;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001313
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001314 TRACE_ENTER(QMUX_EV_QCS_SEND, qcc->conn, qcs);
1315
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001316 BUG_ON(offset > qcs->tx.sent_offset);
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001317 BUG_ON(offset + data > qcs->tx.offset);
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001318
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001319 /* check if the STREAM frame has already been notified. It can happen
1320 * for retransmission.
1321 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001322 if (offset + data < qcs->tx.sent_offset) {
1323 TRACE_DEVEL("offset already notified", QMUX_EV_QCS_SEND, qcc->conn, qcs);
1324 goto out;
1325 }
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001326
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001327 qcs_idle_open(qcs);
1328
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001329 diff = offset + data - qcs->tx.sent_offset;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001330 if (diff) {
1331 /* increase offset sum on connection */
1332 qcc->tx.sent_offsets += diff;
1333 BUG_ON_HOT(qcc->tx.sent_offsets > qcc->rfctl.md);
1334 if (qcc->tx.sent_offsets == qcc->rfctl.md)
1335 qcc->flags |= QC_CF_BLK_MFCTL;
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001336
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001337 /* increase offset on stream */
1338 qcs->tx.sent_offset += diff;
1339 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.msd);
1340 BUG_ON_HOT(qcs->tx.sent_offset > qcs->tx.offset);
1341 if (qcs->tx.sent_offset == qcs->tx.msd)
1342 qcs->flags |= QC_SF_BLK_SFCTL;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001343
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001344 if (qcs->tx.offset == qcs->tx.sent_offset &&
1345 b_full(&qcs->stream->buf->buf)) {
1346 qc_stream_buf_release(qcs->stream);
1347 /* prepare qcs for immediate send retry if data to send */
1348 if (b_data(&qcs->tx.buf))
1349 LIST_APPEND(&qcc->send_retry_list, &qcs->el);
1350 }
1351 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001352
Amaury Denoyelle3dc4e5a2022-09-13 16:49:21 +02001353 if (qcs->tx.offset == qcs->tx.sent_offset && !b_data(&qcs->tx.buf) &&
1354 qcs->flags & (QC_SF_FIN_STREAM|QC_SF_DETACH)) {
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001355 /* Close stream locally. */
1356 qcs_close_local(qcs);
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001357 /* Reset flag to not emit multiple FIN STREAM frames. */
1358 qcs->flags &= ~QC_SF_FIN_STREAM;
Amaury Denoyellea4569202022-04-15 17:29:25 +02001359 }
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001360
1361 out:
1362 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01001363}
1364
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001365/* Wrapper for send on transport layer. Send a list of frames <frms> for the
1366 * connection <qcc>.
1367 *
1368 * Returns 0 if all data sent with success else non-zero.
1369 */
1370static int qc_send_frames(struct qcc *qcc, struct list *frms)
1371{
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001372 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
1373
1374 if (LIST_ISEMPTY(frms)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001375 TRACE_DEVEL("no frames to send", QMUX_EV_QCC_SEND, qcc->conn);
1376 goto err;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001377 }
Frédéric Lécaille4e22f282022-03-18 18:38:19 +01001378
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001379 LIST_INIT(&qcc->send_retry_list);
1380
Amaury Denoyelle70467562022-08-17 16:33:13 +02001381 qc_send_mux(qcc->conn->handle.qc, frms);
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +01001382
Amaury Denoyelledb5d1a12022-03-10 16:42:23 +01001383 /* If there is frames left at this stage, transport layer is blocked.
1384 * Subscribe on it to retry later.
1385 */
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001386 if (!LIST_ISEMPTY(frms)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001387 TRACE_DEVEL("remaining frames to send, subscribing", QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001388 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1389 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001390 goto err;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001391 }
1392
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001393 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001394 return 0;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001395
1396 err:
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001397 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001398 return 1;
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001399}
1400
1401/* Emit a RESET_STREAM on <qcs>.
1402 *
1403 * Returns 0 if the frame has been successfully sent else non-zero.
1404 */
1405static int qcs_send_reset(struct qcs *qcs)
1406{
1407 struct list frms = LIST_HEAD_INIT(frms);
1408 struct quic_frame *frm;
1409
1410 TRACE_ENTER(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1411
1412 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001413 if (!frm) {
1414 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001415 return 1;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001416 }
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001417
1418 LIST_INIT(&frm->reflist);
1419 frm->type = QUIC_FT_RESET_STREAM;
1420 frm->reset_stream.id = qcs->id;
1421 frm->reset_stream.app_error_code = qcs->err;
1422 frm->reset_stream.final_size = qcs->tx.sent_offset;
1423
1424 LIST_APPEND(&frms, &frm->list);
1425 if (qc_send_frames(qcs->qcc, &frms)) {
1426 pool_free(pool_head_quic_frame, frm);
1427 TRACE_DEVEL("cannot send RESET_STREAM", QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
1428 return 1;
1429 }
1430
1431 if (qcs_sc(qcs)) {
1432 se_fl_set_error(qcs->sd);
1433 qcs_alert(qcs);
1434 }
1435
1436 qcs_close_local(qcs);
1437 qcs->flags &= ~QC_SF_TO_RESET;
1438
1439 TRACE_LEAVE(QMUX_EV_QCS_SEND, qcs->qcc->conn, qcs);
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001440 return 0;
1441}
1442
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001443/* Used internally by qc_send function. Proceed to send for <qcs>. This will
1444 * transfer data from qcs buffer to its quic_stream counterpart. A STREAM frame
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001445 * is then generated and inserted in <frms> list.
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001446 *
1447 * Returns the total bytes transferred between qcs and quic_stream buffers. Can
1448 * be null if out buffer cannot be allocated.
1449 */
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001450static int _qc_send_qcs(struct qcs *qcs, struct list *frms)
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001451{
1452 struct qcc *qcc = qcs->qcc;
1453 struct buffer *buf = &qcs->tx.buf;
1454 struct buffer *out = qc_stream_buf_get(qcs->stream);
1455 int xfer = 0;
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001456 char fin = 0;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001457
1458 /* Allocate <out> buffer if necessary. */
1459 if (!out) {
1460 if (qcc->flags & QC_CF_CONN_FULL)
1461 return 0;
1462
1463 out = qc_stream_buf_alloc(qcs->stream, qcs->tx.offset);
1464 if (!out) {
1465 qcc->flags |= QC_CF_CONN_FULL;
1466 return 0;
1467 }
1468 }
1469
1470 /* Transfer data from <buf> to <out>. */
1471 if (b_data(buf)) {
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001472 xfer = qcs_xfer_data(qcs, out, buf);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001473 if (xfer > 0) {
1474 qcs_notify_send(qcs);
1475 qcs->flags &= ~QC_SF_BLK_MROOM;
1476 }
1477
1478 qcs->tx.offset += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001479 BUG_ON_HOT(qcs->tx.offset > qcs->tx.msd);
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001480 qcc->tx.offsets += xfer;
Amaury Denoyelle78fa5592022-06-10 15:18:12 +02001481 BUG_ON_HOT(qcc->tx.offsets > qcc->rfctl.md);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001482 }
1483
1484 /* out buffer cannot be emptied if qcs offsets differ. */
1485 BUG_ON(!b_data(out) && qcs->tx.sent_offset != qcs->tx.offset);
1486
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001487 /* FIN is set if all incoming data were transferred. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001488 fin = qcs_stream_fin(qcs);
1489
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001490 /* Build a new STREAM frame with <out> buffer. */
Amaury Denoyellee53b4892022-07-08 17:19:40 +02001491 if (qcs->tx.sent_offset != qcs->tx.offset || fin) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001492 int ret;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001493 ret = qcs_build_stream_frm(qcs, out, fin, frms);
Amaury Denoyelleb50f3112022-04-28 14:41:50 +02001494 if (ret < 0) { ABORT_NOW(); /* TODO handle this properly */ }
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001495 }
1496
1497 return xfer;
1498}
1499
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001500/* Proceed to sending. Loop through all available streams for the <qcc>
1501 * instance and try to send as much as possible.
1502 *
1503 * Returns the total of bytes sent to the transport layer.
1504 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001505static int qc_send(struct qcc *qcc)
1506{
Amaury Denoyelle6ccfa3c2022-03-10 16:45:53 +01001507 struct list frms = LIST_HEAD_INIT(frms);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001508 struct eb64_node *node;
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001509 struct qcs *qcs, *qcs_tmp;
1510 int total = 0, tmp_total = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001511
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001512 TRACE_ENTER(QMUX_EV_QCC_SEND, qcc->conn);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001513
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001514 if (qcc->conn->flags & CO_FL_SOCK_WR_SH || qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001515 qcc->conn->flags |= CO_FL_ERROR;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001516 TRACE_DEVEL("connection on error", QMUX_EV_QCC_SEND, qcc->conn);
1517 goto err;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02001518 }
1519
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001520 if (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1521 if (qc_send_frames(qcc, &qcc->lfctl.frms)) {
1522 TRACE_DEVEL("flow-control frames rejected by transport, aborting send", QMUX_EV_QCC_SEND, qcc->conn);
1523 goto out;
1524 }
1525 }
Amaury Denoyellec9337802022-04-04 16:36:34 +02001526
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001527 if (qcc->flags & QC_CF_BLK_MFCTL)
1528 return 0;
1529
Frédéric Lécaille3dd79d32022-09-08 17:53:36 +02001530 if (!(qcc->flags & QC_CF_APP_FINAL) && !eb_is_empty(&qcc->streams_by_id) &&
1531 qcc->app_ops->finalize) {
1532 /* Finalize the application layer before sending any stream.
1533 * For h3 this consists in preparing the control stream data (SETTINGS h3).
1534 */
1535 qcc->app_ops->finalize(qcc->ctx);
1536 qcc->flags |= QC_CF_APP_FINAL;
1537 }
1538
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001539 /* loop through all streams, construct STREAM frames if data available.
1540 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001541 */
1542 node = eb64_first(&qcc->streams_by_id);
1543 while (node) {
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001544 int ret;
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001545 uint64_t id;
1546
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001547 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001548 id = qcs->id;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +01001549
Amaury Denoyellec6195d72022-05-23 11:39:14 +02001550 if (quic_stream_is_uni(id) && quic_stream_is_remote(qcc, id)) {
Amaury Denoyellee2ec9422022-03-10 16:46:18 +01001551 node = eb64_next(node);
1552 continue;
1553 }
1554
Amaury Denoyelle843a1192022-07-04 11:44:38 +02001555 if (qcs->flags & QC_SF_TO_RESET) {
1556 qcs_send_reset(qcs);
1557 node = eb64_next(node);
1558 continue;
1559 }
1560
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001561 if (qcs_is_close_local(qcs)) {
1562 node = eb64_next(node);
1563 continue;
1564 }
1565
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001566 if (qcs->flags & QC_SF_BLK_SFCTL) {
1567 node = eb64_next(node);
1568 continue;
1569 }
1570
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001571 if (!b_data(&qcs->tx.buf) && !qc_stream_buf_get(qcs->stream)) {
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02001572 node = eb64_next(node);
1573 continue;
1574 }
Amaury Denoyellea4569202022-04-15 17:29:25 +02001575
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001576 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001577 total += ret;
1578 node = eb64_next(node);
1579 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001580
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001581 if (qc_send_frames(qcc, &frms)) {
1582 /* data rejected by transport layer, do not retry. */
1583 goto out;
1584 }
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001585
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001586 retry:
1587 tmp_total = 0;
1588 list_for_each_entry_safe(qcs, qcs_tmp, &qcc->send_retry_list, el) {
1589 int ret;
1590 BUG_ON(!b_data(&qcs->tx.buf));
1591 BUG_ON(qc_stream_buf_get(qcs->stream));
Amaury Denoyelleda6ad202022-04-12 11:41:04 +02001592
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001593 ret = _qc_send_qcs(qcs, &frms);
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001594 tmp_total += ret;
1595 LIST_DELETE(&qcs->el);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001596 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001597
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001598 total += tmp_total;
1599 if (!qc_send_frames(qcc, &frms) && !LIST_ISEMPTY(&qcc->send_retry_list))
1600 goto retry;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +01001601
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001602 out:
Amaury Denoyelle43c090c2022-06-10 15:16:40 +02001603 /* Deallocate frames that the transport layer has rejected. */
1604 if (!LIST_ISEMPTY(&frms)) {
1605 struct quic_frame *frm, *frm2;
1606 list_for_each_entry_safe(frm, frm2, &frms, list) {
1607 LIST_DELETE(&frm->list);
1608 pool_free(pool_head_quic_frame, frm);
1609 }
1610 }
1611
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001612 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
Amaury Denoyelle75d14ad2022-03-22 15:10:29 +01001613 return total;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001614
1615 err:
1616 TRACE_LEAVE(QMUX_EV_QCC_SEND, qcc->conn);
1617 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001618}
1619
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001620/* Proceed on receiving. Loop through all streams from <qcc> and use decode_qcs
1621 * operation.
1622 *
1623 * Returns 0 on success else non-zero.
1624 */
1625static int qc_recv(struct qcc *qcc)
1626{
1627 struct eb64_node *node;
1628 struct qcs *qcs;
1629
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001630 TRACE_ENTER(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyellee1cad8b2022-05-23 18:52:11 +02001631
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001632 if (qcc->flags & QC_CF_CC_EMIT) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001633 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle5c4373a2022-05-24 14:47:48 +02001634 return 0;
1635 }
1636
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001637 node = eb64_first(&qcc->streams_by_id);
1638 while (node) {
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001639 uint64_t id;
1640
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001641 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001642 id = qcs->id;
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001643
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001644 if (!ncb_data(&qcs->rx.ncbuf, 0) || (qcs->flags & QC_SF_DEM_FULL)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001645 node = eb64_next(node);
1646 continue;
1647 }
1648
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02001649 if (quic_stream_is_uni(id) && quic_stream_is_local(qcc, id)) {
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001650 node = eb64_next(node);
1651 continue;
1652 }
1653
1654 qcc_decode_qcs(qcc, qcs);
1655 node = eb64_next(node);
Amaury Denoyelle849b24f2022-05-24 17:22:07 +02001656
1657 if (qcs->flags & QC_SF_READ_ABORTED) {
1658 /* TODO should send a STOP_SENDING */
1659 qcs_free(qcs);
1660 }
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001661 }
1662
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001663 TRACE_LEAVE(QMUX_EV_QCC_RECV, qcc->conn);
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001664 return 0;
1665}
1666
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001667
1668/* Release all streams which have their transfer operation achieved.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001669 *
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001670 * Returns true if at least one stream is released.
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +01001671 */
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001672static int qc_purge_streams(struct qcc *qcc)
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001673{
1674 struct eb64_node *node;
1675 int release = 0;
1676
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001677 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001678
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001679 node = eb64_first(&qcc->streams_by_id);
1680 while (node) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001681 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001682 node = eb64_next(node);
1683
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001684 /* Release not attached closed streams. */
1685 if (qcs->st == QC_SS_CLO && !qcs_sc(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001686 TRACE_STATE("purging closed stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02001687 qcs_destroy(qcs);
1688 release = 1;
1689 continue;
1690 }
1691
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001692 /* Release detached streams with empty buffer. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001693 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02001694 if (qcs_is_close_local(qcs)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02001695 TRACE_STATE("purging detached stream", QMUX_EV_QCC_WAKE, qcs->qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001696 qcs_destroy(qcs);
1697 release = 1;
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001698 continue;
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001699 }
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001700
1701 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1702 SUB_RETRY_SEND, &qcc->wait_event);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001703 }
1704 }
1705
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001706 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001707 return release;
1708}
1709
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001710/* release function. This one should be called to free all resources allocated
1711 * to the mux.
1712 */
1713static void qc_release(struct qcc *qcc)
1714{
1715 struct connection *conn = qcc->conn;
1716 struct eb64_node *node;
1717
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001718 TRACE_ENTER(QMUX_EV_QCC_END, conn);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001719
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001720 if (qcc->app_ops && qcc->app_ops->shutdown) {
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001721 /* Application protocol with dedicated connection closing
1722 * procedure.
1723 */
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001724 qcc->app_ops->shutdown(qcc->ctx);
Amaury Denoyellea154dc02022-06-13 17:09:01 +02001725
1726 /* useful if application protocol should emit some closing
1727 * frames. For example HTTP/3 GOAWAY frame.
1728 */
1729 qc_send(qcc);
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001730 }
1731 else {
1732 qcc_emit_cc_app(qcc, QC_ERR_NO_ERROR, 0);
1733 }
1734
1735 if (qcc->task) {
1736 task_destroy(qcc->task);
1737 qcc->task = NULL;
1738 }
1739
1740 if (qcc->wait_event.tasklet)
1741 tasklet_free(qcc->wait_event.tasklet);
1742 if (conn && qcc->wait_event.events) {
1743 conn->xprt->unsubscribe(conn, conn->xprt_ctx,
1744 qcc->wait_event.events,
1745 &qcc->wait_event);
1746 }
1747
1748 /* liberate remaining qcs instances */
1749 node = eb64_first(&qcc->streams_by_id);
1750 while (node) {
1751 struct qcs *qcs = eb64_entry(node, struct qcs, by_id);
1752 node = eb64_next(node);
1753 qcs_free(qcs);
1754 }
1755
1756 while (!LIST_ISEMPTY(&qcc->lfctl.frms)) {
1757 struct quic_frame *frm = LIST_ELEM(qcc->lfctl.frms.n, struct quic_frame *, list);
1758 LIST_DELETE(&frm->list);
1759 pool_free(pool_head_quic_frame, frm);
1760 }
1761
Amaury Denoyellef8aaf8b2022-09-14 16:23:47 +02001762 if (qcc->app_ops && qcc->app_ops->release)
1763 qcc->app_ops->release(qcc->ctx);
1764 TRACE_PROTO("application layer released", QMUX_EV_QCC_END, conn);
1765
Amaury Denoyellec49d5d12022-07-15 10:32:53 +02001766 pool_free(pool_head_qcc, qcc);
1767
1768 if (conn) {
1769 LIST_DEL_INIT(&conn->stopping_list);
1770
1771 conn->handle.qc->conn = NULL;
1772 conn->mux = NULL;
1773 conn->ctx = NULL;
1774
1775 TRACE_DEVEL("freeing conn", QMUX_EV_QCC_END, conn);
1776
1777 conn_stop_tracking(conn);
1778 conn_full_close(conn);
1779 if (conn->destroy_cb)
1780 conn->destroy_cb(conn);
1781 conn_free(conn);
1782 }
1783
1784 TRACE_LEAVE(QMUX_EV_QCC_END);
1785}
1786
Willy Tarreau41e701e2022-09-08 15:12:59 +02001787struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001788{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001789 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001790
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001791 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc->conn);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001792
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001793 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001794
Amaury Denoyellec1a6dfd2022-07-08 14:04:21 +02001795 if (qc_purge_streams(qcc)) {
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001796 if (qcc_is_dead(qcc)) {
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001797 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001798 goto release;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02001799 }
Amaury Denoyelle2873a312021-12-08 14:42:55 +01001800 }
1801
Amaury Denoyelle37c2e4a2022-05-16 13:54:59 +02001802 qc_recv(qcc);
1803
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02001804 /* TODO check if qcc proxy is disabled. If yes, use graceful shutdown
1805 * to close the connection.
1806 */
1807
1808 qcc_refresh_timeout(qcc);
1809
Amaury Denoyelled3973852022-07-25 14:56:54 +02001810 end:
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001811 TRACE_LEAVE(QMUX_EV_QCC_WAKE, qcc->conn);
1812 return NULL;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001813
Amaury Denoyelle3baab742022-08-11 18:35:55 +02001814 release:
1815 qc_release(qcc);
1816 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001817 return NULL;
1818}
1819
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001820static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
1821{
1822 struct qcc *qcc = ctx;
1823 int expired = tick_is_expired(t->expire, now_ms);
1824
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001825 TRACE_ENTER(QMUX_EV_QCC_WAKE, qcc ? qcc->conn : NULL);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001826
1827 if (qcc) {
1828 if (!expired) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001829 TRACE_DEVEL("not expired", QMUX_EV_QCC_WAKE, qcc->conn);
1830 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001831 }
1832
1833 if (!qcc_may_expire(qcc)) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001834 TRACE_DEVEL("cannot expired", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001835 t->expire = TICK_ETERNITY;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001836 goto requeue;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001837 }
1838 }
1839
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001840 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001841
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001842 if (!qcc) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001843 TRACE_DEVEL("no more qcc", QMUX_EV_QCC_WAKE);
1844 goto out;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001845 }
Amaury Denoyelleea3e0352022-02-21 10:05:16 +01001846
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001847 qcc->task = NULL;
1848
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02001849 /* TODO depending on the timeout condition, different shutdown mode
1850 * should be used. For http keep-alive or disabled proxy, a graceful
1851 * shutdown should occurs. For all other cases, an immediate close
1852 * seems legitimate.
1853 */
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001854 if (qcc_is_dead(qcc)) {
1855 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001856 qc_release(qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001857 }
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001858
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001859 out:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01001860 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001861 return NULL;
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02001862
1863 requeue:
1864 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
1865 return t;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001866}
1867
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001868static int qc_init(struct connection *conn, struct proxy *prx,
1869 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001870{
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001871 struct qcc *qcc;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001872 struct quic_transport_params *lparams, *rparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001873
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001874 TRACE_ENTER(QMUX_EV_QCC_NEW);
1875
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001876 qcc = pool_alloc(pool_head_qcc);
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001877 if (!qcc) {
1878 TRACE_ERROR("alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001879 goto fail_no_qcc;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001880 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001881
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001882 qcc->conn = conn;
1883 conn->ctx = qcc;
Amaury Denoyellec603de42022-07-25 11:21:46 +02001884 qcc->nb_hreq = qcc->nb_sc = 0;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +01001885 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001886
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001887 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001888
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001889 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001890
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001891 /* Server parameters, params used for RX flow control. */
Willy Tarreau784b8682022-04-11 14:18:10 +02001892 lparams = &conn->handle.qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001893
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001894 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyelleb9e06402022-06-10 15:16:21 +02001895 qcc->tx.sent_offsets = qcc->tx.offsets = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001896
1897 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001898 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001899 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001900 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001901 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001902
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001903 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001904 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001905 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001906 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001907
1908 /* Server initiated streams must respect the server flow control. */
1909 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001910 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001911 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001912 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
1913
1914 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001915 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +01001916 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +01001917 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001918
Amaury Denoyellec985cb12022-05-16 14:29:59 +02001919 LIST_INIT(&qcc->lfctl.frms);
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001920 qcc->lfctl.ms_bidi = qcc->lfctl.ms_bidi_init = lparams->initial_max_streams_bidi;
Amaury Denoyellebf3c2082022-08-16 11:29:08 +02001921 qcc->lfctl.ms_uni = lparams->initial_max_streams_uni;
Amaury Denoyelle44d09122022-04-26 11:21:10 +02001922 qcc->lfctl.msd_bidi_l = lparams->initial_max_stream_data_bidi_local;
1923 qcc->lfctl.msd_bidi_r = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyelle78396e52022-03-21 17:13:32 +01001924 qcc->lfctl.cl_bidi_r = 0;
Amaury Denoyellec055e302022-02-07 16:09:06 +01001925
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001926 qcc->lfctl.md = qcc->lfctl.md_init = lparams->initial_max_data;
Amaury Denoyelled46b0f52022-05-20 15:05:07 +02001927 qcc->lfctl.offsets_recv = qcc->lfctl.offsets_consume = 0;
Amaury Denoyellec830e1e2022-05-16 16:19:59 +02001928
Willy Tarreau784b8682022-04-11 14:18:10 +02001929 rparams = &conn->handle.qc->tx.params;
Amaury Denoyelle05ce55e2022-03-08 10:35:42 +01001930 qcc->rfctl.md = rparams->initial_max_data;
Amaury Denoyelle6ea78192022-03-07 15:47:02 +01001931 qcc->rfctl.msd_bidi_l = rparams->initial_max_stream_data_bidi_local;
1932 qcc->rfctl.msd_bidi_r = rparams->initial_max_stream_data_bidi_remote;
1933
Amaury Denoyellea509ffb2022-07-04 15:50:33 +02001934 if (conn_is_back(conn)) {
1935 qcc->next_bidi_l = 0x00;
1936 qcc->largest_bidi_r = 0x01;
1937 qcc->next_uni_l = 0x02;
1938 qcc->largest_uni_r = 0x03;
1939 }
1940 else {
1941 qcc->largest_bidi_r = 0x00;
1942 qcc->next_bidi_l = 0x01;
1943 qcc->largest_uni_r = 0x02;
1944 qcc->next_uni_l = 0x03;
1945 }
1946
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001947 qcc->wait_event.tasklet = tasklet_new();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001948 if (!qcc->wait_event.tasklet) {
1949 TRACE_ERROR("taslket alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001950 goto fail_no_tasklet;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001951 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001952
Amaury Denoyelle1b2dba52022-04-15 17:32:04 +02001953 LIST_INIT(&qcc->send_retry_list);
1954
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001955 qcc->subs = NULL;
1956 qcc->wait_event.tasklet->process = qc_io_cb;
1957 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécaillef27b66f2022-03-18 22:49:22 +01001958 qcc->wait_event.events = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001959
Amaury Denoyelle07bf8f42022-07-22 16:16:03 +02001960 qcc->proxy = prx;
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001961 /* haproxy timeouts */
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001962 qcc->task = NULL;
Amaury Denoyelleb6309452022-07-25 14:51:56 +02001963 qcc->timeout = conn_is_back(qcc->conn) ? prx->timeout.server :
1964 prx->timeout.client;
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001965 if (tick_isset(qcc->timeout)) {
1966 qcc->task = task_new_here();
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001967 if (!qcc->task) {
1968 TRACE_ERROR("timeout task alloc failure", QMUX_EV_QCC_NEW);
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001969 goto fail_no_timeout_task;
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001970 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02001971 qcc->task->process = qc_timeout_task;
1972 qcc->task->context = qcc;
1973 qcc->task->expire = tick_add(now_ms, qcc->timeout);
1974 }
Amaury Denoyellebd6ec1b2022-07-25 11:53:18 +02001975 qcc_reset_idle_start(qcc);
Amaury Denoyelle30e260e2022-08-03 11:17:57 +02001976 LIST_INIT(&qcc->opening_list);
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001977
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01001978 if (!conn_is_back(conn)) {
1979 if (!LIST_INLIST(&conn->stopping_list)) {
1980 LIST_APPEND(&mux_stopping_data[tid].list,
1981 &conn->stopping_list);
1982 }
1983 }
1984
Willy Tarreau784b8682022-04-11 14:18:10 +02001985 HA_ATOMIC_STORE(&conn->handle.qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001986 /* init read cycle */
1987 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001988
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001989 TRACE_LEAVE(QMUX_EV_QCC_NEW, qcc->conn);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001990 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001991
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +01001992 fail_no_timeout_task:
1993 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001994 fail_no_tasklet:
1995 pool_free(pool_head_qcc, qcc);
1996 fail_no_qcc:
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02001997 TRACE_LEAVE(QMUX_EV_QCC_NEW);
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001998 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001999}
2000
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002001static void qc_destroy(void *ctx)
2002{
2003 struct qcc *qcc = ctx;
2004
2005 TRACE_ENTER(QMUX_EV_QCC_END, qcc->conn);
2006 qc_release(qcc);
2007 TRACE_LEAVE(QMUX_EV_QCC_END);
2008}
2009
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002010static void qc_detach(struct sedesc *sd)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002011{
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002012 struct qcs *qcs = sd->se;
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002013 struct qcc *qcc = qcs->qcc;
2014
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002015 TRACE_ENTER(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002016
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002017 /* TODO this BUG_ON_HOT() is not correct as the stconn layer may detach
2018 * from the stream even if it is not closed remotely at the QUIC layer.
2019 * This happens for example when a stream must be closed due to a
2020 * rejected request. To better handle these cases, it will be required
2021 * to implement shutr/shutw MUX operations. Once this is done, this
2022 * BUG_ON_HOT() statement can be adjusted.
2023 */
2024 //BUG_ON_HOT(!qcs_is_close_remote(qcs));
Amaury Denoyellec603de42022-07-25 11:21:46 +02002025
2026 qcc_rm_sc(qcc);
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002027
Amaury Denoyelle20d1f842022-07-11 11:23:17 +02002028 if (!qcs_is_close_local(qcs) && !(qcc->conn->flags & CO_FL_ERROR)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002029 TRACE_STATE("remaining data, detaching qcs", QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002030 qcs->flags |= QC_SF_DETACH;
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002031 qcc_refresh_timeout(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002032
2033 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn, qcs);
Amaury Denoyelle2873a312021-12-08 14:42:55 +01002034 return;
2035 }
2036
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002037 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +01002038
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002039 if (qcc_is_dead(qcc)) {
Amaury Denoyelle047d86a2022-08-10 16:42:35 +02002040 TRACE_STATE("killing dead connection", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002041 goto release;
Amaury Denoyelle06890aa2022-04-04 16:15:06 +02002042 }
Amaury Denoyellea3daaec2022-04-21 16:29:27 +02002043 else if (qcc->task) {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002044 TRACE_DEVEL("refreshing connection's timeout", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002045 qcc_refresh_timeout(qcc);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +01002046 }
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002047 else {
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002048 TRACE_DEVEL("completed", QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002049 }
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002050
2051 TRACE_LEAVE(QMUX_EV_STRM_END, qcc->conn);
Amaury Denoyelle35a66c02022-08-12 15:56:21 +02002052 return;
2053
2054 release:
2055 qc_release(qcc);
2056 TRACE_LEAVE(QMUX_EV_STRM_END);
2057 return;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002058}
2059
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002060/* Called from the upper layer, to receive data */
Willy Tarreau3215e732022-05-27 10:09:11 +02002061static size_t qc_rcv_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002062 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002063{
Willy Tarreau3215e732022-05-27 10:09:11 +02002064 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002065 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002066 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002067
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002068 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002069
Amaury Denoyelled80fbca2022-09-19 17:02:28 +02002070 ret = qcs_http_rcv_buf(qcs, buf, count, &fin);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002071
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002072 if (b_data(&qcs->rx.app_buf)) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002073 se_fl_set(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002074 }
2075 else {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002076 se_fl_clr(qcs->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
2077 if (se_fl_test(qcs->sd, SE_FL_ERR_PENDING))
2078 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002079
Amaury Denoyelle72a78e82022-07-29 15:34:12 +02002080 /* Set end-of-input if FIN received and all data extracted. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +01002081 if (fin)
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002082 se_fl_set(qcs->sd, SE_FL_EOI);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002083
2084 if (b_size(&qcs->rx.app_buf)) {
2085 b_free(&qcs->rx.app_buf);
2086 offer_buffers(NULL, 1);
2087 }
2088 }
2089
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002090 if (ret) {
2091 qcs->flags &= ~QC_SF_DEM_FULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002092 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
Amaury Denoyellef1fc0b32022-05-02 11:07:06 +02002093 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002094
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002095 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
2096
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01002097 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002098}
2099
Willy Tarreau3215e732022-05-27 10:09:11 +02002100static size_t qc_snd_buf(struct stconn *sc, struct buffer *buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002101 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002102{
Willy Tarreau3215e732022-05-27 10:09:11 +02002103 struct qcs *qcs = __sc_mux_strm(sc);
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002104 size_t ret;
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002105 char fin;
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002106
2107 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002108
Amaury Denoyelle3dc4e5a2022-09-13 16:49:21 +02002109 /* stream layer has been detached so no transfer must occur after. */
2110 BUG_ON_HOT(qcs->flags & QC_SF_DETACH);
2111
Amaury Denoyelle843a1192022-07-04 11:44:38 +02002112 if (qcs_is_close_local(qcs) || (qcs->flags & QC_SF_TO_RESET)) {
Amaury Denoyelle0ed617a2022-09-20 14:46:40 +02002113 ret = qcs_http_reset_buf(qcs, buf, count);
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002114 goto end;
2115 }
2116
Amaury Denoyelle9534e592022-09-19 17:14:27 +02002117 ret = qcs_http_snd_buf(qcs, buf, count, &fin);
2118 if (fin)
2119 qcs->flags |= QC_SF_FIN_STREAM;
2120
2121 if (ret) {
2122 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
2123 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
2124 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002125
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002126 end:
Amaury Denoyelle4f137572022-03-24 17:10:00 +01002127 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
2128
2129 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002130}
2131
2132/* Called from the upper layer, to subscribe <es> to events <event_type>. The
2133 * event subscriber <es> is not allowed to change from a previous call as long
2134 * as at least one event is still subscribed. The <event_type> must only be a
2135 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
2136 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002137static int qc_subscribe(struct stconn *sc, int event_type,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002138 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002139{
Willy Tarreau3215e732022-05-27 10:09:11 +02002140 return qcs_subscribe(__sc_mux_strm(sc), event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002141}
2142
2143/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
2144 * The <es> pointer is not allowed to differ from the one passed to the
2145 * subscribe() call. It always returns zero.
2146 */
Willy Tarreau3215e732022-05-27 10:09:11 +02002147static int qc_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002148{
Willy Tarreau3215e732022-05-27 10:09:11 +02002149 struct qcs *qcs = __sc_mux_strm(sc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002150
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002151 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
2152 BUG_ON(qcs->subs && qcs->subs != es);
2153
2154 es->events &= ~event_type;
2155 if (!es->events)
2156 qcs->subs = NULL;
2157
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002158 return 0;
2159}
2160
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002161/* Loop through all qcs from <qcc>. If CO_FL_ERROR is set on the connection,
Willy Tarreau4596fe22022-05-17 19:07:51 +02002162 * report SE_FL_ERR_PENDING|SE_FL_ERROR on the attached stream connectors and
2163 * wake them.
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002164 */
2165static int qc_wake_some_streams(struct qcc *qcc)
2166{
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002167 struct qcs *qcs;
2168 struct eb64_node *node;
2169
2170 for (node = eb64_first(&qcc->streams_by_id); node;
2171 node = eb64_next(node)) {
Amaury Denoyellee4301da2022-04-19 17:59:50 +02002172 qcs = eb64_entry(node, struct qcs, by_id);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002173
Amaury Denoyelle3abeb572022-07-04 11:42:27 +02002174 if (!qcs_sc(qcs))
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002175 continue;
2176
2177 if (qcc->conn->flags & CO_FL_ERROR) {
Willy Tarreaud7b7e0d2022-05-27 16:09:35 +02002178 se_fl_set(qcs->sd, SE_FL_ERR_PENDING);
2179 if (se_fl_test(qcs->sd, SE_FL_EOS))
2180 se_fl_set(qcs->sd, SE_FL_ERROR);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002181
Amaury Denoyelle4561f842022-07-06 14:54:34 +02002182 qcs_alert(qcs);
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002183 }
2184 }
2185
2186 return 0;
2187}
2188
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002189static int qc_wake(struct connection *conn)
2190{
2191 struct qcc *qcc = conn->ctx;
Willy Tarreau784b8682022-04-11 14:18:10 +02002192 struct proxy *prx = conn->handle.qc->li->bind_conf->frontend;
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002193
2194 TRACE_ENTER(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002195
2196 /* Check if a soft-stop is in progress.
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002197 *
cui flitera94bedc2022-08-29 14:42:57 +08002198 * TODO this is relevant for frontend connections only.
Amaury Denoyelled0c62322022-05-23 08:52:58 +02002199 *
2200 * TODO Client should be notified with a H3 GOAWAY and then a
2201 * CONNECTION_CLOSE. However, quic-conn uses the listener socket for
2202 * sending which at this stage is already closed.
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002203 */
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002204 if (unlikely(prx->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Amaury Denoyelled0c62322022-05-23 08:52:58 +02002205 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002206
Willy Tarreau784b8682022-04-11 14:18:10 +02002207 if (conn->handle.qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002208 qcc->conn->flags |= (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH);
2209
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002210 qc_send(qcc);
2211
Amaury Denoyellefe035ec2022-04-06 15:46:30 +02002212 qc_wake_some_streams(qcc);
2213
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002214 if (qcc_is_dead(qcc))
2215 goto release;
2216
Amaury Denoyelle5fc05d12022-07-25 14:58:48 +02002217 qcc_refresh_timeout(qcc);
2218
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002219 TRACE_LEAVE(QMUX_EV_QCC_WAKE, conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002220 return 0;
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002221
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002222 release:
Amaury Denoyelle4c9a1642022-08-10 16:58:01 +02002223 TRACE_STATE("releasing dead connection", QMUX_EV_QCC_WAKE, qcc->conn);
Amaury Denoyelled97fc802022-04-06 16:13:09 +02002224 qc_release(qcc);
Amaury Denoyellef0b67f92022-08-10 16:14:32 +02002225 TRACE_LEAVE(QMUX_EV_QCC_WAKE);
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002226 return 1;
2227}
2228
Amaury Denoyelle38e60062022-07-01 16:48:42 +02002229
Willy Tarreaub4a4fee2022-09-02 16:00:40 +02002230/* for debugging with CLI's "show sess" command. May emit multiple lines, each
2231 * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single
2232 * line is used. Each field starts with a space so it's safe to print it after
2233 * existing fields.
2234 */
2235static int qc_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx)
2236{
2237 struct qcs *qcs = sd->se;
2238 struct qcc *qcc;
2239 int ret = 0;
2240
2241 if (!qcs)
2242 return ret;
2243
2244 chunk_appendf(msg, " qcs=%p .flg=%#x .id=%llu .st=%s .ctx=%p, .err=%#llx",
2245 qcs, qcs->flags, (ull)qcs->id, qcs_st_to_str(qcs->st), qcs->ctx, (ull)qcs->err);
2246
2247 if (pfx)
2248 chunk_appendf(msg, "\n%s", pfx);
2249
2250 qcc = qcs->qcc;
2251 chunk_appendf(msg, " qcc=%p .flg=%#x .nbsc=%llu .nbhreq=%llu, .task=%p",
2252 qcc, qcc->flags, (ull)qcc->nb_sc, (ull)qcc->nb_hreq, qcc->task);
2253 return ret;
2254}
2255
2256
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002257static const struct mux_ops qc_ops = {
2258 .init = qc_init,
Amaury Denoyelle2461bd52022-04-13 16:54:52 +02002259 .destroy = qc_destroy,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002260 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002261 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002262 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002263 .subscribe = qc_subscribe,
2264 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +01002265 .wake = qc_wake,
Willy Tarreaub4a4fee2022-09-02 16:00:40 +02002266 .show_sd = qc_show_sd,
Willy Tarreaub5821e12022-04-26 11:54:08 +02002267 .flags = MX_FL_HTX|MX_FL_NO_UPG|MX_FL_FRAMED,
Willy Tarreau671bd5a2022-04-11 09:29:21 +02002268 .name = "QUIC",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002269};
2270
2271static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +01002272 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002273
2274INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);