Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 1 | #include <haproxy/mux_quic.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 2 | |
Amaury Denoyelle | eb01f59 | 2021-10-07 16:44:05 +0200 | [diff] [blame] | 3 | #include <import/eb64tree.h> |
| 4 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 5 | #include <haproxy/api.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 6 | #include <haproxy/connection.h> |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 7 | #include <haproxy/dynbuf.h> |
| 8 | #include <haproxy/pool.h> |
Amaury Denoyelle | eb01f59 | 2021-10-07 16:44:05 +0200 | [diff] [blame] | 9 | #include <haproxy/ssl_sock-t.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 10 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 11 | DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc)); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 12 | DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs)); |
| 13 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 14 | void quic_mux_transport_params_update(struct qcc *qcc) |
| 15 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 16 | struct quic_transport_params *clt_params; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 17 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 18 | /* Client parameters, params used to TX. */ |
| 19 | clt_params = &qcc->conn->qc->tx.params; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 20 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 21 | 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écaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 32 | } |
| 33 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 34 | /* Allocate a new QUIC streams with id <id> and type <type>. */ |
| 35 | struct qcs *qcs_new(struct qcc *qcc, uint64_t id, enum qcs_type type) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 36 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 37 | struct qcs *qcs; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 38 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 39 | qcs = pool_alloc(pool_head_qcs); |
| 40 | if (!qcs) |
| 41 | goto out; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 42 | |
Amaury Denoyelle | fdbf63e | 2021-12-16 15:22:30 +0100 | [diff] [blame] | 43 | fprintf(stderr, "%s: stream ID %lu\n", __func__, id); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 44 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 45 | qcs->qcc = qcc; |
| 46 | qcs->cs = NULL; |
| 47 | qcs->flags = QC_SF_NONE; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 48 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 49 | 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écaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 52 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 53 | qcs->rx.buf = BUF_NULL; |
| 54 | qcs->rx.offset = 0; |
| 55 | qcs->rx.frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 56 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 57 | 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écaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 62 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 63 | 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écaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 69 | } |
| 70 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 71 | /* 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écaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 73 | */ |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 74 | void uni_qcs_free(struct qcs *qcs) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 75 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 76 | eb64_delete(&qcs->by_id); |
| 77 | pool_free(pool_head_qcs, qcs); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 80 | struct buffer *qc_get_buf(struct qcs *qcs, struct buffer *bptr) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 81 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 82 | struct buffer *buf = b_alloc(bptr); |
| 83 | BUG_ON(!buf); |
| 84 | return buf; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 87 | int 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 | |
| 100 | void 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 | |
| 110 | void 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 Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 120 | /* 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 | */ |
| 124 | struct 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 | |
Ilya Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 192 | /* detaches the QUIC stream from its QCC and releases it to the QCS pool. */ |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 193 | static 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 | |
| 208 | static 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 | */ |
| 221 | static 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) { |
Frédéric Lécaille | 19cd46e | 2022-01-10 11:40:33 +0100 | [diff] [blame] | 237 | conn->qc->conn = NULL; |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 238 | conn->mux = NULL; |
| 239 | conn->ctx = NULL; |
| 240 | |
| 241 | conn_stop_tracking(conn); |
| 242 | conn_full_close(conn); |
| 243 | if (conn->destroy_cb) |
| 244 | conn->destroy_cb(conn); |
| 245 | conn_free(conn); |
Frédéric Lécaille | 19cd46e | 2022-01-10 11:40:33 +0100 | [diff] [blame] | 246 | fprintf(stderr, "conn@%p released\n", conn); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 250 | static int qcs_push_frame(struct qcs *qcs, struct buffer *payload, int fin, uint64_t offset) |
| 251 | { |
| 252 | struct quic_frame *frm; |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 253 | struct buffer *buf = &qcs->tx.xprt_buf; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 254 | struct quic_enc_level *qel = &qcs->qcc->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP]; |
Frédéric Lécaille | d2ba096 | 2021-09-20 17:50:03 +0200 | [diff] [blame] | 255 | int total = 0, to_xfer; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 256 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 257 | fprintf(stderr, "%s\n", __func__); |
| 258 | |
Amaury Denoyelle | 1e308ff | 2021-10-12 18:14:12 +0200 | [diff] [blame] | 259 | qc_get_buf(qcs, buf); |
Frédéric Lécaille | d2ba096 | 2021-09-20 17:50:03 +0200 | [diff] [blame] | 260 | to_xfer = QUIC_MIN(b_data(payload), b_room(buf)); |
| 261 | if (!to_xfer) |
| 262 | goto out; |
| 263 | |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 264 | frm = pool_zalloc(pool_head_quic_frame); |
| 265 | if (!frm) |
| 266 | goto err; |
| 267 | |
Frédéric Lécaille | d2ba096 | 2021-09-20 17:50:03 +0200 | [diff] [blame] | 268 | total = b_force_xfer(buf, payload, to_xfer); |
Amaury Denoyelle | fecfa0d | 2021-12-07 16:50:14 +0100 | [diff] [blame] | 269 | /* FIN is positioned only when the buffer has been totally emptied. */ |
Frédéric Lécaille | d2ba096 | 2021-09-20 17:50:03 +0200 | [diff] [blame] | 270 | fin = fin && !b_data(payload); |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 271 | frm->type = QUIC_FT_STREAM_8; |
| 272 | if (fin) |
| 273 | frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 274 | if (offset) { |
| 275 | frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 276 | frm->stream.offset.key = offset; |
| 277 | } |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 278 | frm->stream.qcs = (struct qcs *)qcs; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 279 | frm->stream.buf = buf; |
| 280 | frm->stream.id = qcs->by_id.key; |
| 281 | if (total) { |
| 282 | frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 283 | frm->stream.len = total; |
| 284 | } |
| 285 | |
Frédéric Lécaille | 82468ea | 2022-01-14 20:23:22 +0100 | [diff] [blame] | 286 | LIST_APPEND(&qel->pktns->tx.frms, &frm->list); |
Frédéric Lécaille | d2ba096 | 2021-09-20 17:50:03 +0200 | [diff] [blame] | 287 | out: |
Frédéric Lécaille | 677b99d | 2021-12-21 11:53:33 +0100 | [diff] [blame] | 288 | fprintf(stderr, "%s: total=%d fin=%d id=%llu offset=%lu\n", |
| 289 | __func__, total, fin, (ull)qcs->by_id.key, offset); |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 290 | return total; |
| 291 | |
| 292 | err: |
| 293 | return -1; |
| 294 | } |
| 295 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 296 | static int qc_send(struct qcc *qcc) |
| 297 | { |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 298 | struct eb64_node *node; |
Amaury Denoyelle | e257d9e | 2021-12-03 14:39:29 +0100 | [diff] [blame] | 299 | int xprt_wake = 0; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 300 | int ret; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 301 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 302 | fprintf(stderr, "%s\n", __func__); |
Frédéric Lécaille | 8526f14 | 2021-09-20 17:58:22 +0200 | [diff] [blame] | 303 | |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 304 | /* TODO simple loop through all streams and check if there is frames to |
| 305 | * send |
| 306 | */ |
| 307 | node = eb64_first(&qcc->streams_by_id); |
| 308 | while (node) { |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 309 | struct qcs *qcs = container_of(node, struct qcs, by_id); |
| 310 | struct buffer *buf = &qcs->tx.buf; |
| 311 | if (b_data(buf)) { |
Amaury Denoyelle | fecfa0d | 2021-12-07 16:50:14 +0100 | [diff] [blame] | 312 | char fin = qcs->flags & QC_SF_FIN_STREAM; |
Amaury Denoyelle | c2025c1 | 2021-12-03 15:03:36 +0100 | [diff] [blame] | 313 | ret = qcs_push_frame(qcs, buf, fin, qcs->tx.offset); |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 314 | if (ret < 0) |
| 315 | ABORT_NOW(); |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 316 | |
Amaury Denoyelle | e257d9e | 2021-12-03 14:39:29 +0100 | [diff] [blame] | 317 | if (ret > 0) { |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 318 | qcs_notify_send(qcs); |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 319 | if (qcs->flags & QC_SF_BLK_MROOM) |
| 320 | qcs->flags &= ~QC_SF_BLK_MROOM; |
| 321 | |
Amaury Denoyelle | e257d9e | 2021-12-03 14:39:29 +0100 | [diff] [blame] | 322 | xprt_wake = 1; |
| 323 | } |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 324 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 325 | fprintf(stderr, "%s ret=%d\n", __func__, ret); |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 326 | qcs->tx.offset += ret; |
Amaury Denoyelle | a2c58a7 | 2021-12-03 14:38:31 +0100 | [diff] [blame] | 327 | |
| 328 | if (b_data(buf)) { |
| 329 | qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx, |
| 330 | SUB_RETRY_SEND, &qcc->wait_event); |
| 331 | } |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 332 | } |
| 333 | node = eb64_next(node); |
| 334 | } |
Frédéric Lécaille | 8526f14 | 2021-09-20 17:58:22 +0200 | [diff] [blame] | 335 | |
Amaury Denoyelle | e257d9e | 2021-12-03 14:39:29 +0100 | [diff] [blame] | 336 | if (xprt_wake) |
| 337 | tasklet_wakeup(((struct ssl_sock_ctx *)(qcc->conn->xprt_ctx))->wait_event.tasklet); |
| 338 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 339 | return ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 340 | } |
| 341 | |
Amaury Denoyelle | 6a4aebf | 2022-02-01 10:16:05 +0100 | [diff] [blame] | 342 | /* Release all streams that are already marked as detached. This is only done |
| 343 | * if their TX buffers are empty or if a CONNECTION_CLOSE has been received. |
| 344 | * |
| 345 | * Return the number of released stream. |
| 346 | */ |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 347 | static int qc_release_detached_streams(struct qcc *qcc) |
| 348 | { |
| 349 | struct eb64_node *node; |
| 350 | int release = 0; |
| 351 | |
| 352 | node = eb64_first(&qcc->streams_by_id); |
| 353 | while (node) { |
| 354 | struct qcs *qcs = container_of(node, struct qcs, by_id); |
| 355 | node = eb64_next(node); |
| 356 | |
| 357 | if (qcs->flags & QC_SF_DETACH) { |
Amaury Denoyelle | 5154e7a | 2021-12-08 14:51:04 +0100 | [diff] [blame] | 358 | if ((!b_data(&qcs->tx.buf) && !b_data(&qcs->tx.xprt_buf)) || |
| 359 | qcc->flags & QC_CF_CC_RECV) { |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 360 | qcs_destroy(qcs); |
| 361 | release = 1; |
| 362 | } |
| 363 | else { |
| 364 | qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx, |
| 365 | SUB_RETRY_SEND, &qcc->wait_event); |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | return release; |
| 371 | } |
| 372 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 373 | static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status) |
| 374 | { |
Amaury Denoyelle | 769e9ff | 2021-10-05 11:43:50 +0200 | [diff] [blame] | 375 | struct qcc *qcc = ctx; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 376 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 377 | fprintf(stderr, "%s\n", __func__); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 378 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 379 | qc_send(qcc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 380 | |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 381 | if (qc_release_detached_streams(qcc)) { |
| 382 | if (qcc_is_dead(qcc)) { |
| 383 | qc_release(qcc); |
| 384 | return NULL; |
| 385 | } |
| 386 | } |
| 387 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 388 | return NULL; |
| 389 | } |
| 390 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 391 | static int qc_init(struct connection *conn, struct proxy *prx, |
| 392 | struct session *sess, struct buffer *input) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 393 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 394 | struct qcc *qcc; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 395 | struct quic_transport_params *srv_params; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 396 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 397 | qcc = pool_alloc(pool_head_qcc); |
| 398 | if (!qcc) |
| 399 | goto fail_no_qcc; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 400 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 401 | qcc->conn = conn; |
| 402 | conn->ctx = qcc; |
Amaury Denoyelle | ce1f30d | 2022-02-01 15:14:24 +0100 | [diff] [blame^] | 403 | qcc->flags = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 404 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 405 | qcc->app_ops = NULL; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 406 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 407 | qcc->streams_by_id = EB_ROOT_UNIQUE; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 408 | |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 409 | /* Server parameters, params used for RX flow control. */ |
| 410 | srv_params = &conn->qc->rx.params; |
| 411 | |
| 412 | qcc->rx.max_data = srv_params->initial_max_data; |
| 413 | qcc->tx.max_data = 0; |
| 414 | |
| 415 | /* Client initiated streams must respect the server flow control. */ |
| 416 | qcc->strms[QCS_CLT_BIDI].max_streams = srv_params->initial_max_streams_bidi; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 417 | qcc->strms[QCS_CLT_BIDI].nb_streams = 0; |
| 418 | qcc->strms[QCS_CLT_BIDI].largest_id = -1; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 419 | qcc->strms[QCS_CLT_BIDI].rx.max_data = 0; |
| 420 | qcc->strms[QCS_CLT_BIDI].tx.max_data = srv_params->initial_max_stream_data_bidi_remote; |
| 421 | |
| 422 | qcc->strms[QCS_CLT_UNI].max_streams = srv_params->initial_max_streams_uni; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 423 | qcc->strms[QCS_CLT_UNI].nb_streams = 0; |
| 424 | qcc->strms[QCS_CLT_UNI].largest_id = -1; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 425 | qcc->strms[QCS_CLT_UNI].rx.max_data = 0; |
| 426 | qcc->strms[QCS_CLT_UNI].tx.max_data = srv_params->initial_max_stream_data_uni; |
| 427 | |
| 428 | /* Server initiated streams must respect the server flow control. */ |
| 429 | qcc->strms[QCS_SRV_BIDI].max_streams = 0; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 430 | qcc->strms[QCS_SRV_BIDI].nb_streams = 0; |
| 431 | qcc->strms[QCS_SRV_BIDI].largest_id = -1; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 432 | qcc->strms[QCS_SRV_BIDI].rx.max_data = srv_params->initial_max_stream_data_bidi_local; |
| 433 | qcc->strms[QCS_SRV_BIDI].tx.max_data = 0; |
| 434 | |
| 435 | qcc->strms[QCS_SRV_UNI].max_streams = 0; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 436 | qcc->strms[QCS_SRV_UNI].nb_streams = 0; |
| 437 | qcc->strms[QCS_SRV_UNI].largest_id = -1; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 438 | qcc->strms[QCS_SRV_UNI].rx.max_data = srv_params->initial_max_stream_data_uni; |
| 439 | qcc->strms[QCS_SRV_UNI].tx.max_data = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 440 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 441 | qcc->wait_event.tasklet = tasklet_new(); |
| 442 | if (!qcc->wait_event.tasklet) |
| 443 | goto fail_no_tasklet; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 444 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 445 | qcc->subs = NULL; |
| 446 | qcc->wait_event.tasklet->process = qc_io_cb; |
| 447 | qcc->wait_event.tasklet->context = qcc; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 448 | |
Frédéric Lécaille | b80b20c | 2022-01-12 17:46:56 +0100 | [diff] [blame] | 449 | HA_ATOMIC_STORE(&conn->qc->qcc, qcc); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 450 | /* init read cycle */ |
| 451 | tasklet_wakeup(qcc->wait_event.tasklet); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 452 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 453 | return 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 454 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 455 | fail_no_tasklet: |
| 456 | pool_free(pool_head_qcc, qcc); |
| 457 | fail_no_qcc: |
| 458 | return -1; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 459 | } |
| 460 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 461 | static void qc_detach(struct conn_stream *cs) |
| 462 | { |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 463 | struct qcs *qcs = cs->ctx; |
| 464 | struct qcc *qcc = qcs->qcc; |
| 465 | |
| 466 | fprintf(stderr, "%s: leaving with tx.buf.data=%lu, tx.xprt_buf.data=%lu\n", |
| 467 | __func__, b_data(&qcs->tx.buf), b_data(&qcs->tx.xprt_buf)); |
| 468 | |
Amaury Denoyelle | 5154e7a | 2021-12-08 14:51:04 +0100 | [diff] [blame] | 469 | if ((b_data(&qcs->tx.buf) || b_data(&qcs->tx.xprt_buf)) && |
| 470 | !(qcc->flags & QC_CF_CC_RECV)) { |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 471 | qcs->flags |= QC_SF_DETACH; |
| 472 | return; |
| 473 | } |
| 474 | |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 475 | qcs_destroy(qcs); |
| 476 | if (qcc_is_dead(qcc)) { |
| 477 | qc_release(qcc); |
| 478 | return; |
| 479 | } |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 480 | } |
| 481 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 482 | /* Called from the upper layer, to receive data */ |
| 483 | static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf, |
| 484 | size_t count, int flags) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 485 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 486 | /* XXX TODO XXX */ |
| 487 | fprintf(stderr, "%s\n", __func__); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 488 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 489 | return 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 490 | } |
| 491 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 492 | static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf, |
| 493 | size_t count, int flags) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 494 | { |
| 495 | struct qcs *qcs = cs->ctx; |
| 496 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 497 | fprintf(stderr, "%s\n", __func__); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 498 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 499 | return qcs->qcc->app_ops->snd_buf(cs, buf, count, flags); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 503 | * event subscriber <es> is not allowed to change from a previous call as long |
| 504 | * as at least one event is still subscribed. The <event_type> must only be a |
| 505 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 506 | */ |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 507 | static int qc_subscribe(struct conn_stream *cs, int event_type, |
| 508 | struct wait_event *es) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 509 | { |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 510 | return qcs_subscribe(cs->ctx, event_type, es); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 514 | * The <es> pointer is not allowed to differ from the one passed to the |
| 515 | * subscribe() call. It always returns zero. |
| 516 | */ |
| 517 | static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es) |
| 518 | { |
| 519 | struct qcs *qcs = cs->ctx; |
| 520 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 521 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 522 | BUG_ON(qcs->subs && qcs->subs != es); |
| 523 | |
| 524 | es->events &= ~event_type; |
| 525 | if (!es->events) |
| 526 | qcs->subs = NULL; |
| 527 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 528 | return 0; |
| 529 | } |
| 530 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 531 | static const struct mux_ops qc_ops = { |
| 532 | .init = qc_init, |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 533 | .detach = qc_detach, |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 534 | .rcv_buf = qc_rcv_buf, |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 535 | .snd_buf = qc_snd_buf, |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 536 | .subscribe = qc_subscribe, |
| 537 | .unsubscribe = qc_unsubscribe, |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 538 | }; |
| 539 | |
| 540 | static struct mux_proto_list mux_proto_quic = |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 541 | { .token = IST("quic"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &qc_ops }; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 542 | |
| 543 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic); |