summaryrefslogtreecommitdiffstats
path: root/include/internal/ring_buf.h
blob: 69b8df2aa83cdb5c96b57664cf7d741a93e0056d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#ifndef OSSL_INTERNAL_RING_BUF_H
# define OSSL_INTERNAL_RING_BUF_H
# pragma once

# include <openssl/e_os2.h>              /* For 'ossl_inline' */

/*
 * ==================================================================
 * Byte-wise ring buffer which supports pushing and popping blocks of multiple
 * bytes at a time. The logical offset of each byte for the purposes of a QUIC
 * stream is tracked. Bytes can be popped from the ring buffer in two stages;
 * first they are popped, and then they are culled. Bytes which have been popped
 * but not yet culled will not be overwritten, and can be restored.
 */
struct ring_buf {
    void       *start;
    size_t      alloc;        /* size of buffer allocation in bytes */

    /*
     * Logical offset of the head (where we append to). This is the current size
     * of the QUIC stream. This increases monotonically.
     */
    uint64_t    head_offset;

    /*
     * Logical offset of the cull tail. Data is no longer needed and is
     * deallocated as the cull tail advances, which occurs as data is
     * acknowledged. This increases monotonically.
     */
    uint64_t    ctail_offset;
};

static ossl_inline int ring_buf_init(struct ring_buf *r)
{
    r->start = NULL;
    r->alloc = 0;
    r->head_offset = r->ctail_offset = 0;
    return 1;
}

static ossl_inline void ring_buf_destroy(struct ring_buf *r)
{
    OPENSSL_free(r->start);
    r->start = NULL;
    r->alloc = 0;
}

static ossl_inline size_t ring_buf_used(struct ring_buf *r)
{
    return (size_t)(r->head_offset - r->ctail_offset);
}

static ossl_inline size_t ring_buf_avail(struct ring_buf *r)
{
    return r->alloc - ring_buf_used(r);
}

static ossl_inline int ring_buf_write_at(struct ring_buf *r,
                                         uint64_t logical_offset,
                                         const unsigned char *buf,
                                         size_t buf_len)
{
    size_t avail, idx, l;
    unsigned char *start = r->start;
    int i;

    avail = ring_buf_avail(r);
    if (logical_offset < r->ctail_offset
        || logical_offset + buf_len > r->head_offset + avail)
        return 0;

    for (i = 0; buf_len > 0 && i < 2; ++i) {
        idx = logical_offset % r->alloc;
        l = r->alloc - idx;
        if (buf_len < l)
            l = buf_len;

        memcpy(start + idx, buf, l);
        if (r->head_offset < logical_offset + l)
            r->head_offset = logical_offset + l;

        logical_offset += l;
        buf += l;
        buf_len -= l;
    }

    assert(buf_len == 0);

    return 1;
}

static ossl_inline size_t ring_buf_push(struct ring_buf *r,
                                        const unsigned char *buf,
                                        size_t buf_len)
{
    size_t pushed = 0, avail, idx, l;
    unsigned char *start = r->start;

    for (;;) {
        avail = ring_buf_avail(r);
        if (buf_len > avail)
            buf_len = avail;

        if (buf_len == 0)
            break;

        idx = r->head_offset % r->alloc;
        l = r->alloc - idx;
        if (buf_len < l)
            l = buf_len;

        memcpy(start + idx, buf, l);
        r->head_offset  += l;
        buf             += l;
        buf_len         -= l;
        pushed          += l;
    }

    return pushed;
}

static ossl_inline const unsigned char *ring_buf_get_ptr(const struct ring_buf *r,
                                                         uint64_t logical_offset,
                                                         size_t *max_len)
{
    unsigned char *start = r->start;
    size_t idx;

    if (logical_offset >= r->head_offset || logical_offset < r->ctail_offset)
        return NULL;
    idx = logical_offset % r->alloc;
    *max_len = r->alloc - idx;
    return start + idx;
}

/*
 * Retrieves data out of the read side of the ring buffer starting at the given
 * logical offset. *buf is set to point to a contiguous span of bytes and
 * *buf_len is set to the number of contiguous bytes. After this function
 * returns, there may or may not be more bytes available at the logical offset
 * of (logical_offset + *buf_len) by calling this function again. If the logical
 * offset is out of the range retained by the ring buffer, returns 0, else
 * returns 1. A logical offset at the end of the range retained by the ring
 * buffer is not considered an error and is returned with a *buf_len of 0.
 *
 * The ring buffer state is not changed.
 */
static ossl_inline int ring_buf_get_buf_at(const struct ring_buf *r,
                                           uint64_t logical_offset,
                                           const unsigned char **buf,
                                           size_t *buf_len)
{
    const unsigned char *start = r->start;
    size_t idx, l;

    if (logical_offset > r->head_offset || logical_offset < r->ctail_offset)
        return 0;

    if (r->alloc == 0) {
        *buf        = NULL;
        *buf_len    = 0;
        return 1;
    }

    idx = logical_offset % r->alloc;
    l   = (size_t)(r->head_offset - logical_offset);
    if (l > r->alloc - idx)
        l = r->alloc - idx;

    *buf        = start + idx;
    *buf_len    = l;
    return 1;
}

static ossl_inline void ring_buf_cpop_range(struct ring_buf *r,
                                            uint64_t start, uint64_t end,
                                            int cleanse)
{
    assert(end >= start);

    if (start > r->ctail_offset)
        return;

    if (cleanse && r->alloc > 0 && end > r->ctail_offset) {
        size_t idx = r->ctail_offset % r->alloc;
        uint64_t cleanse_end = end + 1;
        size_t l;

        if (cleanse_end > r->head_offset)
            cleanse_end = r->head_offset;
        l = (size_t)(cleanse_end - r->ctail_offset);
        if (l > r->alloc - idx) {
            OPENSSL_cleanse((unsigned char *)r->start + idx, r->alloc - idx);
            l -= r->alloc - idx;
            idx = 0;
        }
        if (l > 0)
            OPENSSL_cleanse((unsigned char *)r->start + idx, l);
    }

    r->ctail_offset = end + 1;
    /* Allow culling unpushed data */
    if (r->head_offset < r->ctail_offset)
        r->head_offset = r->ctail_offset;
}

static ossl_inline int ring_buf_resize(struct ring_buf *r, size_t num_bytes)
{
    struct ring_buf rnew = {0};
    const unsigned char *src = NULL;
    size_t src_len = 0, copied = 0;

    if (num_bytes == r->alloc)
        return 1;

    if (num_bytes < ring_buf_used(r))
        return 0;

    rnew.start = OPENSSL_malloc(num_bytes);
    if (rnew.start == NULL)
        return 0;

    rnew.alloc          = num_bytes;
    rnew.head_offset    = r->head_offset - ring_buf_used(r);
    rnew.ctail_offset   = rnew.head_offset;

    for (;;) {
        if (!ring_buf_get_buf_at(r, r->ctail_offset + copied, &src, &src_len)) {
            OPENSSL_free(rnew.start);
            return 0;
        }

        if (src_len == 0)
            break;

        if (ring_buf_push(&rnew, src, src_len) != src_len) {
            OPENSSL_free(rnew.start);
            return 0;
        }

        copied += src_len;
    }

    assert(rnew.head_offset == r->head_offset);
    rnew.ctail_offset   = r->ctail_offset;

    OPENSSL_free(r->start);
    memcpy(r, &rnew, sizeof(*r));
    return 1;
}

#endif                          /* OSSL_INTERNAL_RING_BUF_H */