blob: e25ff256c951b873b20d90fb8fa2eda5741f5d0f [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>
8#include <haproxy/pool.h>
Amaury Denoyelleeb01f592021-10-07 16:44:05 +02009#include <haproxy/ssl_sock-t.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010010
Amaury Denoyelledeed7772021-12-03 11:36:46 +010011DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc));
Frédéric Lécailledfbae762021-02-18 09:59:01 +010012DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs));
13
Frédéric Lécailledfbae762021-02-18 09:59:01 +010014void quic_mux_transport_params_update(struct qcc *qcc)
15{
Amaury Denoyelledeed7772021-12-03 11:36:46 +010016 struct quic_transport_params *clt_params;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010017
Amaury Denoyelledeed7772021-12-03 11:36:46 +010018 /* Client parameters, params used to TX. */
19 clt_params = &qcc->conn->qc->tx.params;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010020
Amaury Denoyelledeed7772021-12-03 11:36:46 +010021 qcc->tx.max_data = clt_params->initial_max_data;
22 /* Client initiated streams must respect the server flow control. */
23 qcc->strms[QCS_CLT_BIDI].rx.max_data = clt_params->initial_max_stream_data_bidi_local;
24 qcc->strms[QCS_CLT_UNI].rx.max_data = clt_params->initial_max_stream_data_uni;
25
26 /* Server initiated streams must respect the server flow control. */
27 qcc->strms[QCS_SRV_BIDI].max_streams = clt_params->initial_max_streams_bidi;
28 qcc->strms[QCS_SRV_BIDI].tx.max_data = clt_params->initial_max_stream_data_bidi_remote;
29
30 qcc->strms[QCS_SRV_UNI].max_streams = clt_params->initial_max_streams_uni;
31 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 +010032}
33
Amaury Denoyelledeed7772021-12-03 11:36:46 +010034/* Allocate a new QUIC streams with id <id> and type <type>. */
35struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type)
Frédéric Lécailledfbae762021-02-18 09:59:01 +010036{
Amaury Denoyelledeed7772021-12-03 11:36:46 +010037 struct qcs *qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010038
Amaury Denoyelledeed7772021-12-03 11:36:46 +010039 qcs = pool_alloc(pool_head_qcs);
40 if (!qcs)
41 goto out;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010042
Amaury Denoyellefdbf63e2021-12-16 15:22:30 +010043 fprintf(stderr, "%s: stream ID %lu\n", __func__, id);
Frédéric Lécailledfbae762021-02-18 09:59:01 +010044
Amaury Denoyelledeed7772021-12-03 11:36:46 +010045 qcs->qcc = qcc;
46 qcs->cs = NULL;
47 qcs->flags = QC_SF_NONE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010048
Amaury Denoyelledeed7772021-12-03 11:36:46 +010049 qcs->by_id.key = id;
50 eb64_insert(&qcc->streams_by_id, &qcs->by_id);
51 qcc->strms[type].nb_streams++;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010052
Amaury Denoyelledeed7772021-12-03 11:36:46 +010053 qcs->rx.buf = BUF_NULL;
54 qcs->rx.offset = 0;
55 qcs->rx.frms = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010056
Amaury Denoyelledeed7772021-12-03 11:36:46 +010057 qcs->tx.buf = BUF_NULL;
58 qcs->tx.xprt_buf = BUF_NULL;
59 qcs->tx.offset = 0;
60 qcs->tx.ack_offset = 0;
61 qcs->tx.acked_frms = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010062
Amaury Denoyelledeed7772021-12-03 11:36:46 +010063 qcs->wait_event.tasklet = NULL;
64 qcs->wait_event.events = 0;
65 qcs->subs = NULL;
66
67 out:
68 return qcs;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010069}
70
Amaury Denoyelledeed7772021-12-03 11:36:46 +010071/* Free a qcs. This function must only be used for unidirectional streams.
72 * Bidirectional streams are released by the upper layer through qc_detach().
Frédéric Lécailledfbae762021-02-18 09:59:01 +010073 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +010074void uni_qcs_free(struct qcs *qcs)
Frédéric Lécailledfbae762021-02-18 09:59:01 +010075{
Amaury Denoyelledeed7772021-12-03 11:36:46 +010076 eb64_delete(&qcs->by_id);
77 pool_free(pool_head_qcs, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +010078}
79
Amaury Denoyelledeed7772021-12-03 11:36:46 +010080struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr)
Frédéric Lécailledfbae762021-02-18 09:59:01 +010081{
Amaury Denoyelledeed7772021-12-03 11:36:46 +010082 struct buffer *buf = b_alloc(bptr);
83 BUG_ON(!buf);
84 return buf;
Frédéric Lécailledfbae762021-02-18 09:59:01 +010085}
86
Amaury Denoyellea3f222d2021-12-06 11:24:00 +010087int qcs_subscribe(struct qcs *qcs, int event_type, struct wait_event *es)
88{
89 fprintf(stderr, "%s\n", __func__);
90
91 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
92 BUG_ON(qcs->subs && qcs->subs != es);
93
94 es->events |= event_type;
95 qcs->subs = es;
96
97 return 0;
98}
99
100void qcs_notify_recv(struct qcs *qcs)
101{
102 if (qcs->subs && qcs->subs->events & SUB_RETRY_RECV) {
103 tasklet_wakeup(qcs->subs->tasklet);
104 qcs->subs->events &= ~SUB_RETRY_RECV;
105 if (!qcs->subs->events)
106 qcs->subs = NULL;
107 }
108}
109
110void qcs_notify_send(struct qcs *qcs)
111{
112 if (qcs->subs && qcs->subs->events & SUB_RETRY_SEND) {
113 tasklet_wakeup(qcs->subs->tasklet);
114 qcs->subs->events &= ~SUB_RETRY_SEND;
115 if (!qcs->subs->events)
116 qcs->subs = NULL;
117 }
118}
119
Amaury Denoyelle8a5b27a2021-12-21 11:53:10 +0100120/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
121 * several streams, depending on the already open ones.
122 * Return this node if succeeded, NULL if not.
123 */
124struct eb64_node *qcc_get_qcs(struct qcc *qcc, uint64_t id)
125{
126 unsigned int strm_type;
127 int64_t sub_id;
128 struct eb64_node *strm_node;
129
130 strm_type = id & QCS_ID_TYPE_MASK;
131 sub_id = id >> QCS_ID_TYPE_SHIFT;
132 strm_node = NULL;
133 if (qc_local_stream_id(qcc, id)) {
134 /* Local streams: this stream must be already opened. */
135 strm_node = eb64_lookup(&qcc->streams_by_id, id);
136 if (!strm_node) {
137 /* unknown stream id */
138 goto out;
139 }
140 }
141 else {
142 /* Remote streams. */
143 struct eb_root *strms;
144 uint64_t largest_id;
145 enum qcs_type qcs_type;
146
147 strms = &qcc->streams_by_id;
148 qcs_type = qcs_id_type(id);
149 if (sub_id + 1 > qcc->strms[qcs_type].max_streams) {
150 /* streams limit reached */
151 goto out;
152 }
153
154 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
155 * correct value.
156 */
157 largest_id = qcc->strms[qcs_type].largest_id;
158 if (sub_id > (int64_t)largest_id) {
159 /* RFC: "A stream ID that is used out of order results in all streams
160 * of that type with lower-numbered stream IDs also being opened".
161 * So, let's "open" these streams.
162 */
163 int64_t i;
164 struct qcs *qcs;
165
166 qcs = NULL;
167 for (i = largest_id + 1; i <= sub_id; i++) {
168 uint64_t id = (i << QCS_ID_TYPE_SHIFT) | strm_type;
169 enum qcs_type type = id & QCS_ID_DIR_BIT ? QCS_CLT_UNI : QCS_CLT_BIDI;
170 qcs = qcs_new(qcc, id, type);
171 if (!qcs) {
172 /* allocation failure */
173 goto out;
174 }
175
176 qcc->strms[qcs_type].largest_id = i;
177 }
178 if (qcs)
179 strm_node = &qcs->by_id;
180 }
181 else {
182 strm_node = eb64_lookup(strms, id);
183 }
184 }
185
186 return strm_node;
187
188 out:
189 return NULL;
190}
191
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100192/* detachs the QUIC stream from its QCC and releases it to the QCS pool. */
193static void qcs_destroy(struct qcs *qcs)
194{
195 fprintf(stderr, "%s: release stream %llu\n", __func__, qcs->by_id.key);
196
197 eb64_delete(&qcs->by_id);
198
199 b_free(&qcs->rx.buf);
200 b_free(&qcs->tx.buf);
201 b_free(&qcs->tx.xprt_buf);
202
203 --qcs->qcc->strms[qcs_id_type(qcs->by_id.key)].nb_streams;
204
205 pool_free(pool_head_qcs, qcs);
206}
207
208static inline int qcc_is_dead(const struct qcc *qcc)
209{
210 fprintf(stderr, "%s: %lu\n", __func__, qcc->strms[QCS_CLT_BIDI].nb_streams);
211
212 if (!qcc->strms[QCS_CLT_BIDI].nb_streams)
213 return 1;
214
215 return 0;
216}
217
218/* release function. This one should be called to free all resources allocated
219 * to the mux.
220 */
221static void qc_release(struct qcc *qcc)
222{
223 struct connection *conn = NULL;
224
225 if (qcc) {
226 /* The connection must be aattached to this mux to be released */
227 if (qcc->conn && qcc->conn->ctx == qcc)
228 conn = qcc->conn;
229
230 if (qcc->wait_event.tasklet)
231 tasklet_free(qcc->wait_event.tasklet);
232
233 pool_free(pool_head_qcc, qcc);
234 }
235
236 if (conn) {
237 conn->mux = NULL;
238 conn->ctx = NULL;
239
240 conn_stop_tracking(conn);
241 conn_full_close(conn);
242 if (conn->destroy_cb)
243 conn->destroy_cb(conn);
244 conn_free(conn);
245 }
246}
247
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200248static int qcs_push_frame(struct qcs *qcs, struct buffer *payload, int fin, uint64_t offset)
249{
250 struct quic_frame *frm;
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200251 struct buffer *buf = &qcs->tx.xprt_buf;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200252 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 +0200253 int total = 0, to_xfer;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200254
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100255 fprintf(stderr, "%s\n", __func__);
256
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +0200257 qc_get_buf(qcs, buf);
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200258 to_xfer = QUIC_MIN(b_data(payload), b_room(buf));
259 if (!to_xfer)
260 goto out;
261
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200262 frm = pool_zalloc(pool_head_quic_frame);
263 if (!frm)
264 goto err;
265
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200266 total = b_force_xfer(buf, payload, to_xfer);
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100267 /* FIN is positioned only when the buffer has been totally emptied. */
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200268 fin = fin && !b_data(payload);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200269 frm->type = QUIC_FT_STREAM_8;
270 if (fin)
271 frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT;
272 if (offset) {
273 frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
274 frm->stream.offset.key = offset;
275 }
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100276 frm->stream.qcs = (struct qcs *)qcs;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200277 frm->stream.buf = buf;
278 frm->stream.id = qcs->by_id.key;
279 if (total) {
280 frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
281 frm->stream.len = total;
282 }
283
284 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécailled2ba0962021-09-20 17:50:03 +0200285 out:
Frédéric Lécaille677b99d2021-12-21 11:53:33 +0100286 fprintf(stderr, "%s: total=%d fin=%d id=%llu offset=%lu\n",
287 __func__, total, fin, (ull)qcs->by_id.key, offset);
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200288 return total;
289
290 err:
291 return -1;
292}
293
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100294static int qc_send(struct qcc *qcc)
295{
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200296 struct eb64_node *node;
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100297 int xprt_wake = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100298 int ret;
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200299
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100300 fprintf(stderr, "%s\n", __func__);
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200301
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200302 /* TODO simple loop through all streams and check if there is frames to
303 * send
304 */
305 node = eb64_first(&qcc->streams_by_id);
306 while (node) {
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200307 struct qcs *qcs = container_of(node, struct qcs, by_id);
308 struct buffer *buf = &qcs->tx.buf;
309 if (b_data(buf)) {
Amaury Denoyellefecfa0d2021-12-07 16:50:14 +0100310 char fin = qcs->flags & QC_SF_FIN_STREAM;
Amaury Denoyellec2025c12021-12-03 15:03:36 +0100311 ret = qcs_push_frame(qcs, buf, fin, qcs->tx.offset);
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200312 if (ret < 0)
313 ABORT_NOW();
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200314
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100315 if (ret > 0) {
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100316 qcs_notify_send(qcs);
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100317 if (qcs->flags & QC_SF_BLK_MROOM)
318 qcs->flags &= ~QC_SF_BLK_MROOM;
319
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100320 xprt_wake = 1;
321 }
Amaury Denoyellea543eb12021-10-06 14:53:13 +0200322
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100323 fprintf(stderr, "%s ret=%d\n", __func__, ret);
Amaury Denoyelled3d97c62021-10-05 11:45:58 +0200324 qcs->tx.offset += ret;
Amaury Denoyellea2c58a72021-12-03 14:38:31 +0100325
326 if (b_data(buf)) {
327 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
328 SUB_RETRY_SEND, &qcc->wait_event);
329 }
Frédéric Lécaille578a7892021-09-13 16:13:00 +0200330 }
331 node = eb64_next(node);
332 }
Frédéric Lécaille8526f142021-09-20 17:58:22 +0200333
Amaury Denoyellee257d9e2021-12-03 14:39:29 +0100334 if (xprt_wake)
335 tasklet_wakeup(((struct ssl_sock_ctx *)(qcc->conn->xprt_ctx))->wait_event.tasklet);
336
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100337 return ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100338}
339
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100340static int qc_release_detached_streams(struct qcc *qcc)
341{
342 struct eb64_node *node;
343 int release = 0;
344
345 node = eb64_first(&qcc->streams_by_id);
346 while (node) {
347 struct qcs *qcs = container_of(node, struct qcs, by_id);
348 node = eb64_next(node);
349
350 if (qcs->flags & QC_SF_DETACH) {
Amaury Denoyelle5154e7a2021-12-08 14:51:04 +0100351 if ((!b_data(&qcs->tx.buf) && !b_data(&qcs->tx.xprt_buf)) ||
352 qcc->flags & QC_CF_CC_RECV) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100353 qcs_destroy(qcs);
354 release = 1;
355 }
356 else {
357 qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx,
358 SUB_RETRY_SEND, &qcc->wait_event);
359 }
360 }
361 }
362
363 return release;
364}
365
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100366static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status)
367{
Amaury Denoyelle769e9ff2021-10-05 11:43:50 +0200368 struct qcc *qcc = ctx;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100369
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100370 fprintf(stderr, "%s\n", __func__);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100371
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100372 qc_send(qcc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100373
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100374 if (qc_release_detached_streams(qcc)) {
375 if (qcc_is_dead(qcc)) {
376 qc_release(qcc);
377 return NULL;
378 }
379 }
380
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100381 return NULL;
382}
383
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100384static int qc_init(struct connection *conn, struct proxy *prx,
385 struct session *sess, struct buffer *input)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100386{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100387 struct qcc *qcc;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100388 struct quic_transport_params *srv_params;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100389
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100390 qcc = pool_alloc(pool_head_qcc);
391 if (!qcc)
392 goto fail_no_qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100393
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100394 qcc->conn = conn;
395 conn->ctx = qcc;
396 conn->qc->qcc = qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100397
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100398 qcc->app_ops = NULL;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100399
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100400 qcc->streams_by_id = EB_ROOT_UNIQUE;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100401
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100402 /* Server parameters, params used for RX flow control. */
403 srv_params = &conn->qc->rx.params;
404
405 qcc->rx.max_data = srv_params->initial_max_data;
406 qcc->tx.max_data = 0;
407
408 /* Client initiated streams must respect the server flow control. */
409 qcc->strms[QCS_CLT_BIDI].max_streams = srv_params->initial_max_streams_bidi;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100410 qcc->strms[QCS_CLT_BIDI].nb_streams = 0;
411 qcc->strms[QCS_CLT_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100412 qcc->strms[QCS_CLT_BIDI].rx.max_data = 0;
413 qcc->strms[QCS_CLT_BIDI].tx.max_data = srv_params->initial_max_stream_data_bidi_remote;
414
415 qcc->strms[QCS_CLT_UNI].max_streams = srv_params->initial_max_streams_uni;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100416 qcc->strms[QCS_CLT_UNI].nb_streams = 0;
417 qcc->strms[QCS_CLT_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100418 qcc->strms[QCS_CLT_UNI].rx.max_data = 0;
419 qcc->strms[QCS_CLT_UNI].tx.max_data = srv_params->initial_max_stream_data_uni;
420
421 /* Server initiated streams must respect the server flow control. */
422 qcc->strms[QCS_SRV_BIDI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100423 qcc->strms[QCS_SRV_BIDI].nb_streams = 0;
424 qcc->strms[QCS_SRV_BIDI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100425 qcc->strms[QCS_SRV_BIDI].rx.max_data = srv_params->initial_max_stream_data_bidi_local;
426 qcc->strms[QCS_SRV_BIDI].tx.max_data = 0;
427
428 qcc->strms[QCS_SRV_UNI].max_streams = 0;
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100429 qcc->strms[QCS_SRV_UNI].nb_streams = 0;
430 qcc->strms[QCS_SRV_UNI].largest_id = -1;
Amaury Denoyellef3b0ba72021-12-08 15:12:01 +0100431 qcc->strms[QCS_SRV_UNI].rx.max_data = srv_params->initial_max_stream_data_uni;
432 qcc->strms[QCS_SRV_UNI].tx.max_data = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100433
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100434 qcc->wait_event.tasklet = tasklet_new();
435 if (!qcc->wait_event.tasklet)
436 goto fail_no_tasklet;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100437
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100438 qcc->subs = NULL;
439 qcc->wait_event.tasklet->process = qc_io_cb;
440 qcc->wait_event.tasklet->context = qcc;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100441
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100442 /* init read cycle */
443 tasklet_wakeup(qcc->wait_event.tasklet);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100444
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100445 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100446
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100447 fail_no_tasklet:
448 pool_free(pool_head_qcc, qcc);
449 fail_no_qcc:
450 return -1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100451}
452
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100453static void qc_detach(struct conn_stream *cs)
454{
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +0100455 struct qcs *qcs = cs->ctx;
456 struct qcc *qcc = qcs->qcc;
457
458 fprintf(stderr, "%s: leaving with tx.buf.data=%lu, tx.xprt_buf.data=%lu\n",
459 __func__, b_data(&qcs->tx.buf), b_data(&qcs->tx.xprt_buf));
460
Amaury Denoyelle5154e7a2021-12-08 14:51:04 +0100461 if ((b_data(&qcs->tx.buf) || b_data(&qcs->tx.xprt_buf)) &&
462 !(qcc->flags & QC_CF_CC_RECV)) {
Amaury Denoyelle2873a312021-12-08 14:42:55 +0100463 qcs->flags |= QC_SF_DETACH;
464 return;
465 }
466
Amaury Denoyelle916f0ac2021-12-06 16:03:47 +0100467 qcs_destroy(qcs);
468 if (qcc_is_dead(qcc)) {
469 qc_release(qcc);
470 return;
471 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100472}
473
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100474/* Called from the upper layer, to receive data */
475static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf,
476 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100477{
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100478 /* XXX TODO XXX */
479 fprintf(stderr, "%s\n", __func__);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100480
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100481 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100482}
483
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100484static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf,
485 size_t count, int flags)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100486{
487 struct qcs *qcs = cs->ctx;
488
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100489 fprintf(stderr, "%s\n", __func__);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100490
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100491 return qcs->qcc->app_ops->snd_buf(cs, buf, count, flags);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100492}
493
494/* Called from the upper layer, to subscribe <es> to events <event_type>. The
495 * event subscriber <es> is not allowed to change from a previous call as long
496 * as at least one event is still subscribed. The <event_type> must only be a
497 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
498 */
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100499static int qc_subscribe(struct conn_stream *cs, int event_type,
500 struct wait_event *es)
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100501{
Amaury Denoyellea3f222d2021-12-06 11:24:00 +0100502 return qcs_subscribe(cs->ctx, event_type, es);
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100503}
504
505/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
506 * The <es> pointer is not allowed to differ from the one passed to the
507 * subscribe() call. It always returns zero.
508 */
509static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
510{
511 struct qcs *qcs = cs->ctx;
512
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100513 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
514 BUG_ON(qcs->subs && qcs->subs != es);
515
516 es->events &= ~event_type;
517 if (!es->events)
518 qcs->subs = NULL;
519
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100520 return 0;
521}
522
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100523static const struct mux_ops qc_ops = {
524 .init = qc_init,
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100525 .detach = qc_detach,
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100526 .rcv_buf = qc_rcv_buf,
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100527 .snd_buf = qc_snd_buf,
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100528 .subscribe = qc_subscribe,
529 .unsubscribe = qc_unsubscribe,
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100530};
531
532static struct mux_proto_list mux_proto_quic =
Amaury Denoyelledeed7772021-12-03 11:36:46 +0100533 { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops };
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100534
535INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic);