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> |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 7 | #include <haproxy/conn_stream.h> |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 8 | #include <haproxy/dynbuf.h> |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 9 | #include <haproxy/htx.h> |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 10 | #include <haproxy/pool.h> |
Amaury Denoyelle | eb01f59 | 2021-10-07 16:44:05 +0200 | [diff] [blame] | 11 | #include <haproxy/ssl_sock-t.h> |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 12 | #include <haproxy/xprt_quic.h> |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 13 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 14 | DECLARE_POOL(pool_head_qcc, "qcc", sizeof(struct qcc)); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 15 | DECLARE_POOL(pool_head_qcs, "qcs", sizeof(struct qcs)); |
| 16 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 17 | /* Allocate a new QUIC streams with id <id> and type <type>. */ |
| 18 | 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] | 19 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 20 | struct qcs *qcs; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 21 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 22 | qcs = pool_alloc(pool_head_qcs); |
| 23 | if (!qcs) |
| 24 | goto out; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 25 | |
Amaury Denoyelle | fdbf63e | 2021-12-16 15:22:30 +0100 | [diff] [blame] | 26 | fprintf(stderr, "%s: stream ID %lu\n", __func__, id); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 27 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 28 | qcs->qcc = qcc; |
| 29 | qcs->cs = NULL; |
| 30 | qcs->flags = QC_SF_NONE; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 31 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 32 | 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écaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 35 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 36 | qcs->rx.buf = BUF_NULL; |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 37 | qcs->rx.app_buf = BUF_NULL; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 38 | qcs->rx.offset = 0; |
| 39 | qcs->rx.frms = EB_ROOT_UNIQUE; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 40 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 41 | 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écaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 46 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 47 | 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écaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 55 | /* 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écaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 57 | */ |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 58 | void uni_qcs_free(struct qcs *qcs) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 59 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 60 | eb64_delete(&qcs->by_id); |
| 61 | pool_free(pool_head_qcs, qcs); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 64 | 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] | 65 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 66 | struct buffer *buf = b_alloc(bptr); |
| 67 | BUG_ON(!buf); |
| 68 | return buf; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 69 | } |
| 70 | |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 71 | int 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 | |
| 84 | void 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 | |
| 94 | void 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 Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 104 | /* 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 | */ |
| 108 | struct 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 Denoyelle | 0dc40f0 | 2022-02-07 11:44:17 +0100 | [diff] [blame] | 117 | if (quic_stream_is_local(qcc, id)) { |
Amaury Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 118 | /* 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 Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 133 | |
| 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 Denoyelle | 8a5b27a | 2021-12-21 11:53:10 +0100 | [diff] [blame] | 140 | } |
| 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 Denoyelle | 0e3010b | 2022-02-28 11:37:48 +0100 | [diff] [blame] | 180 | /* 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 | */ |
| 190 | int 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 | */ |
| 255 | int 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 Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 265 | static 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 Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 270 | /* 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] | 271 | static void qcs_destroy(struct qcs *qcs) |
| 272 | { |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 273 | const uint64_t id = qcs->by_id.key; |
| 274 | |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 275 | fprintf(stderr, "%s: release stream %llu\n", __func__, qcs->by_id.key); |
| 276 | |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 277 | 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 Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 285 | 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 | |
| 296 | static 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 Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 300 | 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. */ |
| 307 | static 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 Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 313 | 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 | */ |
| 322 | static 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 Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 338 | LIST_DEL_INIT(&conn->stopping_list); |
| 339 | |
Frédéric Lécaille | 19cd46e | 2022-01-10 11:40:33 +0100 | [diff] [blame] | 340 | conn->qc->conn = NULL; |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 341 | 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écaille | 19cd46e | 2022-01-10 11:40:33 +0100 | [diff] [blame] | 349 | fprintf(stderr, "conn@%p released\n", conn); |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 353 | static int qcs_push_frame(struct qcs *qcs, struct buffer *payload, int fin, uint64_t offset, |
| 354 | struct list *frm_list) |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 355 | { |
| 356 | struct quic_frame *frm; |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 357 | struct buffer *buf = &qcs->tx.xprt_buf; |
Frédéric Lécaille | d2ba096 | 2021-09-20 17:50:03 +0200 | [diff] [blame] | 358 | int total = 0, to_xfer; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 359 | unsigned char *btail; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 360 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 361 | fprintf(stderr, "%s\n", __func__); |
| 362 | |
Amaury Denoyelle | 1e308ff | 2021-10-12 18:14:12 +0200 | [diff] [blame] | 363 | qc_get_buf(qcs, buf); |
Frédéric Lécaille | d2ba096 | 2021-09-20 17:50:03 +0200 | [diff] [blame] | 364 | to_xfer = QUIC_MIN(b_data(payload), b_room(buf)); |
| 365 | if (!to_xfer) |
| 366 | goto out; |
| 367 | |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 368 | frm = pool_zalloc(pool_head_quic_frame); |
| 369 | if (!frm) |
| 370 | goto err; |
| 371 | |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 372 | /* store buffer end before transfering data for frm.stream.data */ |
| 373 | btail = (unsigned char *)b_tail(buf); |
Frédéric Lécaille | d2ba096 | 2021-09-20 17:50:03 +0200 | [diff] [blame] | 374 | total = b_force_xfer(buf, payload, to_xfer); |
Amaury Denoyelle | fecfa0d | 2021-12-07 16:50:14 +0100 | [diff] [blame] | 375 | /* 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] | 376 | fin = fin && !b_data(payload); |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 377 | 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 Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 384 | frm->stream.qcs = (struct qcs *)qcs; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 385 | frm->stream.buf = buf; |
Amaury Denoyelle | 642ab06 | 2022-02-23 10:54:42 +0100 | [diff] [blame] | 386 | frm->stream.data = btail; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 387 | 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 Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 393 | LIST_APPEND(frm_list, &frm->list); |
Frédéric Lécaille | d2ba096 | 2021-09-20 17:50:03 +0200 | [diff] [blame] | 394 | out: |
Frédéric Lécaille | 677b99d | 2021-12-21 11:53:33 +0100 | [diff] [blame] | 395 | 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écaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 397 | return total; |
| 398 | |
| 399 | err: |
| 400 | return -1; |
| 401 | } |
| 402 | |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 403 | /* 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 | */ |
| 408 | static int qc_send_frames(struct qcc *qcc, struct list *frms) |
| 409 | { |
Amaury Denoyelle | e9c4cc1 | 2022-03-04 15:29:53 +0100 | [diff] [blame] | 410 | void *first_frm = NULL; |
| 411 | |
| 412 | retry_send: |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 413 | if (!LIST_ISEMPTY(frms)) |
| 414 | qc_send_app_pkts(qcc->conn->qc, frms); |
| 415 | |
Amaury Denoyelle | e9c4cc1 | 2022-03-04 15:29:53 +0100 | [diff] [blame] | 416 | /* 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 Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 422 | */ |
Amaury Denoyelle | e9c4cc1 | 2022-03-04 15:29:53 +0100 | [diff] [blame] | 423 | if (!LIST_ISEMPTY(frms) && first_frm != frms->n) { |
| 424 | first_frm = frms->n; |
| 425 | goto retry_send; |
| 426 | } |
| 427 | |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 428 | 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écaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 438 | static int qc_send(struct qcc *qcc) |
| 439 | { |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 440 | struct eb64_node *node; |
Amaury Denoyelle | c0b66ca | 2022-02-21 18:45:22 +0100 | [diff] [blame] | 441 | int ret = 0; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 442 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 443 | fprintf(stderr, "%s\n", __func__); |
Frédéric Lécaille | 8526f14 | 2021-09-20 17:58:22 +0200 | [diff] [blame] | 444 | |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 445 | /* 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écaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 447 | */ |
| 448 | node = eb64_first(&qcc->streams_by_id); |
| 449 | while (node) { |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 450 | struct qcs *qcs = container_of(node, struct qcs, by_id); |
| 451 | struct buffer *buf = &qcs->tx.buf; |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 452 | |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 453 | if (b_data(buf)) { |
Amaury Denoyelle | fecfa0d | 2021-12-07 16:50:14 +0100 | [diff] [blame] | 454 | char fin = qcs->flags & QC_SF_FIN_STREAM; |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 455 | ret = qcs_push_frame(qcs, buf, fin, qcs->tx.offset, |
| 456 | &qcc->tx.frms); |
Amaury Denoyelle | 1455113 | 2022-03-04 16:51:20 +0100 | [diff] [blame] | 457 | BUG_ON(ret < 0); /* TODO handle this properly */ |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 458 | |
Amaury Denoyelle | e257d9e | 2021-12-03 14:39:29 +0100 | [diff] [blame] | 459 | if (ret > 0) { |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 460 | qcs_notify_send(qcs); |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 461 | if (qcs->flags & QC_SF_BLK_MROOM) |
| 462 | qcs->flags &= ~QC_SF_BLK_MROOM; |
Amaury Denoyelle | e257d9e | 2021-12-03 14:39:29 +0100 | [diff] [blame] | 463 | } |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 464 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 465 | fprintf(stderr, "%s ret=%d\n", __func__, ret); |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 466 | qcs->tx.offset += ret; |
Amaury Denoyelle | a2c58a7 | 2021-12-03 14:38:31 +0100 | [diff] [blame] | 467 | |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 468 | /* Subscribe if not all data can be send. */ |
Amaury Denoyelle | a2c58a7 | 2021-12-03 14:38:31 +0100 | [diff] [blame] | 469 | 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écaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 473 | } |
| 474 | node = eb64_next(node); |
| 475 | } |
Frédéric Lécaille | 8526f14 | 2021-09-20 17:58:22 +0200 | [diff] [blame] | 476 | |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 477 | qc_send_frames(qcc, &qcc->tx.frms); |
| 478 | /* TODO adjust ret if not all frames are sent. */ |
Amaury Denoyelle | e257d9e | 2021-12-03 14:39:29 +0100 | [diff] [blame] | 479 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 480 | return ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 481 | } |
| 482 | |
Amaury Denoyelle | 6a4aebf | 2022-02-01 10:16:05 +0100 | [diff] [blame] | 483 | /* 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 Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 488 | static 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 Denoyelle | d975148 | 2022-02-01 15:15:11 +0100 | [diff] [blame] | 499 | if ((!b_data(&qcs->tx.buf) && !b_data(&qcs->tx.xprt_buf))) { |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 500 | 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 Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 513 | /* 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 | */ |
| 519 | static 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écaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 543 | static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status) |
| 544 | { |
Amaury Denoyelle | 769e9ff | 2021-10-05 11:43:50 +0200 | [diff] [blame] | 545 | struct qcc *qcc = ctx; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 546 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 547 | fprintf(stderr, "%s\n", __func__); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 548 | |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 549 | if (qc_is_max_streams_needed(qcc)) |
| 550 | qc_send_max_streams(qcc); |
| 551 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 552 | qc_send(qcc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 553 | |
Amaury Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 554 | if (qc_release_detached_streams(qcc)) { |
Amaury Denoyelle | 1136e92 | 2022-02-01 10:33:09 +0100 | [diff] [blame] | 555 | /* 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 Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 562 | return NULL; |
| 563 | } |
| 564 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 565 | static 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 Denoyelle | ea3e035 | 2022-02-21 10:05:16 +0100 | [diff] [blame] | 587 | |
| 588 | if (!qcc) |
| 589 | return NULL; |
| 590 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 591 | qcc->task = NULL; |
| 592 | |
| 593 | if (qcc_is_dead(qcc)) |
| 594 | qc_release(qcc); |
| 595 | |
| 596 | return NULL; |
| 597 | } |
| 598 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 599 | static int qc_init(struct connection *conn, struct proxy *prx, |
| 600 | struct session *sess, struct buffer *input) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 601 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 602 | struct qcc *qcc; |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 603 | struct quic_transport_params *lparams; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 604 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 605 | qcc = pool_alloc(pool_head_qcc); |
| 606 | if (!qcc) |
| 607 | goto fail_no_qcc; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 608 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 609 | qcc->conn = conn; |
| 610 | conn->ctx = qcc; |
Amaury Denoyelle | ce1f30d | 2022-02-01 15:14:24 +0100 | [diff] [blame] | 611 | qcc->flags = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 612 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 613 | qcc->app_ops = NULL; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 614 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 615 | qcc->streams_by_id = EB_ROOT_UNIQUE; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 616 | |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 617 | /* Server parameters, params used for RX flow control. */ |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 618 | lparams = &conn->qc->rx.params; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 619 | |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 620 | qcc->rx.max_data = lparams->initial_max_data; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 621 | qcc->tx.max_data = 0; |
Amaury Denoyelle | 2c71fe5 | 2022-02-09 18:16:49 +0100 | [diff] [blame] | 622 | LIST_INIT(&qcc->tx.frms); |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 623 | |
| 624 | /* Client initiated streams must respect the server flow control. */ |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 625 | qcc->strms[QCS_CLT_BIDI].max_streams = lparams->initial_max_streams_bidi; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 626 | qcc->strms[QCS_CLT_BIDI].nb_streams = 0; |
| 627 | qcc->strms[QCS_CLT_BIDI].largest_id = -1; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 628 | qcc->strms[QCS_CLT_BIDI].rx.max_data = 0; |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 629 | qcc->strms[QCS_CLT_BIDI].tx.max_data = lparams->initial_max_stream_data_bidi_remote; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 630 | |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 631 | qcc->strms[QCS_CLT_UNI].max_streams = lparams->initial_max_streams_uni; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 632 | qcc->strms[QCS_CLT_UNI].nb_streams = 0; |
| 633 | qcc->strms[QCS_CLT_UNI].largest_id = -1; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 634 | qcc->strms[QCS_CLT_UNI].rx.max_data = 0; |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 635 | qcc->strms[QCS_CLT_UNI].tx.max_data = lparams->initial_max_stream_data_uni; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 636 | |
| 637 | /* Server initiated streams must respect the server flow control. */ |
| 638 | qcc->strms[QCS_SRV_BIDI].max_streams = 0; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 639 | qcc->strms[QCS_SRV_BIDI].nb_streams = 0; |
| 640 | qcc->strms[QCS_SRV_BIDI].largest_id = -1; |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 641 | qcc->strms[QCS_SRV_BIDI].rx.max_data = lparams->initial_max_stream_data_bidi_local; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 642 | qcc->strms[QCS_SRV_BIDI].tx.max_data = 0; |
| 643 | |
| 644 | qcc->strms[QCS_SRV_UNI].max_streams = 0; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 645 | qcc->strms[QCS_SRV_UNI].nb_streams = 0; |
| 646 | qcc->strms[QCS_SRV_UNI].largest_id = -1; |
Amaury Denoyelle | 749cb64 | 2022-02-09 10:25:29 +0100 | [diff] [blame] | 647 | qcc->strms[QCS_SRV_UNI].rx.max_data = lparams->initial_max_stream_data_uni; |
Amaury Denoyelle | f3b0ba7 | 2021-12-08 15:12:01 +0100 | [diff] [blame] | 648 | qcc->strms[QCS_SRV_UNI].tx.max_data = 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 649 | |
Amaury Denoyelle | c055e30 | 2022-02-07 16:09:06 +0100 | [diff] [blame] | 650 | 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 Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 653 | qcc->wait_event.tasklet = tasklet_new(); |
| 654 | if (!qcc->wait_event.tasklet) |
| 655 | goto fail_no_tasklet; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 656 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 657 | qcc->subs = NULL; |
| 658 | qcc->wait_event.tasklet->process = qc_io_cb; |
| 659 | qcc->wait_event.tasklet->context = qcc; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 660 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 661 | /* 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 Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 670 | 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écaille | b80b20c | 2022-01-12 17:46:56 +0100 | [diff] [blame] | 677 | HA_ATOMIC_STORE(&conn->qc->qcc, qcc); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 678 | /* init read cycle */ |
| 679 | tasklet_wakeup(qcc->wait_event.tasklet); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 680 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 681 | return 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 682 | |
Amaury Denoyelle | aebe26f | 2022-01-13 16:28:06 +0100 | [diff] [blame] | 683 | fail_no_timeout_task: |
| 684 | tasklet_free(qcc->wait_event.tasklet); |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 685 | fail_no_tasklet: |
| 686 | pool_free(pool_head_qcc, qcc); |
| 687 | fail_no_qcc: |
| 688 | return -1; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 689 | } |
| 690 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 691 | static void qc_detach(struct conn_stream *cs) |
| 692 | { |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 693 | 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 Denoyelle | d975148 | 2022-02-01 15:15:11 +0100 | [diff] [blame] | 699 | /* 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 Denoyelle | 2873a31 | 2021-12-08 14:42:55 +0100 | [diff] [blame] | 705 | qcs->flags |= QC_SF_DETACH; |
| 706 | return; |
| 707 | } |
| 708 | |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 709 | qcs_destroy(qcs); |
Amaury Denoyelle | 1136e92 | 2022-02-01 10:33:09 +0100 | [diff] [blame] | 710 | |
| 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 Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 715 | } |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 716 | } |
| 717 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 718 | /* Called from the upper layer, to receive data */ |
| 719 | static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf, |
| 720 | size_t count, int flags) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 721 | { |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 722 | struct qcs *qcs = cs->ctx; |
| 723 | struct htx *qcs_htx = NULL; |
| 724 | struct htx *cs_htx = NULL; |
| 725 | size_t ret = 0; |
Amaury Denoyelle | eb53e5b | 2022-02-14 17:11:32 +0100 | [diff] [blame] | 726 | char fin = 0; |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 727 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 728 | fprintf(stderr, "%s\n", __func__); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 729 | |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 730 | 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 Denoyelle | eb53e5b | 2022-02-14 17:11:32 +0100 | [diff] [blame] | 751 | if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) { |
| 752 | cs_htx->flags |= HTX_FL_EOM; |
| 753 | fin = 1; |
| 754 | } |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 755 | |
| 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 Denoyelle | eb53e5b | 2022-02-14 17:11:32 +0100 | [diff] [blame] | 770 | if (fin) |
| 771 | cs->flags |= (CS_FL_EOI|CS_FL_EOS); |
Amaury Denoyelle | 9a327a7 | 2022-02-14 17:11:09 +0100 | [diff] [blame] | 772 | |
| 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écaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 783 | } |
| 784 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 785 | static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf, |
| 786 | size_t count, int flags) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 787 | { |
| 788 | struct qcs *qcs = cs->ctx; |
| 789 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 790 | fprintf(stderr, "%s\n", __func__); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 791 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 792 | 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] | 793 | } |
| 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 Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 800 | static int qc_subscribe(struct conn_stream *cs, int event_type, |
| 801 | struct wait_event *es) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 802 | { |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 803 | return qcs_subscribe(cs->ctx, event_type, es); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 804 | } |
| 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 | */ |
| 810 | static 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écaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 814 | 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écaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 821 | return 0; |
| 822 | } |
| 823 | |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 824 | static 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écaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 838 | static const struct mux_ops qc_ops = { |
| 839 | .init = qc_init, |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 840 | .detach = qc_detach, |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 841 | .rcv_buf = qc_rcv_buf, |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 842 | .snd_buf = qc_snd_buf, |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 843 | .subscribe = qc_subscribe, |
| 844 | .unsubscribe = qc_unsubscribe, |
Amaury Denoyelle | 0e0969d | 2022-01-31 15:41:14 +0100 | [diff] [blame] | 845 | .wake = qc_wake, |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 846 | }; |
| 847 | |
| 848 | static struct mux_proto_list mux_proto_quic = |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 849 | { .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] | 850 | |
| 851 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic); |