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