blob: a704efb40f4f7b57abdfb308f7280cfd9002068a [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 * buffer.c: allocate/free network buffers.
17 *
18 * Copyright (c) 2008 Eliot Dresselhaus
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
39
Chris Luked4024f52016-09-06 09:32:36 -040040/**
Chris Luked4024f52016-09-06 09:32:36 -040041 * @file
42 *
43 * Allocate/free network buffers.
44 */
45
Ed Warnickecb9cada2015-12-08 15:45:58 -070046#include <vlib/vlib.h>
Damjan Marion374e2c52017-03-09 20:38:15 +010047#include <vlib/unix/unix.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070048
Damjan Marion04a7f052017-07-10 15:06:17 +020049vlib_buffer_callbacks_t *vlib_buffer_callbacks = 0;
Damjan Marion49d66f12017-07-20 18:10:35 +020050static u32 vlib_buffer_physmem_sz = 32 << 20;
Damjan Marion04a7f052017-07-10 15:06:17 +020051
Damjan Mariond1274cb2018-03-13 21:32:17 +010052vlib_buffer_main_t buffer_main;
53
Dave Barach9b8ffd92016-07-08 08:13:45 -040054uword
55vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
56 vlib_buffer_t * b_first)
Ed Warnickecb9cada2015-12-08 15:45:58 -070057{
Dave Barach9b8ffd92016-07-08 08:13:45 -040058 vlib_buffer_t *b = b_first;
Ed Warnickecb9cada2015-12-08 15:45:58 -070059 uword l_first = b_first->current_length;
60 uword l = 0;
61 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
62 {
63 b = vlib_get_buffer (vm, b->next_buffer);
64 l += b->current_length;
65 }
66 b_first->total_length_not_including_first_buffer = l;
67 b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
68 return l + l_first;
69}
70
Dave Barach9b8ffd92016-07-08 08:13:45 -040071u8 *
72format_vlib_buffer (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070073{
Dave Barach9b8ffd92016-07-08 08:13:45 -040074 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +020075 u32 indent = format_get_indent (s);
Damjan Mariondac03522018-02-01 15:30:13 +010076 u8 *a = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010077
Damjan Mariondac03522018-02-01 15:30:13 +010078#define _(bit, name, v) \
79 if (v && (b->flags & VLIB_BUFFER_##name)) \
80 a = format (a, "%s ", v);
81 foreach_vlib_buffer_flag
82#undef _
83 s = format (s, "current data %d, length %d, free-list %d, clone-count %u",
84 b->current_data, b->current_length,
85 vlib_buffer_get_free_list_index (b), b->n_add_refs);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010086
87 if (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID)
88 s = format (s, ", totlen-nifb %d",
89 b->total_length_not_including_first_buffer);
90
91 if (b->flags & VLIB_BUFFER_IS_TRACED)
92 s = format (s, ", trace 0x%x", b->trace_index);
93
Damjan Mariondac03522018-02-01 15:30:13 +010094 if (a)
95 s = format (s, "\n%U%v", format_white_space, indent, a);
96 vec_free (a);
97
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010098 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
99 {
100 vlib_main_t *vm = vlib_get_main ();
101 u32 next_buffer = b->next_buffer;
102 b = vlib_get_buffer (vm, next_buffer);
103
Damjan Marionc47ed032017-01-25 14:18:03 +0100104 s =
105 format (s, "\n%Unext-buffer 0x%x, segment length %d, clone-count %u",
106 format_white_space, indent, next_buffer, b->current_length,
107 b->n_add_refs);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100108 }
109
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110 return s;
111}
112
Dave Barach9b8ffd92016-07-08 08:13:45 -0400113u8 *
114format_vlib_buffer_and_data (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400116 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
117
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118 s = format (s, "%U, %U",
119 format_vlib_buffer, b,
120 format_hex_bytes, vlib_buffer_get_current (b), 64);
121
122 return s;
123}
124
Dave Barach9b8ffd92016-07-08 08:13:45 -0400125static u8 *
126format_vlib_buffer_known_state (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127{
128 vlib_buffer_known_state_t state = va_arg (*args, vlib_buffer_known_state_t);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400129 char *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130
131 switch (state)
132 {
133 case VLIB_BUFFER_UNKNOWN:
134 t = "unknown";
135 break;
136
137 case VLIB_BUFFER_KNOWN_ALLOCATED:
138 t = "known-allocated";
139 break;
140
141 case VLIB_BUFFER_KNOWN_FREE:
142 t = "known-free";
143 break;
144
145 default:
146 t = "invalid";
147 break;
148 }
149
150 return format (s, "%s", t);
151}
152
Dave Barach9b8ffd92016-07-08 08:13:45 -0400153u8 *
154format_vlib_buffer_contents (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400156 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
157 vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *);
158
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159 while (1)
160 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400161 vec_add (s, vlib_buffer_get_current (b), b->current_length);
162 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163 break;
164 b = vlib_get_buffer (vm, b->next_buffer);
165 }
166
167 return s;
168}
169
170static u8 *
171vlib_validate_buffer_helper (vlib_main_t * vm,
172 u32 bi,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400173 uword follow_buffer_next, uword ** unique_hash)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400175 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400176 vlib_buffer_free_list_t *fl;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177
Damjan Marion072401e2017-07-13 18:53:27 +0200178 if (pool_is_free_index
Damjan Mariond1274cb2018-03-13 21:32:17 +0100179 (vm->buffer_free_list_pool, vlib_buffer_get_free_list_index (b)))
Damjan Marion072401e2017-07-13 18:53:27 +0200180 return format (0, "unknown free list 0x%x",
181 vlib_buffer_get_free_list_index (b));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182
Damjan Marion072401e2017-07-13 18:53:27 +0200183 fl =
Damjan Mariond1274cb2018-03-13 21:32:17 +0100184 pool_elt_at_index (vm->buffer_free_list_pool,
Damjan Marion072401e2017-07-13 18:53:27 +0200185 vlib_buffer_get_free_list_index (b));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186
Dave Barach9b8ffd92016-07-08 08:13:45 -0400187 if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188 return format (0, "current data %d before pre-data", b->current_data);
Damjan Marion878c6092017-01-04 13:19:27 +0100189
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190 if (b->current_data + b->current_length > fl->n_data_bytes)
191 return format (0, "%d-%d beyond end of buffer %d",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400192 b->current_data, b->current_length, fl->n_data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193
Dave Barach9b8ffd92016-07-08 08:13:45 -0400194 if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195 {
196 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400197 u8 *msg, *result;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198
Steven899a84b2018-01-29 20:09:09 -0800199 k = vlib_buffer_is_known (b->next_buffer);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200 if (k != VLIB_BUFFER_KNOWN_ALLOCATED)
201 return format (0, "next 0x%x: %U",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400202 b->next_buffer, format_vlib_buffer_known_state, k);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203
204 if (unique_hash)
205 {
206 if (hash_get (*unique_hash, b->next_buffer))
207 return format (0, "duplicate buffer 0x%x", b->next_buffer);
208
209 hash_set1 (*unique_hash, b->next_buffer);
210 }
211
212 msg = vlib_validate_buffer (vm, b->next_buffer, follow_buffer_next);
213 if (msg)
214 {
215 result = format (0, "next 0x%x: %v", b->next_buffer, msg);
216 vec_free (msg);
217 return result;
218 }
219 }
220
221 return 0;
222}
223
224u8 *
225vlib_validate_buffer (vlib_main_t * vm, u32 bi, uword follow_buffer_next)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400226{
227 return vlib_validate_buffer_helper (vm, bi, follow_buffer_next,
228 /* unique_hash */ 0);
229}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230
231u8 *
232vlib_validate_buffers (vlib_main_t * vm,
233 u32 * buffers,
234 uword next_buffer_stride,
235 uword n_buffers,
236 vlib_buffer_known_state_t known_state,
237 uword follow_buffer_next)
238{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400239 uword i, *hash;
240 u32 bi, *b = buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400242 u8 *msg = 0, *result = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243
244 hash = hash_create (0, 0);
245 for (i = 0; i < n_buffers; i++)
246 {
247 bi = b[0];
248 b += next_buffer_stride;
249
250 /* Buffer is not unique. */
251 if (hash_get (hash, bi))
252 {
253 msg = format (0, "not unique");
254 goto done;
255 }
256
Steven899a84b2018-01-29 20:09:09 -0800257 k = vlib_buffer_is_known (bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258 if (k != known_state)
259 {
260 msg = format (0, "is %U; expected %U",
261 format_vlib_buffer_known_state, k,
262 format_vlib_buffer_known_state, known_state);
263 goto done;
264 }
265
266 msg = vlib_validate_buffer_helper (vm, bi, follow_buffer_next, &hash);
267 if (msg)
268 goto done;
269
270 hash_set1 (hash, bi);
271 }
272
Dave Barach9b8ffd92016-07-08 08:13:45 -0400273done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274 if (msg)
275 {
276 result = format (0, "0x%x: %v", bi, msg);
277 vec_free (msg);
278 }
279 hash_free (hash);
280 return result;
281}
282
Dave Barach80f54e22017-03-08 19:08:56 -0500283/*
284 * Hand-craft a static vector w/ length 1, so vec_len(vlib_mains) =1
285 * and vlib_mains[0] = &vlib_global_main from the beginning of time.
286 *
287 * The only place which should ever expand vlib_mains is start_workers()
288 * in threads.c. It knows about the bootstrap vector.
289 */
290/* *INDENT-OFF* */
291static struct
292{
293 vec_header_t h;
294 vlib_main_t *vm;
295} __attribute__ ((packed)) __bootstrap_vlib_main_vector
296 __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES))) =
297{
298 .h.len = 1,
299 .vm = &vlib_global_main,
300};
301/* *INDENT-ON* */
302
303vlib_main_t **vlib_mains = &__bootstrap_vlib_main_vector.vm;
304
Ed Warnickecb9cada2015-12-08 15:45:58 -0700305
306/* When dubugging validate that given buffers are either known allocated
307 or known free. */
Damjan Marionc8a26c62017-11-24 20:15:23 +0100308void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309vlib_buffer_validate_alloc_free (vlib_main_t * vm,
310 u32 * buffers,
311 uword n_buffers,
312 vlib_buffer_known_state_t expected_state)
313{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400314 u32 *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315 uword i, bi, is_free;
316
317 if (CLIB_DEBUG == 0)
318 return;
319
Damjan Marionc8a26c62017-11-24 20:15:23 +0100320 if (vlib_buffer_callbacks)
321 return;
322
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323 is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED;
324 b = buffers;
325 for (i = 0; i < n_buffers; i++)
326 {
327 vlib_buffer_known_state_t known;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400328
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329 bi = b[0];
330 b += 1;
Steven899a84b2018-01-29 20:09:09 -0800331 known = vlib_buffer_is_known (bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332 if (known != expected_state)
333 {
334 ASSERT (0);
335 vlib_panic_with_msg
336 (vm, "%s %U buffer 0x%x",
337 is_free ? "freeing" : "allocating",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400338 format_vlib_buffer_known_state, known, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700339 }
340
341 vlib_buffer_set_known_state
Steven899a84b2018-01-29 20:09:09 -0800342 (bi, is_free ? VLIB_BUFFER_KNOWN_FREE : VLIB_BUFFER_KNOWN_ALLOCATED);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343 }
344}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346/* Add buffer free list. */
Damjan Mariondac03522018-02-01 15:30:13 +0100347static vlib_buffer_free_list_index_t
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348vlib_buffer_create_free_list_helper (vlib_main_t * vm,
349 u32 n_data_bytes,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400350 u32 is_public, u32 is_default, u8 * name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100352 vlib_buffer_main_t *bm = &buffer_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400353 vlib_buffer_free_list_t *f;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100354 int i;
355
Damjan Marion586afd72017-04-05 19:18:20 +0200356 ASSERT (vlib_get_thread_index () == 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100357
Damjan Mariond1274cb2018-03-13 21:32:17 +0100358 if (!is_default && pool_elts (vm->buffer_free_list_pool) == 0)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100359 {
Damjan Mariondac03522018-02-01 15:30:13 +0100360 vlib_buffer_free_list_index_t default_free_free_list_index;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100361
362 /* *INDENT-OFF* */
363 default_free_free_list_index =
364 vlib_buffer_create_free_list_helper
365 (vm,
366 /* default buffer size */ VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES,
367 /* is_public */ 1,
368 /* is_default */ 1,
369 (u8 *) "default");
370 /* *INDENT-ON* */
371 ASSERT (default_free_free_list_index ==
372 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
373
374 if (n_data_bytes == VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES && is_public)
375 return default_free_free_list_index;
376 }
377
Damjan Mariond1274cb2018-03-13 21:32:17 +0100378 pool_get_aligned (vm->buffer_free_list_pool, f, CLIB_CACHE_LINE_BYTES);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100379
380 memset (f, 0, sizeof (f[0]));
Damjan Mariond1274cb2018-03-13 21:32:17 +0100381 f->index = f - vm->buffer_free_list_pool;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100382 f->n_data_bytes = vlib_buffer_round_size (n_data_bytes);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100383 f->min_n_buffers_each_alloc = VLIB_FRAME_SIZE;
384 f->buffer_pool_index = 0;
Chris Luke7447d072017-07-05 12:57:10 -0400385 f->name = clib_mem_is_vec (name) ? name : format (0, "%s", name);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100386
387 /* Setup free buffer template. */
Damjan Marion072401e2017-07-13 18:53:27 +0200388 vlib_buffer_set_free_list_index (&f->buffer_init_template, f->index);
Damjan Marionc47ed032017-01-25 14:18:03 +0100389 f->buffer_init_template.n_add_refs = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100390
391 if (is_public)
392 {
393 uword *p = hash_get (bm->free_list_by_size, f->n_data_bytes);
394 if (!p)
395 hash_set (bm->free_list_by_size, f->n_data_bytes, f->index);
396 }
397
398 for (i = 1; i < vec_len (vlib_mains); i++)
399 {
Damjan Mariond1274cb2018-03-13 21:32:17 +0100400 vlib_main_t *wvm = vlib_mains[i];
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100401 vlib_buffer_free_list_t *wf;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100402 pool_get_aligned (wvm->buffer_free_list_pool,
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100403 wf, CLIB_CACHE_LINE_BYTES);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100404 ASSERT (f - vm->buffer_free_list_pool ==
405 wf - wvm->buffer_free_list_pool);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100406 wf[0] = f[0];
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100407 wf->buffers = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100408 wf->n_alloc = 0;
409 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410
411 return f->index;
412}
413
Damjan Mariondac03522018-02-01 15:30:13 +0100414vlib_buffer_free_list_index_t
Dave Barach9b8ffd92016-07-08 08:13:45 -0400415vlib_buffer_create_free_list (vlib_main_t * vm, u32 n_data_bytes,
416 char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417{
418 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400419 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420
421 va_start (va, fmt);
422 name = va_format (0, fmt, &va);
423 va_end (va);
424
425 return vlib_buffer_create_free_list_helper (vm, n_data_bytes,
426 /* is_public */ 0,
427 /* is_default */ 0,
428 name);
429}
430
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431static void
432del_free_list (vlib_main_t * vm, vlib_buffer_free_list_t * f)
433{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100434 vlib_buffer_pool_t *bp = vlib_buffer_pool_get (f->buffer_pool_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435
Damjan Mariond1274cb2018-03-13 21:32:17 +0100436 vec_add_aligned (bp->buffers, f->buffers, vec_len (f->buffers),
437 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438 vec_free (f->name);
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100439 vec_free (f->buffers);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100440
441 /* Poison it. */
442 memset (f, 0xab, sizeof (f[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443}
444
445/* Add buffer free list. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400446void
Damjan Mariondac03522018-02-01 15:30:13 +0100447vlib_buffer_delete_free_list_internal (vlib_main_t * vm,
448 vlib_buffer_free_list_index_t index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400450 vlib_buffer_free_list_t *f;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100451 int i;
452
Damjan Marion586afd72017-04-05 19:18:20 +0200453 ASSERT (vlib_get_thread_index () == 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100454
Damjan Mariondac03522018-02-01 15:30:13 +0100455 f = vlib_buffer_get_free_list (vm, index);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100456
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100457 ASSERT (vec_len (f->buffers) == f->n_alloc);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100458
459 del_free_list (vm, f);
460
Damjan Mariond1274cb2018-03-13 21:32:17 +0100461 pool_put (vm->buffer_free_list_pool, f);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100462
463 for (i = 1; i < vec_len (vlib_mains); i++)
464 {
Damjan Mariond1274cb2018-03-13 21:32:17 +0100465 vlib_main_t *wvm = vlib_mains[i];
466 f = vlib_buffer_get_free_list (vlib_mains[i], index);
467 del_free_list (wvm, f);
468 pool_put (wvm->buffer_free_list_pool, f);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100469 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700470}
471
Damjan Mariond1274cb2018-03-13 21:32:17 +0100472static_always_inline void *
473vlib_buffer_pool_get_buffer (vlib_buffer_pool_t * bp)
474{
475 uword slot, page, addr;
476
477 if (PREDICT_FALSE (bp->n_elts == bp->n_used))
478 {
479 clib_spinlock_unlock (&bp->lock);
480 return 0;
481 }
482 slot = bp->next_clear;
483 bp->bitmap = clib_bitmap_set (bp->bitmap, slot, 1);
484 bp->next_clear = clib_bitmap_next_clear (bp->bitmap, slot + 1);
485 bp->n_used++;
486
487 page = slot / bp->buffers_per_page;
488 slot -= page * bp->buffers_per_page;
489
490 addr = bp->start + (page << bp->log2_page_size) + slot * bp->buffer_size;
491
492 return uword_to_pointer (addr, void *);
493}
494
Ed Warnickecb9cada2015-12-08 15:45:58 -0700495/* Make sure free list has at least given number of free buffers. */
496static uword
Damjan Marionc8a26c62017-11-24 20:15:23 +0100497vlib_buffer_fill_free_list_internal (vlib_main_t * vm,
498 vlib_buffer_free_list_t * fl,
499 uword min_free_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100501 vlib_buffer_t *b;
502 vlib_buffer_pool_t *bp = vlib_buffer_pool_get (fl->buffer_pool_index);
503 int n;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400504 u32 *bi;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100505 u32 n_alloc = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507 /* Already have enough free buffers on free list? */
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100508 n = min_free_buffers - vec_len (fl->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509 if (n <= 0)
510 return min_free_buffers;
511
Damjan Mariond1274cb2018-03-13 21:32:17 +0100512 if (vec_len (bp->buffers) > 0)
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200513 {
514 int n_copy, n_left;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100515 clib_spinlock_lock (&bp->lock);
516 n_copy = clib_min (vec_len (bp->buffers), n);
517 n_left = vec_len (bp->buffers) - n_copy;
518 vec_add_aligned (fl->buffers, bp->buffers + n_left, n_copy,
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200519 CLIB_CACHE_LINE_BYTES);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100520 _vec_len (bp->buffers) = n_left;
521 clib_spinlock_unlock (&bp->lock);
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200522 n = min_free_buffers - vec_len (fl->buffers);
523 if (n <= 0)
524 return min_free_buffers;
525 }
526
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527 /* Always allocate round number of buffers. */
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100528 n = round_pow2 (n, CLIB_CACHE_LINE_BYTES / sizeof (u32));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529
530 /* Always allocate new buffers in reasonably large sized chunks. */
Damjan Mariond1274cb2018-03-13 21:32:17 +0100531 n = clib_max (n, fl->min_n_buffers_each_alloc);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532
Damjan Mariond1274cb2018-03-13 21:32:17 +0100533 clib_spinlock_lock (&bp->lock);
534 while (n_alloc < n)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535 {
Damjan Mariond1274cb2018-03-13 21:32:17 +0100536 if ((b = vlib_buffer_pool_get_buffer (bp)) == 0)
537 goto done;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400538
Damjan Mariond1274cb2018-03-13 21:32:17 +0100539 n_alloc += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540
Damjan Mariond1274cb2018-03-13 21:32:17 +0100541 vec_add2_aligned (fl->buffers, bi, 1, CLIB_CACHE_LINE_BYTES);
542 bi[0] = vlib_get_buffer_index (vm, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543
Damjan Mariond1274cb2018-03-13 21:32:17 +0100544 if (CLIB_DEBUG > 0)
545 vlib_buffer_set_known_state (bi[0], VLIB_BUFFER_KNOWN_FREE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546
Damjan Mariond1274cb2018-03-13 21:32:17 +0100547 memset (b, 0, sizeof (vlib_buffer_t));
548 vlib_buffer_init_for_free_list (b, fl);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400549
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550 if (fl->buffer_init_function)
Damjan Mariond1274cb2018-03-13 21:32:17 +0100551 fl->buffer_init_function (vm, fl, bi, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700552 }
Damjan Mariond1274cb2018-03-13 21:32:17 +0100553
554done:
555 clib_spinlock_unlock (&bp->lock);
556 fl->n_alloc += n_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557 return n_alloc;
558}
559
Dave Barach9b8ffd92016-07-08 08:13:45 -0400560void *
561vlib_set_buffer_free_callback (vlib_main_t * vm, void *fp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100563 vlib_buffer_main_t *bm = &buffer_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400564 void *rv = bm->buffer_free_callback;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565
566 bm->buffer_free_callback = fp;
567 return rv;
568}
569
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570static_always_inline void
Damjan Marioneac3b112018-03-05 09:36:31 +0100571recycle_or_free (vlib_main_t * vm, vlib_buffer_main_t * bm, u32 bi,
572 vlib_buffer_t * b, u32 follow_buffer_next)
573{
574 vlib_buffer_free_list_t *fl;
575 vlib_buffer_free_list_index_t fi;
Damjan Marion8e715292018-09-03 15:41:45 +0200576 u32 flags, next;
577
Damjan Marioneac3b112018-03-05 09:36:31 +0100578 fl = vlib_buffer_get_buffer_free_list (vm, b, &fi);
579
Damjan Marion8e715292018-09-03 15:41:45 +0200580 do
Damjan Marioneac3b112018-03-05 09:36:31 +0100581 {
Damjan Marion8e715292018-09-03 15:41:45 +0200582 vlib_buffer_t *nb = vlib_get_buffer (vm, bi);
583 flags = nb->flags;
584 next = nb->next_buffer;
585 if (nb->n_add_refs)
586 nb->n_add_refs--;
587 else
Damjan Marioneac3b112018-03-05 09:36:31 +0100588 {
Damjan Marion8e715292018-09-03 15:41:45 +0200589 vlib_buffer_validate_alloc_free (vm, &bi, 1,
590 VLIB_BUFFER_KNOWN_ALLOCATED);
591 vlib_buffer_add_to_free_list (vm, fl, bi, 1);
Damjan Marioneac3b112018-03-05 09:36:31 +0100592 }
Damjan Marion8e715292018-09-03 15:41:45 +0200593 bi = next;
Damjan Marioneac3b112018-03-05 09:36:31 +0100594 }
Damjan Marion8e715292018-09-03 15:41:45 +0200595 while (follow_buffer_next && (flags & VLIB_BUFFER_NEXT_PRESENT));
Damjan Marioneac3b112018-03-05 09:36:31 +0100596}
597
598static_always_inline void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599vlib_buffer_free_inline (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400600 u32 * buffers, u32 n_buffers, u32 follow_buffer_next)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100602 vlib_buffer_main_t *bm = &buffer_main;
Damjan Marioneac3b112018-03-05 09:36:31 +0100603 vlib_buffer_t *p, *b0, *b1, *b2, *b3;
604 int i = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400605 u32 (*cb) (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
606 u32 follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700607
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608 cb = bm->buffer_free_callback;
609
610 if (PREDICT_FALSE (cb != 0))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400611 n_buffers = (*cb) (vm, buffers, n_buffers, follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700612
Dave Barach9b8ffd92016-07-08 08:13:45 -0400613 if (!n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700614 return;
615
Damjan Marioneac3b112018-03-05 09:36:31 +0100616 while (i + 11 < n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700617 {
Damjan Marioneac3b112018-03-05 09:36:31 +0100618 p = vlib_get_buffer (vm, buffers[i + 8]);
619 vlib_prefetch_buffer_header (p, LOAD);
620 p = vlib_get_buffer (vm, buffers[i + 9]);
621 vlib_prefetch_buffer_header (p, LOAD);
622 p = vlib_get_buffer (vm, buffers[i + 10]);
623 vlib_prefetch_buffer_header (p, LOAD);
624 p = vlib_get_buffer (vm, buffers[i + 11]);
625 vlib_prefetch_buffer_header (p, LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626
Damjan Marioneac3b112018-03-05 09:36:31 +0100627 b0 = vlib_get_buffer (vm, buffers[i]);
628 b1 = vlib_get_buffer (vm, buffers[i + 1]);
629 b2 = vlib_get_buffer (vm, buffers[i + 2]);
630 b3 = vlib_get_buffer (vm, buffers[i + 3]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631
Damjan Marioneac3b112018-03-05 09:36:31 +0100632 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
633 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1);
634 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b2);
635 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700636
Damjan Marioneac3b112018-03-05 09:36:31 +0100637 recycle_or_free (vm, bm, buffers[i], b0, follow_buffer_next);
638 recycle_or_free (vm, bm, buffers[i + 1], b1, follow_buffer_next);
639 recycle_or_free (vm, bm, buffers[i + 2], b2, follow_buffer_next);
640 recycle_or_free (vm, bm, buffers[i + 3], b3, follow_buffer_next);
Damjan Marionc47ed032017-01-25 14:18:03 +0100641
Damjan Marioneac3b112018-03-05 09:36:31 +0100642 i += 4;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643 }
Damjan Marioneac3b112018-03-05 09:36:31 +0100644
645 while (i < n_buffers)
646 {
647 b0 = vlib_get_buffer (vm, buffers[i]);
648 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
649 recycle_or_free (vm, bm, buffers[i], b0, follow_buffer_next);
650 i++;
651 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700652}
653
Damjan Marion878c6092017-01-04 13:19:27 +0100654static void
655vlib_buffer_free_internal (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400657 vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
658 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659}
660
Damjan Marion878c6092017-01-04 13:19:27 +0100661static void
662vlib_buffer_free_no_next_internal (vlib_main_t * vm, u32 * buffers,
663 u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400665 vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
666 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700667}
668
669/* Copy template packet data into buffers as they are allocated. */
Damjan Marion878c6092017-01-04 13:19:27 +0100670static void __attribute__ ((unused))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671vlib_packet_template_buffer_init (vlib_main_t * vm,
672 vlib_buffer_free_list_t * fl,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400673 u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700674{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400675 vlib_packet_template_t *t =
676 uword_to_pointer (fl->buffer_init_function_opaque,
677 vlib_packet_template_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678 uword i;
679
680 for (i = 0; i < n_buffers; i++)
681 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400682 vlib_buffer_t *b = vlib_get_buffer (vm, buffers[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683 ASSERT (b->current_length == vec_len (t->packet_data));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400684 clib_memcpy (vlib_buffer_get_current (b), t->packet_data,
685 b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686 }
687}
688
Dave Barach9b8ffd92016-07-08 08:13:45 -0400689void
690vlib_packet_template_init (vlib_main_t * vm,
691 vlib_packet_template_t * t,
692 void *packet_data,
693 uword n_packet_data_bytes,
Damjan Mariond1274cb2018-03-13 21:32:17 +0100694 uword min_n_buffers_each_alloc, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700695{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100696 vlib_buffer_main_t *bm = &buffer_main;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100697 va_list va;
Chris Luke7447d072017-07-05 12:57:10 -0400698 u8 *name;
Damjan Marion878c6092017-01-04 13:19:27 +0100699 vlib_buffer_free_list_t *fl;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100700
701 va_start (va, fmt);
702 name = va_format (0, fmt, &va);
703 va_end (va);
704
Damjan Marion878c6092017-01-04 13:19:27 +0100705 if (bm->cb.vlib_packet_template_init_cb)
706 bm->cb.vlib_packet_template_init_cb (vm, (void *) t, packet_data,
707 n_packet_data_bytes,
Damjan Mariond1274cb2018-03-13 21:32:17 +0100708 min_n_buffers_each_alloc, name);
Damjan Marion878c6092017-01-04 13:19:27 +0100709
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100710 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711
712 memset (t, 0, sizeof (t[0]));
713
714 vec_add (t->packet_data, packet_data, n_packet_data_bytes);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100715 t->min_n_buffers_each_alloc = min_n_buffers_each_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700716
717 t->free_list_index = vlib_buffer_create_free_list_helper
718 (vm, n_packet_data_bytes,
719 /* is_public */ 1,
720 /* is_default */ 0,
721 name);
722
723 ASSERT (t->free_list_index != 0);
724 fl = vlib_buffer_get_free_list (vm, t->free_list_index);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100725 fl->min_n_buffers_each_alloc = t->min_n_buffers_each_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726
727 fl->buffer_init_function = vlib_packet_template_buffer_init;
728 fl->buffer_init_function_opaque = pointer_to_uword (t);
729
730 fl->buffer_init_template.current_data = 0;
731 fl->buffer_init_template.current_length = n_packet_data_bytes;
732 fl->buffer_init_template.flags = 0;
Damjan Marionc47ed032017-01-25 14:18:03 +0100733 fl->buffer_init_template.n_add_refs = 0;
Damjan Marion878c6092017-01-04 13:19:27 +0100734 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700735}
736
737void *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400738vlib_packet_template_get_packet (vlib_main_t * vm,
739 vlib_packet_template_t * t, u32 * bi_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700740{
741 u32 bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400742 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700743
744 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
745 return 0;
746
747 *bi_result = bi;
748
749 b = vlib_get_buffer (vm, bi);
Damjan Marionf1213b82016-03-13 02:22:06 +0100750 clib_memcpy (vlib_buffer_get_current (b),
Dave Barach9b8ffd92016-07-08 08:13:45 -0400751 t->packet_data, vec_len (t->packet_data));
752 b->current_length = vec_len (t->packet_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700753
754 return b->data;
755}
756
Dave Barach9b8ffd92016-07-08 08:13:45 -0400757void
758vlib_packet_template_get_packet_helper (vlib_main_t * vm,
759 vlib_packet_template_t * t)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100761 word n = t->min_n_buffers_each_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700762 word l = vec_len (t->packet_data);
763 word n_alloc;
764
765 ASSERT (l > 0);
766 ASSERT (vec_len (t->free_buffers) == 0);
767
768 vec_validate (t->free_buffers, n - 1);
769 n_alloc = vlib_buffer_alloc_from_free_list (vm, t->free_buffers,
770 n, t->free_list_index);
771 _vec_len (t->free_buffers) = n_alloc;
772}
773
774/* Append given data to end of buffer, possibly allocating new buffers. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400775u32
776vlib_buffer_add_data (vlib_main_t * vm,
Damjan Mariondac03522018-02-01 15:30:13 +0100777 vlib_buffer_free_list_index_t free_list_index,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400778 u32 buffer_index, void *data, u32 n_data_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700779{
780 u32 n_buffer_bytes, n_left, n_left_this_buffer, bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400781 vlib_buffer_t *b;
782 void *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700783
784 bi = buffer_index;
Dave Barach0d65d112018-03-16 15:21:35 -0400785 if (bi == ~0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700786 && 1 != vlib_buffer_alloc_from_free_list (vm, &bi, 1, free_list_index))
787 goto out_of_buffers;
788
789 d = data;
790 n_left = n_data_bytes;
791 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm, free_list_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400792
Ed Warnickecb9cada2015-12-08 15:45:58 -0700793 b = vlib_get_buffer (vm, bi);
794 b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
795
Dave Barach9b8ffd92016-07-08 08:13:45 -0400796 /* Get to the end of the chain before we try to append data... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
798 b = vlib_get_buffer (vm, b->next_buffer);
799
800 while (1)
801 {
802 u32 n;
803
804 ASSERT (n_buffer_bytes >= b->current_length);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400805 n_left_this_buffer =
806 n_buffer_bytes - (b->current_data + b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700807 n = clib_min (n_left_this_buffer, n_left);
Damjan Marionf1213b82016-03-13 02:22:06 +0100808 clib_memcpy (vlib_buffer_get_current (b) + b->current_length, d, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700809 b->current_length += n;
810 n_left -= n;
811 if (n_left == 0)
812 break;
813
814 d += n;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400815 if (1 !=
816 vlib_buffer_alloc_from_free_list (vm, &b->next_buffer, 1,
817 free_list_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700818 goto out_of_buffers;
819
820 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
821
822 b = vlib_get_buffer (vm, b->next_buffer);
823 }
824
825 return bi;
826
Dave Barach9b8ffd92016-07-08 08:13:45 -0400827out_of_buffers:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700828 clib_error ("out of buffers");
829 return bi;
830}
831
Pierre Pfister328e99b2016-02-12 13:18:42 +0000832u16
Dave Barach9b8ffd92016-07-08 08:13:45 -0400833vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
Damjan Mariondac03522018-02-01 15:30:13 +0100834 vlib_buffer_free_list_index_t
835 free_list_index,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400836 vlib_buffer_t * first,
Damjan Mariondac03522018-02-01 15:30:13 +0100837 vlib_buffer_t ** last, void *data,
838 u16 data_len)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400839{
Pierre Pfister328e99b2016-02-12 13:18:42 +0000840 vlib_buffer_t *l = *last;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400841 u32 n_buffer_bytes =
842 vlib_buffer_free_list_buffer_size (vm, free_list_index);
Pierre Pfister328e99b2016-02-12 13:18:42 +0000843 u16 copied = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400844 ASSERT (n_buffer_bytes >= l->current_length + l->current_data);
845 while (data_len)
846 {
847 u16 max = n_buffer_bytes - l->current_length - l->current_data;
848 if (max == 0)
849 {
850 if (1 !=
851 vlib_buffer_alloc_from_free_list (vm, &l->next_buffer, 1,
852 free_list_index))
853 return copied;
854 *last = l = vlib_buffer_chain_buffer (vm, first, l, l->next_buffer);
855 max = n_buffer_bytes - l->current_length - l->current_data;
856 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000857
Dave Barach9b8ffd92016-07-08 08:13:45 -0400858 u16 len = (data_len > max) ? max : data_len;
859 clib_memcpy (vlib_buffer_get_current (l) + l->current_length,
860 data + copied, len);
861 vlib_buffer_chain_increase_length (first, l, len);
862 data_len -= len;
863 copied += len;
864 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000865 return copied;
866}
867
Damjan Marion149ba772017-10-12 13:09:26 +0200868u8
Damjan Mariond1274cb2018-03-13 21:32:17 +0100869vlib_buffer_pool_create (vlib_main_t * vm, vlib_physmem_region_index_t pri,
870 u16 buffer_size)
Damjan Marion04a7f052017-07-10 15:06:17 +0200871{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100872 vlib_buffer_main_t *bm = &buffer_main;
Damjan Marion149ba772017-10-12 13:09:26 +0200873 vlib_physmem_region_t *pr = vlib_physmem_get_region (vm, pri);
874 vlib_buffer_pool_t *p;
875 uword start = pointer_to_uword (pr->mem);
876 uword size = pr->size;
Damjan Marion04a7f052017-07-10 15:06:17 +0200877
878 if (bm->buffer_mem_size == 0)
879 {
880 bm->buffer_mem_start = start;
881 bm->buffer_mem_size = size;
882 }
883 else if (start < bm->buffer_mem_start)
884 {
885 bm->buffer_mem_size += bm->buffer_mem_start - start;
886 bm->buffer_mem_start = start;
887 if (size > bm->buffer_mem_size)
888 bm->buffer_mem_size = size;
889 }
890 else if (start > bm->buffer_mem_start)
891 {
892 uword new_size = start - bm->buffer_mem_start + size;
893 if (new_size > bm->buffer_mem_size)
894 bm->buffer_mem_size = new_size;
895 }
896
897 if ((u64) bm->buffer_mem_size >
898 ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES)))
899 {
900 clib_panic ("buffer memory size out of range!");
901 }
Damjan Marion149ba772017-10-12 13:09:26 +0200902
903 vec_add2 (bm->buffer_pools, p, 1);
904 p->start = start;
905 p->size = size;
906 p->physmem_region = pri;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100907
908 if (buffer_size == 0)
909 goto done;
910
911 p->log2_page_size = pr->log2_page_size;
912 p->buffer_size = buffer_size;
913 p->buffers_per_page = (1 << pr->log2_page_size) / p->buffer_size;
914 p->n_elts = p->buffers_per_page * pr->n_pages;
915 p->n_used = 0;
916 clib_spinlock_init (&p->lock);
917done:
918 ASSERT (p - bm->buffer_pools < 256);
Damjan Marion149ba772017-10-12 13:09:26 +0200919 return p - bm->buffer_pools;
Damjan Marion04a7f052017-07-10 15:06:17 +0200920}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700921
Dave Barach9b8ffd92016-07-08 08:13:45 -0400922static u8 *
923format_vlib_buffer_free_list (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700924{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400925 vlib_buffer_free_list_t *f = va_arg (*va, vlib_buffer_free_list_t *);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100926 u32 threadnum = va_arg (*va, u32);
927 uword bytes_alloc, bytes_free, n_free, size;
928
929 if (!f)
930 return format (s, "%=7s%=30s%=12s%=12s%=12s%=12s%=12s%=12s",
931 "Thread", "Name", "Index", "Size", "Alloc", "Free",
932 "#Alloc", "#Free");
933
934 size = sizeof (vlib_buffer_t) + f->n_data_bytes;
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100935 n_free = vec_len (f->buffers);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100936 bytes_alloc = size * f->n_alloc;
937 bytes_free = size * n_free;
938
Chris Luke7447d072017-07-05 12:57:10 -0400939 s = format (s, "%7d%30v%12d%12d%=12U%=12U%=12d%=12d", threadnum,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700940 f->name, f->index, f->n_data_bytes,
941 format_memory_size, bytes_alloc,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400942 format_memory_size, bytes_free, f->n_alloc, n_free);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700943
944 return s;
945}
946
947static clib_error_t *
948show_buffers (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400949 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700950{
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100951 vlib_buffer_free_list_t *f;
952 vlib_main_t *curr_vm;
953 u32 vm_index = 0;
954
955 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, 0, 0);
956
957 do
958 {
Dave Barach80f54e22017-03-08 19:08:56 -0500959 curr_vm = vlib_mains[vm_index];
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100960
961 /* *INDENT-OFF* */
Damjan Mariond1274cb2018-03-13 21:32:17 +0100962 pool_foreach (f, curr_vm->buffer_free_list_pool, ({
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100963 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, f, vm_index);
964 }));
965 /* *INDENT-ON* */
966
967 vm_index++;
968 }
969 while (vm_index < vec_len (vlib_mains));
970
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971 return 0;
972}
973
Dave Barach9b8ffd92016-07-08 08:13:45 -0400974/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975VLIB_CLI_COMMAND (show_buffers_command, static) = {
976 .path = "show buffers",
977 .short_help = "Show packet buffer allocation",
978 .function = show_buffers,
979};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400980/* *INDENT-ON* */
981
Damjan Marion49d66f12017-07-20 18:10:35 +0200982clib_error_t *
983vlib_buffer_main_init (struct vlib_main_t * vm)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100984{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100985 vlib_buffer_main_t *bm = &buffer_main;
Damjan Marion149ba772017-10-12 13:09:26 +0200986 vlib_physmem_region_index_t pri;
Damjan Marion49d66f12017-07-20 18:10:35 +0200987 clib_error_t *error;
988
Damjan Marion49d66f12017-07-20 18:10:35 +0200989 if (vlib_buffer_callbacks)
990 {
991 /* external plugin has registered own buffer callbacks
992 so we just copy them and quit */
Damjan Marion49d66f12017-07-20 18:10:35 +0200993 clib_memcpy (&bm->cb, vlib_buffer_callbacks,
994 sizeof (vlib_buffer_callbacks_t));
995 bm->callbacks_registered = 1;
996 return 0;
997 }
Damjan Marion04a7f052017-07-10 15:06:17 +0200998
Damjan Marionc8a26c62017-11-24 20:15:23 +0100999 bm->cb.vlib_buffer_fill_free_list_cb = &vlib_buffer_fill_free_list_internal;
Damjan Marion878c6092017-01-04 13:19:27 +01001000 bm->cb.vlib_buffer_free_cb = &vlib_buffer_free_internal;
1001 bm->cb.vlib_buffer_free_no_next_cb = &vlib_buffer_free_no_next_internal;
1002 bm->cb.vlib_buffer_delete_free_list_cb =
1003 &vlib_buffer_delete_free_list_internal;
Damjan Marion6b0f5892017-07-27 04:01:24 -04001004 clib_spinlock_init (&bm->buffer_known_hash_lockp);
Damjan Marion49d66f12017-07-20 18:10:35 +02001005
1006 /* allocate default region */
1007 error = vlib_physmem_region_alloc (vm, "buffers",
1008 vlib_buffer_physmem_sz, 0,
Damjan Marion1ba0fa42018-03-04 17:19:08 +01001009 VLIB_PHYSMEM_F_SHARED |
Damjan Mariond1274cb2018-03-13 21:32:17 +01001010 VLIB_PHYSMEM_F_HUGETLB, &pri);
Damjan Marion49d66f12017-07-20 18:10:35 +02001011
1012 if (error == 0)
Damjan Marion149ba772017-10-12 13:09:26 +02001013 goto done;
Damjan Marion49d66f12017-07-20 18:10:35 +02001014
1015 clib_error_free (error);
1016
Damjan Marion1ba0fa42018-03-04 17:19:08 +01001017 error = vlib_physmem_region_alloc (vm, "buffers",
Damjan Marion49d66f12017-07-20 18:10:35 +02001018 vlib_buffer_physmem_sz, 0,
Damjan Mariond1274cb2018-03-13 21:32:17 +01001019 VLIB_PHYSMEM_F_SHARED, &pri);
Damjan Marion149ba772017-10-12 13:09:26 +02001020done:
Damjan Marion1ba0fa42018-03-04 17:19:08 +01001021 if (error == 0)
Damjan Mariond1274cb2018-03-13 21:32:17 +01001022 vlib_buffer_pool_create (vm, pri, sizeof (vlib_buffer_t) +
1023 VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES);
Damjan Marion49d66f12017-07-20 18:10:35 +02001024 return error;
Damjan Marion878c6092017-01-04 13:19:27 +01001025}
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001026
Damjan Marion49d66f12017-07-20 18:10:35 +02001027static clib_error_t *
1028vlib_buffers_configure (vlib_main_t * vm, unformat_input_t * input)
1029{
1030 u32 size_in_mb;
1031
1032 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1033 {
1034 if (unformat (input, "memory-size-in-mb %d", &size_in_mb))
1035 vlib_buffer_physmem_sz = size_in_mb << 20;
1036 else
1037 return unformat_parse_error (input);
1038 }
1039
1040 unformat_free (input);
1041 return 0;
1042}
1043
1044VLIB_EARLY_CONFIG_FUNCTION (vlib_buffers_configure, "buffers");
1045
1046
Chris Luked4024f52016-09-06 09:32:36 -04001047/** @endcond */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001048/*
1049 * fd.io coding-style-patch-verification: ON
1050 *
1051 * Local Variables:
1052 * eval: (c-set-style "gnu")
1053 * End:
1054 */