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 | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 43 | fprintf(stderr, "%s: stream ID %llu\n", __func__, qcs->by_id.key); |
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 | |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 120 | static int qcs_push_frame(struct qcs *qcs, struct buffer *payload, int fin, uint64_t offset) |
| 121 | { |
| 122 | struct quic_frame *frm; |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 123 | struct buffer *buf = &qcs->tx.xprt_buf; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 124 | 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] | 125 | int total = 0, to_xfer; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 126 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 127 | fprintf(stderr, "%s\n", __func__); |
| 128 | |
Amaury Denoyelle | 1e308ff | 2021-10-12 18:14:12 +0200 | [diff] [blame] | 129 | qc_get_buf(qcs, buf); |
Frédéric Lécaille | d2ba096 | 2021-09-20 17:50:03 +0200 | [diff] [blame] | 130 | to_xfer = QUIC_MIN(b_data(payload), b_room(buf)); |
| 131 | if (!to_xfer) |
| 132 | goto out; |
| 133 | |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 134 | frm = pool_zalloc(pool_head_quic_frame); |
| 135 | if (!frm) |
| 136 | goto err; |
| 137 | |
Frédéric Lécaille | d2ba096 | 2021-09-20 17:50:03 +0200 | [diff] [blame] | 138 | total = b_force_xfer(buf, payload, to_xfer); |
Amaury Denoyelle | fecfa0d | 2021-12-07 16:50:14 +0100 | [diff] [blame^] | 139 | /* 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] | 140 | fin = fin && !b_data(payload); |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 141 | frm->type = QUIC_FT_STREAM_8; |
| 142 | if (fin) |
| 143 | frm->type |= QUIC_STREAM_FRAME_TYPE_FIN_BIT; |
| 144 | if (offset) { |
| 145 | frm->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT; |
| 146 | frm->stream.offset.key = offset; |
| 147 | } |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 148 | frm->stream.qcs = (struct qcs *)qcs; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 149 | frm->stream.buf = buf; |
| 150 | frm->stream.id = qcs->by_id.key; |
| 151 | if (total) { |
| 152 | frm->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT; |
| 153 | frm->stream.len = total; |
| 154 | } |
| 155 | |
| 156 | MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list); |
Frédéric Lécaille | d2ba096 | 2021-09-20 17:50:03 +0200 | [diff] [blame] | 157 | out: |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 158 | fprintf(stderr, "%s: total=%d fin=%d offset=%lu\n", __func__, total, fin, offset); |
| 159 | return total; |
| 160 | |
| 161 | err: |
| 162 | return -1; |
| 163 | } |
| 164 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 165 | static int qc_send(struct qcc *qcc) |
| 166 | { |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 167 | struct eb64_node *node; |
Amaury Denoyelle | e257d9e | 2021-12-03 14:39:29 +0100 | [diff] [blame] | 168 | int xprt_wake = 0; |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 169 | int ret; |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 170 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 171 | fprintf(stderr, "%s\n", __func__); |
Frédéric Lécaille | 8526f14 | 2021-09-20 17:58:22 +0200 | [diff] [blame] | 172 | |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 173 | /* TODO simple loop through all streams and check if there is frames to |
| 174 | * send |
| 175 | */ |
| 176 | node = eb64_first(&qcc->streams_by_id); |
| 177 | while (node) { |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 178 | struct qcs *qcs = container_of(node, struct qcs, by_id); |
| 179 | struct buffer *buf = &qcs->tx.buf; |
| 180 | if (b_data(buf)) { |
Amaury Denoyelle | fecfa0d | 2021-12-07 16:50:14 +0100 | [diff] [blame^] | 181 | char fin = qcs->flags & QC_SF_FIN_STREAM; |
Amaury Denoyelle | c2025c1 | 2021-12-03 15:03:36 +0100 | [diff] [blame] | 182 | ret = qcs_push_frame(qcs, buf, fin, qcs->tx.offset); |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 183 | if (ret < 0) |
| 184 | ABORT_NOW(); |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 185 | |
Amaury Denoyelle | e257d9e | 2021-12-03 14:39:29 +0100 | [diff] [blame] | 186 | if (ret > 0) { |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 187 | qcs_notify_send(qcs); |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 188 | if (qcs->flags & QC_SF_BLK_MROOM) |
| 189 | qcs->flags &= ~QC_SF_BLK_MROOM; |
| 190 | |
Amaury Denoyelle | e257d9e | 2021-12-03 14:39:29 +0100 | [diff] [blame] | 191 | xprt_wake = 1; |
| 192 | } |
Amaury Denoyelle | a543eb1 | 2021-10-06 14:53:13 +0200 | [diff] [blame] | 193 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 194 | fprintf(stderr, "%s ret=%d\n", __func__, ret); |
Amaury Denoyelle | d3d97c6 | 2021-10-05 11:45:58 +0200 | [diff] [blame] | 195 | qcs->tx.offset += ret; |
Amaury Denoyelle | a2c58a7 | 2021-12-03 14:38:31 +0100 | [diff] [blame] | 196 | |
| 197 | if (b_data(buf)) { |
| 198 | qcc->conn->xprt->subscribe(qcc->conn, qcc->conn->xprt_ctx, |
| 199 | SUB_RETRY_SEND, &qcc->wait_event); |
| 200 | } |
Frédéric Lécaille | 578a789 | 2021-09-13 16:13:00 +0200 | [diff] [blame] | 201 | } |
| 202 | node = eb64_next(node); |
| 203 | } |
Frédéric Lécaille | 8526f14 | 2021-09-20 17:58:22 +0200 | [diff] [blame] | 204 | |
Amaury Denoyelle | e257d9e | 2021-12-03 14:39:29 +0100 | [diff] [blame] | 205 | if (xprt_wake) |
| 206 | tasklet_wakeup(((struct ssl_sock_ctx *)(qcc->conn->xprt_ctx))->wait_event.tasklet); |
| 207 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 208 | return ret; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 211 | static struct task *qc_io_cb(struct task *t, void *ctx, unsigned int status) |
| 212 | { |
Amaury Denoyelle | 769e9ff | 2021-10-05 11:43:50 +0200 | [diff] [blame] | 213 | struct qcc *qcc = ctx; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 214 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 215 | fprintf(stderr, "%s\n", __func__); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 216 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 217 | qc_send(qcc); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 218 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 219 | return NULL; |
| 220 | } |
| 221 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 222 | static int qc_init(struct connection *conn, struct proxy *prx, |
| 223 | struct session *sess, struct buffer *input) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 224 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 225 | struct qcc *qcc; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 226 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 227 | qcc = pool_alloc(pool_head_qcc); |
| 228 | if (!qcc) |
| 229 | goto fail_no_qcc; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 230 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 231 | qcc->conn = conn; |
| 232 | conn->ctx = qcc; |
| 233 | conn->qc->qcc = qcc; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 234 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 235 | qcc->app_ops = NULL; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 236 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 237 | qcc->streams_by_id = EB_ROOT_UNIQUE; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 238 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 239 | qcc->strms[QCS_CLT_BIDI].nb_streams = 0; |
| 240 | qcc->strms[QCS_CLT_BIDI].largest_id = -1; |
| 241 | qcc->strms[QCS_CLT_UNI].nb_streams = 0; |
| 242 | qcc->strms[QCS_CLT_UNI].largest_id = -1; |
| 243 | qcc->strms[QCS_SRV_BIDI].nb_streams = 0; |
| 244 | qcc->strms[QCS_SRV_BIDI].largest_id = -1; |
| 245 | qcc->strms[QCS_SRV_UNI].nb_streams = 0; |
| 246 | qcc->strms[QCS_SRV_UNI].largest_id = -1; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 247 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 248 | qcc->wait_event.tasklet = tasklet_new(); |
| 249 | if (!qcc->wait_event.tasklet) |
| 250 | goto fail_no_tasklet; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 251 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 252 | qcc->subs = NULL; |
| 253 | qcc->wait_event.tasklet->process = qc_io_cb; |
| 254 | qcc->wait_event.tasklet->context = qcc; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 255 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 256 | /* init read cycle */ |
| 257 | tasklet_wakeup(qcc->wait_event.tasklet); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 258 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 259 | return 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 260 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 261 | fail_no_tasklet: |
| 262 | pool_free(pool_head_qcc, qcc); |
| 263 | fail_no_qcc: |
| 264 | return -1; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 265 | } |
| 266 | |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 267 | /* detachs the QUIC stream from its QCC and releases it to the QCS pool. */ |
| 268 | static void qcs_destroy(struct qcs *qcs) |
| 269 | { |
| 270 | fprintf(stderr, "%s: release stream %llu\n", __func__, qcs->by_id.key); |
| 271 | |
| 272 | eb64_delete(&qcs->by_id); |
| 273 | |
| 274 | b_free(&qcs->rx.buf); |
| 275 | b_free(&qcs->tx.buf); |
| 276 | b_free(&qcs->tx.xprt_buf); |
| 277 | |
| 278 | --qcs->qcc->strms[qcs_id_type(qcs->by_id.key)].nb_streams; |
| 279 | |
| 280 | pool_free(pool_head_qcs, qcs); |
| 281 | } |
| 282 | |
| 283 | static inline int qcc_is_dead(const struct qcc *qcc) |
| 284 | { |
| 285 | fprintf(stderr, "%s: %lu\n", __func__, qcc->strms[QCS_CLT_BIDI].nb_streams); |
| 286 | |
| 287 | if (!qcc->strms[QCS_CLT_BIDI].nb_streams) |
| 288 | return 1; |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | /* release function. This one should be called to free all resources allocated |
| 294 | * to the mux. |
| 295 | */ |
| 296 | static void qc_release(struct qcc *qcc) |
| 297 | { |
| 298 | struct connection *conn = NULL; |
| 299 | |
| 300 | if (qcc) { |
| 301 | /* The connection must be aattached to this mux to be released */ |
| 302 | if (qcc->conn && qcc->conn->ctx == qcc) |
| 303 | conn = qcc->conn; |
| 304 | |
| 305 | if (qcc->wait_event.tasklet) |
| 306 | tasklet_free(qcc->wait_event.tasklet); |
| 307 | |
| 308 | pool_free(pool_head_qcc, qcc); |
| 309 | } |
| 310 | |
| 311 | if (conn) { |
| 312 | conn->mux = NULL; |
| 313 | conn->ctx = NULL; |
| 314 | |
| 315 | conn_stop_tracking(conn); |
| 316 | conn_full_close(conn); |
| 317 | if (conn->destroy_cb) |
| 318 | conn->destroy_cb(conn); |
| 319 | conn_free(conn); |
| 320 | } |
| 321 | } |
| 322 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 323 | static void qc_detach(struct conn_stream *cs) |
| 324 | { |
Amaury Denoyelle | 916f0ac | 2021-12-06 16:03:47 +0100 | [diff] [blame] | 325 | struct qcs *qcs = cs->ctx; |
| 326 | struct qcc *qcc = qcs->qcc; |
| 327 | |
| 328 | fprintf(stderr, "%s: leaving with tx.buf.data=%lu, tx.xprt_buf.data=%lu\n", |
| 329 | __func__, b_data(&qcs->tx.buf), b_data(&qcs->tx.xprt_buf)); |
| 330 | |
| 331 | qcs_destroy(qcs); |
| 332 | if (qcc_is_dead(qcc)) { |
| 333 | qc_release(qcc); |
| 334 | return; |
| 335 | } |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 338 | /* Called from the upper layer, to receive data */ |
| 339 | static size_t qc_rcv_buf(struct conn_stream *cs, struct buffer *buf, |
| 340 | size_t count, int flags) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 341 | { |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 342 | /* XXX TODO XXX */ |
| 343 | fprintf(stderr, "%s\n", __func__); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 344 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 345 | return 0; |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 346 | } |
| 347 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 348 | static size_t qc_snd_buf(struct conn_stream *cs, struct buffer *buf, |
| 349 | size_t count, int flags) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 350 | { |
| 351 | struct qcs *qcs = cs->ctx; |
| 352 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 353 | fprintf(stderr, "%s\n", __func__); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 354 | |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 355 | 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] | 356 | } |
| 357 | |
| 358 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 359 | * event subscriber <es> is not allowed to change from a previous call as long |
| 360 | * as at least one event is still subscribed. The <event_type> must only be a |
| 361 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 362 | */ |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 363 | static int qc_subscribe(struct conn_stream *cs, int event_type, |
| 364 | struct wait_event *es) |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 365 | { |
Amaury Denoyelle | a3f222d | 2021-12-06 11:24:00 +0100 | [diff] [blame] | 366 | return qcs_subscribe(cs->ctx, event_type, es); |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 370 | * The <es> pointer is not allowed to differ from the one passed to the |
| 371 | * subscribe() call. It always returns zero. |
| 372 | */ |
| 373 | static int qc_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es) |
| 374 | { |
| 375 | struct qcs *qcs = cs->ctx; |
| 376 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 377 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
| 378 | BUG_ON(qcs->subs && qcs->subs != es); |
| 379 | |
| 380 | es->events &= ~event_type; |
| 381 | if (!es->events) |
| 382 | qcs->subs = NULL; |
| 383 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 384 | return 0; |
| 385 | } |
| 386 | |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 387 | static const struct mux_ops qc_ops = { |
| 388 | .init = qc_init, |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 389 | .detach = qc_detach, |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 390 | .rcv_buf = qc_rcv_buf, |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 391 | .snd_buf = qc_snd_buf, |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 392 | .subscribe = qc_subscribe, |
| 393 | .unsubscribe = qc_unsubscribe, |
Frédéric Lécaille | dfbae76 | 2021-02-18 09:59:01 +0100 | [diff] [blame] | 394 | }; |
| 395 | |
| 396 | static struct mux_proto_list mux_proto_quic = |
Amaury Denoyelle | deed777 | 2021-12-03 11:36:46 +0100 | [diff] [blame] | 397 | { .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] | 398 | |
| 399 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_quic); |