blob: 55a4dc5577703703285e62f4aaf2ff277e84b83b [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
324/* returns true if the connection is allowed to expire, false otherwise. A
325 * connection may expire when:
326 * - it has no stream
327 * - it has data in the mux buffer
328 * - it has streams in the blocked list
329 * - it has streams in the fctl list
330 * - it has streams in the send list
331 * Otherwise it means some streams are waiting in the data layer and it should
332 * not expire.
333 */
334__maybe_unused
335static inline int qcc_may_expire(const struct qcc *qcc)
336{
337 return eb_is_empty(&qcc->streams_by_id) ||
338 br_data(qcc->mbuf) ||
339 !LIST_ISEMPTY(&qcc->blocked_list) ||
340 !LIST_ISEMPTY(&qcc->fctl_list) ||
341 !LIST_ISEMPTY(&qcc->send_list);
342}
343
344static __inline int
345qcc_is_dead(const struct qcc *qcc)
346{
347 if (eb_is_empty(&qcc->streams_by_id) && /* don't close if streams exist */
348 ((qcc->conn->flags & CO_FL_ERROR) || /* errors close immediately */
349 (qcc->st0 >= QC_CS_ERROR && !qcc->task) || /* a timeout stroke earlier */
350 (!(qcc->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
351 (!br_data(qcc->mbuf) && /* mux buffer empty, also process clean events below */
352 conn_xprt_read0_pending(qcc->conn))))
353 return 1;
354
355 return 0;
356}
357
358/*****************************************************/
359/* functions below are for dynamic buffer management */
360/*****************************************************/
361
362/* indicates whether or not the we may call the qc_recv() function to attempt
363 * to receive data into the buffer and/or demux pending data. The condition is
364 * a bit complex due to some API limits for now. The rules are the following :
365 * - if an error or a shutdown was detected on the connection and the buffer
366 * is empty, we must not attempt to receive
367 * - if the demux buf failed to be allocated, we must not try to receive and
368 * we know there is nothing pending
369 * - if no flag indicates a blocking condition, we may attempt to receive,
370 * regardless of whether the demux buffer is full or not, so that only
371 * de demux part decides whether or not to block. This is needed because
372 * the connection API indeed prevents us from re-enabling receipt that is
373 * already enabled in a polled state, so we must always immediately stop
374 * as soon as the demux can't proceed so as never to hit an end of read
375 * with data pending in the buffers.
376 * - otherwise must may not attempt
377 */
378static inline int qc_recv_allowed(const struct qcc *qcc)
379{
380 if (qcc->rx.inmux == 0 &&
381 (qcc->st0 >= QC_CS_ERROR ||
382 qcc->conn->flags & CO_FL_ERROR ||
383 conn_xprt_read0_pending(qcc->conn)))
384 return 0;
385
386 if (!(qcc->flags & QC_CF_DEM_BLOCK_ANY))
387 return 1;
388
389 return 0;
390}
391
392/* restarts reading on the connection if it was not enabled */
393static inline void qcc_restart_reading(const struct qcc *qcc, int consider_buffer)
394{
395 if (!qc_recv_allowed(qcc))
396 return;
397
398 if ((!consider_buffer || !qcc->rx.inmux)
399 && (qcc->wait_event.events & SUB_RETRY_RECV))
400 return;
401
402 tasklet_wakeup(qcc->wait_event.tasklet);
403}
404
405/* Tries to grab a buffer and to re-enable processing on mux <target>. The qcc
406 * flags are used to figure what buffer was requested. It returns 1 if the
407 * allocation succeeds, in which case the connection is woken up, or 0 if it's
408 * impossible to wake up and we prefer to be woken up later.
409 */
410static int qc_buf_available(void *target)
411{
412 struct qcc *qcc = target;
413
414 if ((qcc->flags & QC_CF_MUX_MALLOC) && b_alloc(br_tail(qcc->mbuf))) {
415 qcc->flags &= ~QC_CF_MUX_MALLOC;
416
417 if (qcc->flags & QC_CF_DEM_MROOM) {
418 qcc->flags &= ~QC_CF_DEM_MROOM;
419 qcc_restart_reading(qcc, 1);
420 }
421 return 1;
422 }
423
424#if 0
425 if ((qcc->flags & QC_CF_DEM_SALLOC) &&
426 (qcs = qcc_st_by_id(qcc, qcc->dsi)) && qcs->cs &&
427 b_alloc_margin(&qcs->rxbuf, 0)) {
428 qcc->flags &= ~QC_CF_DEM_SALLOC;
429 qcc_restart_reading(qcc, 1);
430 return 1;
431 }
432#endif
433
434 return 0;
435}
436
437struct buffer *qc_get_buf(struct qcc *qcc, struct buffer *bptr)
438{
439 struct buffer *buf = NULL;
440
441 if (likely(!LIST_INLIST(&qcc->buf_wait.list)) &&
442 unlikely((buf = b_alloc(bptr)) == NULL)) {
443 qcc->buf_wait.target = qcc;
444 qcc->buf_wait.wakeup_cb = qc_buf_available;
Willy Tarreaub4e34762021-09-30 19:02:18 +0200445 LIST_APPEND(&th_ctx->buffer_wq, &qcc->buf_wait.list);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100446 }
447
448 return buf;
449}
450
451__maybe_unused
452static inline void qc_release_buf(struct qcc *qcc, struct buffer *bptr)
453{
454 if (bptr->size) {
455 b_free(bptr);
456 offer_buffers(NULL, 1);
457 }
458}
459
460static inline void qc_release_mbuf(struct qcc *qcc)
461{
462 struct buffer *buf;
463 unsigned int count = 0;
464
465 while (b_size(buf = br_head_pick(qcc->mbuf))) {
466 b_free(buf);
467 count++;
468 }
469 if (count)
470 offer_buffers(NULL, count);
471}
472
473/* returns the number of streams in use on a connection to figure if it's
474 * idle or not. We check nb_cs and not nb_streams as the caller will want
475 * to know if it was the last one after a detach().
476 */
477static int qc_used_streams(struct connection *conn)
478{
479 struct qcc *qcc = conn->ctx;
480
481 return qcc->nb_cs;
482}
483
484/* returns the number of concurrent streams available on the connection with <dir>
485 * as direction
486 */
487static int qc_avail_streams(struct connection *conn, enum qcs_dir dir)
488{
489 struct qcc *qcc = conn->ctx;
490 enum qcs_type qcs_type;
491
492 if (qcc->st0 >= QC_CS_ERROR)
493 return 0;
494
495 qcs_type = qcs_type_from_dir(qcc, dir);
496
497 return qcc->strms[qcs_type].max_streams - qcc->strms[qcs_type].nb_streams;
498}
499
500
501/* returns the number of concurrent bidirectional streams available on the
502 * connection.
503 */
504static int qc_avail_streams_bidi(struct connection *conn)
505{
506 return qc_avail_streams(conn, QCS_BIDI);
507}
508
509/* returns the number of concurrent unidirectional streams available on the
510 * connection.
511 */
512static int qc_avail_streams_uni(struct connection *conn)
513{
514 return qc_avail_streams(conn, QCS_UNI);
515}
516
517/*****************************************************************/
518/* functions below are dedicated to the mux setup and management */
519/*****************************************************************/
520
521/* Update the mux transport parameter after having received remote transpot parameters */
522void quic_mux_transport_params_update(struct qcc *qcc)
523{
524 if (objt_listener(qcc->conn->target)) {
525 struct quic_transport_params *clt_params;
526
527 /* Client parameters, params used to TX. */
528 clt_params = &qcc->conn->qc->tx.params;
529
530 qcc->tx.max_data = clt_params->initial_max_data;
531 /* Client initiated streams must respect the server flow control. */
532 qcc->strms[QCS_CLT_BIDI].rx.max_data = clt_params->initial_max_stream_data_bidi_local;
533 qcc->strms[QCS_CLT_UNI].rx.max_data = clt_params->initial_max_stream_data_uni;
534
535 /* Server initiated streams must respect the server flow control. */
536 qcc->strms[QCS_SRV_BIDI].max_streams = clt_params->initial_max_streams_bidi;
537 qcc->strms[QCS_SRV_BIDI].tx.max_data = clt_params->initial_max_stream_data_bidi_remote;
538
539 qcc->strms[QCS_SRV_UNI].max_streams = clt_params->initial_max_streams_uni;
540 qcc->strms[QCS_SRV_UNI].tx.max_data = clt_params->initial_max_stream_data_uni;
541 }
542 else {
543 struct quic_transport_params *srv_params;
544
545 /* server parameters, TX params. */
546 srv_params = &qcc->conn->qc->tx.params;
547
548 qcc->tx.max_data = srv_params->initial_max_data;
549 /* Client initiated streams must respect the server flow control. */
550 qcc->strms[QCS_CLT_BIDI].max_streams = srv_params->initial_max_streams_bidi;
551 qcc->strms[QCS_CLT_BIDI].tx.max_data = srv_params->initial_max_stream_data_bidi_remote;
552
553 qcc->strms[QCS_CLT_UNI].max_streams = srv_params->initial_max_streams_uni;
554 qcc->strms[QCS_CLT_UNI].tx.max_data = srv_params->initial_max_stream_data_uni;
555
556 /* Server initiated streams must respect the server flow control. */
557 qcc->strms[QCS_SRV_BIDI].rx.max_data = srv_params->initial_max_stream_data_bidi_local;
558 qcc->strms[QCS_SRV_UNI].rx.max_data = srv_params->initial_max_stream_data_uni;
559 }
560
561 /* Now that we have all the flow control information, we can finalize the application
562 * context.
563 */
564 qcc->app_ops->finalize(qcc->ctx);
565}
566
567/* Initialize the mux once it's attached. For outgoing connections, the context
568 * is already initialized before installing the mux, so we detect incoming
569 * connections from the fact that the context is still NULL (even during mux
570 * upgrades). <input> is always used as Input buffer and may contain data. It is
571 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
572 */
573static int qc_init(struct connection *conn, struct proxy *prx,
574 struct session *sess, struct buffer *input)
575{
576 struct qcc *qcc;
577 struct task *t = NULL;
578 void *conn_ctx = conn->ctx;
579
580 TRACE_ENTER(QC_EV_QCC_NEW);
581
582 qcc = pool_alloc(pool_head_qcc);
583 if (!qcc)
584 goto fail_no_qcc;
585
586 if (conn_is_back(conn)) {
587 qcc->flags = QC_CF_IS_BACK;
588 qcc->shut_timeout = qcc->timeout = prx->timeout.server;
589 if (tick_isset(prx->timeout.serverfin))
590 qcc->shut_timeout = prx->timeout.serverfin;
591
592 qcc->px_counters = EXTRA_COUNTERS_GET(prx->extra_counters_be,
593 &qc_stats_module);
594 } else {
595 qcc->flags = QC_CF_NONE;
596 qcc->shut_timeout = qcc->timeout = prx->timeout.client;
597 if (tick_isset(prx->timeout.clientfin))
598 qcc->shut_timeout = prx->timeout.clientfin;
599
600 qcc->px_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
601 &qc_stats_module);
602 }
603
604 qcc->proxy = prx;
605 qcc->task = NULL;
606 if (tick_isset(qcc->timeout)) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200607 t = task_new_here();
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100608 if (!t)
609 goto fail;
610
611 qcc->task = t;
612 t->process = qc_timeout_task;
613 t->context = qcc;
614 t->expire = tick_add(now_ms, qcc->timeout);
615 }
616
Amaury Denoyellecde91122021-09-22 15:28:27 +0200617 qcc->subs = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100618 qcc->wait_event.tasklet = tasklet_new();
619 if (!qcc->wait_event.tasklet)
620 goto fail;
621
622 qcc->wait_event.tasklet->process = qc_io_cb;
623 qcc->wait_event.tasklet->context = qcc;
624 qcc->wait_event.events = 0;
625
626 /* Initialize the context. */
627 qcc->st0 = QC_CS_NOERR;
628 qcc->conn = conn;
629 qcc->conn->qc->qcc = qcc;
630
631 /* Application layer initialization. */
632 qcc->app_ops = &h3_ops;
633 if (!qcc->app_ops->init(qcc))
634 goto fail;
635
636 /* The transports parameters which control the data sent have been stored
637 * in ->tx.params. The ones which control the received data are stored in
638 * in ->rx.params.
639 */
640 if (objt_listener(qcc->conn->target)) {
641 struct quic_transport_params *srv_params;
642
643 /* Server parameters, params used for RX flow control. */
644 srv_params = &conn->qc->rx.params;
645
646 qcc->rx.max_data = srv_params->initial_max_data;
647 qcc->tx.max_data = 0;
648 /* Client initiated streams must respect the server flow control. */
649 qcc->strms[QCS_CLT_BIDI].max_streams = srv_params->initial_max_streams_bidi;
650 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
651 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
652 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
653 qcc->strms[QCS_CLT_BIDI].tx.max_data = srv_params->initial_max_stream_data_bidi_remote;
654
655 qcc->strms[QCS_CLT_UNI].max_streams = srv_params->initial_max_streams_uni;
656 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
657 qcc->strms[QCS_CLT_UNI].largest_id = -1;
658 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
659 qcc->strms[QCS_CLT_UNI].tx.max_data = srv_params->initial_max_stream_data_uni;
660
661 /* Server initiated streams must respect the server flow control. */
662 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
663 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
664 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
665 qcc->strms[QCS_SRV_BIDI].rx.max_data = srv_params->initial_max_stream_data_bidi_local;
666 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
667
668 qcc->strms[QCS_SRV_UNI].max_streams = 0;
669 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
670 qcc->strms[QCS_SRV_UNI].largest_id = -1;
671 qcc->strms[QCS_SRV_UNI].rx.max_data = srv_params->initial_max_stream_data_uni;
672 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
673 }
674 else {
675 struct quic_transport_params *clt_params;
676
677 /* client parameters, RX params. */
678 clt_params = &conn->qc->rx.params;
679
680 qcc->rx.max_data = clt_params->initial_max_data;
681 qcc->tx.max_data = 0;
682 /* Client initiated streams must respect the server flow control. */
683 qcc->strms[QCS_CLT_BIDI].max_streams = 0;
684 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
685 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
686 qcc->strms[QCS_CLT_BIDI].rx.max_data = clt_params->initial_max_stream_data_bidi_local;
687 qcc->strms[QCS_CLT_BIDI].tx.max_data = 0;
688
689 qcc->strms[QCS_CLT_UNI].max_streams = 0;
690 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
691 qcc->strms[QCS_CLT_UNI].largest_id = -1;
692 qcc->strms[QCS_CLT_UNI].rx.max_data = clt_params->initial_max_stream_data_uni;
693 qcc->strms[QCS_CLT_UNI].tx.max_data = 0;
694
695 /* Server initiated streams must respect the server flow control. */
696 qcc->strms[QCS_SRV_BIDI].max_streams = clt_params->initial_max_streams_bidi;
697 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
698 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
699 qcc->strms[QCS_SRV_BIDI].rx.max_data = 0;
700 qcc->strms[QCS_SRV_BIDI].tx.max_data = clt_params->initial_max_stream_data_bidi_remote;
701
702 qcc->strms[QCS_SRV_UNI].max_streams = clt_params->initial_max_streams_uni;
703 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
704 qcc->strms[QCS_SRV_UNI].largest_id = -1;
705 qcc->strms[QCS_SRV_UNI].rx.max_data = 0;
706 qcc->strms[QCS_SRV_UNI].tx.max_data = clt_params->initial_max_stream_data_uni;
707
708 }
709
710 /* Initialize the streams counters. */
711 qcc->nb_cs = 0;
712 qcc->stream_cnt = 0;
713
714 br_init(qcc->mbuf, sizeof(qcc->mbuf) / sizeof(qcc->mbuf[0]));
715 qcc->streams_by_id = EB_ROOT_UNIQUE;
716 LIST_INIT(&qcc->send_list);
717 LIST_INIT(&qcc->fctl_list);
718 LIST_INIT(&qcc->blocked_list);
719 LIST_INIT(&qcc->buf_wait.list);
720 MT_LIST_INIT(&qcc->qcs_rxbuf_wlist);
721
Frédéric Lécaille01abc462021-07-21 09:34:27 +0200722 HA_ATOMIC_STORE(&conn->ctx, qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100723
724 if (t)
725 task_queue(t);
726
727 if (qcc->flags & QC_CF_IS_BACK) {
728 /* FIXME: For outgoing connections we need to immediately allocate streams.
729 * This highly depends on the QUIC application needs.
730 */
731 }
732
733 HA_ATOMIC_ADD(&qcc->px_counters->open_conns, 1);
734 HA_ATOMIC_ADD(&qcc->px_counters->total_conns, 1);
735
736 /* prepare to read something */
737 qcc_restart_reading(qcc, 1);
738 TRACE_LEAVE(QC_EV_QCC_NEW, conn);
739 return 0;
740
741 fail:
742 task_destroy(t);
743 if (qcc->wait_event.tasklet)
744 tasklet_free(qcc->wait_event.tasklet);
745 pool_free(pool_head_qcc, qcc);
746 fail_no_qcc:
747 conn->ctx = conn_ctx; /* restore saved ctx */
748 TRACE_DEVEL("leaving in error", QC_EV_QCC_NEW|QC_EV_QCC_END|QC_EV_QCC_ERR);
749 return -1;
750}
751
752/* returns the stream associated with id <id> or NULL if not found */
753__maybe_unused
754static inline struct qcs *qcc_st_by_id(struct qcc *qcc, int id)
755{
756 struct eb64_node *node;
757
758 node = eb64_lookup(&qcc->streams_by_id, id);
759 if (!node)
760 return (struct qcs *)qc_closed_stream;
761
762 return container_of(node, struct qcs, by_id);
763}
764
765/* release function. This one should be called to free all resources allocated
766 * to the mux.
767 */
768static void qc_release(struct qcc *qcc)
769{
770 struct connection *conn = NULL;
771
772 TRACE_ENTER(QC_EV_QCC_END);
773
774 if (qcc) {
775 /* The connection must be aattached to this mux to be released */
776 if (qcc->conn && qcc->conn->ctx == qcc)
777 conn = qcc->conn;
778
779 TRACE_DEVEL("freeing qcc", QC_EV_QCC_END, conn);
780
781 if (LIST_INLIST(&qcc->buf_wait.list))
782 LIST_DELETE(&qcc->buf_wait.list);
783
784 qc_release_mbuf(qcc);
785
786 if (qcc->task) {
787 qcc->task->context = NULL;
788 task_wakeup(qcc->task, TASK_WOKEN_OTHER);
789 qcc->task = NULL;
790 }
791 if (qcc->wait_event.tasklet)
792 tasklet_free(qcc->wait_event.tasklet);
793 if (conn && qcc->wait_event.events != 0)
794 conn->xprt->unsubscribe(conn, conn->xprt_ctx, qcc->wait_event.events,
795 &qcc->wait_event);
796
797 HA_ATOMIC_SUB(&qcc->px_counters->open_conns, 1);
798
799 pool_free(pool_head_qcc, qcc);
800 }
801
802 if (conn) {
803 conn->mux = NULL;
804 conn->ctx = NULL;
805 TRACE_DEVEL("freeing conn", QC_EV_QCC_END, conn);
806
807 conn_stop_tracking(conn);
808 conn_full_close(conn);
809 if (conn->destroy_cb)
810 conn->destroy_cb(conn);
811 conn_free(conn);
812 }
813
814 TRACE_LEAVE(QC_EV_QCC_END);
815}
816
817
818/******************************************************/
819/* functions below are for the QUIC protocol processing */
820/******************************************************/
821
822/* attempt to notify the data layer of recv availability */
823__maybe_unused
824static void qcs_notify_recv(struct qcs *qcs)
825{
826 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
827 TRACE_POINT(QC_EV_STRM_WAKE, qcs->qcc->conn, qcs);
828 tasklet_wakeup(qcs->subs->tasklet);
829 qcs->subs->events &= ~SUB_RETRY_RECV;
830 if (!qcs->subs->events)
831 qcs->subs = NULL;
832 }
833}
834
835/* attempt to notify the data layer of send availability */
836__maybe_unused
837static void qcs_notify_send(struct qcs *qcs)
838{
839 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
840 TRACE_POINT(QC_EV_STRM_WAKE, qcs->qcc->conn, qcs);
841 qcs->flags |= QC_SF_NOTIFIED;
842 tasklet_wakeup(qcs->subs->tasklet);
843 qcs->subs->events &= ~SUB_RETRY_SEND;
844 if (!qcs->subs->events)
845 qcs->subs = NULL;
846 }
847 else if (qcs->flags & (QC_SF_WANT_SHUTR | QC_SF_WANT_SHUTW)) {
848 TRACE_POINT(QC_EV_STRM_WAKE, qcs->qcc->conn, qcs);
849 tasklet_wakeup(qcs->shut_tl);
850 }
851}
852
853/* alerts the data layer, trying to wake it up by all means, following
854 * this sequence :
855 * - if the qcs' data layer is subscribed to recv, then it's woken up for recv
856 * - if its subscribed to send, then it's woken up for send
857 * - if it was subscribed to neither, its ->wake() callback is called
858 * It is safe to call this function with a closed stream which doesn't have a
859 * conn_stream anymore.
860 */
861__maybe_unused
862static void qcs_alert(struct qcs *qcs)
863{
864 TRACE_ENTER(QC_EV_QCS_WAKE, qcs->qcc->conn, qcs);
865
866 if (qcs->subs ||
867 (qcs->flags & (QC_SF_WANT_SHUTR | QC_SF_WANT_SHUTW))) {
868 qcs_notify_recv(qcs);
869 qcs_notify_send(qcs);
870 }
871 else if (qcs->cs && qcs->cs->data_cb->wake != NULL) {
872 TRACE_POINT(QC_EV_STRM_WAKE, qcs->qcc->conn, qcs);
873 qcs->cs->data_cb->wake(qcs->cs);
874 }
875
876 TRACE_LEAVE(QC_EV_QCS_WAKE, qcs->qcc->conn, qcs);
877}
878
879/* marks stream <qcs> as CLOSED and decrement the number of active streams for
880 * its connection if the stream was not yet closed. Please use this exclusively
881 * before closing a stream to ensure stream count is well maintained.
882 */
883static inline void qcs_close(struct qcs *qcs)
884{
885 TRACE_ENTER(QC_EV_QCS_END, qcs->qcc->conn, qcs);
886 /* XXX TO DO XXX */
887 TRACE_LEAVE(QC_EV_QCS_END, qcs->qcc->conn, qcs);
888}
889
890/* detaches an QUIC stream from its QCC and releases it to the QCS pool. */
891/* qcs_destroy should only ever be called by the thread that owns the stream,
892 * that means that a tasklet should be used if we want to destroy the qcs
893 * from another thread
894 */
895static void qcs_destroy(struct qcs *qcs)
896{
897 struct connection *conn = qcs->qcc->conn;
898
899 TRACE_ENTER(QC_EV_QCS_END, conn, qcs);
900
901 qcs_close(qcs);
902 eb64_delete(&qcs->by_id);
903 if (b_size(&qcs->rx.buf)) {
904 b_free(&qcs->rx.buf);
905 offer_buffers(NULL, 1);
906 }
907
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200908 b_free(&qcs->tx.buf);
909 b_free(&qcs->tx.xprt_buf);
910
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100911 if (qcs->subs)
912 qcs->subs->events = 0;
913
914 /* There's no need to explicitly call unsubscribe here, the only
915 * reference left would be in the qcc send_list/fctl_list, and if
916 * we're in it, we're getting out anyway
917 */
918 LIST_DEL_INIT(&qcs->list);
Amaury Denoyelled595f102021-09-24 10:05:30 +0200919 --qcs->qcc->strms[qcs_id_type(qcs->id)].nb_streams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100920
921 /* ditto, calling tasklet_free() here should be ok */
922 tasklet_free(qcs->shut_tl);
923 pool_free(pool_head_qcs, qcs);
924
925 TRACE_LEAVE(QC_EV_QCS_END, conn);
926}
927
928/* allocates a new bidirection stream <id> for connection <qcc> and adds it into qcc's
929 * stream tree. In case of error, nothing is added and NULL is returned. The
930 * causes of errors can be any failed memory allocation. The caller is
931 * responsible for checking if the connection may support an extra stream
932 * prior to calling this function.
933 */
934struct qcs *bidi_qcs_new(struct qcc *qcc, uint64_t id)
935{
936 struct qcs *qcs;
937 enum qcs_type qcs_type;
938
939 TRACE_ENTER(QC_EV_QCS_NEW, qcc->conn);
940
941 qcs = pool_alloc(pool_head_qcs);
942 if (!qcs)
943 goto out;
944
945 qcs->shut_tl = tasklet_new();
946 if (!qcs->shut_tl) {
947 pool_free(pool_head_qcs, qcs);
948 goto out;
949 }
950
951 qcs_type = qcs_id_type(id);
952 qcs->qcc = qcc;
953 qcs->cs = NULL;
954 qcs->id = qcs->by_id.key = id;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100955 qcs->flags = QC_SF_NONE;
956
957 qcs->rx.buf = BUF_NULL;
958 qcs->rx.st = QC_RX_SS_IDLE;
959 qcs->rx.bytes = qcs->rx.offset = 0;
960 qcs->rx.max_data = qcc->strms[qcs_type].rx.max_data;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100961 qcs->rx.buf = BUF_NULL;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200962 qcs->rx.frms = EB_ROOT_UNIQUE;
963
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100964 qcs->tx.st = QC_TX_SS_IDLE;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200965 qcs->tx.bytes = qcs->tx.offset = qcs->tx.ack_offset = 0;
966 qcs->tx.acked_frms = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100967 qcs->tx.max_data = qcc->strms[qcs_type].tx.max_data;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200968 qcs->tx.buf = BUF_NULL;
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200969 qcs->tx.xprt_buf = BUF_NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100970
971 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
972 qcc->strms[qcs_type].nb_streams++;
973 qcc->stream_cnt++;
974 qcs->subs = NULL;
975 LIST_INIT(&qcs->list);
976 qcs->shut_tl->process = qc_deferred_shut;
977 qcs->shut_tl->context = qcs;
978
979 HA_ATOMIC_ADD(&qcc->px_counters->open_streams, 1);
980 HA_ATOMIC_ADD(&qcc->px_counters->total_streams, 1);
981
982 TRACE_LEAVE(QC_EV_QCS_NEW, qcc->conn, qcs);
983 return qcs;
984
985 out:
986 TRACE_DEVEL("leaving in error", QC_EV_QCS_ERR|QC_EV_QCS_END, qcc->conn);
987 return NULL;
988}
989
990/* Release <qcs> outgoing uni-stream */
991void qcs_release(struct qcs *qcs)
992{
993 eb64_delete(&qcs->by_id);
994 pool_free(pool_head_qcs, qcs);
995}
996
997/* Allocates a locally initiated unidirectional stream. */
998struct qcs *luqs_new(struct qcc *qcc)
999{
1000 struct qcs *qcs;
1001 uint64_t next_id;
1002 enum qcs_type qcs_type;
1003
1004 TRACE_ENTER(QC_EV_QCS_NEW, qcc->conn);
1005
1006 qcs = NULL;
1007 /* QCS_ID_DIR_BIT bit is set for unidirectional stream. */
1008 if (objt_listener(qcc->conn->target))
1009 qcs_type = QCS_ID_SRV_INTIATOR_BIT | QCS_ID_DIR_BIT;
1010 else
1011 qcs_type = QCS_ID_DIR_BIT;
1012
1013 next_id = qcs_next_id(qcc, qcs_type);
1014 if (next_id == (uint64_t)-1) {
1015 TRACE_PROTO("No more stream available", QC_EV_QCS_NEW, qcc->conn);
1016 goto out;
1017 }
1018
1019 qcs = pool_alloc(pool_head_qcs);
1020 if (!qcs)
1021 goto out;
1022
1023 qcs->qcc = qcc;
1024 qcs->cs = NULL;
1025 qcs->id = qcs->by_id.key = next_id;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001026 qcs->flags = QC_SF_NONE;
1027
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001028 qcs->tx.st = QC_TX_SS_IDLE;
1029 qcs->tx.max_data = qcc->strms[qcs_type].tx.max_data;
1030 qcs->tx.offset = qcs->tx.bytes = qcs->tx.ack_offset = 0;
1031 qcs->tx.acked_frms = EB_ROOT_UNIQUE;
1032 qcs->tx.buf = BUF_NULL;
Amaury Denoyelled3d97c62021-10-05 11:45:58 +02001033 qcs->tx.xprt_buf = BUF_NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001034
1035 qcs->subs = NULL;
1036 LIST_INIT(&qcs->list);
Amaury Denoyelle139814a2021-09-24 10:03:16 +02001037 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001038
1039 TRACE_LEAVE(QC_EV_QCS_NEW, qcc->conn);
1040 return qcs;
1041
1042 out:
1043 if (qcs)
1044 pool_free(pool_head_qcs, qcs);
1045 TRACE_DEVEL("leaving in error", QC_EV_QCS_ERR|QC_EV_QCS_END, qcc->conn);
1046 return NULL;
1047}
1048
1049/* Allocates a remotely initiated unidirectional stream. */
1050struct qcs *ruqs_new(struct qcc *qcc, uint64_t id)
1051{
1052 struct qcs *qcs;
1053 enum qcs_type qcs_type;
1054
1055 TRACE_ENTER(QC_EV_QCS_NEW, qcc->conn);
1056 qcs = pool_alloc(pool_head_qcs);
1057 if (!qcs)
1058 goto out;
1059
1060 qcs_type = qcs_id_type(id);
1061
1062 qcs->qcc = qcc;
1063 qcs->cs = NULL;
1064
1065 qcs->qcc = qcc;
1066 qcs->id = qcs->by_id.key = id;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001067 qcs->flags = QC_SF_NONE;
1068
1069 qcs->rx.st = QC_RX_SS_IDLE;
1070 qcs->rx.max_data = qcc->strms[qcs_type].rx.max_data;
1071 qcs->rx.offset = qcs->rx.bytes = 0;
1072 qcs->rx.buf = BUF_NULL;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001073 qcs->rx.frms = EB_ROOT_UNIQUE;
Amaury Denoyelled3d97c62021-10-05 11:45:58 +02001074 qcs->tx.buf = BUF_NULL;
1075 qcs->tx.xprt_buf = BUF_NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001076
1077 qcs->subs = NULL;
1078 LIST_INIT(&qcs->list);
Amaury Denoyelle139814a2021-09-24 10:03:16 +02001079 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001080
1081 TRACE_LEAVE(QC_EV_QCS_NEW, qcc->conn);
1082 return qcs;
1083
1084 out:
1085 TRACE_DEVEL("leaving in error", QC_EV_QCS_ERR|QC_EV_QCS_END, qcc->conn);
1086 return NULL;
1087}
1088
1089/* attempt to notify the data layer of recv availability */
1090void ruqs_notify_recv(struct qcs *qcs)
1091{
1092 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
1093 TRACE_POINT(QC_EV_STRM_WAKE, qcs->qcc->conn);
1094 tasklet_wakeup(qcs->subs->tasklet);
1095 qcs->subs->events &= ~SUB_RETRY_RECV;
1096 if (!qcs->subs->events)
1097 qcs->subs = NULL;
1098 }
1099}
1100
1101/* Allocates a new stream associated to conn_stream <cs> on the qcc connection
1102 * with dir as direction and returns it, or NULL in case of memory allocation
1103 * error or if the highest possible stream ID was reached.
1104 */
1105static struct qcs *qcc_bck_stream_new(struct qcc *qcc, int dir,
1106 struct conn_stream *cs, struct session *sess)
1107{
1108 struct qcs *qcs = NULL;
1109 enum qcs_type qcs_type;
1110
1111 TRACE_ENTER(QC_EV_QCS_NEW, qcc->conn);
1112
1113 qcs_type = qcs_type_from_dir(qcc, dir);
1114 if (qcc->strms[qcs_type].largest_id + 1 >= qcc->strms[qcs_type].max_streams)
1115 goto out;
1116
1117 /* Defer choosing the ID until we send the first message to create the stream */
1118 qcs = bidi_qcs_new(qcc, qcc->strms[qcs_type].largest_id + 1);
1119 if (!qcs)
1120 goto out;
1121
1122 qcs->cs = cs;
1123 qcs->sess = sess;
1124 cs->ctx = qcs;
1125 qcc->nb_cs++;
1126
1127 out:
1128 if (likely(qcs))
1129 TRACE_LEAVE(QC_EV_QCS_NEW, qcc->conn, qcs);
1130 else
1131 TRACE_LEAVE(QC_EV_QCS_NEW|QC_EV_QCS_ERR|QC_EV_QCS_END, qcc->conn, qcs);
1132 return qcs;
1133}
1134
1135/* Allocates a new bidirectional stream associated to conn_stream <cs> on the <qcc> connection
1136 * and returns it, or NULL in case of memory allocation error or if the highest
1137 * possible stream ID was reached.
1138 */
1139__maybe_unused
1140static struct qcs *qcc_bck_stream_new_bidi(struct qcc *qcc,
1141 struct conn_stream *cs, struct session *sess)
1142{
1143 return qcc_bck_stream_new(qcc, QCS_BIDI, cs, sess);
1144}
1145
1146/* Allocates a new unidirectional stream associated to conn_stream <cs> on the <qcc> connection
1147 * and returns it, or NULL in case of memory allocation error or if the highest
1148 * possible stream ID was reached.
1149 */
1150__maybe_unused
1151static struct qcs *qcc_bck_stream_new_uni(struct qcc *qcc,
1152 struct conn_stream *cs, struct session *sess)
1153{
1154 return qcc_bck_stream_new(qcc, QCS_UNI, cs, sess);
1155}
1156
1157
1158/* wake a specific stream and assign its conn_stream some CS_FL_* flags among
1159 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state
1160 * is automatically updated accordingly. If the stream is orphaned, it is
1161 * destroyed.
1162 */
1163static void qcs_wake_one_stream(struct qcs *qcs)
1164{
1165 struct qcc *qcc = qcs->qcc;
1166
1167 TRACE_ENTER(QC_EV_QCS_WAKE, qcc->conn, qcs);
1168 if (!qcs->cs) {
1169 /* this stream was already orphaned */
1170 qcs_destroy(qcs);
1171 TRACE_DEVEL("leaving with no qcs", QC_EV_QCS_WAKE, qcc->conn);
1172 return;
1173 }
1174 /* XXX TO DO XXX */
1175 TRACE_LEAVE(QC_EV_QCS_WAKE, qcc->conn);
1176}
1177
1178/* wake the streams attached to the connection, whose id is greater than <last>
1179 * or unassigned.
1180 */
1181static void qc_wake_some_streams(struct qcc *qcc, int last)
1182{
1183 struct eb64_node *node;
1184 struct qcs *qcs;
1185
1186 TRACE_ENTER(QC_EV_QCS_WAKE, qcc->conn);
1187
1188 /* Wake all streams with ID > last */
1189 node = eb64_lookup_ge(&qcc->streams_by_id, last + 1);
1190 while (node) {
1191 qcs = container_of(node, struct qcs, by_id);
1192 node = eb64_next(node);
1193 qcs_wake_one_stream(qcs);
1194 }
1195
1196 /* Wake all streams with unassigned ID (ID == 0) */
1197 node = eb64_lookup(&qcc->streams_by_id, 0);
1198 while (node) {
1199 qcs = container_of(node, struct qcs, by_id);
1200 if (qcs->id > 0)
1201 break;
1202 node = eb64_next(node);
1203 qcs_wake_one_stream(qcs);
1204 }
1205
1206 TRACE_LEAVE(QC_EV_QCS_WAKE, qcc->conn);
1207}
1208
1209/* Wake up all blocked streams whose window size has become positive after the
1210 * mux's initial window was adjusted. This should be done after having processed
1211 * SETTINGS frames which have updated the mux's initial window size.
1212 */
1213__maybe_unused
1214static void qcc_unblock_sfctl(struct qcc *qcc)
1215{
1216 TRACE_ENTER(QC_EV_QCC_WAKE, qcc->conn);
1217 /* XXX TO DO XXX */
1218 TRACE_LEAVE(QC_EV_QCC_WAKE, qcc->conn);
1219}
1220
1221/* process Rx frames to be demultiplexed */
1222__maybe_unused
1223static void qc_process_demux(struct qcc *qcc)
1224{
1225 TRACE_ENTER(QC_EV_QCC_WAKE, qcc->conn);
1226 /* XXX TO DO XXX */
1227 TRACE_LEAVE(QC_EV_QCC_WAKE, qcc->conn);
1228}
1229
1230/* resume each qcs eligible for sending in list head <head> */
1231__maybe_unused
1232static void qc_resume_each_sending_qcs(struct qcc *qcc, struct list *head)
1233{
1234 struct qcs *qcs, *qcs_back;
1235
1236 TRACE_ENTER(QC_EV_QCC_SEND|QC_EV_QCS_WAKE, qcc->conn);
1237
1238 list_for_each_entry_safe(qcs, qcs_back, head, list) {
1239 if (qcc_wnd(qcc) <= 0 ||
1240 qcc->flags & QC_CF_MUX_BLOCK_ANY ||
1241 qcc->st0 >= QC_CS_ERROR)
1242 break;
1243
1244 qcs->flags &= ~QC_SF_BLK_ANY;
1245
1246 if (qcs->flags & QC_SF_NOTIFIED)
1247 continue;
1248
1249 /* If the sender changed his mind and unsubscribed, let's just
1250 * remove the stream from the send_list.
1251 */
1252 if (!(qcs->flags & (QC_SF_WANT_SHUTR|QC_SF_WANT_SHUTW)) &&
1253 (!qcs->subs || !(qcs->subs->events & SUB_RETRY_SEND))) {
1254 LIST_DEL_INIT(&qcs->list);
1255 continue;
1256 }
1257
1258 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
1259 qcs->flags |= QC_SF_NOTIFIED;
1260 tasklet_wakeup(qcs->subs->tasklet);
1261 qcs->subs->events &= ~SUB_RETRY_SEND;
1262 if (!qcs->subs->events)
1263 qcs->subs = NULL;
1264 }
1265 else if (qcs->flags & (QC_SF_WANT_SHUTR|QC_SF_WANT_SHUTW)) {
1266 tasklet_wakeup(qcs->shut_tl);
1267 }
1268 }
1269
1270 TRACE_LEAVE(QC_EV_QCC_SEND|QC_EV_QCS_WAKE, qcc->conn);
1271}
1272
1273/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
1274 * the end.
1275 */
1276__maybe_unused
1277static int qc_process_mux(struct qcc *qcc)
1278{
1279 TRACE_ENTER(QC_EV_QCC_WAKE, qcc->conn);
Frédéric Lécaille1d402402021-09-20 17:53:17 +02001280
1281 /* First we always process the flow control list because the streams
1282 * waiting there were already elected for immediate emission but were
1283 * blocked just on this.
1284 */
1285 qc_resume_each_sending_qcs(qcc, &qcc->fctl_list);
1286 qc_resume_each_sending_qcs(qcc, &qcc->send_list);
1287
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001288 TRACE_LEAVE(QC_EV_QCC_WAKE, qcc->conn);
Frédéric Lécaille1d402402021-09-20 17:53:17 +02001289 return 1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001290}
1291
1292
1293/* Attempt to read data, and subscribe if none available.
1294 * The function returns 1 if data has been received, otherwise zero.
1295 */
1296__maybe_unused
1297static int qc_recv(struct qcc *qcc)
1298{
1299 TRACE_ENTER(QC_EV_QCC_RECV, qcc->conn);
1300 /* XXX TO DO XXX */
1301 TRACE_LEAVE(QC_EV_QCC_RECV, qcc->conn);
1302 return 0;
1303}
1304
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001305static int qcs_push_frame(struct qcs *qcs, struct buffer *payload, int fin, uint64_t offset)
1306{
1307 struct quic_frame *frm;
Amaury Denoyelled3d97c62021-10-05 11:45:58 +02001308 struct buffer *buf = &qcs->tx.xprt_buf;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001309 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 +02001310 int total = 0, to_xfer;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001311
1312 qc_get_buf(qcs->qcc, buf);
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001313 to_xfer = QUIC_MIN(b_data(payload), b_room(buf));
1314 if (!to_xfer)
1315 goto out;
1316
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001317 frm = pool_zalloc(pool_head_quic_frame);
1318 if (!frm)
1319 goto err;
1320
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001321 total = b_force_xfer(buf, payload, to_xfer);
1322 fin = fin && !b_data(payload);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001323 frm->type = QUIC_FT_STREAM_8;
1324 if (fin)
1325 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
1326 if (offset) {
1327 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
1328 frm->stream.offset.key = offset;
1329 }
1330 frm->stream.qcs = qcs;
1331 frm->stream.buf = buf;
1332 frm->stream.id = qcs->by_id.key;
1333 if (total) {
1334 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
1335 frm->stream.len = total;
1336 }
1337
1338 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécailled2ba0962021-09-20 17:50:03 +02001339 out:
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001340 fprintf(stderr, "%s: total=%d fin=%d offset=%lu\n", __func__, total, fin, offset);
1341 return total;
1342
1343 err:
1344 return -1;
1345}
1346
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001347/* Try to send data if possible.
1348 * The function returns 1 if data have been sent, otherwise zero.
1349 */
1350static int qc_send(struct qcc *qcc)
1351{
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001352 struct eb64_node *node;
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001353 int ret, done, xprt_wake = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001354
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001355 TRACE_ENTER(QC_EV_QCC_SEND, qcc->conn);
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001356 ret = done = 0;
1357 /* fill as much as we can into the current buffer */
1358 while (((qcc->flags & (QC_CF_MUX_MFULL|QC_CF_MUX_MALLOC)) == 0) && !done)
1359 done = qc_process_mux(qcc);
1360
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001361 /* TODO simple loop through all streams and check if there is frames to
1362 * send
1363 */
1364 node = eb64_first(&qcc->streams_by_id);
1365 while (node) {
Amaury Denoyelled3d97c62021-10-05 11:45:58 +02001366 struct qcs *qcs = container_of(node, struct qcs, by_id);
1367 struct buffer *buf = &qcs->tx.buf;
1368 if (b_data(buf)) {
1369 char fin = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001370
Amaury Denoyelled3d97c62021-10-05 11:45:58 +02001371 /* if FIN is activated, ensure the buffer to
1372 * send is the last
1373 */
1374 if (qcs->flags & QC_SF_FIN_STREAM) {
1375 BUG_ON(b_data(&qcs->tx.buf) < b_data(buf));
1376 fin = (b_data(&qcs->tx.buf) - b_data(buf) == 0);
1377 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001378
Amaury Denoyelled3d97c62021-10-05 11:45:58 +02001379 ret = qcs_push_frame(qcs, buf, fin, qcs->tx.offset);
1380 if (ret < 0)
1381 ABORT_NOW();
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001382
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001383 if (ret > 0) {
1384 xprt_wake = 1;
1385 if (qcs->flags & QC_SF_BLK_MROOM) {
1386 qcs->flags &= ~QC_SF_BLK_MROOM;
1387 qcs_notify_send(qcs);
1388 }
1389 }
1390
Amaury Denoyelled3d97c62021-10-05 11:45:58 +02001391 qcs->tx.offset += ret;
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001392
Amaury Denoyelled3d97c62021-10-05 11:45:58 +02001393 if (b_data(buf)) {
1394 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
1395 SUB_RETRY_SEND, &qcc->wait_event);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001396 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001397 }
1398 node = eb64_next(node);
1399 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +02001400
Amaury Denoyellea543eb12021-10-06 14:53:13 +02001401 if (xprt_wake)
1402 tasklet_wakeup(((struct ssl_sock_ctx *)(qcc->conn->xprt_ctx))->wait_event.tasklet);
Frédéric Lécaille578a7892021-09-13 16:13:00 +02001403
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001404 TRACE_LEAVE(QC_EV_QCC_SEND, qcc->conn);
1405 return 0;
1406}
1407
1408/* this is the tasklet referenced in qcc->wait_event.tasklet */
1409static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
1410{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +02001411 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001412 int ret = 0;
1413
1414
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001415 if (!(qcc->wait_event.events & SUB_RETRY_SEND))
1416 ret = qc_send(qcc);
1417#if 0
1418 if (!(qcc->wait_event.events & SUB_RETRY_RECV))
1419 ret |= qc_recv(qcc);
1420#endif
1421 // TODO redefine the proper condition here
1422 //if (ret || qcc->rx.inmux)
1423 ret = qc_process(qcc);
1424
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001425leave:
1426 TRACE_LEAVE(QC_EV_QCC_WAKE);
1427 return NULL;
1428}
1429
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001430/* callback called on any event by the connection handler.
1431 * It applies changes and returns zero, or < 0 if it wants immediate
1432 * destruction of the connection (which normally doesn not happen in quic).
1433 */
1434static int qc_process(struct qcc *qcc)
1435{
1436 struct connection *conn = qcc->conn;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001437
1438 TRACE_ENTER(QC_EV_QCC_WAKE, conn);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001439 TRACE_LEAVE(QC_EV_QCC_WAKE, conn);
1440 return 0;
1441}
1442
1443/* wake-up function called by the connection layer (mux_ops.wake) */
1444static int qc_wake(struct connection *conn)
1445{
1446 struct qcc *qcc = conn->ctx;
1447 int ret;
1448
1449 TRACE_ENTER(QC_EV_QCC_WAKE, conn);
1450 ret = qc_process(qcc);
1451 if (ret >= 0)
1452 qc_wake_some_streams(qcc, 0);
1453 TRACE_LEAVE(QC_EV_QCC_WAKE);
1454 return ret;
1455}
1456
1457/* Connection timeout management. The principle is that if there's no receipt
1458 * nor sending for a certain amount of time, the connection is closed. If the
1459 * MUX buffer still has lying data or is not allocatable, the connection is
1460 * immediately killed. If it's allocatable and empty, we attempt to send a
1461 * GOAWAY frame.
1462 */
1463static struct task *qc_timeout_task(struct task *t, void *context, unsigned int state)
1464{
1465 TRACE_ENTER(QC_EV_QCC_WAKE);
1466 /* XXX TO DO XXX */
1467 TRACE_LEAVE(QC_EV_QCC_WAKE);
1468 return NULL;
1469}
1470
1471
1472/*******************************************/
1473/* functions below are used by the streams */
1474/*******************************************/
1475
1476/*
1477 * Attach a new stream to a connection
1478 * (Used for outgoing connections)
1479 */
1480static struct conn_stream *qc_attach(struct connection *conn, struct session *sess)
1481{
1482 struct conn_stream *cs;
1483 struct qcs *qcs;
1484 struct qcc *qcc = conn->ctx;
1485
1486 TRACE_ENTER(QC_EV_QCS_NEW, conn);
1487 cs = cs_new(conn, conn->target);
1488 if (!cs) {
1489 TRACE_DEVEL("leaving on CS allocation failure", QC_EV_QCS_NEW|QC_EV_QCS_ERR, conn);
1490 return NULL;
1491 }
1492 qcs = qcc_bck_stream_new(qcc, QCS_BIDI, cs, sess);
1493 if (!qcs) {
1494 TRACE_DEVEL("leaving on stream creation failure", QC_EV_QCS_NEW|QC_EV_QCS_ERR, conn);
1495 cs_free(cs);
1496 return NULL;
1497 }
1498 TRACE_LEAVE(QC_EV_QCS_NEW, conn, qcs);
1499 return cs;
1500}
1501
1502/* Retrieves the first valid conn_stream from this connection, or returns NULL.
1503 * We have to scan because we may have some orphan streams. It might be
1504 * beneficial to scan backwards from the end to reduce the likeliness to find
1505 * orphans.
1506 */
1507static const struct conn_stream *qc_get_first_cs(const struct connection *conn)
1508{
1509 struct qcc *qcc = conn->ctx;
1510 struct qcs *qcs;
1511 struct eb64_node *node;
1512
1513 node = eb64_first(&qcc->streams_by_id);
1514 while (node) {
1515 qcs = container_of(node, struct qcs, by_id);
1516 if (qcs->cs)
1517 return qcs->cs;
1518 node = eb64_next(node);
1519 }
1520 return NULL;
1521}
1522
1523static int qc_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
1524{
1525 int ret = 0;
1526 struct qcc *qcc = conn->ctx;
1527
1528 switch (mux_ctl) {
1529 case MUX_STATUS:
1530 /* Only consider the mux to be ready if we had no error. */
1531 if (qcc->st0 < QC_CS_ERROR)
1532 ret |= MUX_STATUS_READY;
1533 return ret;
1534 case MUX_EXIT_STATUS:
1535 return MUX_ES_UNKNOWN;
1536 default:
1537 return -1;
1538 }
1539}
1540
1541/*
1542 * Destroy the mux and the associated connection, if it is no longer used
1543 */
1544static void qc_destroy(void *ctx)
1545{
1546 struct qcc *qcc = ctx;
1547
1548 TRACE_ENTER(QC_EV_QCC_END, qcc->conn);
1549 if (eb_is_empty(&qcc->streams_by_id) || !qcc->conn || qcc->conn->ctx != qcc)
1550 qc_release(qcc);
1551 TRACE_LEAVE(QC_EV_QCC_END);
1552}
1553
1554/*
1555 * Detach the stream from the connection and possibly release the connection.
1556 */
1557static void qc_detach(struct conn_stream *cs)
1558{
1559 struct qcs *qcs = cs->ctx;
Amaury Denoyellecd28b272021-09-22 14:48:32 +02001560 struct qcc *qcc = qcs->qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001561
1562 TRACE_ENTER(QC_EV_STRM_END, qcs ? qcs->qcc->conn : NULL, qcs);
Amaury Denoyellecd28b272021-09-22 14:48:32 +02001563 qcs_destroy(qcs);
Amaury Denoyelled595f102021-09-24 10:05:30 +02001564 if (!qcc->strms[QCS_CLT_BIDI].nb_streams)
Amaury Denoyellecd28b272021-09-22 14:48:32 +02001565 qc_release(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001566 TRACE_LEAVE(QC_EV_STRM_END, qcs ? qcs->qcc->conn : NULL);
1567}
1568
1569/* Performs a synchronous or asynchronous shutr(). */
1570static void qc_do_shutr(struct qcs *qcs)
1571{
1572 struct qcc *qcc = qcs->qcc;
1573
1574 TRACE_ENTER(QC_EV_STRM_SHUT, qcc->conn, qcs);
1575 /* XXX TO DO XXX */
1576 TRACE_LEAVE(QC_EV_STRM_SHUT, qcc->conn, qcs);
1577 return;
1578}
1579
1580/* Performs a synchronous or asynchronous shutw(). */
1581static void qc_do_shutw(struct qcs *qcs)
1582{
1583 struct qcc *qcc = qcs->qcc;
1584
1585 TRACE_ENTER(QC_EV_STRM_SHUT, qcc->conn, qcs);
1586 /* XXX TO DO XXX */
1587 TRACE_LEAVE(QC_EV_STRM_SHUT, qcc->conn, qcs);
1588 return;
1589}
1590
1591/* This is the tasklet referenced in qcs->shut_tl, it is used for
1592 * deferred shutdowns when the qc_detach() was done but the mux buffer was full
1593 * and prevented the last frame from being emitted.
1594 */
1595static struct task *qc_deferred_shut(struct task *t, void *ctx, unsigned int state)
1596{
1597 struct qcs *qcs = ctx;
1598 struct qcc *qcc = qcs->qcc;
1599
1600 TRACE_ENTER(QC_EV_STRM_SHUT, qcc->conn, qcs);
1601
1602 if (qcs->flags & QC_SF_NOTIFIED) {
1603 /* some data processing remains to be done first */
1604 goto end;
1605 }
1606
1607 if (qcs->flags & QC_SF_WANT_SHUTW)
1608 qc_do_shutw(qcs);
1609
1610 if (qcs->flags & QC_SF_WANT_SHUTR)
1611 qc_do_shutr(qcs);
1612
1613 if (!(qcs->flags & (QC_SF_WANT_SHUTR|QC_SF_WANT_SHUTW))) {
1614 /* We're done trying to send, remove ourself from the send_list */
1615 LIST_DEL_INIT(&qcs->list);
1616
1617 if (!qcs->cs) {
1618 qcs_destroy(qcs);
1619 if (qcc_is_dead(qcc))
1620 qc_release(qcc);
1621 }
1622 }
1623
1624 end:
1625 TRACE_LEAVE(QC_EV_STRM_SHUT);
1626 return NULL;
1627}
1628
1629/* shutr() called by the conn_stream (mux_ops.shutr) */
1630static void qc_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
1631{
1632
1633 struct qcs *qcs = cs->ctx;
1634
1635 TRACE_ENTER(QC_EV_STRM_SHUT, qcs->qcc->conn, qcs);
1636 if (cs->flags & CS_FL_KILL_CONN)
1637 qcs->flags |= QC_SF_KILL_CONN;
1638
1639 if (mode)
1640 qc_do_shutr(qcs);
1641
1642 TRACE_LEAVE(QC_EV_STRM_SHUT, qcs->qcc->conn, qcs);
1643}
1644
1645/* shutw() called by the conn_stream (mux_ops.shutw) */
1646static void qc_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
1647{
1648 struct qcs *qcs = cs->ctx;
1649
1650 TRACE_ENTER(QC_EV_STRM_SHUT, qcs->qcc->conn, qcs);
1651 if (cs->flags & CS_FL_KILL_CONN)
1652 qcs->flags |= QC_SF_KILL_CONN;
1653
1654 qc_do_shutw(qcs);
1655 TRACE_LEAVE(QC_EV_STRM_SHUT, qcs->qcc->conn, qcs);
1656}
1657
1658/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1659 * event subscriber <es> is not allowed to change from a previous call as long
1660 * as at least one event is still subscribed. The <event_type> must only be a
1661 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
1662 */
1663static int qc_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
1664{
1665 struct qcs *qcs = cs->ctx;
1666 struct qcc *qcc = qcs->qcc;
1667
1668 TRACE_ENTER(QC_EV_STRM_SEND|QC_EV_STRM_RECV, qcc->conn, qcs);
1669
1670 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1671 BUG_ON(qcs->subs && qcs->subs != es);
1672
1673 es->events |= event_type;
1674 qcs->subs = es;
1675
1676 if (event_type & SUB_RETRY_RECV)
1677 TRACE_DEVEL("subscribe(recv)", QC_EV_STRM_RECV, qcc->conn, qcs);
1678
1679 if (event_type & SUB_RETRY_SEND) {
1680 TRACE_DEVEL("subscribe(send)", QC_EV_STRM_SEND, qcc->conn, qcs);
1681 if (!(qcs->flags & QC_SF_BLK_SFCTL) &&
1682 !LIST_INLIST(&qcs->list)) {
1683 if (qcs->flags & QC_SF_BLK_MFCTL)
1684 LIST_APPEND(&qcc->fctl_list, &qcs->list);
1685 else
1686 LIST_APPEND(&qcc->send_list, &qcs->list);
1687 }
1688 }
1689 TRACE_LEAVE(QC_EV_STRM_SEND|QC_EV_STRM_RECV, qcc->conn, qcs);
1690 return 0;
1691}
1692
1693/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1694 * The <es> pointer is not allowed to differ from the one passed to the
1695 * subscribe() call. It always returns zero.
1696 */
1697static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
1698{
1699 struct qcs *qcs = cs->ctx;
1700
1701 TRACE_ENTER(QC_EV_STRM_SEND|QC_EV_STRM_RECV, qcs->qcc->conn, qcs);
1702
1703 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
1704 BUG_ON(qcs->subs && qcs->subs != es);
1705
1706 es->events &= ~event_type;
1707 if (!es->events)
1708 qcs->subs = NULL;
1709
1710 if (event_type & SUB_RETRY_RECV)
1711 TRACE_DEVEL("unsubscribe(recv)", QC_EV_STRM_RECV, qcs->qcc->conn, qcs);
1712
1713 if (event_type & SUB_RETRY_SEND) {
1714 TRACE_DEVEL("subscribe(send)", QC_EV_STRM_SEND, qcs->qcc->conn, qcs);
1715 qcs->flags &= ~QC_SF_NOTIFIED;
1716 if (!(qcs->flags & (QC_SF_WANT_SHUTR | QC_SF_WANT_SHUTW)))
1717 LIST_DEL_INIT(&qcs->list);
1718 }
1719
1720 TRACE_LEAVE(QC_EV_STRM_SEND|QC_EV_STRM_RECV, qcs->qcc->conn, qcs);
1721 return 0;
1722}
1723
1724
1725/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1726 * event subscriber <es> is not allowed to change from a previous call as long
1727 * as at least one event is still subscribed. The <event_type> must only be a
1728 * SUB_RETRY_RECV. It always returns 0.
1729 */
1730static int ruqs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
1731{
1732 struct qcc *qcc = qcs->qcc;
1733
1734 TRACE_ENTER(QC_EV_STRM_RECV, qcc->conn, qcs);
1735
1736 BUG_ON(event_type & ~SUB_RETRY_RECV);
1737 BUG_ON(qcs->subs && qcs->subs != es);
1738
1739 es->events |= event_type;
1740 qcs->subs = es;
1741
1742 if (event_type & SUB_RETRY_RECV)
1743 TRACE_DEVEL("subscribe(recv)", QC_EV_STRM_RECV, qcc->conn, qcs);
1744
1745 TRACE_LEAVE(QC_EV_STRM_RECV, qcc->conn, qcs);
1746 return 0;
1747}
1748
1749/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1750 * The <es> pointer is not allowed to differ from the one passed to the
1751 * subscribe() call. It always returns zero.
1752 */
1753static int ruqs_unsubscribe(struct qcs *qcs, int event_type, struct wait_event *es)
1754{
1755 TRACE_ENTER(QC_EV_STRM_RECV, qcs->qcc->conn, qcs);
1756
1757 BUG_ON(event_type & ~SUB_RETRY_RECV);
1758 BUG_ON(qcs->subs && qcs->subs != es);
1759
1760 es->events &= ~event_type;
1761 if (!es->events)
1762 qcs->subs = NULL;
1763
1764 if (event_type & SUB_RETRY_RECV)
1765 TRACE_DEVEL("unsubscribe(recv)", QC_EV_STRM_RECV, qcs->qcc->conn, qcs);
1766
1767 TRACE_LEAVE(QC_EV_STRM_RECV, qcs->qcc->conn, qcs);
1768 return 0;
1769}
1770
1771/* Called from the upper layer, to subscribe <es> to events <event_type>. The
1772 * event subscriber <es> is not allowed to change from a previous call as long
1773 * as at least one event is still subscribed. The <event_type> must only be
1774 * SUB_RETRY_SEND. It always returns 0.
1775 */
1776static int luqs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
1777{
1778 struct qcc *qcc = qcs->qcc;
1779
1780 TRACE_ENTER(QC_EV_STRM_SEND, qcc->conn, qcs);
1781
1782 BUG_ON(event_type & ~SUB_RETRY_SEND);
1783 BUG_ON(qcs->subs && qcs->subs != es);
1784
1785 es->events |= event_type;
1786 qcs->subs = es;
1787
1788 if (event_type & SUB_RETRY_SEND) {
1789 TRACE_DEVEL("subscribe(send)", QC_EV_STRM_SEND, qcc->conn, qcs);
1790 if (!(qcs->flags & QC_SF_BLK_SFCTL) &&
1791 !LIST_INLIST(&qcs->list)) {
1792 if (qcs->flags & QC_SF_BLK_MFCTL)
1793 LIST_APPEND(&qcc->fctl_list, &qcs->list);
1794 else
1795 LIST_APPEND(&qcc->send_list, &qcs->list);
1796 }
1797 }
1798
1799 TRACE_LEAVE(QC_EV_STRM_SEND, qcc->conn, qcs);
1800 return 0;
1801}
1802
1803/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
1804 * The <es> pointer is not allowed to differ from the one passed to the
1805 * subscribe() call. It always returns zero.
1806 */
1807static int luqs_unsubscribe(struct qcs *qcs, int event_type, struct wait_event *es)
1808{
1809 struct qcc *qcc = qcs->qcc;
1810
1811 TRACE_ENTER(QC_EV_STRM_SEND, qcc->conn, qcs);
1812
1813 BUG_ON(event_type & ~SUB_RETRY_SEND);
1814 BUG_ON(qcs->subs && qcs->subs != es);
1815
1816 es->events &= ~event_type;
1817 if (!es->events)
1818 qcs->subs = NULL;
1819
1820 if (event_type & SUB_RETRY_SEND) {
1821 TRACE_DEVEL("subscribe(send)", QC_EV_STRM_SEND, qcc->conn, qcs);
1822 qcs->flags &= ~QC_SF_NOTIFIED;
1823 if (!(qcs->flags & (QC_SF_WANT_SHUTR | QC_SF_WANT_SHUTW)))
1824 LIST_DEL_INIT(&qcs->list);
1825 }
1826
1827 TRACE_LEAVE(QC_EV_STRM_SEND, qcc->conn, qcs);
1828 return 0;
1829}
1830
1831/* Called from the upper layer, to receive data */
1832static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
1833{
1834 struct qcs *qcs = cs->ctx;
1835 struct qcc *qcc = qcs->qcc;
1836 int ret;
1837
1838 ret = 0;
1839 TRACE_ENTER(QC_EV_STRM_RECV, qcc->conn, qcs);
1840 /* XXX TO DO XXX */
1841 TRACE_LEAVE(QC_EV_STRM_RECV, qcc->conn, qcs);
1842 return ret;
1843}
1844
1845/* Called from the upper layer, to send data from buffer <buf> for no more than
1846 * <count> bytes. Returns the number of bytes effectively sent. Some status
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001847 * flags may be updated on the mux.
1848 */
1849size_t luqs_snd_buf(struct qcs *qcs, struct buffer *buf, size_t count, int flags)
1850{
1851 size_t room, total = 0;
1852 struct qcc *qcc = qcs->qcc;
1853 struct buffer *res;
1854
1855 TRACE_ENTER(QC_EV_QCS_SEND|QC_EV_STRM_SEND, qcs->qcc->conn);
1856 if (!count)
1857 goto out;
1858
1859 res = &qcs->tx.buf;
1860 if (!qc_get_buf(qcc, res)) {
1861 qcc->flags |= QC_CF_MUX_MALLOC;
1862 goto out;
1863 }
1864
1865 room = b_room(res);
1866 if (!room)
1867 goto out;
1868
1869 if (count > room)
1870 count = room;
1871
1872 total += b_xfer(res, buf, count);
Amaury Denoyelle7b1d3d62021-08-27 15:05:29 +02001873 qcs_push_frame(qcs, res, 0, 0);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001874
1875 out:
1876 TRACE_LEAVE(QC_EV_QCS_SEND|QC_EV_STRM_SEND, qcs->qcc->conn);
1877 return total;
1878
1879 err:
1880 TRACE_DEVEL("leaving on stream error", QC_EV_QCS_SEND|QC_EV_STRM_SEND, qcs->qcc->conn);
1881 return total;
1882}
1883
1884/* for debugging with CLI's "show fd" command */
1885static int qc_show_fd(struct buffer *msg, struct connection *conn)
1886{
1887 struct qcc *qcc = conn->ctx;
1888 struct qcs *qcs = NULL;
1889 struct eb64_node *node;
1890 int fctl_cnt = 0;
1891 int send_cnt = 0;
1892 int tree_cnt = 0;
1893 int orph_cnt = 0;
1894 struct buffer *hmbuf, *tmbuf;
1895
1896 if (!qcc)
1897 return 0;
1898
1899 list_for_each_entry(qcs, &qcc->fctl_list, list)
1900 fctl_cnt++;
1901
1902 list_for_each_entry(qcs, &qcc->send_list, list)
1903 send_cnt++;
1904
1905 qcs = NULL;
1906 node = eb64_first(&qcc->streams_by_id);
1907 while (node) {
1908 qcs = container_of(node, struct qcs, by_id);
1909 tree_cnt++;
1910 if (!qcs->cs)
1911 orph_cnt++;
1912 node = eb64_next(node);
1913 }
1914
1915 hmbuf = br_head(qcc->mbuf);
1916 tmbuf = br_tail(qcc->mbuf);
1917 chunk_appendf(msg, " qcc.st0=%s .flg=0x%04x"
1918 " clt.nb_streams_bidi=%llu srv.nb_streams_bidi=%llu"
1919 " clt.nb_streams_uni=%llu srv.nb_streams_uni=%llu"
1920 " .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
1921 " .orph_cnt=%d .sub=%d"
1922 " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
1923 qcc_st_to_str(qcc->st0), qcc->flags,
1924 (unsigned long long)qcc->strms[QCS_CLT_BIDI].nb_streams,
1925 (unsigned long long)qcc->strms[QCS_SRV_BIDI].nb_streams,
1926 (unsigned long long)qcc->strms[QCS_CLT_UNI].nb_streams,
1927 (unsigned long long)qcc->strms[QCS_SRV_UNI].nb_streams,
1928 qcc->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
1929 qcc->wait_event.events,
1930 br_head_idx(qcc->mbuf), br_tail_idx(qcc->mbuf), br_size(qcc->mbuf),
1931 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
1932 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
1933 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
1934 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
1935
1936 if (qcs) {
1937 chunk_appendf(msg, " last_qcs=%p .id=%llu rx.st=%s tx.st=%s .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
1938 qcs, (unsigned long long)qcs->id,
1939 qcs_rx_st_to_str(qcs->rx.st), qcs_tx_st_to_str(qcs->tx.st), qcs->flags,
1940 (unsigned int)b_data(&qcs->rx.buf), b_orig(&qcs->rx.buf),
1941 (unsigned int)b_head_ofs(&qcs->rx.buf), (unsigned int)b_size(&qcs->rx.buf),
1942 qcs->cs);
1943 if (qcs->cs)
1944 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
1945 qcs->cs->flags, qcs->cs->data);
1946 }
1947
1948 return 0;
1949}
1950
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001951/****************************************/
1952/* MUX initialization and instantiation */
1953/***************************************/
1954
1955/* The mux operations */
1956static const struct mux_ops qc_ops = {
1957 .init = qc_init,
1958 .wake = qc_wake,
Amaury Denoyelle26dfd902021-08-24 16:33:53 +02001959 //.snd_buf = qc_snd_buf,
1960 .snd_buf = h3_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001961 .rcv_buf = qc_rcv_buf,
1962 .subscribe = qc_subscribe,
1963 .unsubscribe = qc_unsubscribe,
1964 .ruqs_subscribe = ruqs_subscribe,
1965 .ruqs_unsubscribe = ruqs_unsubscribe,
1966 .luqs_subscribe = luqs_subscribe,
1967 .luqs_unsubscribe = luqs_unsubscribe,
1968 .attach = qc_attach,
1969 .get_first_cs = qc_get_first_cs,
1970 .detach = qc_detach,
1971 .destroy = qc_destroy,
1972 .avail_streams_bidi = qc_avail_streams_bidi,
1973 .avail_streams_uni = qc_avail_streams_uni,
1974 .used_streams = qc_used_streams,
1975 .shutr = qc_shutr,
1976 .shutw = qc_shutw,
1977 .ctl = qc_ctl,
1978 .show_fd = qc_show_fd,
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001979 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX|MX_FL_HOL_RISK,
1980 .name = "QUIC",
1981};
1982
1983static struct mux_proto_list mux_proto_quic =
1984 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &qc_ops };
1985
1986INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);
1987