blob: 7399b618ed45c5b7c754dbb5d276efa206763a2f [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
Dave Barach9b8ffd92016-07-08 08:13:45 -040052uword
53vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
54 vlib_buffer_t * b_first)
Ed Warnickecb9cada2015-12-08 15:45:58 -070055{
Dave Barach9b8ffd92016-07-08 08:13:45 -040056 vlib_buffer_t *b = b_first;
Ed Warnickecb9cada2015-12-08 15:45:58 -070057 uword l_first = b_first->current_length;
58 uword l = 0;
59 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
60 {
61 b = vlib_get_buffer (vm, b->next_buffer);
62 l += b->current_length;
63 }
64 b_first->total_length_not_including_first_buffer = l;
65 b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
66 return l + l_first;
67}
68
Dave Barach9b8ffd92016-07-08 08:13:45 -040069u8 *
70format_vlib_buffer (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070071{
Dave Barach9b8ffd92016-07-08 08:13:45 -040072 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010073 uword indent = format_get_indent (s);
74
Damjan Marionc47ed032017-01-25 14:18:03 +010075 s = format (s, "current data %d, length %d, free-list %d, clone-count %u",
Damjan Marion072401e2017-07-13 18:53:27 +020076 b->current_data, b->current_length,
77 vlib_buffer_get_free_list_index (b), b->n_add_refs);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010078
79 if (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID)
80 s = format (s, ", totlen-nifb %d",
81 b->total_length_not_including_first_buffer);
82
83 if (b->flags & VLIB_BUFFER_IS_TRACED)
84 s = format (s, ", trace 0x%x", b->trace_index);
85
86 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
87 {
88 vlib_main_t *vm = vlib_get_main ();
89 u32 next_buffer = b->next_buffer;
90 b = vlib_get_buffer (vm, next_buffer);
91
Damjan Marionc47ed032017-01-25 14:18:03 +010092 s =
93 format (s, "\n%Unext-buffer 0x%x, segment length %d, clone-count %u",
94 format_white_space, indent, next_buffer, b->current_length,
95 b->n_add_refs);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010096 }
97
Ed Warnickecb9cada2015-12-08 15:45:58 -070098 return s;
99}
100
Dave Barach9b8ffd92016-07-08 08:13:45 -0400101u8 *
102format_vlib_buffer_and_data (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400104 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
105
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 s = format (s, "%U, %U",
107 format_vlib_buffer, b,
108 format_hex_bytes, vlib_buffer_get_current (b), 64);
109
110 return s;
111}
112
Dave Barach9b8ffd92016-07-08 08:13:45 -0400113static u8 *
114format_vlib_buffer_known_state (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115{
116 vlib_buffer_known_state_t state = va_arg (*args, vlib_buffer_known_state_t);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400117 char *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118
119 switch (state)
120 {
121 case VLIB_BUFFER_UNKNOWN:
122 t = "unknown";
123 break;
124
125 case VLIB_BUFFER_KNOWN_ALLOCATED:
126 t = "known-allocated";
127 break;
128
129 case VLIB_BUFFER_KNOWN_FREE:
130 t = "known-free";
131 break;
132
133 default:
134 t = "invalid";
135 break;
136 }
137
138 return format (s, "%s", t);
139}
140
Dave Barach9b8ffd92016-07-08 08:13:45 -0400141u8 *
142format_vlib_buffer_contents (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400144 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
145 vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *);
146
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147 while (1)
148 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400149 vec_add (s, vlib_buffer_get_current (b), b->current_length);
150 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151 break;
152 b = vlib_get_buffer (vm, b->next_buffer);
153 }
154
155 return s;
156}
157
158static u8 *
159vlib_validate_buffer_helper (vlib_main_t * vm,
160 u32 bi,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400161 uword follow_buffer_next, uword ** unique_hash)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400163 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
164 vlib_buffer_main_t *bm = vm->buffer_main;
165 vlib_buffer_free_list_t *fl;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166
Damjan Marion072401e2017-07-13 18:53:27 +0200167 if (pool_is_free_index
168 (bm->buffer_free_list_pool, vlib_buffer_get_free_list_index (b)))
169 return format (0, "unknown free list 0x%x",
170 vlib_buffer_get_free_list_index (b));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171
Damjan Marion072401e2017-07-13 18:53:27 +0200172 fl =
173 pool_elt_at_index (bm->buffer_free_list_pool,
174 vlib_buffer_get_free_list_index (b));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
Dave Barach9b8ffd92016-07-08 08:13:45 -0400176 if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 return format (0, "current data %d before pre-data", b->current_data);
Damjan Marion878c6092017-01-04 13:19:27 +0100178
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 if (b->current_data + b->current_length > fl->n_data_bytes)
180 return format (0, "%d-%d beyond end of buffer %d",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400181 b->current_data, b->current_length, fl->n_data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182
Dave Barach9b8ffd92016-07-08 08:13:45 -0400183 if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184 {
185 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400186 u8 *msg, *result;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187
188 k = vlib_buffer_is_known (vm, b->next_buffer);
189 if (k != VLIB_BUFFER_KNOWN_ALLOCATED)
190 return format (0, "next 0x%x: %U",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400191 b->next_buffer, format_vlib_buffer_known_state, k);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192
193 if (unique_hash)
194 {
195 if (hash_get (*unique_hash, b->next_buffer))
196 return format (0, "duplicate buffer 0x%x", b->next_buffer);
197
198 hash_set1 (*unique_hash, b->next_buffer);
199 }
200
201 msg = vlib_validate_buffer (vm, b->next_buffer, follow_buffer_next);
202 if (msg)
203 {
204 result = format (0, "next 0x%x: %v", b->next_buffer, msg);
205 vec_free (msg);
206 return result;
207 }
208 }
209
210 return 0;
211}
212
213u8 *
214vlib_validate_buffer (vlib_main_t * vm, u32 bi, uword follow_buffer_next)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400215{
216 return vlib_validate_buffer_helper (vm, bi, follow_buffer_next,
217 /* unique_hash */ 0);
218}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219
220u8 *
221vlib_validate_buffers (vlib_main_t * vm,
222 u32 * buffers,
223 uword next_buffer_stride,
224 uword n_buffers,
225 vlib_buffer_known_state_t known_state,
226 uword follow_buffer_next)
227{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400228 uword i, *hash;
229 u32 bi, *b = buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400231 u8 *msg = 0, *result = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232
233 hash = hash_create (0, 0);
234 for (i = 0; i < n_buffers; i++)
235 {
236 bi = b[0];
237 b += next_buffer_stride;
238
239 /* Buffer is not unique. */
240 if (hash_get (hash, bi))
241 {
242 msg = format (0, "not unique");
243 goto done;
244 }
245
246 k = vlib_buffer_is_known (vm, bi);
247 if (k != known_state)
248 {
249 msg = format (0, "is %U; expected %U",
250 format_vlib_buffer_known_state, k,
251 format_vlib_buffer_known_state, known_state);
252 goto done;
253 }
254
255 msg = vlib_validate_buffer_helper (vm, bi, follow_buffer_next, &hash);
256 if (msg)
257 goto done;
258
259 hash_set1 (hash, bi);
260 }
261
Dave Barach9b8ffd92016-07-08 08:13:45 -0400262done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263 if (msg)
264 {
265 result = format (0, "0x%x: %v", bi, msg);
266 vec_free (msg);
267 }
268 hash_free (hash);
269 return result;
270}
271
Dave Barach80f54e22017-03-08 19:08:56 -0500272/*
273 * Hand-craft a static vector w/ length 1, so vec_len(vlib_mains) =1
274 * and vlib_mains[0] = &vlib_global_main from the beginning of time.
275 *
276 * The only place which should ever expand vlib_mains is start_workers()
277 * in threads.c. It knows about the bootstrap vector.
278 */
279/* *INDENT-OFF* */
280static struct
281{
282 vec_header_t h;
283 vlib_main_t *vm;
284} __attribute__ ((packed)) __bootstrap_vlib_main_vector
285 __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES))) =
286{
287 .h.len = 1,
288 .vm = &vlib_global_main,
289};
290/* *INDENT-ON* */
291
292vlib_main_t **vlib_mains = &__bootstrap_vlib_main_vector.vm;
293
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294
295/* When dubugging validate that given buffers are either known allocated
296 or known free. */
Damjan Marionc47ed032017-01-25 14:18:03 +0100297static void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298vlib_buffer_validate_alloc_free (vlib_main_t * vm,
299 u32 * buffers,
300 uword n_buffers,
301 vlib_buffer_known_state_t expected_state)
302{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400303 u32 *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304 uword i, bi, is_free;
305
306 if (CLIB_DEBUG == 0)
307 return;
308
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309 is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED;
310 b = buffers;
311 for (i = 0; i < n_buffers; i++)
312 {
313 vlib_buffer_known_state_t known;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400314
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315 bi = b[0];
316 b += 1;
317 known = vlib_buffer_is_known (vm, bi);
318 if (known != expected_state)
319 {
320 ASSERT (0);
321 vlib_panic_with_msg
322 (vm, "%s %U buffer 0x%x",
323 is_free ? "freeing" : "allocating",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400324 format_vlib_buffer_known_state, known, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325 }
326
327 vlib_buffer_set_known_state
328 (vm, bi,
329 is_free ? VLIB_BUFFER_KNOWN_FREE : VLIB_BUFFER_KNOWN_ALLOCATED);
330 }
331}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332
Damjan Marion8a6a3b22017-01-17 14:12:42 +0100333void
334vlib_buffer_merge_free_lists (vlib_buffer_free_list_t * dst,
335 vlib_buffer_free_list_t * src)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336{
337 uword l;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400338 u32 *d;
339
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100340 l = vec_len (src->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341 if (l > 0)
342 {
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100343 vec_add2_aligned (dst->buffers, d, l, CLIB_CACHE_LINE_BYTES);
344 clib_memcpy (d, src->buffers, l * sizeof (d[0]));
345 vec_free (src->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346 }
347}
348
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349/* Add buffer free list. */
350static u32
351vlib_buffer_create_free_list_helper (vlib_main_t * vm,
352 u32 n_data_bytes,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400353 u32 is_public, u32 is_default, u8 * name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400355 vlib_buffer_main_t *bm = vm->buffer_main;
356 vlib_buffer_free_list_t *f;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100357 int i;
358
Damjan Marion586afd72017-04-05 19:18:20 +0200359 ASSERT (vlib_get_thread_index () == 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100360
361 if (!is_default && pool_elts (bm->buffer_free_list_pool) == 0)
362 {
363 u32 default_free_free_list_index;
364
365 /* *INDENT-OFF* */
366 default_free_free_list_index =
367 vlib_buffer_create_free_list_helper
368 (vm,
369 /* default buffer size */ VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES,
370 /* is_public */ 1,
371 /* is_default */ 1,
372 (u8 *) "default");
373 /* *INDENT-ON* */
374 ASSERT (default_free_free_list_index ==
375 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
376
377 if (n_data_bytes == VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES && is_public)
378 return default_free_free_list_index;
379 }
380
381 pool_get_aligned (bm->buffer_free_list_pool, f, CLIB_CACHE_LINE_BYTES);
382
383 memset (f, 0, sizeof (f[0]));
384 f->index = f - bm->buffer_free_list_pool;
385 f->n_data_bytes = vlib_buffer_round_size (n_data_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500386 f->min_n_buffers_each_physmem_alloc = VLIB_FRAME_SIZE;
Chris Luke7447d072017-07-05 12:57:10 -0400387 f->name = clib_mem_is_vec (name) ? name : format (0, "%s", name);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100388
389 /* Setup free buffer template. */
Damjan Marion072401e2017-07-13 18:53:27 +0200390 vlib_buffer_set_free_list_index (&f->buffer_init_template, f->index);
Damjan Marionc47ed032017-01-25 14:18:03 +0100391 f->buffer_init_template.n_add_refs = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100392
393 if (is_public)
394 {
395 uword *p = hash_get (bm->free_list_by_size, f->n_data_bytes);
396 if (!p)
397 hash_set (bm->free_list_by_size, f->n_data_bytes, f->index);
398 }
399
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200400 clib_spinlock_init (&f->global_buffers_lock);
401
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100402 for (i = 1; i < vec_len (vlib_mains); i++)
403 {
404 vlib_buffer_main_t *wbm = vlib_mains[i]->buffer_main;
405 vlib_buffer_free_list_t *wf;
406 pool_get_aligned (wbm->buffer_free_list_pool,
407 wf, CLIB_CACHE_LINE_BYTES);
408 ASSERT (f - bm->buffer_free_list_pool ==
409 wf - wbm->buffer_free_list_pool);
410 wf[0] = f[0];
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100411 wf->buffers = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100412 wf->n_alloc = 0;
413 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414
415 return f->index;
416}
417
Dave Barach9b8ffd92016-07-08 08:13:45 -0400418u32
419vlib_buffer_create_free_list (vlib_main_t * vm, u32 n_data_bytes,
420 char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421{
422 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400423 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424
425 va_start (va, fmt);
426 name = va_format (0, fmt, &va);
427 va_end (va);
428
429 return vlib_buffer_create_free_list_helper (vm, n_data_bytes,
430 /* is_public */ 0,
431 /* is_default */ 0,
432 name);
433}
434
Dave Barach9b8ffd92016-07-08 08:13:45 -0400435u32
436vlib_buffer_get_or_create_free_list (vlib_main_t * vm, u32 n_data_bytes,
437 char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438{
439 u32 i = vlib_buffer_get_free_list_with_size (vm, n_data_bytes);
440
441 if (i == ~0)
442 {
443 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400444 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445
446 va_start (va, fmt);
447 name = va_format (0, fmt, &va);
448 va_end (va);
449
450 i = vlib_buffer_create_free_list_helper (vm, n_data_bytes,
451 /* is_public */ 1,
452 /* is_default */ 0,
453 name);
454 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400455
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456 return i;
457}
458
459static void
460del_free_list (vlib_main_t * vm, vlib_buffer_free_list_t * f)
461{
462 u32 i;
463
464 for (i = 0; i < vec_len (f->buffer_memory_allocated); i++)
Damjan Marion49d66f12017-07-20 18:10:35 +0200465 vm->os_physmem_free (vm, vm->buffer_main->physmem_region,
466 f->buffer_memory_allocated[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467 vec_free (f->name);
468 vec_free (f->buffer_memory_allocated);
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100469 vec_free (f->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700470}
471
472/* Add buffer free list. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400473void
Damjan Marion878c6092017-01-04 13:19:27 +0100474vlib_buffer_delete_free_list_internal (vlib_main_t * vm, u32 free_list_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700475{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400476 vlib_buffer_main_t *bm = vm->buffer_main;
477 vlib_buffer_free_list_t *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478 u32 merge_index;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100479 int i;
480
Damjan Marion586afd72017-04-05 19:18:20 +0200481 ASSERT (vlib_get_thread_index () == 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100482
483 f = vlib_buffer_get_free_list (vm, free_list_index);
484
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100485 ASSERT (vec_len (f->buffers) == f->n_alloc);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100486 merge_index = vlib_buffer_get_free_list_with_size (vm, f->n_data_bytes);
487 if (merge_index != ~0 && merge_index != free_list_index)
488 {
Damjan Marion8a6a3b22017-01-17 14:12:42 +0100489 vlib_buffer_merge_free_lists (pool_elt_at_index
490 (bm->buffer_free_list_pool, merge_index),
491 f);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100492 }
493
494 del_free_list (vm, f);
495
496 /* Poison it. */
497 memset (f, 0xab, sizeof (f[0]));
498
499 pool_put (bm->buffer_free_list_pool, f);
500
501 for (i = 1; i < vec_len (vlib_mains); i++)
502 {
503 bm = vlib_mains[i]->buffer_main;
504 f = vlib_buffer_get_free_list (vlib_mains[i], free_list_index);;
505 memset (f, 0xab, sizeof (f[0]));
506 pool_put (bm->buffer_free_list_pool, f);
507 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508}
509
510/* Make sure free list has at least given number of free buffers. */
511static uword
512fill_free_list (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400513 vlib_buffer_free_list_t * fl, uword min_free_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400515 vlib_buffer_t *buffers, *b;
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200516 vlib_buffer_free_list_t *mfl;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517 int n, n_bytes, i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400518 u32 *bi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519 u32 n_remaining, n_alloc, n_this_chunk;
520
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521 /* Already have enough free buffers on free list? */
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100522 n = min_free_buffers - vec_len (fl->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523 if (n <= 0)
524 return min_free_buffers;
525
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200526 mfl = vlib_buffer_get_free_list (vlib_mains[0], fl->index);
527 if (vec_len (mfl->global_buffers) > 0)
528 {
529 int n_copy, n_left;
530 clib_spinlock_lock (&mfl->global_buffers_lock);
531 n_copy = clib_min (vec_len (mfl->global_buffers), n);
532 n_left = vec_len (mfl->global_buffers) - n_copy;
533 vec_add_aligned (fl->buffers, mfl->global_buffers + n_left, n_copy,
534 CLIB_CACHE_LINE_BYTES);
535 _vec_len (mfl->global_buffers) = n_left;
536 clib_spinlock_unlock (&mfl->global_buffers_lock);
537 n = min_free_buffers - vec_len (fl->buffers);
538 if (n <= 0)
539 return min_free_buffers;
540 }
541
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542 /* Always allocate round number of buffers. */
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100543 n = round_pow2 (n, CLIB_CACHE_LINE_BYTES / sizeof (u32));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544
545 /* Always allocate new buffers in reasonably large sized chunks. */
546 n = clib_max (n, fl->min_n_buffers_each_physmem_alloc);
547
548 n_remaining = n;
549 n_alloc = 0;
550 while (n_remaining > 0)
551 {
552 n_this_chunk = clib_min (n_remaining, 16);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400553
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554 n_bytes = n_this_chunk * (sizeof (b[0]) + fl->n_data_bytes);
555
556 /* drb: removed power-of-2 ASSERT */
Damjan Marion49d66f12017-07-20 18:10:35 +0200557 buffers =
558 vm->os_physmem_alloc_aligned (vm, vm->buffer_main->physmem_region,
559 n_bytes, sizeof (vlib_buffer_t));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400560 if (!buffers)
561 return n_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562
563 /* Record chunk as being allocated so we can free it later. */
564 vec_add1 (fl->buffer_memory_allocated, buffers);
565
566 fl->n_alloc += n_this_chunk;
567 n_alloc += n_this_chunk;
568 n_remaining -= n_this_chunk;
569
570 b = buffers;
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100571 vec_add2_aligned (fl->buffers, bi, n_this_chunk, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572 for (i = 0; i < n_this_chunk; i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400573 {
574 bi[i] = vlib_get_buffer_index (vm, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575
Dave Barach9b8ffd92016-07-08 08:13:45 -0400576 if (CLIB_DEBUG > 0)
577 vlib_buffer_set_known_state (vm, bi[i], VLIB_BUFFER_KNOWN_FREE);
578 b = vlib_buffer_next_contiguous (b, fl->n_data_bytes);
579 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580
581 memset (buffers, 0, n_bytes);
582
583 /* Initialize all new buffers. */
584 b = buffers;
585 for (i = 0; i < n_this_chunk; i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400586 {
587 vlib_buffer_init_for_free_list (b, fl);
588 b = vlib_buffer_next_contiguous (b, fl->n_data_bytes);
589 }
590
Ed Warnickecb9cada2015-12-08 15:45:58 -0700591 if (fl->buffer_init_function)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400592 fl->buffer_init_function (vm, fl, bi, n_this_chunk);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700593 }
594 return n_alloc;
595}
596
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597static u32
598alloc_from_free_list (vlib_main_t * vm,
599 vlib_buffer_free_list_t * free_list,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400600 u32 * alloc_buffers, u32 n_alloc_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601{
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100602 u32 *dst, *src;
603 uword len;
604 uword n_filled;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700605
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606 dst = alloc_buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700607
608 n_filled = fill_free_list (vm, free_list, n_alloc_buffers);
609 if (n_filled == 0)
610 return 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400611
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100612 len = vec_len (free_list->buffers);
613 ASSERT (len >= n_alloc_buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700614
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100615 src = free_list->buffers + len - n_alloc_buffers;
616 clib_memcpy (dst, src, n_alloc_buffers * sizeof (u32));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700617
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100618 _vec_len (free_list->buffers) -= n_alloc_buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700619
620 /* Verify that buffers are known free. */
621 vlib_buffer_validate_alloc_free (vm, alloc_buffers,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400622 n_alloc_buffers, VLIB_BUFFER_KNOWN_FREE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623
624 return n_alloc_buffers;
625}
626
Damjan Marion878c6092017-01-04 13:19:27 +0100627
Ed Warnickecb9cada2015-12-08 15:45:58 -0700628/* Allocate a given number of buffers into given array.
629 Returns number actually allocated which will be either zero or
630 number requested. */
Damjan Marion878c6092017-01-04 13:19:27 +0100631static u32
632vlib_buffer_alloc_internal (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400634 vlib_buffer_main_t *bm = vm->buffer_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635
636 return alloc_from_free_list
637 (vm,
638 pool_elt_at_index (bm->buffer_free_list_pool,
639 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX),
640 buffers, n_buffers);
641}
642
Damjan Marion878c6092017-01-04 13:19:27 +0100643static u32
644vlib_buffer_alloc_from_free_list_internal (vlib_main_t * vm,
645 u32 * buffers,
646 u32 n_buffers, u32 free_list_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700647{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400648 vlib_buffer_main_t *bm = vm->buffer_main;
649 vlib_buffer_free_list_t *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650 f = pool_elt_at_index (bm->buffer_free_list_pool, free_list_index);
651 return alloc_from_free_list (vm, f, buffers, n_buffers);
652}
653
Dave Barach9b8ffd92016-07-08 08:13:45 -0400654void *
655vlib_set_buffer_free_callback (vlib_main_t * vm, void *fp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400657 vlib_buffer_main_t *bm = vm->buffer_main;
658 void *rv = bm->buffer_free_callback;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659
660 bm->buffer_free_callback = fp;
661 return rv;
662}
663
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664static_always_inline void
665vlib_buffer_free_inline (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400666 u32 * buffers, u32 n_buffers, u32 follow_buffer_next)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700667{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400668 vlib_buffer_main_t *bm = vm->buffer_main;
669 vlib_buffer_free_list_t *fl;
Damjan Marionc47ed032017-01-25 14:18:03 +0100670 u32 fi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671 int i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400672 u32 (*cb) (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
673 u32 follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700674
Ed Warnickecb9cada2015-12-08 15:45:58 -0700675 cb = bm->buffer_free_callback;
676
677 if (PREDICT_FALSE (cb != 0))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400678 n_buffers = (*cb) (vm, buffers, n_buffers, follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700679
Dave Barach9b8ffd92016-07-08 08:13:45 -0400680 if (!n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700681 return;
682
Damjan Marionc47ed032017-01-25 14:18:03 +0100683 for (i = 0; i < n_buffers; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700684 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100685 vlib_buffer_t *b;
686 u32 bi = buffers[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700687
Damjan Marionc47ed032017-01-25 14:18:03 +0100688 b = vlib_get_buffer (vm, bi);
Florin Coras2f9b0c02017-09-11 20:54:15 -0400689 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
Damjan Marionc47ed032017-01-25 14:18:03 +0100690 fl = vlib_buffer_get_buffer_free_list (vm, b, &fi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700691
Damjan Marionc47ed032017-01-25 14:18:03 +0100692 /* The only current use of this callback: multicast recycle */
693 if (PREDICT_FALSE (fl->buffers_added_to_freelist_function != 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100695 int j;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700696
Damjan Marionc47ed032017-01-25 14:18:03 +0100697 vlib_buffer_add_to_free_list
698 (vm, fl, buffers[i], (b->flags & VLIB_BUFFER_RECYCLE) == 0);
699
700 for (j = 0; j < vec_len (bm->announce_list); j++)
701 {
702 if (fl == bm->announce_list[j])
703 goto already_announced;
704 }
705 vec_add1 (bm->announce_list, fl);
706 already_announced:
707 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700708 }
709 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400710 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100711 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_RECYCLE) == 0))
712 {
713 u32 flags, next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714
Damjan Marionc47ed032017-01-25 14:18:03 +0100715 do
716 {
717 vlib_buffer_t *nb = vlib_get_buffer (vm, bi);
718 flags = nb->flags;
719 next = nb->next_buffer;
720 if (nb->n_add_refs)
721 nb->n_add_refs--;
722 else
723 {
724 vlib_buffer_validate_alloc_free (vm, &bi, 1,
725 VLIB_BUFFER_KNOWN_ALLOCATED);
726 vlib_buffer_add_to_free_list (vm, fl, bi, 1);
727 }
728 bi = next;
729 }
730 while (follow_buffer_next
731 && (flags & VLIB_BUFFER_NEXT_PRESENT));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700732
Damjan Marionc47ed032017-01-25 14:18:03 +0100733 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700734 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700735 }
Damjan Marionc47ed032017-01-25 14:18:03 +0100736 if (vec_len (bm->announce_list))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400738 vlib_buffer_free_list_t *fl;
Damjan Marionc47ed032017-01-25 14:18:03 +0100739 for (i = 0; i < vec_len (bm->announce_list); i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400740 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100741 fl = bm->announce_list[i];
Dave Barach9b8ffd92016-07-08 08:13:45 -0400742 fl->buffers_added_to_freelist_function (vm, fl);
743 }
Damjan Marionc47ed032017-01-25 14:18:03 +0100744 _vec_len (bm->announce_list) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745 }
746}
747
Damjan Marion878c6092017-01-04 13:19:27 +0100748static void
749vlib_buffer_free_internal (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400751 vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
752 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700753}
754
Damjan Marion878c6092017-01-04 13:19:27 +0100755static void
756vlib_buffer_free_no_next_internal (vlib_main_t * vm, u32 * buffers,
757 u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700758{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400759 vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
760 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700761}
762
763/* Copy template packet data into buffers as they are allocated. */
Damjan Marion878c6092017-01-04 13:19:27 +0100764static void __attribute__ ((unused))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700765vlib_packet_template_buffer_init (vlib_main_t * vm,
766 vlib_buffer_free_list_t * fl,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400767 u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400769 vlib_packet_template_t *t =
770 uword_to_pointer (fl->buffer_init_function_opaque,
771 vlib_packet_template_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772 uword i;
773
774 for (i = 0; i < n_buffers; i++)
775 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400776 vlib_buffer_t *b = vlib_get_buffer (vm, buffers[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777 ASSERT (b->current_length == vec_len (t->packet_data));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400778 clib_memcpy (vlib_buffer_get_current (b), t->packet_data,
779 b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780 }
781}
782
Dave Barach9b8ffd92016-07-08 08:13:45 -0400783void
784vlib_packet_template_init (vlib_main_t * vm,
785 vlib_packet_template_t * t,
786 void *packet_data,
787 uword n_packet_data_bytes,
788 uword min_n_buffers_each_physmem_alloc,
789 char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700790{
Damjan Marion878c6092017-01-04 13:19:27 +0100791 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100792 va_list va;
Chris Luke7447d072017-07-05 12:57:10 -0400793 u8 *name;
Damjan Marion878c6092017-01-04 13:19:27 +0100794 vlib_buffer_free_list_t *fl;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100795
796 va_start (va, fmt);
797 name = va_format (0, fmt, &va);
798 va_end (va);
799
Damjan Marion878c6092017-01-04 13:19:27 +0100800 if (bm->cb.vlib_packet_template_init_cb)
801 bm->cb.vlib_packet_template_init_cb (vm, (void *) t, packet_data,
802 n_packet_data_bytes,
803 min_n_buffers_each_physmem_alloc,
804 name);
805
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100806 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700807
808 memset (t, 0, sizeof (t[0]));
809
810 vec_add (t->packet_data, packet_data, n_packet_data_bytes);
811 t->min_n_buffers_each_physmem_alloc = min_n_buffers_each_physmem_alloc;
812
813 t->free_list_index = vlib_buffer_create_free_list_helper
814 (vm, n_packet_data_bytes,
815 /* is_public */ 1,
816 /* is_default */ 0,
817 name);
818
819 ASSERT (t->free_list_index != 0);
820 fl = vlib_buffer_get_free_list (vm, t->free_list_index);
821 fl->min_n_buffers_each_physmem_alloc = t->min_n_buffers_each_physmem_alloc;
822
823 fl->buffer_init_function = vlib_packet_template_buffer_init;
824 fl->buffer_init_function_opaque = pointer_to_uword (t);
825
826 fl->buffer_init_template.current_data = 0;
827 fl->buffer_init_template.current_length = n_packet_data_bytes;
828 fl->buffer_init_template.flags = 0;
Damjan Marionc47ed032017-01-25 14:18:03 +0100829 fl->buffer_init_template.n_add_refs = 0;
Damjan Marion878c6092017-01-04 13:19:27 +0100830 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700831}
832
833void *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400834vlib_packet_template_get_packet (vlib_main_t * vm,
835 vlib_packet_template_t * t, u32 * bi_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700836{
837 u32 bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400838 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700839
840 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
841 return 0;
842
843 *bi_result = bi;
844
845 b = vlib_get_buffer (vm, bi);
Damjan Marionf1213b82016-03-13 02:22:06 +0100846 clib_memcpy (vlib_buffer_get_current (b),
Dave Barach9b8ffd92016-07-08 08:13:45 -0400847 t->packet_data, vec_len (t->packet_data));
848 b->current_length = vec_len (t->packet_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700849
850 return b->data;
851}
852
Dave Barach9b8ffd92016-07-08 08:13:45 -0400853void
854vlib_packet_template_get_packet_helper (vlib_main_t * vm,
855 vlib_packet_template_t * t)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700856{
857 word n = t->min_n_buffers_each_physmem_alloc;
858 word l = vec_len (t->packet_data);
859 word n_alloc;
860
861 ASSERT (l > 0);
862 ASSERT (vec_len (t->free_buffers) == 0);
863
864 vec_validate (t->free_buffers, n - 1);
865 n_alloc = vlib_buffer_alloc_from_free_list (vm, t->free_buffers,
866 n, t->free_list_index);
867 _vec_len (t->free_buffers) = n_alloc;
868}
869
870/* Append given data to end of buffer, possibly allocating new buffers. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400871u32
872vlib_buffer_add_data (vlib_main_t * vm,
873 u32 free_list_index,
874 u32 buffer_index, void *data, u32 n_data_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700875{
876 u32 n_buffer_bytes, n_left, n_left_this_buffer, bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400877 vlib_buffer_t *b;
878 void *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700879
880 bi = buffer_index;
881 if (bi == 0
882 && 1 != vlib_buffer_alloc_from_free_list (vm, &bi, 1, free_list_index))
883 goto out_of_buffers;
884
885 d = data;
886 n_left = n_data_bytes;
887 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm, free_list_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400888
Ed Warnickecb9cada2015-12-08 15:45:58 -0700889 b = vlib_get_buffer (vm, bi);
890 b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
891
Dave Barach9b8ffd92016-07-08 08:13:45 -0400892 /* Get to the end of the chain before we try to append data... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700893 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
894 b = vlib_get_buffer (vm, b->next_buffer);
895
896 while (1)
897 {
898 u32 n;
899
900 ASSERT (n_buffer_bytes >= b->current_length);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400901 n_left_this_buffer =
902 n_buffer_bytes - (b->current_data + b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700903 n = clib_min (n_left_this_buffer, n_left);
Damjan Marionf1213b82016-03-13 02:22:06 +0100904 clib_memcpy (vlib_buffer_get_current (b) + b->current_length, d, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700905 b->current_length += n;
906 n_left -= n;
907 if (n_left == 0)
908 break;
909
910 d += n;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400911 if (1 !=
912 vlib_buffer_alloc_from_free_list (vm, &b->next_buffer, 1,
913 free_list_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700914 goto out_of_buffers;
915
916 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
917
918 b = vlib_get_buffer (vm, b->next_buffer);
919 }
920
921 return bi;
922
Dave Barach9b8ffd92016-07-08 08:13:45 -0400923out_of_buffers:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700924 clib_error ("out of buffers");
925 return bi;
926}
927
Pierre Pfister328e99b2016-02-12 13:18:42 +0000928u16
Dave Barach9b8ffd92016-07-08 08:13:45 -0400929vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
930 u32 free_list_index,
931 vlib_buffer_t * first,
932 vlib_buffer_t ** last,
933 void *data, u16 data_len)
934{
Pierre Pfister328e99b2016-02-12 13:18:42 +0000935 vlib_buffer_t *l = *last;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400936 u32 n_buffer_bytes =
937 vlib_buffer_free_list_buffer_size (vm, free_list_index);
Pierre Pfister328e99b2016-02-12 13:18:42 +0000938 u16 copied = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400939 ASSERT (n_buffer_bytes >= l->current_length + l->current_data);
940 while (data_len)
941 {
942 u16 max = n_buffer_bytes - l->current_length - l->current_data;
943 if (max == 0)
944 {
945 if (1 !=
946 vlib_buffer_alloc_from_free_list (vm, &l->next_buffer, 1,
947 free_list_index))
948 return copied;
949 *last = l = vlib_buffer_chain_buffer (vm, first, l, l->next_buffer);
950 max = n_buffer_bytes - l->current_length - l->current_data;
951 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000952
Dave Barach9b8ffd92016-07-08 08:13:45 -0400953 u16 len = (data_len > max) ? max : data_len;
954 clib_memcpy (vlib_buffer_get_current (l) + l->current_length,
955 data + copied, len);
956 vlib_buffer_chain_increase_length (first, l, len);
957 data_len -= len;
958 copied += len;
959 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000960 return copied;
961}
962
Damjan Marion04a7f052017-07-10 15:06:17 +0200963void
964vlib_buffer_add_mem_range (vlib_main_t * vm, uword start, uword size)
965{
966 vlib_buffer_main_t *bm = vm->buffer_main;
967
968 if (bm->buffer_mem_size == 0)
969 {
970 bm->buffer_mem_start = start;
971 bm->buffer_mem_size = size;
972 }
973 else if (start < bm->buffer_mem_start)
974 {
975 bm->buffer_mem_size += bm->buffer_mem_start - start;
976 bm->buffer_mem_start = start;
977 if (size > bm->buffer_mem_size)
978 bm->buffer_mem_size = size;
979 }
980 else if (start > bm->buffer_mem_start)
981 {
982 uword new_size = start - bm->buffer_mem_start + size;
983 if (new_size > bm->buffer_mem_size)
984 bm->buffer_mem_size = new_size;
985 }
986
987 if ((u64) bm->buffer_mem_size >
988 ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES)))
989 {
990 clib_panic ("buffer memory size out of range!");
991 }
992}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700993
Dave Barach9b8ffd92016-07-08 08:13:45 -0400994static u8 *
995format_vlib_buffer_free_list (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700996{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400997 vlib_buffer_free_list_t *f = va_arg (*va, vlib_buffer_free_list_t *);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100998 u32 threadnum = va_arg (*va, u32);
999 uword bytes_alloc, bytes_free, n_free, size;
1000
1001 if (!f)
1002 return format (s, "%=7s%=30s%=12s%=12s%=12s%=12s%=12s%=12s",
1003 "Thread", "Name", "Index", "Size", "Alloc", "Free",
1004 "#Alloc", "#Free");
1005
1006 size = sizeof (vlib_buffer_t) + f->n_data_bytes;
Damjan Marionbd69a5f2017-02-05 23:44:42 +01001007 n_free = vec_len (f->buffers);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001008 bytes_alloc = size * f->n_alloc;
1009 bytes_free = size * n_free;
1010
Chris Luke7447d072017-07-05 12:57:10 -04001011 s = format (s, "%7d%30v%12d%12d%=12U%=12U%=12d%=12d", threadnum,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001012 f->name, f->index, f->n_data_bytes,
1013 format_memory_size, bytes_alloc,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001014 format_memory_size, bytes_free, f->n_alloc, n_free);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001015
1016 return s;
1017}
1018
1019static clib_error_t *
1020show_buffers (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001021 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001022{
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001023 vlib_buffer_main_t *bm;
1024 vlib_buffer_free_list_t *f;
1025 vlib_main_t *curr_vm;
1026 u32 vm_index = 0;
1027
1028 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, 0, 0);
1029
1030 do
1031 {
Dave Barach80f54e22017-03-08 19:08:56 -05001032 curr_vm = vlib_mains[vm_index];
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001033 bm = curr_vm->buffer_main;
1034
1035 /* *INDENT-OFF* */
1036 pool_foreach (f, bm->buffer_free_list_pool, ({
1037 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, f, vm_index);
1038 }));
1039 /* *INDENT-ON* */
1040
1041 vm_index++;
1042 }
1043 while (vm_index < vec_len (vlib_mains));
1044
Ed Warnickecb9cada2015-12-08 15:45:58 -07001045 return 0;
1046}
1047
Dave Barach9b8ffd92016-07-08 08:13:45 -04001048/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001049VLIB_CLI_COMMAND (show_buffers_command, static) = {
1050 .path = "show buffers",
1051 .short_help = "Show packet buffer allocation",
1052 .function = show_buffers,
1053};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001054/* *INDENT-ON* */
1055
Damjan Marion49d66f12017-07-20 18:10:35 +02001056clib_error_t *
1057vlib_buffer_main_init (struct vlib_main_t * vm)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001058{
Damjan Marion49d66f12017-07-20 18:10:35 +02001059 vlib_buffer_main_t *bm;
1060 clib_error_t *error;
1061
1062 vec_validate (vm->buffer_main, 0);
1063 bm = vm->buffer_main;
1064
1065 if (vlib_buffer_callbacks)
1066 {
1067 /* external plugin has registered own buffer callbacks
1068 so we just copy them and quit */
1069 vlib_buffer_main_t *bm = vm->buffer_main;
1070 clib_memcpy (&bm->cb, vlib_buffer_callbacks,
1071 sizeof (vlib_buffer_callbacks_t));
1072 bm->callbacks_registered = 1;
1073 return 0;
1074 }
Damjan Marion04a7f052017-07-10 15:06:17 +02001075
Damjan Marion878c6092017-01-04 13:19:27 +01001076 bm->cb.vlib_buffer_alloc_cb = &vlib_buffer_alloc_internal;
1077 bm->cb.vlib_buffer_alloc_from_free_list_cb =
1078 &vlib_buffer_alloc_from_free_list_internal;
1079 bm->cb.vlib_buffer_free_cb = &vlib_buffer_free_internal;
1080 bm->cb.vlib_buffer_free_no_next_cb = &vlib_buffer_free_no_next_internal;
1081 bm->cb.vlib_buffer_delete_free_list_cb =
1082 &vlib_buffer_delete_free_list_internal;
Damjan Marion6b0f5892017-07-27 04:01:24 -04001083 clib_spinlock_init (&bm->buffer_known_hash_lockp);
Damjan Marion49d66f12017-07-20 18:10:35 +02001084
1085 /* allocate default region */
1086 error = vlib_physmem_region_alloc (vm, "buffers",
1087 vlib_buffer_physmem_sz, 0,
1088 VLIB_PHYSMEM_F_INIT_MHEAP |
1089 VLIB_PHYSMEM_F_HAVE_BUFFERS,
1090 &bm->physmem_region);
1091
1092 if (error == 0)
1093 return 0;
1094
1095 clib_error_free (error);
1096
1097 /* we my be running unpriviledged, so try to allocate fake physmem */
1098 error = vlib_physmem_region_alloc (vm, "buffers (fake)",
1099 vlib_buffer_physmem_sz, 0,
1100 VLIB_PHYSMEM_F_FAKE |
1101 VLIB_PHYSMEM_F_INIT_MHEAP |
1102 VLIB_PHYSMEM_F_HAVE_BUFFERS,
1103 &bm->physmem_region);
1104 return error;
Damjan Marion878c6092017-01-04 13:19:27 +01001105}
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001106
Damjan Marion49d66f12017-07-20 18:10:35 +02001107static clib_error_t *
1108vlib_buffers_configure (vlib_main_t * vm, unformat_input_t * input)
1109{
1110 u32 size_in_mb;
1111
1112 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1113 {
1114 if (unformat (input, "memory-size-in-mb %d", &size_in_mb))
1115 vlib_buffer_physmem_sz = size_in_mb << 20;
1116 else
1117 return unformat_parse_error (input);
1118 }
1119
1120 unformat_free (input);
1121 return 0;
1122}
1123
1124VLIB_EARLY_CONFIG_FUNCTION (vlib_buffers_configure, "buffers");
1125
1126
Chris Luked4024f52016-09-06 09:32:36 -04001127/** @endcond */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001128/*
1129 * fd.io coding-style-patch-verification: ON
1130 *
1131 * Local Variables:
1132 * eval: (c-set-style "gnu")
1133 * End:
1134 */