blob: e3ad4eb9c050263d38a6d98e64b4847b7ffdd95b [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>
Christopher Faulet1329f2a2021-12-16 17:32:56 +01007#include <haproxy/conn_stream.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +01008#include <haproxy/dynbuf.h>
Amaury Denoyelle9a327a72022-02-14 17:11:09 +01009#include <haproxy/htx.h>
Amaury Denoyelledeed7772021-12-03 11:36:46 +010010#include <haproxy/pool.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020011#include <haproxy/ssl_sock-t.h>
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +010012#include <haproxy/xprt_quic.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010013
Amaury Denoyelledeed7772021-12-03 11:36:46 +010014DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010015DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
16
Amaury Denoyelledeed7772021-12-03 11:36:46 +010017/* Allocate a new QUIC streams with id <id> and type <type>. */
18struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +010019{
Amaury Denoyelledeed7772021-12-03 11:36:46 +010020 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010021
Amaury Denoyelledeed7772021-12-03 11:36:46 +010022 qcs = pool_alloc(pool_head_qcs);
23 if (!qcs)
24 goto out;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010025
Amaury Denoyellefdbf63e2021-12-16 15:22:30 +010026 fprintf(stderr, "%s: stream ID %lu\n", __func__, id);
Frédéric Lécailledfbae762021-02-18 09:59:01 +010027
Amaury Denoyelledeed7772021-12-03 11:36:46 +010028 qcs->qcc = qcc;
29 qcs->cs = NULL;
30 qcs->flags = QC_SF_NONE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010031
Amaury Denoyelledeed7772021-12-03 11:36:46 +010032 qcs->by_id.key = id;
33 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
34 qcc->strms[type].nb_streams++;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010035
Amaury Denoyelledeed7772021-12-03 11:36:46 +010036 qcs->rx.buf = BUF_NULL;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +010037 qcs->rx.app_buf = BUF_NULL;
Amaury Denoyelledeed7772021-12-03 11:36:46 +010038 qcs->rx.offset = 0;
39 qcs->rx.frms = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010040
Amaury Denoyelledeed7772021-12-03 11:36:46 +010041 qcs->tx.buf = BUF_NULL;
42 qcs->tx.xprt_buf = BUF_NULL;
43 qcs->tx.offset = 0;
44 qcs->tx.ack_offset = 0;
45 qcs->tx.acked_frms = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010046
Amaury Denoyelledeed7772021-12-03 11:36:46 +010047 qcs->wait_event.tasklet = NULL;
48 qcs->wait_event.events = 0;
49 qcs->subs = NULL;
50
51 out:
52 return qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010053}
54
Amaury Denoyelledeed7772021-12-03 11:36:46 +010055/* Free a qcs. This function must only be used for unidirectional streams.
56 * Bidirectional streams are released by the upper layer through qc_detach().
Frédéric Lécailledfbae762021-02-18 09:59:01 +010057 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +010058void uni_qcs_free(struct qcs *qcs)
Frédéric Lécailledfbae762021-02-18 09:59:01 +010059{
Amaury Denoyelledeed7772021-12-03 11:36:46 +010060 eb64_delete(&qcs->by_id);
61 pool_free(pool_head_qcs, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +010062}
63
Amaury Denoyelledeed7772021-12-03 11:36:46 +010064struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +010065{
Amaury Denoyelledeed7772021-12-03 11:36:46 +010066 struct buffer *buf = b_alloc(bptr);
67 BUG_ON(!buf);
68 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010069}
70
Amaury Denoyellea3f222d2021-12-06 11:24:00 +010071int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
72{
73 fprintf(stderr, "%s\n", __func__);
74
75 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
76 BUG_ON(qcs->subs && qcs->subs != es);
77
78 es->events |= event_type;
79 qcs->subs = es;
80
81 return 0;
82}
83
84void qcs_notify_recv(struct qcs *qcs)
85{
86 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
87 tasklet_wakeup(qcs->subs->tasklet);
88 qcs->subs->events &= ~SUB_RETRY_RECV;
89 if (!qcs->subs->events)
90 qcs->subs = NULL;
91 }
92}
93
94void qcs_notify_send(struct qcs *qcs)
95{
96 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
97 tasklet_wakeup(qcs->subs->tasklet);
98 qcs->subs->events &= ~SUB_RETRY_SEND;
99 if (!qcs->subs->events)
100 qcs->subs = NULL;
101 }
102}
103
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100104/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
105 * several streams, depending on the already open ones.
106 * Return this node if succeeded, NULL if not.
107 */
108struct eb64_node *qcc_get_qcs(struct qcc *qcc, uint64_t id)
109{
110 unsigned int strm_type;
111 int64_t sub_id;
112 struct eb64_node *strm_node;
113
114 strm_type = id & QCS_ID_TYPE_MASK;
115 sub_id = id >> QCS_ID_TYPE_SHIFT;
116 strm_node = NULL;
Amaury Denoyelle0dc40f02022-02-07 11:44:17 +0100117 if (quic_stream_is_local(qcc, id)) {
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100118 /* Local streams: this stream must be already opened. */
119 strm_node = eb64_lookup(&qcc->streams_by_id, id);
120 if (!strm_node) {
121 /* unknown stream id */
122 goto out;
123 }
124 }
125 else {
126 /* Remote streams. */
127 struct eb_root *strms;
128 uint64_t largest_id;
129 enum qcs_type qcs_type;
130
131 strms = &qcc->streams_by_id;
132 qcs_type = qcs_id_type(id);
Amaury Denoyellec055e302022-02-07 16:09:06 +0100133
134 /* TODO also checks max-streams for uni streams */
135 if (quic_stream_is_bidi(id)) {
136 if (sub_id + 1 > qcc->lfctl.max_bidi_streams) {
137 /* streams limit reached */
138 goto out;
139 }
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100140 }
141
142 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
143 * correct value.
144 */
145 largest_id = qcc->strms[qcs_type].largest_id;
146 if (sub_id > (int64_t)largest_id) {
147 /* RFC: "A stream ID that is used out of order results in all streams
148 * of that type with lower-numbered stream IDs also being opened".
149 * So, let's "open" these streams.
150 */
151 int64_t i;
152 struct qcs *qcs;
153
154 qcs = NULL;
155 for (i = largest_id + 1; i <= sub_id; i++) {
156 uint64_t id = (i << QCS_ID_TYPE_SHIFT) | strm_type;
157 enum qcs_type type = id & QCS_ID_DIR_BIT ? QCS_CLT_UNI : QCS_CLT_BIDI;
158 qcs = qcs_new(qcc, id, type);
159 if (!qcs) {
160 /* allocation failure */
161 goto out;
162 }
163
164 qcc->strms[qcs_type].largest_id = i;
165 }
166 if (qcs)
167 strm_node = &qcs->by_id;
168 }
169 else {
170 strm_node = eb64_lookup(strms, id);
171 }
172 }
173
174 return strm_node;
175
176 out:
177 return NULL;
178}
179
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +0100180/* Handle a new STREAM frame <strm_frm>. The frame content will be copied in
181 * the buffer of the stream instance. The stream instance will be stored in
182 * <out_qcs>. In case of success, the caller can immediatly call qcc_decode_qcs
183 * to process the frame content.
184 *
185 * Returns 0 on success. On errors, two codes are present.
186 * - 1 is returned if the frame cannot be decoded and must be discarded.
187 * - 2 is returned if the stream cannot decode at the moment the frame. The
188 * frame should be buffered to be handled later.
189 */
190int qcc_recv(struct qcc *qcc, uint64_t id, uint64_t len, uint64_t offset,
191 char fin, char *data, struct qcs **out_qcs)
192{
193 struct qcs *qcs;
194 struct eb64_node *strm_node;
195 size_t total, diff;
196
197 strm_node = qcc_get_qcs(qcc, id);
198 if (!strm_node) {
199 fprintf(stderr, "%s: stream not found\n", __func__);
200 return 1;
201 }
202
203 qcs = eb64_entry(&strm_node->node, struct qcs, by_id);
204 *out_qcs = qcs;
205
206 if (offset > qcs->rx.offset)
207 return 2;
208
209 if (offset + len <= qcs->rx.offset) {
210 fprintf(stderr, "%s: already received STREAM data\n", __func__);
211 return 1;
212 }
213
214 /* Last frame already handled for this stream. */
215 BUG_ON(qcs->flags & QC_SF_FIN_RECV);
216
217 if (!qc_get_buf(qcs, &qcs->rx.buf)) {
218 /* TODO should mark qcs as full */
219 return 2;
220 }
221
222 fprintf(stderr, "%s: new STREAM data\n", __func__);
223 diff = qcs->rx.offset - offset;
224
225 /* TODO do not partially copy a frame if not enough size left. Maybe
226 * this can be optimized.
227 */
228 if (len > b_room(&qcs->rx.buf)) {
229 /* TODO handle STREAM frames larger than RX buffer. */
230 BUG_ON(len > b_size(&qcs->rx.buf));
231 return 2;
232 }
233
234 len -= diff;
235 data += diff;
236
237 total = b_putblk(&qcs->rx.buf, data, len);
238 /* TODO handle partial copy of a STREAM frame. */
239 BUG_ON(len != total);
240
241 qcs->rx.offset += total;
242
243 if (fin)
244 qcs->flags |= QC_SF_FIN_RECV;
245
246 out:
247 return 0;
248}
249
250/* Decode the content of STREAM frames already received on the stream instance
251 * <qcs>.
252 *
253 * Returns 0 on success else non-zero.
254 */
255int qcc_decode_qcs(struct qcc *qcc, struct qcs *qcs)
256{
257 if (qcc->app_ops->decode_qcs(qcs, qcs->flags & QC_SF_FIN_RECV, qcc->ctx) < 0) {
258 fprintf(stderr, "%s: decoding error\n", __func__);
259 return 1;
260 }
261
262 return 0;
263}
264
Amaury Denoyellec055e302022-02-07 16:09:06 +0100265static int qc_is_max_streams_needed(struct qcc *qcc)
266{
267 return qcc->lfctl.closed_bidi_streams > qcc->lfctl.initial_max_bidi_streams / 2;
268}
269
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500270/* detaches the QUIC stream from its QCC and releases it to the QCS pool. */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100271static void qcs_destroy(struct qcs *qcs)
272{
Amaury Denoyellec055e302022-02-07 16:09:06 +0100273 const uint64_t id = qcs->by_id.key;
274
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100275 fprintf(stderr, "%s: release stream %llu\n", __func__, qcs->by_id.key);
276
Amaury Denoyellec055e302022-02-07 16:09:06 +0100277 if (quic_stream_is_remote(qcs->qcc, id)) {
278 if (quic_stream_is_bidi(id)) {
279 ++qcs->qcc->lfctl.closed_bidi_streams;
280 if (qc_is_max_streams_needed(qcs->qcc))
281 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
282 }
283 }
284
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100285 eb64_delete(&qcs->by_id);
286
287 b_free(&qcs->rx.buf);
288 b_free(&qcs->tx.buf);
289 b_free(&qcs->tx.xprt_buf);
290
291 --qcs->qcc->strms[qcs_id_type(qcs->by_id.key)].nb_streams;
292
293 pool_free(pool_head_qcs, qcs);
294}
295
296static inline int qcc_is_dead(const struct qcc *qcc)
297{
298 fprintf(stderr, "%s: %lu\n", __func__, qcc->strms[QCS_CLT_BIDI].nb_streams);
299
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100300 if (!qcc->strms[QCS_CLT_BIDI].nb_streams && !qcc->task)
301 return 1;
302
303 return 0;
304}
305
306/* Return true if the mux timeout should be armed. */
307static inline int qcc_may_expire(struct qcc *qcc)
308{
309
310 /* Consider that the timeout must be set if no bidirectional streams
311 * are opened.
312 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100313 if (!qcc->strms[QCS_CLT_BIDI].nb_streams)
314 return 1;
315
316 return 0;
317}
318
319/* release function. This one should be called to free all resources allocated
320 * to the mux.
321 */
322static void qc_release(struct qcc *qcc)
323{
324 struct connection *conn = NULL;
325
326 if (qcc) {
327 /* The connection must be aattached to this mux to be released */
328 if (qcc->conn && qcc->conn->ctx == qcc)
329 conn = qcc->conn;
330
331 if (qcc->wait_event.tasklet)
332 tasklet_free(qcc->wait_event.tasklet);
333
334 pool_free(pool_head_qcc, qcc);
335 }
336
337 if (conn) {
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100338 LIST_DEL_INIT(&conn->stopping_list);
339
Frédéric Lécaille19cd46e2022-01-10 11:40:33 +0100340 conn->qc->conn = NULL;
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100341 conn->mux = NULL;
342 conn->ctx = NULL;
343
344 conn_stop_tracking(conn);
345 conn_full_close(conn);
346 if (conn->destroy_cb)
347 conn->destroy_cb(conn);
348 conn_free(conn);
Frédéric Lécaille19cd46e2022-01-10 11:40:33 +0100349 fprintf(stderr, "conn@%p released\n", conn);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100350 }
351}
352
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100353static int qcs_push_frame(struct qcs *qcs, struct buffer *payload, int fin, uint64_t offset,
354 struct list *frm_list)
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200355{
356 struct quic_frame *frm;
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200357 struct buffer *buf = &qcs->tx.xprt_buf;
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200358 int total = 0, to_xfer;
Amaury Denoyelle642ab062022-02-23 10:54:42 +0100359 unsigned char *btail;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200360
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100361 fprintf(stderr, "%s\n", __func__);
362
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +0200363 qc_get_buf(qcs, buf);
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200364 to_xfer = QUIC_MIN(b_data(payload), b_room(buf));
365 if (!to_xfer)
366 goto out;
367
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200368 frm = pool_zalloc(pool_head_quic_frame);
369 if (!frm)
370 goto err;
371
Amaury Denoyelle642ab062022-02-23 10:54:42 +0100372 /* store buffer end before transfering data for frm.stream.data */
373 btail = (unsigned char *)b_tail(buf);
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200374 total = b_force_xfer(buf, payload, to_xfer);
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100375 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200376 fin = fin && !b_data(payload);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200377 frm->type = QUIC_FT_STREAM_8;
378 if (fin)
379 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
380 if (offset) {
381 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
382 frm->stream.offset.key = offset;
383 }
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100384 frm->stream.qcs = (struct qcs *)qcs;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200385 frm->stream.buf = buf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +0100386 frm->stream.data = btail;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200387 frm->stream.id = qcs->by_id.key;
388 if (total) {
389 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
390 frm->stream.len = total;
391 }
392
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100393 LIST_APPEND(frm_list, &frm->list);
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200394 out:
Frédéric Lécaille677b99d2021-12-21 11:53:33 +0100395 fprintf(stderr, "%s: total=%d fin=%d id=%llu offset=%lu\n",
396 __func__, total, fin, (ull)qcs->by_id.key, offset);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200397 return total;
398
399 err:
400 return -1;
401}
402
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100403/* Wrapper for send on transport layer. Send a list of frames <frms> for the
404 * connection <qcc>.
405 *
406 * Returns 0 if all data sent with success else non-zero.
407 */
408static int qc_send_frames(struct qcc *qcc, struct list *frms)
409{
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100410 void *first_frm = NULL;
411
412 retry_send:
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100413 if (!LIST_ISEMPTY(frms))
414 qc_send_app_pkts(qcc->conn->qc, frms);
415
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100416 /* if the frame list is not empty, retry immediatly to send. Remember
417 * the first frame in the list : if the pointer did not advance, it
418 * means the transport layer is blocked.
419 *
420 * TODO implement immediate retry on transport layer. This way on mux
421 * always subscribe if the list is not empty.
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100422 */
Amaury Denoyellee9c4cc12022-03-04 15:29:53 +0100423 if (!LIST_ISEMPTY(frms) && first_frm != frms->n) {
424 first_frm = frms->n;
425 goto retry_send;
426 }
427
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100428 if (!LIST_ISEMPTY(frms)) {
429 fprintf(stderr, "%s: remaining frames to send\n", __func__);
430 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
431 SUB_RETRY_SEND, &qcc->wait_event);
432 return 1;
433 }
434
435 return 0;
436}
437
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100438static int qc_send(struct qcc *qcc)
439{
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200440 struct eb64_node *node;
Amaury Denoyellec0b66ca2022-02-21 18:45:22 +0100441 int ret = 0;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200442
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100443 fprintf(stderr, "%s\n", __func__);
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200444
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100445 /* loop through all streams, construct STREAM frames if data available.
446 * TODO optimize the loop to favor streams which are not too heavy.
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200447 */
448 node = eb64_first(&qcc->streams_by_id);
449 while (node) {
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200450 struct qcs *qcs = container_of(node, struct qcs, by_id);
451 struct buffer *buf = &qcs->tx.buf;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100452
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200453 if (b_data(buf)) {
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100454 char fin = qcs->flags & QC_SF_FIN_STREAM;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100455 ret = qcs_push_frame(qcs, buf, fin, qcs->tx.offset,
456 &qcc->tx.frms);
Amaury Denoyelle14551132022-03-04 16:51:20 +0100457 BUG_ON(ret < 0); /* TODO handle this properly */
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200458
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100459 if (ret > 0) {
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100460 qcs_notify_send(qcs);
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100461 if (qcs->flags & QC_SF_BLK_MROOM)
462 qcs->flags &= ~QC_SF_BLK_MROOM;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100463 }
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200464
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100465 fprintf(stderr, "%s ret=%d\n", __func__, ret);
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200466 qcs->tx.offset += ret;
Amaury Denoyellea2c58a72021-12-03 14:38:31 +0100467
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100468 /* Subscribe if not all data can be send. */
Amaury Denoyellea2c58a72021-12-03 14:38:31 +0100469 if (b_data(buf)) {
470 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
471 SUB_RETRY_SEND, &qcc->wait_event);
472 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200473 }
474 node = eb64_next(node);
475 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200476
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100477 qc_send_frames(qcc, &qcc->tx.frms);
478 /* TODO adjust ret if not all frames are sent. */
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100479
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100480 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100481}
482
Amaury Denoyelle6a4aebf2022-02-01 10:16:05 +0100483/* Release all streams that are already marked as detached. This is only done
484 * if their TX buffers are empty or if a CONNECTION_CLOSE has been received.
485 *
486 * Return the number of released stream.
487 */
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100488static int qc_release_detached_streams(struct qcc *qcc)
489{
490 struct eb64_node *node;
491 int release = 0;
492
493 node = eb64_first(&qcc->streams_by_id);
494 while (node) {
495 struct qcs *qcs = container_of(node, struct qcs, by_id);
496 node = eb64_next(node);
497
498 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelled9751482022-02-01 15:15:11 +0100499 if ((!b_data(&qcs->tx.buf) && !b_data(&qcs->tx.xprt_buf))) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100500 qcs_destroy(qcs);
501 release = 1;
502 }
503 else {
504 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
505 SUB_RETRY_SEND, &qcc->wait_event);
506 }
507 }
508 }
509
510 return release;
511}
512
Amaury Denoyellec055e302022-02-07 16:09:06 +0100513/* Send a MAX_STREAM_BIDI frame to update the limit of bidirectional streams
514 * allowed to be opened by the peer. The caller should have first checked if
515 * this is required with qc_is_max_streams_needed.
516 *
517 * Returns 0 on success else non-zero.
518 */
519static int qc_send_max_streams(struct qcc *qcc)
520{
521 struct list frms = LIST_HEAD_INIT(frms);
522 struct quic_frame *frm;
523
524 frm = pool_zalloc(pool_head_quic_frame);
525 BUG_ON(!frm); /* TODO handle this properly */
526
527 frm->type = QUIC_FT_MAX_STREAMS_BIDI;
528 frm->max_streams_bidi.max_streams = qcc->lfctl.max_bidi_streams +
529 qcc->lfctl.closed_bidi_streams;
530 fprintf(stderr, "SET MAX_STREAMS %lu\n", frm->max_streams_bidi.max_streams);
531 LIST_APPEND(&frms, &frm->list);
532
533 if (qc_send_frames(qcc, &frms))
534 return 1;
535
536 /* save the new limit if the frame has been send. */
537 qcc->lfctl.max_bidi_streams += qcc->lfctl.closed_bidi_streams;
538 qcc->lfctl.closed_bidi_streams = 0;
539
540 return 0;
541}
542
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100543static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
544{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +0200545 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100546
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100547 fprintf(stderr, "%s\n", __func__);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100548
Amaury Denoyellec055e302022-02-07 16:09:06 +0100549 if (qc_is_max_streams_needed(qcc))
550 qc_send_max_streams(qcc);
551
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100552 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100553
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100554 if (qc_release_detached_streams(qcc)) {
Amaury Denoyelle1136e922022-02-01 10:33:09 +0100555 /* Schedule the mux timeout if no bidirectional streams left. */
556 if (qcc_may_expire(qcc)) {
557 qcc->task->expire = tick_add(now_ms, qcc->timeout);
558 task_queue(qcc->task);
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100559 }
560 }
561
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100562 return NULL;
563}
564
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100565static struct task *qc_timeout_task(struct task *t, void *ctx, unsigned int state)
566{
567 struct qcc *qcc = ctx;
568 int expired = tick_is_expired(t->expire, now_ms);
569
570 fprintf(stderr, "%s\n", __func__);
571
572 if (qcc) {
573 if (!expired) {
574 fprintf(stderr, "%s: not expired\n", __func__);
575 return t;
576 }
577
578 if (!qcc_may_expire(qcc)) {
579 fprintf(stderr, "%s: cannot expire\n", __func__);
580 t->expire = TICK_ETERNITY;
581 return t;
582 }
583 }
584
585 fprintf(stderr, "%s: timeout\n", __func__);
586 task_destroy(t);
Amaury Denoyelleea3e0352022-02-21 10:05:16 +0100587
588 if (!qcc)
589 return NULL;
590
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100591 qcc->task = NULL;
592
593 if (qcc_is_dead(qcc))
594 qc_release(qcc);
595
596 return NULL;
597}
598
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100599static int qc_init(struct connection *conn, struct proxy *prx,
600 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100601{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100602 struct qcc *qcc;
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100603 struct quic_transport_params *lparams;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100604
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100605 qcc = pool_alloc(pool_head_qcc);
606 if (!qcc)
607 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100608
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100609 qcc->conn = conn;
610 conn->ctx = qcc;
Amaury Denoyellece1f30d2022-02-01 15:14:24 +0100611 qcc->flags = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100612
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100613 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100614
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100615 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100616
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100617 /* Server parameters, params used for RX flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100618 lparams = &conn->qc->rx.params;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100619
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100620 qcc->rx.max_data = lparams->initial_max_data;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100621 qcc->tx.max_data = 0;
Amaury Denoyelle2c71fe52022-02-09 18:16:49 +0100622 LIST_INIT(&qcc->tx.frms);
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100623
624 /* Client initiated streams must respect the server flow control. */
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100625 qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100626 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
627 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100628 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100629 qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100630
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100631 qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100632 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
633 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100634 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100635 qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100636
637 /* Server initiated streams must respect the server flow control. */
638 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100639 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
640 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100641 qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100642 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
643
644 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100645 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
646 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyelle749cb642022-02-09 10:25:29 +0100647 qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100648 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100649
Amaury Denoyellec055e302022-02-07 16:09:06 +0100650 qcc->lfctl.max_bidi_streams = qcc->lfctl.initial_max_bidi_streams = lparams->initial_max_streams_bidi;
651 qcc->lfctl.closed_bidi_streams = 0;
652
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100653 qcc->wait_event.tasklet = tasklet_new();
654 if (!qcc->wait_event.tasklet)
655 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100656
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100657 qcc->subs = NULL;
658 qcc->wait_event.tasklet->process = qc_io_cb;
659 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100660
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100661 /* haproxy timeouts */
662 qcc->timeout = prx->timeout.client;
663 qcc->task = task_new_here();
664 if (!qcc->task)
665 goto fail_no_timeout_task;
666 qcc->task->process = qc_timeout_task;
667 qcc->task->context = qcc;
668 qcc->task->expire = tick_add(now_ms, qcc->timeout);
669
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100670 if (!conn_is_back(conn)) {
671 if (!LIST_INLIST(&conn->stopping_list)) {
672 LIST_APPEND(&mux_stopping_data[tid].list,
673 &conn->stopping_list);
674 }
675 }
676
Frédéric Lécailleb80b20c2022-01-12 17:46:56 +0100677 HA_ATOMIC_STORE(&conn->qc->qcc, qcc);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100678 /* init read cycle */
679 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100680
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100681 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100682
Amaury Denoyelleaebe26f2022-01-13 16:28:06 +0100683 fail_no_timeout_task:
684 tasklet_free(qcc->wait_event.tasklet);
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100685 fail_no_tasklet:
686 pool_free(pool_head_qcc, qcc);
687 fail_no_qcc:
688 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100689}
690
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100691static void qc_detach(struct conn_stream *cs)
692{
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +0100693 struct qcs *qcs = cs->ctx;
694 struct qcc *qcc = qcs->qcc;
695
696 fprintf(stderr, "%s: leaving with tx.buf.data=%lu, tx.xprt_buf.data=%lu\n",
697 __func__, b_data(&qcs->tx.buf), b_data(&qcs->tx.xprt_buf));
698
Amaury Denoyelled9751482022-02-01 15:15:11 +0100699 /* TODO on CONNECTION_CLOSE reception, it should be possible to free
700 * qcs instances. This should be done once the buffering and ACK
701 * managment between xprt and mux is reorganized.
702 */
703
704 if ((b_data(&qcs->tx.buf) || b_data(&qcs->tx.xprt_buf))) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100705 qcs->flags |= QC_SF_DETACH;
706 return;
707 }
708
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +0100709 qcs_destroy(qcs);
Amaury Denoyelle1136e922022-02-01 10:33:09 +0100710
711 /* Schedule the mux timeout if no bidirectional streams left. */
712 if (qcc_may_expire(qcc)) {
713 qcc->task->expire = tick_add(now_ms, qcc->timeout);
714 task_queue(qcc->task);
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +0100715 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100716}
717
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100718/* Called from the upper layer, to receive data */
719static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf,
720 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100721{
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100722 struct qcs *qcs = cs->ctx;
723 struct htx *qcs_htx = NULL;
724 struct htx *cs_htx = NULL;
725 size_t ret = 0;
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +0100726 char fin = 0;
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100727
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100728 fprintf(stderr, "%s\n", __func__);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100729
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100730 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
731 if (htx_is_empty(qcs_htx)) {
732 /* Set buffer data to 0 as HTX is empty. */
733 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
734 goto end;
735 }
736
737 ret = qcs_htx->data;
738
739 cs_htx = htx_from_buf(buf);
740 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
741 htx_to_buf(cs_htx, buf);
742 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
743 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
744 goto end;
745 }
746
747 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
748 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
749
750 /* Copy EOM from src to dst buffer if all data copied. */
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +0100751 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
752 cs_htx->flags |= HTX_FL_EOM;
753 fin = 1;
754 }
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100755
756 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
757 htx_to_buf(cs_htx, buf);
758 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
759 ret -= qcs_htx->data;
760
761 end:
762 if (b_data(&qcs->rx.app_buf)) {
763 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
764 }
765 else {
766 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
767 if (cs->flags & CS_FL_ERR_PENDING)
768 cs->flags |= CS_FL_ERROR;
769
Amaury Denoyelleeb53e5b2022-02-14 17:11:32 +0100770 if (fin)
771 cs->flags |= (CS_FL_EOI|CS_FL_EOS);
Amaury Denoyelle9a327a72022-02-14 17:11:09 +0100772
773 if (b_size(&qcs->rx.app_buf)) {
774 b_free(&qcs->rx.app_buf);
775 offer_buffers(NULL, 1);
776 }
777 }
778
779 if (ret)
780 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
781
782 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100783}
784
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100785static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf,
786 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100787{
788 struct qcs *qcs = cs->ctx;
789
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100790 fprintf(stderr, "%s\n", __func__);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100791
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100792 return qcs->qcc->app_ops->snd_buf(cs, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100793}
794
795/* Called from the upper layer, to subscribe <es> to events <event_type>. The
796 * event subscriber <es> is not allowed to change from a previous call as long
797 * as at least one event is still subscribed. The <event_type> must only be a
798 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
799 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100800static int qc_subscribe(struct conn_stream *cs, int event_type,
801 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100802{
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100803 return qcs_subscribe(cs->ctx, event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100804}
805
806/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
807 * The <es> pointer is not allowed to differ from the one passed to the
808 * subscribe() call. It always returns zero.
809 */
810static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
811{
812 struct qcs *qcs = cs->ctx;
813
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100814 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
815 BUG_ON(qcs->subs && qcs->subs != es);
816
817 es->events &= ~event_type;
818 if (!es->events)
819 qcs->subs = NULL;
820
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100821 return 0;
822}
823
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100824static int qc_wake(struct connection *conn)
825{
826 struct qcc *qcc = conn->ctx;
827
828 /* Check if a soft-stop is in progress.
829 * Release idling front connection if this is the case.
830 */
831 if (unlikely(conn->qc->li->bind_conf->frontend->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
832 qc_release(qcc);
833 }
834
835 return 1;
836}
837
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100838static const struct mux_ops qc_ops = {
839 .init = qc_init,
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100840 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100841 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100842 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100843 .subscribe = qc_subscribe,
844 .unsubscribe = qc_unsubscribe,
Amaury Denoyelle0e0969d2022-01-31 15:41:14 +0100845 .wake = qc_wake,
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100846};
847
848static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100849 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100850
851INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);