-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
nc_mbuf.c
287 lines (236 loc) · 6.71 KB
/
nc_mbuf.c
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* twemproxy - A fast and lightweight proxy for memcached protocol.
* Copyright (C) 2011 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include <string.h>
#include <nc_core.h>
static uint32_t nfree_mbufq; /* # free mbuf */
static struct mhdr free_mbufq; /* free mbuf q */
static size_t mbuf_chunk_size; /* mbuf chunk size - header + data (const) */
static size_t mbuf_offset; /* mbuf offset in chunk (const) */
static struct mbuf *
_mbuf_get(void)
{
struct mbuf *mbuf;
uint8_t *buf;
if (!STAILQ_EMPTY(&free_mbufq)) {
ASSERT(nfree_mbufq > 0);
mbuf = STAILQ_FIRST(&free_mbufq);
nfree_mbufq--;
STAILQ_REMOVE_HEAD(&free_mbufq, next);
ASSERT(mbuf->magic == MBUF_MAGIC);
goto done;
}
buf = nc_alloc(mbuf_chunk_size);
if (buf == NULL) {
return NULL;
}
/*
* mbuf header is at the tail end of the mbuf. This enables us to catch
* buffer overrun early by asserting on the magic value during get or
* put operations
*
* <------------- mbuf_chunk_size ------------->
* +-------------------------------------------+
* | mbuf data | mbuf header |
* | (mbuf_offset) | (struct mbuf) |
* +-------------------------------------------+
* ^ ^ ^ ^^
* | | | ||
* \ | | |\
* mbuf->start \ | | mbuf->end (one byte past valid bound)
* mbuf->pos \
* \ mbuf
* mbuf->last (one byte past valid byte)
*
*/
mbuf = (struct mbuf *)(buf + mbuf_offset);
mbuf->magic = MBUF_MAGIC;
done:
STAILQ_NEXT(mbuf, next) = NULL;
return mbuf;
}
struct mbuf *
mbuf_get(void)
{
struct mbuf *mbuf;
uint8_t *buf;
mbuf = _mbuf_get();
if (mbuf == NULL) {
return NULL;
}
buf = (uint8_t *)mbuf - mbuf_offset;
mbuf->start = buf;
mbuf->end = buf + mbuf_offset;
ASSERT(mbuf->end - mbuf->start == (int)mbuf_offset);
ASSERT(mbuf->start < mbuf->end);
mbuf->pos = mbuf->start;
mbuf->last = mbuf->start;
log_debug(LOG_VVERB, "get mbuf %p", mbuf);
return mbuf;
}
static void
mbuf_free(struct mbuf *mbuf)
{
uint8_t *buf;
log_debug(LOG_VVERB, "put mbuf %p len %d", mbuf, (int)(mbuf->last - mbuf->pos));
ASSERT(STAILQ_NEXT(mbuf, next) == NULL);
ASSERT(mbuf->magic == MBUF_MAGIC);
buf = (uint8_t *)mbuf - mbuf_offset;
nc_free(buf);
}
void
mbuf_put(struct mbuf *mbuf)
{
log_debug(LOG_VVERB, "put mbuf %p len %d", mbuf, (int)(mbuf->last - mbuf->pos));
ASSERT(STAILQ_NEXT(mbuf, next) == NULL);
ASSERT(mbuf->magic == MBUF_MAGIC);
nfree_mbufq++;
STAILQ_INSERT_HEAD(&free_mbufq, mbuf, next);
}
/*
* Rewind the mbuf by discarding any of the read or unread data that it
* might hold.
*/
void
mbuf_rewind(struct mbuf *mbuf)
{
mbuf->pos = mbuf->start;
mbuf->last = mbuf->start;
}
/*
* Return the length of data in mbuf. Mbuf cannot contain more than
* 2^32 bytes (4G).
*/
uint32_t
mbuf_length(const struct mbuf *mbuf)
{
ASSERT(mbuf->last >= mbuf->pos);
return (uint32_t)(mbuf->last - mbuf->pos);
}
/*
* Return the remaining space size for any new data in mbuf. Mbuf cannot
* contain more than 2^32 bytes (4G).
*/
uint32_t
mbuf_size(const struct mbuf *mbuf)
{
ASSERT(mbuf->end >= mbuf->last);
return (uint32_t)(mbuf->end - mbuf->last);
}
/*
* Return the maximum available space size for data in any mbuf. Mbuf cannot
* contain more than 2^32 bytes (4G).
*/
size_t
mbuf_data_size(void)
{
return mbuf_offset;
}
/*
* Insert mbuf at the tail of the mhdr Q
*/
void
mbuf_insert(struct mhdr *mhdr, struct mbuf *mbuf)
{
STAILQ_INSERT_TAIL(mhdr, mbuf, next);
log_debug(LOG_VVERB, "insert mbuf %p len %d", mbuf,
(int)(mbuf->last - mbuf->pos));
}
/*
* Remove mbuf from the mhdr Q
*/
void
mbuf_remove(struct mhdr *mhdr, struct mbuf *mbuf)
{
log_debug(LOG_VVERB, "remove mbuf %p len %d", mbuf,
(int)(mbuf->last - mbuf->pos));
STAILQ_REMOVE(mhdr, mbuf, mbuf, next);
STAILQ_NEXT(mbuf, next) = NULL;
}
/*
* Copy n bytes from memory area pos to mbuf.
*
* The memory areas should not overlap and the mbuf should have
* enough space for n bytes.
*/
void
mbuf_copy(struct mbuf *mbuf, const uint8_t *pos, size_t n)
{
if (n == 0) {
return;
}
/* mbuf has space for n bytes */
ASSERT(!mbuf_full(mbuf) && n <= mbuf_size(mbuf));
/* no overlapping copy */
ASSERT(pos < mbuf->start || pos >= mbuf->end);
nc_memcpy(mbuf->last, pos, n);
mbuf->last += n;
}
/*
* Split mbuf h into h and t by copying data from h to t. Before
* the copy, we invoke a precopy handler cb that will copy a predefined
* string to the head of t.
*
* Return new mbuf t, if the split was successful.
*/
struct mbuf *
mbuf_split(struct mhdr *h, uint8_t *pos, mbuf_copy_t cb, void *cbarg)
{
struct mbuf *mbuf, *nbuf;
size_t size;
ASSERT(!STAILQ_EMPTY(h));
mbuf = STAILQ_LAST(h, mbuf, next);
ASSERT(pos >= mbuf->pos && pos <= mbuf->last);
nbuf = mbuf_get();
if (nbuf == NULL) {
return NULL;
}
if (cb != NULL) {
/* precopy nbuf */
cb(nbuf, cbarg);
}
/* copy data from mbuf to nbuf */
size = (size_t)(mbuf->last - pos);
mbuf_copy(nbuf, pos, size);
/* adjust mbuf */
mbuf->last = pos;
log_debug(LOG_VVERB, "split into mbuf %p len %"PRIu32" and nbuf %p len "
"%"PRIu32" copied %zu bytes", mbuf, mbuf_length(mbuf), nbuf,
mbuf_length(nbuf), size);
return nbuf;
}
void
mbuf_init(const struct instance *nci)
{
nfree_mbufq = 0;
STAILQ_INIT(&free_mbufq);
mbuf_chunk_size = nci->mbuf_chunk_size;
mbuf_offset = mbuf_chunk_size - MBUF_HSIZE;
log_debug(LOG_DEBUG, "mbuf hsize %d chunk size %zu offset %zu length %zu",
(int)MBUF_HSIZE, mbuf_chunk_size, mbuf_offset, mbuf_offset);
}
void
mbuf_deinit(void)
{
while (!STAILQ_EMPTY(&free_mbufq)) {
struct mbuf *mbuf = STAILQ_FIRST(&free_mbufq);
mbuf_remove(&free_mbufq, mbuf);
mbuf_free(mbuf);
nfree_mbufq--;
}
ASSERT(nfree_mbufq == 0);
}