blob: a517a5972aa36572420d12a7e773a35e35b55d53 [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
Dave Barach9b8ffd92016-07-08 08:13:45 -040049uword
50vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
51 vlib_buffer_t * b_first)
Ed Warnickecb9cada2015-12-08 15:45:58 -070052{
Dave Barach9b8ffd92016-07-08 08:13:45 -040053 vlib_buffer_t *b = b_first;
Ed Warnickecb9cada2015-12-08 15:45:58 -070054 uword l_first = b_first->current_length;
55 uword l = 0;
56 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
57 {
58 b = vlib_get_buffer (vm, b->next_buffer);
59 l += b->current_length;
60 }
61 b_first->total_length_not_including_first_buffer = l;
62 b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
63 return l + l_first;
64}
65
Dave Barach9b8ffd92016-07-08 08:13:45 -040066u8 *
67format_vlib_buffer (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070068{
Dave Barach9b8ffd92016-07-08 08:13:45 -040069 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010070 uword indent = format_get_indent (s);
71
Damjan Marionc47ed032017-01-25 14:18:03 +010072 s = format (s, "current data %d, length %d, free-list %d, clone-count %u",
73 b->current_data, b->current_length, b->free_list_index,
74 b->n_add_refs);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010075
76 if (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID)
77 s = format (s, ", totlen-nifb %d",
78 b->total_length_not_including_first_buffer);
79
80 if (b->flags & VLIB_BUFFER_IS_TRACED)
81 s = format (s, ", trace 0x%x", b->trace_index);
82
83 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
84 {
85 vlib_main_t *vm = vlib_get_main ();
86 u32 next_buffer = b->next_buffer;
87 b = vlib_get_buffer (vm, next_buffer);
88
Damjan Marionc47ed032017-01-25 14:18:03 +010089 s =
90 format (s, "\n%Unext-buffer 0x%x, segment length %d, clone-count %u",
91 format_white_space, indent, next_buffer, b->current_length,
92 b->n_add_refs);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010093 }
94
Ed Warnickecb9cada2015-12-08 15:45:58 -070095 return s;
96}
97
Dave Barach9b8ffd92016-07-08 08:13:45 -040098u8 *
99format_vlib_buffer_and_data (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400101 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
102
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103 s = format (s, "%U, %U",
104 format_vlib_buffer, b,
105 format_hex_bytes, vlib_buffer_get_current (b), 64);
106
107 return s;
108}
109
Dave Barach9b8ffd92016-07-08 08:13:45 -0400110static u8 *
111format_vlib_buffer_known_state (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112{
113 vlib_buffer_known_state_t state = va_arg (*args, vlib_buffer_known_state_t);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400114 char *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115
116 switch (state)
117 {
118 case VLIB_BUFFER_UNKNOWN:
119 t = "unknown";
120 break;
121
122 case VLIB_BUFFER_KNOWN_ALLOCATED:
123 t = "known-allocated";
124 break;
125
126 case VLIB_BUFFER_KNOWN_FREE:
127 t = "known-free";
128 break;
129
130 default:
131 t = "invalid";
132 break;
133 }
134
135 return format (s, "%s", t);
136}
137
Dave Barach9b8ffd92016-07-08 08:13:45 -0400138u8 *
139format_vlib_buffer_contents (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400141 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
142 vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *);
143
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144 while (1)
145 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400146 vec_add (s, vlib_buffer_get_current (b), b->current_length);
147 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148 break;
149 b = vlib_get_buffer (vm, b->next_buffer);
150 }
151
152 return s;
153}
154
155static u8 *
156vlib_validate_buffer_helper (vlib_main_t * vm,
157 u32 bi,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400158 uword follow_buffer_next, uword ** unique_hash)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400160 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
161 vlib_buffer_main_t *bm = vm->buffer_main;
162 vlib_buffer_free_list_t *fl;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163
Dave Barach9b8ffd92016-07-08 08:13:45 -0400164 if (pool_is_free_index (bm->buffer_free_list_pool, b->free_list_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165 return format (0, "unknown free list 0x%x", b->free_list_index);
166
Dave Barach9b8ffd92016-07-08 08:13:45 -0400167 fl = pool_elt_at_index (bm->buffer_free_list_pool, b->free_list_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168
Dave Barach9b8ffd92016-07-08 08:13:45 -0400169 if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170 return format (0, "current data %d before pre-data", b->current_data);
Damjan Marion878c6092017-01-04 13:19:27 +0100171
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 if (b->current_data + b->current_length > fl->n_data_bytes)
173 return format (0, "%d-%d beyond end of buffer %d",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400174 b->current_data, b->current_length, fl->n_data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
Dave Barach9b8ffd92016-07-08 08:13:45 -0400176 if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 {
178 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400179 u8 *msg, *result;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180
181 k = vlib_buffer_is_known (vm, b->next_buffer);
182 if (k != VLIB_BUFFER_KNOWN_ALLOCATED)
183 return format (0, "next 0x%x: %U",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400184 b->next_buffer, format_vlib_buffer_known_state, k);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185
186 if (unique_hash)
187 {
188 if (hash_get (*unique_hash, b->next_buffer))
189 return format (0, "duplicate buffer 0x%x", b->next_buffer);
190
191 hash_set1 (*unique_hash, b->next_buffer);
192 }
193
194 msg = vlib_validate_buffer (vm, b->next_buffer, follow_buffer_next);
195 if (msg)
196 {
197 result = format (0, "next 0x%x: %v", b->next_buffer, msg);
198 vec_free (msg);
199 return result;
200 }
201 }
202
203 return 0;
204}
205
206u8 *
207vlib_validate_buffer (vlib_main_t * vm, u32 bi, uword follow_buffer_next)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400208{
209 return vlib_validate_buffer_helper (vm, bi, follow_buffer_next,
210 /* unique_hash */ 0);
211}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212
213u8 *
214vlib_validate_buffers (vlib_main_t * vm,
215 u32 * buffers,
216 uword next_buffer_stride,
217 uword n_buffers,
218 vlib_buffer_known_state_t known_state,
219 uword follow_buffer_next)
220{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400221 uword i, *hash;
222 u32 bi, *b = buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400224 u8 *msg = 0, *result = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225
226 hash = hash_create (0, 0);
227 for (i = 0; i < n_buffers; i++)
228 {
229 bi = b[0];
230 b += next_buffer_stride;
231
232 /* Buffer is not unique. */
233 if (hash_get (hash, bi))
234 {
235 msg = format (0, "not unique");
236 goto done;
237 }
238
239 k = vlib_buffer_is_known (vm, bi);
240 if (k != known_state)
241 {
242 msg = format (0, "is %U; expected %U",
243 format_vlib_buffer_known_state, k,
244 format_vlib_buffer_known_state, known_state);
245 goto done;
246 }
247
248 msg = vlib_validate_buffer_helper (vm, bi, follow_buffer_next, &hash);
249 if (msg)
250 goto done;
251
252 hash_set1 (hash, bi);
253 }
254
Dave Barach9b8ffd92016-07-08 08:13:45 -0400255done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256 if (msg)
257 {
258 result = format (0, "0x%x: %v", bi, msg);
259 vec_free (msg);
260 }
261 hash_free (hash);
262 return result;
263}
264
Dave Barach80f54e22017-03-08 19:08:56 -0500265/*
266 * Hand-craft a static vector w/ length 1, so vec_len(vlib_mains) =1
267 * and vlib_mains[0] = &vlib_global_main from the beginning of time.
268 *
269 * The only place which should ever expand vlib_mains is start_workers()
270 * in threads.c. It knows about the bootstrap vector.
271 */
272/* *INDENT-OFF* */
273static struct
274{
275 vec_header_t h;
276 vlib_main_t *vm;
277} __attribute__ ((packed)) __bootstrap_vlib_main_vector
278 __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES))) =
279{
280 .h.len = 1,
281 .vm = &vlib_global_main,
282};
283/* *INDENT-ON* */
284
285vlib_main_t **vlib_mains = &__bootstrap_vlib_main_vector.vm;
286
Ed Warnickecb9cada2015-12-08 15:45:58 -0700287
288/* When dubugging validate that given buffers are either known allocated
289 or known free. */
Damjan Marionc47ed032017-01-25 14:18:03 +0100290static void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291vlib_buffer_validate_alloc_free (vlib_main_t * vm,
292 u32 * buffers,
293 uword n_buffers,
294 vlib_buffer_known_state_t expected_state)
295{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400296 u32 *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297 uword i, bi, is_free;
298
299 if (CLIB_DEBUG == 0)
300 return;
301
Dave Barach9b8ffd92016-07-08 08:13:45 -0400302 ASSERT (os_get_cpu_number () == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303
304 /* smp disaster check */
Dave Barach80f54e22017-03-08 19:08:56 -0500305 if (vec_len (vlib_mains) > 1)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400306 ASSERT (vm == vlib_mains[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307
308 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
358 ASSERT (os_get_cpu_number () == 0);
359
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;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100386 f->name = clib_mem_is_heap_object (name) ? name : format (0, "%s", name);
387
388 /* Setup free buffer template. */
389 f->buffer_init_template.free_list_index = 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
477 ASSERT (os_get_cpu_number () == 0);
478
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
Damjan Marion374e2c52017-03-09 20:38:15 +0100587 /* wait with buffer memory allocation as long as possible
588 in case external buffer manager takes over */
589 if (PREDICT_FALSE (vm->os_physmem_alloc_aligned == 0))
590 unix_physmem_init (vm, 0 /* fail_if_physical_memory_not_present */ );
591
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592 n_filled = fill_free_list (vm, free_list, n_alloc_buffers);
593 if (n_filled == 0)
594 return 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400595
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100596 len = vec_len (free_list->buffers);
597 ASSERT (len >= n_alloc_buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700598
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100599 src = free_list->buffers + len - n_alloc_buffers;
600 clib_memcpy (dst, src, n_alloc_buffers * sizeof (u32));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100602 _vec_len (free_list->buffers) -= n_alloc_buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700603
604 /* Verify that buffers are known free. */
605 vlib_buffer_validate_alloc_free (vm, alloc_buffers,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400606 n_alloc_buffers, VLIB_BUFFER_KNOWN_FREE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700607
608 return n_alloc_buffers;
609}
610
Damjan Marion878c6092017-01-04 13:19:27 +0100611
Ed Warnickecb9cada2015-12-08 15:45:58 -0700612/* Allocate a given number of buffers into given array.
613 Returns number actually allocated which will be either zero or
614 number requested. */
Damjan Marion878c6092017-01-04 13:19:27 +0100615static u32
616vlib_buffer_alloc_internal (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700617{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400618 vlib_buffer_main_t *bm = vm->buffer_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700619
620 return alloc_from_free_list
621 (vm,
622 pool_elt_at_index (bm->buffer_free_list_pool,
623 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX),
624 buffers, n_buffers);
625}
626
Damjan Marion878c6092017-01-04 13:19:27 +0100627static u32
628vlib_buffer_alloc_from_free_list_internal (vlib_main_t * vm,
629 u32 * buffers,
630 u32 n_buffers, u32 free_list_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400632 vlib_buffer_main_t *bm = vm->buffer_main;
633 vlib_buffer_free_list_t *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634 f = pool_elt_at_index (bm->buffer_free_list_pool, free_list_index);
635 return alloc_from_free_list (vm, f, buffers, n_buffers);
636}
637
Dave Barach9b8ffd92016-07-08 08:13:45 -0400638void *
639vlib_set_buffer_free_callback (vlib_main_t * vm, void *fp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700640{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400641 vlib_buffer_main_t *bm = vm->buffer_main;
642 void *rv = bm->buffer_free_callback;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643
644 bm->buffer_free_callback = fp;
645 return rv;
646}
647
Ed Warnickecb9cada2015-12-08 15:45:58 -0700648static_always_inline void
649vlib_buffer_free_inline (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400650 u32 * buffers, u32 n_buffers, u32 follow_buffer_next)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400652 vlib_buffer_main_t *bm = vm->buffer_main;
653 vlib_buffer_free_list_t *fl;
Damjan Marionc47ed032017-01-25 14:18:03 +0100654 u32 fi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700655 int i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400656 u32 (*cb) (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
657 u32 follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700658
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659 cb = bm->buffer_free_callback;
660
661 if (PREDICT_FALSE (cb != 0))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400662 n_buffers = (*cb) (vm, buffers, n_buffers, follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700663
Dave Barach9b8ffd92016-07-08 08:13:45 -0400664 if (!n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665 return;
666
Damjan Marionc47ed032017-01-25 14:18:03 +0100667 for (i = 0; i < n_buffers; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100669 vlib_buffer_t *b;
670 u32 bi = buffers[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671
Damjan Marionc47ed032017-01-25 14:18:03 +0100672 b = vlib_get_buffer (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673
Damjan Marionc47ed032017-01-25 14:18:03 +0100674 fl = vlib_buffer_get_buffer_free_list (vm, b, &fi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700675
Damjan Marionc47ed032017-01-25 14:18:03 +0100676 /* The only current use of this callback: multicast recycle */
677 if (PREDICT_FALSE (fl->buffers_added_to_freelist_function != 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100679 int j;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700680
Damjan Marionc47ed032017-01-25 14:18:03 +0100681 vlib_buffer_add_to_free_list
682 (vm, fl, buffers[i], (b->flags & VLIB_BUFFER_RECYCLE) == 0);
683
684 for (j = 0; j < vec_len (bm->announce_list); j++)
685 {
686 if (fl == bm->announce_list[j])
687 goto already_announced;
688 }
689 vec_add1 (bm->announce_list, fl);
690 already_announced:
691 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692 }
693 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400694 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100695 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_RECYCLE) == 0))
696 {
697 u32 flags, next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700698
Damjan Marionc47ed032017-01-25 14:18:03 +0100699 do
700 {
701 vlib_buffer_t *nb = vlib_get_buffer (vm, bi);
702 flags = nb->flags;
703 next = nb->next_buffer;
704 if (nb->n_add_refs)
705 nb->n_add_refs--;
706 else
707 {
708 vlib_buffer_validate_alloc_free (vm, &bi, 1,
709 VLIB_BUFFER_KNOWN_ALLOCATED);
710 vlib_buffer_add_to_free_list (vm, fl, bi, 1);
711 }
712 bi = next;
713 }
714 while (follow_buffer_next
715 && (flags & VLIB_BUFFER_NEXT_PRESENT));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700716
Damjan Marionc47ed032017-01-25 14:18:03 +0100717 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700718 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700719 }
Damjan Marionc47ed032017-01-25 14:18:03 +0100720 if (vec_len (bm->announce_list))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400722 vlib_buffer_free_list_t *fl;
Damjan Marionc47ed032017-01-25 14:18:03 +0100723 for (i = 0; i < vec_len (bm->announce_list); i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400724 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100725 fl = bm->announce_list[i];
Dave Barach9b8ffd92016-07-08 08:13:45 -0400726 fl->buffers_added_to_freelist_function (vm, fl);
727 }
Damjan Marionc47ed032017-01-25 14:18:03 +0100728 _vec_len (bm->announce_list) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700729 }
730}
731
Damjan Marion878c6092017-01-04 13:19:27 +0100732static void
733vlib_buffer_free_internal (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700734{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400735 vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
736 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737}
738
Damjan Marion878c6092017-01-04 13:19:27 +0100739static void
740vlib_buffer_free_no_next_internal (vlib_main_t * vm, u32 * buffers,
741 u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700742{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400743 vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
744 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745}
746
747/* Copy template packet data into buffers as they are allocated. */
Damjan Marion878c6092017-01-04 13:19:27 +0100748static void __attribute__ ((unused))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749vlib_packet_template_buffer_init (vlib_main_t * vm,
750 vlib_buffer_free_list_t * fl,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400751 u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700752{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400753 vlib_packet_template_t *t =
754 uword_to_pointer (fl->buffer_init_function_opaque,
755 vlib_packet_template_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700756 uword i;
757
758 for (i = 0; i < n_buffers; i++)
759 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400760 vlib_buffer_t *b = vlib_get_buffer (vm, buffers[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700761 ASSERT (b->current_length == vec_len (t->packet_data));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400762 clib_memcpy (vlib_buffer_get_current (b), t->packet_data,
763 b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700764 }
765}
766
Dave Barach9b8ffd92016-07-08 08:13:45 -0400767void
768vlib_packet_template_init (vlib_main_t * vm,
769 vlib_packet_template_t * t,
770 void *packet_data,
771 uword n_packet_data_bytes,
772 uword min_n_buffers_each_physmem_alloc,
773 char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700774{
Damjan Marion878c6092017-01-04 13:19:27 +0100775 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100776 va_list va;
777 __attribute__ ((unused)) u8 *name;
Damjan Marion878c6092017-01-04 13:19:27 +0100778 vlib_buffer_free_list_t *fl;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100779
780 va_start (va, fmt);
781 name = va_format (0, fmt, &va);
782 va_end (va);
783
Damjan Marion878c6092017-01-04 13:19:27 +0100784 if (bm->cb.vlib_packet_template_init_cb)
785 bm->cb.vlib_packet_template_init_cb (vm, (void *) t, packet_data,
786 n_packet_data_bytes,
787 min_n_buffers_each_physmem_alloc,
788 name);
789
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100790 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791
792 memset (t, 0, sizeof (t[0]));
793
794 vec_add (t->packet_data, packet_data, n_packet_data_bytes);
795 t->min_n_buffers_each_physmem_alloc = min_n_buffers_each_physmem_alloc;
796
797 t->free_list_index = vlib_buffer_create_free_list_helper
798 (vm, n_packet_data_bytes,
799 /* is_public */ 1,
800 /* is_default */ 0,
801 name);
802
803 ASSERT (t->free_list_index != 0);
804 fl = vlib_buffer_get_free_list (vm, t->free_list_index);
805 fl->min_n_buffers_each_physmem_alloc = t->min_n_buffers_each_physmem_alloc;
806
807 fl->buffer_init_function = vlib_packet_template_buffer_init;
808 fl->buffer_init_function_opaque = pointer_to_uword (t);
809
810 fl->buffer_init_template.current_data = 0;
811 fl->buffer_init_template.current_length = n_packet_data_bytes;
812 fl->buffer_init_template.flags = 0;
Damjan Marionc47ed032017-01-25 14:18:03 +0100813 fl->buffer_init_template.n_add_refs = 0;
Damjan Marion878c6092017-01-04 13:19:27 +0100814 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700815}
816
817void *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400818vlib_packet_template_get_packet (vlib_main_t * vm,
819 vlib_packet_template_t * t, u32 * bi_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700820{
821 u32 bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400822 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700823
824 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
825 return 0;
826
827 *bi_result = bi;
828
829 b = vlib_get_buffer (vm, bi);
Damjan Marionf1213b82016-03-13 02:22:06 +0100830 clib_memcpy (vlib_buffer_get_current (b),
Dave Barach9b8ffd92016-07-08 08:13:45 -0400831 t->packet_data, vec_len (t->packet_data));
832 b->current_length = vec_len (t->packet_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700833
834 return b->data;
835}
836
Dave Barach9b8ffd92016-07-08 08:13:45 -0400837void
838vlib_packet_template_get_packet_helper (vlib_main_t * vm,
839 vlib_packet_template_t * t)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700840{
841 word n = t->min_n_buffers_each_physmem_alloc;
842 word l = vec_len (t->packet_data);
843 word n_alloc;
844
845 ASSERT (l > 0);
846 ASSERT (vec_len (t->free_buffers) == 0);
847
848 vec_validate (t->free_buffers, n - 1);
849 n_alloc = vlib_buffer_alloc_from_free_list (vm, t->free_buffers,
850 n, t->free_list_index);
851 _vec_len (t->free_buffers) = n_alloc;
852}
853
854/* Append given data to end of buffer, possibly allocating new buffers. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400855u32
856vlib_buffer_add_data (vlib_main_t * vm,
857 u32 free_list_index,
858 u32 buffer_index, void *data, u32 n_data_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700859{
860 u32 n_buffer_bytes, n_left, n_left_this_buffer, bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400861 vlib_buffer_t *b;
862 void *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700863
864 bi = buffer_index;
865 if (bi == 0
866 && 1 != vlib_buffer_alloc_from_free_list (vm, &bi, 1, free_list_index))
867 goto out_of_buffers;
868
869 d = data;
870 n_left = n_data_bytes;
871 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm, free_list_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400872
Ed Warnickecb9cada2015-12-08 15:45:58 -0700873 b = vlib_get_buffer (vm, bi);
874 b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
875
Dave Barach9b8ffd92016-07-08 08:13:45 -0400876 /* Get to the end of the chain before we try to append data... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700877 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
878 b = vlib_get_buffer (vm, b->next_buffer);
879
880 while (1)
881 {
882 u32 n;
883
884 ASSERT (n_buffer_bytes >= b->current_length);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400885 n_left_this_buffer =
886 n_buffer_bytes - (b->current_data + b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700887 n = clib_min (n_left_this_buffer, n_left);
Damjan Marionf1213b82016-03-13 02:22:06 +0100888 clib_memcpy (vlib_buffer_get_current (b) + b->current_length, d, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700889 b->current_length += n;
890 n_left -= n;
891 if (n_left == 0)
892 break;
893
894 d += n;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400895 if (1 !=
896 vlib_buffer_alloc_from_free_list (vm, &b->next_buffer, 1,
897 free_list_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700898 goto out_of_buffers;
899
900 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
901
902 b = vlib_get_buffer (vm, b->next_buffer);
903 }
904
905 return bi;
906
Dave Barach9b8ffd92016-07-08 08:13:45 -0400907out_of_buffers:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700908 clib_error ("out of buffers");
909 return bi;
910}
911
Pierre Pfister328e99b2016-02-12 13:18:42 +0000912u16
Dave Barach9b8ffd92016-07-08 08:13:45 -0400913vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
914 u32 free_list_index,
915 vlib_buffer_t * first,
916 vlib_buffer_t ** last,
917 void *data, u16 data_len)
918{
Pierre Pfister328e99b2016-02-12 13:18:42 +0000919 vlib_buffer_t *l = *last;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400920 u32 n_buffer_bytes =
921 vlib_buffer_free_list_buffer_size (vm, free_list_index);
Pierre Pfister328e99b2016-02-12 13:18:42 +0000922 u16 copied = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400923 ASSERT (n_buffer_bytes >= l->current_length + l->current_data);
924 while (data_len)
925 {
926 u16 max = n_buffer_bytes - l->current_length - l->current_data;
927 if (max == 0)
928 {
929 if (1 !=
930 vlib_buffer_alloc_from_free_list (vm, &l->next_buffer, 1,
931 free_list_index))
932 return copied;
933 *last = l = vlib_buffer_chain_buffer (vm, first, l, l->next_buffer);
934 max = n_buffer_bytes - l->current_length - l->current_data;
935 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000936
Dave Barach9b8ffd92016-07-08 08:13:45 -0400937 u16 len = (data_len > max) ? max : data_len;
938 clib_memcpy (vlib_buffer_get_current (l) + l->current_length,
939 data + copied, len);
940 vlib_buffer_chain_increase_length (first, l, len);
941 data_len -= len;
942 copied += len;
943 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000944 return copied;
945}
946
Ed Warnickecb9cada2015-12-08 15:45:58 -0700947
Dave Barach9b8ffd92016-07-08 08:13:45 -0400948static u8 *
949format_vlib_buffer_free_list (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700950{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400951 vlib_buffer_free_list_t *f = va_arg (*va, vlib_buffer_free_list_t *);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100952 u32 threadnum = va_arg (*va, u32);
953 uword bytes_alloc, bytes_free, n_free, size;
954
955 if (!f)
956 return format (s, "%=7s%=30s%=12s%=12s%=12s%=12s%=12s%=12s",
957 "Thread", "Name", "Index", "Size", "Alloc", "Free",
958 "#Alloc", "#Free");
959
960 size = sizeof (vlib_buffer_t) + f->n_data_bytes;
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100961 n_free = vec_len (f->buffers);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100962 bytes_alloc = size * f->n_alloc;
963 bytes_free = size * n_free;
964
965 s = format (s, "%7d%30s%12d%12d%=12U%=12U%=12d%=12d", threadnum,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700966 f->name, f->index, f->n_data_bytes,
967 format_memory_size, bytes_alloc,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400968 format_memory_size, bytes_free, f->n_alloc, n_free);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700969
970 return s;
971}
972
973static clib_error_t *
974show_buffers (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400975 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700976{
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100977 vlib_buffer_main_t *bm;
978 vlib_buffer_free_list_t *f;
979 vlib_main_t *curr_vm;
980 u32 vm_index = 0;
981
982 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, 0, 0);
983
984 do
985 {
Dave Barach80f54e22017-03-08 19:08:56 -0500986 curr_vm = vlib_mains[vm_index];
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100987 bm = curr_vm->buffer_main;
988
989 /* *INDENT-OFF* */
990 pool_foreach (f, bm->buffer_free_list_pool, ({
991 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, f, vm_index);
992 }));
993 /* *INDENT-ON* */
994
995 vm_index++;
996 }
997 while (vm_index < vec_len (vlib_mains));
998
Ed Warnickecb9cada2015-12-08 15:45:58 -0700999 return 0;
1000}
1001
Dave Barach9b8ffd92016-07-08 08:13:45 -04001002/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001003VLIB_CLI_COMMAND (show_buffers_command, static) = {
1004 .path = "show buffers",
1005 .short_help = "Show packet buffer allocation",
1006 .function = show_buffers,
1007};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001008/* *INDENT-ON* */
1009
Damjan Marion878c6092017-01-04 13:19:27 +01001010void
1011vlib_buffer_cb_init (struct vlib_main_t *vm)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001012{
Damjan Marion878c6092017-01-04 13:19:27 +01001013 vlib_buffer_main_t *bm = vm->buffer_main;
1014 bm->cb.vlib_buffer_alloc_cb = &vlib_buffer_alloc_internal;
1015 bm->cb.vlib_buffer_alloc_from_free_list_cb =
1016 &vlib_buffer_alloc_from_free_list_internal;
1017 bm->cb.vlib_buffer_free_cb = &vlib_buffer_free_internal;
1018 bm->cb.vlib_buffer_free_no_next_cb = &vlib_buffer_free_no_next_internal;
1019 bm->cb.vlib_buffer_delete_free_list_cb =
1020 &vlib_buffer_delete_free_list_internal;
1021 bm->extern_buffer_mgmt = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001022}
1023
Damjan Marion878c6092017-01-04 13:19:27 +01001024int
1025vlib_buffer_cb_register (struct vlib_main_t *vm, vlib_buffer_callbacks_t * cb)
1026{
1027 vlib_buffer_main_t *bm = vm->buffer_main;
1028 if (bm->extern_buffer_mgmt)
1029 return -1;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001030
Damjan Marion878c6092017-01-04 13:19:27 +01001031#define _(x) bm->cb.x = cb->x
1032 _(vlib_buffer_alloc_cb);
1033 _(vlib_buffer_alloc_from_free_list_cb);
1034 _(vlib_buffer_free_cb);
1035 _(vlib_buffer_free_no_next_cb);
1036 _(vlib_buffer_delete_free_list_cb);
1037#undef _
1038 bm->extern_buffer_mgmt = 1;
1039 return 0;
1040}
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001041
Chris Luked4024f52016-09-06 09:32:36 -04001042/** @endcond */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001043/*
1044 * fd.io coding-style-patch-verification: ON
1045 *
1046 * Local Variables:
1047 * eval: (c-set-style "gnu")
1048 * End:
1049 */