blob: 3424a2276368f8f93d196a979e0815f223d62ccf [file] [log] [blame]
Amaury Denoyelledeed7772021-12-03 11:36:46 +01001#include <haproxy/mux_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002
Amaury Denoyelleeb01f592021-10-07 16:44:05 +02003#include <import/eb64tree.h>
4
Frédéric Lécailledfbae762021-02-18 09:59:01 +01005#include <haproxy/api.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +01006#include <haproxy/connection.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +01007#include <haproxy/dynbuf.h>
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01008#include <haproxy/htx.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +01009#include <haproxy/pool.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020010#include <haproxy/ssl_sock-t.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010011
Amaury Denoyelledeed7772021-12-03 11:36:46 +010012DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010013DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
14
Frédéric Lécailledfbae762021-02-18 09:59:01 +010015void quic_mux_transport_params_update(struct qcc *qcc)
16{
Amaury Denoyelledeed7772021-12-03 11:36:46 +010017 struct quic_transport_params *clt_params;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010018
Amaury Denoyelledeed7772021-12-03 11:36:46 +010019 /* Client parameters, params used to TX. */
20 clt_params = &qcc->conn->qc->tx.params;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010021
Amaury Denoyelledeed7772021-12-03 11:36:46 +010022 qcc->tx.max_data = clt_params->initial_max_data;
23 /* Client initiated streams must respect the server flow control. */
24 qcc->strms[QCS_CLT_BIDI].rx.max_data = clt_params->initial_max_stream_data_bidi_local;
25 qcc->strms[QCS_CLT_UNI].rx.max_data = clt_params->initial_max_stream_data_uni;
26
27 /* Server initiated streams must respect the server flow control. */
28 qcc->strms[QCS_SRV_BIDI].max_streams = clt_params->initial_max_streams_bidi;
29 qcc->strms[QCS_SRV_BIDI].tx.max_data = clt_params->initial_max_stream_data_bidi_remote;
30
31 qcc->strms[QCS_SRV_UNI].max_streams = clt_params->initial_max_streams_uni;
32 qcc->strms[QCS_SRV_UNI].tx.max_data = clt_params->initial_max_stream_data_uni;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010033}
34
Amaury Denoyelledeed7772021-12-03 11:36:46 +010035/* Allocate a new QUIC streams with id <id> and type <type>. */
36struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +010037{
Amaury Denoyelledeed7772021-12-03 11:36:46 +010038 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010039
Amaury Denoyelledeed7772021-12-03 11:36:46 +010040 qcs = pool_alloc(pool_head_qcs);
41 if (!qcs)
42 goto out;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010043
Amaury Denoyellefdbf63e2021-12-16 15:22:30 +010044 fprintf(stderr, "%s: stream ID %lu\n", __func__, id);
Frédéric Lécailledfbae762021-02-18 09:59:01 +010045
Amaury Denoyelledeed7772021-12-03 11:36:46 +010046 qcs->qcc = qcc;
47 qcs->cs = NULL;
48 qcs->flags = QC_SF_NONE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010049
Amaury Denoyelledeed7772021-12-03 11:36:46 +010050 qcs->by_id.key = id;
51 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
52 qcc->strms[type].nb_streams++;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010053
Amaury Denoyelledeed7772021-12-03 11:36:46 +010054 qcs->rx.buf = BUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +010055 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +010056 qcs->rx.offset = 0;
57 qcs->rx.frms = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010058
Amaury Denoyelledeed7772021-12-03 11:36:46 +010059 qcs->tx.buf = BUF_NULL;
60 qcs->tx.xprt_buf = BUF_NULL;
61 qcs->tx.offset = 0;
62 qcs->tx.ack_offset = 0;
63 qcs->tx.acked_frms = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010064
Amaury Denoyelledeed7772021-12-03 11:36:46 +010065 qcs->wait_event.tasklet = NULL;
66 qcs->wait_event.events = 0;
67 qcs->subs = NULL;
68
69 out:
70 return qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010071}
72
Amaury Denoyelledeed7772021-12-03 11:36:46 +010073/* Free a qcs. This function must only be used for unidirectional streams.
74 * Bidirectional streams are released by the upper layer through qc_detach().
Frédéric Lécailledfbae762021-02-18 09:59:01 +010075 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +010076void uni_qcs_free(struct qcs *qcs)
Frédéric Lécailledfbae762021-02-18 09:59:01 +010077{
Amaury Denoyelledeed7772021-12-03 11:36:46 +010078 eb64_delete(&qcs->by_id);
79 pool_free(pool_head_qcs, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +010080}
81
Amaury Denoyelledeed7772021-12-03 11:36:46 +010082struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +010083{
Amaury Denoyelledeed7772021-12-03 11:36:46 +010084 struct buffer *buf = b_alloc(bptr);
85 BUG_ON(!buf);
86 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010087}
88
Amaury Denoyellea3f222d2021-12-06 11:24:00 +010089int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
90{
91 fprintf(stderr, "%s\n", __func__);
92
93 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
94 BUG_ON(qcs->subs && qcs->subs != es);
95
96 es->events |= event_type;
97 qcs->subs = es;
98
99 return 0;
100}
101
102void qcs_notify_recv(struct qcs *qcs)
103{
104 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
105 tasklet_wakeup(qcs->subs->tasklet);
106 qcs->subs->events &= ~SUB_RETRY_RECV;
107 if (!qcs->subs->events)
108 qcs->subs = NULL;
109 }
110}
111
112void qcs_notify_send(struct qcs *qcs)
113{
114 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
115 tasklet_wakeup(qcs->subs->tasklet);
116 qcs->subs->events &= ~SUB_RETRY_SEND;
117 if (!qcs->subs->events)
118 qcs->subs = NULL;
119 }
120}
121
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100122/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
123 * several streams, depending on the already open ones.
124 * Return this node if succeeded, NULL if not.
125 */
126struct eb64_node *qcc_get_qcs(struct qcc *qcc, uint64_t id)
127{
128 unsigned int strm_type;
129 int64_t sub_id;
130 struct eb64_node *strm_node;
131
132 strm_type = id & QCS_ID_TYPE_MASK;
133 sub_id = id >> QCS_ID_TYPE_SHIFT;
134 strm_node = NULL;
135 if (qc_local_stream_id(qcc, id)) {
136 /* Local streams: this stream must be already opened. */
137 strm_node = eb64_lookup(&qcc->streams_by_id, id);
138 if (!strm_node) {
139 /* unknown stream id */
140 goto out;
141 }
142 }
143 else {
144 /* Remote streams. */
145 struct eb_root *strms;
146 uint64_t largest_id;
147 enum qcs_type qcs_type;
148
149 strms = &qcc->streams_by_id;
150 qcs_type = qcs_id_type(id);
151 if (sub_id + 1 > qcc->strms[qcs_type].max_streams) {
152 /* streams limit reached */
153 goto out;
154 }
155
156 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
157 * correct value.
158 */
159 largest_id = qcc->strms[qcs_type].largest_id;
160 if (sub_id > (int64_t)largest_id) {
161 /* RFC: "A stream ID that is used out of order results in all streams
162 * of that type with lower-numbered stream IDs also being opened".
163 * So, let's "open" these streams.
164 */
165 int64_t i;
166 struct qcs *qcs;
167
168 qcs = NULL;
169 for (i = largest_id + 1; i <= sub_id; i++) {
170 uint64_t id = (i << QCS_ID_TYPE_SHIFT) | strm_type;
171 enum qcs_type type = id & QCS_ID_DIR_BIT ? QCS_CLT_UNI : QCS_CLT_BIDI;
172 qcs = qcs_new(qcc, id, type);
173 if (!qcs) {
174 /* allocation failure */
175 goto out;
176 }
177
178 qcc->strms[qcs_type].largest_id = i;
179 }
180 if (qcs)
181 strm_node = &qcs->by_id;
182 }
183 else {
184 strm_node = eb64_lookup(strms, id);
185 }
186 }
187
188 return strm_node;
189
190 out:
191 return NULL;
192}
193
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500194/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100195static void qcs_destroy(struct qcs *qcs)
196{
197 fprintf(stderr, "%s: release stream %llu\n", __func__, qcs->by_id.key);
198
199 eb64_delete(&qcs->by_id);
200
201 b_free(&qcs->rx.buf);
202 b_free(&qcs->tx.buf);
203 b_free(&qcs->tx.xprt_buf);
204
205 --qcs->qcc->strms[qcs_id_type(qcs->by_id.key)].nb_streams;
206
207 pool_free(pool_head_qcs, qcs);
208}
209
210static inline int qcc_is_dead(const struct qcc *qcc)
211{
212 fprintf(stderr, "%s: %lu\n", __func__, qcc->strms[QCS_CLT_BIDI].nb_streams);
213
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100214 if (!qcc->strms[QCS_CLT_BIDI].nb_streams && !qcc->task)
215 return 1;
216
217 return 0;
218}
219
220/* Return true if the mux timeout should be armed. */
221static inline int qcc_may_expire(struct qcc *qcc)
222{
223
224 /* Consider that the timeout must be set if no bidirectional streams
225 * are opened.
226 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100227 if (!qcc->strms[QCS_CLT_BIDI].nb_streams)
228 return 1;
229
230 return 0;
231}
232
233/* release function. This one should be called to free all resources allocated
234 * to the mux.
235 */
236static void qc_release(struct qcc *qcc)
237{
238 struct connection *conn = NULL;
239
240 if (qcc) {
241 /* The connection must be aattached to this mux to be released */
242 if (qcc->conn && qcc->conn->ctx == qcc)
243 conn = qcc->conn;
244
245 if (qcc->wait_event.tasklet)
246 tasklet_free(qcc->wait_event.tasklet);
247
248 pool_free(pool_head_qcc, qcc);
249 }
250
251 if (conn) {
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100252 LIST_DEL_INIT(&conn->stopping_list);
253
Frédéric Lécaille19cd46e2022-01-10 11:40:33 +0100254 conn->qc->conn = NULL;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100255 conn->mux = NULL;
256 conn->ctx = NULL;
257
258 conn_stop_tracking(conn);
259 conn_full_close(conn);
260 if (conn->destroy_cb)
261 conn->destroy_cb(conn);
262 conn_free(conn);
Frédéric Lécaille19cd46e2022-01-10 11:40:33 +0100263 fprintf(stderr, "conn@%p released\n", conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100264 }
265}
266
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200267static int qcs_push_frame(struct qcs *qcs, struct buffer *payload, int fin, uint64_t offset)
268{
269 struct quic_frame *frm;
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200270 struct buffer *buf = &qcs->tx.xprt_buf;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200271 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 +0200272 int total = 0, to_xfer;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200273
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100274 fprintf(stderr, "%s\n", __func__);
275
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +0200276 qc_get_buf(qcs, buf);
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200277 to_xfer = QUIC_MIN(b_data(payload), b_room(buf));
278 if (!to_xfer)
279 goto out;
280
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200281 frm = pool_zalloc(pool_head_quic_frame);
282 if (!frm)
283 goto err;
284
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200285 total = b_force_xfer(buf, payload, to_xfer);
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100286 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200287 fin = fin && !b_data(payload);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200288 frm->type = QUIC_FT_STREAM_8;
289 if (fin)
290 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
291 if (offset) {
292 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
293 frm->stream.offset.key = offset;
294 }
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100295 frm->stream.qcs = (struct qcs *)qcs;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200296 frm->stream.buf = buf;
297 frm->stream.id = qcs->by_id.key;
298 if (total) {
299 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
300 frm->stream.len = total;
301 }
302
Frédéric Lécaille82468ea2022-01-14 20:23:22 +0100303 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200304 out:
Frédéric Lécaille677b99d2021-12-21 11:53:33 +0100305 fprintf(stderr, "%s: total=%d fin=%d id=%llu offset=%lu\n",
306 __func__, total, fin, (ull)qcs->by_id.key, offset);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200307 return total;
308
309 err:
310 return -1;
311}
312
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100313static int qc_send(struct qcc *qcc)
314{
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200315 struct eb64_node *node;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100316 int xprt_wake = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100317 int ret;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200318
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100319 fprintf(stderr, "%s\n", __func__);
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200320
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200321 /* TODO simple loop through all streams and check if there is frames to
322 * send
323 */
324 node = eb64_first(&qcc->streams_by_id);
325 while (node) {
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200326 struct qcs *qcs = container_of(node, struct qcs, by_id);
327 struct buffer *buf = &qcs->tx.buf;
328 if (b_data(buf)) {
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100329 char fin = qcs->flags & QC_SF_FIN_STREAM;
Amaury Denoyellec2025c12021-12-03 15:03:36 +0100330 ret = qcs_push_frame(qcs, buf, fin, qcs->tx.offset);
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200331 if (ret < 0)
332 ABORT_NOW();
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200333
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100334 if (ret > 0) {
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100335 qcs_notify_send(qcs);
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100336 if (qcs->flags & QC_SF_BLK_MROOM)
337 qcs->flags &= ~QC_SF_BLK_MROOM;
338
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100339 xprt_wake = 1;
340 }
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200341
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100342 fprintf(stderr, "%s ret=%d\n", __func__, ret);
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200343 qcs->tx.offset += ret;
Amaury Denoyellea2c58a72021-12-03 14:38:31 +0100344
345 if (b_data(buf)) {
346 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
347 SUB_RETRY_SEND, &qcc->wait_event);
348 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200349 }
350 node = eb64_next(node);
351 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200352
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100353 if (xprt_wake)
354 tasklet_wakeup(((struct ssl_sock_ctx *)(qcc->conn->xprt_ctx))->wait_event.tasklet);
355
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100356 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100357}
358
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +0100359/* Release all streams that are already marked as detached. This is only done
360 * if their TX buffers are empty or if a CONNECTION_CLOSE has been received.
361 *
362 * Return the number of released stream.
363 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100364static int qc_release_detached_streams(struct qcc *qcc)
365{
366 struct eb64_node *node;
367 int release = 0;
368
369 node = eb64_first(&qcc->streams_by_id);
370 while (node) {
371 struct qcs *qcs = container_of(node, struct qcs, by_id);
372 node = eb64_next(node);
373
374 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelled9751482022-02-01 15:15:11 +0100375 if ((!b_data(&qcs->tx.buf) && !b_data(&qcs->tx.xprt_buf))) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100376 qcs_destroy(qcs);
377 release = 1;
378 }
379 else {
380 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
381 SUB_RETRY_SEND, &qcc->wait_event);
382 }
383 }
384 }
385
386 return release;
387}
388
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100389static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
390{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +0200391 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100392
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100393 fprintf(stderr, "%s\n", __func__);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100394
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100395 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100396
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100397 if (qc_release_detached_streams(qcc)) {
Amaury Denoyelle1136e922022-02-01 10:33:09 +0100398 /* Schedule the mux timeout if no bidirectional streams left. */
399 if (qcc_may_expire(qcc)) {
400 qcc->task->expire = tick_add(now_ms, qcc->timeout);
401 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100402 }
403 }
404
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100405 return NULL;
406}
407
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100408static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
409{
410 struct qcc *qcc = ctx;
411 int expired = tick_is_expired(t->expire, now_ms);
412
413 fprintf(stderr, "%s\n", __func__);
414
415 if (qcc) {
416 if (!expired) {
417 fprintf(stderr, "%s: not expired\n", __func__);
418 return t;
419 }
420
421 if (!qcc_may_expire(qcc)) {
422 fprintf(stderr, "%s: cannot expire\n", __func__);
423 t->expire = TICK_ETERNITY;
424 return t;
425 }
426 }
427
428 fprintf(stderr, "%s: timeout\n", __func__);
429 task_destroy(t);
430 qcc->task = NULL;
431
432 if (qcc_is_dead(qcc))
433 qc_release(qcc);
434
435 return NULL;
436}
437
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100438static int qc_init(struct connection *conn, struct proxy *prx,
439 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100440{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100441 struct qcc *qcc;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100442 struct quic_transport_params *srv_params;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100443
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100444 qcc = pool_alloc(pool_head_qcc);
445 if (!qcc)
446 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100447
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100448 qcc->conn = conn;
449 conn->ctx = qcc;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +0100450 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100451
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100452 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100453
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100454 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100455
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100456 /* Server parameters, params used for RX flow control. */
457 srv_params = &conn->qc->rx.params;
458
459 qcc->rx.max_data = srv_params->initial_max_data;
460 qcc->tx.max_data = 0;
461
462 /* Client initiated streams must respect the server flow control. */
463 qcc->strms[QCS_CLT_BIDI].max_streams = srv_params->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100464 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
465 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100466 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
467 qcc->strms[QCS_CLT_BIDI].tx.max_data = srv_params->initial_max_stream_data_bidi_remote;
468
469 qcc->strms[QCS_CLT_UNI].max_streams = srv_params->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100470 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
471 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100472 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
473 qcc->strms[QCS_CLT_UNI].tx.max_data = srv_params->initial_max_stream_data_uni;
474
475 /* Server initiated streams must respect the server flow control. */
476 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100477 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
478 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100479 qcc->strms[QCS_SRV_BIDI].rx.max_data = srv_params->initial_max_stream_data_bidi_local;
480 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
481
482 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100483 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
484 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100485 qcc->strms[QCS_SRV_UNI].rx.max_data = srv_params->initial_max_stream_data_uni;
486 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100487
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100488 qcc->wait_event.tasklet = tasklet_new();
489 if (!qcc->wait_event.tasklet)
490 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100491
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100492 qcc->subs = NULL;
493 qcc->wait_event.tasklet->process = qc_io_cb;
494 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100495
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100496 /* haproxy timeouts */
497 qcc->timeout = prx->timeout.client;
498 qcc->task = task_new_here();
499 if (!qcc->task)
500 goto fail_no_timeout_task;
501 qcc->task->process = qc_timeout_task;
502 qcc->task->context = qcc;
503 qcc->task->expire = tick_add(now_ms, qcc->timeout);
504
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100505 if (!conn_is_back(conn)) {
506 if (!LIST_INLIST(&conn->stopping_list)) {
507 LIST_APPEND(&mux_stopping_data[tid].list,
508 &conn->stopping_list);
509 }
510 }
511
Frédéric Lécailleb80b20c2022-01-12 17:46:56 +0100512 HA_ATOMIC_STORE(&conn->qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100513 /* init read cycle */
514 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100515
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100516 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100517
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100518 fail_no_timeout_task:
519 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100520 fail_no_tasklet:
521 pool_free(pool_head_qcc, qcc);
522 fail_no_qcc:
523 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100524}
525
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100526static void qc_detach(struct conn_stream *cs)
527{
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +0100528 struct qcs *qcs = cs->ctx;
529 struct qcc *qcc = qcs->qcc;
530
531 fprintf(stderr, "%s: leaving with tx.buf.data=%lu, tx.xprt_buf.data=%lu\n",
532 __func__, b_data(&qcs->tx.buf), b_data(&qcs->tx.xprt_buf));
533
Amaury Denoyelled9751482022-02-01 15:15:11 +0100534 /* TODO on CONNECTION_CLOSE reception, it should be possible to free
535 * qcs instances. This should be done once the buffering and ACK
536 * managment between xprt and mux is reorganized.
537 */
538
539 if ((b_data(&qcs->tx.buf) || b_data(&qcs->tx.xprt_buf))) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100540 qcs->flags |= QC_SF_DETACH;
541 return;
542 }
543
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +0100544 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +0100545
546 /* Schedule the mux timeout if no bidirectional streams left. */
547 if (qcc_may_expire(qcc)) {
548 qcc->task->expire = tick_add(now_ms, qcc->timeout);
549 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +0100550 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100551}
552
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100553/* Called from the upper layer, to receive data */
554static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf,
555 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100556{
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100557 struct qcs *qcs = cs->ctx;
558 struct htx *qcs_htx = NULL;
559 struct htx *cs_htx = NULL;
560 size_t ret = 0;
561
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100562 fprintf(stderr, "%s\n", __func__);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100563
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100564 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
565 if (htx_is_empty(qcs_htx)) {
566 /* Set buffer data to 0 as HTX is empty. */
567 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
568 goto end;
569 }
570
571 ret = qcs_htx->data;
572
573 cs_htx = htx_from_buf(buf);
574 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
575 htx_to_buf(cs_htx, buf);
576 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
577 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
578 goto end;
579 }
580
581 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
582 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
583
584 /* Copy EOM from src to dst buffer if all data copied. */
585 if (htx_is_empty(qcs_htx))
586 cs_htx->flags |= (qcs_htx->flags & HTX_FL_EOM);
587
588 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
589 htx_to_buf(cs_htx, buf);
590 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
591 ret -= qcs_htx->data;
592
593 end:
594 if (b_data(&qcs->rx.app_buf)) {
595 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
596 }
597 else {
598 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
599 if (cs->flags & CS_FL_ERR_PENDING)
600 cs->flags |= CS_FL_ERROR;
601
602 /* TODO put CS_FL_EOI/EOS on fin */
603
604 if (b_size(&qcs->rx.app_buf)) {
605 b_free(&qcs->rx.app_buf);
606 offer_buffers(NULL, 1);
607 }
608 }
609
610 if (ret)
611 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
612
613 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100614}
615
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100616static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf,
617 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100618{
619 struct qcs *qcs = cs->ctx;
620
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100621 fprintf(stderr, "%s\n", __func__);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100622
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100623 return qcs->qcc->app_ops->snd_buf(cs, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100624}
625
626/* Called from the upper layer, to subscribe <es> to events <event_type>. The
627 * event subscriber <es> is not allowed to change from a previous call as long
628 * as at least one event is still subscribed. The <event_type> must only be a
629 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
630 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100631static int qc_subscribe(struct conn_stream *cs, int event_type,
632 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100633{
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100634 return qcs_subscribe(cs->ctx, event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100635}
636
637/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
638 * The <es> pointer is not allowed to differ from the one passed to the
639 * subscribe() call. It always returns zero.
640 */
641static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
642{
643 struct qcs *qcs = cs->ctx;
644
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100645 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
646 BUG_ON(qcs->subs && qcs->subs != es);
647
648 es->events &= ~event_type;
649 if (!es->events)
650 qcs->subs = NULL;
651
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100652 return 0;
653}
654
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100655static int qc_wake(struct connection *conn)
656{
657 struct qcc *qcc = conn->ctx;
658
659 /* Check if a soft-stop is in progress.
660 * Release idling front connection if this is the case.
661 */
662 if (unlikely(conn->qc->li->bind_conf->frontend->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
663 qc_release(qcc);
664 }
665
666 return 1;
667}
668
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100669static const struct mux_ops qc_ops = {
670 .init = qc_init,
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100671 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100672 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100673 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100674 .subscribe = qc_subscribe,
675 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100676 .wake = qc_wake,
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100677};
678
679static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100680 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100681
682INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);