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