diff options
author | Damjan Marion <damarion@cisco.com> | 2018-03-13 21:32:17 +0100 |
---|---|---|
committer | Dave Barach <openvpp@barachs.net> | 2018-03-14 16:38:06 +0000 |
commit | d1274cb207a48f0142a5a6cbcd22d61c22dbc230 (patch) | |
tree | bad838ef9e2d89271f7222b341935f6d1958af8a /src | |
parent | 1f15facc74b1313dd680cf20eb6f490cc743b2a1 (diff) |
vlib: internal buffer manager rework
- buffer_main is no longer part of vlib_main_t
- pool of free lists is still part of vlib_main_t
- mheap is not used anymore for buffer allocation
- simple bitmap bassed buffer alloc scheme is introduced
Change-Id: I3e1e6d00e2c8122293ed0a741245eb841315a1ff
Signed-off-by: Damjan Marion <damarion@cisco.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/dpdk/buffer.c | 48 | ||||
-rw-r--r-- | src/vlib/buffer.c | 275 | ||||
-rw-r--r-- | src/vlib/buffer.h | 57 | ||||
-rw-r--r-- | src/vlib/buffer_funcs.h | 64 | ||||
-rw-r--r-- | src/vlib/main.c | 7 | ||||
-rw-r--r-- | src/vlib/main.h | 6 | ||||
-rw-r--r-- | src/vlib/threads.c | 12 | ||||
-rw-r--r-- | src/vnet/replication.c | 5 |
8 files changed, 190 insertions, 284 deletions
diff --git a/src/plugins/dpdk/buffer.c b/src/plugins/dpdk/buffer.c index 47d66484b3d..20036f2b764 100644 --- a/src/plugins/dpdk/buffer.c +++ b/src/plugins/dpdk/buffer.c @@ -162,6 +162,8 @@ del_free_list (vlib_main_t * vm, vlib_buffer_free_list_t * f) vec_free (f->name); vec_free (f->buffers); + /* Poison it. */ + memset (f, 0xab, sizeof (f[0])); } /* Add buffer free list. */ @@ -169,37 +171,23 @@ static void dpdk_buffer_delete_free_list (vlib_main_t * vm, vlib_buffer_free_list_index_t free_list_index) { - vlib_buffer_main_t *bm = vm->buffer_main; vlib_buffer_free_list_t *f; - vlib_buffer_free_list_index_t merge_index; int i; ASSERT (vlib_get_thread_index () == 0); f = vlib_buffer_get_free_list (vm, free_list_index); - merge_index = vlib_buffer_get_free_list_with_size (vm, f->n_data_bytes); - if (merge_index != (vlib_buffer_free_list_index_t) ~ 0 && - merge_index != free_list_index) - { - vlib_buffer_merge_free_lists (pool_elt_at_index - (bm->buffer_free_list_pool, merge_index), - f); - } - del_free_list (vm, f); - /* Poison it. */ - memset (f, 0xab, sizeof (f[0])); - - pool_put (bm->buffer_free_list_pool, f); + pool_put (vm->buffer_free_list_pool, f); for (i = 1; i < vec_len (vlib_mains); i++) { - bm = vlib_mains[i]->buffer_main; - f = vlib_buffer_get_free_list (vlib_mains[i], free_list_index);; - memset (f, 0xab, sizeof (f[0])); - pool_put (bm->buffer_free_list_pool, f); + vlib_main_t *wvm = vlib_mains[i]; + f = vlib_buffer_get_free_list (vlib_mains[i], free_list_index); + del_free_list (wvm, f); + pool_put (wvm->buffer_free_list_pool, f); } } #endif @@ -233,7 +221,7 @@ CLIB_MULTIARCH_FN (dpdk_buffer_fill_free_list) (vlib_main_t * vm, 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_physmem_alloc); + n = clib_max (n, fl->min_n_buffers_each_alloc); vec_validate_aligned (vm->mbuf_alloc_list, n - 1, CLIB_CACHE_LINE_BYTES); @@ -341,12 +329,12 @@ recycle_or_free (vlib_main_t * vm, vlib_buffer_main_t * bm, u32 bi, vlib_buffer_add_to_free_list (vm, fl, bi, (b->flags & VLIB_BUFFER_RECYCLE) == 0); - for (j = 0; j < vec_len (bm->announce_list); j++) + for (j = 0; j < vec_len (vm->buffer_announce_list); j++) { - if (fl == bm->announce_list[j]) + if (fl == vm->buffer_announce_list[j]) goto already_announced; } - vec_add1 (bm->announce_list, fl); + vec_add1 (vm->buffer_announce_list, fl); already_announced: ; } @@ -361,7 +349,7 @@ 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_main_t *bm = &buffer_main; dpdk_buffer_main_t *dbm = &dpdk_buffer_main; vlib_buffer_t *b0, *b1, *b2, *b3; u32 thread_index = vlib_get_thread_index (); @@ -409,15 +397,15 @@ vlib_buffer_free_inline (vlib_main_t * vm, recycle_or_free (vm, bm, buffers[i], b0); i++; } - if (vec_len (bm->announce_list)) + if (vec_len (vm->buffer_announce_list)) { vlib_buffer_free_list_t *fl; - for (i = 0; i < vec_len (bm->announce_list); i++) + for (i = 0; i < vec_len (vm->buffer_announce_list); i++) { - fl = bm->announce_list[i]; + fl = vm->buffer_announce_list[i]; fl->buffers_added_to_freelist_function (vm, fl); } - _vec_len (bm->announce_list) = 0; + _vec_len (vm->buffer_announce_list) = 0; } vec_foreach_index (i, d->mbuf_pending_free_list) @@ -454,7 +442,7 @@ dpdk_packet_template_init (vlib_main_t * vm, void *vt, void *packet_data, uword n_packet_data_bytes, - uword min_n_buffers_each_physmem_alloc, u8 * name) + uword min_n_buffers_each_alloc, u8 * name) { vlib_packet_template_t *t = (vlib_packet_template_t *) vt; @@ -609,7 +597,7 @@ dpdk_buffer_pool_create (vlib_main_t * vm, unsigned num_mbufs, rte_mempool_obj_iter (rmp, rte_pktmbuf_init, 0); dpdk_mempool_private_t *privp = rte_mempool_get_priv (rmp); - privp->buffer_pool_index = vlib_buffer_add_physmem_region (vm, pri); + privp->buffer_pool_index = vlib_buffer_pool_create (vm, pri, 0); dm->pktmbuf_pools[socket_id] = rmp; diff --git a/src/vlib/buffer.c b/src/vlib/buffer.c index 3e2fe82fb12..6caca72cf03 100644 --- a/src/vlib/buffer.c +++ b/src/vlib/buffer.c @@ -49,6 +49,8 @@ vlib_buffer_callbacks_t *vlib_buffer_callbacks = 0; static u32 vlib_buffer_physmem_sz = 32 << 20; +vlib_buffer_main_t buffer_main; + uword vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm, vlib_buffer_t * b_first) @@ -171,16 +173,15 @@ vlib_validate_buffer_helper (vlib_main_t * vm, uword follow_buffer_next, uword ** unique_hash) { vlib_buffer_t *b = vlib_get_buffer (vm, bi); - vlib_buffer_main_t *bm = vm->buffer_main; vlib_buffer_free_list_t *fl; if (pool_is_free_index - (bm->buffer_free_list_pool, vlib_buffer_get_free_list_index (b))) + (vm->buffer_free_list_pool, vlib_buffer_get_free_list_index (b))) return format (0, "unknown free list 0x%x", vlib_buffer_get_free_list_index (b)); fl = - pool_elt_at_index (bm->buffer_free_list_pool, + pool_elt_at_index (vm->buffer_free_list_pool, vlib_buffer_get_free_list_index (b)); if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE) @@ -342,35 +343,19 @@ vlib_buffer_validate_alloc_free (vlib_main_t * vm, } } -void -vlib_buffer_merge_free_lists (vlib_buffer_free_list_t * dst, - vlib_buffer_free_list_t * src) -{ - uword l; - u32 *d; - - l = vec_len (src->buffers); - if (l > 0) - { - vec_add2_aligned (dst->buffers, d, l, CLIB_CACHE_LINE_BYTES); - clib_memcpy (d, src->buffers, l * sizeof (d[0])); - vec_free (src->buffers); - } -} - /* 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_main_t *bm = &buffer_main; vlib_buffer_free_list_t *f; int i; ASSERT (vlib_get_thread_index () == 0); - if (!is_default && pool_elts (bm->buffer_free_list_pool) == 0) + if (!is_default && pool_elts (vm->buffer_free_list_pool) == 0) { vlib_buffer_free_list_index_t default_free_free_list_index; @@ -390,12 +375,13 @@ vlib_buffer_create_free_list_helper (vlib_main_t * vm, return default_free_free_list_index; } - pool_get_aligned (bm->buffer_free_list_pool, f, CLIB_CACHE_LINE_BYTES); + pool_get_aligned (vm->buffer_free_list_pool, f, CLIB_CACHE_LINE_BYTES); memset (f, 0, sizeof (f[0])); - f->index = f - bm->buffer_free_list_pool; + f->index = f - vm->buffer_free_list_pool; f->n_data_bytes = vlib_buffer_round_size (n_data_bytes); - f->min_n_buffers_each_physmem_alloc = VLIB_FRAME_SIZE; + 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. */ @@ -409,16 +395,14 @@ vlib_buffer_create_free_list_helper (vlib_main_t * vm, hash_set (bm->free_list_by_size, f->n_data_bytes, f->index); } - clib_spinlock_init (&f->global_buffers_lock); - for (i = 1; i < vec_len (vlib_mains); i++) { - vlib_buffer_main_t *wbm = vlib_mains[i]->buffer_main; + vlib_main_t *wvm = vlib_mains[i]; vlib_buffer_free_list_t *wf; - pool_get_aligned (wbm->buffer_free_list_pool, + pool_get_aligned (wvm->buffer_free_list_pool, wf, CLIB_CACHE_LINE_BYTES); - ASSERT (f - bm->buffer_free_list_pool == - wf - wbm->buffer_free_list_pool); + ASSERT (f - vm->buffer_free_list_pool == + wf - wvm->buffer_free_list_pool); wf[0] = f[0]; wf->buffers = 0; wf->n_alloc = 0; @@ -444,41 +428,18 @@ vlib_buffer_create_free_list (vlib_main_t * vm, u32 n_data_bytes, name); } -vlib_buffer_free_list_index_t -vlib_buffer_get_or_create_free_list (vlib_main_t * vm, u32 n_data_bytes, - char *fmt, ...) -{ - u32 i = vlib_buffer_get_free_list_with_size (vm, n_data_bytes); - - if (i == ~0) - { - va_list va; - u8 *name; - - va_start (va, fmt); - name = va_format (0, fmt, &va); - va_end (va); - - i = vlib_buffer_create_free_list_helper (vm, n_data_bytes, - /* is_public */ 1, - /* is_default */ 0, - name); - } - - return i; -} - static void del_free_list (vlib_main_t * vm, vlib_buffer_free_list_t * f) { - u32 i; + vlib_buffer_pool_t *bp = vlib_buffer_pool_get (f->buffer_pool_index); - for (i = 0; i < vec_len (f->buffer_memory_allocated); i++) - vm->os_physmem_free (vm, vm->buffer_main->buffer_pools[0].physmem_region, - f->buffer_memory_allocated[i]); + vec_add_aligned (bp->buffers, f->buffers, vec_len (f->buffers), + CLIB_CACHE_LINE_BYTES); vec_free (f->name); - vec_free (f->buffer_memory_allocated); vec_free (f->buffers); + + /* Poison it. */ + memset (f, 0xab, sizeof (f[0])); } /* Add buffer free list. */ @@ -486,9 +447,7 @@ void vlib_buffer_delete_free_list_internal (vlib_main_t * vm, vlib_buffer_free_list_index_t index) { - vlib_buffer_main_t *bm = vm->buffer_main; vlib_buffer_free_list_t *f; - vlib_buffer_free_list_index_t merge_index; int i; ASSERT (vlib_get_thread_index () == 0); @@ -496,59 +455,70 @@ vlib_buffer_delete_free_list_internal (vlib_main_t * vm, f = vlib_buffer_get_free_list (vm, index); ASSERT (vec_len (f->buffers) == f->n_alloc); - merge_index = vlib_buffer_get_free_list_with_size (vm, f->n_data_bytes); - if (merge_index != (vlib_buffer_free_list_index_t) ~ 0 - && merge_index != index) - { - vlib_buffer_merge_free_lists (pool_elt_at_index - (bm->buffer_free_list_pool, merge_index), - f); - } del_free_list (vm, f); - /* Poison it. */ - memset (f, 0xab, sizeof (f[0])); - - pool_put (bm->buffer_free_list_pool, f); + pool_put (vm->buffer_free_list_pool, f); for (i = 1; i < vec_len (vlib_mains); i++) { - bm = vlib_mains[i]->buffer_main; - f = vlib_buffer_get_free_list (vlib_mains[i], index);; - memset (f, 0xab, sizeof (f[0])); - pool_put (bm->buffer_free_list_pool, f); + vlib_main_t *wvm = vlib_mains[i]; + f = vlib_buffer_get_free_list (vlib_mains[i], index); + del_free_list (wvm, f); + pool_put (wvm->buffer_free_list_pool, f); } } +static_always_inline void * +vlib_buffer_pool_get_buffer (vlib_buffer_pool_t * bp) +{ + uword slot, page, addr; + + if (PREDICT_FALSE (bp->n_elts == bp->n_used)) + { + clib_spinlock_unlock (&bp->lock); + return 0; + } + slot = bp->next_clear; + bp->bitmap = clib_bitmap_set (bp->bitmap, slot, 1); + bp->next_clear = clib_bitmap_next_clear (bp->bitmap, slot + 1); + bp->n_used++; + + page = slot / bp->buffers_per_page; + slot -= page * bp->buffers_per_page; + + addr = bp->start + (page << bp->log2_page_size) + slot * bp->buffer_size; + + return uword_to_pointer (addr, void *); +} + /* 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 *buffers, *b; - vlib_buffer_free_list_t *mfl; - int n, n_bytes, i; + vlib_buffer_t *b; + vlib_buffer_pool_t *bp = vlib_buffer_pool_get (fl->buffer_pool_index); + int n; u32 *bi; - u32 n_remaining, n_alloc, n_this_chunk; + 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; - mfl = vlib_buffer_get_free_list (vlib_mains[0], fl->index); - if (vec_len (mfl->global_buffers) > 0) + if (vec_len (bp->buffers) > 0) { int n_copy, n_left; - clib_spinlock_lock (&mfl->global_buffers_lock); - n_copy = clib_min (vec_len (mfl->global_buffers), n); - n_left = vec_len (mfl->global_buffers) - n_copy; - vec_add_aligned (fl->buffers, mfl->global_buffers + n_left, n_copy, + 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 (mfl->global_buffers) = n_left; - clib_spinlock_unlock (&mfl->global_buffers_lock); + _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; @@ -558,62 +528,39 @@ vlib_buffer_fill_free_list_internal (vlib_main_t * vm, 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_physmem_alloc); + n = clib_max (n, fl->min_n_buffers_each_alloc); - n_remaining = n; - n_alloc = 0; - while (n_remaining > 0) + clib_spinlock_lock (&bp->lock); + while (n_alloc < n) { - vlib_buffer_pool_t *bp = &vm->buffer_main->buffer_pools[0]; - n_this_chunk = clib_min (n_remaining, bp->alloc_chunk_size); - - n_bytes = n_this_chunk * (sizeof (b[0]) + fl->n_data_bytes); - - /* drb: removed power-of-2 ASSERT */ - buffers = - vm->os_physmem_alloc_aligned (vm, bp->physmem_region, n_bytes, - sizeof (vlib_buffer_t)); - if (!buffers) - return n_alloc; - - /* Record chunk as being allocated so we can free it later. */ - vec_add1 (fl->buffer_memory_allocated, buffers); - - fl->n_alloc += n_this_chunk; - n_alloc += n_this_chunk; - n_remaining -= n_this_chunk; + if ((b = vlib_buffer_pool_get_buffer (bp)) == 0) + goto done; - b = buffers; - vec_add2_aligned (fl->buffers, bi, n_this_chunk, CLIB_CACHE_LINE_BYTES); - for (i = 0; i < n_this_chunk; i++) - { - bi[i] = vlib_get_buffer_index (vm, b); + n_alloc += 1; - if (CLIB_DEBUG > 0) - vlib_buffer_set_known_state (bi[i], VLIB_BUFFER_KNOWN_FREE); - b = vlib_buffer_next_contiguous (b, fl->n_data_bytes); - } + vec_add2_aligned (fl->buffers, bi, 1, CLIB_CACHE_LINE_BYTES); + bi[0] = vlib_get_buffer_index (vm, b); - memset (buffers, 0, n_bytes); + if (CLIB_DEBUG > 0) + vlib_buffer_set_known_state (bi[0], VLIB_BUFFER_KNOWN_FREE); - /* Initialize all new buffers. */ - b = buffers; - for (i = 0; i < n_this_chunk; i++) - { - vlib_buffer_init_for_free_list (b, fl); - b = vlib_buffer_next_contiguous (b, fl->n_data_bytes); - } + memset (b, 0, sizeof (vlib_buffer_t)); + vlib_buffer_init_for_free_list (b, fl); if (fl->buffer_init_function) - fl->buffer_init_function (vm, fl, bi, n_this_chunk); + 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; + vlib_buffer_main_t *bm = &buffer_main; void *rv = bm->buffer_free_callback; bm->buffer_free_callback = fp; @@ -636,12 +583,12 @@ recycle_or_free (vlib_main_t * vm, vlib_buffer_main_t * bm, u32 bi, vlib_buffer_add_to_free_list (vm, fl, bi, (b->flags & VLIB_BUFFER_RECYCLE) == 0); - for (j = 0; j < vec_len (bm->announce_list); j++) + for (j = 0; j < vec_len (vm->buffer_announce_list); j++) { - if (fl == bm->announce_list[j]) + if (fl == vm->buffer_announce_list[j]) goto already_announced; } - vec_add1 (bm->announce_list, fl); + vec_add1 (vm->buffer_announce_list, fl); already_announced: ; } @@ -676,7 +623,7 @@ 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_main_t *bm = &buffer_main; vlib_buffer_t *p, *b0, *b1, *b2, *b3; int i = 0; u32 (*cb) (vlib_main_t * vm, u32 * buffers, u32 n_buffers, @@ -727,15 +674,15 @@ vlib_buffer_free_inline (vlib_main_t * vm, i++; } - if (vec_len (bm->announce_list)) + if (vec_len (vm->buffer_announce_list)) { vlib_buffer_free_list_t *fl; - for (i = 0; i < vec_len (bm->announce_list); i++) + for (i = 0; i < vec_len (vm->buffer_announce_list); i++) { - fl = bm->announce_list[i]; + fl = vm->buffer_announce_list[i]; fl->buffers_added_to_freelist_function (vm, fl); } - _vec_len (bm->announce_list) = 0; + _vec_len (vm->buffer_announce_list) = 0; } } @@ -779,10 +726,9 @@ vlib_packet_template_init (vlib_main_t * vm, vlib_packet_template_t * t, void *packet_data, uword n_packet_data_bytes, - uword min_n_buffers_each_physmem_alloc, - char *fmt, ...) + uword min_n_buffers_each_alloc, char *fmt, ...) { - vlib_buffer_main_t *bm = vm->buffer_main; + vlib_buffer_main_t *bm = &buffer_main; va_list va; u8 *name; vlib_buffer_free_list_t *fl; @@ -794,15 +740,14 @@ vlib_packet_template_init (vlib_main_t * vm, if (bm->cb.vlib_packet_template_init_cb) bm->cb.vlib_packet_template_init_cb (vm, (void *) t, packet_data, n_packet_data_bytes, - min_n_buffers_each_physmem_alloc, - name); + min_n_buffers_each_alloc, name); vlib_worker_thread_barrier_sync (vm); memset (t, 0, sizeof (t[0])); vec_add (t->packet_data, packet_data, n_packet_data_bytes); - t->min_n_buffers_each_physmem_alloc = min_n_buffers_each_physmem_alloc; + t->min_n_buffers_each_alloc = min_n_buffers_each_alloc; t->free_list_index = vlib_buffer_create_free_list_helper (vm, n_packet_data_bytes, @@ -812,7 +757,7 @@ vlib_packet_template_init (vlib_main_t * vm, ASSERT (t->free_list_index != 0); fl = vlib_buffer_get_free_list (vm, t->free_list_index); - fl->min_n_buffers_each_physmem_alloc = t->min_n_buffers_each_physmem_alloc; + fl->min_n_buffers_each_alloc = t->min_n_buffers_each_alloc; fl->buffer_init_function = vlib_packet_template_buffer_init; fl->buffer_init_function_opaque = pointer_to_uword (t); @@ -848,7 +793,7 @@ void vlib_packet_template_get_packet_helper (vlib_main_t * vm, vlib_packet_template_t * t) { - word n = t->min_n_buffers_each_physmem_alloc; + word n = t->min_n_buffers_each_alloc; word l = vec_len (t->packet_data); word n_alloc; @@ -956,10 +901,10 @@ vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm, } u8 -vlib_buffer_add_physmem_region (vlib_main_t * vm, - vlib_physmem_region_index_t pri) +vlib_buffer_pool_create (vlib_main_t * vm, vlib_physmem_region_index_t pri, + u16 buffer_size) { - vlib_buffer_main_t *bm = vm->buffer_main; + vlib_buffer_main_t *bm = &buffer_main; vlib_physmem_region_t *pr = vlib_physmem_get_region (vm, pri); vlib_buffer_pool_t *p; uword start = pointer_to_uword (pr->mem); @@ -994,7 +939,18 @@ vlib_buffer_add_physmem_region (vlib_main_t * vm, p->start = start; p->size = size; p->physmem_region = pri; - p->alloc_chunk_size = (pr->log2_page_size > 18) ? 16 : 1; + + if (buffer_size == 0) + goto done; + + p->log2_page_size = pr->log2_page_size; + p->buffer_size = buffer_size; + p->buffers_per_page = (1 << pr->log2_page_size) / p->buffer_size; + p->n_elts = p->buffers_per_page * pr->n_pages; + p->n_used = 0; + clib_spinlock_init (&p->lock); +done: + ASSERT (p - bm->buffer_pools < 256); return p - bm->buffer_pools; } @@ -1027,7 +983,6 @@ static clib_error_t * show_buffers (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { - vlib_buffer_main_t *bm; vlib_buffer_free_list_t *f; vlib_main_t *curr_vm; u32 vm_index = 0; @@ -1037,10 +992,9 @@ show_buffers (vlib_main_t * vm, do { curr_vm = vlib_mains[vm_index]; - bm = curr_vm->buffer_main; /* *INDENT-OFF* */ - pool_foreach (f, bm->buffer_free_list_pool, ({ + pool_foreach (f, curr_vm->buffer_free_list_pool, ({ vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, f, vm_index); })); /* *INDENT-ON* */ @@ -1063,18 +1017,14 @@ VLIB_CLI_COMMAND (show_buffers_command, static) = { clib_error_t * vlib_buffer_main_init (struct vlib_main_t * vm) { - vlib_buffer_main_t *bm; + vlib_buffer_main_t *bm = &buffer_main; vlib_physmem_region_index_t pri; clib_error_t *error; - vec_validate (vm->buffer_main, 0); - bm = vm->buffer_main; - if (vlib_buffer_callbacks) { /* external plugin has registered own buffer callbacks so we just copy them and quit */ - vlib_buffer_main_t *bm = vm->buffer_main; clib_memcpy (&bm->cb, vlib_buffer_callbacks, sizeof (vlib_buffer_callbacks_t)); bm->callbacks_registered = 1; @@ -1092,8 +1042,7 @@ vlib_buffer_main_init (struct vlib_main_t * vm) error = vlib_physmem_region_alloc (vm, "buffers", vlib_buffer_physmem_sz, 0, VLIB_PHYSMEM_F_SHARED | - VLIB_PHYSMEM_F_HUGETLB | - VLIB_PHYSMEM_F_INIT_MHEAP, &pri); + VLIB_PHYSMEM_F_HUGETLB, &pri); if (error == 0) goto done; @@ -1102,11 +1051,11 @@ vlib_buffer_main_init (struct vlib_main_t * vm) error = vlib_physmem_region_alloc (vm, "buffers", vlib_buffer_physmem_sz, 0, - VLIB_PHYSMEM_F_SHARED | - VLIB_PHYSMEM_F_INIT_MHEAP, &pri); + VLIB_PHYSMEM_F_SHARED, &pri); done: if (error == 0) - vlib_buffer_add_physmem_region (vm, pri); + vlib_buffer_pool_create (vm, pri, sizeof (vlib_buffer_t) + + VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES); return error; } diff --git a/src/vlib/buffer.h b/src/vlib/buffer.h index bf09eb9863c..48988222de7 100644 --- a/src/vlib/buffer.h +++ b/src/vlib/buffer.h @@ -190,12 +190,6 @@ typedef struct #define vlib_prefetch_buffer_header(b,type) CLIB_PREFETCH (b, 64, type) -always_inline vlib_buffer_t * -vlib_buffer_next_contiguous (vlib_buffer_t * b, u32 buffer_bytes) -{ - return (void *) (b + 1) + buffer_bytes; -} - always_inline void vlib_buffer_struct_is_sane (vlib_buffer_t * b) { @@ -362,9 +356,8 @@ typedef struct vlib_buffer_free_list_t /* Number of data bytes for buffers in this free list. */ u32 n_data_bytes; - /* Number of buffers to allocate when we need to allocate new buffers - from physmem heap. */ - u32 min_n_buffers_each_physmem_alloc; + /* 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; @@ -372,16 +365,8 @@ typedef struct vlib_buffer_free_list_t /* Vector of free buffers. Each element is a byte offset into I/O heap. */ u32 *buffers; - /* global vector of free buffers, used only on main thread. - Bufers are returned to global buffers only in case when number of - buffers on free buffers list grows about threshold */ - u32 *global_buffers; - clib_spinlock_t global_buffers_lock; - - /* Memory chunks allocated for this free list - recorded here so they can be freed when free list - is deleted. */ - void **buffer_memory_allocated; + /* index of buffer pool used to get / put buffers */ + u8 buffer_pool_index; /* Free list name. */ u8 *name; @@ -431,8 +416,18 @@ typedef struct CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); uword start; uword size; + uword log2_page_size; vlib_physmem_region_index_t physmem_region; - int alloc_chunk_size; + + u32 *buffers; + + u16 buffer_size; + uword buffers_per_page; + uword n_elts; + uword n_used; + uword next_clear; + uword *bitmap; + clib_spinlock_t lock; } vlib_buffer_pool_t; typedef struct @@ -448,11 +443,6 @@ typedef struct u32 (*buffer_free_callback) (struct vlib_main_t * vm, u32 * buffers, u32 n_buffers, u32 follow_buffer_next); - /* Pool of buffer free lists. - Multiple free lists exist for packet generator which uses - separate free lists for each packet stream --- so as to avoid - initializing static data for each packet generated. */ - vlib_buffer_free_list_t *buffer_free_list_pool; #define VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX (0) #define VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES VLIB_BUFFER_DATA_SIZE @@ -467,16 +457,23 @@ typedef struct uword *buffer_known_hash; clib_spinlock_t buffer_known_hash_lockp; - /* List of free-lists needing Blue Light Special announcements */ - vlib_buffer_free_list_t **announce_list; - /* Callbacks */ vlib_buffer_callbacks_t cb; int callbacks_registered; } vlib_buffer_main_t; -u8 vlib_buffer_add_physmem_region (struct vlib_main_t *vm, - vlib_physmem_region_index_t region); +extern vlib_buffer_main_t buffer_main; + +static_always_inline vlib_buffer_pool_t * +vlib_buffer_pool_get (u8 buffer_pool_index) +{ + vlib_buffer_main_t *bm = &buffer_main; + return vec_elt_at_index (bm->buffer_pools, buffer_pool_index); +} + +u8 vlib_buffer_pool_create (struct vlib_main_t * vm, + vlib_physmem_region_index_t region, + u16 buffer_size); clib_error_t *vlib_buffer_main_init (struct vlib_main_t *vm); diff --git a/src/vlib/buffer_funcs.h b/src/vlib/buffer_funcs.h index d1aecac7e80..0ac05f3f703 100644 --- a/src/vlib/buffer_funcs.h +++ b/src/vlib/buffer_funcs.h @@ -56,7 +56,7 @@ always_inline vlib_buffer_t * vlib_get_buffer (vlib_main_t * vm, u32 buffer_index) { - vlib_buffer_main_t *bm = vm->buffer_main; + vlib_buffer_main_t *bm = &buffer_main; uword offset = ((uword) buffer_index) << CLIB_LOG2_CACHE_LINE_BYTES; ASSERT (offset < bm->buffer_mem_size); @@ -73,7 +73,7 @@ vlib_get_buffer (vlib_main_t * vm, u32 buffer_index) always_inline u32 vlib_get_buffer_index (vlib_main_t * vm, void *p) { - vlib_buffer_main_t *bm = vm->buffer_main; + vlib_buffer_main_t *bm = &buffer_main; uword offset = pointer_to_uword (p) - bm->buffer_mem_start; ASSERT (pointer_to_uword (p) >= bm->buffer_mem_start); ASSERT (offset < bm->buffer_mem_size); @@ -162,7 +162,7 @@ vlib_buffer_contents (vlib_main_t * vm, u32 buffer_index, u8 * contents) always_inline u64 vlib_get_buffer_data_physical_address (vlib_main_t * vm, u32 buffer_index) { - vlib_buffer_main_t *bm = vm->buffer_main; + vlib_buffer_main_t *bm = &buffer_main; vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index); vlib_buffer_pool_t *pool = vec_elt_at_index (bm->buffer_pools, b->buffer_pool_index); @@ -224,7 +224,7 @@ void vlib_buffer_validate_alloc_free (vlib_main_t * vm, u32 * buffers, always_inline vlib_buffer_known_state_t vlib_buffer_is_known (u32 buffer_index) { - vlib_buffer_main_t *bm = vlib_global_main.buffer_main; + vlib_buffer_main_t *bm = &buffer_main; clib_spinlock_lock (&bm->buffer_known_hash_lockp); uword *p = hash_get (bm->buffer_known_hash, buffer_index); @@ -236,7 +236,7 @@ always_inline void vlib_buffer_set_known_state (u32 buffer_index, vlib_buffer_known_state_t state) { - vlib_buffer_main_t *bm = vlib_global_main.buffer_main; + vlib_buffer_main_t *bm = &buffer_main; clib_spinlock_lock (&bm->buffer_known_hash_lockp); hash_set (bm->buffer_known_hash, buffer_index, state); @@ -290,14 +290,14 @@ vlib_buffer_alloc_from_free_list (vlib_main_t * vm, u32 n_buffers, vlib_buffer_free_list_index_t index) { - vlib_buffer_main_t *bm = vm->buffer_main; + vlib_buffer_main_t *bm = &buffer_main; vlib_buffer_free_list_t *fl; u32 *src; uword len; ASSERT (bm->cb.vlib_buffer_fill_free_list_cb); - fl = pool_elt_at_index (bm->buffer_free_list_pool, index); + fl = pool_elt_at_index (vm->buffer_free_list_pool, index); len = vec_len (fl->buffers); @@ -394,7 +394,7 @@ vlib_buffer_free (vlib_main_t * vm, /* number of buffers to free */ u32 n_buffers) { - vlib_buffer_main_t *bm = vm->buffer_main; + vlib_buffer_main_t *bm = &buffer_main; ASSERT (bm->cb.vlib_buffer_free_cb); @@ -415,7 +415,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; + vlib_buffer_main_t *bm = &buffer_main; ASSERT (bm->cb.vlib_buffer_free_no_next_cb); @@ -468,60 +468,36 @@ always_inline void vlib_buffer_delete_free_list (vlib_main_t * vm, vlib_buffer_free_list_index_t free_list_index) { - vlib_buffer_main_t *bm = vm->buffer_main; + vlib_buffer_main_t *bm = &buffer_main; ASSERT (bm->cb.vlib_buffer_delete_free_list_cb); bm->cb.vlib_buffer_delete_free_list_cb (vm, free_list_index); } -/* Find already existing public free list with given size or create one. */ -vlib_buffer_free_list_index_t vlib_buffer_get_or_create_free_list (vlib_main_t - * vm, - u32 - n_data_bytes, - char *fmt, - ...); - -/* Merge two free lists */ -void vlib_buffer_merge_free_lists (vlib_buffer_free_list_t * dst, - vlib_buffer_free_list_t * src); - /* Make sure we have at least given number of unaligned buffers. */ void vlib_buffer_free_list_fill_unaligned (vlib_main_t * vm, vlib_buffer_free_list_t * free_list, uword n_unaligned_buffers); -always_inline vlib_buffer_free_list_index_t -vlib_buffer_get_free_list_with_size (vlib_main_t * vm, u32 size) -{ - vlib_buffer_main_t *bm = vm->buffer_main; - - size = vlib_buffer_round_size (size); - uword *p = hash_get (bm->free_list_by_size, size); - return p ? p[0] : ~0; -} - always_inline vlib_buffer_free_list_t * vlib_buffer_get_buffer_free_list (vlib_main_t * vm, vlib_buffer_t * b, vlib_buffer_free_list_index_t * index) { - vlib_buffer_main_t *bm = vm->buffer_main; vlib_buffer_free_list_index_t i; *index = i = vlib_buffer_get_free_list_index (b); - return pool_elt_at_index (bm->buffer_free_list_pool, i); + return pool_elt_at_index (vm->buffer_free_list_pool, i); } always_inline vlib_buffer_free_list_t * vlib_buffer_get_free_list (vlib_main_t * vm, vlib_buffer_free_list_index_t free_list_index) { - vlib_buffer_main_t *bm = vm->buffer_main; vlib_buffer_free_list_t *f; - f = pool_elt_at_index (bm->buffer_free_list_pool, free_list_index); + f = pool_elt_at_index (vm->buffer_free_list_pool, free_list_index); /* Sanity: indices must match. */ ASSERT (f->index == free_list_index); @@ -849,9 +825,8 @@ typedef struct /* Vector of packet data. */ u8 *packet_data; - /* Number of buffers to allocate in each call to physmem - allocator. */ - u32 min_n_buffers_each_physmem_alloc; + /* 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; @@ -866,7 +841,7 @@ void vlib_packet_template_init (vlib_main_t * vm, vlib_packet_template_t * t, void *packet_data, uword n_packet_data_bytes, - uword min_n_buffers_each_physmem_alloc, + uword min_n_buffers_each_alloc, char *fmt, ...); void *vlib_packet_template_get_packet (vlib_main_t * vm, @@ -957,6 +932,7 @@ 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 (f->buffer_pool_index); vlib_buffer_t *b; b = vlib_get_buffer (vm, buffer_index); if (PREDICT_TRUE (do_init)) @@ -965,15 +941,13 @@ vlib_buffer_add_to_free_list (vlib_main_t * vm, if (vec_len (f->buffers) > 4 * VLIB_FRAME_SIZE) { - vlib_buffer_free_list_t *mf; - mf = vlib_buffer_get_free_list (vlib_mains[0], f->index); - clib_spinlock_lock (&mf->global_buffers_lock); + clib_spinlock_lock (&bp->lock); /* keep last stored buffers, as they are more likely hot in the cache */ - vec_add_aligned (mf->global_buffers, f->buffers, VLIB_FRAME_SIZE, + 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 (&mf->global_buffers_lock); + clib_spinlock_unlock (&bp->lock); } } diff --git a/src/vlib/main.c b/src/vlib/main.c index 5efeac26f79..d1f7592e502 100644 --- a/src/vlib/main.c +++ b/src/vlib/main.c @@ -460,7 +460,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 (buffer_main.callbacks_registered == 0 && 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); @@ -1741,9 +1741,8 @@ vlib_main (vlib_main_t * volatile vm, unformat_input_t * input) goto done; /* Create default buffer free list. */ - vlib_buffer_get_or_create_free_list (vm, - VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES, - "default"); + vlib_buffer_create_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES, + "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 2eec930e9b6..4e48d1de09c 100644 --- a/src/vlib/main.h +++ b/src/vlib/main.h @@ -103,7 +103,11 @@ typedef struct vlib_main_t void *heap_base; uword heap_size; - vlib_buffer_main_t *buffer_main; + /* Pool of buffer free lists. */ + vlib_buffer_free_list_t *buffer_free_list_pool; + + /* List of free-lists needing Blue Light Special announcements */ + vlib_buffer_free_list_t **buffer_announce_list; /* Allocate/free buffer memory for DMA transfers, descriptor rings, etc. buffer memory is guaranteed to be cache-aligned. */ diff --git a/src/vlib/threads.c b/src/vlib/threads.c index 3edf1ebbc62..50056462d1a 100644 --- a/src/vlib/threads.c +++ b/src/vlib/threads.c @@ -783,7 +783,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_main_t *bm_clone; vlib_buffer_free_list_t *fl_clone, *fl_orig; vlib_buffer_free_list_t *orig_freelist_pool; int k; @@ -919,19 +918,16 @@ start_workers (vlib_main_t * vm) vec_dup (vlib_mains[0]->error_main.counters_last_clear); /* Fork the vlib_buffer_main_t free lists, etc. */ - bm_clone = vec_dup (vm_clone->buffer_main); - vm_clone->buffer_main = bm_clone; - - orig_freelist_pool = bm_clone->buffer_free_list_pool; - bm_clone->buffer_free_list_pool = 0; + 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 (bm_clone->buffer_free_list_pool, + pool_get_aligned (vm_clone->buffer_free_list_pool, fl_clone, CLIB_CACHE_LINE_BYTES); ASSERT (fl_orig - orig_freelist_pool - == fl_clone - bm_clone->buffer_free_list_pool); + == fl_clone - vm_clone->buffer_free_list_pool); fl_clone[0] = fl_orig[0]; fl_clone->buffers = 0; diff --git a/src/vnet/replication.c b/src/vnet/replication.c index 217aa76a4d2..21f5485e6c7 100644 --- a/src/vnet/replication.c +++ b/src/vnet/replication.c @@ -219,7 +219,7 @@ replication_recycle_callback (vlib_main_t * vm, vlib_buffer_free_list_t * fl) b0->flags |= VLIB_BUFFER_IS_RECYCLED; #if (CLIB_DEBUG > 0) - if (vm->buffer_main->callbacks_registered == 0) + if (buffer_main.callbacks_registered == 0) vlib_buffer_set_known_state (bi0, VLIB_BUFFER_KNOWN_ALLOCATED); #endif @@ -251,7 +251,6 @@ clib_error_t * replication_init (vlib_main_t * vm) { replication_main_t *rm = &replication_main; - vlib_buffer_main_t *bm = vm->buffer_main; vlib_buffer_free_list_t *fl; __attribute__ ((unused)) replication_context_t *ctx; vlib_thread_main_t *tm = vlib_get_thread_main (); @@ -262,7 +261,7 @@ replication_init (vlib_main_t * vm) vlib_buffer_create_free_list (vm, 1024 /* fictional */ , "replication-recycle"); - fl = pool_elt_at_index (bm->buffer_free_list_pool, rm->recycle_list_index); + fl = pool_elt_at_index (vm->buffer_free_list_pool, rm->recycle_list_index); fl->buffers_added_to_freelist_function = replication_recycle_callback; |