blob: b15f1f7f32fdb0c7e0aebe1f132f628b93f99199 [file] [log] [blame]
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001/*
2 * QUIC mux-demux for connections
3 *
4 * Copyright 2021 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020013#include <import/eb64tree.h>
14
Frédéric Lécailledfbae762021-02-18 09:59:01 +010015#include <haproxy/api.h>
16#include <haproxy/cfgparse.h>
17#include <haproxy/connection.h>
18#include <haproxy/h3.h>
19#include <haproxy/istbuf.h>
20#include <haproxy/log.h>
21#include <haproxy/mux_quic.h>
22#include <haproxy/net_helper.h>
23#include <haproxy/quic_frame.h>
24#include <haproxy/session-t.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020025#include <haproxy/ssl_sock-t.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010026#include <haproxy/stats.h>
27#include <haproxy/stream.h>
28#include <haproxy/stream_interface.h>
29#include <haproxy/trace.h>
30
31/* dummy streams returned for closed, error, refused, idle and states */
32static const struct qcs *qc_closed_stream;
33
Frédéric Lécailledfbae762021-02-18 09:59:01 +010034#define QC_SS_MASK(state) (1UL << (state))
35#define QC_SS_IDLE_BIT (1UL << QC_SS_IDLE)
36#define QC_SS_RLOC_BIT (1UL << QC_SS_RLOC)
37#define QC_SS_RREM_BIT (1UL << QC_SS_RREM)
38#define QC_SS_OPEN_BIT (1UL << QC_SS_OPEN)
39#define QC_SS_HREM_BIT (1UL << QC_SS_HREM)
40#define QC_SS_HLOC_BIT (1UL << QC_SS_HLOC)
41#define QC_SS_ERROR_BIT (1UL << QC_SS_ERROR)
42#define QC_SS_CLOSED_BIT (1UL << QC_SS_CLOSED)
43
44
45/* trace source and events */
46static void qc_trace(enum trace_level level, uint64_t mask, \
47 const struct trace_source *src,
48 const struct ist where, const struct ist func,
49 const void *a1, const void *a2, const void *a3, const void *a4);
50
51/* The event representation is split like this :
52 * strm - application layer
53 * qcs - internal QUIC stream
54 * qcc - internal QUIC connection
55 * conn - external connection
56 *
57 */
58static const struct trace_event qc_trace_events[] = {
59#define QC_EV_QCC_NEW (1ULL << 0)
60 { .mask = QC_EV_QCC_NEW, .name = "qcc_new", .desc = "new QUIC connection" },
61#define QC_EV_QCC_RECV (1ULL << 1)
62 { .mask = QC_EV_QCC_RECV, .name = "qcc_recv", .desc = "Rx on QUIC connection" },
63#define QC_EV_QCC_SEND (1ULL << 2)
64 { .mask = QC_EV_QCC_SEND, .name = "qcc_send", .desc = "Tx on QUIC connection" },
65#define QC_EV_QCC_FCTL (1ULL << 3)
66 { .mask = QC_EV_QCC_FCTL, .name = "qcc_fctl", .desc = "QUIC connection flow-controlled" },
67#define QC_EV_QCC_BLK (1ULL << 4)
68 { .mask = QC_EV_QCC_BLK, .name = "qcc_blk", .desc = "QUIC connection blocked" },
69#define QC_EV_QCC_WAKE (1ULL << 5)
70 { .mask = QC_EV_QCC_WAKE, .name = "qcc_wake", .desc = "QUIC connection woken up" },
71#define QC_EV_QCC_END (1ULL << 6)
72 { .mask = QC_EV_QCC_END, .name = "qcc_end", .desc = "QUIC connection terminated" },
73#define QC_EV_QCC_ERR (1ULL << 7)
74 { .mask = QC_EV_QCC_ERR, .name = "qcc_err", .desc = "error on QUIC connection" },
75#define QC_EV_TX_FRAME (1ULL << 8)
76 { .mask = QC_EV_TX_FRAME, .name = "tx_frame", .desc = "transmission of any QUIC frame" },
77#define QC_EV_QCS_NEW (1ULL << 9)
78 { .mask = QC_EV_QCS_NEW, .name = "qcs_new", .desc = "new QUIC stream" },
79#define QC_EV_QCS_GET (1ULL << 10)
80 { .mask = QC_EV_QCS_GET, .name = "qcs_get", .desc = "get QUIC stream by ID" },
81#define QC_EV_QCS_SEND (1ULL << 11)
82 { .mask = QC_EV_QCS_SEND, .name = "qcs_send", .desc = "Tx for QUIC stream" },
83#define QC_EV_QCS_FCTL (1ULL << 12)
84 { .mask = QC_EV_QCS_FCTL, .name = "qcs_fctl", .desc = "QUIC stream flow-controlled" },
85#define QC_EV_QCS_BLK (1ULL << 13)
86 { .mask = QC_EV_QCS_BLK, .name = "qcs_blk", .desc = "QUIC stream blocked" },
87#define QC_EV_QCS_WAKE (1ULL << 14)
88 { .mask = QC_EV_QCS_WAKE, .name = "qcs_wake", .desc = "QUIC stream woken up" },
89#define QC_EV_QCS_END (1ULL << 15)
90 { .mask = QC_EV_QCS_END, .name = "qcs_end", .desc = "QUIC stream terminated" },
91#define QC_EV_QCS_ERR (1ULL << 16)
92 { .mask = QC_EV_QCS_ERR, .name = "qcs_err", .desc = "error on QUIC stream" },
93#define QC_EV_STRM_NEW (1ULL << 17)
94 { .mask = QC_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
95#define QC_EV_STRM_RECV (1ULL << 18)
96 { .mask = QC_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
97#define QC_EV_STRM_SEND (1ULL << 19)
98 { .mask = QC_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
99#define QC_EV_STRM_FULL (1ULL << 20)
100 { .mask = QC_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" },
101#define QC_EV_STRM_WAKE (1ULL << 21)
102 { .mask = QC_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
103#define QC_EV_STRM_SHUT (1ULL << 22)
104 { .mask = QC_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
105#define QC_EV_STRM_END (1ULL << 23)
106 { .mask = QC_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
107#define QC_EV_STRM_ERR (1ULL << 24)
108 { .mask = QC_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
109 { }
110};
111
112static const struct name_desc qc_trace_lockon_args[4] = {
113 /* arg1 */ { /* already used by the connection */ },
114 /* arg2 */ { .name = "qcs", .desc = "QUIC stream" },
115 /* arg3 */ { },
116 /* arg4 */ { }
117};
118
119static const struct name_desc qc_trace_decoding[] = {
120#define QC_VERB_CLEAN 1
121 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
122#define QC_VERB_MINIMAL 2
123 { .name="minimal", .desc="report only qcc/qcs state and flags, no real decoding" },
124#define QC_VERB_SIMPLE 3
125 { .name="simple", .desc="add request/response status line or frame info when available" },
126#define QC_VERB_ADVANCED 4
127 { .name="advanced", .desc="add header fields or frame decoding when available" },
128#define QC_VERB_COMPLETE 5
129 { .name="complete", .desc="add full data dump when available" },
130 { /* end */ }
131};
132
133static struct trace_source trace_mux_quic = {
134 .name = IST("mux_quic"),
135 .desc = "QUIC multiplexer",
136 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
137 .default_cb = qc_trace,
138 .known_events = qc_trace_events,
139 .lockon_args = qc_trace_lockon_args,
140 .decoding = qc_trace_decoding,
141 .report_events = ~0, // report everything by default
142};
143
144#define TRACE_SOURCE &trace_mux_quic
145INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
146
147/* quic stats module */
148enum {
149 QC_ST_RESET_STREAM_RCVD,
150
151 QC_ST_CONN_PROTO_ERR,
152 QC_ST_STRM_PROTO_ERR,
153 QC_ST_RESET_STREAM_SENT,
154
155 QC_ST_OPEN_CONN,
156 QC_ST_OPEN_STREAM,
157 QC_ST_TOTAL_CONN,
158 QC_ST_TOTAL_STREAM,
159
160 QC_STATS_COUNT /* must be the last member of the enum */
161};
162
163static struct name_desc qc_stats[] = {
164 [QC_ST_RESET_STREAM_RCVD] = { .name = "qc_rst_stream_rcvd",
165 .desc = "Total number of received RESET_STREAM frames" },
166
167 [QC_ST_CONN_PROTO_ERR] = { .name = "qc_detected_conn_protocol_errors",
168 .desc = "Total number of connection protocol errors" },
169 [QC_ST_STRM_PROTO_ERR] = { .name = "qc_detected_strm_protocol_errors",
170 .desc = "Total number of stream protocol errors" },
171 [QC_ST_RESET_STREAM_SENT] = { .name = "qc_rst_stream_resp",
172 .desc = "Total number of RESET_STREAM sent on detected error" },
173
174 [QC_ST_OPEN_CONN] = { .name = "qc_open_connections",
175 .desc = "Count of currently open connections" },
176 [QC_ST_OPEN_STREAM] = { .name = "qc_backend_open_streams",
177 .desc = "Count of currently open streams" },
178 [QC_ST_TOTAL_CONN] = { .name = "qc_open_connections",
179 .desc = "Total number of connections" },
180 [QC_ST_TOTAL_STREAM] = { .name = "qc_backend_open_streams",
181 .desc = "Total number of streams" },
182};
183
184static struct qc_counters {
185 long long rst_stream_rcvd; /* total number of RESET_STREAM frame received */
186
187 long long conn_proto_err; /* total number of protocol errors detected */
188 long long strm_proto_err; /* total number of protocol errors detected */
189 long long rst_stream_resp; /* total number of RESET_STREAM frame sent on error */
190
191 long long open_conns; /* count of currently open connections */
192 long long open_streams; /* count of currently open streams */
193 long long total_conns; /* total number of connections */
194 long long total_streams; /* total number of streams */
195} qc_counters;
196
197static void qc_fill_stats(void *data, struct field *stats)
198{
199 struct qc_counters *counters = data;
200
201 stats[QC_ST_RESET_STREAM_RCVD] = mkf_u64(FN_COUNTER, counters->rst_stream_rcvd);
202
203 stats[QC_ST_CONN_PROTO_ERR] = mkf_u64(FN_COUNTER, counters->conn_proto_err);
204 stats[QC_ST_STRM_PROTO_ERR] = mkf_u64(FN_COUNTER, counters->strm_proto_err);
205 stats[QC_ST_RESET_STREAM_SENT] = mkf_u64(FN_COUNTER, counters->rst_stream_resp);
206
207 stats[QC_ST_OPEN_CONN] = mkf_u64(FN_GAUGE, counters->open_conns);
208 stats[QC_ST_OPEN_STREAM] = mkf_u64(FN_GAUGE, counters->open_streams);
209 stats[QC_ST_TOTAL_CONN] = mkf_u64(FN_COUNTER, counters->total_conns);
210 stats[QC_ST_TOTAL_STREAM] = mkf_u64(FN_COUNTER, counters->total_streams);
211}
212
213static struct stats_module qc_stats_module = {
214 .name = "quic",
215 .fill_stats = qc_fill_stats,
216 .stats = qc_stats,
217 .stats_count = QC_STATS_COUNT,
218 .counters = &qc_counters,
219 .counters_size = sizeof(qc_counters),
220 .domain_flags = MK_STATS_PROXY_DOMAIN(STATS_PX_CAP_FE|STATS_PX_CAP_BE),
221 .clearable = 1,
222};
223
224INITCALL1(STG_REGISTER, stats_register_module, &qc_stats_module);
225
226/* the qcc connection pool */
227DECLARE_STATIC_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
228/* the qcs stream pool */
229DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
230
231static struct task *qc_timeout_task(struct task *t, void *context, unsigned int state);
232static int qc_send(struct qcc *qcc);
233static int qc_recv(struct qcc *qcc);
234static int qc_process(struct qcc *qcc);
235static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int state);
236static inline struct qcs *qcc_st_by_id(struct qcc *qcc, int id);
237static struct task *qc_deferred_shut(struct task *t, void *ctx, unsigned int state);
238static struct qcs *qcc_bck_stream_new(struct qcc *qcc, int dir,
239 struct conn_stream *cs, struct session *sess);
240static void qcs_alert(struct qcs *qcs);
241
242/* returns a qcc state as an abbreviated 3-letter string, or "???" if unknown */
243static inline const char *qcc_st_to_str(enum qc_cs st)
244{
245 switch (st) {
246 case QC_CS_NOERR: return "NER";
247 default: return "???";
248 }
249}
250
251/* marks an error on the connection */
252void qc_error(struct qcc *qcc, int err)
253{
254 TRACE_POINT(QC_EV_QCC_ERR, qcc->conn, 0, 0, (void *)(long)(err));
255 qcc->errcode = err;
256 qcc->st0 = QC_CS_ERROR;
257}
258
259static inline const char *qcs_rx_st_to_str(enum qcs_rx_st st)
260{
261 switch (st) {
262 case QC_RX_SS_IDLE: return "IDL";
263 case QC_RX_SS_RECV: return "RCV";
264 case QC_RX_SS_SIZE_KNOWN: return "SKNWN";
265 case QC_RX_SS_DATA_RECVD: return "DATARCVD";
266 case QC_RX_SS_DATA_READ : return "DATAREAD";
267 case QC_RX_SS_RST_RECVD: return "RSTRCVD";
268 case QC_RX_SS_RST_READ: return "RSTREAD";
269 default: return "???";
270 }
271}
272
273static inline const char *qcs_tx_st_to_str(enum qcs_tx_st st)
274{
275 switch (st) {
276 case QC_TX_SS_IDLE: return "IDL";
277 case QC_TX_SS_READY: return "READY";
278 case QC_TX_SS_SEND: return "SEND";
279 case QC_TX_SS_DATA_SENT: return "DATASENT";
280 case QC_TX_SS_DATA_RECVD: return "DATARCVD";
281 case QC_TX_SS_RST_SENT: return "RSTSENT";
282 case QC_TX_SS_RST_RECVD: return "RSTRCVD";
283 default: return "???";
284 }
285}
286
287/* the QUIC traces always expect that arg1, if non-null, is of type connection
288 * (from which we can derive qcc), that arg2, if non-null, is of type qcs.
289 */
290static void qc_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
291 const struct ist where, const struct ist func,
292 const void *a1, const void *a2, const void *a3, const void *a4)
293{
294 const struct connection *conn = a1;
295 const struct qcc *qcc = conn ? conn->ctx : NULL;
296 const struct qcs *qcs = a2;
297
298 if (!qcc)
299 return;
300
301 if (src->verbosity > QC_VERB_CLEAN) {
302 chunk_appendf(&trace_buf, " : qcc=%p(%c,%s)",
303 qcc, conn_is_back(conn) ? 'B' : 'F', qcc_st_to_str(qcc->st0));
304 if (qcs) {
305 chunk_appendf(&trace_buf, " qcs=%p(rx.%s,tx.%s)",
306 qcs, qcs_rx_st_to_str(qcs->rx.st), qcs_tx_st_to_str(qcs->tx.st));
307 }
308 }
309}
310
311
312/* Detect a pending read0 for a QUIC connection. It happens if a read0 is pending
313 * on the connection AND if there is no more data in the demux buffer. The
314 * function returns 1 to report a read0 or 0 otherwise.
315 */
316__maybe_unused
317static int qcc_read0_pending(struct qcc *qcc)
318{
319 if (conn_xprt_read0_pending(qcc->conn) && !qcc->rx.inmux)
320 return 1;
321 return 0;
322}
323
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100324static __inline int
325qcc_is_dead(const struct qcc *qcc)
326{
Amaury Denoyelleac8ee252021-10-08 17:57:03 +0200327#if 0
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100328 if (eb_is_empty(&qcc->streams_by_id) && /* don't close if streams exist */
329 ((qcc->conn->flags & CO_FL_ERROR) || /* errors close immediately */
330 (qcc->st0 >= QC_CS_ERROR && !qcc->task) || /* a timeout stroke earlier */
331 (!(qcc->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
332 (!br_data(qcc->mbuf) && /* mux buffer empty, also process clean events below */
333 conn_xprt_read0_pending(qcc->conn))))
334 return 1;
Amaury Denoyelleac8ee252021-10-08 17:57:03 +0200335#endif
336 if (!qcc->strms[QCS_CLT_BIDI].nb_streams)
337 return 1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100338
339 return 0;
340}
341
342/*****************************************************/
343/* functions below are for dynamic buffer management */
344/*****************************************************/
345
346/* indicates whether or not the we may call the qc_recv() function to attempt
347 * to receive data into the buffer and/or demux pending data. The condition is
348 * a bit complex due to some API limits for now. The rules are the following :
349 * - if an error or a shutdown was detected on the connection and the buffer
350 * is empty, we must not attempt to receive
351 * - if the demux buf failed to be allocated, we must not try to receive and
352 * we know there is nothing pending
353 * - if no flag indicates a blocking condition, we may attempt to receive,
354 * regardless of whether the demux buffer is full or not, so that only
355 * de demux part decides whether or not to block. This is needed because
356 * the connection API indeed prevents us from re-enabling receipt that is
357 * already enabled in a polled state, so we must always immediately stop
358 * as soon as the demux can't proceed so as never to hit an end of read
359 * with data pending in the buffers.
360 * - otherwise must may not attempt
361 */
362static inline int qc_recv_allowed(const struct qcc *qcc)
363{
364 if (qcc->rx.inmux == 0 &&
365 (qcc->st0 >= QC_CS_ERROR ||
366 qcc->conn->flags & CO_FL_ERROR ||
367 conn_xprt_read0_pending(qcc->conn)))
368 return 0;
369
370 if (!(qcc->flags & QC_CF_DEM_BLOCK_ANY))
371 return 1;
372
373 return 0;
374}
375
376/* restarts reading on the connection if it was not enabled */
377static inline void qcc_restart_reading(const struct qcc *qcc, int consider_buffer)
378{
379 if (!qc_recv_allowed(qcc))
380 return;
381
382 if ((!consider_buffer || !qcc->rx.inmux)
383 && (qcc->wait_event.events & SUB_RETRY_RECV))
384 return;
385
386 tasklet_wakeup(qcc->wait_event.tasklet);
387}
388
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100389static int qc_buf_available(void *target)
390{
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +0200391 struct qcs *qcs = target;
392 if (!b_alloc(&qcs->tx.buf))
393 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100394
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +0200395 qcc_restart_reading(qcs->qcc, 1);
396 return 1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100397}
398
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +0200399struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100400{
401 struct buffer *buf = NULL;
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +0200402 struct qcc *qcc = qcs->qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100403
404 if (likely(!LIST_INLIST(&qcc->buf_wait.list)) &&
405 unlikely((buf = b_alloc(bptr)) == NULL)) {
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +0200406 qcc->buf_wait.target = qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100407 qcc->buf_wait.wakeup_cb = qc_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +0200408 LIST_APPEND(&th_ctx->buffer_wq, &qcc->buf_wait.list);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100409 }
410
411 return buf;
412}
413
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100414/* returns the number of streams in use on a connection to figure if it's
415 * idle or not. We check nb_cs and not nb_streams as the caller will want
416 * to know if it was the last one after a detach().
417 */
418static int qc_used_streams(struct connection *conn)
419{
420 struct qcc *qcc = conn->ctx;
421
422 return qcc->nb_cs;
423}
424
425/* returns the number of concurrent streams available on the connection with <dir>
426 * as direction
427 */
428static int qc_avail_streams(struct connection *conn, enum qcs_dir dir)
429{
430 struct qcc *qcc = conn->ctx;
431 enum qcs_type qcs_type;
432
433 if (qcc->st0 >= QC_CS_ERROR)
434 return 0;
435
436 qcs_type = qcs_type_from_dir(qcc, dir);
437
438 return qcc->strms[qcs_type].max_streams - qcc->strms[qcs_type].nb_streams;
439}
440
441
442/* returns the number of concurrent bidirectional streams available on the
443 * connection.
444 */
445static int qc_avail_streams_bidi(struct connection *conn)
446{
447 return qc_avail_streams(conn, QCS_BIDI);
448}
449
450/* returns the number of concurrent unidirectional streams available on the
451 * connection.
452 */
453static int qc_avail_streams_uni(struct connection *conn)
454{
455 return qc_avail_streams(conn, QCS_UNI);
456}
457
458/*****************************************************************/
459/* functions below are dedicated to the mux setup and management */
460/*****************************************************************/
461
462/* Update the mux transport parameter after having received remote transpot parameters */
463void quic_mux_transport_params_update(struct qcc *qcc)
464{
465 if (objt_listener(qcc->conn->target)) {
466 struct quic_transport_params *clt_params;
467
468 /* Client parameters, params used to TX. */
469 clt_params = &qcc->conn->qc->tx.params;
470
471 qcc->tx.max_data = clt_params->initial_max_data;
472 /* Client initiated streams must respect the server flow control. */
473 qcc->strms[QCS_CLT_BIDI].rx.max_data = clt_params->initial_max_stream_data_bidi_local;
474 qcc->strms[QCS_CLT_UNI].rx.max_data = clt_params->initial_max_stream_data_uni;
475
476 /* Server initiated streams must respect the server flow control. */
477 qcc->strms[QCS_SRV_BIDI].max_streams = clt_params->initial_max_streams_bidi;
478 qcc->strms[QCS_SRV_BIDI].tx.max_data = clt_params->initial_max_stream_data_bidi_remote;
479
480 qcc->strms[QCS_SRV_UNI].max_streams = clt_params->initial_max_streams_uni;
481 qcc->strms[QCS_SRV_UNI].tx.max_data = clt_params->initial_max_stream_data_uni;
482 }
483 else {
484 struct quic_transport_params *srv_params;
485
486 /* server parameters, TX params. */
487 srv_params = &qcc->conn->qc->tx.params;
488
489 qcc->tx.max_data = srv_params->initial_max_data;
490 /* Client initiated streams must respect the server flow control. */
491 qcc->strms[QCS_CLT_BIDI].max_streams = srv_params->initial_max_streams_bidi;
492 qcc->strms[QCS_CLT_BIDI].tx.max_data = srv_params->initial_max_stream_data_bidi_remote;
493
494 qcc->strms[QCS_CLT_UNI].max_streams = srv_params->initial_max_streams_uni;
495 qcc->strms[QCS_CLT_UNI].tx.max_data = srv_params->initial_max_stream_data_uni;
496
497 /* Server initiated streams must respect the server flow control. */
498 qcc->strms[QCS_SRV_BIDI].rx.max_data = srv_params->initial_max_stream_data_bidi_local;
499 qcc->strms[QCS_SRV_UNI].rx.max_data = srv_params->initial_max_stream_data_uni;
500 }
501
502 /* Now that we have all the flow control information, we can finalize the application
503 * context.
504 */
Amaury Denoyelle71e588c2021-11-12 11:23:29 +0100505 if (qcc->app_ops)
506 qcc->app_ops->finalize(qcc->ctx);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100507}
508
509/* Initialize the mux once it's attached. For outgoing connections, the context
510 * is already initialized before installing the mux, so we detect incoming
511 * connections from the fact that the context is still NULL (even during mux
512 * upgrades). <input> is always used as Input buffer and may contain data. It is
513 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
514 */
515static int qc_init(struct connection *conn, struct proxy *prx,
516 struct session *sess, struct buffer *input)
517{
518 struct qcc *qcc;
519 struct task *t = NULL;
520 void *conn_ctx = conn->ctx;
521
522 TRACE_ENTER(QC_EV_QCC_NEW);
523
524 qcc = pool_alloc(pool_head_qcc);
525 if (!qcc)
526 goto fail_no_qcc;
527
528 if (conn_is_back(conn)) {
529 qcc->flags = QC_CF_IS_BACK;
530 qcc->shut_timeout = qcc->timeout = prx->timeout.server;
531 if (tick_isset(prx->timeout.serverfin))
532 qcc->shut_timeout = prx->timeout.serverfin;
533
534 qcc->px_counters = EXTRA_COUNTERS_GET(prx->extra_counters_be,
535 &qc_stats_module);
536 } else {
537 qcc->flags = QC_CF_NONE;
538 qcc->shut_timeout = qcc->timeout = prx->timeout.client;
539 if (tick_isset(prx->timeout.clientfin))
540 qcc->shut_timeout = prx->timeout.clientfin;
541
542 qcc->px_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
543 &qc_stats_module);
544 }
545
546 qcc->proxy = prx;
547 qcc->task = NULL;
548 if (tick_isset(qcc->timeout)) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200549 t = task_new_here();
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100550 if (!t)
551 goto fail;
552
553 qcc->task = t;
554 t->process = qc_timeout_task;
555 t->context = qcc;
556 t->expire = tick_add(now_ms, qcc->timeout);
557 }
558
Amaury Denoyellecde91122021-09-22 15:28:27 +0200559 qcc->subs = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100560 qcc->wait_event.tasklet = tasklet_new();
561 if (!qcc->wait_event.tasklet)
562 goto fail;
563
564 qcc->wait_event.tasklet->process = qc_io_cb;
565 qcc->wait_event.tasklet->context = qcc;
566 qcc->wait_event.events = 0;
567
568 /* Initialize the context. */
569 qcc->st0 = QC_CS_NOERR;
570 qcc->conn = conn;
571 qcc->conn->qc->qcc = qcc;
572
573 /* Application layer initialization. */
Amaury Denoyelle71e588c2021-11-12 11:23:29 +0100574 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100575
576 /* The transports parameters which control the data sent have been stored
577 * in ->tx.params. The ones which control the received data are stored in
578 * in ->rx.params.
579 */
580 if (objt_listener(qcc->conn->target)) {
581 struct quic_transport_params *srv_params;
582
583 /* Server parameters, params used for RX flow control. */
584 srv_params = &conn->qc->rx.params;
585
586 qcc->rx.max_data = srv_params->initial_max_data;
587 qcc->tx.max_data = 0;
588 /* Client initiated streams must respect the server flow control. */
589 qcc->strms[QCS_CLT_BIDI].max_streams = srv_params->initial_max_streams_bidi;
590 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
591 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
592 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
593 qcc->strms[QCS_CLT_BIDI].tx.max_data = srv_params->initial_max_stream_data_bidi_remote;
594
595 qcc->strms[QCS_CLT_UNI].max_streams = srv_params->initial_max_streams_uni;
596 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
597 qcc->strms[QCS_CLT_UNI].largest_id = -1;
598 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
599 qcc->strms[QCS_CLT_UNI].tx.max_data = srv_params->initial_max_stream_data_uni;
600
601 /* Server initiated streams must respect the server flow control. */
602 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
603 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
604 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
605 qcc->strms[QCS_SRV_BIDI].rx.max_data = srv_params->initial_max_stream_data_bidi_local;
606 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
607
608 qcc->strms[QCS_SRV_UNI].max_streams = 0;
609 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
610 qcc->strms[QCS_SRV_UNI].largest_id = -1;
611 qcc->strms[QCS_SRV_UNI].rx.max_data = srv_params->initial_max_stream_data_uni;
612 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
613 }
614 else {
615 struct quic_transport_params *clt_params;
616
617 /* client parameters, RX params. */
618 clt_params = &conn->qc->rx.params;
619
620 qcc->rx.max_data = clt_params->initial_max_data;
621 qcc->tx.max_data = 0;
622 /* Client initiated streams must respect the server flow control. */
623 qcc->strms[QCS_CLT_BIDI].max_streams = 0;
624 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
625 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
626 qcc->strms[QCS_CLT_BIDI].rx.max_data = clt_params->initial_max_stream_data_bidi_local;
627 qcc->strms[QCS_CLT_BIDI].tx.max_data = 0;
628
629 qcc->strms[QCS_CLT_UNI].max_streams = 0;
630 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
631 qcc->strms[QCS_CLT_UNI].largest_id = -1;
632 qcc->strms[QCS_CLT_UNI].rx.max_data = clt_params->initial_max_stream_data_uni;
633 qcc->strms[QCS_CLT_UNI].tx.max_data = 0;
634
635 /* Server initiated streams must respect the server flow control. */
636 qcc->strms[QCS_SRV_BIDI].max_streams = clt_params->initial_max_streams_bidi;
637 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
638 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
639 qcc->strms[QCS_SRV_BIDI].rx.max_data = 0;
640 qcc->strms[QCS_SRV_BIDI].tx.max_data = clt_params->initial_max_stream_data_bidi_remote;
641
642 qcc->strms[QCS_SRV_UNI].max_streams = clt_params->initial_max_streams_uni;
643 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
644 qcc->strms[QCS_SRV_UNI].largest_id = -1;
645 qcc->strms[QCS_SRV_UNI].rx.max_data = 0;
646 qcc->strms[QCS_SRV_UNI].tx.max_data = clt_params->initial_max_stream_data_uni;
647
648 }
649
650 /* Initialize the streams counters. */
651 qcc->nb_cs = 0;
652 qcc->stream_cnt = 0;
653
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100654 qcc->streams_by_id = EB_ROOT_UNIQUE;
655 LIST_INIT(&qcc->send_list);
656 LIST_INIT(&qcc->fctl_list);
657 LIST_INIT(&qcc->blocked_list);
658 LIST_INIT(&qcc->buf_wait.list);
659 MT_LIST_INIT(&qcc->qcs_rxbuf_wlist);
660
Frédéric Lécaille01abc462021-07-21 09:34:27 +0200661 HA_ATOMIC_STORE(&conn->ctx, qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100662
663 if (t)
664 task_queue(t);
665
666 if (qcc->flags & QC_CF_IS_BACK) {
667 /* FIXME: For outgoing connections we need to immediately allocate streams.
668 * This highly depends on the QUIC application needs.
669 */
670 }
671
672 HA_ATOMIC_ADD(&qcc->px_counters->open_conns, 1);
673 HA_ATOMIC_ADD(&qcc->px_counters->total_conns, 1);
674
675 /* prepare to read something */
676 qcc_restart_reading(qcc, 1);
677 TRACE_LEAVE(QC_EV_QCC_NEW, conn);
678 return 0;
679
680 fail:
681 task_destroy(t);
682 if (qcc->wait_event.tasklet)
683 tasklet_free(qcc->wait_event.tasklet);
684 pool_free(pool_head_qcc, qcc);
685 fail_no_qcc:
686 conn->ctx = conn_ctx; /* restore saved ctx */
687 TRACE_DEVEL("leaving in error", QC_EV_QCC_NEW|QC_EV_QCC_END|QC_EV_QCC_ERR);
688 return -1;
689}
690
691/* returns the stream associated with id <id> or NULL if not found */
692__maybe_unused
693static inline struct qcs *qcc_st_by_id(struct qcc *qcc, int id)
694{
695 struct eb64_node *node;
696
697 node = eb64_lookup(&qcc->streams_by_id, id);
698 if (!node)
699 return (struct qcs *)qc_closed_stream;
700
701 return container_of(node, struct qcs, by_id);
702}
703
704/* release function. This one should be called to free all resources allocated
705 * to the mux.
706 */
707static void qc_release(struct qcc *qcc)
708{
709 struct connection *conn = NULL;
710
711 TRACE_ENTER(QC_EV_QCC_END);
712
713 if (qcc) {
714 /* The connection must be aattached to this mux to be released */
715 if (qcc->conn && qcc->conn->ctx == qcc)
716 conn = qcc->conn;
717
718 TRACE_DEVEL("freeing qcc", QC_EV_QCC_END, conn);
719
720 if (LIST_INLIST(&qcc->buf_wait.list))
721 LIST_DELETE(&qcc->buf_wait.list);
722
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100723 if (qcc->task) {
724 qcc->task->context = NULL;
725 task_wakeup(qcc->task, TASK_WOKEN_OTHER);
726 qcc->task = NULL;
727 }
728 if (qcc->wait_event.tasklet)
729 tasklet_free(qcc->wait_event.tasklet);
730 if (conn && qcc->wait_event.events != 0)
731 conn->xprt->unsubscribe(conn, conn->xprt_ctx, qcc->wait_event.events,
732 &qcc->wait_event);
733
734 HA_ATOMIC_SUB(&qcc->px_counters->open_conns, 1);
735
736 pool_free(pool_head_qcc, qcc);
737 }
738
739 if (conn) {
740 conn->mux = NULL;
741 conn->ctx = NULL;
742 TRACE_DEVEL("freeing conn", QC_EV_QCC_END, conn);
743
744 conn_stop_tracking(conn);
745 conn_full_close(conn);
746 if (conn->destroy_cb)
747 conn->destroy_cb(conn);
748 conn_free(conn);
749 }
750
751 TRACE_LEAVE(QC_EV_QCC_END);
752}
753
754
755/******************************************************/
756/* functions below are for the QUIC protocol processing */
757/******************************************************/
758
759/* attempt to notify the data layer of recv availability */
760__maybe_unused
761static void qcs_notify_recv(struct qcs *qcs)
762{
763 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
764 TRACE_POINT(QC_EV_STRM_WAKE, qcs->qcc->conn, qcs);
765 tasklet_wakeup(qcs->subs->tasklet);
766 qcs->subs->events &= ~SUB_RETRY_RECV;
767 if (!qcs->subs->events)
768 qcs->subs = NULL;
769 }
770}
771
772/* attempt to notify the data layer of send availability */
773__maybe_unused
774static void qcs_notify_send(struct qcs *qcs)
775{
776 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
777 TRACE_POINT(QC_EV_STRM_WAKE, qcs->qcc->conn, qcs);
778 qcs->flags |= QC_SF_NOTIFIED;
779 tasklet_wakeup(qcs->subs->tasklet);
780 qcs->subs->events &= ~SUB_RETRY_SEND;
781 if (!qcs->subs->events)
782 qcs->subs = NULL;
783 }
784 else if (qcs->flags & (QC_SF_WANT_SHUTR | QC_SF_WANT_SHUTW)) {
785 TRACE_POINT(QC_EV_STRM_WAKE, qcs->qcc->conn, qcs);
786 tasklet_wakeup(qcs->shut_tl);
787 }
788}
789
790/* alerts the data layer, trying to wake it up by all means, following
791 * this sequence :
792 * - if the qcs' data layer is subscribed to recv, then it's woken up for recv
793 * - if its subscribed to send, then it's woken up for send
794 * - if it was subscribed to neither, its ->wake() callback is called
795 * It is safe to call this function with a closed stream which doesn't have a
796 * conn_stream anymore.
797 */
798__maybe_unused
799static void qcs_alert(struct qcs *qcs)
800{
801 TRACE_ENTER(QC_EV_QCS_WAKE, qcs->qcc->conn, qcs);
802
803 if (qcs->subs ||
804 (qcs->flags & (QC_SF_WANT_SHUTR | QC_SF_WANT_SHUTW))) {
805 qcs_notify_recv(qcs);
806 qcs_notify_send(qcs);
807 }
808 else if (qcs->cs && qcs->cs->data_cb->wake != NULL) {
809 TRACE_POINT(QC_EV_STRM_WAKE, qcs->qcc->conn, qcs);
810 qcs->cs->data_cb->wake(qcs->cs);
811 }
812
813 TRACE_LEAVE(QC_EV_QCS_WAKE, qcs->qcc->conn, qcs);
814}
815
816/* marks stream <qcs> as CLOSED and decrement the number of active streams for
817 * its connection if the stream was not yet closed. Please use this exclusively
818 * before closing a stream to ensure stream count is well maintained.
819 */
820static inline void qcs_close(struct qcs *qcs)
821{
822 TRACE_ENTER(QC_EV_QCS_END, qcs->qcc->conn, qcs);
823 /* XXX TO DO XXX */
824 TRACE_LEAVE(QC_EV_QCS_END, qcs->qcc->conn, qcs);
825}
826
827/* detaches an QUIC stream from its QCC and releases it to the QCS pool. */
828/* qcs_destroy should only ever be called by the thread that owns the stream,
829 * that means that a tasklet should be used if we want to destroy the qcs
830 * from another thread
831 */
832static void qcs_destroy(struct qcs *qcs)
833{
834 struct connection *conn = qcs->qcc->conn;
835
836 TRACE_ENTER(QC_EV_QCS_END, conn, qcs);
837
838 qcs_close(qcs);
839 eb64_delete(&qcs->by_id);
840 if (b_size(&qcs->rx.buf)) {
841 b_free(&qcs->rx.buf);
842 offer_buffers(NULL, 1);
843 }
844
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200845 b_free(&qcs->tx.buf);
846 b_free(&qcs->tx.xprt_buf);
847
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100848 if (qcs->subs)
849 qcs->subs->events = 0;
850
851 /* There's no need to explicitly call unsubscribe here, the only
852 * reference left would be in the qcc send_list/fctl_list, and if
853 * we're in it, we're getting out anyway
854 */
855 LIST_DEL_INIT(&qcs->list);
Amaury Denoyelled595f102021-09-24 10:05:30 +0200856 --qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100857
858 /* ditto, calling tasklet_free() here should be ok */
859 tasklet_free(qcs->shut_tl);
860 pool_free(pool_head_qcs, qcs);
861
862 TRACE_LEAVE(QC_EV_QCS_END, conn);
863}
864
865/* allocates a new bidirection stream <id> for connection <qcc> and adds it into qcc's
866 * stream tree. In case of error, nothing is added and NULL is returned. The
867 * causes of errors can be any failed memory allocation. The caller is
868 * responsible for checking if the connection may support an extra stream
869 * prior to calling this function.
870 */
871struct qcs *bidi_qcs_new(struct qcc *qcc, uint64_t id)
872{
873 struct qcs *qcs;
874 enum qcs_type qcs_type;
875
876 TRACE_ENTER(QC_EV_QCS_NEW, qcc->conn);
877
878 qcs = pool_alloc(pool_head_qcs);
879 if (!qcs)
880 goto out;
881
882 qcs->shut_tl = tasklet_new();
883 if (!qcs->shut_tl) {
884 pool_free(pool_head_qcs, qcs);
885 goto out;
886 }
887
888 qcs_type = qcs_id_type(id);
889 qcs->qcc = qcc;
890 qcs->cs = NULL;
891 qcs->id = qcs->by_id.key = id;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100892 qcs->flags = QC_SF_NONE;
893
894 qcs->rx.buf = BUF_NULL;
895 qcs->rx.st = QC_RX_SS_IDLE;
896 qcs->rx.bytes = qcs->rx.offset = 0;
897 qcs->rx.max_data = qcc->strms[qcs_type].rx.max_data;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100898 qcs->rx.buf = BUF_NULL;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200899 qcs->rx.frms = EB_ROOT_UNIQUE;
900
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100901 qcs->tx.st = QC_TX_SS_IDLE;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200902 qcs->tx.bytes = qcs->tx.offset = qcs->tx.ack_offset = 0;
903 qcs->tx.acked_frms = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100904 qcs->tx.max_data = qcc->strms[qcs_type].tx.max_data;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200905 qcs->tx.buf = BUF_NULL;
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200906 qcs->tx.xprt_buf = BUF_NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100907
908 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
909 qcc->strms[qcs_type].nb_streams++;
910 qcc->stream_cnt++;
911 qcs->subs = NULL;
912 LIST_INIT(&qcs->list);
913 qcs->shut_tl->process = qc_deferred_shut;
914 qcs->shut_tl->context = qcs;
915
916 HA_ATOMIC_ADD(&qcc->px_counters->open_streams, 1);
917 HA_ATOMIC_ADD(&qcc->px_counters->total_streams, 1);
918
919 TRACE_LEAVE(QC_EV_QCS_NEW, qcc->conn, qcs);
920 return qcs;
921
922 out:
923 TRACE_DEVEL("leaving in error", QC_EV_QCS_ERR|QC_EV_QCS_END, qcc->conn);
924 return NULL;
925}
926
927/* Release <qcs> outgoing uni-stream */
928void qcs_release(struct qcs *qcs)
929{
930 eb64_delete(&qcs->by_id);
931 pool_free(pool_head_qcs, qcs);
932}
933
934/* Allocates a locally initiated unidirectional stream. */
935struct qcs *luqs_new(struct qcc *qcc)
936{
937 struct qcs *qcs;
938 uint64_t next_id;
939 enum qcs_type qcs_type;
940
941 TRACE_ENTER(QC_EV_QCS_NEW, qcc->conn);
942
943 qcs = NULL;
944 /* QCS_ID_DIR_BIT bit is set for unidirectional stream. */
945 if (objt_listener(qcc->conn->target))
946 qcs_type = QCS_ID_SRV_INTIATOR_BIT | QCS_ID_DIR_BIT;
947 else
948 qcs_type = QCS_ID_DIR_BIT;
949
950 next_id = qcs_next_id(qcc, qcs_type);
951 if (next_id == (uint64_t)-1) {
952 TRACE_PROTO("No more stream available", QC_EV_QCS_NEW, qcc->conn);
953 goto out;
954 }
955
956 qcs = pool_alloc(pool_head_qcs);
957 if (!qcs)
958 goto out;
959
960 qcs->qcc = qcc;
961 qcs->cs = NULL;
962 qcs->id = qcs->by_id.key = next_id;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100963 qcs->flags = QC_SF_NONE;
964
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200965 qcs->tx.st = QC_TX_SS_IDLE;
966 qcs->tx.max_data = qcc->strms[qcs_type].tx.max_data;
967 qcs->tx.offset = qcs->tx.bytes = qcs->tx.ack_offset = 0;
968 qcs->tx.acked_frms = EB_ROOT_UNIQUE;
969 qcs->tx.buf = BUF_NULL;
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200970 qcs->tx.xprt_buf = BUF_NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100971
972 qcs->subs = NULL;
973 LIST_INIT(&qcs->list);
Amaury Denoyelle139814a2021-09-24 10:03:16 +0200974 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100975
976 TRACE_LEAVE(QC_EV_QCS_NEW, qcc->conn);
977 return qcs;
978
979 out:
980 if (qcs)
981 pool_free(pool_head_qcs, qcs);
982 TRACE_DEVEL("leaving in error", QC_EV_QCS_ERR|QC_EV_QCS_END, qcc->conn);
983 return NULL;
984}
985
986/* Allocates a remotely initiated unidirectional stream. */
987struct qcs *ruqs_new(struct qcc *qcc, uint64_t id)
988{
989 struct qcs *qcs;
990 enum qcs_type qcs_type;
991
992 TRACE_ENTER(QC_EV_QCS_NEW, qcc->conn);
993 qcs = pool_alloc(pool_head_qcs);
994 if (!qcs)
995 goto out;
996
997 qcs_type = qcs_id_type(id);
998
999 qcs->qcc = qcc;
1000 qcs->cs = NULL;
1001
1002 qcs->qcc = qcc;
1003 qcs->id = qcs->by_id.key = id;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001004 qcs->flags = QC_SF_NONE;
1005
1006 qcs->rx.st = QC_RX_SS_IDLE;
1007 qcs->rx.max_data = qcc->strms[qcs_type].rx.max_data;
1008 qcs->rx.offset = qcs->rx.bytes = 0;
1009 qcs->rx.buf = BUF_NULL;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001010 qcs->rx.frms = EB_ROOT_UNIQUE;
Amaury Denoyelled3d97c62021-10-05 11:45:58 +02001011 qcs->tx.buf = BUF_NULL;
1012 qcs->tx.xprt_buf = BUF_NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001013
1014 qcs->subs = NULL;
1015 LIST_INIT(&qcs->list);
Amaury Denoyelle139814a2021-09-24 10:03:16 +02001016 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001017
1018 TRACE_LEAVE(QC_EV_QCS_NEW, qcc->conn);
1019 return qcs;
1020
1021 out:
1022 TRACE_DEVEL("leaving in error", QC_EV_QCS_ERR|QC_EV_QCS_END, qcc->conn);
1023 return NULL;
1024}
1025
1026/* attempt to notify the data layer of recv availability */
1027void ruqs_notify_recv(struct qcs *qcs)
1028{
1029 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
1030 TRACE_POINT(QC_EV_STRM_WAKE, qcs->qcc->conn);
1031 tasklet_wakeup(qcs->subs->tasklet);
1032 qcs->subs->events &= ~SUB_RETRY_RECV;
1033 if (!qcs->subs->events)
1034 qcs->subs = NULL;
1035 }
1036}
1037
1038/* Allocates a new stream associated to conn_stream <cs> on the qcc connection
1039 * with dir as direction and returns it, or NULL in case of memory allocation
1040 * error or if the highest possible stream ID was reached.
1041 */
1042static struct qcs *qcc_bck_stream_new(struct qcc *qcc, int dir,
1043 struct conn_stream *cs, struct session *sess)
1044{
1045 struct qcs *qcs = NULL;
1046 enum qcs_type qcs_type;
1047
1048 TRACE_ENTER(QC_EV_QCS_NEW, qcc->conn);
1049
1050 qcs_type = qcs_type_from_dir(qcc, dir);
1051 if (qcc->strms[qcs_type].largest_id + 1 >= qcc->strms[qcs_type].max_streams)
1052 goto out;
1053
1054 /* Defer choosing the ID until we send the first message to create the stream */
1055 qcs = bidi_qcs_new(qcc, qcc->strms[qcs_type].largest_id + 1);
1056 if (!qcs)
1057 goto out;
1058
1059 qcs->cs = cs;
1060 qcs->sess = sess;
1061 cs->ctx = qcs;
1062 qcc->nb_cs++;
1063
1064 out:
1065 if (likely(qcs))
1066 TRACE_LEAVE(QC_EV_QCS_NEW, qcc->conn, qcs);
1067 else
1068 TRACE_LEAVE(QC_EV_QCS_NEW|QC_EV_QCS_ERR|QC_EV_QCS_END, qcc->conn, qcs);
1069 return qcs;
1070}
1071
1072/* Allocates a new bidirectional stream associated to conn_stream <cs> on the <qcc> connection
1073 * and returns it, or NULL in case of memory allocation error or if the highest
1074 * possible stream ID was reached.
1075 */
1076__maybe_unused
1077static struct qcs *qcc_bck_stream_new_bidi(struct qcc *qcc,
1078 struct conn_stream *cs, struct session *sess)
1079{
1080 return qcc_bck_stream_new(qcc, QCS_BIDI, cs, sess);
1081}
1082
1083/* Allocates a new unidirectional stream associated to conn_stream <cs> on the <qcc> connection
1084 * and returns it, or NULL in case of memory allocation error or if the highest
1085 * possible stream ID was reached.
1086 */
1087__maybe_unused
1088static struct qcs *qcc_bck_stream_new_uni(struct qcc *qcc,
1089 struct conn_stream *cs, struct session *sess)
1090{
1091 return qcc_bck_stream_new(qcc, QCS_UNI, cs, sess);
1092}
1093
1094
1095/* wake a specific stream and assign its conn_stream some CS_FL_* flags among
1096 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state
1097 * is automatically updated accordingly. If the stream is orphaned, it is
1098 * destroyed.
1099 */
1100static void qcs_wake_one_stream(struct qcs *qcs)
1101{
1102 struct qcc *qcc = qcs->qcc;
1103
1104 TRACE_ENTER(QC_EV_QCS_WAKE, qcc->conn, qcs);
1105 if (!qcs->cs) {
1106 /* this stream was already orphaned */
1107 qcs_destroy(qcs);
1108 TRACE_DEVEL("leaving with no qcs", QC_EV_QCS_WAKE, qcc->conn);
1109 return;
1110 }
1111 /* XXX TO DO XXX */
1112 TRACE_LEAVE(QC_EV_QCS_WAKE, qcc->conn);
1113}
1114
1115/* wake the streams attached to the connection, whose id is greater than <last>
1116 * or unassigned.
1117 */
1118static void qc_wake_some_streams(struct qcc *qcc, int last)
1119{
1120 struct eb64_node *node;
1121 struct qcs *qcs;
1122
1123 TRACE_ENTER(QC_EV_QCS_WAKE, qcc->conn);
1124
1125 /* Wake all streams with ID > last */
1126 node = eb64_lookup_ge(&qcc->streams_by_id, last + 1);
1127 while (node) {
1128 qcs = container_of(node, struct qcs, by_id);
1129 node = eb64_next(node);
1130 qcs_wake_one_stream(qcs);
1131 }
1132
1133 /* Wake all streams with unassigned ID (ID == 0) */
1134 node = eb64_lookup(&qcc->streams_by_id, 0);
1135 while (node) {
1136 qcs = container_of(node, struct qcs, by_id);
1137 if (qcs->id > 0)
1138 break;
1139 node = eb64_next(node);
1140 qcs_wake_one_stream(qcs);
1141 }
1142
1143 TRACE_LEAVE(QC_EV_QCS_WAKE, qcc->conn);
1144}
1145
1146/* Wake up all blocked streams whose window size has become positive after the
1147 * mux's initial window was adjusted. This should be done after having processed
1148 * SETTINGS frames which have updated the mux's initial window size.
1149 */
1150__maybe_unused
1151static void qcc_unblock_sfctl(struct qcc *qcc)
1152{
1153 TRACE_ENTER(QC_EV_QCC_WAKE, qcc->conn);
1154 /* XXX TO DO XXX */
1155 TRACE_LEAVE(QC_EV_QCC_WAKE, qcc->conn);
1156}
1157
1158/* process Rx frames to be demultiplexed */
1159__maybe_unused
1160static void qc_process_demux(struct qcc *qcc)
1161{
1162 TRACE_ENTER(QC_EV_QCC_WAKE, qcc->conn);
1163 /* XXX TO DO XXX */
1164 TRACE_LEAVE(QC_EV_QCC_WAKE, qcc->conn);
1165}
1166
1167/* resume each qcs eligible for sending in list head <head> */
1168__maybe_unused
1169static void qc_resume_each_sending_qcs(struct qcc *qcc, struct list *head)
1170{
1171 struct qcs *qcs, *qcs_back;
1172
1173 TRACE_ENTER(QC_EV_QCC_SEND|QC_EV_QCS_WAKE, qcc->conn);
1174
1175 list_for_each_entry_safe(qcs, qcs_back, head, list) {
1176 if (qcc_wnd(qcc) <= 0 ||
1177 qcc->flags & QC_CF_MUX_BLOCK_ANY ||
1178 qcc->st0 >= QC_CS_ERROR)
1179 break;
1180
1181 qcs->flags &= ~QC_SF_BLK_ANY;
1182
1183 if (qcs->flags & QC_SF_NOTIFIED)
1184 continue;
1185
1186 /* If the sender changed his mind and unsubscribed, let's just
1187 * remove the stream from the send_list.
1188 */
1189 if (!(qcs->flags & (QC_SF_WANT_SHUTR|QC_SF_WANT_SHUTW)) &&
1190 (!qcs->subs || !(qcs->subs->events & SUB_RETRY_SEND))) {
1191 LIST_DEL_INIT(&qcs->list);
1192 continue;
1193 }
1194
1195 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
1196 qcs->flags |= QC_SF_NOTIFIED;
1197 tasklet_wakeup(qcs->subs->tasklet);
1198 qcs->subs->events &= ~SUB_RETRY_SEND;
1199 if (!qcs->subs->events)
1200 qcs->subs = NULL;
1201 }
1202 else if (qcs->flags & (QC_SF_WANT_SHUTR|QC_SF_WANT_SHUTW)) {
1203 tasklet_wakeup(qcs->shut_tl);
1204 }
1205 }
1206
1207 TRACE_LEAVE(QC_EV_QCC_SEND|QC_EV_QCS_WAKE, qcc->conn);
1208}
1209
1210/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
1211 * the end.
1212 */
1213__maybe_unused
1214static int qc_process_mux(struct qcc *qcc)
1215{
1216 TRACE_ENTER(QC_EV_QCC_WAKE, qcc->conn);
Frédéric Lécaille1d402402021-09-20 17:53:17 +02001217
1218 /* First we always process the flow control list because the streams
1219 * waiting there were already elected for immediate emission but were
1220 * blocked just on this.
1221 */
1222 qc_resume_each_sending_qcs(qcc, &qcc->fctl_list);
1223 qc_resume_each_sending_qcs(qcc, &qcc->send_list);
1224
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001225 TRACE_LEAVE(QC_EV_QCC_WAKE, qcc->conn);
Frédéric Lécaille1d402402021-09-20 17:53:17 +02001226 return 1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001227}
1228
1229
1230/* Attempt to read data, and subscribe if none available.
1231 * The function returns 1 if data has been received, otherwise zero.
1232 */
1233__maybe_unused
1234static int qc_recv(struct qcc *qcc)
1235{
1236 TRACE_ENTER(QC_EV_QCC_RECV, qcc->conn);
1237 /* XXX TO DO XXX */
1238 TRACE_LEAVE(QC_EV_QCC_RECV, qcc->conn);
1239 return 0;
1240}
1241
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001242static int qcs_push_frame(struct qcs *qcs, struct buffer *payload, int fin, uint64_t offset)
1243{
1244 struct quic_frame *frm;
Amaury Denoyelled3d97c62021-10-05 11:45:58 +02001245 struct buffer *buf = &qcs->tx.xprt_buf;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001246 struct quic_enc_level *qel = &qcs->qcc->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001247 int total = 0, to_xfer;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001248
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02001249 qc_get_buf(qcs, buf);
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001250 to_xfer = QUIC_MIN(b_data(payload), b_room(buf));
1251 if (!to_xfer)
1252 goto out;
1253
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001254 frm = pool_zalloc(pool_head_quic_frame);
1255 if (!frm)
1256 goto err;
1257
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001258 total = b_force_xfer(buf, payload, to_xfer);
1259 fin = fin && !b_data(payload);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001260 frm->type = QUIC_FT_STREAM_8;
1261 if (fin)
1262 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
1263 if (offset) {
1264 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
1265 frm->stream.offset.key = offset;
1266 }
1267 frm->stream.qcs = qcs;
1268 frm->stream.buf = buf;
1269 frm->stream.id = qcs->by_id.key;
1270 if (total) {
1271 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
1272 frm->stream.len = total;
1273 }
1274
1275 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001276 out:
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001277 fprintf(stderr, "%s: total=%d fin=%d offset=%lu\n", __func__, total, fin, offset);
1278 return total;
1279
1280 err:
1281 return -1;
1282}
1283
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001284/* Try to send data if possible.
1285 * The function returns 1 if data have been sent, otherwise zero.
1286 */
1287static int qc_send(struct qcc *qcc)
1288{
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001289 struct eb64_node *node;
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001290 int ret, done, xprt_wake = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001291
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001292 TRACE_ENTER(QC_EV_QCC_SEND, qcc->conn);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001293 ret = done = 0;
1294 /* fill as much as we can into the current buffer */
1295 while (((qcc->flags & (QC_CF_MUX_MFULL|QC_CF_MUX_MALLOC)) == 0) && !done)
1296 done = qc_process_mux(qcc);
1297
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001298 /* TODO simple loop through all streams and check if there is frames to
1299 * send
1300 */
1301 node = eb64_first(&qcc->streams_by_id);
1302 while (node) {
Amaury Denoyelled3d97c62021-10-05 11:45:58 +02001303 struct qcs *qcs = container_of(node, struct qcs, by_id);
1304 struct buffer *buf = &qcs->tx.buf;
1305 if (b_data(buf)) {
1306 char fin = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001307
Amaury Denoyelled3d97c62021-10-05 11:45:58 +02001308 /* if FIN is activated, ensure the buffer to
1309 * send is the last
1310 */
1311 if (qcs->flags & QC_SF_FIN_STREAM) {
1312 BUG_ON(b_data(&qcs->tx.buf) < b_data(buf));
1313 fin = (b_data(&qcs->tx.buf) - b_data(buf) == 0);
1314 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001315
Amaury Denoyelled3d97c62021-10-05 11:45:58 +02001316 ret = qcs_push_frame(qcs, buf, fin, qcs->tx.offset);
1317 if (ret < 0)
1318 ABORT_NOW();
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001319
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001320 if (ret > 0) {
1321 xprt_wake = 1;
1322 if (qcs->flags & QC_SF_BLK_MROOM) {
1323 qcs->flags &= ~QC_SF_BLK_MROOM;
1324 qcs_notify_send(qcs);
1325 }
1326 }
1327
Amaury Denoyelled3d97c62021-10-05 11:45:58 +02001328 qcs->tx.offset += ret;
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001329
Amaury Denoyelled3d97c62021-10-05 11:45:58 +02001330 if (b_data(buf)) {
1331 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1332 SUB_RETRY_SEND, &qcc->wait_event);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001333 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001334 }
1335 node = eb64_next(node);
1336 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001337
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001338 if (xprt_wake)
1339 tasklet_wakeup(((struct ssl_sock_ctx *)(qcc->conn->xprt_ctx))->wait_event.tasklet);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001340
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001341 TRACE_LEAVE(QC_EV_QCC_SEND, qcc->conn);
1342 return 0;
1343}
1344
1345/* this is the tasklet referenced in qcc->wait_event.tasklet */
1346static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1347{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001348 struct qcc *qcc = ctx;
Amaury Denoyelle7bb54f92021-11-08 08:58:26 +01001349 //int ret = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001350
1351
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001352 if (!(qcc->wait_event.events & SUB_RETRY_SEND))
Amaury Denoyelle7bb54f92021-11-08 08:58:26 +01001353 //ret = qc_send(qcc);
1354 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001355#if 0
1356 if (!(qcc->wait_event.events & SUB_RETRY_RECV))
1357 ret |= qc_recv(qcc);
1358#endif
1359 // TODO redefine the proper condition here
1360 //if (ret || qcc->rx.inmux)
Amaury Denoyelle7bb54f92021-11-08 08:58:26 +01001361 //ret = qc_process(qcc);
1362 qc_process(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001363
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001364leave:
1365 TRACE_LEAVE(QC_EV_QCC_WAKE);
1366 return NULL;
1367}
1368
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001369/* callback called on any event by the connection handler.
1370 * It applies changes and returns zero, or < 0 if it wants immediate
1371 * destruction of the connection (which normally doesn not happen in quic).
1372 */
1373static int qc_process(struct qcc *qcc)
1374{
1375 struct connection *conn = qcc->conn;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001376
1377 TRACE_ENTER(QC_EV_QCC_WAKE, conn);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001378 TRACE_LEAVE(QC_EV_QCC_WAKE, conn);
1379 return 0;
1380}
1381
1382/* wake-up function called by the connection layer (mux_ops.wake) */
1383static int qc_wake(struct connection *conn)
1384{
1385 struct qcc *qcc = conn->ctx;
1386 int ret;
1387
1388 TRACE_ENTER(QC_EV_QCC_WAKE, conn);
1389 ret = qc_process(qcc);
1390 if (ret >= 0)
1391 qc_wake_some_streams(qcc, 0);
1392 TRACE_LEAVE(QC_EV_QCC_WAKE);
1393 return ret;
1394}
1395
1396/* Connection timeout management. The principle is that if there's no receipt
1397 * nor sending for a certain amount of time, the connection is closed. If the
1398 * MUX buffer still has lying data or is not allocatable, the connection is
1399 * immediately killed. If it's allocatable and empty, we attempt to send a
1400 * GOAWAY frame.
1401 */
1402static struct task *qc_timeout_task(struct task *t, void *context, unsigned int state)
1403{
1404 TRACE_ENTER(QC_EV_QCC_WAKE);
1405 /* XXX TO DO XXX */
1406 TRACE_LEAVE(QC_EV_QCC_WAKE);
1407 return NULL;
1408}
1409
1410
1411/*******************************************/
1412/* functions below are used by the streams */
1413/*******************************************/
1414
1415/*
1416 * Attach a new stream to a connection
1417 * (Used for outgoing connections)
1418 */
1419static struct conn_stream *qc_attach(struct connection *conn, struct session *sess)
1420{
1421 struct conn_stream *cs;
1422 struct qcs *qcs;
1423 struct qcc *qcc = conn->ctx;
1424
1425 TRACE_ENTER(QC_EV_QCS_NEW, conn);
1426 cs = cs_new(conn, conn->target);
1427 if (!cs) {
1428 TRACE_DEVEL("leaving on CS allocation failure", QC_EV_QCS_NEW|QC_EV_QCS_ERR, conn);
1429 return NULL;
1430 }
1431 qcs = qcc_bck_stream_new(qcc, QCS_BIDI, cs, sess);
1432 if (!qcs) {
1433 TRACE_DEVEL("leaving on stream creation failure", QC_EV_QCS_NEW|QC_EV_QCS_ERR, conn);
1434 cs_free(cs);
1435 return NULL;
1436 }
1437 TRACE_LEAVE(QC_EV_QCS_NEW, conn, qcs);
1438 return cs;
1439}
1440
1441/* Retrieves the first valid conn_stream from this connection, or returns NULL.
1442 * We have to scan because we may have some orphan streams. It might be
1443 * beneficial to scan backwards from the end to reduce the likeliness to find
1444 * orphans.
1445 */
1446static const struct conn_stream *qc_get_first_cs(const struct connection *conn)
1447{
1448 struct qcc *qcc = conn->ctx;
1449 struct qcs *qcs;
1450 struct eb64_node *node;
1451
1452 node = eb64_first(&qcc->streams_by_id);
1453 while (node) {
1454 qcs = container_of(node, struct qcs, by_id);
1455 if (qcs->cs)
1456 return qcs->cs;
1457 node = eb64_next(node);
1458 }
1459 return NULL;
1460}
1461
1462static int qc_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
1463{
1464 int ret = 0;
1465 struct qcc *qcc = conn->ctx;
1466
1467 switch (mux_ctl) {
1468 case MUX_STATUS:
1469 /* Only consider the mux to be ready if we had no error. */
1470 if (qcc->st0 < QC_CS_ERROR)
1471 ret |= MUX_STATUS_READY;
1472 return ret;
1473 case MUX_EXIT_STATUS:
1474 return MUX_ES_UNKNOWN;
1475 default:
1476 return -1;
1477 }
1478}
1479
1480/*
1481 * Destroy the mux and the associated connection, if it is no longer used
1482 */
1483static void qc_destroy(void *ctx)
1484{
1485 struct qcc *qcc = ctx;
1486
1487 TRACE_ENTER(QC_EV_QCC_END, qcc->conn);
1488 if (eb_is_empty(&qcc->streams_by_id) || !qcc->conn || qcc->conn->ctx != qcc)
1489 qc_release(qcc);
1490 TRACE_LEAVE(QC_EV_QCC_END);
1491}
1492
1493/*
1494 * Detach the stream from the connection and possibly release the connection.
1495 */
1496static void qc_detach(struct conn_stream *cs)
1497{
1498 struct qcs *qcs = cs->ctx;
Amaury Denoyellecd28b272021-09-22 14:48:32 +02001499 struct qcc *qcc = qcs->qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001500
1501 TRACE_ENTER(QC_EV_STRM_END, qcs ? qcs->qcc->conn : NULL, qcs);
Amaury Denoyelle493bb1d2021-10-13 16:34:49 +02001502 if (b_data(&qcs->tx.buf) ||
1503 (b_data(&qcs->tx.xprt_buf) && !(qcc->flags & QC_CF_CC_RECV))) {
Amaury Denoyellecae07912021-10-08 17:57:41 +02001504 qcs->flags |= QC_SF_DETACH;
1505 goto out;
1506 }
1507
Amaury Denoyellecd28b272021-09-22 14:48:32 +02001508 qcs_destroy(qcs);
Amaury Denoyelle493bb1d2021-10-13 16:34:49 +02001509 if (qcc_is_dead(qcc)) {
Amaury Denoyellecd28b272021-09-22 14:48:32 +02001510 qc_release(qcc);
Amaury Denoyelle493bb1d2021-10-13 16:34:49 +02001511 TRACE_LEAVE(QC_EV_STRM_END, NULL);
1512 return;
1513 }
Amaury Denoyellecae07912021-10-08 17:57:41 +02001514
1515 out:
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001516 TRACE_LEAVE(QC_EV_STRM_END, qcs ? qcs->qcc->conn : NULL);
1517}
1518
1519/* Performs a synchronous or asynchronous shutr(). */
1520static void qc_do_shutr(struct qcs *qcs)
1521{
1522 struct qcc *qcc = qcs->qcc;
1523
1524 TRACE_ENTER(QC_EV_STRM_SHUT, qcc->conn, qcs);
1525 /* XXX TO DO XXX */
1526 TRACE_LEAVE(QC_EV_STRM_SHUT, qcc->conn, qcs);
1527 return;
1528}
1529
1530/* Performs a synchronous or asynchronous shutw(). */
1531static void qc_do_shutw(struct qcs *qcs)
1532{
1533 struct qcc *qcc = qcs->qcc;
1534
1535 TRACE_ENTER(QC_EV_STRM_SHUT, qcc->conn, qcs);
1536 /* XXX TO DO XXX */
1537 TRACE_LEAVE(QC_EV_STRM_SHUT, qcc->conn, qcs);
1538 return;
1539}
1540
1541/* This is the tasklet referenced in qcs->shut_tl, it is used for
1542 * deferred shutdowns when the qc_detach() was done but the mux buffer was full
1543 * and prevented the last frame from being emitted.
1544 */
1545static struct task *qc_deferred_shut(struct task *t, void *ctx, unsigned int state)
1546{
1547 struct qcs *qcs = ctx;
1548 struct qcc *qcc = qcs->qcc;
1549
1550 TRACE_ENTER(QC_EV_STRM_SHUT, qcc->conn, qcs);
1551
Amaury Denoyellecae07912021-10-08 17:57:41 +02001552 if (qcs->flags & QC_SF_NOTIFIED ||
1553 (b_data(&qcs->tx.buf) || b_data(&qcs->tx.xprt_buf))) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001554 /* some data processing remains to be done first */
1555 goto end;
1556 }
1557
1558 if (qcs->flags & QC_SF_WANT_SHUTW)
1559 qc_do_shutw(qcs);
1560
1561 if (qcs->flags & QC_SF_WANT_SHUTR)
1562 qc_do_shutr(qcs);
1563
1564 if (!(qcs->flags & (QC_SF_WANT_SHUTR|QC_SF_WANT_SHUTW))) {
1565 /* We're done trying to send, remove ourself from the send_list */
1566 LIST_DEL_INIT(&qcs->list);
1567
1568 if (!qcs->cs) {
1569 qcs_destroy(qcs);
1570 if (qcc_is_dead(qcc))
1571 qc_release(qcc);
1572 }
1573 }
1574
1575 end:
1576 TRACE_LEAVE(QC_EV_STRM_SHUT);
1577 return NULL;
1578}
1579
1580/* shutr() called by the conn_stream (mux_ops.shutr) */
1581static void qc_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
1582{
1583
1584 struct qcs *qcs = cs->ctx;
1585
1586 TRACE_ENTER(QC_EV_STRM_SHUT, qcs->qcc->conn, qcs);
1587 if (cs->flags & CS_FL_KILL_CONN)
1588 qcs->flags |= QC_SF_KILL_CONN;
1589
1590 if (mode)
1591 qc_do_shutr(qcs);
1592
1593 TRACE_LEAVE(QC_EV_STRM_SHUT, qcs->qcc->conn, qcs);
1594}
1595
1596/* shutw() called by the conn_stream (mux_ops.shutw) */
1597static void qc_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
1598{
1599 struct qcs *qcs = cs->ctx;
1600
1601 TRACE_ENTER(QC_EV_STRM_SHUT, qcs->qcc->conn, qcs);
1602 if (cs->flags & CS_FL_KILL_CONN)
1603 qcs->flags |= QC_SF_KILL_CONN;
1604
1605 qc_do_shutw(qcs);
1606 TRACE_LEAVE(QC_EV_STRM_SHUT, qcs->qcc->conn, qcs);
1607}
1608
1609/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1610 * event subscriber <es> is not allowed to change from a previous call as long
1611 * as at least one event is still subscribed. The <event_type> must only be a
1612 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1613 */
1614static int qc_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
1615{
1616 struct qcs *qcs = cs->ctx;
1617 struct qcc *qcc = qcs->qcc;
1618
1619 TRACE_ENTER(QC_EV_STRM_SEND|QC_EV_STRM_RECV, qcc->conn, qcs);
1620
1621 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1622 BUG_ON(qcs->subs && qcs->subs != es);
1623
1624 es->events |= event_type;
1625 qcs->subs = es;
1626
1627 if (event_type & SUB_RETRY_RECV)
1628 TRACE_DEVEL("subscribe(recv)", QC_EV_STRM_RECV, qcc->conn, qcs);
1629
1630 if (event_type & SUB_RETRY_SEND) {
1631 TRACE_DEVEL("subscribe(send)", QC_EV_STRM_SEND, qcc->conn, qcs);
1632 if (!(qcs->flags & QC_SF_BLK_SFCTL) &&
1633 !LIST_INLIST(&qcs->list)) {
1634 if (qcs->flags & QC_SF_BLK_MFCTL)
1635 LIST_APPEND(&qcc->fctl_list, &qcs->list);
1636 else
1637 LIST_APPEND(&qcc->send_list, &qcs->list);
1638 }
1639 }
1640 TRACE_LEAVE(QC_EV_STRM_SEND|QC_EV_STRM_RECV, qcc->conn, qcs);
1641 return 0;
1642}
1643
1644/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1645 * The <es> pointer is not allowed to differ from the one passed to the
1646 * subscribe() call. It always returns zero.
1647 */
1648static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
1649{
1650 struct qcs *qcs = cs->ctx;
1651
1652 TRACE_ENTER(QC_EV_STRM_SEND|QC_EV_STRM_RECV, qcs->qcc->conn, qcs);
1653
1654 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1655 BUG_ON(qcs->subs && qcs->subs != es);
1656
1657 es->events &= ~event_type;
1658 if (!es->events)
1659 qcs->subs = NULL;
1660
1661 if (event_type & SUB_RETRY_RECV)
1662 TRACE_DEVEL("unsubscribe(recv)", QC_EV_STRM_RECV, qcs->qcc->conn, qcs);
1663
1664 if (event_type & SUB_RETRY_SEND) {
1665 TRACE_DEVEL("subscribe(send)", QC_EV_STRM_SEND, qcs->qcc->conn, qcs);
1666 qcs->flags &= ~QC_SF_NOTIFIED;
1667 if (!(qcs->flags & (QC_SF_WANT_SHUTR | QC_SF_WANT_SHUTW)))
1668 LIST_DEL_INIT(&qcs->list);
1669 }
1670
1671 TRACE_LEAVE(QC_EV_STRM_SEND|QC_EV_STRM_RECV, qcs->qcc->conn, qcs);
1672 return 0;
1673}
1674
1675
1676/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1677 * event subscriber <es> is not allowed to change from a previous call as long
1678 * as at least one event is still subscribed. The <event_type> must only be a
1679 * SUB_RETRY_RECV. It always returns 0.
1680 */
1681static int ruqs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
1682{
1683 struct qcc *qcc = qcs->qcc;
1684
1685 TRACE_ENTER(QC_EV_STRM_RECV, qcc->conn, qcs);
1686
1687 BUG_ON(event_type & ~SUB_RETRY_RECV);
1688 BUG_ON(qcs->subs && qcs->subs != es);
1689
1690 es->events |= event_type;
1691 qcs->subs = es;
1692
1693 if (event_type & SUB_RETRY_RECV)
1694 TRACE_DEVEL("subscribe(recv)", QC_EV_STRM_RECV, qcc->conn, qcs);
1695
1696 TRACE_LEAVE(QC_EV_STRM_RECV, qcc->conn, qcs);
1697 return 0;
1698}
1699
1700/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1701 * The <es> pointer is not allowed to differ from the one passed to the
1702 * subscribe() call. It always returns zero.
1703 */
1704static int ruqs_unsubscribe(struct qcs *qcs, int event_type, struct wait_event *es)
1705{
1706 TRACE_ENTER(QC_EV_STRM_RECV, qcs->qcc->conn, qcs);
1707
1708 BUG_ON(event_type & ~SUB_RETRY_RECV);
1709 BUG_ON(qcs->subs && qcs->subs != es);
1710
1711 es->events &= ~event_type;
1712 if (!es->events)
1713 qcs->subs = NULL;
1714
1715 if (event_type & SUB_RETRY_RECV)
1716 TRACE_DEVEL("unsubscribe(recv)", QC_EV_STRM_RECV, qcs->qcc->conn, qcs);
1717
1718 TRACE_LEAVE(QC_EV_STRM_RECV, qcs->qcc->conn, qcs);
1719 return 0;
1720}
1721
1722/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1723 * event subscriber <es> is not allowed to change from a previous call as long
1724 * as at least one event is still subscribed. The <event_type> must only be
1725 * SUB_RETRY_SEND. It always returns 0.
1726 */
1727static int luqs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
1728{
1729 struct qcc *qcc = qcs->qcc;
1730
1731 TRACE_ENTER(QC_EV_STRM_SEND, qcc->conn, qcs);
1732
1733 BUG_ON(event_type & ~SUB_RETRY_SEND);
1734 BUG_ON(qcs->subs && qcs->subs != es);
1735
1736 es->events |= event_type;
1737 qcs->subs = es;
1738
1739 if (event_type & SUB_RETRY_SEND) {
1740 TRACE_DEVEL("subscribe(send)", QC_EV_STRM_SEND, qcc->conn, qcs);
1741 if (!(qcs->flags & QC_SF_BLK_SFCTL) &&
1742 !LIST_INLIST(&qcs->list)) {
1743 if (qcs->flags & QC_SF_BLK_MFCTL)
1744 LIST_APPEND(&qcc->fctl_list, &qcs->list);
1745 else
1746 LIST_APPEND(&qcc->send_list, &qcs->list);
1747 }
1748 }
1749
1750 TRACE_LEAVE(QC_EV_STRM_SEND, qcc->conn, qcs);
1751 return 0;
1752}
1753
1754/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1755 * The <es> pointer is not allowed to differ from the one passed to the
1756 * subscribe() call. It always returns zero.
1757 */
1758static int luqs_unsubscribe(struct qcs *qcs, int event_type, struct wait_event *es)
1759{
1760 struct qcc *qcc = qcs->qcc;
1761
1762 TRACE_ENTER(QC_EV_STRM_SEND, qcc->conn, qcs);
1763
1764 BUG_ON(event_type & ~SUB_RETRY_SEND);
1765 BUG_ON(qcs->subs && qcs->subs != es);
1766
1767 es->events &= ~event_type;
1768 if (!es->events)
1769 qcs->subs = NULL;
1770
1771 if (event_type & SUB_RETRY_SEND) {
1772 TRACE_DEVEL("subscribe(send)", QC_EV_STRM_SEND, qcc->conn, qcs);
1773 qcs->flags &= ~QC_SF_NOTIFIED;
1774 if (!(qcs->flags & (QC_SF_WANT_SHUTR | QC_SF_WANT_SHUTW)))
1775 LIST_DEL_INIT(&qcs->list);
1776 }
1777
1778 TRACE_LEAVE(QC_EV_STRM_SEND, qcc->conn, qcs);
1779 return 0;
1780}
1781
1782/* Called from the upper layer, to receive data */
1783static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1784{
1785 struct qcs *qcs = cs->ctx;
1786 struct qcc *qcc = qcs->qcc;
1787 int ret;
1788
1789 ret = 0;
1790 TRACE_ENTER(QC_EV_STRM_RECV, qcc->conn, qcs);
1791 /* XXX TO DO XXX */
1792 TRACE_LEAVE(QC_EV_STRM_RECV, qcc->conn, qcs);
1793 return ret;
1794}
1795
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001796/* for debugging with CLI's "show fd" command */
1797static int qc_show_fd(struct buffer *msg, struct connection *conn)
1798{
1799 struct qcc *qcc = conn->ctx;
1800 struct qcs *qcs = NULL;
1801 struct eb64_node *node;
1802 int fctl_cnt = 0;
1803 int send_cnt = 0;
1804 int tree_cnt = 0;
1805 int orph_cnt = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001806
1807 if (!qcc)
1808 return 0;
1809
1810 list_for_each_entry(qcs, &qcc->fctl_list, list)
1811 fctl_cnt++;
1812
1813 list_for_each_entry(qcs, &qcc->send_list, list)
1814 send_cnt++;
1815
1816 qcs = NULL;
1817 node = eb64_first(&qcc->streams_by_id);
1818 while (node) {
1819 qcs = container_of(node, struct qcs, by_id);
1820 tree_cnt++;
1821 if (!qcs->cs)
1822 orph_cnt++;
1823 node = eb64_next(node);
1824 }
1825
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001826 chunk_appendf(msg, " qcc.st0=%s .flg=0x%04x"
1827 " clt.nb_streams_bidi=%llu srv.nb_streams_bidi=%llu"
1828 " clt.nb_streams_uni=%llu srv.nb_streams_uni=%llu"
1829 " .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02001830 " .orph_cnt=%d .sub=%d",
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001831 qcc_st_to_str(qcc->st0), qcc->flags,
1832 (unsigned long long)qcc->strms[QCS_CLT_BIDI].nb_streams,
1833 (unsigned long long)qcc->strms[QCS_SRV_BIDI].nb_streams,
1834 (unsigned long long)qcc->strms[QCS_CLT_UNI].nb_streams,
1835 (unsigned long long)qcc->strms[QCS_SRV_UNI].nb_streams,
1836 qcc->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02001837 qcc->wait_event.events);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001838
1839 if (qcs) {
1840 chunk_appendf(msg, " last_qcs=%p .id=%llu rx.st=%s tx.st=%s .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
1841 qcs, (unsigned long long)qcs->id,
1842 qcs_rx_st_to_str(qcs->rx.st), qcs_tx_st_to_str(qcs->tx.st), qcs->flags,
1843 (unsigned int)b_data(&qcs->rx.buf), b_orig(&qcs->rx.buf),
1844 (unsigned int)b_head_ofs(&qcs->rx.buf), (unsigned int)b_size(&qcs->rx.buf),
1845 qcs->cs);
1846 if (qcs->cs)
1847 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
1848 qcs->cs->flags, qcs->cs->data);
1849 }
1850
1851 return 0;
1852}
1853
Amaury Denoyelleabbe91e2021-11-12 16:09:29 +01001854static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1855{
1856 struct qcs *qcs = cs->ctx;
1857 return qcs->qcc->app_ops->snd_buf(cs, buf, count, flags);
1858}
1859
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001860/****************************************/
1861/* MUX initialization and instantiation */
1862/***************************************/
1863
1864/* The mux operations */
1865static const struct mux_ops qc_ops = {
1866 .init = qc_init,
1867 .wake = qc_wake,
Amaury Denoyelleabbe91e2021-11-12 16:09:29 +01001868 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001869 .rcv_buf = qc_rcv_buf,
1870 .subscribe = qc_subscribe,
1871 .unsubscribe = qc_unsubscribe,
1872 .ruqs_subscribe = ruqs_subscribe,
1873 .ruqs_unsubscribe = ruqs_unsubscribe,
1874 .luqs_subscribe = luqs_subscribe,
1875 .luqs_unsubscribe = luqs_unsubscribe,
1876 .attach = qc_attach,
1877 .get_first_cs = qc_get_first_cs,
1878 .detach = qc_detach,
1879 .destroy = qc_destroy,
1880 .avail_streams_bidi = qc_avail_streams_bidi,
1881 .avail_streams_uni = qc_avail_streams_uni,
1882 .used_streams = qc_used_streams,
1883 .shutr = qc_shutr,
1884 .shutw = qc_shutw,
1885 .ctl = qc_ctl,
1886 .show_fd = qc_show_fd,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001887 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX|MX_FL_HOL_RISK,
1888 .name = "QUIC",
1889};
1890
1891static struct mux_proto_list mux_proto_quic =
1892 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &qc_ops };
1893
1894INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);
1895