aboutsummaryrefslogtreecommitdiffstats
path: root/src/vlib
diff options
context:
space:
mode:
authorDamjan Marion <damarion@cisco.com>2019-01-21 11:48:34 +0100
committerDave Barach <openvpp@barachs.net>2019-01-30 16:19:22 +0000
commit910d3694e8b22c9d14e5f2913d14ae149e184620 (patch)
treee4993e93e4d7dba51a5898e82bb6149a3e4bd7ba /src/vlib
parent4fd5a9d3e6abdf61f266da8400a299fe5b0eb0ed (diff)
buffers: major cleanup and improvements
This patch introduces following changes: - deprecated free lists which are not used and not compatible with external buffer managers (i.e. DPDK) - introduces native support for per-numa buffer pools - significantly improves performance of buffer alloc and free Change-Id: I4a8e723ae47056717afd6cac0efe87cb731b5be7 Signed-off-by: Damjan Marion <damarion@cisco.com>
Diffstat (limited to 'src/vlib')
-rw-r--r--src/vlib/buffer.c599
-rw-r--r--src/vlib/buffer.h122
-rw-r--r--src/vlib/buffer_funcs.h516
-rw-r--r--src/vlib/log.h2
-rw-r--r--src/vlib/main.c9
-rw-r--r--src/vlib/main.h3
-rw-r--r--src/vlib/threads.c22
-rw-r--r--src/vlib/vlib.h1
8 files changed, 591 insertions, 683 deletions
diff --git a/src/vlib/buffer.c b/src/vlib/buffer.c
index 643f2c50342..3e194113675 100644
--- a/src/vlib/buffer.c
+++ b/src/vlib/buffer.c
@@ -43,18 +43,24 @@
* Allocate/free network buffers.
*/
+#include <vppinfra/linux/sysfs.h>
#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
-vlib_buffer_callbacks_t *vlib_buffer_callbacks = 0;
+#define VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA 16384
+#define VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA_UNPRIV 8192
-/* when running unpriviledged we are limited by RLIMIT_MEMLOCK which is
- typically set to 16MB so setting default size for buffer memory to 14MB
- */
-static u32 vlib_buffer_physmem_sz = 14 << 20;
+#ifdef CLIB_HAVE_VEC128
+/* Assumptions by vlib_buffer_free_inline: */
+STATIC_ASSERT_FITS_IN (vlib_buffer_t, flags, 16);
+STATIC_ASSERT_FITS_IN (vlib_buffer_t, ref_count, 16);
+STATIC_ASSERT_FITS_IN (vlib_buffer_t, buffer_pool_index, 16);
+#endif
-/* logging */
-static vlib_log_class_t buffer_log_default;
+/* Make sure that buffer template size is not accidentally changed */
+STATIC_ASSERT_OFFSET_OF (vlib_buffer_t, template_end, 64);
+
+u16 __vlib_buffer_external_hdr_size = 0;
uword
vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
@@ -86,8 +92,8 @@ format_vlib_buffer (u8 * s, va_list * args)
foreach_vlib_buffer_flag
#undef _
s = format (s, "current data %d, length %d, buffer-pool %d, "
- "clone-count %u", b->current_data, b->current_length,
- b->buffer_pool_index, b->n_add_refs);
+ "ref-count %u", b->current_data, b->current_length,
+ b->buffer_pool_index, b->ref_count);
if (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID)
s = format (s, ", totlen-nifb %d",
@@ -107,9 +113,9 @@ format_vlib_buffer (u8 * s, va_list * args)
b = vlib_get_buffer (vm, next_buffer);
s =
- format (s, "\n%Unext-buffer 0x%x, segment length %d, clone-count %u",
+ format (s, "\n%Unext-buffer 0x%x, segment length %d, ref-count %u",
format_white_space, indent, next_buffer, b->current_length,
- b->n_add_refs);
+ b->ref_count);
}
return s;
@@ -177,14 +183,18 @@ vlib_validate_buffer_helper (vlib_main_t * vm,
u32 bi,
uword follow_buffer_next, uword ** unique_hash)
{
+ vlib_buffer_main_t *bm = vm->buffer_main;
vlib_buffer_t *b = vlib_get_buffer (vm, bi);
+ if (vec_len (bm->buffer_pools) <= b->buffer_pool_index)
+ return format (0, "unknown buffer pool 0x%x", b->buffer_pool_index);
+
if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE)
return format (0, "current data %d before pre-data", b->current_data);
if (b->current_data + b->current_length > VLIB_BUFFER_DATA_SIZE)
- return format (0, "%d-%d beyond end of buffer %d",
- b->current_data, b->current_length, VLIB_BUFFER_DATA_SIZE);
+ return format (0, "%d-%d beyond end of buffer %d", b->current_data,
+ b->current_length, VLIB_BUFFER_DATA_SIZE);
if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT))
{
@@ -306,15 +316,13 @@ vlib_buffer_validate_alloc_free (vlib_main_t * vm,
uword n_buffers,
vlib_buffer_known_state_t expected_state)
{
+ vlib_buffer_main_t *bm = vm->buffer_main;
u32 *b;
uword i, bi, is_free;
if (CLIB_DEBUG == 0)
return;
- if (vlib_buffer_callbacks)
- return;
-
is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED;
b = buffers;
for (i = 0; i < n_buffers; i++)
@@ -324,287 +332,26 @@ vlib_buffer_validate_alloc_free (vlib_main_t * vm,
bi = b[0];
b += 1;
known = vlib_buffer_is_known (vm, bi);
+
+ if (known == VLIB_BUFFER_UNKNOWN &&
+ expected_state == VLIB_BUFFER_KNOWN_FREE)
+ known = VLIB_BUFFER_KNOWN_FREE;
+
if (known != expected_state)
{
- ASSERT (0);
vlib_panic_with_msg
(vm, "%s %U buffer 0x%x",
is_free ? "freeing" : "allocating",
format_vlib_buffer_known_state, known, bi);
}
- vlib_buffer_set_known_state (vm, bi, is_free ? VLIB_BUFFER_KNOWN_FREE :
- VLIB_BUFFER_KNOWN_ALLOCATED);
- }
-}
-
-/* Add buffer free list. */
-static vlib_buffer_free_list_index_t
-vlib_buffer_create_free_list_helper (vlib_main_t * vm,
- u32 n_data_bytes,
- u32 is_public, u32 is_default, u8 * name)
-{
- vlib_buffer_main_t *bm = vm->buffer_main;
- vlib_buffer_free_list_t *f;
- int i;
-
- ASSERT (vlib_get_thread_index () == 0);
-
- if (!is_default && pool_elts (vm->buffer_free_list_pool) == 0)
- {
- vlib_buffer_free_list_index_t default_free_free_list_index;
-
- /* *INDENT-OFF* */
- default_free_free_list_index =
- vlib_buffer_create_free_list_helper
- (vm,
- /* default buffer size */ VLIB_BUFFER_DATA_SIZE,
- /* is_public */ 1,
- /* is_default */ 1,
- (u8 *) "default");
- /* *INDENT-ON* */
- ASSERT (default_free_free_list_index ==
- VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
-
- if (n_data_bytes == VLIB_BUFFER_DATA_SIZE && is_public)
- return default_free_free_list_index;
- }
-
- pool_get_aligned (vm->buffer_free_list_pool, f, CLIB_CACHE_LINE_BYTES);
-
- clib_memset (f, 0, sizeof (f[0]));
- f->index = f - vm->buffer_free_list_pool;
- vec_validate (f->buffers, 0);
- vec_reset_length (f->buffers);
- f->min_n_buffers_each_alloc = VLIB_FRAME_SIZE;
- f->buffer_pool_index = 0;
- f->name = clib_mem_is_vec (name) ? name : format (0, "%s", name);
-
- /* Setup free buffer template. */
- f->buffer_init_template.n_add_refs = 0;
-
- if (is_public)
- {
- uword *p = hash_get (bm->free_list_by_size, VLIB_BUFFER_DATA_SIZE);
- if (!p)
- hash_set (bm->free_list_by_size, VLIB_BUFFER_DATA_SIZE, f->index);
- }
-
- for (i = 1; i < vec_len (vlib_mains); i++)
- {
- vlib_main_t *wvm = vlib_mains[i];
- vlib_buffer_free_list_t *wf;
- pool_get_aligned (wvm->buffer_free_list_pool,
- wf, CLIB_CACHE_LINE_BYTES);
- ASSERT (f - vm->buffer_free_list_pool ==
- wf - wvm->buffer_free_list_pool);
- wf[0] = f[0];
- wf->buffers = 0;
- vec_validate (wf->buffers, 0);
- vec_reset_length (wf->buffers);
- wf->n_alloc = 0;
- }
-
- return f->index;
-}
-
-vlib_buffer_free_list_index_t
-vlib_buffer_create_free_list (vlib_main_t * vm, u32 n_data_bytes,
- char *fmt, ...)
-{
- va_list va;
- u8 *name;
-
- va_start (va, fmt);
- name = va_format (0, fmt, &va);
- va_end (va);
-
- return vlib_buffer_create_free_list_helper (vm, n_data_bytes,
- /* is_public */ 0,
- /* is_default */ 0,
- name);
-}
-
-static_always_inline void *
-vlib_buffer_pool_get_buffer (vlib_main_t * vm, vlib_buffer_pool_t * bp)
-{
- return vlib_physmem_alloc_from_map (vm, bp->physmem_map_index,
- bp->buffer_size, CLIB_CACHE_LINE_BYTES);
-}
-
-/* Make sure free list has at least given number of free buffers. */
-static uword
-vlib_buffer_fill_free_list_internal (vlib_main_t * vm,
- vlib_buffer_free_list_t * fl,
- uword min_free_buffers)
-{
- vlib_buffer_t *b;
- vlib_buffer_pool_t *bp = vlib_buffer_pool_get (vm, fl->buffer_pool_index);
- int n;
- u32 *bi;
- u32 n_alloc = 0;
-
- /* Already have enough free buffers on free list? */
- n = min_free_buffers - vec_len (fl->buffers);
- if (n <= 0)
- return min_free_buffers;
-
- if (vec_len (bp->buffers) > 0)
- {
- int n_copy, n_left;
- clib_spinlock_lock (&bp->lock);
- n_copy = clib_min (vec_len (bp->buffers), n);
- n_left = vec_len (bp->buffers) - n_copy;
- vec_add_aligned (fl->buffers, bp->buffers + n_left, n_copy,
- CLIB_CACHE_LINE_BYTES);
- _vec_len (bp->buffers) = n_left;
- clib_spinlock_unlock (&bp->lock);
- n = min_free_buffers - vec_len (fl->buffers);
- if (n <= 0)
- return min_free_buffers;
- }
-
- /* Always allocate round number of buffers. */
- n = round_pow2 (n, CLIB_CACHE_LINE_BYTES / sizeof (u32));
-
- /* Always allocate new buffers in reasonably large sized chunks. */
- n = clib_max (n, fl->min_n_buffers_each_alloc);
-
- clib_spinlock_lock (&bp->lock);
- while (n_alloc < n)
- {
- if ((b = vlib_buffer_pool_get_buffer (vm, bp)) == 0)
- goto done;
-
- n_alloc += 1;
-
- vec_add2_aligned (fl->buffers, bi, 1, CLIB_CACHE_LINE_BYTES);
- bi[0] = vlib_get_buffer_index (vm, b);
-
- if (CLIB_DEBUG > 0)
- vlib_buffer_set_known_state (vm, bi[0], VLIB_BUFFER_KNOWN_FREE);
-
- clib_memset (b, 0, sizeof (vlib_buffer_t));
-
- if (fl->buffer_init_function)
- fl->buffer_init_function (vm, fl, bi, 1);
- }
-
-done:
- clib_spinlock_unlock (&bp->lock);
- fl->n_alloc += n_alloc;
- return n_alloc;
-}
-
-void *
-vlib_set_buffer_free_callback (vlib_main_t * vm, void *fp)
-{
- vlib_buffer_main_t *bm = vm->buffer_main;
- void *rv = bm->buffer_free_callback;
-
- bm->buffer_free_callback = fp;
- return rv;
-}
-
-static_always_inline void
-recycle_or_free (vlib_main_t * vm, vlib_buffer_main_t * bm, u32 bi,
- vlib_buffer_t * b, u32 follow_buffer_next)
-{
- vlib_buffer_free_list_t *fl;
- u32 flags, next;
-
- fl = pool_elt_at_index (vm->buffer_free_list_pool,
- VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
-
- do
- {
- vlib_buffer_t *nb = vlib_get_buffer (vm, bi);
- flags = nb->flags;
- next = nb->next_buffer;
- if (nb->n_add_refs)
- nb->n_add_refs--;
- else
- {
- vlib_buffer_validate_alloc_free (vm, &bi, 1,
- VLIB_BUFFER_KNOWN_ALLOCATED);
- vlib_buffer_add_to_free_list (vm, fl, bi, 1);
- }
- bi = next;
- }
- while (follow_buffer_next && (flags & VLIB_BUFFER_NEXT_PRESENT));
-}
-
-static_always_inline void
-vlib_buffer_free_inline (vlib_main_t * vm,
- u32 * buffers, u32 n_buffers, u32 follow_buffer_next)
-{
- vlib_buffer_main_t *bm = vm->buffer_main;
- vlib_buffer_t *p, *b0, *b1, *b2, *b3;
- int i = 0;
- u32 (*cb) (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
- u32 follow_buffer_next);
-
- cb = bm->buffer_free_callback;
-
- if (PREDICT_FALSE (cb != 0))
- n_buffers = (*cb) (vm, buffers, n_buffers, follow_buffer_next);
-
- if (!n_buffers)
- return;
-
- while (i + 11 < n_buffers)
- {
- p = vlib_get_buffer (vm, buffers[i + 8]);
- vlib_prefetch_buffer_header (p, LOAD);
- p = vlib_get_buffer (vm, buffers[i + 9]);
- vlib_prefetch_buffer_header (p, LOAD);
- p = vlib_get_buffer (vm, buffers[i + 10]);
- vlib_prefetch_buffer_header (p, LOAD);
- p = vlib_get_buffer (vm, buffers[i + 11]);
- vlib_prefetch_buffer_header (p, LOAD);
-
- b0 = vlib_get_buffer (vm, buffers[i]);
- b1 = vlib_get_buffer (vm, buffers[i + 1]);
- b2 = vlib_get_buffer (vm, buffers[i + 2]);
- b3 = vlib_get_buffer (vm, buffers[i + 3]);
-
- VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
- VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1);
- VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b2);
- VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b3);
-
- recycle_or_free (vm, bm, buffers[i], b0, follow_buffer_next);
- recycle_or_free (vm, bm, buffers[i + 1], b1, follow_buffer_next);
- recycle_or_free (vm, bm, buffers[i + 2], b2, follow_buffer_next);
- recycle_or_free (vm, bm, buffers[i + 3], b3, follow_buffer_next);
-
- i += 4;
- }
-
- while (i < n_buffers)
- {
- b0 = vlib_get_buffer (vm, buffers[i]);
- VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
- recycle_or_free (vm, bm, buffers[i], b0, follow_buffer_next);
- i++;
+ clib_spinlock_lock (&bm->buffer_known_hash_lockp);
+ hash_set (bm->buffer_known_hash, bi, is_free ? VLIB_BUFFER_KNOWN_FREE :
+ VLIB_BUFFER_KNOWN_ALLOCATED);
+ clib_spinlock_unlock (&bm->buffer_known_hash_lockp);
}
}
-static void
-vlib_buffer_free_internal (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
-{
- vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
- 1);
-}
-
-static void
-vlib_buffer_free_no_next_internal (vlib_main_t * vm, u32 * buffers,
- u32 n_buffers)
-{
- vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
- 0);
-}
-
void
vlib_packet_template_init (vlib_main_t * vm,
vlib_packet_template_t * t,
@@ -624,7 +371,6 @@ vlib_packet_template_init (vlib_main_t * vm,
vec_add (t->packet_data, packet_data, n_packet_data_bytes);
t->min_n_buffers_each_alloc = min_n_buffers_each_alloc;
-
vlib_worker_thread_barrier_release (vm);
}
@@ -706,8 +452,6 @@ out_of_buffers:
u16
vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
- vlib_buffer_free_list_index_t
- free_list_index,
vlib_buffer_t * first,
vlib_buffer_t ** last, void *data,
u16 data_len)
@@ -721,9 +465,8 @@ vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
u16 max = n_buffer_bytes - l->current_length - l->current_data;
if (max == 0)
{
- if (1 !=
- vlib_buffer_alloc_from_free_list (vm, &l->next_buffer, 1,
- free_list_index))
+ if (1 != vlib_buffer_alloc_from_pool (vm, &l->next_buffer, 1,
+ first->buffer_pool_index))
return copied;
*last = l = vlib_buffer_chain_buffer (vm, l, l->next_buffer);
max = n_buffer_bytes - l->current_length - l->current_data;
@@ -739,14 +482,26 @@ vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
return copied;
}
-u8
-vlib_buffer_register_physmem_map (vlib_main_t * vm, u32 physmem_map_index)
+clib_error_t *
+vlib_buffer_pool_create (vlib_main_t * vm, u8 index, char *name,
+ u32 data_size, u32 physmem_map_index)
{
vlib_buffer_main_t *bm = vm->buffer_main;
- vlib_buffer_pool_t *p;
+ vlib_buffer_pool_t *bp;
vlib_physmem_map_t *m = vlib_physmem_get_map (vm, physmem_map_index);
uword start = pointer_to_uword (m->base);
uword size = (uword) m->n_pages << m->log2_page_size;
+ u32 alloc_size, n_alloc_per_page;;
+
+ vec_validate_aligned (bm->buffer_pools, index, CLIB_CACHE_LINE_BYTES);
+ bp = vec_elt_at_index (bm->buffer_pools, index);
+
+ if (bp->start)
+ return clib_error_return (0, "buffer with index %u already exists",
+ index);
+
+ if (index >= 255)
+ return clib_error_return (0, "buffer index must be < 255", index);
if (bm->buffer_mem_size == 0)
{
@@ -773,36 +528,77 @@ vlib_buffer_register_physmem_map (vlib_main_t * vm, u32 physmem_map_index)
clib_panic ("buffer memory size out of range!");
}
- vec_add2 (bm->buffer_pools, p, 1);
- p->start = start;
- p->size = size;
- p->physmem_map_index = physmem_map_index;
+ bp->start = start;
+ bp->size = size;
+ bp->index = bp - bm->buffer_pools;
+ bp->buffer_template.buffer_pool_index = bp->index;
+ bp->buffer_template.ref_count = 1;
+ bp->physmem_map_index = physmem_map_index;
+ bp->name = format (0, "%s%c", name, 0);
+ bp->data_size = data_size;
+ bp->numa_node = m->numa_node;
- ASSERT (p - bm->buffer_pools < 256);
- return p - bm->buffer_pools;
-}
+ vec_validate_aligned (bp->threads, vec_len (vlib_mains) - 1,
+ CLIB_CACHE_LINE_BYTES);
-static u8 *
-format_vlib_buffer_free_list (u8 * s, va_list * va)
-{
- vlib_buffer_free_list_t *f = va_arg (*va, vlib_buffer_free_list_t *);
- u32 threadnum = va_arg (*va, u32);
- uword bytes_alloc, bytes_free, n_free, size;
+ alloc_size = data_size + sizeof (vlib_buffer_t) + bm->ext_hdr_size;
+ n_alloc_per_page = (1ULL << m->log2_page_size) / alloc_size;
+
+ /* preallocate buffer indices memory */
+ vec_validate_aligned (bp->buffers, m->n_pages * n_alloc_per_page,
+ CLIB_CACHE_LINE_BYTES);
+ vec_reset_length (bp->buffers);
+
+ clib_spinlock_init (&bp->lock);
+
+ while (1)
+ {
+ u8 *p;
+ u32 bi;
+
+ p = vlib_physmem_alloc_from_map (vm, bp->physmem_map_index, alloc_size,
+ CLIB_CACHE_LINE_BYTES);
+
+ if (p == 0)
+ break;
+
+ p += bm->ext_hdr_size;
+
+ vlib_buffer_copy_template ((vlib_buffer_t *) p, &bp->buffer_template);
- if (!f)
- return format (s, "%=7s%=30s%=12s%=12s%=12s%=12s%=12s%=12s",
- "Thread", "Name", "Index", "Size", "Alloc", "Free",
- "#Alloc", "#Free");
+ bi = vlib_get_buffer_index (vm, (vlib_buffer_t *) p);
- size = sizeof (vlib_buffer_t) + VLIB_BUFFER_DATA_SIZE;
- n_free = vec_len (f->buffers);
- bytes_alloc = size * f->n_alloc;
- bytes_free = size * n_free;
+ vec_add1_aligned (bp->buffers, bi, CLIB_CACHE_LINE_BYTES);
+
+ bp->n_buffers += 1;
+ }
+
+ return 0;
+}
- s = format (s, "%7d%30v%12d%12d%=12U%=12U%=12d%=12d", threadnum,
- f->name, f->index, VLIB_BUFFER_DATA_SIZE,
- format_memory_size, bytes_alloc,
- format_memory_size, bytes_free, f->n_alloc, n_free);
+static u8 *
+format_vlib_buffer_pool (u8 * s, va_list * va)
+{
+ vlib_main_t *vm = va_arg (*va, vlib_main_t *);
+ vlib_buffer_pool_t *bp = va_arg (*va, vlib_buffer_pool_t *);
+ vlib_buffer_pool_thread_t *bpt;
+ u32 cached = 0;
+
+ if (!bp)
+ return format (s, "%-20s%=6s%=6s%=6s%=11s%=6s%=8s%=8s%=8s",
+ "Pool Name", "Index", "NUMA", "Size", "Data Size",
+ "Total", "Avail", "Cached", "Used");
+
+ /* *INDENT-OFF* */
+ vec_foreach (bpt, bp->threads)
+ cached += vec_len (bpt->cached_buffers);
+ /* *INDENT-ON* */
+
+ s = format (s, "%-20s%=6d%=6d%=6u%=11u%=6u%=8u%=8u%=8u",
+ bp->name, bp->index, bp->numa_node, bp->data_size +
+ sizeof (vlib_buffer_t) + vm->buffer_main->ext_hdr_size,
+ bp->data_size, bp->n_buffers, vec_len (bp->buffers), cached,
+ bp->n_buffers - vec_len (bp->buffers) - cached);
return s;
}
@@ -811,25 +607,15 @@ static clib_error_t *
show_buffers (vlib_main_t * vm,
unformat_input_t * input, vlib_cli_command_t * cmd)
{
- vlib_buffer_free_list_t *f;
- vlib_main_t *curr_vm;
- u32 vm_index = 0;
-
- vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, 0, 0);
-
- do
- {
- curr_vm = vlib_mains[vm_index];
+ vlib_buffer_main_t *bm = vm->buffer_main;
+ vlib_buffer_pool_t *bp;
- /* *INDENT-OFF* */
- pool_foreach (f, curr_vm->buffer_free_list_pool, ({
- vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, f, vm_index);
- }));
- /* *INDENT-ON* */
+ vlib_cli_output (vm, "%U", format_vlib_buffer_pool, vm, 0);
- vm_index++;
- }
- while (vm_index < vec_len (vlib_mains));
+ /* *INDENT-OFF* */
+ vec_foreach (bp, bm->buffer_pools)
+ vlib_cli_output (vm, "%U", format_vlib_buffer_pool, vm, bp);
+ /* *INDENT-ON* */
return 0;
}
@@ -843,71 +629,134 @@ VLIB_CLI_COMMAND (show_buffers_command, static) = {
/* *INDENT-ON* */
clib_error_t *
-vlib_buffer_main_init (struct vlib_main_t * vm)
+vlib_buffer_worker_init (vlib_main_t * vm)
{
- vlib_buffer_main_t *bm;
- clib_error_t *error;
- u32 physmem_map_index;
- u8 pool_index;
- int log2_page_size = 0;
-
- buffer_log_default = vlib_log_register_class ("buffer", 0);
-
- bm = vm->buffer_main = clib_mem_alloc (sizeof (bm[0]));
- clib_memset (bm, 0, sizeof (bm[0]));
+ vlib_buffer_main_t *bm = vm->buffer_main;
+ vlib_buffer_pool_t *bp;
- if (vlib_buffer_callbacks)
+ /* *INDENT-OFF* */
+ vec_foreach (bp, bm->buffer_pools)
{
- /* external plugin has registered own buffer callbacks
- so we just copy them and quit */
- clib_memcpy_fast (&bm->cb, vlib_buffer_callbacks,
- sizeof (vlib_buffer_callbacks_t));
- bm->callbacks_registered = 1;
- return 0;
+ clib_spinlock_lock (&bp->lock);
+ vec_validate_aligned (bp->threads, vec_len (vlib_mains) - 1,
+ CLIB_CACHE_LINE_BYTES);
+ clib_spinlock_unlock (&bp->lock);
}
+ /* *INDENT-ON* */
- bm->cb.vlib_buffer_fill_free_list_cb = &vlib_buffer_fill_free_list_internal;
- bm->cb.vlib_buffer_free_cb = &vlib_buffer_free_internal;
- bm->cb.vlib_buffer_free_no_next_cb = &vlib_buffer_free_no_next_internal;
- clib_spinlock_init (&bm->buffer_known_hash_lockp);
+ return 0;
+}
+
+VLIB_WORKER_INIT_FUNCTION (vlib_buffer_worker_init);
+
+static clib_error_t *
+vlib_buffer_main_init_numa_node (struct vlib_main_t *vm, u32 numa_node)
+{
+ vlib_buffer_main_t *bm = vm->buffer_main;
+ clib_error_t *error;
+ u32 physmem_map_index;
+ uword n_pages, pagesize;
+ u32 buffers_per_numa;
+ u32 buffer_size = CLIB_CACHE_LINE_ROUND (bm->ext_hdr_size +
+ sizeof (vlib_buffer_t) +
+ VLIB_BUFFER_DATA_SIZE);
+ u8 *name;
+
+ pagesize = clib_mem_get_default_hugepage_size ();
+ name = format (0, "buffers-numa-%d%c", numa_node, 0);
+
+ buffers_per_numa = bm->buffers_per_numa ? bm->buffers_per_numa :
+ VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA;
retry:
- error = vlib_physmem_shared_map_create (vm, "buffers",
- vlib_buffer_physmem_sz,
- log2_page_size,
- CLIB_PMALLOC_NUMA_LOCAL,
+ n_pages = (buffers_per_numa - 1) / (pagesize / buffer_size) + 1;
+ error = vlib_physmem_shared_map_create (vm, (char *) name,
+ n_pages * pagesize,
+ min_log2 (pagesize), numa_node,
&physmem_map_index);
- if (error && log2_page_size == 0)
+ if (error && pagesize != clib_mem_get_page_size ())
{
- vlib_log_warn (buffer_log_default, "%U", format_clib_error, error);
+ vlib_log_warn (bm->log_default, "%U", format_clib_error, error);
clib_error_free (error);
- vlib_log_warn (buffer_log_default, "falling back to non-hugepage "
+ vlib_log_warn (bm->log_default, "falling back to non-hugepage "
"backed buffer pool");
- log2_page_size = min_log2 (clib_mem_get_page_size ());
+ pagesize = clib_mem_get_page_size ();
+ buffers_per_numa = bm->buffers_per_numa ? bm->buffers_per_numa :
+ VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA_UNPRIV;
goto retry;
}
if (error)
return error;
- pool_index = vlib_buffer_register_physmem_map (vm, physmem_map_index);
- vlib_buffer_pool_t *bp = vlib_buffer_pool_get (vm, pool_index);
- clib_spinlock_init (&bp->lock);
- bp->buffer_size = VLIB_BUFFER_DATA_SIZE + sizeof (vlib_buffer_t);
+ vec_reset_length (name);
+ name = format (name, "default-numa-%d%c", numa_node, 0);
- return 0;
+ return vlib_buffer_pool_create (vm, numa_node, (char *) name,
+ VLIB_BUFFER_DATA_SIZE, physmem_map_index);
+}
+
+clib_error_t *
+vlib_buffer_main_init (struct vlib_main_t *vm)
+{
+ vlib_buffer_main_t *bm;
+ clib_error_t *err;
+ clib_bitmap_t *bmp = 0;
+ u32 numa_node;
+
+ if (vm->buffer_main == 0)
+ {
+ vm->buffer_main = clib_mem_alloc (sizeof (bm[0]));
+ clib_memset (vm->buffer_main, 0, sizeof (bm[0]));
+ }
+
+ bm = vm->buffer_main;
+ bm->log_default = vlib_log_register_class ("buffer", 0);
+ bm->ext_hdr_size = __vlib_buffer_external_hdr_size;
+
+ clib_spinlock_init (&bm->buffer_known_hash_lockp);
+
+ err = clib_sysfs_read ("/sys/devices/system/node/possible", "%U",
+ unformat_bitmap_list, &bmp);
+ if (err)
+ {
+ /* no info from sysfs, assuming that only numa 0 exists */
+ clib_error_free (err);
+ bmp = clib_bitmap_set (bmp, 0, 1);
+ }
+
+ /* *INDENT-OFF* */
+ clib_bitmap_foreach (numa_node, bmp, {
+ if ((err = vlib_buffer_main_init_numa_node(vm, numa_node)))
+ goto done;
+ });
+ /* *INDENT-ON* */
+
+ bm->n_numa_nodes = clib_bitmap_last_set (bmp) + 1;
+
+done:
+ vec_free (bmp);
+ return err;
}
static clib_error_t *
vlib_buffers_configure (vlib_main_t * vm, unformat_input_t * input)
{
- u32 size_in_mb;
+ vlib_buffer_main_t *bm;
+
+ if (vm->buffer_main == 0)
+ {
+ vm->buffer_main = clib_mem_alloc (sizeof (bm[0]));
+ clib_memset (vm->buffer_main, 0, sizeof (bm[0]));
+ }
+
+ bm = vm->buffer_main;
while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
{
- if (unformat (input, "memory-size-in-mb %d", &size_in_mb))
- vlib_buffer_physmem_sz = size_in_mb << 20;
+ if (unformat (input, "buffers-per-numa %u", &bm->buffers_per_numa))
+ ;
else
return unformat_parse_error (input);
}
diff --git a/src/vlib/buffer.h b/src/vlib/buffer.h
index f7fdf208a83..1adde73d4ee 100644
--- a/src/vlib/buffer.h
+++ b/src/vlib/buffer.h
@@ -59,8 +59,6 @@
/* Amount of head buffer data copied to each replica head buffer */
#define VLIB_BUFFER_CLONE_HEAD_SIZE (256)
-typedef u8 vlib_buffer_free_list_index_t;
-
/** \file
vlib buffer structure definition and a few select
access methods. This structure and the buffer allocation
@@ -127,8 +125,8 @@ typedef union
/** Generic flow identifier */
u32 flow_id;
- /** Number of additional references to this buffer. */
- u8 n_add_refs;
+ /** Reference count for this buffer. */
+ volatile u8 ref_count;
/** index of buffer pool this buffer belongs. */
u8 buffer_pool_index;
@@ -367,66 +365,32 @@ vlib_buffer_pull (vlib_buffer_t * b, u8 size)
/* Forward declaration. */
struct vlib_main_t;
-typedef struct vlib_buffer_free_list_t
-{
- /* Template buffer used to initialize first 16 bytes of buffers
- allocated on this free list. */
- vlib_buffer_t buffer_init_template;
-
- /* Our index into vlib_main_t's buffer_free_list_pool. */
- vlib_buffer_free_list_index_t index;
-
- /* Number of buffers to allocate when we need to allocate new buffers */
- u32 min_n_buffers_each_alloc;
-
- /* Total number of buffers allocated from this free list. */
- u32 n_alloc;
-
- /* Vector of free buffers. Each element is a byte offset into I/O heap. */
- u32 *buffers;
-
- /* index of buffer pool used to get / put buffers */
- u8 buffer_pool_index;
-
- /* Free list name. */
- u8 *name;
-
- /* Callback functions to initialize newly allocated buffers.
- If null buffers are zeroed. */
- void (*buffer_init_function) (struct vlib_main_t * vm,
- struct vlib_buffer_free_list_t * fl,
- u32 * buffers, u32 n_buffers);
-
- uword buffer_init_function_opaque;
-} __attribute__ ((aligned (16))) vlib_buffer_free_list_t;
-
-typedef uword (vlib_buffer_fill_free_list_cb_t) (struct vlib_main_t * vm,
- vlib_buffer_free_list_t * fl,
- uword min_free_buffers);
-typedef void (vlib_buffer_free_cb_t) (struct vlib_main_t * vm, u32 * buffers,
- u32 n_buffers);
-typedef void (vlib_buffer_free_no_next_cb_t) (struct vlib_main_t * vm,
- u32 * buffers, u32 n_buffers);
-
typedef struct
{
- vlib_buffer_fill_free_list_cb_t *vlib_buffer_fill_free_list_cb;
- vlib_buffer_free_cb_t *vlib_buffer_free_cb;
- vlib_buffer_free_no_next_cb_t *vlib_buffer_free_no_next_cb;
-} vlib_buffer_callbacks_t;
-
-extern vlib_buffer_callbacks_t *vlib_buffer_callbacks;
-
+ CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
+ u32 *cached_buffers;
+ u32 n_alloc;
+} vlib_buffer_pool_thread_t;
typedef struct
{
CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
uword start;
uword size;
uword log2_page_size;
+ u8 index;
+ u32 numa_node;
u32 physmem_map_index;
- u32 buffer_size;
+ u32 data_size;
+ u32 n_buffers;
u32 *buffers;
+ u8 *name;
clib_spinlock_t lock;
+
+ /* per-thread data */
+ vlib_buffer_pool_thread_t *threads;
+
+ /* buffer metadata template */
+ vlib_buffer_t buffer_template;
} vlib_buffer_pool_t;
typedef struct
@@ -438,36 +402,24 @@ typedef struct
uword buffer_mem_size;
vlib_buffer_pool_t *buffer_pools;
- /* Buffer free callback, for subversive activities */
- u32 (*buffer_free_callback) (struct vlib_main_t * vm,
- u32 * buffers,
- u32 n_buffers, u32 follow_buffer_next);
-#define VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX (0)
-
- /* Hash table mapping buffer size (rounded to next unit of
- sizeof (vlib_buffer_t)) to free list index. */
- uword *free_list_by_size;
-
/* Hash table mapping buffer index into number
0 => allocated but free, 1 => allocated and not-free.
If buffer index is not in hash table then this buffer
has never been allocated. */
uword *buffer_known_hash;
clib_spinlock_t buffer_known_hash_lockp;
+ u32 n_numa_nodes;
- /* Callbacks */
- vlib_buffer_callbacks_t cb;
- int callbacks_registered;
-} vlib_buffer_main_t;
+ /* config */
+ u32 buffers_per_numa;
+ u16 ext_hdr_size;
-u8 vlib_buffer_register_physmem_map (struct vlib_main_t *vm,
- u32 physmem_map_index);
+ /* logging */
+ vlib_log_class_t log_default;
+} vlib_buffer_main_t;
clib_error_t *vlib_buffer_main_init (struct vlib_main_t *vm);
-
-void *vlib_set_buffer_free_callback (struct vlib_main_t *vm, void *fp);
-
/*
*/
@@ -488,23 +440,17 @@ extern void vlib_buffer_trace_trajectory_init (vlib_buffer_t * b);
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
#endif /* VLIB_BUFFER_TRACE_TRAJECTORY */
-#endif /* included_vlib_buffer_h */
+extern u16 __vlib_buffer_external_hdr_size;
+#define VLIB_BUFFER_SET_EXT_HDR_SIZE(x) \
+static void __clib_constructor \
+vnet_buffer_set_ext_hdr_size() \
+{ \
+ if (__vlib_buffer_external_hdr_size) \
+ clib_error ("buffer external header space already set"); \
+ __vlib_buffer_external_hdr_size = CLIB_CACHE_LINE_ROUND (x); \
+}
-#define VLIB_BUFFER_REGISTER_CALLBACKS(x,...) \
- __VA_ARGS__ vlib_buffer_callbacks_t __##x##_buffer_callbacks; \
-static void __vlib_add_buffer_callbacks_t_##x (void) \
- __attribute__((__constructor__)) ; \
-static void __vlib_add_buffer_callbacks_t_##x (void) \
-{ \
- if (vlib_buffer_callbacks) \
- clib_panic ("vlib buffer callbacks already registered"); \
- vlib_buffer_callbacks = &__##x##_buffer_callbacks; \
-} \
-static void __vlib_rm_buffer_callbacks_t_##x (void) \
- __attribute__((__destructor__)) ; \
-static void __vlib_rm_buffer_callbacks_t_##x (void) \
-{ vlib_buffer_callbacks = 0; } \
-__VA_ARGS__ vlib_buffer_callbacks_t __##x##_buffer_callbacks
+#endif /* included_vlib_buffer_h */
/*
* fd.io coding-style-patch-verification: ON
diff --git a/src/vlib/buffer_funcs.h b/src/vlib/buffer_funcs.h
index cedd7d02646..ce8d1ef9078 100644
--- a/src/vlib/buffer_funcs.h
+++ b/src/vlib/buffer_funcs.h
@@ -89,6 +89,13 @@ vlib_buffer_copy_template (vlib_buffer_t * b, vlib_buffer_t * bt)
#endif
}
+always_inline u8
+vlib_buffer_pool_get_default_for_numa (vlib_main_t * vm, u32 numa_node)
+{
+ ASSERT (numa_node < vm->buffer_main->n_numa_nodes);
+ return numa_node;
+}
+
/** \brief Translate array of buffer indices into buffer pointers with offset
@param vm - (vlib_main_t *) vlib main data structure pointer
@@ -394,29 +401,48 @@ vlib_buffer_is_known (vlib_main_t * vm, u32 buffer_index)
return p ? p[0] : VLIB_BUFFER_UNKNOWN;
}
-always_inline void
-vlib_buffer_set_known_state (vlib_main_t * vm, u32 buffer_index,
- vlib_buffer_known_state_t state)
-{
- vlib_buffer_main_t *bm = vm->buffer_main;
-
- clib_spinlock_lock (&bm->buffer_known_hash_lockp);
- hash_set (bm->buffer_known_hash, buffer_index, state);
- clib_spinlock_unlock (&bm->buffer_known_hash_lockp);
-}
-
/* Validates sanity of a single buffer.
Returns format'ed vector with error message if any. */
u8 *vlib_validate_buffer (vlib_main_t * vm, u32 buffer_index,
uword follow_chain);
-always_inline u32
-vlib_buffer_round_size (u32 size)
+static_always_inline vlib_buffer_pool_t *
+vlib_get_buffer_pool (vlib_main_t * vm, u8 buffer_pool_index)
{
- return round_pow2 (size, sizeof (vlib_buffer_t));
+ vlib_buffer_main_t *bm = vm->buffer_main;
+ return vec_elt_at_index (bm->buffer_pools, buffer_pool_index);
}
-/** \brief Allocate buffers from specific freelist into supplied array
+static_always_inline uword
+vlib_buffer_pool_get (vlib_main_t * vm, u8 buffer_pool_index, u32 * buffers,
+ u32 n_buffers)
+{
+ vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, buffer_pool_index);
+ u32 len;
+
+ ASSERT (bp->buffers);
+
+ clib_spinlock_lock (&bp->lock);
+ len = vec_len (bp->buffers);
+ if (PREDICT_TRUE (n_buffers < len))
+ {
+ len -= n_buffers;
+ vlib_buffer_copy_indices (buffers, bp->buffers + len, n_buffers);
+ _vec_len (bp->buffers) = len;
+ clib_spinlock_unlock (&bp->lock);
+ return n_buffers;
+ }
+ else
+ {
+ vlib_buffer_copy_indices (buffers, bp->buffers, len);
+ _vec_len (bp->buffers) = 0;
+ clib_spinlock_unlock (&bp->lock);
+ return len;
+ }
+}
+
+
+/** \brief Allocate buffers from specific pool into supplied array
@param vm - (vlib_main_t *) vlib main data structure pointer
@param buffers - (u32 * ) buffer index array
@@ -424,57 +450,87 @@ vlib_buffer_round_size (u32 size)
@return - (u32) number of buffers actually allocated, may be
less than the number requested or zero
*/
+
always_inline u32
-vlib_buffer_alloc_from_free_list (vlib_main_t * vm,
- u32 * buffers,
- u32 n_buffers,
- vlib_buffer_free_list_index_t index)
+vlib_buffer_alloc_from_pool (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
+ u8 buffer_pool_index)
{
- vlib_buffer_free_list_t *fl;
vlib_buffer_main_t *bm = vm->buffer_main;
- u32 *src;
- uword len;
-
- ASSERT (bm->cb.vlib_buffer_fill_free_list_cb);
+ vlib_buffer_pool_t *bp;
+ vlib_buffer_pool_thread_t *bpt;
+ u32 *src, *dst, len, n_left;
- fl = pool_elt_at_index (vm->buffer_free_list_pool, index);
+ bp = vec_elt_at_index (bm->buffer_pools, buffer_pool_index);
+ bpt = vec_elt_at_index (bp->threads, vm->thread_index);
- len = vec_len (fl->buffers);
+ dst = buffers;
+ n_left = n_buffers;
+ len = vec_len (bpt->cached_buffers);
- if (PREDICT_FALSE (len < n_buffers))
+ /* per-thread cache contains enough buffers */
+ if (len >= n_buffers)
{
- bm->cb.vlib_buffer_fill_free_list_cb (vm, fl, n_buffers);
- if (PREDICT_FALSE ((len = vec_len (fl->buffers)) == 0))
- return 0;
+ src = bpt->cached_buffers + len - n_buffers;
+ vlib_buffer_copy_indices (dst, src, n_buffers);
+ _vec_len (bpt->cached_buffers) -= n_buffers;
- /* even if fill free list didn't manage to refill free list
- we should give what we have */
- n_buffers = clib_min (len, n_buffers);
+ if (CLIB_DEBUG > 0)
+ vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
+ VLIB_BUFFER_KNOWN_FREE);
+ return n_buffers;
+ }
- /* following code is intentionaly duplicated to allow compiler
- to optimize fast path when n_buffers is constant value */
- src = fl->buffers + len - n_buffers;
- vlib_buffer_copy_indices (buffers, src, n_buffers);
- _vec_len (fl->buffers) -= n_buffers;
+ /* take everything available in the cache */
+ if (len)
+ {
+ vlib_buffer_copy_indices (dst, bpt->cached_buffers, len);
+ _vec_len (bpt->cached_buffers) = 0;
+ dst += len;
+ n_left -= len;
+ }
- /* Verify that buffers are known free. */
- vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
- VLIB_BUFFER_KNOWN_FREE);
+ len = round_pow2 (n_left, 32);
+ vec_validate_aligned (bpt->cached_buffers, len - 1, CLIB_CACHE_LINE_BYTES);
+ len = vlib_buffer_pool_get (vm, buffer_pool_index, bpt->cached_buffers,
+ len);
+ _vec_len (bpt->cached_buffers) = len;
- return n_buffers;
+ if (len)
+ {
+ u32 n_copy = clib_min (len, n_left);
+ src = bpt->cached_buffers + len - n_copy;
+ vlib_buffer_copy_indices (dst, src, n_copy);
+ _vec_len (bpt->cached_buffers) -= n_copy;
+ n_left -= n_copy;
}
- src = fl->buffers + len - n_buffers;
- vlib_buffer_copy_indices (buffers, src, n_buffers);
- _vec_len (fl->buffers) -= n_buffers;
+ n_buffers -= n_left;
/* Verify that buffers are known free. */
- vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
- VLIB_BUFFER_KNOWN_FREE);
+ if (CLIB_DEBUG > 0)
+ vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
+ VLIB_BUFFER_KNOWN_FREE);
return n_buffers;
}
+/** \brief Allocate buffers from specific numa node into supplied array
+
+ @param vm - (vlib_main_t *) vlib main data structure pointer
+ @param buffers - (u32 * ) buffer index array
+ @param n_buffers - (u32) number of buffers requested
+ @param numa_node - (u32) numa node
+ @return - (u32) number of buffers actually allocated, may be
+ less than the number requested or zero
+*/
+always_inline u32
+vlib_buffer_alloc_on_numa (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
+ u32 numa_node)
+{
+ u8 index = vlib_buffer_pool_get_default_for_numa (vm, numa_node);
+ return vlib_buffer_alloc_from_pool (vm, buffers, n_buffers, index);
+}
+
/** \brief Allocate buffers into supplied array
@param vm - (vlib_main_t *) vlib main data structure pointer
@@ -483,11 +539,11 @@ vlib_buffer_alloc_from_free_list (vlib_main_t * vm,
@return - (u32) number of buffers actually allocated, may be
less than the number requested or zero
*/
+
always_inline u32
vlib_buffer_alloc (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
{
- return vlib_buffer_alloc_from_free_list (vm, buffers, n_buffers,
- VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
+ return vlib_buffer_alloc_on_numa (vm, buffers, n_buffers, vm->numa_node);
}
/** \brief Allocate buffers into ring
@@ -519,6 +575,215 @@ vlib_buffer_alloc_to_ring (vlib_main_t * vm, u32 * ring, u32 start,
return n_alloc;
}
+/** \brief Allocate buffers into ring from specific buffer pool
+
+ @param vm - (vlib_main_t *) vlib main data structure pointer
+ @param buffers - (u32 * ) buffer index ring
+ @param start - (u32) first slot in the ring
+ @param ring_size - (u32) ring size
+ @param n_buffers - (u32) number of buffers requested
+ @return - (u32) number of buffers actually allocated, may be
+ less than the number requested or zero
+*/
+always_inline u32
+vlib_buffer_alloc_to_ring_from_pool (vlib_main_t * vm, u32 * ring, u32 start,
+ u32 ring_size, u32 n_buffers,
+ u8 buffer_pool_index)
+{
+ u32 n_alloc;
+
+ ASSERT (n_buffers <= ring_size);
+
+ if (PREDICT_TRUE (start + n_buffers <= ring_size))
+ return vlib_buffer_alloc_from_pool (vm, ring + start, n_buffers,
+ buffer_pool_index);
+
+ n_alloc = vlib_buffer_alloc_from_pool (vm, ring + start, ring_size - start,
+ buffer_pool_index);
+
+ if (PREDICT_TRUE (n_alloc == ring_size - start))
+ n_alloc += vlib_buffer_alloc_from_pool (vm, ring, n_buffers - n_alloc,
+ buffer_pool_index);
+
+ return n_alloc;
+}
+
+static void
+vlib_buffer_pool_put (vlib_main_t * vm, u8 buffer_pool_index,
+ u32 * buffers, u32 n_buffers)
+{
+ vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, buffer_pool_index);
+ vlib_buffer_pool_thread_t *bpt =
+ vec_elt_at_index (bp->threads, vm->thread_index);
+
+ vec_add_aligned (bpt->cached_buffers, buffers, n_buffers,
+ CLIB_CACHE_LINE_BYTES);
+
+ if (vec_len (bpt->cached_buffers) > 4 * VLIB_FRAME_SIZE)
+ {
+ clib_spinlock_lock (&bp->lock);
+ /* keep last stored buffers, as they are more likely hot in the cache */
+ vec_add_aligned (bp->buffers, bpt->cached_buffers, VLIB_FRAME_SIZE,
+ CLIB_CACHE_LINE_BYTES);
+ vec_delete (bpt->cached_buffers, VLIB_FRAME_SIZE, 0);
+ bpt->n_alloc -= VLIB_FRAME_SIZE;
+ clib_spinlock_unlock (&bp->lock);
+ }
+}
+
+static_always_inline void
+vlib_buffer_free_inline (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
+ int maybe_next)
+{
+ const int queue_size = 128;
+ vlib_buffer_pool_t *bp = 0;
+ u8 buffer_pool_index = ~0;
+ u32 n_queue = 0, queue[queue_size + 4];
+ vlib_buffer_t bt = { };
+#if defined(CLIB_HAVE_VEC128) && !__aarch64__
+ vlib_buffer_t bpi_mask = {.buffer_pool_index = ~0 };
+ vlib_buffer_t bpi_vec = {.buffer_pool_index = ~0 };
+ vlib_buffer_t flags_refs_mask = {
+ .flags = VLIB_BUFFER_NEXT_PRESENT,
+ .ref_count = ~0
+ };
+#endif
+
+ while (n_buffers)
+ {
+ vlib_buffer_t *b[8];
+ u32 bi, sum = 0, flags, next;
+
+ if (n_buffers < 12)
+ goto one_by_one;
+
+ vlib_get_buffers (vm, buffers, b, 4);
+ vlib_get_buffers (vm, buffers + 8, b + 4, 4);
+
+ vlib_prefetch_buffer_header (b[4], LOAD);
+ vlib_prefetch_buffer_header (b[5], LOAD);
+ vlib_prefetch_buffer_header (b[6], LOAD);
+ vlib_prefetch_buffer_header (b[7], LOAD);
+
+#if defined(CLIB_HAVE_VEC128) && !__aarch64__
+ u8x16 p0, p1, p2, p3, r;
+ p0 = u8x16_load_unaligned (b[0]);
+ p1 = u8x16_load_unaligned (b[1]);
+ p2 = u8x16_load_unaligned (b[2]);
+ p3 = u8x16_load_unaligned (b[3]);
+
+ r = p0 ^ bpi_vec.as_u8x16[0];
+ r |= p1 ^ bpi_vec.as_u8x16[0];
+ r |= p2 ^ bpi_vec.as_u8x16[0];
+ r |= p3 ^ bpi_vec.as_u8x16[0];
+ r &= bpi_mask.as_u8x16[0];
+ r |= (p0 | p1 | p2 | p3) & flags_refs_mask.as_u8x16[0];
+
+ sum = !u8x16_is_all_zero (r);
+#else
+ sum |= b[0]->flags;
+ sum |= b[1]->flags;
+ sum |= b[2]->flags;
+ sum |= b[3]->flags;
+ sum &= VLIB_BUFFER_NEXT_PRESENT;
+ sum += b[0]->ref_count - 1;
+ sum += b[1]->ref_count - 1;
+ sum += b[2]->ref_count - 1;
+ sum += b[3]->ref_count - 1;
+ sum |= b[0]->buffer_pool_index ^ buffer_pool_index;
+ sum |= b[1]->buffer_pool_index ^ buffer_pool_index;
+ sum |= b[2]->buffer_pool_index ^ buffer_pool_index;
+ sum |= b[3]->buffer_pool_index ^ buffer_pool_index;
+#endif
+
+ if (sum)
+ goto one_by_one;
+
+ vlib_buffer_copy_indices (queue + n_queue, buffers, 4);
+ vlib_buffer_copy_template (b[0], &bt);
+ vlib_buffer_copy_template (b[1], &bt);
+ vlib_buffer_copy_template (b[2], &bt);
+ vlib_buffer_copy_template (b[3], &bt);
+ n_queue += 4;
+
+ if (CLIB_DEBUG > 0)
+ vlib_buffer_validate_alloc_free (vm, buffers, 4,
+ VLIB_BUFFER_KNOWN_ALLOCATED);
+
+ VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
+ VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[1]);
+ VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[2]);
+ VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[3]);
+
+ if (n_queue >= queue_size)
+ {
+ vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
+ n_queue = 0;
+ }
+ buffers += 4;
+ n_buffers -= 4;
+ continue;
+
+ one_by_one:
+ bi = buffers[0];
+
+ next_in_chain:
+ b[0] = vlib_get_buffer (vm, bi);
+ flags = b[0]->flags;
+ next = b[0]->next_buffer;
+
+ if (PREDICT_FALSE (buffer_pool_index != b[0]->buffer_pool_index))
+ {
+ buffer_pool_index = b[0]->buffer_pool_index;
+#if defined(CLIB_HAVE_VEC128) && !__aarch64__
+ bpi_vec.buffer_pool_index = buffer_pool_index;
+#endif
+ bp = vlib_get_buffer_pool (vm, buffer_pool_index);
+ vlib_buffer_copy_template (&bt, &bp->buffer_template);
+
+ if (n_queue)
+ {
+ vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
+ n_queue = 0;
+ }
+ }
+
+ ASSERT (pointer_to_uword (b[0]) >= bp->start &&
+ pointer_to_uword (b[0]) <
+ bp->start + bp->size - (bp->data_size + sizeof (*b[0])));
+
+ VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
+
+ if (clib_atomic_sub_fetch (&b[0]->ref_count, 1) == 0)
+ {
+ if (CLIB_DEBUG > 0)
+ vlib_buffer_validate_alloc_free (vm, &bi, 1,
+ VLIB_BUFFER_KNOWN_ALLOCATED);
+ vlib_buffer_copy_template (b[0], &bt);
+ queue[n_queue++] = bi;
+ }
+
+ if (n_queue == queue_size)
+ {
+ vlib_buffer_pool_put (vm, buffer_pool_index, queue, queue_size);
+ n_queue = 0;
+ }
+
+ if (flags & VLIB_BUFFER_NEXT_PRESENT)
+ {
+ bi = next;
+ goto next_in_chain;
+ }
+
+ buffers++;
+ n_buffers--;
+ }
+
+ if (n_queue)
+ vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
+}
+
+
/** \brief Free buffers
Frees the entire buffer chain for each buffer
@@ -534,11 +799,7 @@ vlib_buffer_free (vlib_main_t * vm,
/* number of buffers to free */
u32 n_buffers)
{
- vlib_buffer_main_t *bm = vm->buffer_main;
-
- ASSERT (bm->cb.vlib_buffer_free_cb);
-
- return bm->cb.vlib_buffer_free_cb (vm, buffers, n_buffers);
+ vlib_buffer_free_inline (vm, buffers, n_buffers, /* maybe next */ 1);
}
/** \brief Free buffers, does not free the buffer chain for each buffer
@@ -555,11 +816,7 @@ vlib_buffer_free_no_next (vlib_main_t * vm,
/* number of buffers to free */
u32 n_buffers)
{
- vlib_buffer_main_t *bm = vm->buffer_main;
-
- ASSERT (bm->cb.vlib_buffer_free_no_next_cb);
-
- return bm->cb.vlib_buffer_free_no_next_cb (vm, buffers, n_buffers);
+ vlib_buffer_free_inline (vm, buffers, n_buffers, /* maybe next */ 0);
}
/** \brief Free one buffer
@@ -571,7 +828,7 @@ vlib_buffer_free_no_next (vlib_main_t * vm,
always_inline void
vlib_buffer_free_one (vlib_main_t * vm, u32 buffer_index)
{
- vlib_buffer_free (vm, &buffer_index, /* n_buffers */ 1);
+ vlib_buffer_free_inline (vm, &buffer_index, 1, /* maybe next */ 1);
}
/** \brief Free buffers from ring
@@ -624,11 +881,6 @@ vlib_buffer_free_from_ring_no_next (vlib_main_t * vm, u32 * ring, u32 start,
}
}
-/* Add/delete buffer free lists. */
-vlib_buffer_free_list_index_t vlib_buffer_create_free_list (vlib_main_t * vm,
- u32 n_data_bytes,
- char *fmt, ...);
-
/* Append given data to end of buffer, possibly allocating new buffers. */
int vlib_buffer_add_data (vlib_main_t * vm, u32 * buffer_index, void *data,
u32 n_data_bytes);
@@ -710,7 +962,7 @@ vlib_buffer_clone_256 (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
u16 i;
vlib_buffer_t *s = vlib_get_buffer (vm, src_buffer);
- ASSERT (s->n_add_refs == 0);
+ ASSERT (s->ref_count == 1);
ASSERT (n_buffers);
ASSERT (n_buffers <= 256);
@@ -735,13 +987,16 @@ vlib_buffer_clone_256 (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
return 1;
}
- n_buffers = vlib_buffer_alloc (vm, buffers, n_buffers);
+ n_buffers = vlib_buffer_alloc_from_pool (vm, buffers, n_buffers,
+ s->buffer_pool_index);
for (i = 0; i < n_buffers; i++)
{
vlib_buffer_t *d = vlib_get_buffer (vm, buffers[i]);
d->current_data = s->current_data;
d->current_length = head_end_offset;
+ ASSERT (d->buffer_pool_index == s->buffer_pool_index);
+
d->total_length_not_including_first_buffer = s->current_length -
head_end_offset;
if (PREDICT_FALSE (s->flags & VLIB_BUFFER_NEXT_PRESENT))
@@ -758,11 +1013,11 @@ vlib_buffer_clone_256 (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
d->next_buffer = src_buffer;
}
vlib_buffer_advance (s, head_end_offset);
- s->n_add_refs = n_buffers - 1;
+ s->ref_count = n_buffers;
while (s->flags & VLIB_BUFFER_NEXT_PRESENT)
{
s = vlib_get_buffer (vm, s->next_buffer);
- s->n_add_refs = n_buffers - 1;
+ s->ref_count = n_buffers;
}
return n_buffers;
@@ -816,6 +1071,7 @@ vlib_buffer_attach_clone (vlib_main_t * vm, vlib_buffer_t * head,
vlib_buffer_t * tail)
{
ASSERT ((head->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
+ ASSERT (head->buffer_pool_index == tail->buffer_pool_index);
head->flags |= VLIB_BUFFER_NEXT_PRESENT;
head->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
@@ -826,7 +1082,7 @@ vlib_buffer_attach_clone (vlib_main_t * vm, vlib_buffer_t * head,
tail->total_length_not_including_first_buffer;
next_segment:
- clib_atomic_add_fetch (&tail->n_add_refs, 1);
+ clib_atomic_add_fetch (&tail->ref_count, 1);
if (tail->flags & VLIB_BUFFER_NEXT_PRESENT)
{
@@ -875,7 +1131,6 @@ vlib_buffer_chain_increase_length (vlib_buffer_t * first,
* Returns the number of copied bytes. */
always_inline u16
vlib_buffer_chain_append_data (vlib_main_t * vm,
- vlib_buffer_free_list_index_t free_list_index,
vlib_buffer_t * first,
vlib_buffer_t * last, void *data, u16 data_len)
{
@@ -897,8 +1152,6 @@ vlib_buffer_chain_append_data (vlib_main_t * vm,
* chained and points to the last buffer in the chain. */
u16
vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
- vlib_buffer_free_list_index_t
- free_list_index,
vlib_buffer_t * first,
vlib_buffer_t ** last, void *data,
u16 data_len);
@@ -915,17 +1168,9 @@ typedef struct
/* Number of buffers to allocate in each call to allocator. */
u32 min_n_buffers_each_alloc;
- /* Buffer free list for this template. */
- vlib_buffer_free_list_index_t free_list_index;
-
- u32 *free_buffers;
-
u8 *name;
} vlib_packet_template_t;
-void vlib_packet_template_get_packet_helper (vlib_main_t * vm,
- vlib_packet_template_t * t);
-
void vlib_packet_template_init (vlib_main_t * vm,
vlib_packet_template_t * t,
void *packet_data,
@@ -943,112 +1188,9 @@ vlib_packet_template_free (vlib_main_t * vm, vlib_packet_template_t * t)
vec_free (t->packet_data);
}
-static_always_inline vlib_buffer_pool_t *
-vlib_buffer_pool_get (vlib_main_t * vm, u8 buffer_pool_index)
-{
- vlib_buffer_main_t *bm = vm->buffer_main;
- return vec_elt_at_index (bm->buffer_pools, buffer_pool_index);
-}
-
-always_inline void
-vlib_buffer_add_to_free_list (vlib_main_t * vm,
- vlib_buffer_free_list_t * f,
- u32 buffer_index, u8 do_init)
-{
- vlib_buffer_pool_t *bp = vlib_buffer_pool_get (vm, f->buffer_pool_index);
- vlib_buffer_t *b;
- b = vlib_get_buffer (vm, buffer_index);
- if (PREDICT_TRUE (do_init))
- clib_memset (b, 0, STRUCT_OFFSET_OF (vlib_buffer_t, template_end));
- vec_add1_aligned (f->buffers, buffer_index, CLIB_CACHE_LINE_BYTES);
-
- if (vec_len (f->buffers) > 4 * VLIB_FRAME_SIZE)
- {
- clib_spinlock_lock (&bp->lock);
- /* keep last stored buffers, as they are more likely hot in the cache */
- vec_add_aligned (bp->buffers, f->buffers, VLIB_FRAME_SIZE,
- CLIB_CACHE_LINE_BYTES);
- vec_delete (f->buffers, VLIB_FRAME_SIZE, 0);
- f->n_alloc -= VLIB_FRAME_SIZE;
- clib_spinlock_unlock (&bp->lock);
- }
-}
-
-#if CLIB_DEBUG > 0
-extern u32 *vlib_buffer_state_validation_lock;
-extern uword *vlib_buffer_state_validation_hash;
-extern void *vlib_buffer_state_heap;
-#endif
-
-static inline void
-vlib_validate_buffer_in_use (vlib_buffer_t * b, u32 expected)
-{
-#if CLIB_DEBUG > 0
- uword *p;
- void *oldheap;
-
- oldheap = clib_mem_set_heap (vlib_buffer_state_heap);
-
- while (clib_atomic_test_and_set (vlib_buffer_state_validation_lock))
- ;
-
- p = hash_get (vlib_buffer_state_validation_hash, b);
-
- /* If we don't know about b, declare it to be in the expected state */
- if (!p)
- {
- hash_set (vlib_buffer_state_validation_hash, b, expected);
- goto out;
- }
-
- if (p[0] != expected)
- {
- void cj_stop (void);
- u32 bi;
- vlib_main_t *vm = &vlib_global_main;
-
- cj_stop ();
-
- bi = vlib_get_buffer_index (vm, b);
-
- clib_mem_set_heap (oldheap);
- clib_warning ("%.6f buffer %llx (%d): %s, not %s",
- vlib_time_now (vm), bi,
- p[0] ? "busy" : "free", expected ? "busy" : "free");
- os_panic ();
- }
-out:
- CLIB_MEMORY_BARRIER ();
- *vlib_buffer_state_validation_lock = 0;
- clib_mem_set_heap (oldheap);
-#endif
-}
-
-static inline void
-vlib_validate_buffer_set_in_use (vlib_buffer_t * b, u32 expected)
-{
-#if CLIB_DEBUG > 0
- void *oldheap;
-
- oldheap = clib_mem_set_heap (vlib_buffer_state_heap);
-
- while (clib_atomic_test_and_set (vlib_buffer_state_validation_lock))
- ;
-
- hash_set (vlib_buffer_state_validation_hash, b, expected);
-
- CLIB_MEMORY_BARRIER ();
- *vlib_buffer_state_validation_lock = 0;
- clib_mem_set_heap (oldheap);
-#endif
-}
-
-/** minimum data size of first buffer in a buffer chain */
-#define VLIB_BUFFER_CHAIN_MIN_FIRST_DATA_SIZE (256)
-
/**
* @brief compress buffer chain in a way where the first buffer is at least
- * VLIB_BUFFER_CHAIN_MIN_FIRST_DATA_SIZE long
+ * VLIB_BUFFER_CLONE_HEAD_SIZE long
*
* @param[in] vm - vlib_main
* @param[in,out] first - first buffer in chain
@@ -1059,14 +1201,14 @@ always_inline void
vlib_buffer_chain_compress (vlib_main_t * vm,
vlib_buffer_t * first, u32 ** discard_vector)
{
- if (first->current_length >= VLIB_BUFFER_CHAIN_MIN_FIRST_DATA_SIZE ||
+ if (first->current_length >= VLIB_BUFFER_CLONE_HEAD_SIZE ||
!(first->flags & VLIB_BUFFER_NEXT_PRESENT))
{
/* this is already big enough or not a chain */
return;
}
- u32 want_first_size = clib_min (VLIB_BUFFER_CHAIN_MIN_FIRST_DATA_SIZE,
+ u32 want_first_size = clib_min (VLIB_BUFFER_CLONE_HEAD_SIZE,
VLIB_BUFFER_DATA_SIZE -
first->current_data);
do
diff --git a/src/vlib/log.h b/src/vlib/log.h
index d21bca9f195..9b285429b9a 100644
--- a/src/vlib/log.h
+++ b/src/vlib/log.h
@@ -18,8 +18,6 @@
#include <vppinfra/types.h>
-typedef u32 vlib_log_class_t;
-
#define foreach_vlib_log_level \
_(0, EMERG, emerg) \
_(1, ALERT, alert) \
diff --git a/src/vlib/main.c b/src/vlib/main.c
index a64a991d438..c0ab8e129b9 100644
--- a/src/vlib/main.c
+++ b/src/vlib/main.c
@@ -463,7 +463,7 @@ vlib_put_next_frame (vlib_main_t * vm,
vlib_frame_t *f;
u32 n_vectors_in_frame;
- if (vm->buffer_main->callbacks_registered == 0 && CLIB_DEBUG > 0)
+ if (CLIB_DEBUG > 0)
vlib_put_next_frame_validate (vm, r, next_index, n_vectors_left);
nf = vlib_node_runtime_get_next_frame (vm, r, next_index);
@@ -987,8 +987,8 @@ format_buffer_metadata (u8 * s, va_list * args)
(i32) (b->current_data), (i32) (b->current_length));
s = format (s, "current_config_index: %d, flow_id: %x, next_buffer: %x\n",
b->current_config_index, b->flow_id, b->next_buffer);
- s = format (s, "error: %d, n_add_refs: %d, buffer_pool_index: %d\n",
- (u32) (b->error), (u32) (b->n_add_refs),
+ s = format (s, "error: %d, ref_count: %d, buffer_pool_index: %d\n",
+ (u32) (b->error), (u32) (b->ref_count),
(u32) (b->buffer_pool_index));
s = format (s,
"trace_index: %d, len_not_first_buf: %d\n",
@@ -1993,9 +1993,6 @@ vlib_main (vlib_main_t * volatile vm, unformat_input_t * input)
if ((error = vlib_call_all_init_functions (vm)))
goto done;
- /* Create default buffer free list. */
- vlib_buffer_create_free_list (vm, VLIB_BUFFER_DATA_SIZE, "default");
-
nm->timing_wheel = clib_mem_alloc_aligned (sizeof (TWT (tw_timer_wheel)),
CLIB_CACHE_LINE_BYTES);
diff --git a/src/vlib/main.h b/src/vlib/main.h
index c6786d8f788..920e5f3f95a 100644
--- a/src/vlib/main.h
+++ b/src/vlib/main.h
@@ -115,9 +115,6 @@ typedef struct vlib_main_t
/* Size of the heap */
uword heap_size;
- /* Pool of buffer free lists. */
- vlib_buffer_free_list_t *buffer_free_list_pool;
-
/* buffer main structure. */
vlib_buffer_main_t *buffer_main;
diff --git a/src/vlib/threads.c b/src/vlib/threads.c
index ed1ba61a0cc..cdb4eb082b5 100644
--- a/src/vlib/threads.c
+++ b/src/vlib/threads.c
@@ -719,8 +719,6 @@ start_workers (vlib_main_t * vm)
for (i = 0; i < vec_len (tm->registrations); i++)
{
vlib_node_main_t *nm, *nm_clone;
- vlib_buffer_free_list_t *fl_clone, *fl_orig;
- vlib_buffer_free_list_t *orig_freelist_pool;
int k;
tr = tm->registrations[i];
@@ -883,26 +881,6 @@ start_workers (vlib_main_t * vm)
(vlib_mains[0]->error_main.counters_last_clear,
CLIB_CACHE_LINE_BYTES);
- /* Fork the vlib_buffer_main_t free lists, etc. */
- orig_freelist_pool = vm_clone->buffer_free_list_pool;
- vm_clone->buffer_free_list_pool = 0;
-
- /* *INDENT-OFF* */
- pool_foreach (fl_orig, orig_freelist_pool,
- ({
- pool_get_aligned (vm_clone->buffer_free_list_pool,
- fl_clone, CLIB_CACHE_LINE_BYTES);
- ASSERT (fl_orig - orig_freelist_pool
- == fl_clone - vm_clone->buffer_free_list_pool);
-
- fl_clone[0] = fl_orig[0];
- fl_clone->buffers = 0;
- vec_validate(fl_clone->buffers, 0);
- vec_reset_length(fl_clone->buffers);
- fl_clone->n_alloc = 0;
- }));
-/* *INDENT-ON* */
-
worker_thread_index++;
}
}
diff --git a/src/vlib/vlib.h b/src/vlib/vlib.h
index 49bb51bfbe3..2149c4763ab 100644
--- a/src/vlib/vlib.h
+++ b/src/vlib/vlib.h
@@ -48,6 +48,7 @@
/* Forward declarations of structs to avoid circular dependencies. */
struct vlib_main_t;
+typedef u32 vlib_log_class_t;
/* All includes in alphabetical order. */
#include <vlib/physmem.h>