blob: a5c955c76e66551ae09a93366d57482247ff1255 [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;
50
Dave Barach9b8ffd92016-07-08 08:13:45 -040051uword
52vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
53 vlib_buffer_t * b_first)
Ed Warnickecb9cada2015-12-08 15:45:58 -070054{
Dave Barach9b8ffd92016-07-08 08:13:45 -040055 vlib_buffer_t *b = b_first;
Ed Warnickecb9cada2015-12-08 15:45:58 -070056 uword l_first = b_first->current_length;
57 uword l = 0;
58 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
59 {
60 b = vlib_get_buffer (vm, b->next_buffer);
61 l += b->current_length;
62 }
63 b_first->total_length_not_including_first_buffer = l;
64 b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
65 return l + l_first;
66}
67
Dave Barach9b8ffd92016-07-08 08:13:45 -040068u8 *
69format_vlib_buffer (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070070{
Dave Barach9b8ffd92016-07-08 08:13:45 -040071 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010072 uword indent = format_get_indent (s);
73
Damjan Marionc47ed032017-01-25 14:18:03 +010074 s = format (s, "current data %d, length %d, free-list %d, clone-count %u",
Damjan Marion072401e2017-07-13 18:53:27 +020075 b->current_data, b->current_length,
76 vlib_buffer_get_free_list_index (b), b->n_add_refs);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010077
78 if (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID)
79 s = format (s, ", totlen-nifb %d",
80 b->total_length_not_including_first_buffer);
81
82 if (b->flags & VLIB_BUFFER_IS_TRACED)
83 s = format (s, ", trace 0x%x", b->trace_index);
84
85 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
86 {
87 vlib_main_t *vm = vlib_get_main ();
88 u32 next_buffer = b->next_buffer;
89 b = vlib_get_buffer (vm, next_buffer);
90
Damjan Marionc47ed032017-01-25 14:18:03 +010091 s =
92 format (s, "\n%Unext-buffer 0x%x, segment length %d, clone-count %u",
93 format_white_space, indent, next_buffer, b->current_length,
94 b->n_add_refs);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010095 }
96
Ed Warnickecb9cada2015-12-08 15:45:58 -070097 return s;
98}
99
Dave Barach9b8ffd92016-07-08 08:13:45 -0400100u8 *
101format_vlib_buffer_and_data (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400103 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
104
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105 s = format (s, "%U, %U",
106 format_vlib_buffer, b,
107 format_hex_bytes, vlib_buffer_get_current (b), 64);
108
109 return s;
110}
111
Dave Barach9b8ffd92016-07-08 08:13:45 -0400112static u8 *
113format_vlib_buffer_known_state (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114{
115 vlib_buffer_known_state_t state = va_arg (*args, vlib_buffer_known_state_t);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400116 char *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117
118 switch (state)
119 {
120 case VLIB_BUFFER_UNKNOWN:
121 t = "unknown";
122 break;
123
124 case VLIB_BUFFER_KNOWN_ALLOCATED:
125 t = "known-allocated";
126 break;
127
128 case VLIB_BUFFER_KNOWN_FREE:
129 t = "known-free";
130 break;
131
132 default:
133 t = "invalid";
134 break;
135 }
136
137 return format (s, "%s", t);
138}
139
Dave Barach9b8ffd92016-07-08 08:13:45 -0400140u8 *
141format_vlib_buffer_contents (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400143 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
144 vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *);
145
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146 while (1)
147 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400148 vec_add (s, vlib_buffer_get_current (b), b->current_length);
149 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150 break;
151 b = vlib_get_buffer (vm, b->next_buffer);
152 }
153
154 return s;
155}
156
157static u8 *
158vlib_validate_buffer_helper (vlib_main_t * vm,
159 u32 bi,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400160 uword follow_buffer_next, uword ** unique_hash)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400162 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
163 vlib_buffer_main_t *bm = vm->buffer_main;
164 vlib_buffer_free_list_t *fl;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165
Damjan Marion072401e2017-07-13 18:53:27 +0200166 if (pool_is_free_index
167 (bm->buffer_free_list_pool, vlib_buffer_get_free_list_index (b)))
168 return format (0, "unknown free list 0x%x",
169 vlib_buffer_get_free_list_index (b));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170
Damjan Marion072401e2017-07-13 18:53:27 +0200171 fl =
172 pool_elt_at_index (bm->buffer_free_list_pool,
173 vlib_buffer_get_free_list_index (b));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174
Dave Barach9b8ffd92016-07-08 08:13:45 -0400175 if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176 return format (0, "current data %d before pre-data", b->current_data);
Damjan Marion878c6092017-01-04 13:19:27 +0100177
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178 if (b->current_data + b->current_length > fl->n_data_bytes)
179 return format (0, "%d-%d beyond end of buffer %d",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400180 b->current_data, b->current_length, fl->n_data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181
Dave Barach9b8ffd92016-07-08 08:13:45 -0400182 if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183 {
184 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400185 u8 *msg, *result;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186
187 k = vlib_buffer_is_known (vm, b->next_buffer);
188 if (k != VLIB_BUFFER_KNOWN_ALLOCATED)
189 return format (0, "next 0x%x: %U",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400190 b->next_buffer, format_vlib_buffer_known_state, k);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191
192 if (unique_hash)
193 {
194 if (hash_get (*unique_hash, b->next_buffer))
195 return format (0, "duplicate buffer 0x%x", b->next_buffer);
196
197 hash_set1 (*unique_hash, b->next_buffer);
198 }
199
200 msg = vlib_validate_buffer (vm, b->next_buffer, follow_buffer_next);
201 if (msg)
202 {
203 result = format (0, "next 0x%x: %v", b->next_buffer, msg);
204 vec_free (msg);
205 return result;
206 }
207 }
208
209 return 0;
210}
211
212u8 *
213vlib_validate_buffer (vlib_main_t * vm, u32 bi, uword follow_buffer_next)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400214{
215 return vlib_validate_buffer_helper (vm, bi, follow_buffer_next,
216 /* unique_hash */ 0);
217}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218
219u8 *
220vlib_validate_buffers (vlib_main_t * vm,
221 u32 * buffers,
222 uword next_buffer_stride,
223 uword n_buffers,
224 vlib_buffer_known_state_t known_state,
225 uword follow_buffer_next)
226{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400227 uword i, *hash;
228 u32 bi, *b = buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400230 u8 *msg = 0, *result = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231
232 hash = hash_create (0, 0);
233 for (i = 0; i < n_buffers; i++)
234 {
235 bi = b[0];
236 b += next_buffer_stride;
237
238 /* Buffer is not unique. */
239 if (hash_get (hash, bi))
240 {
241 msg = format (0, "not unique");
242 goto done;
243 }
244
245 k = vlib_buffer_is_known (vm, bi);
246 if (k != known_state)
247 {
248 msg = format (0, "is %U; expected %U",
249 format_vlib_buffer_known_state, k,
250 format_vlib_buffer_known_state, known_state);
251 goto done;
252 }
253
254 msg = vlib_validate_buffer_helper (vm, bi, follow_buffer_next, &hash);
255 if (msg)
256 goto done;
257
258 hash_set1 (hash, bi);
259 }
260
Dave Barach9b8ffd92016-07-08 08:13:45 -0400261done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262 if (msg)
263 {
264 result = format (0, "0x%x: %v", bi, msg);
265 vec_free (msg);
266 }
267 hash_free (hash);
268 return result;
269}
270
Dave Barach80f54e22017-03-08 19:08:56 -0500271/*
272 * Hand-craft a static vector w/ length 1, so vec_len(vlib_mains) =1
273 * and vlib_mains[0] = &vlib_global_main from the beginning of time.
274 *
275 * The only place which should ever expand vlib_mains is start_workers()
276 * in threads.c. It knows about the bootstrap vector.
277 */
278/* *INDENT-OFF* */
279static struct
280{
281 vec_header_t h;
282 vlib_main_t *vm;
283} __attribute__ ((packed)) __bootstrap_vlib_main_vector
284 __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES))) =
285{
286 .h.len = 1,
287 .vm = &vlib_global_main,
288};
289/* *INDENT-ON* */
290
291vlib_main_t **vlib_mains = &__bootstrap_vlib_main_vector.vm;
292
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293
294/* When dubugging validate that given buffers are either known allocated
295 or known free. */
Damjan Marionc47ed032017-01-25 14:18:03 +0100296static void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297vlib_buffer_validate_alloc_free (vlib_main_t * vm,
298 u32 * buffers,
299 uword n_buffers,
300 vlib_buffer_known_state_t expected_state)
301{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400302 u32 *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303 uword i, bi, is_free;
304
305 if (CLIB_DEBUG == 0)
306 return;
307
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308 is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED;
309 b = buffers;
310 for (i = 0; i < n_buffers; i++)
311 {
312 vlib_buffer_known_state_t known;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400313
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314 bi = b[0];
315 b += 1;
316 known = vlib_buffer_is_known (vm, bi);
317 if (known != expected_state)
318 {
319 ASSERT (0);
320 vlib_panic_with_msg
321 (vm, "%s %U buffer 0x%x",
322 is_free ? "freeing" : "allocating",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400323 format_vlib_buffer_known_state, known, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324 }
325
326 vlib_buffer_set_known_state
327 (vm, bi,
328 is_free ? VLIB_BUFFER_KNOWN_FREE : VLIB_BUFFER_KNOWN_ALLOCATED);
329 }
330}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331
Damjan Marion8a6a3b22017-01-17 14:12:42 +0100332void
333vlib_buffer_merge_free_lists (vlib_buffer_free_list_t * dst,
334 vlib_buffer_free_list_t * src)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335{
336 uword l;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400337 u32 *d;
338
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100339 l = vec_len (src->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340 if (l > 0)
341 {
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100342 vec_add2_aligned (dst->buffers, d, l, CLIB_CACHE_LINE_BYTES);
343 clib_memcpy (d, src->buffers, l * sizeof (d[0]));
344 vec_free (src->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345 }
346}
347
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348/* Add buffer free list. */
349static u32
350vlib_buffer_create_free_list_helper (vlib_main_t * vm,
351 u32 n_data_bytes,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400352 u32 is_public, u32 is_default, u8 * name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400354 vlib_buffer_main_t *bm = vm->buffer_main;
355 vlib_buffer_free_list_t *f;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100356 int i;
357
Damjan Marion586afd72017-04-05 19:18:20 +0200358 ASSERT (vlib_get_thread_index () == 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100359
360 if (!is_default && pool_elts (bm->buffer_free_list_pool) == 0)
361 {
362 u32 default_free_free_list_index;
363
364 /* *INDENT-OFF* */
365 default_free_free_list_index =
366 vlib_buffer_create_free_list_helper
367 (vm,
368 /* default buffer size */ VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES,
369 /* is_public */ 1,
370 /* is_default */ 1,
371 (u8 *) "default");
372 /* *INDENT-ON* */
373 ASSERT (default_free_free_list_index ==
374 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
375
376 if (n_data_bytes == VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES && is_public)
377 return default_free_free_list_index;
378 }
379
380 pool_get_aligned (bm->buffer_free_list_pool, f, CLIB_CACHE_LINE_BYTES);
381
382 memset (f, 0, sizeof (f[0]));
383 f->index = f - bm->buffer_free_list_pool;
384 f->n_data_bytes = vlib_buffer_round_size (n_data_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500385 f->min_n_buffers_each_physmem_alloc = VLIB_FRAME_SIZE;
Chris Luke7447d072017-07-05 12:57:10 -0400386 f->name = clib_mem_is_vec (name) ? name : format (0, "%s", name);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100387
388 /* Setup free buffer template. */
Damjan Marion072401e2017-07-13 18:53:27 +0200389 vlib_buffer_set_free_list_index (&f->buffer_init_template, f->index);
Damjan Marionc47ed032017-01-25 14:18:03 +0100390 f->buffer_init_template.n_add_refs = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100391
392 if (is_public)
393 {
394 uword *p = hash_get (bm->free_list_by_size, f->n_data_bytes);
395 if (!p)
396 hash_set (bm->free_list_by_size, f->n_data_bytes, f->index);
397 }
398
399 for (i = 1; i < vec_len (vlib_mains); i++)
400 {
401 vlib_buffer_main_t *wbm = vlib_mains[i]->buffer_main;
402 vlib_buffer_free_list_t *wf;
403 pool_get_aligned (wbm->buffer_free_list_pool,
404 wf, CLIB_CACHE_LINE_BYTES);
405 ASSERT (f - bm->buffer_free_list_pool ==
406 wf - wbm->buffer_free_list_pool);
407 wf[0] = f[0];
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100408 wf->buffers = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100409 wf->n_alloc = 0;
410 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411
412 return f->index;
413}
414
Dave Barach9b8ffd92016-07-08 08:13:45 -0400415u32
416vlib_buffer_create_free_list (vlib_main_t * vm, u32 n_data_bytes,
417 char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418{
419 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400420 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421
422 va_start (va, fmt);
423 name = va_format (0, fmt, &va);
424 va_end (va);
425
426 return vlib_buffer_create_free_list_helper (vm, n_data_bytes,
427 /* is_public */ 0,
428 /* is_default */ 0,
429 name);
430}
431
Dave Barach9b8ffd92016-07-08 08:13:45 -0400432u32
433vlib_buffer_get_or_create_free_list (vlib_main_t * vm, u32 n_data_bytes,
434 char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435{
436 u32 i = vlib_buffer_get_free_list_with_size (vm, n_data_bytes);
437
438 if (i == ~0)
439 {
440 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400441 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442
443 va_start (va, fmt);
444 name = va_format (0, fmt, &va);
445 va_end (va);
446
447 i = vlib_buffer_create_free_list_helper (vm, n_data_bytes,
448 /* is_public */ 1,
449 /* is_default */ 0,
450 name);
451 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400452
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453 return i;
454}
455
456static void
457del_free_list (vlib_main_t * vm, vlib_buffer_free_list_t * f)
458{
459 u32 i;
460
461 for (i = 0; i < vec_len (f->buffer_memory_allocated); i++)
462 vm->os_physmem_free (f->buffer_memory_allocated[i]);
463 vec_free (f->name);
464 vec_free (f->buffer_memory_allocated);
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100465 vec_free (f->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466}
467
468/* Add buffer free list. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400469void
Damjan Marion878c6092017-01-04 13:19:27 +0100470vlib_buffer_delete_free_list_internal (vlib_main_t * vm, u32 free_list_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400472 vlib_buffer_main_t *bm = vm->buffer_main;
473 vlib_buffer_free_list_t *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700474 u32 merge_index;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100475 int i;
476
Damjan Marion586afd72017-04-05 19:18:20 +0200477 ASSERT (vlib_get_thread_index () == 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100478
479 f = vlib_buffer_get_free_list (vm, free_list_index);
480
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100481 ASSERT (vec_len (f->buffers) == f->n_alloc);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100482 merge_index = vlib_buffer_get_free_list_with_size (vm, f->n_data_bytes);
483 if (merge_index != ~0 && merge_index != free_list_index)
484 {
Damjan Marion8a6a3b22017-01-17 14:12:42 +0100485 vlib_buffer_merge_free_lists (pool_elt_at_index
486 (bm->buffer_free_list_pool, merge_index),
487 f);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100488 }
489
490 del_free_list (vm, f);
491
492 /* Poison it. */
493 memset (f, 0xab, sizeof (f[0]));
494
495 pool_put (bm->buffer_free_list_pool, f);
496
497 for (i = 1; i < vec_len (vlib_mains); i++)
498 {
499 bm = vlib_mains[i]->buffer_main;
500 f = vlib_buffer_get_free_list (vlib_mains[i], free_list_index);;
501 memset (f, 0xab, sizeof (f[0]));
502 pool_put (bm->buffer_free_list_pool, f);
503 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504}
505
506/* Make sure free list has at least given number of free buffers. */
507static uword
508fill_free_list (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400509 vlib_buffer_free_list_t * fl, uword min_free_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400511 vlib_buffer_t *buffers, *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700512 int n, n_bytes, i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400513 u32 *bi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514 u32 n_remaining, n_alloc, n_this_chunk;
515
Ed Warnickecb9cada2015-12-08 15:45:58 -0700516 /* Already have enough free buffers on free list? */
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100517 n = min_free_buffers - vec_len (fl->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518 if (n <= 0)
519 return min_free_buffers;
520
521 /* Always allocate round number of buffers. */
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100522 n = round_pow2 (n, CLIB_CACHE_LINE_BYTES / sizeof (u32));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523
524 /* Always allocate new buffers in reasonably large sized chunks. */
525 n = clib_max (n, fl->min_n_buffers_each_physmem_alloc);
526
527 n_remaining = n;
528 n_alloc = 0;
529 while (n_remaining > 0)
530 {
531 n_this_chunk = clib_min (n_remaining, 16);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400532
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533 n_bytes = n_this_chunk * (sizeof (b[0]) + fl->n_data_bytes);
534
535 /* drb: removed power-of-2 ASSERT */
536 buffers = vm->os_physmem_alloc_aligned (&vm->physmem_main,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400537 n_bytes,
538 sizeof (vlib_buffer_t));
539 if (!buffers)
540 return n_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541
542 /* Record chunk as being allocated so we can free it later. */
543 vec_add1 (fl->buffer_memory_allocated, buffers);
544
545 fl->n_alloc += n_this_chunk;
546 n_alloc += n_this_chunk;
547 n_remaining -= n_this_chunk;
548
549 b = buffers;
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100550 vec_add2_aligned (fl->buffers, bi, n_this_chunk, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700551 for (i = 0; i < n_this_chunk; i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400552 {
553 bi[i] = vlib_get_buffer_index (vm, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554
Dave Barach9b8ffd92016-07-08 08:13:45 -0400555 if (CLIB_DEBUG > 0)
556 vlib_buffer_set_known_state (vm, bi[i], VLIB_BUFFER_KNOWN_FREE);
557 b = vlib_buffer_next_contiguous (b, fl->n_data_bytes);
558 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700559
560 memset (buffers, 0, n_bytes);
561
562 /* Initialize all new buffers. */
563 b = buffers;
564 for (i = 0; i < n_this_chunk; i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400565 {
566 vlib_buffer_init_for_free_list (b, fl);
567 b = vlib_buffer_next_contiguous (b, fl->n_data_bytes);
568 }
569
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570 if (fl->buffer_init_function)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400571 fl->buffer_init_function (vm, fl, bi, n_this_chunk);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572 }
573 return n_alloc;
574}
575
Ed Warnickecb9cada2015-12-08 15:45:58 -0700576static u32
577alloc_from_free_list (vlib_main_t * vm,
578 vlib_buffer_free_list_t * free_list,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400579 u32 * alloc_buffers, u32 n_alloc_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580{
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100581 u32 *dst, *src;
582 uword len;
583 uword n_filled;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700584
Ed Warnickecb9cada2015-12-08 15:45:58 -0700585 dst = alloc_buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700586
587 n_filled = fill_free_list (vm, free_list, n_alloc_buffers);
588 if (n_filled == 0)
589 return 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400590
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100591 len = vec_len (free_list->buffers);
592 ASSERT (len >= n_alloc_buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700593
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100594 src = free_list->buffers + len - n_alloc_buffers;
595 clib_memcpy (dst, src, n_alloc_buffers * sizeof (u32));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700596
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100597 _vec_len (free_list->buffers) -= n_alloc_buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700598
599 /* Verify that buffers are known free. */
600 vlib_buffer_validate_alloc_free (vm, alloc_buffers,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400601 n_alloc_buffers, VLIB_BUFFER_KNOWN_FREE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700602
603 return n_alloc_buffers;
604}
605
Damjan Marion878c6092017-01-04 13:19:27 +0100606
Ed Warnickecb9cada2015-12-08 15:45:58 -0700607/* Allocate a given number of buffers into given array.
608 Returns number actually allocated which will be either zero or
609 number requested. */
Damjan Marion878c6092017-01-04 13:19:27 +0100610static u32
611vlib_buffer_alloc_internal (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700612{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400613 vlib_buffer_main_t *bm = vm->buffer_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700614
615 return alloc_from_free_list
616 (vm,
617 pool_elt_at_index (bm->buffer_free_list_pool,
618 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX),
619 buffers, n_buffers);
620}
621
Damjan Marion878c6092017-01-04 13:19:27 +0100622static u32
623vlib_buffer_alloc_from_free_list_internal (vlib_main_t * vm,
624 u32 * buffers,
625 u32 n_buffers, u32 free_list_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400627 vlib_buffer_main_t *bm = vm->buffer_main;
628 vlib_buffer_free_list_t *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629 f = pool_elt_at_index (bm->buffer_free_list_pool, free_list_index);
630 return alloc_from_free_list (vm, f, buffers, n_buffers);
631}
632
Dave Barach9b8ffd92016-07-08 08:13:45 -0400633void *
634vlib_set_buffer_free_callback (vlib_main_t * vm, void *fp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400636 vlib_buffer_main_t *bm = vm->buffer_main;
637 void *rv = bm->buffer_free_callback;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700638
639 bm->buffer_free_callback = fp;
640 return rv;
641}
642
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643static_always_inline void
644vlib_buffer_free_inline (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400645 u32 * buffers, u32 n_buffers, u32 follow_buffer_next)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700646{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400647 vlib_buffer_main_t *bm = vm->buffer_main;
648 vlib_buffer_free_list_t *fl;
Damjan Marionc47ed032017-01-25 14:18:03 +0100649 u32 fi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650 int i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400651 u32 (*cb) (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
652 u32 follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700653
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654 cb = bm->buffer_free_callback;
655
656 if (PREDICT_FALSE (cb != 0))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400657 n_buffers = (*cb) (vm, buffers, n_buffers, follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700658
Dave Barach9b8ffd92016-07-08 08:13:45 -0400659 if (!n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700660 return;
661
Damjan Marionc47ed032017-01-25 14:18:03 +0100662 for (i = 0; i < n_buffers; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700663 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100664 vlib_buffer_t *b;
665 u32 bi = buffers[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700666
Damjan Marionc47ed032017-01-25 14:18:03 +0100667 b = vlib_get_buffer (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668
Damjan Marionc47ed032017-01-25 14:18:03 +0100669 fl = vlib_buffer_get_buffer_free_list (vm, b, &fi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700670
Damjan Marionc47ed032017-01-25 14:18:03 +0100671 /* The only current use of this callback: multicast recycle */
672 if (PREDICT_FALSE (fl->buffers_added_to_freelist_function != 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100674 int j;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700675
Damjan Marionc47ed032017-01-25 14:18:03 +0100676 vlib_buffer_add_to_free_list
677 (vm, fl, buffers[i], (b->flags & VLIB_BUFFER_RECYCLE) == 0);
678
679 for (j = 0; j < vec_len (bm->announce_list); j++)
680 {
681 if (fl == bm->announce_list[j])
682 goto already_announced;
683 }
684 vec_add1 (bm->announce_list, fl);
685 already_announced:
686 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700687 }
688 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400689 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100690 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_RECYCLE) == 0))
691 {
692 u32 flags, next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693
Damjan Marionc47ed032017-01-25 14:18:03 +0100694 do
695 {
696 vlib_buffer_t *nb = vlib_get_buffer (vm, bi);
697 flags = nb->flags;
698 next = nb->next_buffer;
699 if (nb->n_add_refs)
700 nb->n_add_refs--;
701 else
702 {
703 vlib_buffer_validate_alloc_free (vm, &bi, 1,
704 VLIB_BUFFER_KNOWN_ALLOCATED);
705 vlib_buffer_add_to_free_list (vm, fl, bi, 1);
706 }
707 bi = next;
708 }
709 while (follow_buffer_next
710 && (flags & VLIB_BUFFER_NEXT_PRESENT));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711
Damjan Marionc47ed032017-01-25 14:18:03 +0100712 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714 }
Damjan Marionc47ed032017-01-25 14:18:03 +0100715 if (vec_len (bm->announce_list))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700716 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400717 vlib_buffer_free_list_t *fl;
Damjan Marionc47ed032017-01-25 14:18:03 +0100718 for (i = 0; i < vec_len (bm->announce_list); i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400719 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100720 fl = bm->announce_list[i];
Dave Barach9b8ffd92016-07-08 08:13:45 -0400721 fl->buffers_added_to_freelist_function (vm, fl);
722 }
Damjan Marionc47ed032017-01-25 14:18:03 +0100723 _vec_len (bm->announce_list) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700724 }
725}
726
Damjan Marion878c6092017-01-04 13:19:27 +0100727static void
728vlib_buffer_free_internal (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700729{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400730 vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
731 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700732}
733
Damjan Marion878c6092017-01-04 13:19:27 +0100734static void
735vlib_buffer_free_no_next_internal (vlib_main_t * vm, u32 * buffers,
736 u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400738 vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
739 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700740}
741
742/* Copy template packet data into buffers as they are allocated. */
Damjan Marion878c6092017-01-04 13:19:27 +0100743static void __attribute__ ((unused))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700744vlib_packet_template_buffer_init (vlib_main_t * vm,
745 vlib_buffer_free_list_t * fl,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400746 u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700747{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400748 vlib_packet_template_t *t =
749 uword_to_pointer (fl->buffer_init_function_opaque,
750 vlib_packet_template_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751 uword i;
752
753 for (i = 0; i < n_buffers; i++)
754 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400755 vlib_buffer_t *b = vlib_get_buffer (vm, buffers[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700756 ASSERT (b->current_length == vec_len (t->packet_data));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400757 clib_memcpy (vlib_buffer_get_current (b), t->packet_data,
758 b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700759 }
760}
761
Dave Barach9b8ffd92016-07-08 08:13:45 -0400762void
763vlib_packet_template_init (vlib_main_t * vm,
764 vlib_packet_template_t * t,
765 void *packet_data,
766 uword n_packet_data_bytes,
767 uword min_n_buffers_each_physmem_alloc,
768 char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769{
Damjan Marion878c6092017-01-04 13:19:27 +0100770 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100771 va_list va;
Chris Luke7447d072017-07-05 12:57:10 -0400772 u8 *name;
Damjan Marion878c6092017-01-04 13:19:27 +0100773 vlib_buffer_free_list_t *fl;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100774
775 va_start (va, fmt);
776 name = va_format (0, fmt, &va);
777 va_end (va);
778
Damjan Marion878c6092017-01-04 13:19:27 +0100779 if (bm->cb.vlib_packet_template_init_cb)
780 bm->cb.vlib_packet_template_init_cb (vm, (void *) t, packet_data,
781 n_packet_data_bytes,
782 min_n_buffers_each_physmem_alloc,
783 name);
784
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100785 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700786
787 memset (t, 0, sizeof (t[0]));
788
789 vec_add (t->packet_data, packet_data, n_packet_data_bytes);
790 t->min_n_buffers_each_physmem_alloc = min_n_buffers_each_physmem_alloc;
791
792 t->free_list_index = vlib_buffer_create_free_list_helper
793 (vm, n_packet_data_bytes,
794 /* is_public */ 1,
795 /* is_default */ 0,
796 name);
797
798 ASSERT (t->free_list_index != 0);
799 fl = vlib_buffer_get_free_list (vm, t->free_list_index);
800 fl->min_n_buffers_each_physmem_alloc = t->min_n_buffers_each_physmem_alloc;
801
802 fl->buffer_init_function = vlib_packet_template_buffer_init;
803 fl->buffer_init_function_opaque = pointer_to_uword (t);
804
805 fl->buffer_init_template.current_data = 0;
806 fl->buffer_init_template.current_length = n_packet_data_bytes;
807 fl->buffer_init_template.flags = 0;
Damjan Marionc47ed032017-01-25 14:18:03 +0100808 fl->buffer_init_template.n_add_refs = 0;
Damjan Marion878c6092017-01-04 13:19:27 +0100809 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700810}
811
812void *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400813vlib_packet_template_get_packet (vlib_main_t * vm,
814 vlib_packet_template_t * t, u32 * bi_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700815{
816 u32 bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400817 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700818
819 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
820 return 0;
821
822 *bi_result = bi;
823
824 b = vlib_get_buffer (vm, bi);
Damjan Marionf1213b82016-03-13 02:22:06 +0100825 clib_memcpy (vlib_buffer_get_current (b),
Dave Barach9b8ffd92016-07-08 08:13:45 -0400826 t->packet_data, vec_len (t->packet_data));
827 b->current_length = vec_len (t->packet_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700828
829 return b->data;
830}
831
Dave Barach9b8ffd92016-07-08 08:13:45 -0400832void
833vlib_packet_template_get_packet_helper (vlib_main_t * vm,
834 vlib_packet_template_t * t)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700835{
836 word n = t->min_n_buffers_each_physmem_alloc;
837 word l = vec_len (t->packet_data);
838 word n_alloc;
839
840 ASSERT (l > 0);
841 ASSERT (vec_len (t->free_buffers) == 0);
842
843 vec_validate (t->free_buffers, n - 1);
844 n_alloc = vlib_buffer_alloc_from_free_list (vm, t->free_buffers,
845 n, t->free_list_index);
846 _vec_len (t->free_buffers) = n_alloc;
847}
848
849/* Append given data to end of buffer, possibly allocating new buffers. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400850u32
851vlib_buffer_add_data (vlib_main_t * vm,
852 u32 free_list_index,
853 u32 buffer_index, void *data, u32 n_data_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700854{
855 u32 n_buffer_bytes, n_left, n_left_this_buffer, bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400856 vlib_buffer_t *b;
857 void *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700858
859 bi = buffer_index;
860 if (bi == 0
861 && 1 != vlib_buffer_alloc_from_free_list (vm, &bi, 1, free_list_index))
862 goto out_of_buffers;
863
864 d = data;
865 n_left = n_data_bytes;
866 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm, free_list_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400867
Ed Warnickecb9cada2015-12-08 15:45:58 -0700868 b = vlib_get_buffer (vm, bi);
869 b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
870
Dave Barach9b8ffd92016-07-08 08:13:45 -0400871 /* Get to the end of the chain before we try to append data... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700872 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
873 b = vlib_get_buffer (vm, b->next_buffer);
874
875 while (1)
876 {
877 u32 n;
878
879 ASSERT (n_buffer_bytes >= b->current_length);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400880 n_left_this_buffer =
881 n_buffer_bytes - (b->current_data + b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700882 n = clib_min (n_left_this_buffer, n_left);
Damjan Marionf1213b82016-03-13 02:22:06 +0100883 clib_memcpy (vlib_buffer_get_current (b) + b->current_length, d, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700884 b->current_length += n;
885 n_left -= n;
886 if (n_left == 0)
887 break;
888
889 d += n;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400890 if (1 !=
891 vlib_buffer_alloc_from_free_list (vm, &b->next_buffer, 1,
892 free_list_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700893 goto out_of_buffers;
894
895 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
896
897 b = vlib_get_buffer (vm, b->next_buffer);
898 }
899
900 return bi;
901
Dave Barach9b8ffd92016-07-08 08:13:45 -0400902out_of_buffers:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700903 clib_error ("out of buffers");
904 return bi;
905}
906
Pierre Pfister328e99b2016-02-12 13:18:42 +0000907u16
Dave Barach9b8ffd92016-07-08 08:13:45 -0400908vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
909 u32 free_list_index,
910 vlib_buffer_t * first,
911 vlib_buffer_t ** last,
912 void *data, u16 data_len)
913{
Pierre Pfister328e99b2016-02-12 13:18:42 +0000914 vlib_buffer_t *l = *last;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400915 u32 n_buffer_bytes =
916 vlib_buffer_free_list_buffer_size (vm, free_list_index);
Pierre Pfister328e99b2016-02-12 13:18:42 +0000917 u16 copied = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400918 ASSERT (n_buffer_bytes >= l->current_length + l->current_data);
919 while (data_len)
920 {
921 u16 max = n_buffer_bytes - l->current_length - l->current_data;
922 if (max == 0)
923 {
924 if (1 !=
925 vlib_buffer_alloc_from_free_list (vm, &l->next_buffer, 1,
926 free_list_index))
927 return copied;
928 *last = l = vlib_buffer_chain_buffer (vm, first, l, l->next_buffer);
929 max = n_buffer_bytes - l->current_length - l->current_data;
930 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000931
Dave Barach9b8ffd92016-07-08 08:13:45 -0400932 u16 len = (data_len > max) ? max : data_len;
933 clib_memcpy (vlib_buffer_get_current (l) + l->current_length,
934 data + copied, len);
935 vlib_buffer_chain_increase_length (first, l, len);
936 data_len -= len;
937 copied += len;
938 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000939 return copied;
940}
941
Damjan Marion04a7f052017-07-10 15:06:17 +0200942void
943vlib_buffer_add_mem_range (vlib_main_t * vm, uword start, uword size)
944{
945 vlib_buffer_main_t *bm = vm->buffer_main;
946
947 if (bm->buffer_mem_size == 0)
948 {
949 bm->buffer_mem_start = start;
950 bm->buffer_mem_size = size;
951 }
952 else if (start < bm->buffer_mem_start)
953 {
954 bm->buffer_mem_size += bm->buffer_mem_start - start;
955 bm->buffer_mem_start = start;
956 if (size > bm->buffer_mem_size)
957 bm->buffer_mem_size = size;
958 }
959 else if (start > bm->buffer_mem_start)
960 {
961 uword new_size = start - bm->buffer_mem_start + size;
962 if (new_size > bm->buffer_mem_size)
963 bm->buffer_mem_size = new_size;
964 }
965
966 if ((u64) bm->buffer_mem_size >
967 ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES)))
968 {
969 clib_panic ("buffer memory size out of range!");
970 }
971}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700972
Dave Barach9b8ffd92016-07-08 08:13:45 -0400973static u8 *
974format_vlib_buffer_free_list (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400976 vlib_buffer_free_list_t *f = va_arg (*va, vlib_buffer_free_list_t *);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100977 u32 threadnum = va_arg (*va, u32);
978 uword bytes_alloc, bytes_free, n_free, size;
979
980 if (!f)
981 return format (s, "%=7s%=30s%=12s%=12s%=12s%=12s%=12s%=12s",
982 "Thread", "Name", "Index", "Size", "Alloc", "Free",
983 "#Alloc", "#Free");
984
985 size = sizeof (vlib_buffer_t) + f->n_data_bytes;
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100986 n_free = vec_len (f->buffers);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100987 bytes_alloc = size * f->n_alloc;
988 bytes_free = size * n_free;
989
Chris Luke7447d072017-07-05 12:57:10 -0400990 s = format (s, "%7d%30v%12d%12d%=12U%=12U%=12d%=12d", threadnum,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700991 f->name, f->index, f->n_data_bytes,
992 format_memory_size, bytes_alloc,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400993 format_memory_size, bytes_free, f->n_alloc, n_free);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700994
995 return s;
996}
997
998static clib_error_t *
999show_buffers (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001000 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001001{
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001002 vlib_buffer_main_t *bm;
1003 vlib_buffer_free_list_t *f;
1004 vlib_main_t *curr_vm;
1005 u32 vm_index = 0;
1006
1007 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, 0, 0);
1008
1009 do
1010 {
Dave Barach80f54e22017-03-08 19:08:56 -05001011 curr_vm = vlib_mains[vm_index];
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001012 bm = curr_vm->buffer_main;
1013
1014 /* *INDENT-OFF* */
1015 pool_foreach (f, bm->buffer_free_list_pool, ({
1016 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, f, vm_index);
1017 }));
1018 /* *INDENT-ON* */
1019
1020 vm_index++;
1021 }
1022 while (vm_index < vec_len (vlib_mains));
1023
Ed Warnickecb9cada2015-12-08 15:45:58 -07001024 return 0;
1025}
1026
Dave Barach9b8ffd92016-07-08 08:13:45 -04001027/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001028VLIB_CLI_COMMAND (show_buffers_command, static) = {
1029 .path = "show buffers",
1030 .short_help = "Show packet buffer allocation",
1031 .function = show_buffers,
1032};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001033/* *INDENT-ON* */
1034
Damjan Marion878c6092017-01-04 13:19:27 +01001035void
1036vlib_buffer_cb_init (struct vlib_main_t *vm)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001037{
Damjan Marion878c6092017-01-04 13:19:27 +01001038 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion04a7f052017-07-10 15:06:17 +02001039
Damjan Marion878c6092017-01-04 13:19:27 +01001040 bm->cb.vlib_buffer_alloc_cb = &vlib_buffer_alloc_internal;
1041 bm->cb.vlib_buffer_alloc_from_free_list_cb =
1042 &vlib_buffer_alloc_from_free_list_internal;
1043 bm->cb.vlib_buffer_free_cb = &vlib_buffer_free_internal;
1044 bm->cb.vlib_buffer_free_no_next_cb = &vlib_buffer_free_no_next_internal;
1045 bm->cb.vlib_buffer_delete_free_list_cb =
1046 &vlib_buffer_delete_free_list_internal;
Damjan Marion6b0f5892017-07-27 04:01:24 -04001047 clib_spinlock_init (&bm->buffer_known_hash_lockp);
Damjan Marion878c6092017-01-04 13:19:27 +01001048}
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001049
Chris Luked4024f52016-09-06 09:32:36 -04001050/** @endcond */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001051/*
1052 * fd.io coding-style-patch-verification: ON
1053 *
1054 * Local Variables:
1055 * eval: (c-set-style "gnu")
1056 * End:
1057 */