Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 1 | #include <haproxy/quic_stream.h> |
| 2 | |
| 3 | #include <import/eb64tree.h> |
| 4 | |
| 5 | #include <haproxy/api.h> |
| 6 | #include <haproxy/buf.h> |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 7 | #include <haproxy/dynbuf.h> |
Amaury Denoyelle | 5c25dc5 | 2022-09-30 17:44:15 +0200 | [diff] [blame] | 8 | #include <haproxy/list.h> |
| 9 | #include <haproxy/mux_quic-t.h> |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 10 | #include <haproxy/pool.h> |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 11 | #include <haproxy/quic_conn.h> |
Amaury Denoyelle | 5c25dc5 | 2022-09-30 17:44:15 +0200 | [diff] [blame] | 12 | #include <haproxy/task.h> |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 13 | |
Willy Tarreau | a448227 | 2022-05-27 09:11:02 +0200 | [diff] [blame] | 14 | DECLARE_STATIC_POOL(pool_head_quic_stream_desc, "qc_stream_desc", |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 15 | sizeof(struct qc_stream_desc)); |
Willy Tarreau | a448227 | 2022-05-27 09:11:02 +0200 | [diff] [blame] | 16 | DECLARE_STATIC_POOL(pool_head_quic_stream_buf, "qc_stream_buf", |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 17 | sizeof(struct qc_stream_buf)); |
| 18 | |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 19 | |
| 20 | /* Allocate a new stream descriptor with id <id>. The caller is responsible to |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 21 | * store the stream in the appropriate tree. -1 special value must be used for |
| 22 | * a CRYPTO data stream, the type being ignored. |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 23 | * |
| 24 | * Returns the newly allocated instance on success or else NULL. |
| 25 | */ |
Frédéric Lécaille | 664741e | 2022-05-02 18:46:58 +0200 | [diff] [blame] | 26 | struct qc_stream_desc *qc_stream_desc_new(uint64_t id, enum qcs_type type, void *ctx, |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 27 | struct quic_conn *qc) |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 28 | { |
| 29 | struct qc_stream_desc *stream; |
| 30 | |
Willy Tarreau | a448227 | 2022-05-27 09:11:02 +0200 | [diff] [blame] | 31 | stream = pool_alloc(pool_head_quic_stream_desc); |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 32 | if (!stream) |
| 33 | return NULL; |
| 34 | |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 35 | if (id == (uint64_t)-1) { |
| 36 | stream->by_id.key = (uint64_t)-1; |
| 37 | } |
| 38 | else { |
| 39 | stream->by_id.key = id; |
| 40 | eb64_insert(&qc->streams_by_id, &stream->by_id); |
| 41 | qc->rx.strms[type].nb_streams++; |
| 42 | } |
Amaury Denoyelle | b22c046 | 2022-04-21 11:00:41 +0200 | [diff] [blame] | 43 | stream->qc = qc; |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 44 | |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 45 | stream->buf = NULL; |
| 46 | LIST_INIT(&stream->buf_list); |
| 47 | stream->buf_offset = 0; |
| 48 | |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 49 | stream->acked_frms = EB_ROOT; |
| 50 | stream->ack_offset = 0; |
| 51 | stream->release = 0; |
| 52 | stream->ctx = ctx; |
| 53 | |
| 54 | return stream; |
| 55 | } |
| 56 | |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 57 | /* Mark the stream descriptor <stream> as released. It will be freed as soon as |
Amaury Denoyelle | 93fba32 | 2022-05-24 16:53:14 +0200 | [diff] [blame] | 58 | * all its buffered data are acknowledged. Does nothing if <stream> is already |
| 59 | * NULL. |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 60 | */ |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 61 | void qc_stream_desc_release(struct qc_stream_desc *stream) |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 62 | { |
Amaury Denoyelle | 93fba32 | 2022-05-24 16:53:14 +0200 | [diff] [blame] | 63 | if (!stream) |
| 64 | return; |
| 65 | |
Amaury Denoyelle | e4301da | 2022-04-19 17:59:50 +0200 | [diff] [blame] | 66 | /* A stream can be released only one time. */ |
| 67 | BUG_ON(stream->release); |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 68 | |
| 69 | stream->release = 1; |
| 70 | stream->ctx = NULL; |
| 71 | |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 72 | if (LIST_ISEMPTY(&stream->buf_list)) { |
| 73 | /* if no buffer left we can free the stream. */ |
Frédéric Lécaille | ea4a5cb | 2022-08-20 18:59:36 +0200 | [diff] [blame] | 74 | qc_stream_desc_free(stream, 0); |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 75 | } |
| 76 | else { |
| 77 | /* A released stream does not use <stream.buf>. */ |
| 78 | stream->buf = NULL; |
| 79 | } |
| 80 | } |
| 81 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 82 | /* Acknowledge data at <offset> of length <len> for <stream>. It is handled |
| 83 | * only if it covers a range corresponding to stream.ack_offset. After data |
| 84 | * removal, if the stream does not contains data any more and is already |
| 85 | * released, the instance stream is freed. <stream> is set to NULL to indicate |
| 86 | * this. |
| 87 | * |
| 88 | * Returns the count of byte removed from stream. Do not forget to check if |
| 89 | * <stream> is NULL after invocation. |
| 90 | */ |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 91 | int qc_stream_desc_ack(struct qc_stream_desc **stream, size_t offset, size_t len) |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 92 | { |
| 93 | struct qc_stream_desc *s = *stream; |
| 94 | struct qc_stream_buf *stream_buf; |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 95 | struct quic_conn *qc = s->qc; |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 96 | struct buffer *buf; |
| 97 | size_t diff; |
| 98 | |
| 99 | if (offset + len <= s->ack_offset || offset > s->ack_offset) |
| 100 | return 0; |
| 101 | |
| 102 | /* There must be at least a buffer or we must not report an ACK. */ |
| 103 | BUG_ON(LIST_ISEMPTY(&s->buf_list)); |
| 104 | |
| 105 | /* get oldest buffer from buf_list */ |
| 106 | stream_buf = LIST_NEXT(&s->buf_list, struct qc_stream_buf *, list); |
| 107 | buf = &stream_buf->buf; |
| 108 | |
| 109 | diff = offset + len - s->ack_offset; |
| 110 | s->ack_offset += diff; |
| 111 | b_del(buf, diff); |
| 112 | |
| 113 | /* nothing more to do if buf still not empty. */ |
| 114 | if (b_data(buf)) |
| 115 | return diff; |
| 116 | |
| 117 | /* buf is empty and can now be freed. Do not forget to reset current |
| 118 | * buf ptr if we were working on it. |
| 119 | */ |
| 120 | LIST_DELETE(&stream_buf->list); |
| 121 | if (stream_buf == s->buf) { |
| 122 | /* current buf must always be last entry in buflist */ |
| 123 | BUG_ON(!LIST_ISEMPTY(&s->buf_list)); |
| 124 | s->buf = NULL; |
| 125 | } |
| 126 | |
| 127 | b_free(buf); |
Willy Tarreau | a448227 | 2022-05-27 09:11:02 +0200 | [diff] [blame] | 128 | pool_free(pool_head_quic_stream_buf, stream_buf); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 129 | offer_buffers(NULL, 1); |
| 130 | |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 131 | /* notify MUX about available buffers. */ |
| 132 | --qc->stream_buf_count; |
| 133 | if (qc->mux_state == QC_MUX_READY) { |
| 134 | if (qc->qcc->flags & QC_CF_CONN_FULL) { |
| 135 | qc->qcc->flags &= ~QC_CF_CONN_FULL; |
| 136 | tasklet_wakeup(qc->qcc->wait_event.tasklet); |
| 137 | } |
| 138 | } |
| 139 | |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 140 | /* Free stream instance if already released and no buffers left. */ |
| 141 | if (s->release && LIST_ISEMPTY(&s->buf_list)) { |
Frédéric Lécaille | ea4a5cb | 2022-08-20 18:59:36 +0200 | [diff] [blame] | 142 | qc_stream_desc_free(s, 0); |
Amaury Denoyelle | 1b81dda | 2022-04-21 09:32:53 +0200 | [diff] [blame] | 143 | *stream = NULL; |
| 144 | } |
| 145 | |
| 146 | return diff; |
| 147 | } |
| 148 | |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 149 | /* Free the stream descriptor <stream> content. This function should be used |
Frédéric Lécaille | ea4a5cb | 2022-08-20 18:59:36 +0200 | [diff] [blame] | 150 | * when all its data have been acknowledged or on full connection closing if <closing> |
| 151 | * boolean is set to 1. It must only be called after the stream is released. |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 152 | */ |
Frédéric Lécaille | ea4a5cb | 2022-08-20 18:59:36 +0200 | [diff] [blame] | 153 | void qc_stream_desc_free(struct qc_stream_desc *stream, int closing) |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 154 | { |
| 155 | struct qc_stream_buf *buf, *buf_back; |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 156 | struct quic_conn *qc = stream->qc; |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 157 | struct eb64_node *frm_node; |
| 158 | unsigned int free_count = 0; |
| 159 | |
| 160 | /* This function only deals with released streams. */ |
| 161 | BUG_ON(!stream->release); |
| 162 | |
| 163 | /* free remaining stream buffers */ |
| 164 | list_for_each_entry_safe(buf, buf_back, &stream->buf_list, list) { |
Frédéric Lécaille | ea4a5cb | 2022-08-20 18:59:36 +0200 | [diff] [blame] | 165 | if (!(b_data(&buf->buf)) || closing) { |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 166 | b_free(&buf->buf); |
| 167 | LIST_DELETE(&buf->list); |
Willy Tarreau | a448227 | 2022-05-27 09:11:02 +0200 | [diff] [blame] | 168 | pool_free(pool_head_quic_stream_buf, buf); |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 169 | |
| 170 | ++free_count; |
| 171 | } |
| 172 | } |
| 173 | |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 174 | if (free_count) { |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 175 | offer_buffers(NULL, free_count); |
| 176 | |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 177 | qc->stream_buf_count -= free_count; |
| 178 | if (qc->mux_state == QC_MUX_READY) { |
| 179 | /* notify MUX about available buffers. */ |
| 180 | if (qc->qcc->flags & QC_CF_CONN_FULL) { |
| 181 | qc->qcc->flags &= ~QC_CF_CONN_FULL; |
| 182 | tasklet_wakeup(qc->qcc->wait_event.tasklet); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 187 | /* qc_stream_desc might be freed before having received all its ACKs. |
| 188 | * This is the case if some frames were retransmitted. |
| 189 | */ |
| 190 | frm_node = eb64_first(&stream->acked_frms); |
| 191 | while (frm_node) { |
| 192 | struct quic_stream *strm; |
| 193 | struct quic_frame *frm; |
| 194 | |
Frédéric Lécaille | a54e49d | 2022-05-10 15:15:24 +0200 | [diff] [blame] | 195 | strm = eb64_entry(frm_node, struct quic_stream, offset); |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 196 | |
| 197 | frm_node = eb64_next(frm_node); |
| 198 | eb64_delete(&strm->offset); |
| 199 | |
| 200 | frm = container_of(strm, struct quic_frame, stream); |
Frédéric Lécaille | da34255 | 2022-04-25 10:28:49 +0200 | [diff] [blame] | 201 | qc_release_frm(qc, frm); |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 202 | } |
| 203 | |
Frédéric Lécaille | 7e3f7c4 | 2022-09-09 18:05:45 +0200 | [diff] [blame] | 204 | if (stream->by_id.key != (uint64_t)-1) |
| 205 | eb64_delete(&stream->by_id); |
Willy Tarreau | a448227 | 2022-05-27 09:11:02 +0200 | [diff] [blame] | 206 | pool_free(pool_head_quic_stream_desc, stream); |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /* Return the current buffer of <stream>. May be NULL if not allocated. */ |
| 210 | struct buffer *qc_stream_buf_get(struct qc_stream_desc *stream) |
| 211 | { |
| 212 | if (!stream->buf) |
| 213 | return NULL; |
| 214 | |
| 215 | return &stream->buf->buf; |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 216 | } |
| 217 | |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 218 | /* Check if a new stream buffer can be allocated for the connection <qc>. |
| 219 | * Returns a boolean. |
| 220 | */ |
Amaury Denoyelle | 1b2dba5 | 2022-04-15 17:32:04 +0200 | [diff] [blame] | 221 | static int qc_stream_buf_avail(struct quic_conn *qc) |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 222 | { |
Amaury Denoyelle | 97e84c6 | 2022-04-19 18:26:55 +0200 | [diff] [blame] | 223 | return qc->stream_buf_count < global.tune.quic_streams_buf; |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* Allocate a new current buffer for <stream>. The buffer limit count for the |
| 227 | * connection is checked first. This function is not allowed if current buffer |
| 228 | * is not NULL prior to this call. The new buffer represents stream payload at |
| 229 | * offset <offset>. |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 230 | * |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 231 | * Returns the buffer or NULL. |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 232 | */ |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 233 | struct buffer *qc_stream_buf_alloc(struct qc_stream_desc *stream, |
| 234 | uint64_t offset) |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 235 | { |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 236 | struct quic_conn *qc = stream->qc; |
| 237 | |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 238 | /* current buffer must be released first before allocate a new one. */ |
| 239 | BUG_ON(stream->buf); |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 240 | |
Amaury Denoyelle | d2f80a2 | 2022-04-15 17:30:49 +0200 | [diff] [blame] | 241 | if (!qc_stream_buf_avail(qc)) |
| 242 | return NULL; |
| 243 | |
| 244 | ++qc->stream_buf_count; |
| 245 | |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 246 | stream->buf_offset = offset; |
Willy Tarreau | a448227 | 2022-05-27 09:11:02 +0200 | [diff] [blame] | 247 | stream->buf = pool_alloc(pool_head_quic_stream_buf); |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 248 | if (!stream->buf) |
| 249 | return NULL; |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 250 | |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 251 | stream->buf->buf = BUF_NULL; |
| 252 | LIST_APPEND(&stream->buf_list, &stream->buf->list); |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 253 | |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 254 | return &stream->buf->buf; |
| 255 | } |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 256 | |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 257 | /* Release the current buffer of <stream>. It will be kept internally by |
| 258 | * the <stream>. The current buffer cannot be NULL. |
| 259 | */ |
| 260 | void qc_stream_buf_release(struct qc_stream_desc *stream) |
| 261 | { |
| 262 | /* current buffer already released */ |
| 263 | BUG_ON(!stream->buf); |
Amaury Denoyelle | 0cc02a3 | 2022-04-19 17:21:11 +0200 | [diff] [blame] | 264 | |
Amaury Denoyelle | a456920 | 2022-04-15 17:29:25 +0200 | [diff] [blame] | 265 | stream->buf = NULL; |
| 266 | stream->buf_offset = 0; |
| 267 | } |