Skip to contentSkip to conversation

add external mempool manager

PATCH In review David Hunt 10 participants 238 messages Jan 26, 2016 – Jun 24, 2016 Page 1 of 3

lore
Share & tools
lore

David Hunt

author
#0 lore raw

cover add external mempool manager

Hi all on the list.

Here's a proposed patch for an external mempool manager

The External Mempool Manager is an extension to the mempool API that allows users to add and use an external mempool manager, which allows external memory subsystems such as external hardware memory management systems and software based memory allocators to be used with DPDK.

The existing API to the internal DPDK mempool manager will remain unchanged and will be backward compatible.

There are two aspects to external mempool manager.
1. Adding the code for your new mempool handler. This is achieved by adding a new mempool handler source file into the librte_mempool library, and using the REGISTER_MEMPOOL_HANDLER macro.
2. Using the new API to call rte_mempool_create_ext to create a new mempool using the name parameter to identify which handler to use.

New API calls added
1. A new mempool 'create' function which accepts mempool handler name.
2. A new mempool 'rte_get_mempool_handler' function which accepts mempool handler name, and returns the index to the relevant set of callbacks for that mempool handler

Several external mempool managers may be used in the same application. A new mempool can then be created by using the new 'create' function, providing the mempool handler name to point the mempool to the relevant mempool manager callback structure.

The old 'create' function can still be called by legacy programs, and will internally work out the mempool handle based on the flags provided (single producer, single consumer, etc). By default handles are created internally to implement the built-in DPDK mempool manager and mempool types.

The external mempool manager needs to provide the following functions.
1. alloc - allocates the mempool memory, and adds each object onto a ring
2. put - puts an object back into the mempool once an application has
finished with it
3. get - gets an object from the mempool for use by the application
4. get_count - gets the number of available objects in the mempool
5. free - frees the mempool memory

Every time a get/put/get_count is called from the application/PMD, the callback for that mempool is called. These functions are in the fastpath, and any unoptimised handlers may limit performance.

The new APIs are as follows:

1. rte_mempool_create_ext

struct rte_mempool * rte_mempool_create_ext(const char * name, unsigned n,
unsigned cache_size, unsigned private_data_size, int socket_id, unsigned flags,
const char * handler_name);

2. rte_get_mempool_handler

int16_t
rte_get_mempool_handler(const char *name);

Please see rte_mempool.h for further information on the parameters.

The important thing to note is that the mempool handler is passed by name to rte_mempool_create_ext, and that in turn calls rte_get_mempool_handler to get the handler index, which is stored in the rte_memool structure. This allow multiple processes to use the same mempool, as the function pointers are accessed via handler index.

The mempool handler structure contains callbacks to the implementation of the handler, and is set up for registration as follows:

static struct rte_mempool_handler handler_sp_mc = {
.name = "ring_sp_mc",
.alloc = rte_mempool_common_ring_alloc, .put = common_ring_sp_put,
.get = common_ring_mc_get,
.get_count = common_ring_get_count, .free = common_ring_free,
};

And then the following macro will register the handler in the array of handlers

REGISTER_MEMPOOL_HANDLER(handler_mp_mc);

For and example of a simple malloc based mempool manager, see lib/librte_mempool/custom_mempool.c

For an example of API usage, please see app/test/test_ext_mempool.c, which implements a rudimentary mempool manager using simple mallocs for each mempool object (custom_mempool.c).

David Hunt (5):
mempool: add external mempool manager support
memool: add stack (lifo) based external mempool handler
mempool: add custom external mempool handler example
mempool: add autotest for external mempool custom example
mempool: allow rte_pktmbuf_pool_create switch between memool handlers

 app/test/Makefile                         |   1 +
 app/test/test_ext_mempool.c               | 470 ++++++++++++++++++++++++++++++
 app/test/test_mempool_perf.c              |   2 -
 lib/librte_mbuf/rte_mbuf.c                |  11 +
 lib/librte_mempool/Makefile               |   3 +
 lib/librte_mempool/custom_mempool.c       | 158 ++++++++++
 lib/librte_mempool/rte_mempool.c          | 208 +++++++++----
 lib/librte_mempool/rte_mempool.h          | 205 +++++++++++--
 lib/librte_mempool/rte_mempool_default.c  | 229 +++++++++++++++
 lib/librte_mempool/rte_mempool_internal.h |  70 +++++
 lib/librte_mempool/rte_mempool_stack.c    | 162 ++++++++++
 11 files changed, 1430 insertions(+), 89 deletions(-)
 create mode 100644 app/test/test_ext_mempool.c
 create mode 100644 lib/librte_mempool/custom_mempool.c
 create mode 100644 lib/librte_mempool/rte_mempool_default.c
 create mode 100644 lib/librte_mempool/rte_mempool_internal.h
 create mode 100644 lib/librte_mempool/rte_mempool_stack.c
Signature · 1 line
1.9.3
author
#1 lore raw

PATCH 1/5 mempool: add external mempool manager support

Adds the new rte_mempool_create_ext api and callback mechanism for external mempool handlers

Modifies the existing rte_mempool_create to set up the handler_idx to the relevant mempool handler based on the handler name:
ring_sp_sc
ring_mp_mc
ring_sp_mc
ring_mp_sc

---
 app/test/test_mempool_perf.c              |   1 -
 lib/librte_mempool/Makefile               |   1 +
 lib/librte_mempool/rte_mempool.c          | 210 +++++++++++++++++++--------
 lib/librte_mempool/rte_mempool.h          | 207 +++++++++++++++++++++++----
 lib/librte_mempool/rte_mempool_default.c  | 229 ++++++++++++++++++++++++++++++
 lib/librte_mempool/rte_mempool_internal.h |  74 ++++++++++
 6 files changed, 634 insertions(+), 88 deletions(-)
 create mode 100644 lib/librte_mempool/rte_mempool_default.c
 create mode 100644 lib/librte_mempool/rte_mempool_internal.h
app/test/test_mempool_perf.c +0 -1
index cdc02a0..091c1df 100644
@@ -161,7 +161,6 @@ per_lcore_mempool_test(__attribute__((unused)) void *arg)
 							   n_get_bulk);
 				if (unlikely(ret < 0)) {
 					rte_mempool_dump(stdout, mp);
-					rte_ring_dump(stdout, mp->ring);
 					/* in this case, objects are lost... */
 					return -1;
 				}
lib/librte_mempool/Makefile +1 -0
index a6898ef..7c81ef6 100644
@@ -42,6 +42,7 @@ LIBABIVER := 1
 
 # all source are stored in SRCS-y
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_default.c
 ifeq ($(CONFIG_RTE_LIBRTE_XEN_DOM0),y)
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_dom0_mempool.c
 endif
lib/librte_mempool/rte_mempool.c +154 -56

324 lines not on this page · raw message

lib/librte_mempool/rte_mempool.h +176 -31

302 lines not on this page · raw message

lib/librte_mempool/rte_mempool_default.c new file +229 -0

229 lines not on this page · raw message

lib/librte_mempool/rte_mempool_internal.h new file +74 -0

74 lines not on this page · raw message

Signature · 1 line
1.9.3
#2 lore raw

Re: [PATCH 1/5] mempool: add external mempool manager support

Quoted 192 lines · David Hunt

Adds the new rte_mempool_create_ext api and callback mechanism for external mempool handlers

Modifies the existing rte_mempool_create to set up the handler_idx to the relevant mempool handler based on the handler name:
ring_sp_sc
ring_mp_mc
ring_sp_mc
ring_mp_sc

---
 app/test/test_mempool_perf.c              |   1 -
 lib/librte_mempool/Makefile               |   1 +
 lib/librte_mempool/rte_mempool.c          | 210 +++++++++++++++++++--------
 lib/librte_mempool/rte_mempool.h          | 207 +++++++++++++++++++++++----
 lib/librte_mempool/rte_mempool_default.c  | 229 ++++++++++++++++++++++++++++++
 lib/librte_mempool/rte_mempool_internal.h |  74 ++++++++++
 6 files changed, 634 insertions(+), 88 deletions(-)
 create mode 100644 lib/librte_mempool/rte_mempool_default.c
 create mode 100644 lib/librte_mempool/rte_mempool_internal.h
app/test/test_mempool_perf.c +0 -1
index cdc02a0..091c1df 100644
@@ -161,7 +161,6 @@ per_lcore_mempool_test(__attribute__((unused)) void *arg)
 							   n_get_bulk);
 				if (unlikely(ret < 0)) {
 					rte_mempool_dump(stdout, mp);
-					rte_ring_dump(stdout, mp->ring);
 					/* in this case, objects are lost... */
 					return -1;
 				}
lib/librte_mempool/Makefile +1 -0
index a6898ef..7c81ef6 100644
@@ -42,6 +42,7 @@ LIBABIVER := 1
 
 # all source are stored in SRCS-y
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_default.c
 ifeq ($(CONFIG_RTE_LIBRTE_XEN_DOM0),y)
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_dom0_mempool.c
 endif
lib/librte_mempool/rte_mempool.c +25 -49
index aff5f6d..8c01838 100644
@@ -59,10 +59,11 @@
 #include <rte_spinlock.h>
 
 #include "rte_mempool.h"
+#include "rte_mempool_internal.h"
 
 TAILQ_HEAD(rte_mempool_list, rte_tailq_entry);
 
-static struct rte_tailq_elem rte_mempool_tailq = {
+struct rte_tailq_elem rte_mempool_tailq = {
 	.name = "RTE_MEMPOOL",
 };
 EAL_REGISTER_TAILQ(rte_mempool_tailq)
@@ -149,7 +150,7 @@ mempool_add_elem(struct rte_mempool *mp, void *obj, uint32_t obj_idx,
 		obj_init(mp, obj_init_arg, obj, obj_idx);
 
 	/* enqueue in ring */
-	rte_ring_sp_enqueue(mp->ring, obj);
+	rte_mempool_ext_put_bulk(mp, &obj, 1);
 }
 
 uint32_t
@@ -375,48 +376,28 @@ rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz,
 	return usz;
 }
 
-#ifndef RTE_LIBRTE_XEN_DOM0
-/* stub if DOM0 support not configured */
-struct rte_mempool *
-rte_dom0_mempool_create(const char *name __rte_unused,
-			unsigned n __rte_unused,
-			unsigned elt_size __rte_unused,
-			unsigned cache_size __rte_unused,
-			unsigned private_data_size __rte_unused,
-			rte_mempool_ctor_t *mp_init __rte_unused,
-			void *mp_init_arg __rte_unused,
-			rte_mempool_obj_ctor_t *obj_init __rte_unused,
-			void *obj_init_arg __rte_unused,
-			int socket_id __rte_unused,
-			unsigned flags __rte_unused)
-{
-	rte_errno = EINVAL;
-	return NULL;
-}
-#endif
-
 /* create the mempool */
 struct rte_mempool *
 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
-		   unsigned cache_size, unsigned private_data_size,
-		   rte_mempool_ctor_t *mp_init, void *mp_init_arg,
-		   rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
-		   int socket_id, unsigned flags)
+			unsigned cache_size, unsigned private_data_size,
+			rte_mempool_ctor_t *mp_init, void *mp_init_arg,
+			rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
+			int socket_id, unsigned flags)
 {
 	if (rte_xen_dom0_supported())
 		return rte_dom0_mempool_create(name, n, elt_size,
-					       cache_size, private_data_size,
-					       mp_init, mp_init_arg,
-					       obj_init, obj_init_arg,
-					       socket_id, flags);
+			cache_size, private_data_size,
+			mp_init, mp_init_arg,
+			obj_init, obj_init_arg,
+			socket_id, flags);
 	else
 		return rte_mempool_xmem_create(name, n, elt_size,
-					       cache_size, private_data_size,
-					       mp_init, mp_init_arg,
-					       obj_init, obj_init_arg,
-					       socket_id, flags,
-					       NULL, NULL, MEMPOOL_PG_NUM_DEFAULT,
-					       MEMPOOL_PG_SHIFT_MAX);
+			cache_size, private_data_size,
+			mp_init, mp_init_arg,
+			obj_init, obj_init_arg,
+			socket_id, flags,
+			NULL, NULL,
+			MEMPOOL_PG_NUM_DEFAULT, MEMPOOL_PG_SHIFT_MAX);
 }
 
 /*
@@ -435,11 +416,9 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
 		const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
 {
 	char mz_name[RTE_MEMZONE_NAMESIZE];
-	char rg_name[RTE_RING_NAMESIZE];
 	struct rte_mempool_list *mempool_list;
 	struct rte_mempool *mp = NULL;
 	struct rte_tailq_entry *te;
-	struct rte_ring *r;
 	const struct rte_memzone *mz;
 	size_t mempool_size;
 	int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
@@ -469,7 +448,7 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
 
 	/* asked cache too big */
 	if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
-	    CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
+		CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
 		rte_errno = EINVAL;
 		return NULL;
 	}
@@ -502,16 +481,8 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
 		return NULL;
 	}
 
-	rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
 
-	/* allocate the ring that will be used to store objects */
-	/* Ring functions will return appropriate errors if we are
-	 * running as a secondary process etc., so no checks made
-	 * in this function for that condition */
-	snprintf(rg_name, sizeof(rg_name), RTE_MEMPOOL_MZ_FORMAT, name);
-	r = rte_ring_create(rg_name, rte_align32pow2(n+1), socket_id, rg_flags);
-	if (r == NULL)
-		goto exit;
+	rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
 
 	/*
 	 * reserve a memory zone for this mempool: private data is
@@ -588,7 +559,6 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
 	memset(mp, 0, sizeof(*mp));
 	snprintf(mp->name, sizeof(mp->name), "%s", name);
 	mp->phys_addr = mz->phys_addr;
-	mp->ring = r;
 	mp->size = n;
 	mp->flags = flags;
 	mp->elt_size = objsz.elt_size;
@@ -598,6 +568,22 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
 	mp->cache_flushthresh = CALC_CACHE_FLUSHTHRESH(cache_size);
 	mp->private_data_size = private_data_size;
 
+	/*
+	 * Since we have 4 combinations of the SP/SC/MP/MC, and stack,
+	 * examine the
+	 * flags to set the correct index into the handler table.
+	 */
+	if (flags & MEMPOOL_F_USE_STACK)

lib/librte_mempool/rte_mempool.c rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size, · line 586

Quoted 10 lines
lib/librte_mempool/rte_mempool.c +10 -0
+		mp->handler_idx = rte_get_mempool_handler("stack");
+	else if (flags & (MEMPOOL_F_SP_PUT | MEMPOOL_F_SC_GET))
+		mp->handler_idx = rte_get_mempool_handler("ring_sp_sc");
+	else if (flags & MEMPOOL_F_SP_PUT)
+		mp->handler_idx = rte_get_mempool_handler("ring_sp_mc");
+	else if (flags & MEMPOOL_F_SC_GET)
+		mp->handler_idx = rte_get_mempool_handler("ring_mp_sc");
+	else
+		mp->handler_idx = rte_get_mempool_handler("ring_mp_mc");
+

Why still use flag based selection? Why not "name" based? See below for more description

Quoted 112 lines
lib/librte_mempool/rte_mempool.c +43 -7
 	/* calculate address of the first element for continuous mempool. */
 	obj = (char *)mp + MEMPOOL_HEADER_SIZE(mp, pg_num) +
 		private_data_size;
@@ -613,7 +599,6 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
 		mp->elt_va_start = (uintptr_t)obj;
 		mp->elt_pa[0] = mp->phys_addr +
 			(mp->elt_va_start - (uintptr_t)mp);
-
 	/* mempool elements in a separate chunk of memory. */
 	} else {
 		mp->elt_va_start = (uintptr_t)vaddr;
@@ -622,6 +607,10 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
 
 	mp->elt_va_end = mp->elt_va_start;
 
+	/* Parameters are setup. Call the mempool handler alloc */
+	if ((rte_mempool_ext_alloc(mp, name, n, socket_id, flags)) == NULL)
+		goto exit;
+
 	/* call the initializer */
 	if (mp_init)
 		mp_init(mp, mp_init_arg);
@@ -646,7 +635,7 @@ rte_mempool_count(const struct rte_mempool *mp)
 {
 	unsigned count;
 
-	count = rte_ring_count(mp->ring);
+	count = rte_mempool_ext_get_count(mp);
 
 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
 	{
@@ -681,7 +670,9 @@ rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
 	fprintf(f, "    cache_size=%"PRIu32"\n", mp->cache_size);
 	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
 		cache_count = mp->local_cache[lcore_id].len;
-		fprintf(f, "    cache_count[%u]=%u\n", lcore_id, cache_count);
+		if (cache_count > 0)
+			fprintf(f, "    cache_count[%u]=%u\n",
+						lcore_id, cache_count);
 		count += cache_count;
 	}
 	fprintf(f, "    total_cache_count=%u\n", count);
@@ -802,14 +793,13 @@ rte_mempool_dump(FILE *f, const struct rte_mempool *mp)
 
 	fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
 	fprintf(f, "  flags=%x\n", mp->flags);
-	fprintf(f, "  ring=<%s>@%p\n", mp->ring->name, mp->ring);
 	fprintf(f, "  phys_addr=0x%" PRIx64 "\n", mp->phys_addr);
 	fprintf(f, "  size=%"PRIu32"\n", mp->size);
 	fprintf(f, "  header_size=%"PRIu32"\n", mp->header_size);
 	fprintf(f, "  elt_size=%"PRIu32"\n", mp->elt_size);
 	fprintf(f, "  trailer_size=%"PRIu32"\n", mp->trailer_size);
 	fprintf(f, "  total_obj_size=%"PRIu32"\n",
-	       mp->header_size + mp->elt_size + mp->trailer_size);
+		   mp->header_size + mp->elt_size + mp->trailer_size);
 
 	fprintf(f, "  private_data_size=%"PRIu32"\n", mp->private_data_size);
 	fprintf(f, "  pg_num=%"PRIu32"\n", mp->pg_num);
@@ -825,7 +815,7 @@ rte_mempool_dump(FILE *f, const struct rte_mempool *mp)
 			mp->size);
 
 	cache_count = rte_mempool_dump_cache(f, mp);
-	common_count = rte_ring_count(mp->ring);
+	common_count = /* rte_ring_count(mp->ring)*/0;
 	if ((cache_count + common_count) > mp->size)
 		common_count = mp->size - cache_count;
 	fprintf(f, "  common_pool_count=%u\n", common_count);
@@ -904,7 +894,7 @@ rte_mempool_lookup(const char *name)
 }
 
 void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *),
-		      void *arg)
+			  void *arg)
 {
 	struct rte_tailq_entry *te = NULL;
 	struct rte_mempool_list *mempool_list;
@@ -919,3 +909,111 @@ void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *),
 
 	rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
 }
+
+
+/* create the mempool using and external mempool manager */
+struct rte_mempool *
+rte_mempool_create_ext(const char *name, unsigned n, unsigned elt_size,
+			unsigned cache_size, unsigned private_data_size,
+			rte_mempool_ctor_t *mp_init, void *mp_init_arg,
+			rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
+			int socket_id, unsigned flags,
+			const char *handler_name)
+{
+	char mz_name[RTE_MEMZONE_NAMESIZE];
+	struct rte_mempool_list *mempool_list;
+	struct rte_mempool *mp = NULL;
+	struct rte_tailq_entry *te;
+	const struct rte_memzone *mz;
+	size_t mempool_size;
+	int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
+	int rg_flags = 0;
+	int16_t handler_idx;
+
+	mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
+
+	/* asked cache too big */
+	if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
+		CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
+		rte_errno = EINVAL;
+		return NULL;
+	}
+
+	handler_idx = rte_get_mempool_handler(handler_name);
+	if (handler_idx < 0) {

lib/librte_mempool/rte_mempool.c void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *), · line 953

Quoted 10 lines
lib/librte_mempool/rte_mempool.c +10 -0
+		RTE_LOG(ERR, MEMPOOL, "Cannot find mempool handler by name!\n");
+		goto exit;
+	}
+
+	/* ring flags */
+	if (flags & MEMPOOL_F_SP_PUT)
+		rg_flags |= RING_F_SP_ENQ;
+	if (flags & MEMPOOL_F_SC_GET)
+		rg_flags |= RING_F_SC_DEQ;
+

rg_flags not used anywhere down

Quoted 37 lines
lib/librte_mempool/rte_mempool.c +37 -0
+	rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
+
+	/*
+	 * reserve a memory zone for this mempool: private data is
+	 * cache-aligned
+	 */
+	private_data_size = RTE_ALIGN_CEIL(private_data_size,
+							RTE_MEMPOOL_ALIGN);
+
+	/* try to allocate tailq entry */
+	te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
+	if (te == NULL) {
+		RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
+		goto exit;
+	}
+
+	/*
+	 * If user provided an external memory buffer, then use it to
+	 * store mempool objects. Otherwise reserve a memzone that is large
+	 * enough to hold mempool header and metadata plus mempool objects.
+	 */
+	mempool_size = sizeof(*mp) + private_data_size;
+	mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
+
+	snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
+
+	mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
+
+	/* no more memory */
+	if (mz == NULL) {
+		rte_free(te);
+		goto exit;
+	}
+
+	/* init the mempool structure */
+	mp = mz->addr;
+	memset(mp, 0, sizeof(*mp));

lib/librte_mempool/rte_mempool.c void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *), · line 1000

Quoted 10 lines
lib/librte_mempool/rte_mempool.c +10 -0
+	snprintf(mp->name, sizeof(mp->name), "%s", name);
+	mp->phys_addr = mz->phys_addr;
+	mp->size = n;
+	mp->flags = flags;
+	mp->cache_size = cache_size;
+	mp->cache_flushthresh = CALC_CACHE_FLUSHTHRESH(cache_size);
+	mp->private_data_size = private_data_size;
+	mp->handler_idx = handler_idx;
+	mp->elt_size = elt_size;
+	mp->rt_pool = rte_mempool_ext_alloc(mp, name, n, socket_id, flags);

IMO, We can avoid the duplicaition of above code with rte_mempool_create. i.e rte_mempool_create -> rte_mempool_create_ext(..,"ring_mp_mc")

Quoted 128 lines
lib/librte_mempool/rte_mempool.c +19 -0
+
+	/* call the initializer */
+	if (mp_init)
+		mp_init(mp, mp_init_arg);
+
+	mempool_populate(mp, n, 1, obj_init, obj_init_arg);
+
+	te->data = (void *) mp;
+
+	rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
+	TAILQ_INSERT_TAIL(mempool_list, te, next);
+	rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
+
+exit:
+	rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
+
+	return mp;
+
+}
lib/librte_mempool/rte_mempool.h +77 -1
index 6e2390a..620cfb7 100644
@@ -88,6 +88,8 @@ extern "C" {
 struct rte_mempool_debug_stats {
 	uint64_t put_bulk;         /**< Number of puts. */
 	uint64_t put_objs;         /**< Number of objects successfully put. */
+	uint64_t put_pool_bulk;    /**< Number of puts into pool. */
+	uint64_t put_pool_objs;    /**< Number of objects into pool. */
 	uint64_t get_success_bulk; /**< Successful allocation number. */
 	uint64_t get_success_objs; /**< Objects successfully allocated. */
 	uint64_t get_fail_bulk;    /**< Failed allocation number. */
@@ -123,6 +125,7 @@ struct rte_mempool_objsz {
 #define RTE_MEMPOOL_NAMESIZE 32 /**< Maximum length of a memory pool. */
 #define RTE_MEMPOOL_MZ_PREFIX "MP_"
 
+
 /* "MP_<name>" */
 #define	RTE_MEMPOOL_MZ_FORMAT	RTE_MEMPOOL_MZ_PREFIX "%s"
 
@@ -175,12 +178,85 @@ struct rte_mempool_objtlr {
 #endif
 };
 
+/* Handler functions for external mempool support */
+typedef void *(*rte_mempool_alloc_t)(struct rte_mempool *mp,
+		const char *name, unsigned n, int socket_id, unsigned flags);
+typedef int (*rte_mempool_put_t)(void *p,
+		void * const *obj_table, unsigned n);
+typedef int (*rte_mempool_get_t)(void *p, void **obj_table,
+		unsigned n);
+typedef unsigned (*rte_mempool_get_count)(void *p);
+typedef int(*rte_mempool_free_t)(struct rte_mempool *mp);
+
+/**
+ * @internal wrapper for external mempool manager alloc callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @param name
+ *   Name of the statistics field to increment in the memory pool.
+ * @param n
+ *   Number to add to the object-oriented statistics.
+ * @param socket_id
+ *   socket id on which to allocate.
+ * @param flags
+ *   general flags to allocate function
+ */
+void *
+rte_mempool_ext_alloc(struct rte_mempool *mp,
+		const char *name, unsigned n, int socket_id, unsigned flags);
+
+/**
+ * @internal wrapper for external mempool manager get callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @param obj_table
+ *   Pointer to a table of void * pointers (objects).
+ * @param n
+ *	 Number of objects to get
+ */
+int
+rte_mempool_ext_get_bulk(struct rte_mempool *mp, void **obj_table,
+		unsigned n);
+
+/**
+ * @internal wrapper for external mempool manager put callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @param obj_table
+ *   Pointer to a table of void * pointers (objects).
+ * @param n
+ *   Number of objects to put
+ */
+int
+rte_mempool_ext_put_bulk(struct rte_mempool *mp, void * const *obj_table,
+		unsigned n);
+
+/**
+ * @internal wrapper for external mempool manager get_count callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ */
+int
+rte_mempool_ext_get_count(const struct rte_mempool *mp);
+
+/**
+ * @internal wrapper for external mempool manager free callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ */
+int
+rte_mempool_ext_free(struct rte_mempool *mp);
+
 /**
  * The RTE mempool structure.
  */
 struct rte_mempool {
 	char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
-	struct rte_ring *ring;           /**< Ring to store objects. */
 	phys_addr_t phys_addr;           /**< Phys. addr. of mempool struct. */
 	int flags;                       /**< Flags of the mempool. */
 	uint32_t size;                   /**< Size of the mempool. */

lib/librte_mempool/rte_mempool.h struct rte_mempool { · line 274

Quoted 5 lines
lib/librte_mempool/rte_mempool.h +2 -0
@@ -194,6 +270,11 @@ struct rte_mempool {
 
 	unsigned private_data_size;      /**< Size of private data. */
 
+	/* Common pool data structure pointer */
+	void *rt_pool __rte_cache_aligned;

Do we need to split rt_pool to next cache line, "cache_size" variable, etc are used in fast path, and one more cache line will occupy for this change

Quoted 492 lines
lib/librte_mempool/rte_mempool.h +97 -30
+
+	int16_t handler_idx;
+
 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
 	/** Per-lcore local cache. */
 	struct rte_mempool_cache local_cache[RTE_MAX_LCORE];
@@ -223,6 +304,10 @@ struct rte_mempool {
 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
 #define MEMPOOL_F_SP_PUT         0x0004 /**< Default put is "single-producer".*/
 #define MEMPOOL_F_SC_GET         0x0008 /**< Default get is "single-consumer".*/
+#define MEMPOOL_F_USE_STACK      0x0010 /**< Use a stack for the common pool. */
+#define MEMPOOL_F_USE_TM         0x0020
+#define MEMPOOL_F_NO_SECONDARY   0x0040
+
 
 /**
  * @internal When debug is enabled, store some statistics.
@@ -728,7 +813,6 @@ rte_dom0_mempool_create(const char *name, unsigned n, unsigned elt_size,
 		rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
 		int socket_id, unsigned flags);
 
-
 /**
  * Dump the status of the mempool to the console.
  *
@@ -753,7 +837,7 @@ void rte_mempool_dump(FILE *f, const struct rte_mempool *mp);
  */
 static inline void __attribute__((always_inline))
 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
-		    unsigned n, int is_mp)
+		    unsigned n, __attribute__((unused)) int is_mp)
 {
 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
 	struct rte_mempool_cache *cache;
@@ -769,8 +853,7 @@ __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
 
 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
 	/* cache is not enabled or single producer or non-EAL thread */
-	if (unlikely(cache_size == 0 || is_mp == 0 ||
-		     lcore_id >= RTE_MAX_LCORE))
+	if (unlikely(cache_size == 0 || lcore_id >= RTE_MAX_LCORE))
 		goto ring_enqueue;
 
 	/* Go straight to ring if put would overflow mem allocated for cache */
@@ -793,8 +876,8 @@ __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
 
 	cache->len += n;
 
-	if (cache->len >= flushthresh) {
-		rte_ring_mp_enqueue_bulk(mp->ring, &cache->objs[cache_size],
+	if (unlikely(cache->len >= flushthresh)) {
+		rte_mempool_ext_put_bulk(mp, &cache->objs[cache_size],
 				cache->len - cache_size);
 		cache->len = cache_size;
 	}
@@ -804,22 +887,10 @@ __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
 ring_enqueue:
 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
 
-	/* push remaining objects in ring */
-#ifdef RTE_LIBRTE_MEMPOOL_DEBUG
-	if (is_mp) {
-		if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
-			rte_panic("cannot put objects in mempool\n");
-	}
-	else {
-		if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
-			rte_panic("cannot put objects in mempool\n");
-	}
-#else
-	if (is_mp)
-		rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
-	else
-		rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
-#endif
+	/* Increment stats counter to tell us how many pool puts happened */
+	__MEMPOOL_STAT_ADD(mp, put_pool, n);
+
+	rte_mempool_ext_put_bulk(mp, obj_table, n);
 }
 
 
@@ -943,7 +1014,7 @@ rte_mempool_put(struct rte_mempool *mp, void *obj)
  */
 static inline int __attribute__((always_inline))
 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
-		   unsigned n, int is_mc)
+		   unsigned n, __attribute__((unused))int is_mc)
 {
 	int ret;
 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
@@ -954,8 +1025,8 @@ __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
 	uint32_t cache_size = mp->cache_size;
 
 	/* cache is not enabled or single consumer */
-	if (unlikely(cache_size == 0 || is_mc == 0 ||
-		     n >= cache_size || lcore_id >= RTE_MAX_LCORE))
+	if (unlikely(cache_size == 0 || n >= cache_size ||
+						lcore_id >= RTE_MAX_LCORE))
 		goto ring_dequeue;
 
 	cache = &mp->local_cache[lcore_id];
@@ -967,7 +1038,8 @@ __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
 		uint32_t req = n + (cache_size - cache->len);
 
 		/* How many do we require i.e. number to fill the cache + the request */
-		ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
+		ret = rte_mempool_ext_get_bulk(mp,
+						&cache->objs[cache->len], req);
 		if (unlikely(ret < 0)) {
 			/*
 			 * In the offchance that we are buffer constrained,
@@ -995,10 +1067,7 @@ ring_dequeue:
 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
 
 	/* get remaining objects from ring */
-	if (is_mc)
-		ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
-	else
-		ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
+	ret = rte_mempool_ext_get_bulk(mp, obj_table, n);
 
 	if (ret < 0)
 		__MEMPOOL_STAT_ADD(mp, get_fail, n);
@@ -1401,6 +1470,82 @@ ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz,
 void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *arg),
 		      void *arg);
 
+/**
+ * Function to get an index to an external mempool manager
+ *
+ * @param name
+ *   The name of the mempool handler to search for in the list of handlers
+ * @return
+ *   The index of the mempool handler in the list of registered mempool
+ *   handlers
+ */
+int16_t
+rte_get_mempool_handler(const char *name);
+
+
+/**
+ * Create a new mempool named *name* in memory.
+ *
+ * This function uses an externally defined alloc callback to allocate memory.
+ * Its size is set to n elements.
+ * All elements of the mempool are allocated separately to the mempool header.
+ *
+ * @param name
+ *   The name of the mempool.
+ * @param n
+ *   The number of elements in the mempool. The optimum size (in terms of
+ *   memory usage) for a mempool is when n is a power of two minus one:
+ *   n = (2^q - 1).
+ * @param cache_size
+ *   If cache_size is non-zero, the rte_mempool library will try to
+ *   limit the accesses to the common lockless pool, by maintaining a
+ *   per-lcore object cache. This argument must be lower or equal to
+ *   CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE and n / 1.5. It is advised to choose
+ *   cache_size to have "n modulo cache_size == 0": if this is
+ *   not the case, some elements will always stay in the pool and will
+ *   never be used. The access to the per-lcore table is of course
+ *   faster than the multi-producer/consumer pool. The cache can be
+ *   disabled if the cache_size argument is set to 0; it can be useful to
+ *   avoid losing objects in cache. Note that even if not used, the
+ *   memory space for cache is always reserved in a mempool structure,
+ *   except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
+ * @param private_data_size
+ *   The size of the private data appended after the mempool
+ *   structure. This is useful for storing some private data after the
+ *   mempool structure, as is done for rte_mbuf_pool for example.
+ * @param mp_init
+ *   A function pointer that is called for initialization of the pool,
+ *   before object initialization. The user can initialize the private
+ *   data in this function if needed. This parameter can be NULL if
+ *   not needed.
+ * @param mp_init_arg
+ *   An opaque pointer to data that can be used in the mempool
+ *   constructor function.
+ * @param obj_init
+ *   A function pointer that is called for each object at
+ *   initialization of the pool. The user can set some meta data in
+ *   objects if needed. This parameter can be NULL if not needed.
+ *   The obj_init() function takes the mempool pointer, the init_arg,
+ *   the object pointer and the object number as parameters.
+ * @param obj_init_arg
+ *   An opaque pointer to data that can be used as an argument for
+ *   each call to the object constructor function.
+ * @param socket_id
+ *   The *socket_id* argument is the socket identifier in the case of
+ *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
+ *   constraint for the reserved zone.
+ * @param flags
+ * @return
+ *   The pointer to the new allocated mempool, on success. NULL on error
+ */
+struct rte_mempool *
+rte_mempool_create_ext(const char *name, unsigned n, unsigned elt_size,
+		unsigned cache_size, unsigned private_data_size,
+		rte_mempool_ctor_t *mp_init, void *mp_init_arg,
+		rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
+		int socket_id, unsigned flags,
+		const char *handler_name);
+
 #ifdef __cplusplus
 }
 #endif
lib/librte_mempool/rte_mempool_default.c new file +229 -0
new file mode 100644
index 0000000..2493dc1
@@ -0,0 +1,229 @@
+/*-
+ *   BSD LICENSE
+ *
+ *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <rte_mempool.h>
+#include <rte_malloc.h>
+#include <string.h>
+
+#include "rte_mempool_internal.h"
+
+/*
+ * Indirect jump table to support external memory pools
+ */
+struct rte_mempool_handler_list mempool_handler_list = {
+	.sl =  RTE_SPINLOCK_INITIALIZER ,
+	.num_handlers = 0
+};
+
+/* TODO Convert to older mechanism of an array of stucts */
+int16_t
+add_handler(struct rte_mempool_handler *h)
+{
+	int16_t handler_idx;
+
+	/*  */
+	rte_spinlock_lock(&mempool_handler_list.sl);
+
+	/* Check whether jump table has space */
+	if (mempool_handler_list.num_handlers >= RTE_MEMPOOL_MAX_HANDLER_IDX) {
+		rte_spinlock_unlock(&mempool_handler_list.sl);
+		RTE_LOG(ERR, MEMPOOL,
+				"Maximum number of mempool handlers exceeded\n");
+		return -1;
+	}
+
+	if ((h->put == NULL) || (h->get == NULL) ||
+		(h->get_count == NULL)) {
+		rte_spinlock_unlock(&mempool_handler_list.sl);
+		 RTE_LOG(ERR, MEMPOOL,
+					"Missing callback while registering mempool handler\n");
+		return -1;
+	}
+
+	/* add new handler index */
+	handler_idx = mempool_handler_list.num_handlers++;
+
+	snprintf(mempool_handler_list.handler[handler_idx].name,
+				RTE_MEMPOOL_NAMESIZE, "%s", h->name);
+	mempool_handler_list.handler[handler_idx].alloc = h->alloc;
+	mempool_handler_list.handler[handler_idx].put = h->put;
+	mempool_handler_list.handler[handler_idx].get = h->get;
+	mempool_handler_list.handler[handler_idx].get_count = h->get_count;
+
+	rte_spinlock_unlock(&mempool_handler_list.sl);
+
+	return handler_idx;
+}
+
+/* TODO Convert to older mechanism of an array of stucts */
+int16_t
+rte_get_mempool_handler(const char *name)
+{
+	int16_t i;
+
+	for (i = 0; i < mempool_handler_list.num_handlers; i++) {
+		if (!strcmp(name, mempool_handler_list.handler[i].name))
+			return i;
+	}
+	return -1;
+}
+
+static int
+common_ring_mp_put(void *p, void * const *obj_table, unsigned n)
+{
+	return rte_ring_mp_enqueue_bulk((struct rte_ring *)p, obj_table, n);
+}
+
+static int
+common_ring_sp_put(void *p, void * const *obj_table, unsigned n)
+{
+	return rte_ring_sp_enqueue_bulk((struct rte_ring *)p, obj_table, n);
+}
+
+static int
+common_ring_mc_get(void *p, void **obj_table, unsigned n)
+{
+	return rte_ring_mc_dequeue_bulk((struct rte_ring *)p, obj_table, n);
+}
+
+static int
+common_ring_sc_get(void *p, void **obj_table, unsigned n)
+{
+	return rte_ring_sc_dequeue_bulk((struct rte_ring *)p, obj_table, n);
+}
+
+static unsigned
+common_ring_get_count(void *p)
+{
+	return rte_ring_count((struct rte_ring *)p);
+}
+
+
+static void *
+rte_mempool_common_ring_alloc(struct rte_mempool *mp,
+		const char *name, unsigned n, int socket_id, unsigned flags)
+{
+	struct rte_ring *r;
+	char rg_name[RTE_RING_NAMESIZE];
+	int rg_flags = 0;
+
+	if (flags & MEMPOOL_F_SP_PUT)
+		rg_flags |= RING_F_SP_ENQ;
+	if (flags & MEMPOOL_F_SC_GET)
+		rg_flags |= RING_F_SC_DEQ;
+
+	/* allocate the ring that will be used to store objects */
+	/* Ring functions will return appropriate errors if we are
+	 * running as a secondary process etc., so no checks made
+	 * in this function for that condition */
+	snprintf(rg_name, sizeof(rg_name), "%s-ring", name);
+	r = rte_ring_create(rg_name, rte_align32pow2(n+1), socket_id, rg_flags);
+	if (r == NULL)
+		return NULL;
+
+	mp->rt_pool = (void *)r;
+
+	return (void *) r;
+}
+
+void *
+rte_mempool_ext_alloc(struct rte_mempool *mp,
+		const char *name, unsigned n, int socket_id, unsigned flags)
+{
+	if (mempool_handler_list.handler[mp->handler_idx].alloc) {
+		return (mempool_handler_list.handler[mp->handler_idx].alloc)
+						(mp, name, n, socket_id, flags);
+	}
+	return NULL;
+}
+
+inline int __attribute__((always_inline))
+rte_mempool_ext_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
+{
+	return (mempool_handler_list.handler[mp->handler_idx].get)
+						(mp->rt_pool, obj_table, n);
+}
+
+inline int __attribute__((always_inline))
+rte_mempool_ext_put_bulk(struct rte_mempool *mp, void * const *obj_table,
+		unsigned n)
+{
+	return (mempool_handler_list.handler[mp->handler_idx].put)
+						(mp->rt_pool, obj_table, n);
+}
+
+int
+rte_mempool_ext_get_count(const struct rte_mempool *mp)
+{
+	return (mempool_handler_list.handler[mp->handler_idx].get_count)
+						(mp->rt_pool);
+}
+
+static struct rte_mempool_handler handler_mp_mc = {
+	.name = "ring_mp_mc",
+	.alloc = rte_mempool_common_ring_alloc,
+	.put = common_ring_mp_put,
+	.get = common_ring_mc_get,
+	.get_count = common_ring_get_count,
+	.free = NULL
+};
+static struct rte_mempool_handler handler_sp_sc = {
+	.name = "ring_sp_sc",
+	.alloc = rte_mempool_common_ring_alloc,
+	.put = common_ring_sp_put,
+	.get = common_ring_sc_get,
+	.get_count = common_ring_get_count,
+	.free = NULL
+};
+static struct rte_mempool_handler handler_mp_sc = {
+	.name = "ring_mp_sc",
+	.alloc = rte_mempool_common_ring_alloc,
+	.put = common_ring_mp_put,
+	.get = common_ring_sc_get,
+	.get_count = common_ring_get_count,
+	.free = NULL
+};
+static struct rte_mempool_handler handler_sp_mc = {
+	.name = "ring_sp_mc",
+	.alloc = rte_mempool_common_ring_alloc,
+	.put = common_ring_sp_put,
+	.get = common_ring_mc_get,
+	.get_count = common_ring_get_count,
+	.free = NULL
+};
+
+REGISTER_MEMPOOL_HANDLER(handler_mp_mc);
+REGISTER_MEMPOOL_HANDLER(handler_sp_sc);
+REGISTER_MEMPOOL_HANDLER(handler_mp_sc);
+REGISTER_MEMPOOL_HANDLER(handler_sp_mc);
lib/librte_mempool/rte_mempool_internal.h new file +44 -0
new file mode 100644
index 0000000..92b7bde
@@ -0,0 +1,74 @@
+/*-
+ *   BSD LICENSE
+ *
+ *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _RTE_MEMPOOL_INTERNAL_H_
+#define _RTE_MEMPOOL_INTERNAL_H_
+
+#include <rte_spinlock.h>
+#include <rte_mempool.h>
+
+#define RTE_MEMPOOL_MAX_HANDLER_IDX 16
+
+struct rte_mempool_handler {
+	char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool handler */
+

lib/librte_mempool/rte_mempool_internal.h line 54

Quoted 10 lines
lib/librte_mempool/rte_mempool_internal.h new file +10 -0
+	rte_mempool_alloc_t alloc;
+
+	rte_mempool_put_t put __rte_cache_aligned;
+
+	rte_mempool_get_t get __rte_cache_aligned;
+
+	rte_mempool_get_count get_count __rte_cache_aligned;
+
+	rte_mempool_free_t free __rte_cache_aligned;
+};

IMO, The structure should cache aligned not the individual elements as elements are likely read only in fast path.

Quoted 23 lines
lib/librte_mempool/rte_mempool_internal.h new file +20 -0
+
+struct rte_mempool_handler_list {
+	rte_spinlock_t sl;		  /**< Spinlock for add/delete. */
+
+	int32_t num_handlers;	  /**< Number of handlers that are valid. */
+
+	/* storage for all possible handlers */
+	struct rte_mempool_handler handler[RTE_MEMPOOL_MAX_HANDLER_IDX];
+};
+
+int16_t add_handler(struct rte_mempool_handler *h);
+
+#define REGISTER_MEMPOOL_HANDLER(h) \
+static int16_t __attribute__((used)) testfn_##h(void);\
+int16_t __attribute__((constructor, used)) testfn_##h(void)\
+{\
+	return add_handler(&h);\
+}
+
+#endif
Signature · 1 line
1.9.3
#3 lore raw

Re: [PATCH 1/5] mempool: add external mempool manager support

Quoted 198 lines · Jerin Jacob
Quoted 197 lines · David Hunt

Adds the new rte_mempool_create_ext api and callback mechanism for external mempool handlers

Modifies the existing rte_mempool_create to set up the handler_idx to the relevant mempool handler based on the handler name:
ring_sp_sc
ring_mp_mc
ring_sp_mc
ring_mp_sc

---
  app/test/test_mempool_perf.c              |   1 -
  lib/librte_mempool/Makefile               |   1 +
  lib/librte_mempool/rte_mempool.c          | 210 +++++++++++++++++++--------
  lib/librte_mempool/rte_mempool.h          | 207 +++++++++++++++++++++++----
  lib/librte_mempool/rte_mempool_default.c  | 229 ++++++++++++++++++++++++++++++
  lib/librte_mempool/rte_mempool_internal.h |  74 ++++++++++
  6 files changed, 634 insertions(+), 88 deletions(-)

create mode 100644 lib/librte_mempool/rte_mempool_default.c create mode 100644 lib/librte_mempool/rte_mempool_internal.h

app/test/test_mempool_perf.c +0 -1
index cdc02a0..091c1df 100644
@@ -161,7 +161,6 @@ per_lcore_mempool_test(__attribute__((unused)) void *arg)
  							   n_get_bulk);
  				if (unlikely(ret < 0)) {
  					rte_mempool_dump(stdout, mp);
-					rte_ring_dump(stdout, mp->ring);
  					/* in this case, objects are lost... */
  					return -1;
  				}
lib/librte_mempool/Makefile +1 -0
index a6898ef..7c81ef6 100644
@@ -42,6 +42,7 @@ LIBABIVER := 1
 
  # all source are stored in SRCS-y
  SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_default.c
  ifeq ($(CONFIG_RTE_LIBRTE_XEN_DOM0),y)
  SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_dom0_mempool.c
  endif
lib/librte_mempool/rte_mempool.c +30 -49
index aff5f6d..8c01838 100644
@@ -59,10 +59,11 @@
  #include <rte_spinlock.h>
 
  #include "rte_mempool.h"
+#include "rte_mempool_internal.h"
 
  TAILQ_HEAD(rte_mempool_list, rte_tailq_entry);
 
-static struct rte_tailq_elem rte_mempool_tailq = {
+struct rte_tailq_elem rte_mempool_tailq = {
  	.name = "RTE_MEMPOOL",
  };
  EAL_REGISTER_TAILQ(rte_mempool_tailq)
@@ -149,7 +150,7 @@ mempool_add_elem(struct rte_mempool *mp, void *obj, uint32_t obj_idx,
  		obj_init(mp, obj_init_arg, obj, obj_idx);
 
  	/* enqueue in ring */
-	rte_ring_sp_enqueue(mp->ring, obj);
+	rte_mempool_ext_put_bulk(mp, &obj, 1);
  }
 
  uint32_t
@@ -375,48 +376,28 @@ rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz,
  	return usz;
  }
 
-#ifndef RTE_LIBRTE_XEN_DOM0
-/* stub if DOM0 support not configured */
-struct rte_mempool *
-rte_dom0_mempool_create(const char *name __rte_unused,
-			unsigned n __rte_unused,
-			unsigned elt_size __rte_unused,
-			unsigned cache_size __rte_unused,
-			unsigned private_data_size __rte_unused,
-			rte_mempool_ctor_t *mp_init __rte_unused,
-			void *mp_init_arg __rte_unused,
-			rte_mempool_obj_ctor_t *obj_init __rte_unused,
-			void *obj_init_arg __rte_unused,
-			int socket_id __rte_unused,
-			unsigned flags __rte_unused)
-{
-	rte_errno = EINVAL;
-	return NULL;
-}
-#endif
-
  /* create the mempool */
  struct rte_mempool *
  rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
-		   unsigned cache_size, unsigned private_data_size,
-		   rte_mempool_ctor_t *mp_init, void *mp_init_arg,
-		   rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
-		   int socket_id, unsigned flags)
+			unsigned cache_size, unsigned private_data_size,
+			rte_mempool_ctor_t *mp_init, void *mp_init_arg,
+			rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
+			int socket_id, unsigned flags)
  {
  	if (rte_xen_dom0_supported())
  		return rte_dom0_mempool_create(name, n, elt_size,
-					       cache_size, private_data_size,
-					       mp_init, mp_init_arg,
-					       obj_init, obj_init_arg,
-					       socket_id, flags);
+			cache_size, private_data_size,
+			mp_init, mp_init_arg,
+			obj_init, obj_init_arg,
+			socket_id, flags);
  	else
  		return rte_mempool_xmem_create(name, n, elt_size,
-					       cache_size, private_data_size,
-					       mp_init, mp_init_arg,
-					       obj_init, obj_init_arg,
-					       socket_id, flags,
-					       NULL, NULL, MEMPOOL_PG_NUM_DEFAULT,
-					       MEMPOOL_PG_SHIFT_MAX);
+			cache_size, private_data_size,
+			mp_init, mp_init_arg,
+			obj_init, obj_init_arg,
+			socket_id, flags,
+			NULL, NULL,
+			MEMPOOL_PG_NUM_DEFAULT, MEMPOOL_PG_SHIFT_MAX);
  }
 
  /*
@@ -435,11 +416,9 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
  		const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
  {
  	char mz_name[RTE_MEMZONE_NAMESIZE];
-	char rg_name[RTE_RING_NAMESIZE];
  	struct rte_mempool_list *mempool_list;
  	struct rte_mempool *mp = NULL;
  	struct rte_tailq_entry *te;
-	struct rte_ring *r;
  	const struct rte_memzone *mz;
  	size_t mempool_size;
  	int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
@@ -469,7 +448,7 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
 
  	/* asked cache too big */
  	if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
-	    CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
+		CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
  		rte_errno = EINVAL;
  		return NULL;
  	}
@@ -502,16 +481,8 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
  		return NULL;
  	}
 
-	rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
 
-	/* allocate the ring that will be used to store objects */
-	/* Ring functions will return appropriate errors if we are
-	 * running as a secondary process etc., so no checks made
-	 * in this function for that condition */
-	snprintf(rg_name, sizeof(rg_name), RTE_MEMPOOL_MZ_FORMAT, name);
-	r = rte_ring_create(rg_name, rte_align32pow2(n+1), socket_id, rg_flags);
-	if (r == NULL)
-		goto exit;
+	rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
 
  	/*
  	 * reserve a memory zone for this mempool: private data is
@@ -588,7 +559,6 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
  	memset(mp, 0, sizeof(*mp));
  	snprintf(mp->name, sizeof(mp->name), "%s", name);
  	mp->phys_addr = mz->phys_addr;
-	mp->ring = r;
  	mp->size = n;
  	mp->flags = flags;
  	mp->elt_size = objsz.elt_size;
@@ -598,6 +568,22 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
  	mp->cache_flushthresh = CALC_CACHE_FLUSHTHRESH(cache_size);
  	mp->private_data_size = private_data_size;
 
+	/*
+	 * Since we have 4 combinations of the SP/SC/MP/MC, and stack,
+	 * examine the
+	 * flags to set the correct index into the handler table.
+	 */
+	if (flags & MEMPOOL_F_USE_STACK)
+		mp->handler_idx = rte_get_mempool_handler("stack");
+	else if (flags & (MEMPOOL_F_SP_PUT | MEMPOOL_F_SC_GET))
+		mp->handler_idx = rte_get_mempool_handler("ring_sp_sc");
+	else if (flags & MEMPOOL_F_SP_PUT)
+		mp->handler_idx = rte_get_mempool_handler("ring_sp_mc");
Quoted 8 lines

lib/librte_mempool/rte_mempool.c rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size, · line 586

Quoted 5 lines
lib/librte_mempool/rte_mempool.c +5 -0
+	else if (flags & MEMPOOL_F_SC_GET)
+		mp->handler_idx = rte_get_mempool_handler("ring_mp_sc");
+	else
+		mp->handler_idx = rte_get_mempool_handler("ring_mp_mc");
+

Why still use flag based selection? Why not "name" based? See below for more description

The old API does not have a 'name' parameter, so needs to work out which
handler to use based on the flags. This is not necessary in the new API call, and so it uses the "name" based index.

Quoted 116 lines
Quoted 116 lines
lib/librte_mempool/rte_mempool.c +47 -7
  	/* calculate address of the first element for continuous mempool. */
  	obj = (char *)mp + MEMPOOL_HEADER_SIZE(mp, pg_num) +
  		private_data_size;
@@ -613,7 +599,6 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
  		mp->elt_va_start = (uintptr_t)obj;
  		mp->elt_pa[0] = mp->phys_addr +
  			(mp->elt_va_start - (uintptr_t)mp);
-
  	/* mempool elements in a separate chunk of memory. */
  	} else {
  		mp->elt_va_start = (uintptr_t)vaddr;
@@ -622,6 +607,10 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
 
  	mp->elt_va_end = mp->elt_va_start;
 
+	/* Parameters are setup. Call the mempool handler alloc */
+	if ((rte_mempool_ext_alloc(mp, name, n, socket_id, flags)) == NULL)
+		goto exit;
+
  	/* call the initializer */
  	if (mp_init)
  		mp_init(mp, mp_init_arg);
@@ -646,7 +635,7 @@ rte_mempool_count(const struct rte_mempool *mp)
  {
  	unsigned count;
 
-	count = rte_ring_count(mp->ring);
+	count = rte_mempool_ext_get_count(mp);
 
  #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
  	{
@@ -681,7 +670,9 @@ rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
  	fprintf(f, "    cache_size=%"PRIu32"\n", mp->cache_size);
  	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
  		cache_count = mp->local_cache[lcore_id].len;
-		fprintf(f, "    cache_count[%u]=%u\n", lcore_id, cache_count);
+		if (cache_count > 0)
+			fprintf(f, "    cache_count[%u]=%u\n",
+						lcore_id, cache_count);
  		count += cache_count;
  	}
  	fprintf(f, "    total_cache_count=%u\n", count);
@@ -802,14 +793,13 @@ rte_mempool_dump(FILE *f, const struct rte_mempool *mp)
 
  	fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
  	fprintf(f, "  flags=%x\n", mp->flags);
-	fprintf(f, "  ring=<%s>@%p\n", mp->ring->name, mp->ring);
  	fprintf(f, "  phys_addr=0x%" PRIx64 "\n", mp->phys_addr);
  	fprintf(f, "  size=%"PRIu32"\n", mp->size);
  	fprintf(f, "  header_size=%"PRIu32"\n", mp->header_size);
  	fprintf(f, "  elt_size=%"PRIu32"\n", mp->elt_size);
  	fprintf(f, "  trailer_size=%"PRIu32"\n", mp->trailer_size);
  	fprintf(f, "  total_obj_size=%"PRIu32"\n",
-	       mp->header_size + mp->elt_size + mp->trailer_size);
+		   mp->header_size + mp->elt_size + mp->trailer_size);
 
  	fprintf(f, "  private_data_size=%"PRIu32"\n", mp->private_data_size);
  	fprintf(f, "  pg_num=%"PRIu32"\n", mp->pg_num);
@@ -825,7 +815,7 @@ rte_mempool_dump(FILE *f, const struct rte_mempool *mp)
  			mp->size);
 
  	cache_count = rte_mempool_dump_cache(f, mp);
-	common_count = rte_ring_count(mp->ring);
+	common_count = /* rte_ring_count(mp->ring)*/0;
  	if ((cache_count + common_count) > mp->size)
  		common_count = mp->size - cache_count;
  	fprintf(f, "  common_pool_count=%u\n", common_count);
@@ -904,7 +894,7 @@ rte_mempool_lookup(const char *name)
  }
 
  void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *),
-		      void *arg)
+			  void *arg)
  {
  	struct rte_tailq_entry *te = NULL;
  	struct rte_mempool_list *mempool_list;
@@ -919,3 +909,111 @@ void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *),
 
  	rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
  }
+
+
+/* create the mempool using and external mempool manager */
+struct rte_mempool *
+rte_mempool_create_ext(const char *name, unsigned n, unsigned elt_size,
+			unsigned cache_size, unsigned private_data_size,
+			rte_mempool_ctor_t *mp_init, void *mp_init_arg,
+			rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
+			int socket_id, unsigned flags,
+			const char *handler_name)
+{
+	char mz_name[RTE_MEMZONE_NAMESIZE];
+	struct rte_mempool_list *mempool_list;
+	struct rte_mempool *mp = NULL;
+	struct rte_tailq_entry *te;
+	const struct rte_memzone *mz;
+	size_t mempool_size;
+	int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
+	int rg_flags = 0;
+	int16_t handler_idx;
+
+	mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
+
+	/* asked cache too big */
+	if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
+		CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
+		rte_errno = EINVAL;
+		return NULL;
+	}
+
+	handler_idx = rte_get_mempool_handler(handler_name);
+	if (handler_idx < 0) {
+		RTE_LOG(ERR, MEMPOOL, "Cannot find mempool handler by name!\n");
+		goto exit;
+	}
+
Quoted 8 lines

lib/librte_mempool/rte_mempool.c void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *), · line 953

Quoted 6 lines
lib/librte_mempool/rte_mempool.c +6 -0
+	/* ring flags */
+	if (flags & MEMPOOL_F_SP_PUT)
+		rg_flags |= RING_F_SP_ENQ;
+	if (flags & MEMPOOL_F_SC_GET)
+		rg_flags |= RING_F_SC_DEQ;
+

rg_flags not used anywhere down

Thanks. I've removed them.

Quoted 43 lines
Quoted 43 lines
lib/librte_mempool/rte_mempool.c +43 -0
+	rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
+
+	/*
+	 * reserve a memory zone for this mempool: private data is
+	 * cache-aligned
+	 */
+	private_data_size = RTE_ALIGN_CEIL(private_data_size,
+							RTE_MEMPOOL_ALIGN);
+
+	/* try to allocate tailq entry */
+	te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
+	if (te == NULL) {
+		RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
+		goto exit;
+	}
+
+	/*
+	 * If user provided an external memory buffer, then use it to
+	 * store mempool objects. Otherwise reserve a memzone that is large
+	 * enough to hold mempool header and metadata plus mempool objects.
+	 */
+	mempool_size = sizeof(*mp) + private_data_size;
+	mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
+
+	snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
+
+	mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
+
+	/* no more memory */
+	if (mz == NULL) {
+		rte_free(te);
+		goto exit;
+	}
+
+	/* init the mempool structure */
+	mp = mz->addr;
+	memset(mp, 0, sizeof(*mp));
+	snprintf(mp->name, sizeof(mp->name), "%s", name);
+	mp->phys_addr = mz->phys_addr;
+	mp->size = n;
+	mp->flags = flags;
+	mp->cache_size = cache_size;
+	mp->cache_flushthresh = CALC_CACHE_FLUSHTHRESH(cache_size);
Quoted 8 lines

lib/librte_mempool/rte_mempool.c void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *), · line 1000

Quoted 4 lines
lib/librte_mempool/rte_mempool.c +4 -0
+	mp->private_data_size = private_data_size;
+	mp->handler_idx = handler_idx;
+	mp->elt_size = elt_size;
+	mp->rt_pool = rte_mempool_ext_alloc(mp, name, n, socket_id, flags);

IMO, We can avoid the duplicaition of above code with rte_mempool_create. i.e rte_mempool_create -> rte_mempool_create_ext(..,"ring_mp_mc")

rte_mempool_create is not really a subset of rte_mempool_create_ext, so doing this would not be possible. I did have a look at this before pusing the patch, but the code was so different in each case I decided to leave them as is. Maybe break out the section that sets up the mempool structure in to a separate functinon?

Quoted 129 lines
Quoted 129 lines
lib/librte_mempool/rte_mempool.c +19 -0
+
+	/* call the initializer */
+	if (mp_init)
+		mp_init(mp, mp_init_arg);
+
+	mempool_populate(mp, n, 1, obj_init, obj_init_arg);
+
+	te->data = (void *) mp;
+
+	rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
+	TAILQ_INSERT_TAIL(mempool_list, te, next);
+	rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
+
+exit:
+	rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
+
+	return mp;
+
+}
lib/librte_mempool/rte_mempool.h +77 -1
index 6e2390a..620cfb7 100644
@@ -88,6 +88,8 @@ extern "C" {
  struct rte_mempool_debug_stats {
  	uint64_t put_bulk;         /**< Number of puts. */
  	uint64_t put_objs;         /**< Number of objects successfully put. */
+	uint64_t put_pool_bulk;    /**< Number of puts into pool. */
+	uint64_t put_pool_objs;    /**< Number of objects into pool. */
  	uint64_t get_success_bulk; /**< Successful allocation number. */
  	uint64_t get_success_objs; /**< Objects successfully allocated. */
  	uint64_t get_fail_bulk;    /**< Failed allocation number. */
@@ -123,6 +125,7 @@ struct rte_mempool_objsz {
  #define RTE_MEMPOOL_NAMESIZE 32 /**< Maximum length of a memory pool. */
  #define RTE_MEMPOOL_MZ_PREFIX "MP_"
 
+
  /* "MP_<name>" */
  #define	RTE_MEMPOOL_MZ_FORMAT	RTE_MEMPOOL_MZ_PREFIX "%s"
 
@@ -175,12 +178,85 @@ struct rte_mempool_objtlr {
  #endif
  };
 
+/* Handler functions for external mempool support */
+typedef void *(*rte_mempool_alloc_t)(struct rte_mempool *mp,
+		const char *name, unsigned n, int socket_id, unsigned flags);
+typedef int (*rte_mempool_put_t)(void *p,
+		void * const *obj_table, unsigned n);
+typedef int (*rte_mempool_get_t)(void *p, void **obj_table,
+		unsigned n);
+typedef unsigned (*rte_mempool_get_count)(void *p);
+typedef int(*rte_mempool_free_t)(struct rte_mempool *mp);
+
+/**
+ * @internal wrapper for external mempool manager alloc callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @param name
+ *   Name of the statistics field to increment in the memory pool.
+ * @param n
+ *   Number to add to the object-oriented statistics.
+ * @param socket_id
+ *   socket id on which to allocate.
+ * @param flags
+ *   general flags to allocate function
+ */
+void *
+rte_mempool_ext_alloc(struct rte_mempool *mp,
+		const char *name, unsigned n, int socket_id, unsigned flags);
+
+/**
+ * @internal wrapper for external mempool manager get callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @param obj_table
+ *   Pointer to a table of void * pointers (objects).
+ * @param n
+ *	 Number of objects to get
+ */
+int
+rte_mempool_ext_get_bulk(struct rte_mempool *mp, void **obj_table,
+		unsigned n);
+
+/**
+ * @internal wrapper for external mempool manager put callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @param obj_table
+ *   Pointer to a table of void * pointers (objects).
+ * @param n
+ *   Number of objects to put
+ */
+int
+rte_mempool_ext_put_bulk(struct rte_mempool *mp, void * const *obj_table,
+		unsigned n);
+
+/**
+ * @internal wrapper for external mempool manager get_count callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ */
+int
+rte_mempool_ext_get_count(const struct rte_mempool *mp);
+
+/**
+ * @internal wrapper for external mempool manager free callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ */
+int
+rte_mempool_ext_free(struct rte_mempool *mp);
+
  /**
   * The RTE mempool structure.
   */
  struct rte_mempool {
  	char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
-	struct rte_ring *ring;           /**< Ring to store objects. */
  	phys_addr_t phys_addr;           /**< Phys. addr. of mempool struct. */
  	int flags;                       /**< Flags of the mempool. */
  	uint32_t size;                   /**< Size of the mempool. */
@@ -194,6 +270,11 @@ struct rte_mempool {
 
Quoted 8 lines

lib/librte_mempool/rte_mempool.h struct rte_mempool { · line 274

Quoted 4 lines
lib/librte_mempool/rte_mempool.h +2 -0
  	unsigned private_data_size;      /**< Size of private data. */
 
+	/* Common pool data structure pointer */
+	void *rt_pool __rte_cache_aligned;

Do we need to split rt_pool to next cache line, "cache_size" variable, etc are used in fast path, and one more cache line will occupy for this change

OK, I'll take out the split.

Quoted 497 lines
Quoted 497 lines
lib/librte_mempool/rte_mempool.h +97 -30
+
+	int16_t handler_idx;
+
  #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
  	/** Per-lcore local cache. */
  	struct rte_mempool_cache local_cache[RTE_MAX_LCORE];
@@ -223,6 +304,10 @@ struct rte_mempool {
  #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
  #define MEMPOOL_F_SP_PUT         0x0004 /**< Default put is "single-producer".*/
  #define MEMPOOL_F_SC_GET         0x0008 /**< Default get is "single-consumer".*/
+#define MEMPOOL_F_USE_STACK      0x0010 /**< Use a stack for the common pool. */
+#define MEMPOOL_F_USE_TM         0x0020
+#define MEMPOOL_F_NO_SECONDARY   0x0040
+
 
  /**
   * @internal When debug is enabled, store some statistics.
@@ -728,7 +813,6 @@ rte_dom0_mempool_create(const char *name, unsigned n, unsigned elt_size,
  		rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
  		int socket_id, unsigned flags);
 
-
  /**
   * Dump the status of the mempool to the console.
   *
@@ -753,7 +837,7 @@ void rte_mempool_dump(FILE *f, const struct rte_mempool *mp);
   */
  static inline void __attribute__((always_inline))
  __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
-		    unsigned n, int is_mp)
+		    unsigned n, __attribute__((unused)) int is_mp)
  {
  #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
  	struct rte_mempool_cache *cache;
@@ -769,8 +853,7 @@ __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
 
  #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
  	/* cache is not enabled or single producer or non-EAL thread */
-	if (unlikely(cache_size == 0 || is_mp == 0 ||
-		     lcore_id >= RTE_MAX_LCORE))
+	if (unlikely(cache_size == 0 || lcore_id >= RTE_MAX_LCORE))
  		goto ring_enqueue;
 
  	/* Go straight to ring if put would overflow mem allocated for cache */
@@ -793,8 +876,8 @@ __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
 
  	cache->len += n;
 
-	if (cache->len >= flushthresh) {
-		rte_ring_mp_enqueue_bulk(mp->ring, &cache->objs[cache_size],
+	if (unlikely(cache->len >= flushthresh)) {
+		rte_mempool_ext_put_bulk(mp, &cache->objs[cache_size],
  				cache->len - cache_size);
  		cache->len = cache_size;
  	}
@@ -804,22 +887,10 @@ __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
  ring_enqueue:
  #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
 
-	/* push remaining objects in ring */
-#ifdef RTE_LIBRTE_MEMPOOL_DEBUG
-	if (is_mp) {
-		if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
-			rte_panic("cannot put objects in mempool\n");
-	}
-	else {
-		if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
-			rte_panic("cannot put objects in mempool\n");
-	}
-#else
-	if (is_mp)
-		rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
-	else
-		rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
-#endif
+	/* Increment stats counter to tell us how many pool puts happened */
+	__MEMPOOL_STAT_ADD(mp, put_pool, n);
+
+	rte_mempool_ext_put_bulk(mp, obj_table, n);
  }
 
 
@@ -943,7 +1014,7 @@ rte_mempool_put(struct rte_mempool *mp, void *obj)
   */
  static inline int __attribute__((always_inline))
  __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
-		   unsigned n, int is_mc)
+		   unsigned n, __attribute__((unused))int is_mc)
  {
  	int ret;
  #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
@@ -954,8 +1025,8 @@ __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
  	uint32_t cache_size = mp->cache_size;
 
  	/* cache is not enabled or single consumer */
-	if (unlikely(cache_size == 0 || is_mc == 0 ||
-		     n >= cache_size || lcore_id >= RTE_MAX_LCORE))
+	if (unlikely(cache_size == 0 || n >= cache_size ||
+						lcore_id >= RTE_MAX_LCORE))
  		goto ring_dequeue;
 
  	cache = &mp->local_cache[lcore_id];
@@ -967,7 +1038,8 @@ __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
  		uint32_t req = n + (cache_size - cache->len);
 
  		/* How many do we require i.e. number to fill the cache + the request */
-		ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
+		ret = rte_mempool_ext_get_bulk(mp,
+						&cache->objs[cache->len], req);
  		if (unlikely(ret < 0)) {
  			/*
  			 * In the offchance that we are buffer constrained,
@@ -995,10 +1067,7 @@ ring_dequeue:
  #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
 
  	/* get remaining objects from ring */
-	if (is_mc)
-		ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
-	else
-		ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
+	ret = rte_mempool_ext_get_bulk(mp, obj_table, n);
 
  	if (ret < 0)
  		__MEMPOOL_STAT_ADD(mp, get_fail, n);
@@ -1401,6 +1470,82 @@ ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz,
  void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *arg),
  		      void *arg);
 
+/**
+ * Function to get an index to an external mempool manager
+ *
+ * @param name
+ *   The name of the mempool handler to search for in the list of handlers
+ * @return
+ *   The index of the mempool handler in the list of registered mempool
+ *   handlers
+ */
+int16_t
+rte_get_mempool_handler(const char *name);
+
+
+/**
+ * Create a new mempool named *name* in memory.
+ *
+ * This function uses an externally defined alloc callback to allocate memory.
+ * Its size is set to n elements.
+ * All elements of the mempool are allocated separately to the mempool header.
+ *
+ * @param name
+ *   The name of the mempool.
+ * @param n
+ *   The number of elements in the mempool. The optimum size (in terms of
+ *   memory usage) for a mempool is when n is a power of two minus one:
+ *   n = (2^q - 1).
+ * @param cache_size
+ *   If cache_size is non-zero, the rte_mempool library will try to
+ *   limit the accesses to the common lockless pool, by maintaining a
+ *   per-lcore object cache. This argument must be lower or equal to
+ *   CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE and n / 1.5. It is advised to choose
+ *   cache_size to have "n modulo cache_size == 0": if this is
+ *   not the case, some elements will always stay in the pool and will
+ *   never be used. The access to the per-lcore table is of course
+ *   faster than the multi-producer/consumer pool. The cache can be
+ *   disabled if the cache_size argument is set to 0; it can be useful to
+ *   avoid losing objects in cache. Note that even if not used, the
+ *   memory space for cache is always reserved in a mempool structure,
+ *   except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
+ * @param private_data_size
+ *   The size of the private data appended after the mempool
+ *   structure. This is useful for storing some private data after the
+ *   mempool structure, as is done for rte_mbuf_pool for example.
+ * @param mp_init
+ *   A function pointer that is called for initialization of the pool,
+ *   before object initialization. The user can initialize the private
+ *   data in this function if needed. This parameter can be NULL if
+ *   not needed.
+ * @param mp_init_arg
+ *   An opaque pointer to data that can be used in the mempool
+ *   constructor function.
+ * @param obj_init
+ *   A function pointer that is called for each object at
+ *   initialization of the pool. The user can set some meta data in
+ *   objects if needed. This parameter can be NULL if not needed.
+ *   The obj_init() function takes the mempool pointer, the init_arg,
+ *   the object pointer and the object number as parameters.
+ * @param obj_init_arg
+ *   An opaque pointer to data that can be used as an argument for
+ *   each call to the object constructor function.
+ * @param socket_id
+ *   The *socket_id* argument is the socket identifier in the case of
+ *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
+ *   constraint for the reserved zone.
+ * @param flags
+ * @return
+ *   The pointer to the new allocated mempool, on success. NULL on error
+ */
+struct rte_mempool *
+rte_mempool_create_ext(const char *name, unsigned n, unsigned elt_size,
+		unsigned cache_size, unsigned private_data_size,
+		rte_mempool_ctor_t *mp_init, void *mp_init_arg,
+		rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
+		int socket_id, unsigned flags,
+		const char *handler_name);
+
  #ifdef __cplusplus
  }
  #endif
lib/librte_mempool/rte_mempool_default.c new file +229 -0
new file mode 100644
index 0000000..2493dc1
@@ -0,0 +1,229 @@
+/*-
+ *   BSD LICENSE
+ *
+ *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <rte_mempool.h>
+#include <rte_malloc.h>
+#include <string.h>
+
+#include "rte_mempool_internal.h"
+
+/*
+ * Indirect jump table to support external memory pools
+ */
+struct rte_mempool_handler_list mempool_handler_list = {
+	.sl =  RTE_SPINLOCK_INITIALIZER ,
+	.num_handlers = 0
+};
+
+/* TODO Convert to older mechanism of an array of stucts */
+int16_t
+add_handler(struct rte_mempool_handler *h)
+{
+	int16_t handler_idx;
+
+	/*  */
+	rte_spinlock_lock(&mempool_handler_list.sl);
+
+	/* Check whether jump table has space */
+	if (mempool_handler_list.num_handlers >= RTE_MEMPOOL_MAX_HANDLER_IDX) {
+		rte_spinlock_unlock(&mempool_handler_list.sl);
+		RTE_LOG(ERR, MEMPOOL,
+				"Maximum number of mempool handlers exceeded\n");
+		return -1;
+	}
+
+	if ((h->put == NULL) || (h->get == NULL) ||
+		(h->get_count == NULL)) {
+		rte_spinlock_unlock(&mempool_handler_list.sl);
+		 RTE_LOG(ERR, MEMPOOL,
+					"Missing callback while registering mempool handler\n");
+		return -1;
+	}
+
+	/* add new handler index */
+	handler_idx = mempool_handler_list.num_handlers++;
+
+	snprintf(mempool_handler_list.handler[handler_idx].name,
+				RTE_MEMPOOL_NAMESIZE, "%s", h->name);
+	mempool_handler_list.handler[handler_idx].alloc = h->alloc;
+	mempool_handler_list.handler[handler_idx].put = h->put;
+	mempool_handler_list.handler[handler_idx].get = h->get;
+	mempool_handler_list.handler[handler_idx].get_count = h->get_count;
+
+	rte_spinlock_unlock(&mempool_handler_list.sl);
+
+	return handler_idx;
+}
+
+/* TODO Convert to older mechanism of an array of stucts */
+int16_t
+rte_get_mempool_handler(const char *name)
+{
+	int16_t i;
+
+	for (i = 0; i < mempool_handler_list.num_handlers; i++) {
+		if (!strcmp(name, mempool_handler_list.handler[i].name))
+			return i;
+	}
+	return -1;
+}
+
+static int
+common_ring_mp_put(void *p, void * const *obj_table, unsigned n)
+{
+	return rte_ring_mp_enqueue_bulk((struct rte_ring *)p, obj_table, n);
+}
+
+static int
+common_ring_sp_put(void *p, void * const *obj_table, unsigned n)
+{
+	return rte_ring_sp_enqueue_bulk((struct rte_ring *)p, obj_table, n);
+}
+
+static int
+common_ring_mc_get(void *p, void **obj_table, unsigned n)
+{
+	return rte_ring_mc_dequeue_bulk((struct rte_ring *)p, obj_table, n);
+}
+
+static int
+common_ring_sc_get(void *p, void **obj_table, unsigned n)
+{
+	return rte_ring_sc_dequeue_bulk((struct rte_ring *)p, obj_table, n);
+}
+
+static unsigned
+common_ring_get_count(void *p)
+{
+	return rte_ring_count((struct rte_ring *)p);
+}
+
+
+static void *
+rte_mempool_common_ring_alloc(struct rte_mempool *mp,
+		const char *name, unsigned n, int socket_id, unsigned flags)
+{
+	struct rte_ring *r;
+	char rg_name[RTE_RING_NAMESIZE];
+	int rg_flags = 0;
+
+	if (flags & MEMPOOL_F_SP_PUT)
+		rg_flags |= RING_F_SP_ENQ;
+	if (flags & MEMPOOL_F_SC_GET)
+		rg_flags |= RING_F_SC_DEQ;
+
+	/* allocate the ring that will be used to store objects */
+	/* Ring functions will return appropriate errors if we are
+	 * running as a secondary process etc., so no checks made
+	 * in this function for that condition */
+	snprintf(rg_name, sizeof(rg_name), "%s-ring", name);
+	r = rte_ring_create(rg_name, rte_align32pow2(n+1), socket_id, rg_flags);
+	if (r == NULL)
+		return NULL;
+
+	mp->rt_pool = (void *)r;
+
+	return (void *) r;
+}
+
+void *
+rte_mempool_ext_alloc(struct rte_mempool *mp,
+		const char *name, unsigned n, int socket_id, unsigned flags)
+{
+	if (mempool_handler_list.handler[mp->handler_idx].alloc) {
+		return (mempool_handler_list.handler[mp->handler_idx].alloc)
+						(mp, name, n, socket_id, flags);
+	}
+	return NULL;
+}
+
+inline int __attribute__((always_inline))
+rte_mempool_ext_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
+{
+	return (mempool_handler_list.handler[mp->handler_idx].get)
+						(mp->rt_pool, obj_table, n);
+}
+
+inline int __attribute__((always_inline))
+rte_mempool_ext_put_bulk(struct rte_mempool *mp, void * const *obj_table,
+		unsigned n)
+{
+	return (mempool_handler_list.handler[mp->handler_idx].put)
+						(mp->rt_pool, obj_table, n);
+}
+
+int
+rte_mempool_ext_get_count(const struct rte_mempool *mp)
+{
+	return (mempool_handler_list.handler[mp->handler_idx].get_count)
+						(mp->rt_pool);
+}
+
+static struct rte_mempool_handler handler_mp_mc = {
+	.name = "ring_mp_mc",
+	.alloc = rte_mempool_common_ring_alloc,
+	.put = common_ring_mp_put,
+	.get = common_ring_mc_get,
+	.get_count = common_ring_get_count,
+	.free = NULL
+};
+static struct rte_mempool_handler handler_sp_sc = {
+	.name = "ring_sp_sc",
+	.alloc = rte_mempool_common_ring_alloc,
+	.put = common_ring_sp_put,
+	.get = common_ring_sc_get,
+	.get_count = common_ring_get_count,
+	.free = NULL
+};
+static struct rte_mempool_handler handler_mp_sc = {
+	.name = "ring_mp_sc",
+	.alloc = rte_mempool_common_ring_alloc,
+	.put = common_ring_mp_put,
+	.get = common_ring_sc_get,
+	.get_count = common_ring_get_count,
+	.free = NULL
+};
+static struct rte_mempool_handler handler_sp_mc = {
+	.name = "ring_sp_mc",
+	.alloc = rte_mempool_common_ring_alloc,
+	.put = common_ring_sp_put,
+	.get = common_ring_mc_get,
+	.get_count = common_ring_get_count,
+	.free = NULL
+};
+
+REGISTER_MEMPOOL_HANDLER(handler_mp_mc);
+REGISTER_MEMPOOL_HANDLER(handler_sp_sc);
+REGISTER_MEMPOOL_HANDLER(handler_mp_sc);
+REGISTER_MEMPOOL_HANDLER(handler_sp_mc);
lib/librte_mempool/rte_mempool_internal.h new file +49 -0
new file mode 100644
index 0000000..92b7bde
@@ -0,0 +1,74 @@
+/*-
+ *   BSD LICENSE
+ *
+ *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _RTE_MEMPOOL_INTERNAL_H_
+#define _RTE_MEMPOOL_INTERNAL_H_
+
+#include <rte_spinlock.h>
+#include <rte_mempool.h>
+
+#define RTE_MEMPOOL_MAX_HANDLER_IDX 16
+
+struct rte_mempool_handler {
+	char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool handler */
+
+	rte_mempool_alloc_t alloc;
+
+	rte_mempool_put_t put __rte_cache_aligned;
+
+	rte_mempool_get_t get __rte_cache_aligned;
Quoted 8 lines

lib/librte_mempool/rte_mempool_internal.h line 54

Quoted 5 lines
lib/librte_mempool/rte_mempool_internal.h new file +5 -0
+
+	rte_mempool_get_count get_count __rte_cache_aligned;
+
+	rte_mempool_free_t free __rte_cache_aligned;
+};

IMO, The structure should cache aligned not the individual elements as elements are likely read only in fast path.

OK, Will have a look at some variations and take a look at performance.

Quoted 15 lines
Quoted 15 lines
lib/librte_mempool/rte_mempool_internal.h new file +15 -0
+
+struct rte_mempool_handler_list {
+	rte_spinlock_t sl;		  /**< Spinlock for add/delete. */
+
+	int32_t num_handlers;	  /**< Number of handlers that are valid. */
+
+	/* storage for all possible handlers */
+	struct rte_mempool_handler handler[RTE_MEMPOOL_MAX_HANDLER_IDX];
+};
+
+int16_t add_handler(struct rte_mempool_handler *h);
+
+#define REGISTER_MEMPOOL_HANDLER(h) \
+static int16_t __attribute__((used)) testfn_##h(void);\
+int16_t __attribute__((constructor, used)) testfn_##h(void)\
Quoted 8 lines
Quoted 8 lines
lib/librte_mempool/rte_mempool_internal.h new file +5 -1
+{\
+	return add_handler(&h);\
+}
+
+#endif
--

1.9.3

Thanks, comments appreciated.
Regard,
David.

#4 lore raw

Re: [PATCH 1/5] mempool: add external mempool manager support

Quoted 14 lines · David
Quoted 13 lines · Jerin Jacob
Quoted 12 lines · David Hunt

Adds the new rte_mempool_create_ext api and callback mechanism for external mempool handlers

Modifies the existing rte_mempool_create to set up the handler_idx to the relevant mempool handler based on the handler name:
ring_sp_sc
ring_mp_mc
ring_sp_mc
ring_mp_sc

---

[snip]

Quoted 16 lines
Quoted 6 lines
Quoted 6 lines
lib/librte_mempool/rte_mempool.c +6 -0
+	if (flags & MEMPOOL_F_USE_STACK)
+		mp->handler_idx = rte_get_mempool_handler("stack");
+	else if (flags & (MEMPOOL_F_SP_PUT | MEMPOOL_F_SC_GET))
+		mp->handler_idx = rte_get_mempool_handler("ring_sp_sc");
+	else if (flags & MEMPOOL_F_SP_PUT)
+		mp->handler_idx = rte_get_mempool_handler("ring_sp_mc");
Quoted 8 lines

lib/librte_mempool/rte_mempool.c rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size, · line 586

Quoted 5 lines
lib/librte_mempool/rte_mempool.c +5 -0
+	else if (flags & MEMPOOL_F_SC_GET)
+		mp->handler_idx = rte_get_mempool_handler("ring_mp_sc");
+	else
+		mp->handler_idx = rte_get_mempool_handler("ring_mp_mc");
+

Why still use flag based selection? Why not "name" based? See below for more description

Quoted 4 lines

The old API does not have a 'name' parameter, so needs to work out which handler to use based on the flags. This is not necessary in the new API call, and so it uses the "name" based index.

I agree. But the old API to new API mapping still can be done like below,
rte_mempool_create -> rte_mempool_create_ext(..,"ring_mp_mc")

[snip]

Quoted 20 lines
Quoted 10 lines
Quoted 10 lines
lib/librte_mempool/rte_mempool.c +9 -0
+	/* init the mempool structure */
+	mp = mz->addr;
+	memset(mp, 0, sizeof(*mp));
+	snprintf(mp->name, sizeof(mp->name), "%s", name);
+	mp->phys_addr = mz->phys_addr;
+	mp->size = n;
+	mp->flags = flags;
+	mp->cache_size = cache_size;
+	mp->cache_flushthresh = CALC_CACHE_FLUSHTHRESH(cache_size);
Quoted 8 lines

lib/librte_mempool/rte_mempool.c void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *), · line 1000

Quoted 4 lines
lib/librte_mempool/rte_mempool.c +4 -0
+	mp->private_data_size = private_data_size;
+	mp->handler_idx = handler_idx;
+	mp->elt_size = elt_size;
+	mp->rt_pool = rte_mempool_ext_alloc(mp, name, n, socket_id, flags);

IMO, We can avoid the duplicaition of above code with rte_mempool_create. i.e rte_mempool_create -> rte_mempool_create_ext(..,"ring_mp_mc")

Quoted 5 lines

rte_mempool_create is not really a subset of rte_mempool_create_ext, so doing this would not be possible. I did have a look at this before pusing the patch, but the code was so different in each case I decided to leave them as is. Maybe break out the section that sets up the mempool structure in to a separate functinon?

Yes, Their are a lot of common code between rte_mempool_create/rte_mempool_xmem_create and rte_mempool_create_ext.

IMO, we need to converge these functions. Otherwise, a new feature in mempool would call for changing in both places and it's difficult to maintain.
In my view, both external and internal pool manager should have the same code for creation and just the backend put/get/alloc function pointers will be different to maintain the functional consistency.

Thanks, comments appreciated.
Jerin

#5 lore raw

Re: [PATCH 1/5] mempool: add external mempool manager support

In reply to David Hunt

Hi David,

Nice work, thanks !
Please see some comments below.

Quoted 18 lines · David Hunt
lib/librte_mempool/rte_mempool.c +0 -10
index aff5f6d..8c01838 100644
@@ -375,48 +376,28 @@ rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz,
  	return usz;
  }
 
-#ifndef RTE_LIBRTE_XEN_DOM0
-/* stub if DOM0 support not configured */
-struct rte_mempool *
-rte_dom0_mempool_create(const char *name __rte_unused,
-			unsigned n __rte_unused,
-			unsigned elt_size __rte_unused,
-			unsigned cache_size __rte_unused,
-			unsigned private_data_size __rte_unused,
-			rte_mempool_ctor_t *mp_init __rte_unused,
-			void *mp_init_arg __rte_unused,

lib/librte_mempool/rte_mempool.c rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz, · line 397

Quoted 10 lines
lib/librte_mempool/rte_mempool.c +0 -10
-			rte_mempool_obj_ctor_t *obj_init __rte_unused,
-			void *obj_init_arg __rte_unused,
-			int socket_id __rte_unused,
-			unsigned flags __rte_unused)
-{
-	rte_errno = EINVAL;
-	return NULL;
-}
-#endif
-

Could we move this is a separated commit? "mempool: remove unused rte_dom0_mempool_create stub"

Quoted 27 lines
lib/librte_mempool/rte_mempool.c +8 -11
  /* create the mempool */
  struct rte_mempool *
  rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
-		   unsigned cache_size, unsigned private_data_size,
-		   rte_mempool_ctor_t *mp_init, void *mp_init_arg,
-		   rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
-		   int socket_id, unsigned flags)
+			unsigned cache_size, unsigned private_data_size,
+			rte_mempool_ctor_t *mp_init, void *mp_init_arg,
+			rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
+			int socket_id, unsigned flags)
  {
  	if (rte_xen_dom0_supported())
  		return rte_dom0_mempool_create(name, n, elt_size,
-					       cache_size, private_data_size,
-					       mp_init, mp_init_arg,
-					       obj_init, obj_init_arg,
-					       socket_id, flags);
+			cache_size, private_data_size,
+			mp_init, mp_init_arg,
+			obj_init, obj_init_arg,
+			socket_id, flags);
  	else
  		return rte_mempool_xmem_create(name, n, elt_size,
-					       cache_size, private_data_size,
-					       mp_init, mp_init_arg,
-					       obj_init, obj_init_arg,

lib/librte_mempool/rte_mempool.c rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz, · line 401

Quoted 10 lines
lib/librte_mempool/rte_mempool.c +6 -3
-					       socket_id, flags,
-					       NULL, NULL, MEMPOOL_PG_NUM_DEFAULT,
-					       MEMPOOL_PG_SHIFT_MAX);
+			cache_size, private_data_size,
+			mp_init, mp_init_arg,
+			obj_init, obj_init_arg,
+			socket_id, flags,
+			NULL, NULL,
+			MEMPOOL_PG_NUM_DEFAULT, MEMPOOL_PG_SHIFT_MAX);
  }

As far as I can see, you are not modifying the code here, only the style. For better readability, it should go in another commit that only fixes indent or style issues.

Also, I think the proper indentation is to use only one tab for the subsequent lines.
The coding style guide (doc/guides/contributing/coding_style.rst) is not very clear about this however.

lib/librte_mempool/rte_mempool.c rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size, · line 454

Quoted 9 lines
lib/librte_mempool/rte_mempool.c +1 -1
@@ -469,7 +448,7 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
 
  	/* asked cache too big */
  	if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
-	    CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
+		CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
  		rte_errno = EINVAL;
  		return NULL;
  	}

same here.

lib/librte_mempool/rte_mempool.c rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size, · line 575

Quoted 9 lines
lib/librte_mempool/rte_mempool.c +5 -0
@@ -598,6 +568,22 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
  	mp->cache_flushthresh = CALC_CACHE_FLUSHTHRESH(cache_size);
  	mp->private_data_size = private_data_size;
 
+	/*
+	 * Since we have 4 combinations of the SP/SC/MP/MC, and stack,
+	 * examine the
+	 * flags to set the correct index into the handler table.
+	 */

nit: comment style is not correct

lib/librte_mempool/rte_mempool.c rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size, · line 577

Quoted 2 lines
lib/librte_mempool/rte_mempool.c +2 -0
+	if (flags & MEMPOOL_F_USE_STACK)
+		mp->handler_idx = rte_get_mempool_handler("stack");

The stack handler does not exist yet, it is introduced in the next commit. I think this code should be moved in the next commit too.

lib/librte_mempool/rte_mempool.c rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size, · line 613

Quoted 8 lines
lib/librte_mempool/rte_mempool.c +4 -0
@@ -622,6 +607,10 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
 
  	mp->elt_va_end = mp->elt_va_start;
 
+	/* Parameters are setup. Call the mempool handler alloc */
+	if ((rte_mempool_ext_alloc(mp, name, n, socket_id, flags)) == NULL)
+		goto exit;
+

I think some memory needs to be freed here. At least 'te'. The memzone 'mz' is never freed in this function, even before your patch, but since Sergio's patch (commit ff909fe21f), we could fix that issue too.
I can submit a patch for it, or if you prefer, you can fix it in a separate patch of your series, just let me know.

lib/librte_mempool/rte_mempool.c rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp) · line 678

Quoted 11 lines
lib/librte_mempool/rte_mempool.c +3 -1
@@ -681,7 +670,9 @@ rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
  	fprintf(f, "    cache_size=%"PRIu32"\n", mp->cache_size);
  	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
  		cache_count = mp->local_cache[lcore_id].len;
-		fprintf(f, "    cache_count[%u]=%u\n", lcore_id, cache_count);
+		if (cache_count > 0)
+			fprintf(f, "    cache_count[%u]=%u\n",
+						lcore_id, cache_count);
  		count += cache_count;
  	}
  	fprintf(f, "    total_cache_count=%u\n", count);

This could also be moved in a separate commit.

lib/librte_mempool/rte_mempool.c rte_mempool_dump(FILE *f, const struct rte_mempool *mp) · line 799

Quoted 9 lines
lib/librte_mempool/rte_mempool.c +1 -1
@@ -802,14 +793,13 @@ rte_mempool_dump(FILE *f, const struct rte_mempool *mp)
  	fprintf(f, "  elt_size=%"PRIu32"\n", mp->elt_size);
  	fprintf(f, "  trailer_size=%"PRIu32"\n", mp->trailer_size);
  	fprintf(f, "  total_obj_size=%"PRIu32"\n",
-	       mp->header_size + mp->elt_size + mp->trailer_size);
+		   mp->header_size + mp->elt_size + mp->trailer_size);
 
  	fprintf(f, "  private_data_size=%"PRIu32"\n", mp->private_data_size);
  	fprintf(f, "  pg_num=%"PRIu32"\n", mp->pg_num);

to be moved in the "style" commit.

lib/librte_mempool/rte_mempool.c rte_mempool_dump(FILE *f, const struct rte_mempool *mp) · line 821

Quoted 9 lines
lib/librte_mempool/rte_mempool.c +1 -1
@@ -825,7 +815,7 @@ rte_mempool_dump(FILE *f, const struct rte_mempool *mp)
  			mp->size);
 
  	cache_count = rte_mempool_dump_cache(f, mp);
-	common_count = rte_ring_count(mp->ring);
+	common_count = /* rte_ring_count(mp->ring)*/0;
  	if ((cache_count + common_count) > mp->size)
  		common_count = mp->size - cache_count;
  	fprintf(f, "  common_pool_count=%u\n", common_count);

should it be rte_mempool_ext_get_count(mp) instead?

lib/librte_mempool/rte_mempool.c rte_mempool_lookup(const char *name) · line 900

Quoted 9 lines
lib/librte_mempool/rte_mempool.c +1 -1
@@ -904,7 +894,7 @@ rte_mempool_lookup(const char *name)
  }
 
  void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *),
-		      void *arg)
+			  void *arg)
  {
  	struct rte_tailq_entry *te = NULL;
  	struct rte_mempool_list *mempool_list;

to be moved in the "style" commit.

Quoted 5 lines
lib/librte_mempool/rte_mempool.c +1 -0
@@ -919,3 +909,111 @@ void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *),
 
  	rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
  }
+

lib/librte_mempool/rte_mempool.c void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *), · line 922

Quoted 10 lines
lib/librte_mempool/rte_mempool.c +10 -0
+
+/* create the mempool using and external mempool manager */
+struct rte_mempool *
+rte_mempool_create_ext(const char *name, unsigned n, unsigned elt_size,
+			unsigned cache_size, unsigned private_data_size,
+			rte_mempool_ctor_t *mp_init, void *mp_init_arg,
+			rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
+			int socket_id, unsigned flags,
+			const char *handler_name)
+{

I would have used one tab here for subsequent lines.

Quoted 22 lines
lib/librte_mempool/rte_mempool.c +22 -0
+	char mz_name[RTE_MEMZONE_NAMESIZE];
+	struct rte_mempool_list *mempool_list;
+	struct rte_mempool *mp = NULL;
+	struct rte_tailq_entry *te;
+	const struct rte_memzone *mz;
+	size_t mempool_size;
+	int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
+	int rg_flags = 0;
+	int16_t handler_idx;
+
+	mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
+
+	/* asked cache too big */
+	if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
+		CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
+		rte_errno = EINVAL;
+		return NULL;
+	}
+
+	handler_idx = rte_get_mempool_handler(handler_name);
+	if (handler_idx < 0) {
+		RTE_LOG(ERR, MEMPOOL, "Cannot find mempool handler by name!\n");

lib/librte_mempool/rte_mempool.c void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *), · line 953

Quoted 10 lines
lib/librte_mempool/rte_mempool.c +9 -0
+		goto exit;
+	}
+
+	/* ring flags */
+	if (flags & MEMPOOL_F_SP_PUT)
+		rg_flags |= RING_F_SP_ENQ;
+	if (flags & MEMPOOL_F_SC_GET)
+		rg_flags |= RING_F_SC_DEQ;
+
...

I have the same comment than Jerin here. I think it should be factorized with rte_mempool_xmem_create() if possible. Maybe a at least a function rte_mempool_init() could be introduced, in the same model than rte_ring_init().

lib/librte_mempool/rte_mempool.h extern "C" { · line 95

Quoted 13 lines
lib/librte_mempool/rte_mempool.h +2 -0
index 6e2390a..620cfb7 100644
@@ -88,6 +88,8 @@ extern "C" {
  struct rte_mempool_debug_stats {
  	uint64_t put_bulk;         /**< Number of puts. */
  	uint64_t put_objs;         /**< Number of objects successfully put. */
+	uint64_t put_pool_bulk;    /**< Number of puts into pool. */
+	uint64_t put_pool_objs;    /**< Number of objects into pool. */
  	uint64_t get_success_bulk; /**< Successful allocation number. */
  	uint64_t get_success_objs; /**< Objects successfully allocated. */
  	uint64_t get_fail_bulk;    /**< Failed allocation number. */

I think the comment of put_pool_objs is not very clear. Shouldn't we have the same stats for get?

lib/librte_mempool/rte_mempool.h struct rte_mempool_objsz { · line 131

Quoted 8 lines
lib/librte_mempool/rte_mempool.h +1 -0
@@ -123,6 +125,7 @@ struct rte_mempool_objsz {
  #define RTE_MEMPOOL_NAMESIZE 32 /**< Maximum length of a memory pool. */
  #define RTE_MEMPOOL_MZ_PREFIX "MP_"
 
+
  /* "MP_<name>" */
  #define	RTE_MEMPOOL_MZ_FORMAT	RTE_MEMPOOL_MZ_PREFIX "%s"
 

to be removed

lib/librte_mempool/rte_mempool.h struct rte_mempool_objtlr { · line 189

Quoted 13 lines
lib/librte_mempool/rte_mempool.h +9 -0
@@ -175,12 +178,85 @@ struct rte_mempool_objtlr {
  #endif
  };
 
+/* Handler functions for external mempool support */
+typedef void *(*rte_mempool_alloc_t)(struct rte_mempool *mp,
+		const char *name, unsigned n, int socket_id, unsigned flags);
+typedef int (*rte_mempool_put_t)(void *p,
+		void * const *obj_table, unsigned n);
+typedef int (*rte_mempool_get_t)(void *p, void **obj_table,
+		unsigned n);
+typedef unsigned (*rte_mempool_get_count)(void *p);
+typedef int(*rte_mempool_free_t)(struct rte_mempool *mp);

a space is missing after 'int'.

lib/librte_mempool/rte_mempool.h struct rte_mempool_objtlr { · line 199

Quoted 10 lines
lib/librte_mempool/rte_mempool.h +10 -0
+
+/**
+ * @internal wrapper for external mempool manager alloc callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @param name
+ *   Name of the statistics field to increment in the memory pool.
+ * @param n
+ *   Number to add to the object-oriented statistics.

Are this comments correct?

lib/librte_mempool/rte_mempool.h struct rte_mempool_objtlr { · line 203

Quoted 4 lines
lib/librte_mempool/rte_mempool.h +4 -0
+ * @param socket_id
+ *   socket id on which to allocate.
+ * @param flags
+ *   general flags to allocate function

We could add that we are talking about MEMPOOL_F_* flags.

By the way, the '@return' is missing in all declarations.

lib/librte_mempool/rte_mempool.h struct rte_mempool_objtlr { · line 244

Quoted 8 lines
lib/librte_mempool/rte_mempool.h +8 -0
+/**
+ * @internal wrapper for external mempool manager get_count callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ */
+int
+rte_mempool_ext_get_count(const struct rte_mempool *mp);

should it be unsigned instead of int?

Quoted 20 lines
lib/librte_mempool/rte_mempool.h +10 -1
+
+/**
+ * @internal wrapper for external mempool manager free callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ */
+int
+rte_mempool_ext_free(struct rte_mempool *mp);
+
  /**
   * The RTE mempool structure.
   */
  struct rte_mempool {
  	char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
-	struct rte_ring *ring;           /**< Ring to store objects. */
  	phys_addr_t phys_addr;           /**< Phys. addr. of mempool struct. */
  	int flags;                       /**< Flags of the mempool. */
  	uint32_t size;                   /**< Size of the mempool. */

lib/librte_mempool/rte_mempool.h struct rte_mempool { · line 274

Quoted 5 lines
lib/librte_mempool/rte_mempool.h +2 -0
@@ -194,6 +270,11 @@ struct rte_mempool {
 
  	unsigned private_data_size;      /**< Size of private data. */
 
+	/* Common pool data structure pointer */
+	void *rt_pool __rte_cache_aligned;

What is the meaning of rt_pool?

lib/librte_mempool/rte_mempool.h struct rte_mempool { · line 277

Quoted 3 lines
lib/librte_mempool/rte_mempool.h +3 -0
+
+	int16_t handler_idx;
+

I don't think I'm getting why an index is better than a pointer to the struct rte_mempool_handler. It would simplify the add_handler() function. See below for a detailed explaination.

lib/librte_mempool/rte_mempool.h struct rte_mempool { · line 307

Quoted 5 lines
lib/librte_mempool/rte_mempool.h +1 -0
@@ -223,6 +304,10 @@ struct rte_mempool {
  #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
  #define MEMPOOL_F_SP_PUT         0x0004 /**< Default put is "single-producer".*/
  #define MEMPOOL_F_SC_GET         0x0008 /**< Default get is "single-consumer".*/
+#define MEMPOOL_F_USE_STACK      0x0010 /**< Use a stack for the common pool. */

Stack is not implemented in this commit. It should be moved in next commit.

lib/librte_mempool/rte_mempool.h struct rte_mempool { · line 310

Quoted 3 lines
lib/librte_mempool/rte_mempool.h +3 -0
+#define MEMPOOL_F_USE_TM         0x0020
+#define MEMPOOL_F_NO_SECONDARY   0x0040
+

What are these flags?

lib/librte_mempool/rte_mempool.h rte_dom0_mempool_create(const char *name, unsigned n, unsigned elt_size, · line 818

Quoted 8 lines
lib/librte_mempool/rte_mempool.h +0 -1
@@ -728,7 +813,6 @@ rte_dom0_mempool_create(const char *name, unsigned n, unsigned elt_size,
  		rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
  		int socket_id, unsigned flags);
 
-
  /**
   * Dump the status of the mempool to the console.
   *

style

lib/librte_mempool/rte_mempool.h void rte_mempool_dump(FILE *f, const struct rte_mempool *mp); · line 840

Quoted 6 lines
lib/librte_mempool/rte_mempool.h +1 -1
@@ -753,7 +837,7 @@ void rte_mempool_dump(FILE *f, const struct rte_mempool *mp);
   */
  static inline void __attribute__((always_inline))
  __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
-		    unsigned n, int is_mp)
+		    unsigned n, __attribute__((unused)) int is_mp)

You could use __rte_unused instead of __attribute__((unused))

lib/librte_mempool/rte_mempool.h __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table, · line 859

Quoted 10 lines
lib/librte_mempool/rte_mempool.h +1 -2
@@ -769,8 +853,7 @@ __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
 
  #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
  	/* cache is not enabled or single producer or non-EAL thread */
-	if (unlikely(cache_size == 0 || is_mp == 0 ||
-		     lcore_id >= RTE_MAX_LCORE))
+	if (unlikely(cache_size == 0 || lcore_id >= RTE_MAX_LCORE))
  		goto ring_enqueue;
 
  	/* Go straight to ring if put would overflow mem allocated for cache */

If I understand well, we now always use the cache, even if the mempool is single-producer. I was wondering if it would have a performance impact... I suppose that using the cache is more efficient than the ring in single-producer mode, so it may increase performance. Do you have an idea of the impact here?

I think we could remove the parameter as the function is marked as internal. The comment above should also be fixed. The same comments apply to the get() functions.

lib/librte_mempool/rte_mempool.h __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table, · line 881

Quoted 9 lines
lib/librte_mempool/rte_mempool.h +2 -2
@@ -793,8 +876,8 @@ __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
 
  	cache->len += n;
 
-	if (cache->len >= flushthresh) {
-		rte_ring_mp_enqueue_bulk(mp->ring, &cache->objs[cache_size],
+	if (unlikely(cache->len >= flushthresh)) {
+		rte_mempool_ext_put_bulk(mp, &cache->objs[cache_size],
  				cache->len - cache_size);

Shouldn't we add a __MEMPOOL_STAT_ADD(mp, put_pool,
cache->len - cache_size) here ?

lib/librte_mempool/rte_mempool.h __mempool_get_bulk(struct rte_mempool *mp, void **obj_table, · line 1029

Quoted 8 lines
lib/librte_mempool/rte_mempool.h +2 -2
@@ -954,8 +1025,8 @@ __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
  	uint32_t cache_size = mp->cache_size;
 
  	/* cache is not enabled or single consumer */
-	if (unlikely(cache_size == 0 || is_mc == 0 ||
-		     n >= cache_size || lcore_id >= RTE_MAX_LCORE))
+	if (unlikely(cache_size == 0 || n >= cache_size ||
+						lcore_id >= RTE_MAX_LCORE))

incorrect indent

lib/librte_mempool/rte_mempool.h __mempool_get_bulk(struct rte_mempool *mp, void **obj_table, · line 1042

Quoted 7 lines
lib/librte_mempool/rte_mempool.h +2 -1
@@ -967,7 +1038,8 @@ __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
  		uint32_t req = n + (cache_size - cache->len);
 
  		/* How many do we require i.e. number to fill the cache + the request */
-		ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
+		ret = rte_mempool_ext_get_bulk(mp,
+						&cache->objs[cache->len], req);

indent

lib/librte_mempool/rte_mempool.h ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz, · line 1483

Quoted 11 lines
lib/librte_mempool/rte_mempool.h +11 -0
+/**
+ * Function to get an index to an external mempool manager
+ *
+ * @param name
+ *   The name of the mempool handler to search for in the list of handlers
+ * @return
+ *   The index of the mempool handler in the list of registered mempool
+ *   handlers
+ */
+int16_t
+rte_get_mempool_handler(const char *name);

I would prefer a function like this:

const struct rte_mempool_handler *
rte_get_mempool_handler(const char *name);

(detailed explaination below)

Quoted 44 lines
lib/librte_mempool/rte_mempool_default.c new file +39 -0
new file mode 100644
index 0000000..2493dc1
+#include "rte_mempool_internal.h"
+
+/*
+ * Indirect jump table to support external memory pools
+ */
+struct rte_mempool_handler_list mempool_handler_list = {
+	.sl =  RTE_SPINLOCK_INITIALIZER ,
+	.num_handlers = 0
+};
+
+/* TODO Convert to older mechanism of an array of stucts */
+int16_t
+add_handler(struct rte_mempool_handler *h)
+{
+	int16_t handler_idx;
+
+	/*  */
+	rte_spinlock_lock(&mempool_handler_list.sl);
+
+	/* Check whether jump table has space */
+	if (mempool_handler_list.num_handlers >= RTE_MEMPOOL_MAX_HANDLER_IDX) {
+		rte_spinlock_unlock(&mempool_handler_list.sl);
+		RTE_LOG(ERR, MEMPOOL,
+				"Maximum number of mempool handlers exceeded\n");
+		return -1;
+	}
+
+	if ((h->put == NULL) || (h->get == NULL) ||
+		(h->get_count == NULL)) {
+		rte_spinlock_unlock(&mempool_handler_list.sl);
+		 RTE_LOG(ERR, MEMPOOL,
+					"Missing callback while registering mempool handler\n");
+		return -1;
+	}
+
+	/* add new handler index */
+	handler_idx = mempool_handler_list.num_handlers++;
+
+	snprintf(mempool_handler_list.handler[handler_idx].name,

lib/librte_mempool/rte_mempool_default.c line 87

Quoted 10 lines
lib/librte_mempool/rte_mempool_default.c new file +10 -0
+				RTE_MEMPOOL_NAMESIZE, "%s", h->name);
+	mempool_handler_list.handler[handler_idx].alloc = h->alloc;
+	mempool_handler_list.handler[handler_idx].put = h->put;
+	mempool_handler_list.handler[handler_idx].get = h->get;
+	mempool_handler_list.handler[handler_idx].get_count = h->get_count;
+
+	rte_spinlock_unlock(&mempool_handler_list.sl);
+
+	return handler_idx;
+}

Why not using a similar mechanism than what we have for PMDs?

	void rte_eal_driver_register(struct rte_driver *driver)
	{
		TAILQ_INSERT_TAIL(&dev_driver_list, driver, next);
	}

To do that, you just need to add a TAILQ_ENTRY() in your rte_mempool_handler structure. This would avoid to duplicate the structure into a static array whose size is limited.

Accessing to the callbacks would be easier:

	return mp->mp_handler->put(mp->rt_pool, obj_table, n);

instead of:

	return (mempool_handler_list.handler[mp->handler_idx].put)
					(mp->rt_pool, obj_table, n);

If we really want to copy the handlers somewhere, it could be in the mempool structure. It would avoid an extra dereference (note the first '.' instead of '->'):

	return mp.mp_handler->put(mp->rt_pool, obj_table, n);

After doing that, we could ask ourself if the wrappers are still useful or not. I would have say that they could be removed.

The spinlock could be kept, although it may look a bit overkill:
- I don't expect to have several loading at the same time
- There is no unregister() function, so there is no risk to browse the list atomically

Last thing, I think this code should go in rte_mempool.c, not in rte_mempool_default.c.

lib/librte_mempool/rte_mempool_default.c line 100

Quoted 13 lines
lib/librte_mempool/rte_mempool_default.c new file +13 -0
+
+/* TODO Convert to older mechanism of an array of stucts */
+int16_t
+rte_get_mempool_handler(const char *name)
+{
+	int16_t i;
+
+	for (i = 0; i < mempool_handler_list.num_handlers; i++) {
+		if (!strcmp(name, mempool_handler_list.handler[i].name))
+			return i;
+	}
+	return -1;
+}

This would be replaced by a TAILQ_FOREACH().

Quoted 15 lines
lib/librte_mempool/rte_mempool_default.c new file +15 -0
+static void *
+rte_mempool_common_ring_alloc(struct rte_mempool *mp,
+		const char *name, unsigned n, int socket_id, unsigned flags)
+{
+	struct rte_ring *r;
+	char rg_name[RTE_RING_NAMESIZE];
+	int rg_flags = 0;
+
+	if (flags & MEMPOOL_F_SP_PUT)
+		rg_flags |= RING_F_SP_ENQ;
+	if (flags & MEMPOOL_F_SC_GET)
+		rg_flags |= RING_F_SC_DEQ;
+
+	/* allocate the ring that will be used to store objects */
+	/* Ring functions will return appropriate errors if we are

lib/librte_mempool/rte_mempool_default.c line 157

Quoted 10 lines
lib/librte_mempool/rte_mempool_default.c new file +10 -0
+	 * running as a secondary process etc., so no checks made
+	 * in this function for that condition */
+	snprintf(rg_name, sizeof(rg_name), "%s-ring", name);
+	r = rte_ring_create(rg_name, rte_align32pow2(n+1), socket_id, rg_flags);
+	if (r == NULL)
+		return NULL;
+
+	mp->rt_pool = (void *)r;
+
+	return (void *) r;

I don't think the explicit casts are required.

lib/librte_mempool/rte_mempool_internal.h

Quoted 2 lines
lib/librte_mempool/rte_mempool_internal.h new file +0 -0

Is it the proper name?
We could imagine a mempool handler provided by a plugin, and in this case this code should go in rte_mempool.h.

lib/librte_mempool/rte_mempool_internal.h line 43

Quoted 3 lines
lib/librte_mempool/rte_mempool_internal.h new file +3 -0
+
+struct rte_mempool_handler {
+	char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool handler */

I would use a const char * here instead.

lib/librte_mempool/rte_mempool_internal.h line 54

Quoted 11 lines
lib/librte_mempool/rte_mempool_internal.h new file +11 -0
+
+	rte_mempool_alloc_t alloc;
+
+	rte_mempool_put_t put __rte_cache_aligned;
+
+	rte_mempool_get_t get __rte_cache_aligned;
+
+	rte_mempool_get_count get_count __rte_cache_aligned;
+
+	rte_mempool_free_t free __rte_cache_aligned;
+};

I agree with Jerin's comments. I don't think we should cache align each field. Maybe the whole structure.

lib/librte_mempool/rte_mempool_internal.h line 65

Quoted 11 lines
lib/librte_mempool/rte_mempool_internal.h new file +11 -0
+
+struct rte_mempool_handler_list {
+	rte_spinlock_t sl;		  /**< Spinlock for add/delete. */
+
+	int32_t num_handlers;	  /**< Number of handlers that are valid. */
+
+	/* storage for all possible handlers */
+	struct rte_mempool_handler handler[RTE_MEMPOOL_MAX_HANDLER_IDX];
+};
+
+int16_t add_handler(struct rte_mempool_handler *h);

I think it should be called rte_mempool_register_handler().

lib/librte_mempool/rte_mempool_internal.h line 74

Quoted 10 lines
lib/librte_mempool/rte_mempool_internal.h new file +9 -0
+
+#define REGISTER_MEMPOOL_HANDLER(h) \
+static int16_t __attribute__((used)) testfn_##h(void);\
+int16_t __attribute__((constructor, used)) testfn_##h(void)\
+{\
+	return add_handler(&h);\
+}
+
+#endif

Regards,
Olivier

#6 lore raw

Re: [PATCH 1/5] mempool: add external mempool manager support

Quoted 6 lines · Olivier MATZ

Hi David,

Nice work, thanks !
Please see some comments below.

[snip]

Olivier,
Thanks for your comprehensive comments. I'm working on a v2 patch
based on feedback already received from Jerin, and I'll be sure to include your feedback also. Many thanks,
David.

#7 lore raw

Re: [PATCH 1/5] mempool: add external mempool manager support

Hi David,

Quoted 6 lines · David

Olivier,
Thanks for your comprehensive comments. I'm working on a v2 patch
based on feedback already received from Jerin, and I'll be sure to include your feedback also.
Many thanks,
David.

While answering to Keith, I realized there is the same kind of ABI changes in your patchset too. It means it should also follow the ABI deprecation process: dpdk/doc/guides/contributing/versioning.rst.

Regards,
Olivier

#8 lore raw

Re: [PATCH 1/5] mempool: add external mempool manager support

In reply to Olivier MATZ
Quoted 1 line · Olivier MATZ

Hi David,

[snip]

Just a comment on one of your comments:

Quoted 7 lines

Why not using a similar mechanism than what we have for PMDs?

     void rte_eal_driver_register(struct rte_driver *driver)
     {
         TAILQ_INSERT_TAIL(&dev_driver_list, driver, next);
     }
Quoted 7 lines

To do that, you just need to add a TAILQ_ENTRY() in your rte_mempool_handler structure. This would avoid to duplicate the structure into a static array whose size is limited.

Accessing to the callbacks would be easier:

     return mp->mp_handler->put(mp->rt_pool, obj_table, n);

One of the iterations of the code did indeed use this mechanism, however I ran into problems with multiple processes using the same mempool. In that case, the 'mp_handler' element of the mempool in your return statement is only valid for one of the processes. Hence the need for and index that's valid for all processes rather than a pointer that's valid for only one. And it's not easy to quickly index into an element in a queue, hence the array of 16 mempool_handler structs.

[snip]

Rgds,
Dave.

#9 lore raw

Re: [PATCH 1/5] mempool: add external mempool manager support

Hi David,

Quoted 22 lines · David
Quoted 1 line · Olivier MATZ

Hi David,

[snip]

Just a comment on one of your comments:

Quoted 7 lines

Why not using a similar mechanism than what we have for PMDs?

     void rte_eal_driver_register(struct rte_driver *driver)
     {
         TAILQ_INSERT_TAIL(&dev_driver_list, driver, next);
     }
Quoted 7 lines

To do that, you just need to add a TAILQ_ENTRY() in your rte_mempool_handler structure. This would avoid to duplicate the structure into a static array whose size is limited.

Accessing to the callbacks would be easier:

     return mp->mp_handler->put(mp->rt_pool, obj_table, n);
Quoted 7 lines

One of the iterations of the code did indeed use this mechanism, however I ran into problems with multiple processes using the same mempool. In that case, the 'mp_handler' element of the mempool in your return statement is only valid for one of the processes. Hence the need for and index that's valid for all processes rather than a pointer that's valid for only one. And it's not easy to quickly index into an element in a queue, hence the array of 16 mempool_handler structs.

Oh you mean with a secondary processes, I got it now.

Are we sure we can expect that the registered handlers are the same between multiple processes? For instance, if a handler is registered with a plugin, the same plugins must be passed to all processes.

I don't see any better solution than yours (except removing secondary processes of course ;) ).

Thanks for clarifying,
Olivier

#10 lore raw

Re: [PATCH 1/5] mempool: add external mempool manager support

In reply to Olivier MATZ

Olivier,
Here's my comments on your feedback. Hopefully I've covered all of
it this time, and I've summarised the outstanding questions at the bottom.

Quoted 16 lines · Olivier MATZ
Quoted 16 lines
lib/librte_mempool/rte_mempool.c +0 -15
-#ifndef RTE_LIBRTE_XEN_DOM0
-/* stub if DOM0 support not configured */
-struct rte_mempool *
-rte_dom0_mempool_create(const char *name __rte_unused,
-            unsigned n __rte_unused,
-            unsigned elt_size __rte_unused,
-            unsigned cache_size __rte_unused,
-            unsigned private_data_size __rte_unused,
-            rte_mempool_ctor_t *mp_init __rte_unused,
-            void *mp_init_arg __rte_unused,
-            rte_mempool_obj_ctor_t *obj_init __rte_unused,
-            void *obj_init_arg __rte_unused,
-            int socket_id __rte_unused,
-            unsigned flags __rte_unused)
-{
Quoted 8 lines

lib/librte_mempool/rte_mempool.c rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz, · line 397

Quoted 5 lines
lib/librte_mempool/rte_mempool.c +0 -5
-    rte_errno = EINVAL;
-    return NULL;
-}
-#endif
-

Could we move this is a separated commit? "mempool: remove unused rte_dom0_mempool_create stub"

Will do for v3.

--snip--

Quoted 15 lines

return rte_mempool_xmem_create(name, n, elt_size,

lib/librte_mempool/rte_mempool.c rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz, · line 401

Quoted 13 lines
lib/librte_mempool/rte_mempool.c +6 -6
-                           cache_size, private_data_size,
-                           mp_init, mp_init_arg,
-                           obj_init, obj_init_arg,
-                           socket_id, flags,
-                           NULL, NULL, MEMPOOL_PG_NUM_DEFAULT,
-                           MEMPOOL_PG_SHIFT_MAX);
+            cache_size, private_data_size,
+            mp_init, mp_init_arg,
+            obj_init, obj_init_arg,
+            socket_id, flags,
+            NULL, NULL,
+            MEMPOOL_PG_NUM_DEFAULT, MEMPOOL_PG_SHIFT_MAX);
  }
Quoted 4 lines

As far as I can see, you are not modifying the code here, only the style. For better readability, it should go in another commit that only fixes indent or style issues.

I've removed any changes to style in v2. Only makes things more difficult to read.

Quoted 2 lines

Also, I think the proper indentation is to use only one tab for the subsequent lines.

I've done this in v2.

Quoted 6 lines
Quoted 6 lines
lib/librte_mempool/rte_mempool.c +0 -0
@@ -598,6 +568,22 @@ rte_mempool_xmem_create(const char *name, 
 unsigned n, unsigned elt_size,
      mp->cache_flushthresh = CALC_CACHE_FLUSHTHRESH(cache_size);
      mp->private_data_size = private_data_size;
 
Quoted 8 lines

lib/librte_mempool/rte_mempool.c rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size, · line 575

Quoted 5 lines
lib/librte_mempool/rte_mempool.c +5 -0
+    /*
+     * Since we have 4 combinations of the SP/SC/MP/MC, and stack,
+     * examine the
+     * flags to set the correct index into the handler table.
+     */

nit: comment style is not correct

Will fix.

Quoted 5 lines

lib/librte_mempool/rte_mempool.c rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size, · line 577

Quoted 2 lines
lib/librte_mempool/rte_mempool.c +2 -0
+    if (flags & MEMPOOL_F_USE_STACK)
+        mp->handler_idx = rte_get_mempool_handler("stack");

The stack handler does not exist yet, it is introduced in the next commit. I think this code should be moved in the next commit too.

Done in v2

Quoted 4 lines
Quoted 4 lines
(quoted diff) +0 -0
@@ -622,6 +607,10 @@ rte_mempool_xmem_create(const char *name, 
 unsigned n, unsigned elt_size,
 
Quoted 8 lines

lib/librte_mempool/rte_mempool.c rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size, · line 613

Quoted 6 lines
lib/librte_mempool/rte_mempool.c +4 -0
      mp->elt_va_end = mp->elt_va_start;
 
+    /* Parameters are setup. Call the mempool handler alloc */
+    if ((rte_mempool_ext_alloc(mp, name, n, socket_id, flags)) == NULL)
+        goto exit;
+

I think some memory needs to be freed here. At least 'te'.

Done in v2

Quoted 6 lines
Quoted 6 lines
lib/librte_mempool/rte_mempool.c +0 -1
@@ -681,7 +670,9 @@ rte_mempool_dump_cache(FILE *f, const struct 
 rte_mempool *mp)
      fprintf(f, "    cache_size=%"PRIu32"\n", mp->cache_size);
      for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
          cache_count = mp->local_cache[lcore_id].len;
-        fprintf(f, "    cache_count[%u]=%u\n", lcore_id, cache_count);
Quoted 8 lines
Quoted 6 lines
lib/librte_mempool/rte_mempool.c +3 -0
+        if (cache_count > 0)
+            fprintf(f, "    cache_count[%u]=%u\n",
+                        lcore_id, cache_count);
          count += cache_count;
      }
      fprintf(f, "    total_cache_count=%u\n", count);

This could also be moved in a separate commit.

Removed this change, as it's not really relevant to mempool manager

Quoted 4 lines
Quoted 4 lines
lib/librte_mempool/rte_mempool.c +0 -0
@@ -825,7 +815,7 @@ rte_mempool_dump(FILE *f, const struct 
 rte_mempool *mp)
              mp->size);
 
Quoted 9 lines
Quoted 6 lines
lib/librte_mempool/rte_mempool.c +1 -1
      cache_count = rte_mempool_dump_cache(f, mp);
-    common_count = rte_ring_count(mp->ring);
+    common_count = /* rte_ring_count(mp->ring)*/0;
      if ((cache_count + common_count) > mp->size)
          common_count = mp->size - cache_count;
      fprintf(f, "  common_pool_count=%u\n", common_count);

should it be rte_mempool_ext_get_count(mp) instead?

Done.

Quoted 5 lines
Quoted 5 lines
(quoted diff) +0 -0
@@ -919,3 +909,111 @@ void rte_mempool_walk(void (*func)(const struct 
 rte_mempool *, void *),
 
Quoted 15 lines

lib/librte_mempool/rte_mempool.c void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *), · line 922

Quoted 13 lines
lib/librte_mempool/rte_mempool.c +11 -0
      rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
  }
+
+
+/* create the mempool using and external mempool manager */
+struct rte_mempool *
+rte_mempool_create_ext(const char *name, unsigned n, unsigned elt_size,
+            unsigned cache_size, unsigned private_data_size,
+            rte_mempool_ctor_t *mp_init, void *mp_init_arg,
+            rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
+            int socket_id, unsigned flags,
+            const char *handler_name)
+{

I would have used one tab here for subsequent lines.

Done in v2

Quoted 32 lines
Quoted 32 lines
lib/librte_mempool/rte_mempool.c +11 -0
+    char mz_name[RTE_MEMZONE_NAMESIZE];
+    struct rte_mempool_list *mempool_list;
+    struct rte_mempool *mp = NULL;
+    struct rte_tailq_entry *te;
+    const struct rte_memzone *mz;
+    size_t mempool_size;
+    int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
+    int rg_flags = 0;
+    int16_t handler_idx;
+
+    mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, 

rte_mempool_list);

lib/librte_mempool/rte_mempool.c +11 -0
+
+    /* asked cache too big */
+    if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
+        CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
+        rte_errno = EINVAL;
+        return NULL;
+    }
+
+    handler_idx = rte_get_mempool_handler(handler_name);
+    if (handler_idx < 0) {
+        RTE_LOG(ERR, MEMPOOL, "Cannot find mempool handler by 

name!\n");

lib/librte_mempool/rte_mempool.c +7 -0
+        goto exit;
+    }
+
+    /* ring flags */
+    if (flags & MEMPOOL_F_SP_PUT)
+        rg_flags |= RING_F_SP_ENQ;
+    if (flags & MEMPOOL_F_SC_GET)
Quoted 8 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+        rg_flags |= RING_F_SC_DEQ;
+
...

I have the same comment than Jerin here. I think it should be factorized with rte_mempool_xmem_create() if possible. Maybe a at least a function rte_mempool_init() could be introduced, in the same model than rte_ring_init().

factorization done in v2.

Quoted 10 lines
Quoted 10 lines
lib/librte_mempool/rte_mempool.h +0 -0
index 6e2390a..620cfb7 100644
@@ -88,6 +88,8 @@ extern "C" {
  struct rte_mempool_debug_stats {
      uint64_t put_bulk;         /**< Number of puts. */
      uint64_t put_objs;         /**< Number of objects successfully 

put. */

Quoted 9 lines

lib/librte_mempool/rte_mempool.h extern "C" { · line 95

Quoted 5 lines
lib/librte_mempool/rte_mempool.h +2 -0
+    uint64_t put_pool_bulk;    /**< Number of puts into pool. */
+    uint64_t put_pool_objs;    /**< Number of objects into pool. */
      uint64_t get_success_bulk; /**< Successful allocation number. */
      uint64_t get_success_objs; /**< Objects successfully allocated. */
      uint64_t get_fail_bulk;    /**< Failed allocation number. */

I think the comment of put_pool_objs is not very clear. Shouldn't we have the same stats for get?

Not used, removed. Covered by put_bulk.

Quoted 6 lines
Quoted 6 lines
lib/librte_mempool/rte_mempool.h +0 -0
@@ -123,6 +125,7 @@ struct rte_mempool_objsz {
  #define RTE_MEMPOOL_NAMESIZE 32 /**< Maximum length of a memory 

pool. */
#define RTE_MEMPOOL_MZ_PREFIX "MP_"

Quoted 6 lines
Quoted 4 lines

+
/* "MP_<name>" */
#define RTE_MEMPOOL_MZ_FORMAT RTE_MEMPOOL_MZ_PREFIX "%s"

to be removed

Done in v2.

Quoted 5 lines
Quoted 5 lines
lib/librte_mempool/rte_mempool.h +0 -0
@@ -175,12 +178,85 @@ struct rte_mempool_objtlr {
  #endif
  };
 
Quoted 11 lines

lib/librte_mempool/rte_mempool.h struct rte_mempool_objtlr { · line 189

Quoted 9 lines
lib/librte_mempool/rte_mempool.h +9 -0
+/* Handler functions for external mempool support */
+typedef void *(*rte_mempool_alloc_t)(struct rte_mempool *mp,
+        const char *name, unsigned n, int socket_id, unsigned flags);
+typedef int (*rte_mempool_put_t)(void *p,
+        void * const *obj_table, unsigned n);
+typedef int (*rte_mempool_get_t)(void *p, void **obj_table,
+        unsigned n);
+typedef unsigned (*rte_mempool_get_count)(void *p);
+typedef int(*rte_mempool_free_t)(struct rte_mempool *mp);

a space is missing after 'int'.

done in v2

Quoted 14 lines

lib/librte_mempool/rte_mempool.h struct rte_mempool_objtlr { · line 199

Quoted 12 lines
lib/librte_mempool/rte_mempool.h +10 -0
+
+/**
+ * @internal wrapper for external mempool manager alloc callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @param name
+ *   Name of the statistics field to increment in the memory pool.
+ * @param n
+ *   Number to add to the object-oriented statistics.

Are this comments correct?

Fixed in v2

Quoted 11 lines

lib/librte_mempool/rte_mempool.h struct rte_mempool_objtlr { · line 203

Quoted 6 lines
lib/librte_mempool/rte_mempool.h +4 -0
+ * @param socket_id
+ *   socket id on which to allocate.
+ * @param flags
+ *   general flags to allocate function

We could add that we are talking about MEMPOOL_F_* flags.

By the way, the '@return' is missing in all declarations.

Will fix in v3

Quoted 12 lines

lib/librte_mempool/rte_mempool.h struct rte_mempool_objtlr { · line 244

Quoted 9 lines
lib/librte_mempool/rte_mempool.h +8 -0
+/**
+ * @internal wrapper for external mempool manager get_count callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ */
+int
+rte_mempool_ext_get_count(const struct rte_mempool *mp);

should it be unsigned instead of int?

Yes. Will change.

Quoted 23 lines
Quoted 23 lines
lib/librte_mempool/rte_mempool.h +10 -1
+
+/**
+ * @internal wrapper for external mempool manager free callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ */
+int
+rte_mempool_ext_free(struct rte_mempool *mp);
+
  /**
   * The RTE mempool structure.
   */
  struct rte_mempool {
      char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
-    struct rte_ring *ring;           /**< Ring to store objects. */
      phys_addr_t phys_addr;           /**< Phys. addr. of mempool 

struct. */
int flags; /**< Flags of the mempool. */
uint32_t size; /**< Size of the mempool. */

lib/librte_mempool/rte_mempool.h +0 -0
@@ -194,6 +270,11 @@ struct rte_mempool {
 
Quoted 6 lines

lib/librte_mempool/rte_mempool.h struct rte_mempool { · line 274

Quoted 4 lines
lib/librte_mempool/rte_mempool.h +2 -0
      unsigned private_data_size;      /**< Size of private data. */
 
+    /* Common pool data structure pointer */
+    void *rt_pool __rte_cache_aligned;

What is the meaning of rt_pool?

I agree that it's probably not a very good name. Since it's basically the pointer which is used by the handlers callbacks, maybe we should call it mempool_storage? That leaves it generic enough that it can point at a ring, an array, or whatever else is needed for a particular handler.

Quoted 8 lines

lib/librte_mempool/rte_mempool.h struct rte_mempool { · line 277

Quoted 3 lines
lib/librte_mempool/rte_mempool.h +3 -0
+
+    int16_t handler_idx;
+

I don't think I'm getting why an index is better than a pointer to the struct rte_mempool_handler. It would simplify the add_handler() function. See below for a detailed explaination.

As discussed in previous mails. It's to facilitate secondary processes.

Quoted 4 lines
Quoted 4 lines
lib/librte_mempool/rte_mempool.h +0 -0
@@ -223,6 +304,10 @@ struct rte_mempool {
  #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on 

cache lines.*/
#define MEMPOOL_F_SP_PUT 0x0004 /**< Default put is

Quoted 8 lines
Quoted 5 lines

"single-producer".*/
#define MEMPOOL_F_SC_GET 0x0008 /**< Default get is
"single-consumer".*/
+#define MEMPOOL_F_USE_STACK 0x0010 /**< Use a stack for the
common pool. */

Stack is not implemented in this commit. It should be moved in next commit.

Done in v2

Quoted 5 lines

lib/librte_mempool/rte_mempool.h struct rte_mempool { · line 310

Quoted 3 lines
lib/librte_mempool/rte_mempool.h +3 -0
+#define MEMPOOL_F_USE_TM         0x0020
+#define MEMPOOL_F_NO_SECONDARY   0x0040
+

What are these flags?

Not needed. Part of temporary change. Removed.

Quoted 11 lines
Quoted 9 lines
lib/librte_mempool/rte_mempool.h +0 -1
@@ -728,7 +813,6 @@ rte_dom0_mempool_create(const char *name, 
 unsigned n, unsigned elt_size,
          rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
          int socket_id, unsigned flags);
 
-
  /**
   * Dump the status of the mempool to the console.
   *

style

will fix in v3.

Quoted 11 lines

lib/librte_mempool/rte_mempool.h void rte_mempool_dump(FILE *f, const struct · line 841

Quoted 9 lines
lib/librte_mempool/rte_mempool.h +1 -1
@@ -753,7 +837,7 @@ void rte_mempool_dump(FILE *f, const struct 
 rte_mempool *mp);
   */
  static inline void __attribute__((always_inline))
  __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
-            unsigned n, int is_mp)
+            unsigned n, __attribute__((unused)) int is_mp)

You could use __rte_unused instead of __attribute__((unused))

will change in v3

Quoted 11 lines
Quoted 11 lines
lib/librte_mempool/rte_mempool.h +1 -2
@@ -769,8 +853,7 @@ __mempool_put_bulk(struct rte_mempool *mp, void * 
 const *obj_table,
 
  #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
      /* cache is not enabled or single producer or non-EAL thread */
-    if (unlikely(cache_size == 0 || is_mp == 0 ||
-             lcore_id >= RTE_MAX_LCORE))
+    if (unlikely(cache_size == 0 || lcore_id >= RTE_MAX_LCORE))
          goto ring_enqueue;
 
Quoted 8 lines
Quoted 2 lines
      /* Go straight to ring if put would overflow mem allocated for 

cache */

If I understand well, we now always use the cache, even if the mempool is single-producer. I was wondering if it would have a performance impact... I suppose that using the cache is more efficient than the ring in single-producer mode, so it may increase performance. Do you have an idea of the impact here?

I've seen very little in performance gain, maybe a couple of percent for some tests, and up to 10% drop for some single core tests. I'll do some more specific testing based on SP versus MP.

Quoted 5 lines

I think we could remove the parameter as the function is marked as internal. The comment above should also be fixed. The same comments apply to the get() functions.

will fix comments in v3, and see if we should remove is_mp based on more performance testing.

Quoted 6 lines
Quoted 6 lines
lib/librte_mempool/rte_mempool.h +0 -0
@@ -793,8 +876,8 @@ __mempool_put_bulk(struct rte_mempool *mp, void * 
 const *obj_table,
 
      cache->len += n;
 
Quoted 9 lines

lib/librte_mempool/rte_mempool.h __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table, · line 881

Quoted 5 lines
lib/librte_mempool/rte_mempool.h +2 -2
-    if (cache->len >= flushthresh) {
-        rte_ring_mp_enqueue_bulk(mp->ring, &cache->objs[cache_size],
+    if (unlikely(cache->len >= flushthresh)) {
+        rte_mempool_ext_put_bulk(mp, &cache->objs[cache_size],
                  cache->len - cache_size);

Shouldn't we add a __MEMPOOL_STAT_ADD(mp, put_pool,
cache->len - cache_size) here ?

Correct. Added in v3.

Quoted 11 lines

lib/librte_mempool/rte_mempool.h __mempool_get_bulk(struct rte_mempool *mp, void · line 1030

Quoted 9 lines
lib/librte_mempool/rte_mempool.h +2 -2
@@ -954,8 +1025,8 @@ __mempool_get_bulk(struct rte_mempool *mp, void 
 **obj_table,
      uint32_t cache_size = mp->cache_size;
 
      /* cache is not enabled or single consumer */
-    if (unlikely(cache_size == 0 || is_mc == 0 ||
-             n >= cache_size || lcore_id >= RTE_MAX_LCORE))
+    if (unlikely(cache_size == 0 || n >= cache_size ||
+                        lcore_id >= RTE_MAX_LCORE))

incorrect indent

will fix in v3

Quoted 5 lines
Quoted 5 lines
lib/librte_mempool/rte_mempool.h +0 -0
@@ -967,7 +1038,8 @@ __mempool_get_bulk(struct rte_mempool *mp, void 
 **obj_table,
          uint32_t req = n + (cache_size - cache->len);
 
Quoted 8 lines

lib/librte_mempool/rte_mempool.h __mempool_get_bulk(struct rte_mempool *mp, void **obj_table, · line 1042

Quoted 6 lines
lib/librte_mempool/rte_mempool.h +0 -0
          /* How many do we require i.e. number to fill the cache + 

the request */
- ret = rte_ring_mc_dequeue_bulk(mp->ring,
&cache->objs[cache->len], req);

lib/librte_mempool/rte_mempool.h +2 -0
+        ret = rte_mempool_ext_get_bulk(mp,
+                        &cache->objs[cache->len], req);

indent

will fix in v3

Quoted 15 lines

lib/librte_mempool/rte_mempool.h ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz, · line 1483

Quoted 12 lines

+/**
+ * Function to get an index to an external mempool manager
+ *
+ * @param name
+ * The name of the mempool handler to search for in the list of
handlers
+ * @return
+ * The index of the mempool handler in the list of registered mempool
+ * handlers
+ */

lib/librte_mempool/rte_mempool.h +2 -0
+int16_t
+rte_get_mempool_handler(const char *name);

I would prefer a function like this:

Quoted 4 lines

const struct rte_mempool_handler *
rte_get_mempool_handler(const char *name);

(detailed explaination below)

Already discussed previously, index needed over pointer because of secondary processes.

Quoted 83 lines
Quoted 46 lines
lib/librte_mempool/rte_mempool_default.c new file +21 -0
new file mode 100644
index 0000000..2493dc1
+#include "rte_mempool_internal.h"
+
+/*
+ * Indirect jump table to support external memory pools
+ */
+struct rte_mempool_handler_list mempool_handler_list = {
+    .sl =  RTE_SPINLOCK_INITIALIZER ,
+    .num_handlers = 0
+};
+
+/* TODO Convert to older mechanism of an array of stucts */
+int16_t
+add_handler(struct rte_mempool_handler *h)
+{
+    int16_t handler_idx;
+
+    /*  */
+    rte_spinlock_lock(&mempool_handler_list.sl);
+
+    /* Check whether jump table has space */
+    if (mempool_handler_list.num_handlers >= 

RTE_MEMPOOL_MAX_HANDLER_IDX) {

lib/librte_mempool/rte_mempool_default.c new file +11 -0
+        rte_spinlock_unlock(&mempool_handler_list.sl);
+        RTE_LOG(ERR, MEMPOOL,
+                "Maximum number of mempool handlers exceeded\n");
+        return -1;
+    }
+
+    if ((h->put == NULL) || (h->get == NULL) ||
+        (h->get_count == NULL)) {
+        rte_spinlock_unlock(&mempool_handler_list.sl);
+         RTE_LOG(ERR, MEMPOOL,
+                    "Missing callback while registering mempool 

handler\n");

lib/librte_mempool/rte_mempool_default.c new file +7 -0
+        return -1;
+    }
+
+    /* add new handler index */
+    handler_idx = mempool_handler_list.num_handlers++;
+
+    snprintf(mempool_handler_list.handler[handler_idx].name,

lib/librte_mempool/rte_mempool_default.c line 87

Quoted 10 lines
lib/librte_mempool/rte_mempool_default.c new file +10 -0
+                RTE_MEMPOOL_NAMESIZE, "%s", h->name);
+    mempool_handler_list.handler[handler_idx].alloc = h->alloc;
+    mempool_handler_list.handler[handler_idx].put = h->put;
+    mempool_handler_list.handler[handler_idx].get = h->get;
+    mempool_handler_list.handler[handler_idx].get_count = h->get_count;
+
+    rte_spinlock_unlock(&mempool_handler_list.sl);
+
+    return handler_idx;
+}

Why not using a similar mechanism than what we have for PMDs?

    void rte_eal_driver_register(struct rte_driver *driver)
    {
        TAILQ_INSERT_TAIL(&dev_driver_list, driver, next);
    }

To do that, you just need to add a TAILQ_ENTRY() in your rte_mempool_handler structure. This would avoid to duplicate the structure into a static array whose size is limited.

Accessing to the callbacks would be easier:

    return mp->mp_handler->put(mp->rt_pool, obj_table, n);

instead of:

    return (mempool_handler_list.handler[mp->handler_idx].put)
                    (mp->rt_pool, obj_table, n);

If we really want to copy the handlers somewhere, it could be in the mempool structure. It would avoid an extra dereference (note the first '.' instead of '->'):

    return mp.mp_handler->put(mp->rt_pool, obj_table, n);
Quoted 9 lines

After doing that, we could ask ourself if the wrappers are still useful or not. I would have say that they could be removed.

The spinlock could be kept, although it may look a bit overkill:
- I don't expect to have several loading at the same time
- There is no unregister() function, so there is no risk to browse the list atomically

Already discussed previously, index needed over pointer because of secondary processes.

Quoted 2 lines

Last thing, I think this code should go in rte_mempool.c, not in rte_mempool_default.c.

I was trying to keep the default handlers together in their own file, rather than having them in with the mempool framework. I think it's better having them separate, and new handlers can go in their own files also. no?

Quoted 7 lines
Quoted 7 lines
lib/librte_mempool/rte_mempool_default.c new file +7 -0
+
+/* TODO Convert to older mechanism of an array of stucts */
+int16_t
+rte_get_mempool_handler(const char *name)
+{
+    int16_t i;
+
Quoted 8 lines

lib/librte_mempool/rte_mempool_default.c line 100

Quoted 6 lines
lib/librte_mempool/rte_mempool_default.c new file +6 -0
+    for (i = 0; i < mempool_handler_list.num_handlers; i++) {
+        if (!strcmp(name, mempool_handler_list.handler[i].name))
+            return i;
+    }
+    return -1;
+}

This would be replaced by a TAILQ_FOREACH().

Already discussed previously, index needed over pointer because of secondary processes.

Quoted 21 lines
Quoted 21 lines
lib/librte_mempool/rte_mempool_default.c new file +19 -0
+static void *
+rte_mempool_common_ring_alloc(struct rte_mempool *mp,
+        const char *name, unsigned n, int socket_id, unsigned flags)
+{
+    struct rte_ring *r;
+    char rg_name[RTE_RING_NAMESIZE];
+    int rg_flags = 0;
+
+    if (flags & MEMPOOL_F_SP_PUT)
+        rg_flags |= RING_F_SP_ENQ;
+    if (flags & MEMPOOL_F_SC_GET)
+        rg_flags |= RING_F_SC_DEQ;
+
+    /* allocate the ring that will be used to store objects */
+    /* Ring functions will return appropriate errors if we are
+     * running as a secondary process etc., so no checks made
+     * in this function for that condition */
+    snprintf(rg_name, sizeof(rg_name), "%s-ring", name);
+    r = rte_ring_create(rg_name, rte_align32pow2(n+1), socket_id, 

rg_flags);

Quoted 8 lines

lib/librte_mempool/rte_mempool_default.c line 157

Quoted 6 lines
lib/librte_mempool/rte_mempool_default.c new file +6 -0
+    if (r == NULL)
+        return NULL;
+
+    mp->rt_pool = (void *)r;
+
+    return (void *) r;

I don't think the explicit casts are required.

will change in v3

Quoted 7 lines

lib/librte_mempool/rte_mempool_internal.h

Quoted 3 lines
lib/librte_mempool/rte_mempool_internal.h new file +0 -0

Is it the proper name?
We could imagine a mempool handler provided by a plugin, and in this case this code should go in rte_mempool.h.

I was trying to keep the public APIs in rte_mempool.h, and aal the private stuff in rte_mempool_internal.h. Maybe a better name would be rte_mempool_private.h?

Quoted 6 lines

lib/librte_mempool/rte_mempool_internal.h line 43

Quoted 3 lines
lib/librte_mempool/rte_mempool_internal.h new file +3 -0
+
+struct rte_mempool_handler {
+    char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool handler */

I would use a const char * here instead.

Would we then have to allocate the memory for the string elsewhere? I would have thought this is the more straightforward method.

Quoted 6 lines
Quoted 6 lines
lib/librte_mempool/rte_mempool_internal.h new file +6 -0
+
+    rte_mempool_alloc_t alloc;
+
+    rte_mempool_put_t put __rte_cache_aligned;
+
+    rte_mempool_get_t get __rte_cache_aligned;
Quoted 8 lines

lib/librte_mempool/rte_mempool_internal.h line 54

Quoted 5 lines
lib/librte_mempool/rte_mempool_internal.h new file +5 -0
+
+    rte_mempool_get_count get_count __rte_cache_aligned;
+
+    rte_mempool_free_t free __rte_cache_aligned;
+};

I agree with Jerin's comments. I don't think we should cache align each field. Maybe the whole structure.

Changed in v2.

Quoted 6 lines
Quoted 6 lines
lib/librte_mempool/rte_mempool_internal.h new file +5 -0
+
+struct rte_mempool_handler_list {
+    rte_spinlock_t sl;          /**< Spinlock for add/delete. */
+
+    int32_t num_handlers;      /**< Number of handlers that are 

valid. */

Quoted 8 lines

lib/librte_mempool/rte_mempool_internal.h line 65

Quoted 6 lines
lib/librte_mempool/rte_mempool_internal.h new file +6 -0
+
+    /* storage for all possible handlers */
+    struct rte_mempool_handler handler[RTE_MEMPOOL_MAX_HANDLER_IDX];
+};
+
+int16_t add_handler(struct rte_mempool_handler *h);

I think it should be called rte_mempool_register_handler().

Agreed, changed in v2.

Quoted 7 lines
Quoted 7 lines
lib/librte_mempool/rte_mempool_internal.h new file +7 -0
+
+#define REGISTER_MEMPOOL_HANDLER(h) \
+static int16_t __attribute__((used)) testfn_##h(void);\
+int16_t __attribute__((constructor, used)) testfn_##h(void)\
+{\
+    return add_handler(&h);\
+}
Quoted 8 lines

lib/librte_mempool/rte_mempool_internal.h line 74

Quoted 3 lines
lib/librte_mempool/rte_mempool_internal.h new file +2 -0
+
+#endif

Regards,
Olivier

Apologies for not addressing all of your comments for v2. I'll await your comments on the couple of outstanding questions above, then push up v3. Mainly:
* change "rt_pool" to "mempool_storage"?
* change to const char * for mempool name, or leave as is.
* move all contents of rte_mempool_internal.h to rte_mempool.h, or leave as is.
* alternatively change name of rte_mempool_internal.h to rte_mempool_private.h
* I need to look into the performace of always using cache for single producer/consumer.

Thanks,
David.

#11 lore raw

Re: [PATCH 1/5] mempool: add external mempool manager support

Hi David,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
lib/librte_mempool/rte_mempool.c +0 -0
@@ -622,6 +607,10 @@ rte_mempool_xmem_create(const char *name,
 unsigned n, unsigned elt_size,
 
      mp->elt_va_end = mp->elt_va_start;
 
Quoted 8 lines
Quoted 6 lines

lib/librte_mempool/rte_mempool.c rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size, · line 613

Quoted 4 lines
lib/librte_mempool/rte_mempool.c +4 -0
+    /* Parameters are setup. Call the mempool handler alloc */
+    if ((rte_mempool_ext_alloc(mp, name, n, socket_id, flags)) == NULL)
+        goto exit;
+

I think some memory needs to be freed here. At least 'te'.

Done in v2

Please note that in the meanwhile, this fix has been pushed (as we need it for next release):
http://dpdk.org/browse/dpdk/commit/lib/librte_mempool/rte_mempool.c?id=86f36ff9578b5f3d697c8fcf6072dcb70e2b246f

Quoted 20 lines
Quoted 10 lines
Quoted 10 lines
lib/librte_mempool/rte_mempool.h +0 -0
index 6e2390a..620cfb7 100644
@@ -88,6 +88,8 @@ extern "C" {
  struct rte_mempool_debug_stats {
      uint64_t put_bulk;         /**< Number of puts. */
      uint64_t put_objs;         /**< Number of objects successfully

put. */

Quoted 9 lines

lib/librte_mempool/rte_mempool.h extern "C" { · line 95

Quoted 5 lines
lib/librte_mempool/rte_mempool.h +2 -0
+    uint64_t put_pool_bulk;    /**< Number of puts into pool. */
+    uint64_t put_pool_objs;    /**< Number of objects into pool. */
      uint64_t get_success_bulk; /**< Successful allocation number. */
      uint64_t get_success_objs; /**< Objects successfully allocated. */
      uint64_t get_fail_bulk;    /**< Failed allocation number. */

I think the comment of put_pool_objs is not very clear. Shouldn't we have the same stats for get?

Quoted 1 line

Not used, removed. Covered by put_bulk.

I guess you mean it will be removed in v3? It is still there in the v2 (the field, not the comment that has been fixed).

Shouldn't we have the same stats for get?

Quoted 19 lines
Quoted 12 lines
Quoted 12 lines

/**
* The RTE mempool structure.
*/
struct rte_mempool {
char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
- struct rte_ring *ring; /**< Ring to store objects. */
phys_addr_t phys_addr; /**< Phys. addr. of mempool
struct. */
int flags; /**< Flags of the mempool. */
uint32_t size; /**< Size of the mempool. */

lib/librte_mempool/rte_mempool.h +0 -0
@@ -194,6 +270,11 @@ struct rte_mempool {
 
Quoted 6 lines

lib/librte_mempool/rte_mempool.h struct rte_mempool { · line 274

Quoted 4 lines
lib/librte_mempool/rte_mempool.h +2 -0
      unsigned private_data_size;      /**< Size of private data. */
 
+    /* Common pool data structure pointer */
+    void *rt_pool __rte_cache_aligned;

What is the meaning of rt_pool?

Quoted 4 lines

I agree that it's probably not a very good name. Since it's basically the pointer which is used by the handlers callbacks, maybe we should call it mempool_storage? That leaves it generic enough that it can point at a ring, an array, or whatever else is needed for a particular handler.

My question was more about the "rt_" prefix. Maybe I missed something obvious? I think "pool" or "pool_handler" is ok.

Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
lib/librte_mempool/rte_mempool.h +1 -2
@@ -769,8 +853,7 @@ __mempool_put_bulk(struct rte_mempool *mp, void *
 const *obj_table,
 
  #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
      /* cache is not enabled or single producer or non-EAL thread */
-    if (unlikely(cache_size == 0 || is_mp == 0 ||
-             lcore_id >= RTE_MAX_LCORE))
+    if (unlikely(cache_size == 0 || lcore_id >= RTE_MAX_LCORE))
          goto ring_enqueue;
 
Quoted 8 lines
Quoted 2 lines
      /* Go straight to ring if put would overflow mem allocated for

cache */

If I understand well, we now always use the cache, even if the mempool is single-producer. I was wondering if it would have a performance impact... I suppose that using the cache is more efficient than the ring in single-producer mode, so it may increase performance. Do you have an idea of the impact here?

Quoted 3 lines

I've seen very little in performance gain, maybe a couple of percent for some tests, and up to 10% drop for some single core tests. I'll do some more specific testing based on SP versus MP.

OK thanks!

Quoted 88 lines
Quoted 88 lines
Quoted 47 lines
lib/librte_mempool/rte_mempool_default.c new file +21 -0
new file mode 100644
index 0000000..2493dc1
+#include "rte_mempool_internal.h"
+
+/*
+ * Indirect jump table to support external memory pools
+ */
+struct rte_mempool_handler_list mempool_handler_list = {
+    .sl =  RTE_SPINLOCK_INITIALIZER ,
+    .num_handlers = 0
+};
+
+/* TODO Convert to older mechanism of an array of stucts */
+int16_t
+add_handler(struct rte_mempool_handler *h)
+{
+    int16_t handler_idx;
+
+    /*  */
+    rte_spinlock_lock(&mempool_handler_list.sl);
+
+    /* Check whether jump table has space */
+    if (mempool_handler_list.num_handlers >=

RTE_MEMPOOL_MAX_HANDLER_IDX) {

lib/librte_mempool/rte_mempool_default.c new file +11 -0
+        rte_spinlock_unlock(&mempool_handler_list.sl);
+        RTE_LOG(ERR, MEMPOOL,
+                "Maximum number of mempool handlers exceeded\n");
+        return -1;
+    }
+
+    if ((h->put == NULL) || (h->get == NULL) ||
+        (h->get_count == NULL)) {
+        rte_spinlock_unlock(&mempool_handler_list.sl);
+         RTE_LOG(ERR, MEMPOOL,
+                    "Missing callback while registering mempool

handler\n");

lib/librte_mempool/rte_mempool_default.c new file +7 -0
+        return -1;
+    }
+
+    /* add new handler index */
+    handler_idx = mempool_handler_list.num_handlers++;
+
+    snprintf(mempool_handler_list.handler[handler_idx].name,

lib/librte_mempool/rte_mempool_default.c line 87

Quoted 10 lines
lib/librte_mempool/rte_mempool_default.c new file +10 -0
+                RTE_MEMPOOL_NAMESIZE, "%s", h->name);
+    mempool_handler_list.handler[handler_idx].alloc = h->alloc;
+    mempool_handler_list.handler[handler_idx].put = h->put;
+    mempool_handler_list.handler[handler_idx].get = h->get;
+    mempool_handler_list.handler[handler_idx].get_count = h->get_count;
+
+    rte_spinlock_unlock(&mempool_handler_list.sl);
+
+    return handler_idx;
+}

Why not using a similar mechanism than what we have for PMDs?

    void rte_eal_driver_register(struct rte_driver *driver)
    {
        TAILQ_INSERT_TAIL(&dev_driver_list, driver, next);
    }

To do that, you just need to add a TAILQ_ENTRY() in your rte_mempool_handler structure. This would avoid to duplicate the structure into a static array whose size is limited.

Accessing to the callbacks would be easier:

    return mp->mp_handler->put(mp->rt_pool, obj_table, n);

instead of:

    return (mempool_handler_list.handler[mp->handler_idx].put)
                    (mp->rt_pool, obj_table, n);

If we really want to copy the handlers somewhere, it could be in the mempool structure. It would avoid an extra dereference (note the first '.' instead of '->'):

    return mp.mp_handler->put(mp->rt_pool, obj_table, n);

After doing that, we could ask ourself if the wrappers are still useful or not. I would have say that they could be removed.

Quoted 8 lines
Quoted 5 lines

The spinlock could be kept, although it may look a bit overkill:
- I don't expect to have several loading at the same time
- There is no unregister() function, so there is no risk to browse the list atomically

Already discussed previously, index needed over pointer because of secondary processes.

Could you add a comment stating this? It may help for next readers to have this info.

Quoted 7 lines
Quoted 2 lines

Last thing, I think this code should go in rte_mempool.c, not in rte_mempool_default.c.

I was trying to keep the default handlers together in their own file, rather than having them in with the mempool framework. I think it's better having them separate, and new handlers can go in their own files also. no?

OK for the following functions:
common_ring_mp_put()
common_ring_sp_put()
common_ring_mc_get()
common_ring_sc_get()
common_ring_get_count() rte_mempool_common_ring_alloc() (note: only this one has
a rte_mempool prefix, maybe it should be fixed)

The other functions are part of the framework to add an external handler, I don't think they should go in rte_mempool_default.c They could either go in rte_mempool.c or in another file rte_mempool_handler.c.

Quoted 10 lines
Quoted 6 lines

lib/librte_mempool/rte_mempool_internal.h

Quoted 2 lines
lib/librte_mempool/rte_mempool_internal.h new file +0 -0

Is it the proper name?
We could imagine a mempool handler provided by a plugin, and in this case this code should go in rte_mempool.h.

I was trying to keep the public APIs in rte_mempool.h, and aal the private stuff in rte_mempool_internal.h. Maybe a better name would be rte_mempool_private.h?

Are these functions internal? I mean, is it possible for an application or an external PMD (.so) to provide its own handler? I think it would be really interesting to have this capability.

Then, I would prefer to have this either in rte_mempool.h or in rte_mempool_handler.h (that would be coherent with the .c)

Quoted 9 lines
Quoted 6 lines

lib/librte_mempool/rte_mempool_internal.h line 43

Quoted 3 lines
lib/librte_mempool/rte_mempool_internal.h new file +3 -0
+
+struct rte_mempool_handler {
+    char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool handler */

I would use a const char * here instead.

Would we then have to allocate the memory for the string elsewhere? I would have thought this is the more straightforward method.

My initial question was, can we just have something like this:

	// my_handler.c
	static struct rte_mempool_handler handler_stack = {
		.name = "my_handler",
		.alloc = my_alloc,
		...
	};

	// rte_mempool_handler.c
	int16_t
	rte_mempool_register_handler(struct rte_mempool_handler *h)
	{
		...
		handler->name = h->name; /* instead of snprintf */
		...
	}

But it won't be possible as the structures will be shared with a secondary process. So the name[RTE_MEMPOOL_NAMESIZE] is fine.

Regards,
Olivier

#12 lore raw

Re: [PATCH 1/5] mempool: add external mempool manager support

Hi Olivier,

Quoted 12 lines · Olivier MATZ

Hi David,

Quoted 10 lines
Quoted 10 lines

lib/librte_mempool/rte_mempool.c rte_mempool_xmem_create(const char *name, · line 614

Quoted 9 lines
lib/librte_mempool/rte_mempool.c +4 -0
@@ -622,6 +607,10 @@ rte_mempool_xmem_create(const char *name,
 unsigned n, unsigned elt_size,
 
       mp->elt_va_end = mp->elt_va_start;
 
+    /* Parameters are setup. Call the mempool handler alloc */
+    if ((rte_mempool_ext_alloc(mp, name, n, socket_id, flags)) == NULL)
+        goto exit;
+

I think some memory needs to be freed here. At least 'te'.

Quoted 4 lines
Quoted 1 line

Done in v2

Please note that in the meanwhile, this fix has been pushed (as we need it for next release):
http://dpdk.org/browse/dpdk/commit/lib/librte_mempool/rte_mempool.c?id=86f36ff9578b5f3d697c8fcf6072dcb70e2b246f

v3 will be rebased on top of the latest head of the repo.

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 10 lines
lib/librte_mempool/rte_mempool.h +0 -0
index 6e2390a..620cfb7 100644
@@ -88,6 +88,8 @@ extern "C" {
   struct rte_mempool_debug_stats {
       uint64_t put_bulk;         /**< Number of puts. */
       uint64_t put_objs;         /**< Number of objects successfully

put. */

lib/librte_mempool/rte_mempool.h extern "C" { · line 95

Quoted 5 lines
lib/librte_mempool/rte_mempool.h +2 -0
+    uint64_t put_pool_bulk;    /**< Number of puts into pool. */
+    uint64_t put_pool_objs;    /**< Number of objects into pool. */
       uint64_t get_success_bulk; /**< Successful allocation number. */
       uint64_t get_success_objs; /**< Objects successfully allocated. */
       uint64_t get_fail_bulk;    /**< Failed allocation number. */

I think the comment of put_pool_objs is not very clear. Shouldn't we have the same stats for get?

Quoted 5 lines
Quoted 1 line

Not used, removed. Covered by put_bulk.

I guess you mean it will be removed in v3? It is still there in the v2 (the field, not the comment that has been fixed).

Shouldn't we have the same stats for get?

I believe get's are covered by the get_success_bulk and get_fail_bulk

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 11 lines
   /**
    * The RTE mempool structure.
    */
   struct rte_mempool {
       char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */

- struct rte_ring *ring; /**< Ring to store objects. */
phys_addr_t phys_addr; /**< Phys. addr. of mempool
struct. */
int flags; /**< Flags of the mempool. */
uint32_t size; /**< Size of the mempool. */

lib/librte_mempool/rte_mempool.h struct rte_mempool { · line 274

Quoted 5 lines
lib/librte_mempool/rte_mempool.h +2 -0
@@ -194,6 +270,11 @@ struct rte_mempool {
 
       unsigned private_data_size;      /**< Size of private data. */
 
+    /* Common pool data structure pointer */
+    void *rt_pool __rte_cache_aligned;

What is the meaning of rt_pool?

Quoted 6 lines
Quoted 4 lines

I agree that it's probably not a very good name. Since it's basically the pointer which is used by the handlers callbacks, maybe we should call it mempool_storage? That leaves it generic enough that it can point at a ring, an array, or whatever else is needed for a particular handler.

My question was more about the "rt_" prefix. Maybe I missed something obvious? I think "pool" or "pool_handler" is ok.

v3 will use "pool"

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 6 lines
lib/librte_mempool/rte_mempool.h +5 -1
-
+
+    /* add new handler index */
+    handler_idx = mempool_handler_list.num_handlers++;
+
+    snprintf(mempool_handler_list.handler[handler_idx].name,

lib/librte_mempool/rte_mempool.h struct rte_mempool {

Quoted 10 lines
lib/librte_mempool/rte_mempool.h +10 -0
+                RTE_MEMPOOL_NAMESIZE, "%s", h->name);
+    mempool_handler_list.handler[handler_idx].alloc = h->alloc;
+    mempool_handler_list.handler[handler_idx].put = h->put;
+    mempool_handler_list.handler[handler_idx].get = h->get;
+    mempool_handler_list.handler[handler_idx].get_count = h->get_count;
+
+    rte_spinlock_unlock(&mempool_handler_list.sl);
+
+    return handler_idx;
+}

Why not using a similar mechanism than what we have for PMDs?

     void rte_eal_driver_register(struct rte_driver *driver)
     {
         TAILQ_INSERT_TAIL(&dev_driver_list, driver, next);
     }
Quoted 4 lines
Quoted 2 lines

Already discussed previously, index needed over pointer because of secondary processes.

Could you add a comment stating this? It may help for next readers to have this info.

v3: Comment added to the header file where we define header_idx explaining the use of an index versus pointers

Quoted 15 lines
Quoted 6 lines
Quoted 2 lines

Last thing, I think this code should go in rte_mempool.c, not in rte_mempool_default.c.

I was trying to keep the default handlers together in their own file, rather than having them in with the mempool framework. I think it's better having them separate, and new handlers can go in their own files also. no?

OK for the following functions:
common_ring_mp_put()
common_ring_sp_put()
common_ring_mc_get()
common_ring_sc_get()
common_ring_get_count() rte_mempool_common_ring_alloc() (note: only this one has
a rte_mempool prefix, maybe it should be fixed)

Quoted 4 lines

The other functions are part of the framework to add an external handler, I don't think they should go in rte_mempool_default.c They could either go in rte_mempool.c or in another file rte_mempool_handler.c.

v3: Agreed. The bulk of the v3 is simplification of the files.
All of the "common" callbacks are now in in rte_mempool_handler.c and rte_mempool_handler.h.
I've renamed the 'alloc' function above to be in line with the naming of the others.
The 'custom' handler has been banished to the autotest code, so as to keep the library as clean as possible.
What's interesting is that the autotest can have all the code defining the custom mempool (including it's registration), keeping the library free of user code.

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

lib/librte_mempool/rte_mempool_internal.h

Quoted 2 lines
lib/librte_mempool/rte_mempool_internal.h new file +0 -0

Is it the proper name?
We could imagine a mempool handler provided by a plugin, and in this case this code should go in rte_mempool.h.

I was trying to keep the public APIs in rte_mempool.h, and aal the private stuff in rte_mempool_internal.h. Maybe a better name would be rte_mempool_private.h?

Quoted 7 lines

Are these functions internal? I mean, is it possible for an application or an external PMD (.so) to provide its own handler? I think it would be really interesting to have this capability.

Then, I would prefer to have this either in rte_mempool.h or in rte_mempool_handler.h (that would be coherent with the .c)

Now rte_mempool_handler.h

So to synopsise:

rte_mempool.[ch] - mempool create, populate, audit, dump, etc. rte_mempool_handler.[ch] - handler registration, and fns to call callbacks rte_mempool_default.c - default internal handlers sp/sc, mp/mc, etc.

custom handler has been moved out to app/test/test_ext_mempool.c

Thanks,
Dave.

author
#13 lore raw

PATCH 2/5 memool: add stack (lifo) based external mempool handler

In reply to David Hunt

adds a simple stack based mempool handler

---
 app/test/test_mempool_perf.c           |   1 -
 lib/librte_mempool/Makefile            |   1 +
 lib/librte_mempool/rte_mempool_stack.c | 167 +++++++++++++++++++++++++++++++++
 3 files changed, 168 insertions(+), 1 deletion(-)
 create mode 100644 lib/librte_mempool/rte_mempool_stack.c
app/test/test_mempool_perf.c +0 -1
index 091c1df..c5a1d2a 100644
@@ -52,7 +52,6 @@
 #include <rte_lcore.h>
 #include <rte_atomic.h>
 #include <rte_branch_prediction.h>
-#include <rte_ring.h>
 #include <rte_mempool.h>
 #include <rte_spinlock.h>
 #include <rte_malloc.h>
lib/librte_mempool/Makefile +1 -0
index 7c81ef6..d795b48 100644
@@ -43,6 +43,7 @@ LIBABIVER := 1
 # all source are stored in SRCS-y
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool.c
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_default.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_stack.c
 ifeq ($(CONFIG_RTE_LIBRTE_XEN_DOM0),y)
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_dom0_mempool.c
 endif
lib/librte_mempool/rte_mempool_stack.c new file +167 -0

167 lines not on this page · raw message

Signature · 1 line
1.9.3
#14 lore raw

Re: [PATCH 2/5] memool: add stack (lifo) based external mempool handler

Hi,

Quoted 1 line

[PATCH 2/5] memool: add stack (lifo) based external mempool handler

typo in the patch title: memool -> mempool

Quoted 3 lines · David Hunt

adds a simple stack based mempool handler

What is the purpose of this mempool handler?

Is it an example or is it something that could be useful for dpdk applications?

If it's just an example, I think we could move this code in app/test.

app/test/test_mempool_perf.c line 57

Quoted 10 lines
app/test/test_mempool_perf.c +0 -1
@@ -52,7 +52,6 @@
  #include <rte_lcore.h>
  #include <rte_atomic.h>
  #include <rte_branch_prediction.h>
-#include <rte_ring.h>
  #include <rte_mempool.h>
  #include <rte_spinlock.h>
  #include <rte_malloc.h>

Is this change related?

app/test/test_mempool_perf.c

Quoted 10 lines
app/test/test_mempool_perf.c +10 -0
+struct rte_mempool_common_stack {
+	/* Spinlock to protect access */
+	rte_spinlock_t sl;
+
+	uint32_t size;
+	uint32_t len;
+	void *objs[];
+
+#ifdef RTE_LIBRTE_MEMPOOL_DEBUG
+#endif

There is nothing inside the #ifdef

Quoted 19 lines
app/test/test_mempool_perf.c +19 -0
+static void *
+common_stack_alloc(struct rte_mempool *mp,
+		const char *name, unsigned n, int socket_id, unsigned flags)
+{
+	struct rte_mempool_common_stack *s;
+	char stack_name[RTE_RING_NAMESIZE];
+
+	int size = sizeof(*s) + (n+16)*sizeof(void *);
+
+	flags = flags;
+
+	/* Allocate our local memory structure */
+	snprintf(stack_name, sizeof(stack_name), "%s-common-stack", name);
+	s = rte_zmalloc_socket(stack_name,
+					size, RTE_CACHE_LINE_SIZE, socket_id);
+	if (s == NULL) {
+		RTE_LOG(ERR, MEMPOOL, "Cannot allocate stack!\n");
+		return NULL;
+	}

app/test/test_mempool_perf.c

Quoted 10 lines
app/test/test_mempool_perf.c +10 -0
+
+	/* And the spinlock we use to protect access */
+	rte_spinlock_init(&s->sl);
+
+	s->size = n;
+	mp->rt_pool = (void *) s;
+	mp->handler_idx = rte_get_mempool_handler("stack");
+
+	return (void *) s;
+}

The explicit casts could be removed I think.

app/test/test_mempool_perf.c

Quoted 6 lines
app/test/test_mempool_perf.c +6 -0
+
+static int common_stack_put(void *p, void * const *obj_table,
+		unsigned n)
+{
+	struct rte_mempool_common_stack *s =
+				(struct rte_mempool_common_stack *)p;

indent issue (same in get() and count())

app/test/test_mempool_perf.c

Quoted 12 lines
app/test/test_mempool_perf.c +12 -0
+	void **cache_objs;
+	unsigned index;
+
+	/* Acquire lock */
+	rte_spinlock_lock(&s->sl);
+	cache_objs = &s->objs[s->len];
+
+	/* Is there sufficient space in the stack ? */
+	if ((s->len + n) > s->size) {
+		rte_spinlock_unlock(&s->sl);
+		return -ENOENT;
+	}

I think this cannot happen as there is a check in the get(). I wonder if a rte_panic() wouldn't be better here.

Regards,
Olivier

author
#15 lore raw

PATCH 3/5 mempool: add custom external mempool handler example

In reply to David Hunt

adds a simple ring-based mempool handler using mallocs for each object

---
 lib/librte_mempool/Makefile         |   1 +
 lib/librte_mempool/custom_mempool.c | 160 ++++++++++++++++++++++++++++++++++++
 2 files changed, 161 insertions(+)
 create mode 100644 lib/librte_mempool/custom_mempool.c
lib/librte_mempool/Makefile +1 -0
index d795b48..4f72546 100644
@@ -44,6 +44,7 @@ LIBABIVER := 1
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool.c
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_default.c
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_stack.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  custom_mempool.c
 ifeq ($(CONFIG_RTE_LIBRTE_XEN_DOM0),y)
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_dom0_mempool.c
 endif
lib/librte_mempool/custom_mempool.c new file +160 -0

160 lines not on this page · raw message

Signature · 1 line
1.9.3
#16 lore raw

Re: [PATCH 3/5] mempool: add custom external mempool handler example

Quoted 1 line · David Hunt

adds a simple ring-based mempool handler using mallocs for each object

nit,

$ git am /export/dh/3
Applying: mempool: add custom external mempool handler example
/export/dpdk-master/.git/rebase-apply/patch:184: new blank line at EOF.
+
warning: 1 line adds whitespace errors.
Quoted 189 lines
---
 lib/librte_mempool/Makefile         |   1 +
 lib/librte_mempool/custom_mempool.c | 160 ++++++++++++++++++++++++++++++++++++
 2 files changed, 161 insertions(+)
 create mode 100644 lib/librte_mempool/custom_mempool.c
lib/librte_mempool/Makefile +1 -0
index d795b48..4f72546 100644
@@ -44,6 +44,7 @@ LIBABIVER := 1
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool.c
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_default.c
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_stack.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  custom_mempool.c
 ifeq ($(CONFIG_RTE_LIBRTE_XEN_DOM0),y)
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_dom0_mempool.c
 endif
lib/librte_mempool/custom_mempool.c new file +160 -0
new file mode 100644
index 0000000..a9da8c5
@@ -0,0 +1,160 @@
+/*-
+ *   BSD LICENSE
+ *
+ *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <stdarg.h>
+#include <errno.h>
+#include <sys/queue.h>
+
+#include <rte_mempool.h>
+
+#include "rte_mempool_internal.h"
+
+/*
+ * Mempool
+ * =======
+ *
+ * Basic tests: done on one core with and without cache:
+ *
+ *    - Get one object, put one object
+ *    - Get two objects, put two objects
+ *    - Get all objects, test that their content is not modified and
+ *      put them back in the pool.
+ */
+
+#define TIME_S 5
+#define MEMPOOL_ELT_SIZE 2048
+#define MAX_KEEP 128
+#define MEMPOOL_SIZE 8192
+
+#if 0
+/*
+ * For our example mempool handler, we use the following struct to
+ * pass info to our create callback so it can call rte_mempool_create
+ */
+struct custom_mempool_alloc_params {
+	char ring_name[RTE_RING_NAMESIZE];
+	unsigned n_elt;
+	unsigned elt_size;
+};
+#endif
+
+/*
+ * Simple example of custom mempool structure. Holds pointers to all the
+ * elements which are simply malloc'd in this example.
+ */
+struct custom_mempool {
+	struct rte_ring *r;             /* Ring to manage elements */
+	void *elements[MEMPOOL_SIZE];   /* Element pointers */
+};
+
+/*
+ * Loop though all the element pointers and allocate a chunk of memory, then
+ * insert that memory into the ring.
+ */
+static void *
+custom_mempool_alloc(struct rte_mempool *mp,
+		const char *name, unsigned n,
+		__attribute__((unused)) int socket_id,
+		__attribute__((unused)) unsigned flags)
+
+{
+	static struct custom_mempool *cm;
+	uint32_t *objnum;
+	unsigned int i;
+
+	cm = malloc(sizeof(struct custom_mempool));
+
+	/* Create the ring so we can enqueue/dequeue */
+	cm->r = rte_ring_create(name,
+						rte_align32pow2(n+1), 0, 0);
+	if (cm->r == NULL)
+		return NULL;
+
+	/*
+	 * Loop around the elements an allocate the required memory
+	 * and place them in the ring.
+	 * Not worried about alignment or performance for this example.
+	 * Also, set the first 32-bits to be the element number so we
+	 * can check later on.
+	 */
+	for (i = 0; i < n; i++) {
+		cm->elements[i] = malloc(mp->elt_size);
+		memset(cm->elements[i], 0, mp->elt_size);
+		objnum = (uint32_t *)cm->elements[i];
+		*objnum = i;
+		rte_ring_sp_enqueue_bulk(cm->r, &(cm->elements[i]), 1);
+	}
+
+	return cm;
+}
+
+static int
+custom_mempool_put(void *p, void * const *obj_table, unsigned n)
+{
+	struct custom_mempool *cm = (struct custom_mempool *)p;
+
+	return rte_ring_mp_enqueue_bulk(cm->r, obj_table, n);
+}
+
+
+static int
+custom_mempool_get(void *p, void **obj_table, unsigned n)
+{
+	struct custom_mempool *cm = (struct custom_mempool *)p;
+
+	return rte_ring_mc_dequeue_bulk(cm->r, obj_table, n);
+}
+
+static unsigned
+custom_mempool_get_count(void *p)
+{
+	struct custom_mempool *cm = (struct custom_mempool *)p;
+
+	return rte_ring_count(cm->r);
+}
+
+static struct rte_mempool_handler mempool_handler_custom = {
+	.name = "custom_handler",
+	.alloc = custom_mempool_alloc,
+	.put = custom_mempool_put,
+	.get = custom_mempool_get,
+	.get_count = custom_mempool_get_count,
+};
+
+REGISTER_MEMPOOL_HANDLER(mempool_handler_custom);
+
Signature · 1 line
1.9.3
author
#17 lore raw

PATCH 4/5 mempool: add autotest for external mempool custom example

In reply to David Hunt
---
 app/test/Makefile           |   1 +
 app/test/test_ext_mempool.c | 474 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 475 insertions(+)
 create mode 100644 app/test/test_ext_mempool.c
app/test/Makefile +1 -0
index ec33e1a..9a2f75f 100644
@@ -74,6 +74,7 @@ SRCS-$(CONFIG_RTE_LIBRTE_TIMER) += test_timer_perf.c
 SRCS-$(CONFIG_RTE_LIBRTE_TIMER) += test_timer_racecond.c
 
 SRCS-y += test_mempool.c
+SRCS-y += test_ext_mempool.c
 SRCS-y += test_mempool_perf.c
 
 SRCS-y += test_mbuf.c
app/test/test_ext_mempool.c new file +474 -0

474 lines not on this page · raw message

Signature · 1 line
1.9.3
author
#18 lore raw

PATCH 5/5 mempool: allow rte_pktmbuf_pool_create switch between memool handlers

In reply to David Hunt

if the user wants to have rte_pktmbuf_pool_create() use an external mempool handler, they simply define MEMPOOL_HANDLER_NAME to be the name of the mempool handler they wish to use. May move this to config

---
 lib/librte_mbuf/rte_mbuf.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
lib/librte_mbuf/rte_mbuf.c +11 -0
index c18b438..362396e 100644
@@ -167,10 +167,21 @@ rte_pktmbuf_pool_create(const char *name, unsigned n,
 	mbp_priv.mbuf_data_room_size = data_room_size;
 	mbp_priv.mbuf_priv_size = priv_size;
 
+/* #define MEMPOOL_HANDLER_NAME "custom_handler" */
+#undef MEMPOOL_HANDLER_NAME
+
+#ifndef MEMPOOL_HANDLER_NAME
 	return rte_mempool_create(name, n, elt_size,
 		cache_size, sizeof(struct rte_pktmbuf_pool_private),
 		rte_pktmbuf_pool_init, &mbp_priv, rte_pktmbuf_init, NULL,
 		socket_id, 0);
+#else
+	return rte_mempool_create_ext(name, n, elt_size,
+		cache_size, sizeof(struct rte_pktmbuf_pool_private),
+		rte_pktmbuf_pool_init, &mbp_priv, rte_pktmbuf_init, NULL,
+		socket_id, 0,
+		MEMPOOL_HANDLER_NAME);
+#endif
 }
 
 /* do some sanity checks on a mbuf: panic if it fails */
Signature · 1 line
1.9.3
#19 lore raw

Re: [PATCH 5/5] mempool: allow rte_pktmbuf_pool_create switch between memool handlers

Hi David,

Quoted 3 lines · David Hunt

if the user wants to have rte_pktmbuf_pool_create() use an external mempool handler, they simply define MEMPOOL_HANDLER_NAME to be the name of the mempool handler they wish to use. May move this to config

I agree it could move to configuration.

Quoted 20 lines
---
 lib/librte_mbuf/rte_mbuf.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
lib/librte_mbuf/rte_mbuf.c +4 -0
index c18b438..362396e 100644
@@ -167,10 +167,21 @@ rte_pktmbuf_pool_create(const char *name, unsigned n,
 	mbp_priv.mbuf_data_room_size = data_room_size;
 	mbp_priv.mbuf_priv_size = priv_size;
 
+/* #define MEMPOOL_HANDLER_NAME "custom_handler" */
+#undef MEMPOOL_HANDLER_NAME
+
+#ifndef MEMPOOL_HANDLER_NAME
 	return rte_mempool_create(name, n, elt_size,
 		cache_size, sizeof(struct rte_pktmbuf_pool_private),

lib/librte_mbuf/rte_mbuf.c rte_pktmbuf_pool_create(const char *name, unsigned n, · line 185

Quoted 10 lines
lib/librte_mbuf/rte_mbuf.c +7 -0
 		rte_pktmbuf_pool_init, &mbp_priv, rte_pktmbuf_init, NULL,
 		socket_id, 0);
+#else
+	return rte_mempool_create_ext(name, n, elt_size,
+		cache_size, sizeof(struct rte_pktmbuf_pool_private),
+		rte_pktmbuf_pool_init, &mbp_priv, rte_pktmbuf_init, NULL,
+		socket_id, 0,
+		MEMPOOL_HANDLER_NAME);
+#endif
 }

Is it possible to always use rte_mempool_create_ext(), and set the default handler name to 'ring_mp_mc'?

After checking in rte_mempool_create(), I found the behavior is different when using xen. Do you know if xen still works with your patches?

Regards,
Olivier

#20 lore raw
In reply to David Hunt
Quoted 8 lines · David Hunt

Hi all on the list.

Here's a proposed patch for an external mempool manager

The External Mempool Manager is an extension to the mempool API that allows users to add and use an external mempool manager, which allows external memory subsystems such as external hardware memory management systems and software based memory allocators to be used with DPDK.

I like this approach.It will be useful for external hardware memory pool managers.

BTW, Do you encounter any performance impact on changing to function pointer based approach?

Quoted 49 lines

The existing API to the internal DPDK mempool manager will remain unchanged and will be backward compatible.

There are two aspects to external mempool manager.
1. Adding the code for your new mempool handler. This is achieved by adding a new mempool handler source file into the librte_mempool library, and using the REGISTER_MEMPOOL_HANDLER macro.
2. Using the new API to call rte_mempool_create_ext to create a new mempool using the name parameter to identify which handler to use.

New API calls added
1. A new mempool 'create' function which accepts mempool handler name.
2. A new mempool 'rte_get_mempool_handler' function which accepts mempool handler name, and returns the index to the relevant set of callbacks for that mempool handler

Several external mempool managers may be used in the same application. A new mempool can then be created by using the new 'create' function, providing the mempool handler name to point the mempool to the relevant mempool manager callback structure.

The old 'create' function can still be called by legacy programs, and will internally work out the mempool handle based on the flags provided (single producer, single consumer, etc). By default handles are created internally to implement the built-in DPDK mempool manager and mempool types.

The external mempool manager needs to provide the following functions.
1. alloc - allocates the mempool memory, and adds each object onto a ring
2. put - puts an object back into the mempool once an application has
finished with it
3. get - gets an object from the mempool for use by the application
4. get_count - gets the number of available objects in the mempool
5. free - frees the mempool memory

Every time a get/put/get_count is called from the application/PMD, the callback for that mempool is called. These functions are in the fastpath, and any unoptimised handlers may limit performance.

The new APIs are as follows:

1. rte_mempool_create_ext

struct rte_mempool * rte_mempool_create_ext(const char * name, unsigned n,
unsigned cache_size, unsigned private_data_size, int socket_id, unsigned flags,
const char * handler_name);

Quoted 4 lines

2. rte_get_mempool_handler

int16_t
rte_get_mempool_handler(const char *name);

Do we need above public API as, in any case we need rte_mempool* pointer to operate on mempools(which has the index anyway)?

May a similar functional API with different name/return will be better to figure out, given "name" registered or not in ethernet driver which has dependency on a particular HW pool manager.

Quoted 62 lines

Please see rte_mempool.h for further information on the parameters.

The important thing to note is that the mempool handler is passed by name to rte_mempool_create_ext, and that in turn calls rte_get_mempool_handler to get the handler index, which is stored in the rte_memool structure. This allow multiple processes to use the same mempool, as the function pointers are accessed via handler index.

The mempool handler structure contains callbacks to the implementation of the handler, and is set up for registration as follows:

static struct rte_mempool_handler handler_sp_mc = {
.name = "ring_sp_mc",
.alloc = rte_mempool_common_ring_alloc, .put = common_ring_sp_put,
.get = common_ring_mc_get,
.get_count = common_ring_get_count, .free = common_ring_free,
};

And then the following macro will register the handler in the array of handlers

REGISTER_MEMPOOL_HANDLER(handler_mp_mc);

For and example of a simple malloc based mempool manager, see lib/librte_mempool/custom_mempool.c

For an example of API usage, please see app/test/test_ext_mempool.c, which implements a rudimentary mempool manager using simple mallocs for each mempool object (custom_mempool.c).

David Hunt (5):
mempool: add external mempool manager support
memool: add stack (lifo) based external mempool handler
mempool: add custom external mempool handler example
mempool: add autotest for external mempool custom example
mempool: allow rte_pktmbuf_pool_create switch between memool handlers

 app/test/Makefile                         |   1 +
 app/test/test_ext_mempool.c               | 470 ++++++++++++++++++++++++++++++
 app/test/test_mempool_perf.c              |   2 -
 lib/librte_mbuf/rte_mbuf.c                |  11 +
 lib/librte_mempool/Makefile               |   3 +
 lib/librte_mempool/custom_mempool.c       | 158 ++++++++++
 lib/librte_mempool/rte_mempool.c          | 208 +++++++++----
 lib/librte_mempool/rte_mempool.h          | 205 +++++++++++--
 lib/librte_mempool/rte_mempool_default.c  | 229 +++++++++++++++
 lib/librte_mempool/rte_mempool_internal.h |  70 +++++
 lib/librte_mempool/rte_mempool_stack.c    | 162 ++++++++++
 11 files changed, 1430 insertions(+), 89 deletions(-)
 create mode 100644 app/test/test_ext_mempool.c
 create mode 100644 lib/librte_mempool/custom_mempool.c
 create mode 100644 lib/librte_mempool/rte_mempool_default.c
 create mode 100644 lib/librte_mempool/rte_mempool_internal.h
 create mode 100644 lib/librte_mempool/rte_mempool_stack.c
Signature · 1 line
1.9.3
#21 lore raw
Quoted 10 lines · Jerin Jacob
Quoted 8 lines · David Hunt

Hi all on the list.

Here's a proposed patch for an external mempool manager

The External Mempool Manager is an extension to the mempool API that allows users to add and use an external mempool manager, which allows external memory subsystems such as external hardware memory management systems and software based memory allocators to be used with DPDK.

Quoted 5 lines

I like this approach.It will be useful for external hardware memory pool managers.

BTW, Do you encounter any performance impact on changing to function pointer based approach?

Jerin,
Thanks for your comments.

The performance impacts I've seen depends on whether I'm using an object cache for the mempool or not. Without object cache, I see between 0-10% degradation. With object cache, I see a slight performance gain of between 0-5%. But that will most likely vary from system to system.

Quoted 53 lines
Quoted 48 lines

The existing API to the internal DPDK mempool manager will remain unchanged and will be backward compatible.

There are two aspects to external mempool manager.
1. Adding the code for your new mempool handler. This is achieved by adding a new mempool handler source file into the librte_mempool library, and using the REGISTER_MEMPOOL_HANDLER macro.
2. Using the new API to call rte_mempool_create_ext to create a new mempool using the name parameter to identify which handler to use.

New API calls added
1. A new mempool 'create' function which accepts mempool handler name.
2. A new mempool 'rte_get_mempool_handler' function which accepts mempool handler name, and returns the index to the relevant set of callbacks for that mempool handler

Several external mempool managers may be used in the same application. A new mempool can then be created by using the new 'create' function, providing the mempool handler name to point the mempool to the relevant mempool manager callback structure.

The old 'create' function can still be called by legacy programs, and will internally work out the mempool handle based on the flags provided (single producer, single consumer, etc). By default handles are created internally to implement the built-in DPDK mempool manager and mempool types.

The external mempool manager needs to provide the following functions.
1. alloc - allocates the mempool memory, and adds each object onto a ring
2. put - puts an object back into the mempool once an application has
finished with it
3. get - gets an object from the mempool for use by the application
4. get_count - gets the number of available objects in the mempool
5. free - frees the mempool memory

Every time a get/put/get_count is called from the application/PMD, the callback for that mempool is called. These functions are in the fastpath, and any unoptimised handlers may limit performance.

The new APIs are as follows:

1. rte_mempool_create_ext

struct rte_mempool * rte_mempool_create_ext(const char * name, unsigned n,
unsigned cache_size, unsigned private_data_size, int socket_id, unsigned flags,
const char * handler_name);

Quoted 4 lines

2. rte_get_mempool_handler

int16_t
rte_get_mempool_handler(const char *name);

Quoted 6 lines

Do we need above public API as, in any case we need rte_mempool* pointer to operate on mempools(which has the index anyway)?

May a similar functional API with different name/return will be better to figure out, given "name" registered or not in ethernet driver which has dependency on a particular HW pool manager.

Good point. An earlier revision required getting the index first, then passing that to the create_ext call. Now that the call is by name, the 'get' is mostly redundant. As you suggest, we may need an API for checking the existence of a particular manager/handler. Then again, we could always return an error from the create_ext api if it fails to find that handler. I'll remove the 'get' for the moment.

Thanks,
David.

#22 lore raw
Quoted 85 lines · David
Quoted 10 lines · Jerin Jacob
Quoted 8 lines · David Hunt

Hi all on the list.

Here's a proposed patch for an external mempool manager

The External Mempool Manager is an extension to the mempool API that allows users to add and use an external mempool manager, which allows external memory subsystems such as external hardware memory management systems and software based memory allocators to be used with DPDK.

Quoted 5 lines

I like this approach.It will be useful for external hardware memory pool managers.

BTW, Do you encounter any performance impact on changing to function pointer based approach?

Jerin,
Thanks for your comments.

The performance impacts I've seen depends on whether I'm using an object cache for the mempool or not. Without object cache, I see between 0-10% degradation. With object cache, I see a slight performance gain of between 0-5%. But that will most likely vary from system to system.

Quoted 53 lines
Quoted 48 lines

The existing API to the internal DPDK mempool manager will remain unchanged and will be backward compatible.

There are two aspects to external mempool manager.
1. Adding the code for your new mempool handler. This is achieved by adding a new mempool handler source file into the librte_mempool library, and using the REGISTER_MEMPOOL_HANDLER macro.
2. Using the new API to call rte_mempool_create_ext to create a new mempool using the name parameter to identify which handler to use.

New API calls added
1. A new mempool 'create' function which accepts mempool handler name.
2. A new mempool 'rte_get_mempool_handler' function which accepts mempool handler name, and returns the index to the relevant set of callbacks for that mempool handler

Several external mempool managers may be used in the same application. A new mempool can then be created by using the new 'create' function, providing the mempool handler name to point the mempool to the relevant mempool manager callback structure.

The old 'create' function can still be called by legacy programs, and will internally work out the mempool handle based on the flags provided (single producer, single consumer, etc). By default handles are created internally to implement the built-in DPDK mempool manager and mempool types.

The external mempool manager needs to provide the following functions.
1. alloc - allocates the mempool memory, and adds each object onto a ring
2. put - puts an object back into the mempool once an application has
finished with it
3. get - gets an object from the mempool for use by the application
4. get_count - gets the number of available objects in the mempool
5. free - frees the mempool memory

Every time a get/put/get_count is called from the application/PMD, the callback for that mempool is called. These functions are in the fastpath, and any unoptimised handlers may limit performance.

The new APIs are as follows:

1. rte_mempool_create_ext

struct rte_mempool * rte_mempool_create_ext(const char * name, unsigned n,
unsigned cache_size, unsigned private_data_size, int socket_id, unsigned flags,
const char * handler_name);

Quoted 4 lines

2. rte_get_mempool_handler

int16_t
rte_get_mempool_handler(const char *name);

Quoted 6 lines

Do we need above public API as, in any case we need rte_mempool* pointer to operate on mempools(which has the index anyway)?

May a similar functional API with different name/return will be better to figure out, given "name" registered or not in ethernet driver which has dependency on a particular HW pool manager.

Quoted 6 lines

Good point. An earlier revision required getting the index first, then passing that to the create_ext call. Now that the call is by name, the 'get' is mostly redundant. As you suggest, we may need an API for checking the existence of a particular manager/handler. Then again, we could always return an error from the create_ext api if it fails to find that handler. I'll remove the 'get' for the moment.

OK. But I think an API to get external pool manager name should be required. It's useful in ethernet driver where driver needs to take care of any special arrangement based on a specific any HW pool manager

something like below, feel free to chage the API name,

inline char* __attribute__((always_inline))
rte_mempool_ext_get_name(struct rte_mempool *mp)

{
        return (mempool_handler_list.handler[mp->handler_idx].name)
}
Quoted 10 lines

Thanks,
David.

author
#23 lore raw

cover external mempool manager

In reply to David Hunt

Hi list.

Here's the v2 version of a proposed patch for an external mempool manager

v2 changes:
* There was a lot of duplicate code between rte_mempool_xmem_create and rte_mempool_create_ext. This has now been refactored and is now hopefully cleaner.
* The RTE_NEXT_ABI define is now used to allow building of the library in a format that is compatible with binaries built against previous versions of DPDK.
* Changes out of code reviews. Hopefully I've got most of them included.

The External Mempool Manager is an extension to the mempool API that allows users to add and use an external mempool manager, which allows external memory subsystems such as external hardware memory management systems and software based memory allocators to be used with DPDK.

The existing API to the internal DPDK mempool manager will remain unchanged and will be backward compatible. However, there will be an ABI breakage, as the mempool struct is changing. These changes are all contained withing RTE_NEXT_ABI defs, and the current or next code can be changed with the CONFIG_RTE_NEXT_ABI config setting

There are two aspects to external mempool manager.
1. Adding the code for your new mempool handler. This is achieved by adding a new mempool handler source file into the librte_mempool library, and using the REGISTER_MEMPOOL_HANDLER macro.
2. Using the new API to call rte_mempool_create_ext to create a new mempool using the name parameter to identify which handler to use.

New API calls added
1. A new mempool 'create' function which accepts mempool handler name.
2. A new mempool 'rte_get_mempool_handler' function which accepts mempool handler name, and returns the index to the relevant set of callbacks for that mempool handler

Several external mempool managers may be used in the same application. A new mempool can then be created by using the new 'create' function, providing the mempool handler name to point the mempool to the relevant mempool manager callback structure.

The old 'create' function can still be called by legacy programs, and will internally work out the mempool handle based on the flags provided (single producer, single consumer, etc). By default handles are created internally to implement the built-in DPDK mempool manager and mempool types.

The external mempool manager needs to provide the following functions.
1. alloc - allocates the mempool memory, and adds each object onto a ring
2. put - puts an object back into the mempool once an application has
finished with it
3. get - gets an object from the mempool for use by the application
4. get_count - gets the number of available objects in the mempool
5. free - frees the mempool memory

Every time a get/put/get_count is called from the application/PMD, the callback for that mempool is called. These functions are in the fastpath, and any unoptimised handlers may limit performance.

The new APIs are as follows:

1. rte_mempool_create_ext

struct rte_mempool * rte_mempool_create_ext(const char * name, unsigned n,
unsigned cache_size, unsigned private_data_size, int socket_id, unsigned flags,
const char * handler_name);

2. rte_mempool_get_handler_name

char *
rte_mempool_get_handler_name(struct rte_mempool *mp);

Please see rte_mempool.h for further information on the parameters.

The important thing to note is that the mempool handler is passed by name to rte_mempool_create_ext, and that in turn calls rte_get_mempool_handler to get the handler index, which is stored in the rte_memool structure. This allow multiple processes to use the same mempool, as the function pointers are accessed via handler index.

The mempool handler structure contains callbacks to the implementation of the handler, and is set up for registration as follows:

static struct rte_mempool_handler handler_sp_mc = {
.name = "ring_sp_mc",
.alloc = rte_mempool_common_ring_alloc, .put = common_ring_sp_put,
.get = common_ring_mc_get,
.get_count = common_ring_get_count, .free = common_ring_free,
};

And then the following macro will register the handler in the array of handlers

REGISTER_MEMPOOL_HANDLER(handler_mp_mc);

For and example of a simple malloc based mempool manager, see lib/librte_mempool/custom_mempool.c

For an example of API usage, please see app/test/test_ext_mempool.c, which implements a rudimentary mempool manager using simple mallocs for each mempool object (custom_mempool.c).

David Hunt (6):
mempool: add external mempool manager support
mempool: add stack (lifo) based external mempool handler
mempool: adds a simple ring-based mempool handler using mallocs for
objects
mempool: add autotest for external mempool custom example
mempool: allow rte_pktmbuf_pool_create switch between memool handlers
mempool: add in the RTE_NEXT_ABI protection for ABI breakages

 app/test/Makefile                          |   3 +
 app/test/test_ext_mempool.c                | 451 +++++++++++++++++++++++++++++
 app/test/test_mempool_perf.c               |   2 +
 config/common_bsdapp                       |   2 +
 config/common_linuxapp                     |   2 +
 lib/librte_mbuf/rte_mbuf.c                 |  15 +
 lib/librte_mempool/Makefile                |   5 +
 lib/librte_mempool/custom_mempool.c        | 146 ++++++++++
 lib/librte_mempool/rte_mempool.c           | 383 ++++++++++++++++++++++--
 lib/librte_mempool/rte_mempool.h           | 213 +++++++++++++-
 lib/librte_mempool/rte_mempool_default.c   | 236 +++++++++++++++
 lib/librte_mempool/rte_mempool_internal.h  |  75 +++++
 lib/librte_mempool/rte_mempool_stack.c     | 164 +++++++++++
 lib/librte_mempool/rte_mempool_version.map |   1 +
 14 files changed, 1665 insertions(+), 33 deletions(-)
 create mode 100644 app/test/test_ext_mempool.c
 create mode 100644 lib/librte_mempool/custom_mempool.c
 create mode 100644 lib/librte_mempool/rte_mempool_default.c
 create mode 100644 lib/librte_mempool/rte_mempool_internal.h
 create mode 100644 lib/librte_mempool/rte_mempool_stack.c
Signature · 1 line
2.5.0
author
#24 lore raw

PATCH 1/6 mempool: add external mempool manager support

Adds the new rte_mempool_create_ext api and callback mechanism for external mempool handlers

Modifies the existing rte_mempool_create to set up the handler_idx to the relevant mempool handler based on the handler name:
ring_sp_sc
ring_mp_mc
ring_sp_mc
ring_mp_sc

v2: merges the duplicated code in rte_mempool_xmem_create and rte_mempool_create_ext into one common function. The old functions now call the new common function with the relevant parameters.

---
 app/test/test_mempool_perf.c               |   1 -
 lib/librte_mempool/Makefile                |   2 +
 lib/librte_mempool/rte_mempool.c           | 383 ++++++++++++++++++-----------
 lib/librte_mempool/rte_mempool.h           | 200 ++++++++++++---
 lib/librte_mempool/rte_mempool_default.c   | 236 ++++++++++++++++++
 lib/librte_mempool/rte_mempool_internal.h  |  75 ++++++
 lib/librte_mempool/rte_mempool_version.map |   1 +
 7 files changed, 717 insertions(+), 181 deletions(-)
 create mode 100644 lib/librte_mempool/rte_mempool_default.c
 create mode 100644 lib/librte_mempool/rte_mempool_internal.h
app/test/test_mempool_perf.c +0 -1
index cdc02a0..091c1df 100644
@@ -161,7 +161,6 @@ per_lcore_mempool_test(__attribute__((unused)) void *arg)
 							   n_get_bulk);
 				if (unlikely(ret < 0)) {
 					rte_mempool_dump(stdout, mp);
-					rte_ring_dump(stdout, mp->ring);
 					/* in this case, objects are lost... */
 					return -1;
 				}
lib/librte_mempool/Makefile +2 -0
index a6898ef..aeaffd1 100644
@@ -42,6 +42,8 @@ LIBABIVER := 1
 
 # all source are stored in SRCS-y
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_default.c
+
 ifeq ($(CONFIG_RTE_LIBRTE_XEN_DOM0),y)
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_dom0_mempool.c
 endif
lib/librte_mempool/rte_mempool.c +233 -150

513 lines not on this page · raw message

lib/librte_mempool/rte_mempool.h +170 -30

283 lines not on this page · raw message

lib/librte_mempool/rte_mempool_default.c new file +236 -0

236 lines not on this page · raw message

lib/librte_mempool/rte_mempool_internal.h new file +75 -0

75 lines not on this page · raw message

lib/librte_mempool/rte_mempool_version.map +1 -0
index 17151e0..589db27 100644
@@ -6,6 +6,7 @@ DPDK_2.0 {
 	rte_mempool_calc_obj_size;
 	rte_mempool_count;
 	rte_mempool_create;
+	rte_mempool_create_ext;
 	rte_mempool_dump;
 	rte_mempool_list_dump;
 	rte_mempool_lookup;
Signature · 1 line
2.5.0
#25 lore raw

Re: [dpdk-dev, 1/6] mempool: add external mempool manager support

Hello David,

(I wanted to reply to the 0/6 patch but I couldn't find it anywhere in mbox format, nor on gmane which is strange.)

I could see both versions of the patch series quickly. I've got only one question at the moment. Would it be possible to somehow integrate calls to the dma_map/unmap_* interface of the kernel? I think, this is possible to be implemented through the vfio iommu group zero (hope so).

The issue is that without being able to explicitly flush buffers before a DMA transmission, it is not easily possible to use the mempool manager on most ARMv7 chips. Well, it can be done by calling dma_alloc_coherent and pass this memory into the mempool manager. But such memory is very slow (it is, say, non-cacheable).

Regards
Jan

Quoted 1,217 lines · On Tue, 16 Feb 2016 14:48:10 +0000 David Hunt <[email protected]> wrote:

Adds the new rte_mempool_create_ext api and callback mechanism for external mempool handlers

Modifies the existing rte_mempool_create to set up the handler_idx to the relevant mempool handler based on the handler name:
ring_sp_sc
ring_mp_mc
ring_sp_mc
ring_mp_sc

v2: merges the duplicated code in rte_mempool_xmem_create and rte_mempool_create_ext into one common function. The old functions now call the new common function with the relevant parameters.

---
app/test/test_mempool_perf.c | 1 -

 lib/librte_mempool/Makefile                |   2 +
 lib/librte_mempool/rte_mempool.c           | 383 ++++++++++++++++++-----------
 lib/librte_mempool/rte_mempool.h           | 200 ++++++++++++---
 lib/librte_mempool/rte_mempool_default.c   | 236 ++++++++++++++++++
 lib/librte_mempool/rte_mempool_internal.h  |  75 ++++++
 lib/librte_mempool/rte_mempool_version.map |   1 +
 7 files changed, 717 insertions(+), 181 deletions(-)
 create mode 100644 lib/librte_mempool/rte_mempool_default.c
 create mode 100644 lib/librte_mempool/rte_mempool_internal.h
app/test/test_mempool_perf.c +0 -1
index cdc02a0..091c1df 100644
@@ -161,7 +161,6 @@ per_lcore_mempool_test(__attribute__((unused)) void *arg)
 							   n_get_bulk);
 				if (unlikely(ret < 0)) {
 					rte_mempool_dump(stdout, mp);
-					rte_ring_dump(stdout, mp->ring);
 					/* in this case, objects are lost... */
 					return -1;
 				}
lib/librte_mempool/Makefile +2 -0
index a6898ef..aeaffd1 100644
@@ -42,6 +42,8 @@ LIBABIVER := 1
 
 # all source are stored in SRCS-y
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_default.c
+
 ifeq ($(CONFIG_RTE_LIBRTE_XEN_DOM0),y)
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_dom0_mempool.c
 endif
lib/librte_mempool/rte_mempool.c +233 -150
index aff5f6d..a577a3e 100644
@@ -59,10 +59,11 @@
 #include <rte_spinlock.h>
 
 #include "rte_mempool.h"
+#include "rte_mempool_internal.h"
 
 TAILQ_HEAD(rte_mempool_list, rte_tailq_entry);
 
-static struct rte_tailq_elem rte_mempool_tailq = {
+struct rte_tailq_elem rte_mempool_tailq = {
 	.name = "RTE_MEMPOOL",
 };
 EAL_REGISTER_TAILQ(rte_mempool_tailq)
@@ -149,7 +150,7 @@ mempool_add_elem(struct rte_mempool *mp, void *obj, uint32_t obj_idx,
 		obj_init(mp, obj_init_arg, obj, obj_idx);
 
 	/* enqueue in ring */
-	rte_ring_sp_enqueue(mp->ring, obj);
+	rte_mempool_ext_put_bulk(mp, &obj, 1);
 }
 
 uint32_t
@@ -375,26 +376,6 @@ rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz,
 	return usz;
 }
 
-#ifndef RTE_LIBRTE_XEN_DOM0
-/* stub if DOM0 support not configured */
-struct rte_mempool *
-rte_dom0_mempool_create(const char *name __rte_unused,
-			unsigned n __rte_unused,
-			unsigned elt_size __rte_unused,
-			unsigned cache_size __rte_unused,
-			unsigned private_data_size __rte_unused,
-			rte_mempool_ctor_t *mp_init __rte_unused,
-			void *mp_init_arg __rte_unused,
-			rte_mempool_obj_ctor_t *obj_init __rte_unused,
-			void *obj_init_arg __rte_unused,
-			int socket_id __rte_unused,
-			unsigned flags __rte_unused)
-{
-	rte_errno = EINVAL;
-	return NULL;
-}
-#endif
-
 /* create the mempool */
 struct rte_mempool *
 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
@@ -420,117 +401,76 @@ rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
 }
 
 /*
+ * Common mempool create function.
  * Create the mempool over already allocated chunk of memory.
  * That external memory buffer can consists of physically disjoint pages.
  * Setting vaddr to NULL, makes mempool to fallback to original behaviour
- * and allocate space for mempool and it's elements as one big chunk of
- * physically continuos memory.
- * */
-struct rte_mempool *
-rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
+ * which will call rte_mempool_ext_alloc to allocate the object memory.
+ * If it is an intenal mempool handler, it will allocate space for mempool
+ * and it's elements as one big chunk of physically continuous memory.
+ * If it is an external mempool handler, it will allocate space for mempool
+ * and call the rte_mempool_ext_alloc for the object memory.
+ */
+static struct rte_mempool *
+mempool_create(const char *name,
+		unsigned num_elt, unsigned elt_size,
 		unsigned cache_size, unsigned private_data_size,
 		rte_mempool_ctor_t *mp_init, void *mp_init_arg,
 		rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
-		int socket_id, unsigned flags, void *vaddr,
-		const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
+		int socket_id, unsigned flags,
+		void *vaddr, const phys_addr_t paddr[],
+		uint32_t pg_num, uint32_t pg_shift,
+		const char *handler_name)
 {
-	char mz_name[RTE_MEMZONE_NAMESIZE];
-	char rg_name[RTE_RING_NAMESIZE];
+	const struct rte_memzone *mz;
 	struct rte_mempool_list *mempool_list;
 	struct rte_mempool *mp = NULL;
 	struct rte_tailq_entry *te;
-	struct rte_ring *r;
-	const struct rte_memzone *mz;
-	size_t mempool_size;
+	char mz_name[RTE_MEMZONE_NAMESIZE];
 	int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
-	int rg_flags = 0;
-	void *obj;
 	struct rte_mempool_objsz objsz;
-	void *startaddr;
+	void *startaddr = NULL;
 	int page_size = getpagesize();
-
-	/* compilation-time checks */
-	RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
-			  RTE_CACHE_LINE_MASK) != 0);
-#if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
-	RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
-			  RTE_CACHE_LINE_MASK) != 0);
-	RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, local_cache) &
-			  RTE_CACHE_LINE_MASK) != 0);
-#endif
-#ifdef RTE_LIBRTE_MEMPOOL_DEBUG
-	RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
-			  RTE_CACHE_LINE_MASK) != 0);
-	RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
-			  RTE_CACHE_LINE_MASK) != 0);
-#endif
+	void *obj = NULL;
+	size_t mempool_size;
 
 	mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
 
 	/* asked cache too big */
 	if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
-	    CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
+		CALC_CACHE_FLUSHTHRESH(cache_size) > num_elt) {
 		rte_errno = EINVAL;
 		return NULL;
 	}
 
-	/* check that we have both VA and PA */
-	if (vaddr != NULL && paddr == NULL) {
-		rte_errno = EINVAL;
-		return NULL;
-	}
-
-	/* Check that pg_num and pg_shift parameters are valid. */
-	if (pg_num < RTE_DIM(mp->elt_pa) || pg_shift > MEMPOOL_PG_SHIFT_MAX) {
-		rte_errno = EINVAL;
-		return NULL;
-	}
-
-	/* "no cache align" imply "no spread" */
-	if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
-		flags |= MEMPOOL_F_NO_SPREAD;
+	if (flags && MEMPOOL_F_INT_HANDLER) {
+		/* Check that pg_num and pg_shift parameters are valid. */
+		if (pg_num < RTE_DIM(mp->elt_pa) ||
+				pg_shift > MEMPOOL_PG_SHIFT_MAX) {
+			rte_errno = EINVAL;
+			return NULL;
+		}
 
-	/* ring flags */
-	if (flags & MEMPOOL_F_SP_PUT)
-		rg_flags |= RING_F_SP_ENQ;
-	if (flags & MEMPOOL_F_SC_GET)
-		rg_flags |= RING_F_SC_DEQ;
+		/* "no cache align" imply "no spread" */
+		if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
+			flags |= MEMPOOL_F_NO_SPREAD;
 
-	/* calculate mempool object sizes. */
-	if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
-		rte_errno = EINVAL;
-		return NULL;
+		/* calculate mempool object sizes. */
+		if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
+			rte_errno = EINVAL;
+			return NULL;
+		}
 	}
 
 	rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
 
-	/* allocate the ring that will be used to store objects */
-	/* Ring functions will return appropriate errors if we are
-	 * running as a secondary process etc., so no checks made
-	 * in this function for that condition */
-	snprintf(rg_name, sizeof(rg_name), RTE_MEMPOOL_MZ_FORMAT, name);
-	r = rte_ring_create(rg_name, rte_align32pow2(n+1), socket_id, rg_flags);
-	if (r == NULL)
-		goto exit;
-
 	/*
 	 * reserve a memory zone for this mempool: private data is
 	 * cache-aligned
 	 */
-	private_data_size = (private_data_size +
-			     RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK);
+	private_data_size = RTE_ALIGN_CEIL(private_data_size,
+						RTE_MEMPOOL_ALIGN);
 
-	if (! rte_eal_has_hugepages()) {
-		/*
-		 * expand private data size to a whole page, so that the
-		 * first pool element will start on a new standard page
-		 */
-		int head = sizeof(struct rte_mempool);
-		int new_size = (private_data_size + head) % page_size;
-		if (new_size) {
-			private_data_size += page_size - new_size;
-		}
-	}
 
 	/* try to allocate tailq entry */
 	te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
@@ -539,23 +479,51 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
 		goto exit;
 	}
 
-	/*
-	 * If user provided an external memory buffer, then use it to
-	 * store mempool objects. Otherwise reserve a memzone that is large
-	 * enough to hold mempool header and metadata plus mempool objects.
-	 */
-	mempool_size = MEMPOOL_HEADER_SIZE(mp, pg_num) + private_data_size;
-	mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
-	if (vaddr == NULL)
-		mempool_size += (size_t)objsz.total_size * n;
+	if (flags && MEMPOOL_F_INT_HANDLER) {
 
-	if (! rte_eal_has_hugepages()) {
+		if (!rte_eal_has_hugepages()) {
+			/*
+			 * expand private data size to a whole page, so that the
+			 * first pool element will start on a new standard page
+			 */
+			int head = sizeof(struct rte_mempool);
+			int new_size = (private_data_size + head) % page_size;
+
+			if (new_size)
+				private_data_size += page_size - new_size;
+		}
+
+
+		/*
+		 * If user provided an external memory buffer, then use it to
+		 * store mempool objects. Otherwise reserve a memzone that is
+		 * large enough to hold mempool header and metadata plus
+		 * mempool objects
+		 */
+		mempool_size =
+			MEMPOOL_HEADER_SIZE(mp, pg_num) + private_data_size;
+		mempool_size =
+			RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
+		if (vaddr == NULL)
+			mempool_size += (size_t)objsz.total_size * num_elt;
+
+		if (!rte_eal_has_hugepages()) {
+			/*
+			 * we want the memory pool to start on a page boundary,
+			 * because pool elements crossing page boundaries would
+			 * result in discontiguous physical addresses
+			 */
+			mempool_size += page_size;
+		}
+	} else {
 		/*
-		 * we want the memory pool to start on a page boundary,
-		 * because pool elements crossing page boundaries would
-		 * result in discontiguous physical addresses
+		 * If user provided an external memory buffer, then use it to
+		 * store mempool objects. Otherwise reserve a memzone that is
+		 * large enough to hold mempool header and metadata plus
+		 * mempool objects
 		 */
-		mempool_size += page_size;
+		mempool_size = sizeof(*mp) + private_data_size;
+		mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
 	}
 
 	snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
@@ -563,24 +531,29 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
 	mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
 
 	/*
-	 * no more memory: in this case we loose previously reserved
-	 * space for the ring as we cannot free it
+	 * no more memory
 	 */
 	if (mz == NULL) {
 		rte_free(te);
 		goto exit;
 	}
 
-	if (rte_eal_has_hugepages()) {
-		startaddr = (void*)mz->addr;
-	} else {
-		/* align memory pool start address on a page boundary */
-		unsigned long addr = (unsigned long)mz->addr;
-		if (addr & (page_size - 1)) {
-			addr += page_size;
-			addr &= ~(page_size - 1);
+	if (flags && MEMPOOL_F_INT_HANDLER) {
+
+		if (rte_eal_has_hugepages()) {
+			startaddr = (void *)mz->addr;
+		} else {
+			/* align memory pool start address on a page boundary */
+			unsigned long addr = (unsigned long)mz->addr;
+
+			if (addr & (page_size - 1)) {
+				addr += page_size;
+				addr &= ~(page_size - 1);
+			}
+			startaddr = (void *)addr;
 		}
-		startaddr = (void*)addr;
+	} else {
+		startaddr = (void *)mz->addr;
 	}
 
 	/* init the mempool structure */
@@ -588,8 +561,7 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
 	memset(mp, 0, sizeof(*mp));
 	snprintf(mp->name, sizeof(mp->name), "%s", name);
 	mp->phys_addr = mz->phys_addr;
-	mp->ring = r;
-	mp->size = n;
+	mp->size = num_elt;
 	mp->flags = flags;
 	mp->elt_size = objsz.elt_size;
 	mp->header_size = objsz.header_size;
@@ -598,35 +570,54 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
 	mp->cache_flushthresh = CALC_CACHE_FLUSHTHRESH(cache_size);
 	mp->private_data_size = private_data_size;
 
-	/* calculate address of the first element for continuous mempool. */
-	obj = (char *)mp + MEMPOOL_HEADER_SIZE(mp, pg_num) +
-		private_data_size;
-	obj = RTE_PTR_ALIGN_CEIL(obj, RTE_MEMPOOL_ALIGN);
-
-	/* populate address translation fields. */
-	mp->pg_num = pg_num;
-	mp->pg_shift = pg_shift;
-	mp->pg_mask = RTE_LEN2MASK(mp->pg_shift, typeof(mp->pg_mask));
+	mp->handler_idx = rte_get_mempool_handler_idx(handler_name);
+	if (mp->handler_idx < 0) {
+		RTE_LOG(ERR, MEMPOOL, "Cannot find mempool handler by name!\n");
+		rte_free(te);
+		goto exit;
+	}
 
-	/* mempool elements allocated together with mempool */
-	if (vaddr == NULL) {
-		mp->elt_va_start = (uintptr_t)obj;
-		mp->elt_pa[0] = mp->phys_addr +
-			(mp->elt_va_start - (uintptr_t)mp);
+	if (flags && MEMPOOL_F_INT_HANDLER) {
+		/* calculate address of first element for continuous mempool. */
+		obj = (char *)mp + MEMPOOL_HEADER_SIZE(mp, pg_num) +
+			private_data_size;
+		obj = RTE_PTR_ALIGN_CEIL(obj, RTE_MEMPOOL_ALIGN);
+
+		/* populate address translation fields. */
+		mp->pg_num = pg_num;
+		mp->pg_shift = pg_shift;
+		mp->pg_mask = RTE_LEN2MASK(mp->pg_shift, typeof(mp->pg_mask));
+
+		/* mempool elements allocated together with mempool */
+		if (vaddr == NULL) {
+			mp->elt_va_start = (uintptr_t)obj;
+			mp->elt_pa[0] = mp->phys_addr +
+				(mp->elt_va_start - (uintptr_t)mp);
+		/* mempool elements in a separate chunk of memory. */
+		} else {
+			mp->elt_va_start = (uintptr_t)vaddr;
+			memcpy(mp->elt_pa, paddr,
+				sizeof(mp->elt_pa[0]) * pg_num);
+		}
 
-	/* mempool elements in a separate chunk of memory. */
-	} else {
-		mp->elt_va_start = (uintptr_t)vaddr;
-		memcpy(mp->elt_pa, paddr, sizeof (mp->elt_pa[0]) * pg_num);
+		mp->elt_va_end = mp->elt_va_start;
 	}
 
-	mp->elt_va_end = mp->elt_va_start;
+	/* Parameters are setup. Call the mempool handler alloc */
+	mp->rt_pool =
+		rte_mempool_ext_alloc(mp, name, num_elt, socket_id, flags);
+	if (mp->rt_pool == NULL) {
+		RTE_LOG(ERR, MEMPOOL, "Failed to alloc mempool!\n");
+		rte_free(te);
+		goto exit;
+	}
 
 	/* call the initializer */
 	if (mp_init)
 		mp_init(mp, mp_init_arg);
 
-	mempool_populate(mp, n, 1, obj_init, obj_init_arg);
+	if (obj_init)
+		mempool_populate(mp, num_elt, 1, obj_init, obj_init_arg);
 
 	te->data = (void *) mp;
 
@@ -640,13 +631,79 @@ exit:
 	return mp;
 }
 
+/* Create the mempool over already allocated chunk of memory */
+struct rte_mempool *
+rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
+		unsigned cache_size, unsigned private_data_size,
+		rte_mempool_ctor_t *mp_init, void *mp_init_arg,
+		rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
+		int socket_id, unsigned flags, void *vaddr,
+		const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
+{
+	struct rte_mempool *mp = NULL;
+	char handler_name[RTE_MEMPOOL_NAMESIZE];
+
+
+	/* compilation-time checks */
+	RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
+			  RTE_CACHE_LINE_MASK) != 0);
+#if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
+	RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
+			  RTE_CACHE_LINE_MASK) != 0);
+	RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, local_cache) &
+			  RTE_CACHE_LINE_MASK) != 0);
+#endif
+#ifdef RTE_LIBRTE_MEMPOOL_DEBUG
+	RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
+			  RTE_CACHE_LINE_MASK) != 0);
+	RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
+			  RTE_CACHE_LINE_MASK) != 0);
+#endif
+
+
+	/* check that we have both VA and PA */
+	if (vaddr != NULL && paddr == NULL) {
+		rte_errno = EINVAL;
+		return NULL;
+	}
+
+	/*
+	 * Since we have 4 combinations of the SP/SC/MP/MC, and stack,
+	 * examine the
+	 * flags to set the correct index into the handler table.
+	 */
+	if (flags & (MEMPOOL_F_SP_PUT | MEMPOOL_F_SC_GET))
+		sprintf(handler_name, "%s", "ring_sp_sc");
+	else if (flags & MEMPOOL_F_SP_PUT)
+		sprintf(handler_name, "%s", "ring_sp_mc");
+	else if (flags & MEMPOOL_F_SC_GET)
+		sprintf(handler_name, "%s", "ring_mp_sc");
+	else
+		sprintf(handler_name, "%s", "ring_mp_mc");
+
+	flags |= MEMPOOL_F_INT_HANDLER;
+
+	mp = mempool_create(name,
+		n, elt_size,
+		cache_size, private_data_size,
+		mp_init, mp_init_arg,
+		obj_init, obj_init_arg,
+		socket_id,
+		flags,
+		vaddr, paddr,
+		pg_num, pg_shift,
+		handler_name);
+
+	return mp;
+}
+
 /* Return the number of entries in the mempool */
 unsigned
 rte_mempool_count(const struct rte_mempool *mp)
 {
 	unsigned count;
 
-	count = rte_ring_count(mp->ring);
+	count = rte_mempool_ext_get_count(mp);
 
 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
 	{
@@ -802,7 +859,6 @@ rte_mempool_dump(FILE *f, const struct rte_mempool *mp)
 
 	fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
 	fprintf(f, "  flags=%x\n", mp->flags);
-	fprintf(f, "  ring=<%s>@%p\n", mp->ring->name, mp->ring);
 	fprintf(f, "  phys_addr=0x%" PRIx64 "\n", mp->phys_addr);
 	fprintf(f, "  size=%"PRIu32"\n", mp->size);
 	fprintf(f, "  header_size=%"PRIu32"\n", mp->header_size);
@@ -825,7 +881,7 @@ rte_mempool_dump(FILE *f, const struct rte_mempool *mp)
 			mp->size);
 
 	cache_count = rte_mempool_dump_cache(f, mp);
-	common_count = rte_ring_count(mp->ring);
+	common_count = rte_mempool_ext_get_count(mp);
 	if ((cache_count + common_count) > mp->size)
 		common_count = mp->size - cache_count;
 	fprintf(f, "  common_pool_count=%u\n", common_count);
@@ -919,3 +975,30 @@ void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *),
 
 	rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
 }
+
+
+/* create the mempool using an external mempool manager */
+struct rte_mempool *
+rte_mempool_create_ext(const char *name, unsigned n, unsigned elt_size,
+	unsigned cache_size, unsigned private_data_size,
+	rte_mempool_ctor_t *mp_init, void *mp_init_arg,
+	rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
+	int socket_id, unsigned flags,
+	const char *handler_name)
+{
+	struct rte_mempool *mp = NULL;
+
+	mp = mempool_create(name,
+		n, elt_size,
+		cache_size, private_data_size,
+		mp_init, mp_init_arg,
+		obj_init, obj_init_arg,
+		socket_id, flags,
+		NULL, NULL,              /* vaddr, paddr */
+		0, 0,                    /* pg_num, pg_shift, */
+		handler_name);
+
+	return mp;
+
+
+}
lib/librte_mempool/rte_mempool.h +170 -30
index 9745bf0..3705fbd 100644
@@ -88,6 +88,8 @@ extern "C" {
 struct rte_mempool_debug_stats {
 	uint64_t put_bulk;         /**< Number of puts. */
 	uint64_t put_objs;         /**< Number of objects successfully put. */
+	uint64_t put_pool_bulk;    /**< Number of puts into pool. */
+	uint64_t put_pool_objs;    /**< Number of objects into pool. */
 	uint64_t get_success_bulk; /**< Successful allocation number. */
 	uint64_t get_success_objs; /**< Objects successfully allocated. */
 	uint64_t get_fail_bulk;    /**< Failed allocation number. */
@@ -175,12 +177,85 @@ struct rte_mempool_objtlr {
 #endif
 };
 
+/* Handler functions for external mempool support */
+typedef void *(*rte_mempool_alloc_t)(struct rte_mempool *mp,
+		const char *name, unsigned n, int socket_id, unsigned flags);
+typedef int (*rte_mempool_put_t)(void *p,
+		void * const *obj_table, unsigned n);
+typedef int (*rte_mempool_get_t)(void *p, void **obj_table,
+		unsigned n);
+typedef unsigned (*rte_mempool_get_count)(void *p);
+typedef int (*rte_mempool_free_t)(struct rte_mempool *mp);
+
+/**
+ * @internal wrapper for external mempool manager alloc callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @param name
+ *   Name of the memory pool.
+ * @param n
+ *   Number of objects in the mempool.
+ * @param socket_id
+ *   socket id on which to allocate.
+ * @param flags
+ *   general flags to allocate function (MEMPOOL_F_* flags)
+ */
+void *
+rte_mempool_ext_alloc(struct rte_mempool *mp,
+		const char *name, unsigned n, int socket_id, unsigned flags);
+
+/**
+ * @internal wrapper for external mempool manager get callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @param obj_table
+ *   Pointer to a table of void * pointers (objects).
+ * @param n
+ *	 Number of objects to get
+ */
+int
+rte_mempool_ext_get_bulk(struct rte_mempool *mp, void **obj_table,
+		unsigned n);
+
+/**
+ * @internal wrapper for external mempool manager put callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @param obj_table
+ *   Pointer to a table of void * pointers (objects).
+ * @param n
+ *   Number of objects to put
+ */
+int
+rte_mempool_ext_put_bulk(struct rte_mempool *mp, void * const *obj_table,
+		unsigned n);
+
+/**
+ * @internal wrapper for external mempool manager get_count callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ */
+int
+rte_mempool_ext_get_count(const struct rte_mempool *mp);
+
+/**
+ * @internal wrapper for external mempool manager free callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ */
+int
+rte_mempool_ext_free(struct rte_mempool *mp);
+
 /**
  * The RTE mempool structure.
  */
 struct rte_mempool {
 	char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
-	struct rte_ring *ring;           /**< Ring to store objects. */
 	phys_addr_t phys_addr;           /**< Phys. addr. of mempool struct. */
 	int flags;                       /**< Flags of the mempool. */
 	uint32_t size;                   /**< Size of the mempool. */
@@ -194,6 +269,11 @@ struct rte_mempool {
 
 	unsigned private_data_size;      /**< Size of private data. */
 
+	/* Common pool data structure pointer */
+	void *rt_pool;
+
+	int16_t handler_idx;
+
 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
 	/** Per-lcore local cache. */
 	struct rte_mempool_cache local_cache[RTE_MAX_LCORE];
@@ -223,6 +303,8 @@ struct rte_mempool {
 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
 #define MEMPOOL_F_SP_PUT         0x0004 /**< Default put is "single-producer".*/
 #define MEMPOOL_F_SC_GET         0x0008 /**< Default get is "single-consumer".*/
+#define MEMPOOL_F_INT_HANDLER    0x0020 /**< Using internal mempool handler */
+
 
 /**
  * @internal When debug is enabled, store some statistics.
@@ -753,7 +835,7 @@ void rte_mempool_dump(FILE *f, const struct rte_mempool *mp);
  */
 static inline void __attribute__((always_inline))
 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
-		    unsigned n, int is_mp)
+		    unsigned n, __attribute__((unused)) int is_mp)
 {
 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
 	struct rte_mempool_cache *cache;
@@ -769,8 +851,7 @@ __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
 
 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
 	/* cache is not enabled or single producer or non-EAL thread */
-	if (unlikely(cache_size == 0 || is_mp == 0 ||
-		     lcore_id >= RTE_MAX_LCORE))
+	if (unlikely(cache_size == 0 || lcore_id >= RTE_MAX_LCORE))
 		goto ring_enqueue;
 
 	/* Go straight to ring if put would overflow mem allocated for cache */
@@ -793,8 +874,8 @@ __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
 
 	cache->len += n;
 
-	if (cache->len >= flushthresh) {
-		rte_ring_mp_enqueue_bulk(mp->ring, &cache->objs[cache_size],
+	if (unlikely(cache->len >= flushthresh)) {
+		rte_mempool_ext_put_bulk(mp, &cache->objs[cache_size],
 				cache->len - cache_size);
 		cache->len = cache_size;
 	}
@@ -804,22 +885,10 @@ __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
 ring_enqueue:
 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
 
-	/* push remaining objects in ring */
-#ifdef RTE_LIBRTE_MEMPOOL_DEBUG
-	if (is_mp) {
-		if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
-			rte_panic("cannot put objects in mempool\n");
-	}
-	else {
-		if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
-			rte_panic("cannot put objects in mempool\n");
-	}
-#else
-	if (is_mp)
-		rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
-	else
-		rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
-#endif
+	/* Increment stats counter to tell us how many pool puts happened */
+	__MEMPOOL_STAT_ADD(mp, put_pool, n);
+
+	rte_mempool_ext_put_bulk(mp, obj_table, n);
 }
 
 
@@ -943,7 +1012,7 @@ rte_mempool_put(struct rte_mempool *mp, void *obj)
  */
 static inline int __attribute__((always_inline))
 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
-		   unsigned n, int is_mc)
+		   unsigned n, __attribute__((unused))int is_mc)
 {
 	int ret;
 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
@@ -954,8 +1023,8 @@ __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
 	uint32_t cache_size = mp->cache_size;
 
 	/* cache is not enabled or single consumer */
-	if (unlikely(cache_size == 0 || is_mc == 0 ||
-		     n >= cache_size || lcore_id >= RTE_MAX_LCORE))
+	if (unlikely(cache_size == 0 || n >= cache_size ||
+						lcore_id >= RTE_MAX_LCORE))
 		goto ring_dequeue;
 
 	cache = &mp->local_cache[lcore_id];
@@ -967,7 +1036,8 @@ __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
 		uint32_t req = n + (cache_size - cache->len);
 
 		/* How many do we require i.e. number to fill the cache + the request */
-		ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
+		ret = rte_mempool_ext_get_bulk(mp,
+						&cache->objs[cache->len], req);
 		if (unlikely(ret < 0)) {
 			/*
 			 * In the offchance that we are buffer constrained,
@@ -995,10 +1065,7 @@ ring_dequeue:
 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
 
 	/* get remaining objects from ring */
-	if (is_mc)
-		ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
-	else
-		ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
+	ret = rte_mempool_ext_get_bulk(mp, obj_table, n);
 
 	if (ret < 0)
 		__MEMPOOL_STAT_ADD(mp, get_fail, n);
@@ -1401,6 +1468,79 @@ ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz,
 void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *arg),
 		      void *arg);
 
+/**
+ * Function to get the name of a mempool handler
+ *
+ * @param mp
+ *   A pointer to the mempool structure.
+ * @return
+ *   The name of the mempool handler
+ */
+char *rte_mempool_get_handler_name(struct rte_mempool *mp);
+
+/**
+ * Create a new mempool named *name* in memory.
+ *
+ * This function uses an externally defined alloc callback to allocate memory.
+ * Its size is set to n elements.
+ * All elements of the mempool are allocated separately to the mempool header.
+ *
+ * @param name
+ *   The name of the mempool.
+ * @param n
+ *   The number of elements in the mempool. The optimum size (in terms of
+ *   memory usage) for a mempool is when n is a power of two minus one:
+ *   n = (2^q - 1).
+ * @param cache_size
+ *   If cache_size is non-zero, the rte_mempool library will try to
+ *   limit the accesses to the common lockless pool, by maintaining a
+ *   per-lcore object cache. This argument must be lower or equal to
+ *   CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE and n / 1.5. It is advised to choose
+ *   cache_size to have "n modulo cache_size == 0": if this is
+ *   not the case, some elements will always stay in the pool and will
+ *   never be used. The access to the per-lcore table is of course
+ *   faster than the multi-producer/consumer pool. The cache can be
+ *   disabled if the cache_size argument is set to 0; it can be useful to
+ *   avoid losing objects in cache. Note that even if not used, the
+ *   memory space for cache is always reserved in a mempool structure,
+ *   except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
+ * @param private_data_size
+ *   The size of the private data appended after the mempool
+ *   structure. This is useful for storing some private data after the
+ *   mempool structure, as is done for rte_mbuf_pool for example.
+ * @param mp_init
+ *   A function pointer that is called for initialization of the pool,
+ *   before object initialization. The user can initialize the private
+ *   data in this function if needed. This parameter can be NULL if
+ *   not needed.
+ * @param mp_init_arg
+ *   An opaque pointer to data that can be used in the mempool
+ *   constructor function.
+ * @param obj_init
+ *   A function pointer that is called for each object at
+ *   initialization of the pool. The user can set some meta data in
+ *   objects if needed. This parameter can be NULL if not needed.
+ *   The obj_init() function takes the mempool pointer, the init_arg,
+ *   the object pointer and the object number as parameters.
+ * @param obj_init_arg
+ *   An opaque pointer to data that can be used as an argument for
+ *   each call to the object constructor function.
+ * @param socket_id
+ *   The *socket_id* argument is the socket identifier in the case of
+ *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
+ *   constraint for the reserved zone.
+ * @param flags
+ * @return
+ *   The pointer to the new allocated mempool, on success. NULL on error
+ */
+struct rte_mempool *
+rte_mempool_create_ext(const char *name, unsigned n, unsigned elt_size,
+		unsigned cache_size, unsigned private_data_size,
+		rte_mempool_ctor_t *mp_init, void *mp_init_arg,
+		rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
+		int socket_id, unsigned flags,
+		const char *handler_name);
+
 #ifdef __cplusplus
 }
 #endif
lib/librte_mempool/rte_mempool_default.c new file +236 -0
new file mode 100644
index 0000000..ca3255e
@@ -0,0 +1,236 @@
+/*-
+ *   BSD LICENSE
+ *
+ *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <rte_mempool.h>
+#include <rte_malloc.h>
+#include <string.h>
+
+#include "rte_mempool.h"
+#include "rte_mempool_internal.h"
+
+/*
+ * Indirect jump table to support external memory pools
+ */
+struct rte_mempool_handler_list mempool_handler_list = {
+	.sl =  RTE_SPINLOCK_INITIALIZER ,
+	.num_handlers = 0
+};
+
+/*
+ * Returns the name of the mempool
+ */
+char *
+rte_mempool_get_handler_name(struct rte_mempool *mp) {
+	return mempool_handler_list.handler[mp->handler_idx].name;
+}
+
+int16_t
+rte_mempool_register_handler(struct rte_mempool_handler *h)
+{
+	int16_t handler_idx;
+
+	/*  */
+	rte_spinlock_lock(&mempool_handler_list.sl);
+
+	/* Check whether jump table has space */
+	if (mempool_handler_list.num_handlers >= RTE_MEMPOOL_MAX_HANDLER_IDX) {
+		rte_spinlock_unlock(&mempool_handler_list.sl);
+		RTE_LOG(ERR, MEMPOOL,
+				"Maximum number of mempool handlers exceeded\n");
+		return -1;
+	}
+
+	if ((h->put == NULL) || (h->get == NULL) ||
+		(h->get_count == NULL)) {
+		rte_spinlock_unlock(&mempool_handler_list.sl);
+		 RTE_LOG(ERR, MEMPOOL,
+					"Missing callback while registering mempool handler\n");
+		return -1;
+	}
+
+	/* add new handler index */
+	handler_idx = mempool_handler_list.num_handlers++;
+
+	snprintf(mempool_handler_list.handler[handler_idx].name,
+				RTE_MEMPOOL_NAMESIZE, "%s", h->name);
+	mempool_handler_list.handler[handler_idx].alloc = h->alloc;
+	mempool_handler_list.handler[handler_idx].put = h->put;
+	mempool_handler_list.handler[handler_idx].get = h->get;
+	mempool_handler_list.handler[handler_idx].get_count = h->get_count;
+
+	rte_spinlock_unlock(&mempool_handler_list.sl);
+
+	return handler_idx;
+}
+
+int16_t
+rte_get_mempool_handler_idx(const char *name)
+{
+	int16_t i;
+
+	for (i = 0; i < mempool_handler_list.num_handlers; i++) {
+		if (!strcmp(name, mempool_handler_list.handler[i].name))
+			return i;
+	}
+	return -1;
+}
+
+static int
+common_ring_mp_put(void *p, void * const *obj_table, unsigned n)
+{
+	return rte_ring_mp_enqueue_bulk((struct rte_ring *)p, obj_table, n);
+}
+
+static int
+common_ring_sp_put(void *p, void * const *obj_table, unsigned n)
+{
+	return rte_ring_sp_enqueue_bulk((struct rte_ring *)p, obj_table, n);
+}
+
+static int
+common_ring_mc_get(void *p, void **obj_table, unsigned n)
+{
+	return rte_ring_mc_dequeue_bulk((struct rte_ring *)p, obj_table, n);
+}
+
+static int
+common_ring_sc_get(void *p, void **obj_table, unsigned n)
+{
+	return rte_ring_sc_dequeue_bulk((struct rte_ring *)p, obj_table, n);
+}
+
+static unsigned
+common_ring_get_count(void *p)
+{
+	return rte_ring_count((struct rte_ring *)p);
+}
+
+
+static void *
+rte_mempool_common_ring_alloc(struct rte_mempool *mp,
+		const char *name, unsigned n, int socket_id, unsigned flags)
+{
+	struct rte_ring *r;
+	char rg_name[RTE_RING_NAMESIZE];
+	int rg_flags = 0;
+
+	if (flags & MEMPOOL_F_SP_PUT)
+		rg_flags |= RING_F_SP_ENQ;
+	if (flags & MEMPOOL_F_SC_GET)
+		rg_flags |= RING_F_SC_DEQ;
+
+	/* allocate the ring that will be used to store objects */
+	/* Ring functions will return appropriate errors if we are
+	 * running as a secondary process etc., so no checks made
+	 * in this function for that condition */
+	snprintf(rg_name, sizeof(rg_name), "%s-ring", name);
+	r = rte_ring_create(rg_name, rte_align32pow2(n+1), socket_id, rg_flags);
+	if (r == NULL)
+		return NULL;
+
+	mp->rt_pool = (void *)r;
+
+	return (void *) r;
+}
+
+void *
+rte_mempool_ext_alloc(struct rte_mempool *mp,
+		const char *name, unsigned n, int socket_id, unsigned flags)
+{
+	if (mempool_handler_list.handler[mp->handler_idx].alloc) {
+		return (mempool_handler_list.handler[mp->handler_idx].alloc)
+						(mp, name, n, socket_id, flags);
+	}
+	return NULL;
+}
+
+inline int __attribute__((always_inline))
+rte_mempool_ext_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
+{
+	return (mempool_handler_list.handler[mp->handler_idx].get)
+						(mp->rt_pool, obj_table, n);
+}
+
+inline int __attribute__((always_inline))
+rte_mempool_ext_put_bulk(struct rte_mempool *mp, void * const *obj_table,
+		unsigned n)
+{
+	return (mempool_handler_list.handler[mp->handler_idx].put)
+						(mp->rt_pool, obj_table, n);
+}
+
+int
+rte_mempool_ext_get_count(const struct rte_mempool *mp)
+{
+	return (mempool_handler_list.handler[mp->handler_idx].get_count)
+						(mp->rt_pool);
+}
+
+static struct rte_mempool_handler handler_mp_mc = {
+	.name = "ring_mp_mc",
+	.alloc = rte_mempool_common_ring_alloc,
+	.put = common_ring_mp_put,
+	.get = common_ring_mc_get,
+	.get_count = common_ring_get_count,
+	.free = NULL
+};
+static struct rte_mempool_handler handler_sp_sc = {
+	.name = "ring_sp_sc",
+	.alloc = rte_mempool_common_ring_alloc,
+	.put = common_ring_sp_put,
+	.get = common_ring_sc_get,
+	.get_count = common_ring_get_count,
+	.free = NULL
+};
+static struct rte_mempool_handler handler_mp_sc = {
+	.name = "ring_mp_sc",
+	.alloc = rte_mempool_common_ring_alloc,
+	.put = common_ring_mp_put,
+	.get = common_ring_sc_get,
+	.get_count = common_ring_get_count,
+	.free = NULL
+};
+static struct rte_mempool_handler handler_sp_mc = {
+	.name = "ring_sp_mc",
+	.alloc = rte_mempool_common_ring_alloc,
+	.put = common_ring_sp_put,
+	.get = common_ring_mc_get,
+	.get_count = common_ring_get_count,
+	.free = NULL
+};
+
+REGISTER_MEMPOOL_HANDLER(handler_mp_mc);
+REGISTER_MEMPOOL_HANDLER(handler_sp_sc);
+REGISTER_MEMPOOL_HANDLER(handler_mp_sc);
+REGISTER_MEMPOOL_HANDLER(handler_sp_mc);
lib/librte_mempool/rte_mempool_internal.h new file +75 -0
new file mode 100644
index 0000000..982396f
@@ -0,0 +1,75 @@
+/*-
+ *   BSD LICENSE
+ *
+ *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _RTE_MEMPOOL_INTERNAL_H_
+#define _RTE_MEMPOOL_INTERNAL_H_
+
+#include <rte_spinlock.h>
+#include <rte_mempool.h>
+
+#define RTE_MEMPOOL_MAX_HANDLER_IDX 16
+
+struct rte_mempool_handler {
+	char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool handler */
+
+	rte_mempool_alloc_t alloc;
+
+	rte_mempool_get_count get_count;
+
+	rte_mempool_free_t free;
+
+	rte_mempool_put_t put;
+
+	rte_mempool_get_t get;
+} __rte_cache_aligned;
+
+struct rte_mempool_handler_list {
+	rte_spinlock_t sl;		  /**< Spinlock for add/delete. */
+
+	int32_t num_handlers;	  /**< Number of handlers that are valid. */
+
+	/* storage for all possible handlers */
+	struct rte_mempool_handler handler[RTE_MEMPOOL_MAX_HANDLER_IDX];
+};
+
+int16_t rte_mempool_register_handler(struct rte_mempool_handler *h);
+int16_t rte_get_mempool_handler_idx(const char *name);
+
+#define REGISTER_MEMPOOL_HANDLER(h) \
+static int16_t __attribute__((used)) testfn_##h(void);\
+int16_t __attribute__((constructor, used)) testfn_##h(void)\
+{\
+	return rte_mempool_register_handler(&h);\
+}
+
+#endif
lib/librte_mempool/rte_mempool_version.map +1 -0
index 17151e0..589db27 100644
@@ -6,6 +6,7 @@ DPDK_2.0 {
 	rte_mempool_calc_obj_size;
 	rte_mempool_count;
 	rte_mempool_create;
+	rte_mempool_create_ext;
 	rte_mempool_dump;
 	rte_mempool_list_dump;
 	rte_mempool_lookup;
Signature · 4 lines
   Jan Viktorin                  E-mail: [email protected]
   System Architect              Web:    www.RehiveTech.com
   RehiveTech
   Brno, Czech Republic
#26 lore raw

Re: [PATCH 1/6] mempool: add external mempool manager support

In reply to David Hunt

Hi David,

Quoted 10 lines · David Hunt

Adds the new rte_mempool_create_ext api and callback mechanism for external mempool handlers

Modifies the existing rte_mempool_create to set up the handler_idx to the relevant mempool handler based on the handler name:
ring_sp_sc
ring_mp_mc
ring_sp_mc
ring_mp_sc

Quoted 5 lines

v2: merges the duplicated code in rte_mempool_xmem_create and rte_mempool_create_ext into one common function. The old functions now call the new common function with the relevant parameters.

I think the refactoring of rte_mempool_create() (adding of mempool_create()) should go in another commit. It will make the patches much easier to read.

Also, I'm sorry but it seems that several comments or question I've made in http://dpdk.org/ml/archives/dev/2016-February/032706.html are not addressed.

Examples:
- putting some part of the patch in separate commits
- meaning of "rt_pool"
- put_pool_bulk unclear comment
- should we also have get_pool_bulk stats?
- missing _MEMPOOL_STAT_ADD() in mempool_bulk()
- why internal in rte_mempool_internal.h?
- why default in rte_mempool_default.c?
- remaining references to stack handler (in a comment)
- ...?

As you know, doing a proper code review takes a lot of time. If I have to re-check all of my previous comments, it will take even more. I'm not saying all my comments require a code change, but in case you don't agree, please at least explain your opinion so we can debate on the list.

Regards,
Olivier

#27 lore raw

Re: [PATCH 1/6] mempool: add external mempool manager support

Quoted 37 lines · Olivier MATZ

Hi David,

Quoted 10 lines · David Hunt

Adds the new rte_mempool_create_ext api and callback mechanism for external mempool handlers

Modifies the existing rte_mempool_create to set up the handler_idx to the relevant mempool handler based on the handler name:
ring_sp_sc
ring_mp_mc
ring_sp_mc
ring_mp_sc

Quoted 5 lines

v2: merges the duplicated code in rte_mempool_xmem_create and rte_mempool_create_ext into one common function. The old functions now call the new common function with the relevant parameters.

I think the refactoring of rte_mempool_create() (adding of mempool_create()) should go in another commit. It will make the patches much easier to read.

Also, I'm sorry but it seems that several comments or question I've made in http://dpdk.org/ml/archives/dev/2016-February/032706.html are not addressed.

Examples:
- putting some part of the patch in separate commits
- meaning of "rt_pool"
- put_pool_bulk unclear comment
- should we also have get_pool_bulk stats?
- missing _MEMPOOL_STAT_ADD() in mempool_bulk()
- why internal in rte_mempool_internal.h?
- why default in rte_mempool_default.c?
- remaining references to stack handler (in a comment)
- ...?

Quoted 6 lines

As you know, doing a proper code review takes a lot of time. If I have to re-check all of my previous comments, it will take even more. I'm not saying all my comments require a code change, but in case you don't agree, please at least explain your opinion so we can debate on the list.

Hi Olivier,
Sincerest apologies. I had intended in coming back around to your
original comments after refactoring the code. I will do that now. I did take them into consideration, but I see now that I need to do further work, such as a clearer name for rt_pool, etc. I will respond to your original email. Thanks
David.

#28 lore raw

Re: [PATCH 1/6] mempool: add external mempool manager support

Hi David,

Quoted 21 lines · David
Quoted 15 lines

Also, I'm sorry but it seems that several comments or question I've made in http://dpdk.org/ml/archives/dev/2016-February/032706.html are not addressed.

Examples:
- putting some part of the patch in separate commits
- meaning of "rt_pool"
- put_pool_bulk unclear comment
- should we also have get_pool_bulk stats?
- missing _MEMPOOL_STAT_ADD() in mempool_bulk()
- why internal in rte_mempool_internal.h?
- why default in rte_mempool_default.c?
- remaining references to stack handler (in a comment)
- ...?

Quoted 6 lines

As you know, doing a proper code review takes a lot of time. If I have to re-check all of my previous comments, it will take even more. I'm not saying all my comments require a code change, but in case you don't agree, please at least explain your opinion so we can debate on the list.

Quoted 6 lines

Hi Olivier,
Sincerest apologies. I had intended in coming back around to your
original comments after refactoring the code. I will do that now. I did take them into consideration, but I see now that I need to do further work, such as a clearer name for rt_pool, etc. I will respond to your original email.

I thought some comments were ignored :)
So no problem in that case, thanks for clarifying.

Olivier

author
#29 lore raw

PATCH 2/6 mempool: add stack (lifo) based external mempool handler

In reply to David Hunt

adds a simple stack based mempool handler

---
 lib/librte_mempool/Makefile            |   2 +-
 lib/librte_mempool/rte_mempool.c       |   4 +-
 lib/librte_mempool/rte_mempool.h       |   1 +
 lib/librte_mempool/rte_mempool_stack.c | 164 +++++++++++++++++++++++++++++++++
 4 files changed, 169 insertions(+), 2 deletions(-)
 create mode 100644 lib/librte_mempool/rte_mempool_stack.c
lib/librte_mempool/Makefile +1 -1
index aeaffd1..d795b48 100644
@@ -43,7 +43,7 @@ LIBABIVER := 1
 # all source are stored in SRCS-y
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool.c
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_default.c
-
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_stack.c
 ifeq ($(CONFIG_RTE_LIBRTE_XEN_DOM0),y)
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_dom0_mempool.c
 endif
lib/librte_mempool/rte_mempool.c +3 -1
index a577a3e..44bc92f 100644
@@ -672,7 +672,9 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
 	 * examine the
 	 * flags to set the correct index into the handler table.
 	 */
-	if (flags & (MEMPOOL_F_SP_PUT | MEMPOOL_F_SC_GET))
+	if (flags & MEMPOOL_F_USE_STACK)
+		sprintf(handler_name, "%s", "stack");
+	else if (flags & (MEMPOOL_F_SP_PUT | MEMPOOL_F_SC_GET))
 		sprintf(handler_name, "%s", "ring_sp_sc");
 	else if (flags & MEMPOOL_F_SP_PUT)
 		sprintf(handler_name, "%s", "ring_sp_mc");
lib/librte_mempool/rte_mempool.h +1 -0
index 3705fbd..8d8201f 100644
@@ -303,6 +303,7 @@ struct rte_mempool {
 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
 #define MEMPOOL_F_SP_PUT         0x0004 /**< Default put is "single-producer".*/
 #define MEMPOOL_F_SC_GET         0x0008 /**< Default get is "single-consumer".*/
+#define MEMPOOL_F_USE_STACK      0x0010 /**< Use a stack for the common pool. */
 #define MEMPOOL_F_INT_HANDLER    0x0020 /**< Using internal mempool handler */
 
 
lib/librte_mempool/rte_mempool_stack.c new file +164 -0

164 lines not on this page · raw message

Signature · 1 line
2.5.0
#30 lore raw

Re: [PATCH 2/6] mempool: add stack (lifo) based external mempool handler

Hi David,

Quoted 11 lines · David Hunt

adds a simple stack based mempool handler

---
 lib/librte_mempool/Makefile            |   2 +-
 lib/librte_mempool/rte_mempool.c       |   4 +-
 lib/librte_mempool/rte_mempool.h       |   1 +
 lib/librte_mempool/rte_mempool_stack.c | 164 +++++++++++++++++++++++++++++++++
 4 files changed, 169 insertions(+), 2 deletions(-)
 create mode 100644 lib/librte_mempool/rte_mempool_stack.c

I don't get what is the purpose of this handler. Is it an example or is it something that could be useful for dpdk applications?

If it's an example, we should find a way to put the code outside the librte_mempool library, maybe in the test program. I see there is also a "custom handler". Do we really need to have both?

Regards,
Olivier

#31 lore raw

Re: [PATCH 2/6] mempool: add stack (lifo) based external mempool handler

Quoted 14 lines · Olivier MATZ

Hi David,

Quoted 11 lines · David Hunt

adds a simple stack based mempool handler

---
  lib/librte_mempool/Makefile            |   2 +-
  lib/librte_mempool/rte_mempool.c       |   4 +-
  lib/librte_mempool/rte_mempool.h       |   1 +
  lib/librte_mempool/rte_mempool_stack.c | 164 +++++++++++++++++++++++++++++++++
  4 files changed, 169 insertions(+), 2 deletions(-)

create mode 100644 lib/librte_mempool/rte_mempool_stack.c

Quoted 6 lines

I don't get what is the purpose of this handler. Is it an example or is it something that could be useful for dpdk applications?

If it's an example, we should find a way to put the code outside the librte_mempool library, maybe in the test program. I see there is also a "custom handler". Do we really need to have both?

They are both example handlers. I agree that we could reduce down to one, and since the 'custom' handler has autotests, I would suggest we keep that one.

The next question is where it should live. I agree that it's not ideal to have example code living in the same directory as the mempool library, but they are an integral part of the library itself. How about creating a handlers sub-directory? We could then keep all additional and sample handlers in there, away from the built-in handlers. Also, seeing as the handler code is intended to be part of the library, I think moving it out to the examples directory may confuse matters further.

Regards,
David.

#32 lore raw

Re: [PATCH 2/6] mempool: add stack (lifo) based external mempool handler

Hi David,

Quoted 20 lines · David
Quoted 18 lines · Olivier MATZ

Hi David,

Quoted 12 lines · David Hunt

adds a simple stack based mempool handler

---
  lib/librte_mempool/Makefile            |   2 +-
  lib/librte_mempool/rte_mempool.c       |   4 +-
  lib/librte_mempool/rte_mempool.h       |   1 +
  lib/librte_mempool/rte_mempool_stack.c | 164
+++++++++++++++++++++++++++++++++
  4 files changed, 169 insertions(+), 2 deletions(-)

create mode 100644 lib/librte_mempool/rte_mempool_stack.c

I don't get what is the purpose of this handler. Is it an example or is it something that could be useful for dpdk applications?

Quoted 6 lines
Quoted 3 lines

If it's an example, we should find a way to put the code outside the librte_mempool library, maybe in the test program. I see there is also a "custom handler". Do we really need to have both?

They are both example handlers. I agree that we could reduce down to one, and since the 'custom' handler has autotests, I would suggest we keep that one.

ok

Quoted 7 lines

The next question is where it should live. I agree that it's not ideal to have example code living in the same directory as the mempool library, but they are an integral part of the library itself. How about creating a handlers sub-directory? We could then keep all additional and sample handlers in there, away from the built-in handlers. Also, seeing as the handler code is intended to be part of the library, I think moving it out to the examples directory may confuse matters further.

I really don't think example code should go in the library. Either it should go in dpdk/examples/ or in dpdk/app/test*.

Quoted 1 line

From your initial description: "The External Mempool Manager is an

extension to the mempool API that allows users to add and use an external mempool manager, which allows external memory subsystems such as external hardware memory management systems and software based memory allocators to be used with DPDK."

Can we find a hardware where the external mempool manager is required? This would be the best example ever I think.

Regards,
Olivier

#33 lore raw

Re: [PATCH 2/6] mempool: add stack (lifo) based external mempool handler

In reply to Olivier MATZ
Quoted 23 lines

-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Olivier MATZ
Sent: Friday, February 19, 2016 5:31 AM
To: Hunt, David <[email protected]>; [email protected]
Subject: Re: [dpdk-dev] [PATCH 2/6] mempool: add stack (lifo) based external mempool handler

Hi David,

Quoted 12 lines · David Hunt

adds a simple stack based mempool handler

---
 lib/librte_mempool/Makefile            |   2 +-
 lib/librte_mempool/rte_mempool.c       |   4 +-
 lib/librte_mempool/rte_mempool.h       |   1 +
 lib/librte_mempool/rte_mempool_stack.c | 164
+++++++++++++++++++++++++++++++++
 4 files changed, 169 insertions(+), 2 deletions(-)  create mode

100644 lib/librte_mempool/rte_mempool_stack.c

Quoted 3 lines

I don't get what is the purpose of this handler. Is it an example or is it something that could be useful for dpdk applications?

This is actually something that is useful for pipelining apps, where the mempool cache doesn't really work - example, where we have one core doing rx (and alloc), and another core doing Tx (and return). In such a case, the mempool ring simply cycles through all the mbufs, resulting in a LLC miss on every mbuf allocated when the number of mbufs is large. A stack recycles buffers more effectively in this case.

Quoted 8 lines

If it's an example, we should find a way to put the code outside the librte_mempool library, maybe in the test program. I see there is also a "custom handler". Do we really need to have both?

Regards,
Olivier

#34 lore raw

Re: [PATCH 2/6] mempool: add stack (lifo) based external mempool handler

Hi,

Quoted 19 lines
Quoted 16 lines

Hi David,

Quoted 12 lines · David Hunt

adds a simple stack based mempool handler

---
 lib/librte_mempool/Makefile            |   2 +-
 lib/librte_mempool/rte_mempool.c       |   4 +-
 lib/librte_mempool/rte_mempool.h       |   1 +
 lib/librte_mempool/rte_mempool_stack.c | 164
+++++++++++++++++++++++++++++++++
 4 files changed, 169 insertions(+), 2 deletions(-)  create mode

100644 lib/librte_mempool/rte_mempool_stack.c

Quoted 3 lines

I don't get what is the purpose of this handler. Is it an example or is it something that could be useful for dpdk applications?

Quoted 8 lines

This is actually something that is useful for pipelining apps, where the mempool cache doesn't really work - example, where we have one core doing rx (and alloc), and another core doing Tx (and return). In such a case, the mempool ring simply cycles through all the mbufs, resulting in a LLC miss on every mbuf allocated when the number of mbufs is large. A stack recycles buffers more effectively in this case.

While I agree on the principle, if this is the case the commit should come with an explanation about when this handler should be used, a small test report showing the performance numbers and probably an example app.

Also, I think there is a some room for optimizations, especially I don't think that the spinlock will scale with many cores.

Regards,
Olivier

author
#35 lore raw

PATCH 3/6 mempool: adds a simple ring-based mempool handler using mallocs for objects

In reply to David Hunt
---
 lib/librte_mempool/Makefile         |   1 +
 lib/librte_mempool/custom_mempool.c | 146 ++++++++++++++++++++++++++++++++++++
 2 files changed, 147 insertions(+)
 create mode 100644 lib/librte_mempool/custom_mempool.c
lib/librte_mempool/Makefile +1 -0
index d795b48..4f72546 100644
@@ -44,6 +44,7 @@ LIBABIVER := 1
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool.c
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_default.c
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_stack.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  custom_mempool.c
 ifeq ($(CONFIG_RTE_LIBRTE_XEN_DOM0),y)
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_dom0_mempool.c
 endif
lib/librte_mempool/custom_mempool.c new file +146 -0

146 lines not on this page · raw message

Signature · 1 line
2.5.0
author
#36 lore raw

PATCH 4/6 mempool: add autotest for external mempool custom example

In reply to David Hunt
---
 app/test/Makefile           |   1 +
 app/test/test_ext_mempool.c | 451 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 452 insertions(+)
 create mode 100644 app/test/test_ext_mempool.c
app/test/Makefile +1 -0
index ec33e1a..9a2f75f 100644
@@ -74,6 +74,7 @@ SRCS-$(CONFIG_RTE_LIBRTE_TIMER) += test_timer_perf.c
 SRCS-$(CONFIG_RTE_LIBRTE_TIMER) += test_timer_racecond.c
 
 SRCS-y += test_mempool.c
+SRCS-y += test_ext_mempool.c
 SRCS-y += test_mempool_perf.c
 
 SRCS-y += test_mbuf.c
app/test/test_ext_mempool.c new file +451 -0

451 lines not on this page · raw message

Signature · 1 line
2.5.0
author
#37 lore raw

PATCH 5/6 mempool: allow rte_pktmbuf_pool_create switch between memool handlers

In reply to David Hunt

v2 changes: added to linux and bsd config files:
If the user wants to have rte_pktmbuf_pool_create() use an external mempool handler, they define RTE_MEMPOOL_HANDLER_NAME to be the name of the mempool handler they wish to use, and change RTE_MEMPOOL_HANDLER_EXT to 'y' Applies to both linux and bsd config files

---
 config/common_bsdapp       | 2 ++
 config/common_linuxapp     | 2 ++
 lib/librte_mbuf/rte_mbuf.c | 8 ++++++++
 3 files changed, 12 insertions(+)
config/common_bsdapp +2 -0
index 696382c..e0c812a 100644
@@ -347,6 +347,8 @@ CONFIG_RTE_RING_PAUSE_REP_COUNT=0
 CONFIG_RTE_LIBRTE_MEMPOOL=y
 CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE=512
 CONFIG_RTE_LIBRTE_MEMPOOL_DEBUG=n
+CONFIG_RTE_MEMPOOL_HANDLER_EXT=n
+CONFIG_RTE_MEMPOOL_HANDLER_NAME="custom_handler"
 
 #
 # Compile librte_mbuf
config/common_linuxapp +2 -0
index f1638db..9aa62ca 100644
@@ -363,6 +363,8 @@ CONFIG_RTE_RING_PAUSE_REP_COUNT=0
 CONFIG_RTE_LIBRTE_MEMPOOL=y
 CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE=512
 CONFIG_RTE_LIBRTE_MEMPOOL_DEBUG=n
+CONFIG_RTE_MEMPOOL_HANDLER_EXT=n
+CONFIG_RTE_MEMPOOL_HANDLER_NAME="custom_handler"
 
 #
 # Compile librte_mbuf
lib/librte_mbuf/rte_mbuf.c +8 -0
index c18b438..42b0cd1 100644
@@ -167,10 +167,18 @@ rte_pktmbuf_pool_create(const char *name, unsigned n,
 	mbp_priv.mbuf_data_room_size = data_room_size;
 	mbp_priv.mbuf_priv_size = priv_size;
 
+#ifdef RTE_MEMPOOL_HANDLER_EXT
+	return rte_mempool_create_ext(name, n, elt_size,
+		cache_size, sizeof(struct rte_pktmbuf_pool_private),
+		rte_pktmbuf_pool_init, &mbp_priv, rte_pktmbuf_init, NULL,
+		socket_id, 0,
+		RTE_MEMPOOL_HANDLER_NAME);
+#else
 	return rte_mempool_create(name, n, elt_size,
 		cache_size, sizeof(struct rte_pktmbuf_pool_private),
 		rte_pktmbuf_pool_init, &mbp_priv, rte_pktmbuf_init, NULL,
 		socket_id, 0);
+#endif
 }
 
 /* do some sanity checks on a mbuf: panic if it fails */
Signature · 1 line
2.5.0
author
#38 lore raw

PATCH 6/6 mempool: add in the RTE_NEXT_ABI protection for ABI breakages

In reply to David Hunt

v2: Kept all the NEXT_ABI defs to this patch so as to make the previous patches easier to read, and also to imake it clear what code is necessary to keep ABI compatibility when NEXT_ABI is disabled.

---
 app/test/Makefile                |   2 +
 app/test/test_mempool_perf.c     |   3 +
 lib/librte_mbuf/rte_mbuf.c       |   7 ++
 lib/librte_mempool/Makefile      |   2 +
 lib/librte_mempool/rte_mempool.c | 240 ++++++++++++++++++++++++++++++++++++++-
 lib/librte_mempool/rte_mempool.h |  68 ++++++++++-
 6 files changed, 320 insertions(+), 2 deletions(-)
app/test/Makefile +2 -0
index 9a2f75f..8fcf0c2 100644
@@ -74,7 +74,9 @@ SRCS-$(CONFIG_RTE_LIBRTE_TIMER) += test_timer_perf.c
 SRCS-$(CONFIG_RTE_LIBRTE_TIMER) += test_timer_racecond.c
 
 SRCS-y += test_mempool.c
+ifeq ($(CONFIG_RTE_NEXT_ABI),y)
 SRCS-y += test_ext_mempool.c
+endif
 SRCS-y += test_mempool_perf.c
 
 SRCS-y += test_mbuf.c
app/test/test_mempool_perf.c +3 -0
index 091c1df..ca69e49 100644
@@ -161,6 +161,9 @@ per_lcore_mempool_test(__attribute__((unused)) void *arg)
 							   n_get_bulk);
 				if (unlikely(ret < 0)) {
 					rte_mempool_dump(stdout, mp);
+#ifndef RTE_NEXT_ABI
+					rte_ring_dump(stdout, mp->ring);
+#endif
 					/* in this case, objects are lost... */
 					return -1;
 				}
lib/librte_mbuf/rte_mbuf.c +7 -0
index 42b0cd1..967d987 100644
@@ -167,6 +167,7 @@ rte_pktmbuf_pool_create(const char *name, unsigned n,
 	mbp_priv.mbuf_data_room_size = data_room_size;
 	mbp_priv.mbuf_priv_size = priv_size;
 
+#ifdef RTE_NEXT_ABI
 #ifdef RTE_MEMPOOL_HANDLER_EXT
 	return rte_mempool_create_ext(name, n, elt_size,
 		cache_size, sizeof(struct rte_pktmbuf_pool_private),
@@ -179,6 +180,12 @@ rte_pktmbuf_pool_create(const char *name, unsigned n,
 		rte_pktmbuf_pool_init, &mbp_priv, rte_pktmbuf_init, NULL,
 		socket_id, 0);
 #endif
+#else
+	return rte_mempool_create(name, n, elt_size,
+		cache_size, sizeof(struct rte_pktmbuf_pool_private),
+		rte_pktmbuf_pool_init, &mbp_priv, rte_pktmbuf_init, NULL,
+		socket_id, 0);
+#endif
 }
 
 /* do some sanity checks on a mbuf: panic if it fails */
lib/librte_mempool/Makefile +2 -0
index 4f72546..8038785 100644
@@ -42,9 +42,11 @@ LIBABIVER := 1
 
 # all source are stored in SRCS-y
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool.c
+ifeq ($(CONFIG_RTE_NEXT_ABI),y)
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_default.c
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_stack.c
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  custom_mempool.c
+endif
 ifeq ($(CONFIG_RTE_LIBRTE_XEN_DOM0),y)
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_dom0_mempool.c
 endif
lib/librte_mempool/rte_mempool.c +239 -1

288 lines not on this page · raw message

lib/librte_mempool/rte_mempool.h +67 -1

187 lines not on this page · raw message

Signature · 1 line
2.5.0
#39 lore raw

Re: [PATCH 6/6] mempool: add in the RTE_NEXT_ABI protection for ABI breakages

Hi David,

Quoted 6 lines · David Hunt

v2: Kept all the NEXT_ABI defs to this patch so as to make the previous patches easier to read, and also to imake it clear what code is necessary to keep ABI compatibility when NEXT_ABI is disabled.

Quoted 8 lines
---
 app/test/Makefile                |   2 +
 app/test/test_mempool_perf.c     |   3 +
 lib/librte_mbuf/rte_mbuf.c       |   7 ++
 lib/librte_mempool/Makefile      |   2 +
 lib/librte_mempool/rte_mempool.c | 240 ++++++++++++++++++++++++++++++++++++++-
 lib/librte_mempool/rte_mempool.h |  68 ++++++++++-
 6 files changed, 320 insertions(+), 2 deletions(-)

Given the size of this patch, I don't think it's worth adding the NEXT ABI in that case.

Regards,
Olivier

#40 lore raw

Re: [PATCH 0/6] external mempool manager

In reply to David Hunt

Hi,

Quoted 3 lines · David Hunt

Hi list.

Here's the v2 version of a proposed patch for an external mempool manager

Just to notice the "v2" is missing in the title, it would help to have it for next versions of the series.

Regards,
Olivier

#41 lore raw

Re: [PATCH 0/6] external mempool manager

Quoted 9 lines · Olivier MATZ

Hi,

Quoted 3 lines · David Hunt

Hi list.

Here's the v2 version of a proposed patch for an external mempool manager

Just to notice the "v2" is missing in the title, it would help to have it for next versions of the series.

Thanks, Olivier, I will ensure it's in the next patchset. Regards,
David.

author
#42 lore raw

v3 cover external mempool manager

In reply to David Hunt

Hi list.

Here's the v3 version patch for an external mempool manager

v3 changes:
* simplified the file layout, renamed to rte_mempool_handler.[hc]
* moved the default handlers into rte_mempool_default.c
* moved the example handler out into app/test/test_ext_mempool.c
* removed is_mc/is_mp change, slight perf degredation on sp cached operation
* removed stack hanler, may re-introduce at a later date
* Changes out of code reviews

v2 changes:
* There was a lot of duplicate code between rte_mempool_xmem_create and rte_mempool_create_ext. This has now been refactored and is now hopefully cleaner.
* The RTE_NEXT_ABI define is now used to allow building of the library in a format that is compatible with binaries built against previous versions of DPDK.
* Changes out of code reviews. Hopefully I've got most of them included.

The External Mempool Manager is an extension to the mempool API that allows users to add and use an external mempool manager, which allows external memory subsystems such as external hardware memory management systems and software based memory allocators to be used with DPDK.

The existing API to the internal DPDK mempool manager will remain unchanged and will be backward compatible. However, there will be an ABI breakage, as the mempool struct is changing. These changes are all contained withing RTE_NEXT_ABI defs, and the current or next code can be changed with the CONFIG_RTE_NEXT_ABI config setting

There are two aspects to external mempool manager.
1. Adding the code for your new mempool handler. This is achieved by adding a new mempool handler source file into the librte_mempool library, and using the REGISTER_MEMPOOL_HANDLER macro.
2. Using the new API to call rte_mempool_create_ext to create a new mempool using the name parameter to identify which handler to use.

New API calls added
1. A new mempool 'create' function which accepts mempool handler name.
2. A new mempool 'rte_get_mempool_handler' function which accepts mempool handler name, and returns the index to the relevant set of callbacks for that mempool handler

Several external mempool managers may be used in the same application. A new mempool can then be created by using the new 'create' function, providing the mempool handler name to point the mempool to the relevant mempool manager callback structure.

The old 'create' function can still be called by legacy programs, and will internally work out the mempool handle based on the flags provided (single producer, single consumer, etc). By default handles are created internally to implement the built-in DPDK mempool manager and mempool types.

The external mempool manager needs to provide the following functions.
1. alloc - allocates the mempool memory, and adds each object onto a ring
2. put - puts an object back into the mempool once an application has
finished with it
3. get - gets an object from the mempool for use by the application
4. get_count - gets the number of available objects in the mempool
5. free - frees the mempool memory

Every time a get/put/get_count is called from the application/PMD, the callback for that mempool is called. These functions are in the fastpath, and any unoptimised handlers may limit performance.

The new APIs are as follows:

1. rte_mempool_create_ext

struct rte_mempool * rte_mempool_create_ext(const char * name, unsigned n,
unsigned cache_size, unsigned private_data_size, int socket_id, unsigned flags,
const char * handler_name);

2. rte_mempool_get_handler_name

char *
rte_mempool_get_handler_name(struct rte_mempool *mp);

Please see rte_mempool.h for further information on the parameters.

The important thing to note is that the mempool handler is passed by name to rte_mempool_create_ext, and that in turn calls rte_get_mempool_handler to get the handler index, which is stored in the rte_memool structure. This allow multiple processes to use the same mempool, as the function pointers are accessed via handler index.

The mempool handler structure contains callbacks to the implementation of the handler, and is set up for registration as follows:

static struct rte_mempool_handler handler_sp_mc = {
.name = "ring_sp_mc",
.alloc = rte_mempool_common_ring_alloc, .put = common_ring_sp_put,
.get = common_ring_mc_get,
.get_count = common_ring_get_count, .free = common_ring_free,
};

And then the following macro will register the handler in the array of handlers

REGISTER_MEMPOOL_HANDLER(handler_mp_mc);

For and example of a simple malloc based mempool manager, see lib/librte_mempool/custom_mempool.c

For an example of API usage, please see app/test/test_ext_mempool.c, which implements a rudimentary mempool manager using simple mallocs for each mempool object. This file also contains the callbacks and self registration for the new handler.

David Hunt (4):
mempool: add external mempool manager support
mempool: add custom mempool handler example
mempool: allow rte_pktmbuf_pool_create switch between memool handlers
mempool: add in the RTE_NEXT_ABI for ABI breakages

 app/test/Makefile                          |   3 +
 app/test/test_ext_mempool.c                | 451 +++++++++++++++++++++++++++++
 app/test/test_mempool_perf.c               |   2 +
 config/common_base                         |   2 +
 lib/librte_mbuf/rte_mbuf.c                 |  15 +
 lib/librte_mempool/Makefile                |   5 +
 lib/librte_mempool/rte_mempool.c           | 389 +++++++++++++++++++++++--
 lib/librte_mempool/rte_mempool.h           | 224 +++++++++++++-
 lib/librte_mempool/rte_mempool_default.c   | 136 +++++++++
 lib/librte_mempool/rte_mempool_handler.c   | 140 +++++++++
 lib/librte_mempool/rte_mempool_handler.h   |  75 +++++
 lib/librte_mempool/rte_mempool_version.map |   1 +
 12 files changed, 1415 insertions(+), 28 deletions(-)
 create mode 100644 app/test/test_ext_mempool.c
 create mode 100644 lib/librte_mempool/rte_mempool_default.c
 create mode 100644 lib/librte_mempool/rte_mempool_handler.c
 create mode 100644 lib/librte_mempool/rte_mempool_handler.h
Signature · 1 line
2.5.0
author
#43 lore raw

PATCH v3 1/4 mempool: add external mempool manager support

Adds the new rte_mempool_create_ext api and callback mechanism for external mempool handlers

Modifies the existing rte_mempool_create to set up the handler_idx to the relevant mempool handler based on the handler name:
ring_sp_sc
ring_mp_mc
ring_sp_mc
ring_mp_sc

v3: Cleanup out of list review. Indents, comments, etc. rebase on top of latest head, merged in change - fix leak when creation fails: 86f36ff9578b5f3d697c8fcf6072dcb70e2b246f split rte_mempool_default.c into itself and rte_mempool_handler.c renamed rte_mempool_internal.h to rte_mempool_handler.h

v2: merges the duplicated code in rte_mempool_xmem_create and rte_mempool_create_ext into one common function. The old functions now call the new common function with the relevant parameters.

---
 app/test/test_mempool_perf.c               |   1 -
 lib/librte_mempool/Makefile                |   3 +
 lib/librte_mempool/rte_mempool.c           | 358 ++++++++++++++++++-----------
 lib/librte_mempool/rte_mempool.h           | 213 ++++++++++++++---
 lib/librte_mempool/rte_mempool_default.c   | 136 +++++++++++
 lib/librte_mempool/rte_mempool_handler.c   | 140 +++++++++++
 lib/librte_mempool/rte_mempool_handler.h   |  75 ++++++
 lib/librte_mempool/rte_mempool_version.map |   1 +
 8 files changed, 770 insertions(+), 157 deletions(-)
 create mode 100644 lib/librte_mempool/rte_mempool_default.c
 create mode 100644 lib/librte_mempool/rte_mempool_handler.c
 create mode 100644 lib/librte_mempool/rte_mempool_handler.h
app/test/test_mempool_perf.c +0 -1
index cdc02a0..091c1df 100644
@@ -161,7 +161,6 @@ per_lcore_mempool_test(__attribute__((unused)) void *arg)
 							   n_get_bulk);
 				if (unlikely(ret < 0)) {
 					rte_mempool_dump(stdout, mp);
-					rte_ring_dump(stdout, mp->ring);
 					/* in this case, objects are lost... */
 					return -1;
 				}
lib/librte_mempool/Makefile +3 -0
index a6898ef..a32c89e 100644
@@ -42,6 +42,9 @@ LIBABIVER := 1
 
 # all source are stored in SRCS-y
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_handler.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_default.c
+
 ifeq ($(CONFIG_RTE_LIBRTE_XEN_DOM0),y)
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_dom0_mempool.c
 endif
lib/librte_mempool/rte_mempool.c +229 -129

481 lines not on this page · raw message

lib/librte_mempool/rte_mempool.h +186 -27

286 lines not on this page · raw message

lib/librte_mempool/rte_mempool_default.c new file +136 -0

136 lines not on this page · raw message

lib/librte_mempool/rte_mempool_handler.c new file +140 -0

140 lines not on this page · raw message

lib/librte_mempool/rte_mempool_handler.h new file +75 -0

75 lines not on this page · raw message

lib/librte_mempool/rte_mempool_version.map +1 -0
index 17151e0..589db27 100644
@@ -6,6 +6,7 @@ DPDK_2.0 {
 	rte_mempool_calc_obj_size;
 	rte_mempool_count;
 	rte_mempool_create;
+	rte_mempool_create_ext;
 	rte_mempool_dump;
 	rte_mempool_list_dump;
 	rte_mempool_lookup;
Signature · 1 line
2.5.0
#44 lore raw

Re: [PATCH v3 1/4] mempool: add external mempool manager support

Hi David,

lib/librte_mempool/rte_mempool.c line 66

Quoted 2 lines · David Hunt
lib/librte_mempool/rte_mempool.c +1 -1
-static struct rte_tailq_elem rte_mempool_tailq = {
+struct rte_tailq_elem rte_mempool_tailq = {

Why removing static? I didn't see it's referenced somewhere else.

Review

Quoted 1 line
(quoted diff) +1 -0
+	if (flags && MEMPOOL_F_INT_HANDLER) {

I would assume it's "flags & MEMPOOL_F_INT_HANDLER". BTW, you might want to do a thorough check, as I found few more typos like this.

	--yliu
Quoted 18 lines
lib/librte_mempool/rte_mempool.c +15 -1
+	if (flags && MEMPOOL_F_INT_HANDLER) {
+
+		if (rte_eal_has_hugepages()) {
+			startaddr = (void *)mz->addr;
+		} else {
+			/* align memory pool start address on a page boundary */
+			unsigned long addr = (unsigned long)mz->addr;
+
+			if (addr & (page_size - 1)) {
+				addr += page_size;
+				addr &= ~(page_size - 1);
+			}
+			startaddr = (void *)addr;
 		}
-		startaddr = (void*)addr;
+	} else {
+		startaddr = (void *)mz->addr;
 	}
author
#45 lore raw

PATCH v3 2/4 mempool: add custom mempool handler example

In reply to David Hunt

Add a custom mempool handler as part of an autotest:
ext_mempool_autotest as defined in test_ext_mempool.c

v3: now contains the mempool handler within the test file along with it's get/put/get_count callbacks and self registration

---
 app/test/Makefile           |   1 +
 app/test/test_ext_mempool.c | 451 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 452 insertions(+)
 create mode 100644 app/test/test_ext_mempool.c
app/test/Makefile +1 -0
index ec33e1a..9a2f75f 100644
@@ -74,6 +74,7 @@ SRCS-$(CONFIG_RTE_LIBRTE_TIMER) += test_timer_perf.c
 SRCS-$(CONFIG_RTE_LIBRTE_TIMER) += test_timer_racecond.c
 
 SRCS-y += test_mempool.c
+SRCS-y += test_ext_mempool.c
 SRCS-y += test_mempool_perf.c
 
 SRCS-y += test_mbuf.c
app/test/test_ext_mempool.c new file +451 -0

451 lines not on this page · raw message

Signature · 1 line
2.5.0
author
#46 lore raw

PATCH v3 3/4 mempool: allow rte_pktmbuf_pool_create switch between memool handlers

In reply to David Hunt

If the user wants to have rte_pktmbuf_pool_create() use an external mempool handler, they define RTE_MEMPOOL_HANDLER_NAME to be the name of the mempool handler they wish to use, and change RTE_MEMPOOL_HANDLER_EXT to 'y'

---
 config/common_base         | 2 ++
 lib/librte_mbuf/rte_mbuf.c | 8 ++++++++
 2 files changed, 10 insertions(+)
config/common_base +2 -0
index 1af28c8..9d70cf4 100644
@@ -350,6 +350,8 @@ CONFIG_RTE_RING_PAUSE_REP_COUNT=0
 CONFIG_RTE_LIBRTE_MEMPOOL=y
 CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE=512
 CONFIG_RTE_LIBRTE_MEMPOOL_DEBUG=n
+CONFIG_RTE_MEMPOOL_HANDLER_EXT=n
+CONFIG_RTE_MEMPOOL_HANDLER_NAME="custom_handler"
 
 #
 # Compile librte_mbuf
lib/librte_mbuf/rte_mbuf.c +8 -0
index c18b438..42b0cd1 100644
@@ -167,10 +167,18 @@ rte_pktmbuf_pool_create(const char *name, unsigned n,
 	mbp_priv.mbuf_data_room_size = data_room_size;
 	mbp_priv.mbuf_priv_size = priv_size;
 
+#ifdef RTE_MEMPOOL_HANDLER_EXT
+	return rte_mempool_create_ext(name, n, elt_size,
+		cache_size, sizeof(struct rte_pktmbuf_pool_private),
+		rte_pktmbuf_pool_init, &mbp_priv, rte_pktmbuf_init, NULL,
+		socket_id, 0,
+		RTE_MEMPOOL_HANDLER_NAME);
+#else
 	return rte_mempool_create(name, n, elt_size,
 		cache_size, sizeof(struct rte_pktmbuf_pool_private),
 		rte_pktmbuf_pool_init, &mbp_priv, rte_pktmbuf_init, NULL,
 		socket_id, 0);
+#endif
 }
 
 /* do some sanity checks on a mbuf: panic if it fails */
Signature · 1 line
2.5.0
#47 lore raw

Re: [PATCH v3 3/4] mempool: allow rte_pktmbuf_pool_create switch between memool handlers

Quoted 37 lines · David Hunt

If the user wants to have rte_pktmbuf_pool_create() use an external mempool handler, they define RTE_MEMPOOL_HANDLER_NAME to be the name of the mempool handler they wish to use, and change RTE_MEMPOOL_HANDLER_EXT to 'y'

---
  config/common_base         | 2 ++
  lib/librte_mbuf/rte_mbuf.c | 8 ++++++++
  2 files changed, 10 insertions(+)
config/common_base +2 -0
index 1af28c8..9d70cf4 100644
@@ -350,6 +350,8 @@ CONFIG_RTE_RING_PAUSE_REP_COUNT=0
  CONFIG_RTE_LIBRTE_MEMPOOL=y
  CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE=512
  CONFIG_RTE_LIBRTE_MEMPOOL_DEBUG=n
+CONFIG_RTE_MEMPOOL_HANDLER_EXT=n
+CONFIG_RTE_MEMPOOL_HANDLER_NAME="custom_handler"
 
  #
  # Compile librte_mbuf
lib/librte_mbuf/rte_mbuf.c +5 -0
index c18b438..42b0cd1 100644
@@ -167,10 +167,18 @@ rte_pktmbuf_pool_create(const char *name, unsigned n,
  	mbp_priv.mbuf_data_room_size = data_room_size;
  	mbp_priv.mbuf_priv_size = priv_size;
 
+#ifdef RTE_MEMPOOL_HANDLER_EXT
+	return rte_mempool_create_ext(name, n, elt_size,
+		cache_size, sizeof(struct rte_pktmbuf_pool_private),
+		rte_pktmbuf_pool_init, &mbp_priv, rte_pktmbuf_init, NULL,
+		socket_id, 0,

lib/librte_mbuf/rte_mbuf.c rte_pktmbuf_pool_create(const char *name, unsigned n, · line 184

Quoted 10 lines
lib/librte_mbuf/rte_mbuf.c +3 -0
+		RTE_MEMPOOL_HANDLER_NAME);
+#else
  	return rte_mempool_create(name, n, elt_size,
  		cache_size, sizeof(struct rte_pktmbuf_pool_private),
  		rte_pktmbuf_pool_init, &mbp_priv, rte_pktmbuf_init, NULL,
  		socket_id, 0);
+#endif
  }
 
  /* do some sanity checks on a mbuf: panic if it fails */

This kind of thing really has to be run-time configurable, not a library build-time option.

- Panu -

#48 lore raw

Re: [PATCH v3 3/4] mempool: allow rte_pktmbuf_pool_create switch between memool handlers

Hi Panu,

Quoted 47 lines · Panu Matilainen
Quoted 46 lines · David Hunt

If the user wants to have rte_pktmbuf_pool_create() use an external mempool handler, they define RTE_MEMPOOL_HANDLER_NAME to be the name of the mempool handler they wish to use, and change RTE_MEMPOOL_HANDLER_EXT to 'y'

---
  config/common_base         | 2 ++
  lib/librte_mbuf/rte_mbuf.c | 8 ++++++++
  2 files changed, 10 insertions(+)
config/common_base +2 -0
index 1af28c8..9d70cf4 100644
@@ -350,6 +350,8 @@ CONFIG_RTE_RING_PAUSE_REP_COUNT=0
  CONFIG_RTE_LIBRTE_MEMPOOL=y
  CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE=512
  CONFIG_RTE_LIBRTE_MEMPOOL_DEBUG=n
+CONFIG_RTE_MEMPOOL_HANDLER_EXT=n
+CONFIG_RTE_MEMPOOL_HANDLER_NAME="custom_handler"
 
  #
  # Compile librte_mbuf
lib/librte_mbuf/rte_mbuf.c +8 -0
index c18b438..42b0cd1 100644
@@ -167,10 +167,18 @@ rte_pktmbuf_pool_create(const char *name, 
 unsigned n,
      mbp_priv.mbuf_data_room_size = data_room_size;
      mbp_priv.mbuf_priv_size = priv_size;
 
+#ifdef RTE_MEMPOOL_HANDLER_EXT
+    return rte_mempool_create_ext(name, n, elt_size,
+        cache_size, sizeof(struct rte_pktmbuf_pool_private),
+        rte_pktmbuf_pool_init, &mbp_priv, rte_pktmbuf_init, NULL,
+        socket_id, 0,
+        RTE_MEMPOOL_HANDLER_NAME);
+#else
      return rte_mempool_create(name, n, elt_size,
          cache_size, sizeof(struct rte_pktmbuf_pool_private),
          rte_pktmbuf_pool_init, &mbp_priv, rte_pktmbuf_init, NULL,
          socket_id, 0);
+#endif
  }
 
Quoted 6 lines
Quoted 2 lines

/* do some sanity checks on a mbuf: panic if it fails */

This kind of thing really has to be run-time configurable, not a library build-time option.

- Panu -

Interesting point. I was attempting to minimise the amount of application code changes. Would you prefer if I took out that change, and added a new rte_pktmbuf_pool_create_ext() function which tool an extra parameter as the mempool handler name to use?

/* helper to create a mbuf pool using external mempool handler */ struct rte_mempool *
rte_pktmbuf_pool_create_ext(const char *name, unsigned n,
unsigned cache_size, uint16_t priv_size, uint16_t data_room_size, int socket_id, const char *handler_name)

That way we could leave the old rte_pktmbuf_pool_create() exactly as it is, and any apps that wanted to use an external handler could call rte_pktmbuf_pool_create_ext()
I could do this easily enough for v4 (which I hope to get out later today)?

Thanks,
David.

#49 lore raw

Re: [PATCH v3 3/4] mempool: allow rte_pktmbuf_pool_create switch between memool handlers

Quoted 58 lines · David

Hi Panu,

Quoted 55 lines · Panu Matilainen
Quoted 48 lines · David Hunt

If the user wants to have rte_pktmbuf_pool_create() use an external mempool
handler, they define RTE_MEMPOOL_HANDLER_NAME to be the name of the mempool handler they wish to use, and change RTE_MEMPOOL_HANDLER_EXT to 'y'

---
  config/common_base         | 2 ++
  lib/librte_mbuf/rte_mbuf.c | 8 ++++++++
  2 files changed, 10 insertions(+)
config/common_base +2 -0
index 1af28c8..9d70cf4 100644
@@ -350,6 +350,8 @@ CONFIG_RTE_RING_PAUSE_REP_COUNT=0
  CONFIG_RTE_LIBRTE_MEMPOOL=y
  CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE=512
  CONFIG_RTE_LIBRTE_MEMPOOL_DEBUG=n
+CONFIG_RTE_MEMPOOL_HANDLER_EXT=n
+CONFIG_RTE_MEMPOOL_HANDLER_NAME="custom_handler"
 
  #
  # Compile librte_mbuf
lib/librte_mbuf/rte_mbuf.c +8 -0
index c18b438..42b0cd1 100644
@@ -167,10 +167,18 @@ rte_pktmbuf_pool_create(const char *name,
 unsigned n,
      mbp_priv.mbuf_data_room_size = data_room_size;
      mbp_priv.mbuf_priv_size = priv_size;
 
+#ifdef RTE_MEMPOOL_HANDLER_EXT
+    return rte_mempool_create_ext(name, n, elt_size,
+        cache_size, sizeof(struct rte_pktmbuf_pool_private),
+        rte_pktmbuf_pool_init, &mbp_priv, rte_pktmbuf_init, NULL,
+        socket_id, 0,
+        RTE_MEMPOOL_HANDLER_NAME);
+#else
      return rte_mempool_create(name, n, elt_size,
          cache_size, sizeof(struct rte_pktmbuf_pool_private),
          rte_pktmbuf_pool_init, &mbp_priv, rte_pktmbuf_init, NULL,
          socket_id, 0);
+#endif
  }
 
Quoted 2 lines

/* do some sanity checks on a mbuf: panic if it fails */

This kind of thing really has to be run-time configurable, not a library build-time option.

Quoted 4 lines
Quoted 1 line

- Panu -

Interesting point. I was attempting to minimise the amount of application code changes.

The problem with such build options is that the feature is for all practical purposes unusable in a distro setting where DPDK is just another shared library used by multiple applications.

Quoted 10 lines

Would you prefer if I took out that change, and added a new rte_pktmbuf_pool_create_ext() function which tool an extra parameter as the mempool handler name to use?

/* helper to create a mbuf pool using external mempool handler */ struct rte_mempool *
rte_pktmbuf_pool_create_ext(const char *name, unsigned n,
unsigned cache_size, uint16_t priv_size, uint16_t data_room_size, int socket_id, const char *handler_name)

Quoted 4 lines

That way we could leave the old rte_pktmbuf_pool_create() exactly as it is, and any apps that wanted to use an
external handler could call rte_pktmbuf_pool_create_ext()
I could do this easily enough for v4 (which I hope to get out later today)?

Yes, that's the way to do it. Thanks.

- Panu -

Quoted 7 lines

Thanks,
David.

author
#50 lore raw

PATCH v3 4/4 mempool: add in the RTE_NEXT_ABI for ABI breakages

In reply to David Hunt

This patch is for those people who want to be easily able to switch between the new mempool layout and the old. Change the value of RTE_NEXT_ABI in common_base config file

v3: Updated to take re-work of file layouts into consideration

v2: Kept all the NEXT_ABI defs to this patch so as to make the previous patches easier to read, and also to imake it clear what code is necessary to keep ABI compatibility when NEXT_ABI is disabled.

---
 app/test/Makefile                |   2 +
 app/test/test_mempool_perf.c     |   3 +
 lib/librte_mbuf/rte_mbuf.c       |   7 ++
 lib/librte_mempool/Makefile      |   2 +
 lib/librte_mempool/rte_mempool.c | 245 ++++++++++++++++++++++++++++++++++++++-
 lib/librte_mempool/rte_mempool.h |  59 +++++++++-
 6 files changed, 315 insertions(+), 3 deletions(-)
app/test/Makefile +2 -0
index 9a2f75f..8fcf0c2 100644
@@ -74,7 +74,9 @@ SRCS-$(CONFIG_RTE_LIBRTE_TIMER) += test_timer_perf.c
 SRCS-$(CONFIG_RTE_LIBRTE_TIMER) += test_timer_racecond.c
 
 SRCS-y += test_mempool.c
+ifeq ($(CONFIG_RTE_NEXT_ABI),y)
 SRCS-y += test_ext_mempool.c
+endif
 SRCS-y += test_mempool_perf.c
 
 SRCS-y += test_mbuf.c
app/test/test_mempool_perf.c +3 -0
index 091c1df..ca69e49 100644
@@ -161,6 +161,9 @@ per_lcore_mempool_test(__attribute__((unused)) void *arg)
 							   n_get_bulk);
 				if (unlikely(ret < 0)) {
 					rte_mempool_dump(stdout, mp);
+#ifndef RTE_NEXT_ABI
+					rte_ring_dump(stdout, mp->ring);
+#endif
 					/* in this case, objects are lost... */
 					return -1;
 				}
lib/librte_mbuf/rte_mbuf.c +7 -0
index 42b0cd1..967d987 100644
@@ -167,6 +167,7 @@ rte_pktmbuf_pool_create(const char *name, unsigned n,
 	mbp_priv.mbuf_data_room_size = data_room_size;
 	mbp_priv.mbuf_priv_size = priv_size;
 
+#ifdef RTE_NEXT_ABI
 #ifdef RTE_MEMPOOL_HANDLER_EXT
 	return rte_mempool_create_ext(name, n, elt_size,
 		cache_size, sizeof(struct rte_pktmbuf_pool_private),
@@ -179,6 +180,12 @@ rte_pktmbuf_pool_create(const char *name, unsigned n,
 		rte_pktmbuf_pool_init, &mbp_priv, rte_pktmbuf_init, NULL,
 		socket_id, 0);
 #endif
+#else
+	return rte_mempool_create(name, n, elt_size,
+		cache_size, sizeof(struct rte_pktmbuf_pool_private),
+		rte_pktmbuf_pool_init, &mbp_priv, rte_pktmbuf_init, NULL,
+		socket_id, 0);
+#endif
 }
 
 /* do some sanity checks on a mbuf: panic if it fails */
lib/librte_mempool/Makefile +2 -0
index a32c89e..a27eef9 100644
@@ -42,8 +42,10 @@ LIBABIVER := 1
 
 # all source are stored in SRCS-y
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool.c
+ifeq ($(CONFIG_RTE_NEXT_ABI),y)
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_handler.c
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_default.c
+endif
 
 ifeq ($(CONFIG_RTE_LIBRTE_XEN_DOM0),y)
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_dom0_mempool.c
lib/librte_mempool/rte_mempool.c +244 -1

300 lines not on this page · raw message

lib/librte_mempool/rte_mempool.h +57 -2

155 lines not on this page · raw message

Signature · 1 line
2.5.0
#51 lore raw

Re: [PATCH v3 4/4] mempool: add in the RTE_NEXT_ABI for ABI breakages

Quoted 3 lines · David Hunt

This patch is for those people who want to be easily able to switch between the new mempool layout and the old. Change the value of RTE_NEXT_ABI in common_base config file

I guess the idea here is to document how to switch between the ABIs but to me this reads as if this patch is supposed to change the value in common_base. Of course there's no such change included (nor should there be) here, but the description could use some fine-tuning perhaps.

Quoted 7 lines

v3: Updated to take re-work of file layouts into consideration

v2: Kept all the NEXT_ABI defs to this patch so as to make the previous patches easier to read, and also to imake it clear what code is necessary to keep ABI compatibility when NEXT_ABI is disabled.

Maybe its just me, but:
I can see why NEXT_ABI is in a separate patch for review purposes but for final commit this split doesn't seem right to me. In any case its quite a large change for NEXT_ABI.

In any case, you should add a deprecation notice for the oncoming ABI break in 16.07.

- Panu -

#52 lore raw

Re: [PATCH v3 4/4] mempool: add in the RTE_NEXT_ABI for ABI breakages

Hi Panu,

Quoted 7 lines · Panu Matilainen
Quoted 3 lines · David Hunt

This patch is for those people who want to be easily able to switch between the new mempool layout and the old. Change the value of RTE_NEXT_ABI in common_base config file

I guess the idea here is to document how to switch between the ABIs but to me this reads as if this patch is supposed to change the value in common_base. Of course there's no such change included (nor should there be) here, but the description could use some fine-tuning perhaps.

You're right, I'll clarify the comments. v4 due soon.

Quoted 11 lines
Quoted 7 lines

v3: Updated to take re-work of file layouts into consideration

v2: Kept all the NEXT_ABI defs to this patch so as to make the previous patches easier to read, and also to imake it clear what code is necessary to keep ABI compatibility when NEXT_ABI is disabled.

Maybe its just me, but:
I can see why NEXT_ABI is in a separate patch for review purposes but for final commit this split doesn't seem right to me. In any case its quite a large change for NEXT_ABI.

The patch basically re-introduces the old (pre-mempool) code as the refactoring of the code would have made the NEXT_ABI additions totally unreadable. I think this way is the lesser of two evils.

Quoted 2 lines

In any case, you should add a deprecation notice for the oncoming ABI break in 16.07.

Sure, I'll add that in v4.

Quoted 2 lines

- Panu -

Thanks for the comments,
Regards,
David.

#53 lore raw

Re: [PATCH v3 4/4] mempool: add in the RTE_NEXT_ABI for ABI breakages

Hi David,

Quoted 34 lines · David

Hi Panu,

Quoted 10 lines · Panu Matilainen
Quoted 3 lines · David Hunt

This patch is for those people who want to be easily able to switch between the new mempool layout and the old. Change the value of RTE_NEXT_ABI in common_base config file

I guess the idea here is to document how to switch between the ABIs but to me this reads as if this patch is supposed to change the value in common_base. Of course there's no such change included (nor should there be) here, but the description could use some fine-tuning perhaps.

You're right, I'll clarify the comments. v4 due soon.

Quoted 8 lines
Quoted 7 lines

v3: Updated to take re-work of file layouts into consideration

v2: Kept all the NEXT_ABI defs to this patch so as to make the previous patches easier to read, and also to imake it clear what code is necessary to keep ABI compatibility when NEXT_ABI is disabled.

Quoted 5 lines

Maybe its just me, but:
I can see why NEXT_ABI is in a separate patch for review purposes but for final commit this split doesn't seem right to me. In any case its quite a large change for NEXT_ABI.

The patch basically re-introduces the old (pre-mempool) code as the refactoring of the code would have made the NEXT_ABI additions totally unreadable. I think this way is the lesser of two evils.

Quoted 5 lines
Quoted 3 lines

In any case, you should add a deprecation notice for the oncoming ABI break in 16.07.

Sure, I'll add that in v4.

Sorry, maybe I wasn't very clear in my previous messages. For me, the NEXT_ABI is not the proper solution because, as Panu stated, it makes the patch hard to read. My understanding of NEXT_ABI is that it should only be used if the changes are small enough. Duplicating the code with a big #ifdef NEXT_ABI is not an option to me either.

So that's why the deprecation notice should be used instead. But in this case, it means that this patch won't be present in 16.04, but will be added in 16.07.

Regards,
Olivier

#54 lore raw

Re: [PATCH v3 4/4] mempool: add in the RTE_NEXT_ABI for ABI breakages

Hi Olivier,

Quoted 42 lines · Olivier MATZ

Hi David,

Quoted 25 lines · David

Hi Panu,

Quoted 9 lines · Panu Matilainen
Quoted 3 lines · David Hunt

This patch is for those people who want to be easily able to switch between the new mempool layout and the old. Change the value of RTE_NEXT_ABI in common_base config file

I guess the idea here is to document how to switch between the ABIs but to me this reads as if this patch is supposed to change the value in common_base. Of course there's no such change included (nor should there be) here, but the description could use some fine-tuning perhaps.

You're right, I'll clarify the comments. v4 due soon.

Quoted 11 lines
Quoted 6 lines

v3: Updated to take re-work of file layouts into consideration

v2: Kept all the NEXT_ABI defs to this patch so as to make the previous patches easier to read, and also to imake it clear what code is necessary to keep ABI compatibility when NEXT_ABI is disabled.

Maybe its just me, but:
I can see why NEXT_ABI is in a separate patch for review purposes but for final commit this split doesn't seem right to me. In any case its quite a large change for NEXT_ABI.

Quoted 8 lines

The patch basically re-introduces the old (pre-mempool) code as the refactoring of the code would have made the NEXT_ABI additions totally unreadable. I think this way is the lesser of two evils.

Quoted 3 lines

In any case, you should add a deprecation notice for the oncoming ABI break in 16.07.

Sure, I'll add that in v4.

Sorry, maybe I wasn't very clear in my previous messages. For me, the NEXT_ABI is not the proper solution because, as Panu stated, it makes the patch hard to read. My understanding of NEXT_ABI is that it should only be used if the changes are small enough. Duplicating the code with a big #ifdef NEXT_ABI is not an option to me either.

Quoted 6 lines

So that's why the deprecation notice should be used instead. But in this case, it means that this patch won't be present in 16.04, but will be added in 16.07.

Regards,
Olivier

Sure, v4 will remove the NEXT_ABI patch , and replace it with just the ABI break announcement for 16.07. For anyone who what's to try out the patch, they can always get it from patchwork, but not as part 16.04.

Thanks,
David.

#55 lore raw

Re: [PATCH v3 4/4] mempool: add in the RTE_NEXT_ABI for ABI breakages

Hi David,

Quoted 6 lines · David
Quoted 6 lines

Sorry, maybe I wasn't very clear in my previous messages. For me, the NEXT_ABI is not the proper solution because, as Panu stated, it makes the patch hard to read. My understanding of NEXT_ABI is that it should only be used if the changes are small enough. Duplicating the code with a big #ifdef NEXT_ABI is not an option to me either.

Quoted 7 lines
Quoted 4 lines

So that's why the deprecation notice should be used instead. But in this case, it means that this patch won't be present in 16.04, but will be added in 16.07.

Sure, v4 will remove the NEXT_ABI patch , and replace it with just the ABI break announcement for 16.07. For anyone who what's to try out the patch, they can always get it from patchwork, but not as part 16.04.

I think it's better to have the deprecation notice in a separate mail, outside of the patch series, so Thomas can just apply this one and let the series pending for 16.07.

Thanks,
Olivier

#56 lore raw

Re: [PATCH v3 4/4] mempool: add in the RTE_NEXT_ABI for ABI breakages

Hi Olivier,

Quoted 7 lines · Olivier MATZ

Hi David,

Quoted 3 lines · David

Sure, v4 will remove the NEXT_ABI patch , and replace it with just the ABI break announcement for 16.07. For anyone who what's to try out the patch, they can always get it from patchwork, but not as part 16.04.

Quoted 6 lines

I think it's better to have the deprecation notice in a separate mail, outside of the patch series, so Thomas can just apply this one and let the series pending for 16.07.

Thanks,
Olivier

Yes, sure, makes perfect sense.

Thanks,
David.

#57 lore raw

Re: [PATCH v3 0/4] external mempool manager

In reply to David Hunt
Quoted 2 lines · David Hunt

* removed stack hanler, may re-introduce at a later date

Some comments regarding this have made good points to keep this handler in. Will do in v4.

Regards,
David.

#58 lore raw

Re: [PATCH v3 0/4] external mempool manager

In reply to David Hunt

On Wed, Mar 09, 2016 at 09:50:33AM +0000, David Hunt wrote:
...

Quoted 7 lines

The external mempool manager needs to provide the following functions.
1. alloc - allocates the mempool memory, and adds each object onto a ring
2. put - puts an object back into the mempool once an application has
finished with it
3. get - gets an object from the mempool for use by the application
4. get_count - gets the number of available objects in the mempool
5. free - frees the mempool memory

It's a lengthy and great description, and it's a pity that you don't include it in the commit log: cover letter will not be in the history.

Quoted 3 lines

For and example of a simple malloc based mempool manager, see lib/librte_mempool/custom_mempool.c

I didn't see this file. Forgot to include it?

	--yliu
#59 lore raw

v4 cover external mempool manager

In reply to David Hunt

Here's a reworked version of the patch initially sent by David Hunt. The main change is that it is rebased on top of the "mempool: rework memory allocation" series [1], which simplifies a lot the first patch.

[1] http://dpdk.org/ml/archives/dev/2016-April/037464.html

v4 changes:
* remove the rte_mempool_create_ext() function. To change the handler, the user has to do the following:
- mp = rte_mempool_create_empty()
- rte_mempool_set_handler(mp, "my_handler")
- rte_mempool_populate_default(mp)
This avoids to add another function with more than 10 arguments, duplicating the doxygen comments
* change the api of rte_mempool_alloc_t: only the mempool pointer is required as all information is available in it
* change the api of rte_mempool_free_t: remove return value
* move inline wrapper functions from the .c to the .h (else they won't be inlined). This implies to have one header file (rte_mempool.h), or it would have generate cross dependencies issues.
* remove now unused MEMPOOL_F_INT_HANDLER (note: it was misused anyway due to the use of && instead of &)
* fix build in debug mode (__MEMPOOL_STAT_ADD(mp, put_pool, n) remaining)
* fix build with shared libraries (global handler has to be declared in the .map file)
* rationalize #include order
* remove unused function rte_mempool_get_handler_name()
* rename some structures, fields, functions
* remove the static in front of rte_tailq_elem rte_mempool_tailq (comment from Yuanhan)
* test the ext mempool handler in the same file than standard mempool tests, avoiding to duplicate the code
* rework the custom handler in mempool_test
* rework a bit the patch selecting default mbuf pool handler
* fix some doxygen comments

Things that should still be discussed:

- Panu pointed out that having a compile-time configuration option for selecting the default mbuf handler is not a good idea. I mostly agree, except in one case (and that's why I kept this patch):
if a specific architecture has its own way to provide an efficient pool handler for mbufs, it could be the proper place to have this option. But as far as I know, there is no such architecture today in dpdk.

- The other question I would like to raise is about the use cases. The cover letter below could be a bit more explicit about what this feature will be used for.

This is the initial unmodified cover letter from David Hunt:

Hi list.

Here's the v3 version patch for an external mempool manager

v3 changes:
* simplified the file layout, renamed to rte_mempool_handler.[hc]
* moved the default handlers into rte_mempool_default.c
* moved the example handler out into app/test/test_ext_mempool.c
* removed is_mc/is_mp change, slight perf degredation on sp cached operation
* removed stack hanler, may re-introduce at a later date
* Changes out of code reviews

v2 changes:
* There was a lot of duplicate code between rte_mempool_xmem_create and rte_mempool_create_ext. This has now been refactored and is now hopefully cleaner.
* The RTE_NEXT_ABI define is now used to allow building of the library in a format that is compatible with binaries built against previous versions of DPDK.
* Changes out of code reviews. Hopefully I've got most of them included.

The External Mempool Manager is an extension to the mempool API that allows users to add and use an external mempool manager, which allows external memory subsystems such as external hardware memory management systems and software based memory allocators to be used with DPDK.

The existing API to the internal DPDK mempool manager will remain unchanged and will be backward compatible. However, there will be an ABI breakage, as the mempool struct is changing. These changes are all contained withing RTE_NEXT_ABI defs, and the current or next code can be changed with the CONFIG_RTE_NEXT_ABI config setting

There are two aspects to external mempool manager.
1. Adding the code for your new mempool handler. This is achieved by adding a new mempool handler source file into the librte_mempool library, and using the REGISTER_MEMPOOL_HANDLER macro.
2. Using the new API to call rte_mempool_create_ext to create a new mempool using the name parameter to identify which handler to use.

New API calls added
1. A new mempool 'create' function which accepts mempool handler name.
2. A new mempool 'rte_get_mempool_handler' function which accepts mempool handler name, and returns the index to the relevant set of callbacks for that mempool handler

Several external mempool managers may be used in the same application. A new mempool can then be created by using the new 'create' function, providing the mempool handler name to point the mempool to the relevant mempool manager callback structure.

The old 'create' function can still be called by legacy programs, and will internally work out the mempool handle based on the flags provided (single producer, single consumer, etc). By default handles are created internally to implement the built-in DPDK mempool manager and mempool types.

The external mempool manager needs to provide the following functions.
1. alloc - allocates the mempool memory, and adds each object onto a ring
2. put - puts an object back into the mempool once an application has
finished with it
3. get - gets an object from the mempool for use by the application
4. get_count - gets the number of available objects in the mempool
5. free - frees the mempool memory

Every time a get/put/get_count is called from the application/PMD, the callback for that mempool is called. These functions are in the fastpath, and any unoptimised handlers may limit performance.

The new APIs are as follows:

1. rte_mempool_create_ext

struct rte_mempool * rte_mempool_create_ext(const char * name, unsigned n,
unsigned cache_size, unsigned private_data_size, int socket_id, unsigned flags,
const char * handler_name);

2. rte_mempool_get_handler_name

char *
rte_mempool_get_handler_name(struct rte_mempool *mp);

Please see rte_mempool.h for further information on the parameters.

The important thing to note is that the mempool handler is passed by name to rte_mempool_create_ext, and that in turn calls rte_get_mempool_handler to get the handler index, which is stored in the rte_memool structure. This allow multiple processes to use the same mempool, as the function pointers are accessed via handler index.

The mempool handler structure contains callbacks to the implementation of the handler, and is set up for registration as follows:

static struct rte_mempool_handler handler_sp_mc = {
.name = "ring_sp_mc",
.alloc = rte_mempool_common_ring_alloc, .put = common_ring_sp_put,
.get = common_ring_mc_get,
.get_count = common_ring_get_count, .free = common_ring_free,
};

And then the following macro will register the handler in the array of handlers

REGISTER_MEMPOOL_HANDLER(handler_mp_mc);

For and example of a simple malloc based mempool manager, see lib/librte_mempool/custom_mempool.c

For an example of API usage, please see app/test/test_ext_mempool.c, which implements a rudimentary mempool manager using simple mallocs for each mempool object. This file also contains the callbacks and self registration for the new handler.

David Hunt (2):
mempool: support external handler
mbuf: get default mempool handler from configuration

Olivier Matz (1):
app/test: test external mempool handler

 app/test/test_mempool.c                    | 113 +++++++++++++++
 app/test/test_mempool_perf.c               |   1 -
 config/common_base                         |   1 +
 lib/librte_mbuf/rte_mbuf.c                 |  21 ++-
 lib/librte_mempool/Makefile                |   2 +
 lib/librte_mempool/rte_mempool.c           |  72 ++++------
 lib/librte_mempool/rte_mempool.h           | 212 +++++++++++++++++++++++++----
 lib/librte_mempool/rte_mempool_default.c   | 147 ++++++++++++++++++++
 lib/librte_mempool/rte_mempool_handler.c   | 139 +++++++++++++++++++
 lib/librte_mempool/rte_mempool_version.map |   4 +
 10 files changed, 637 insertions(+), 75 deletions(-)
 create mode 100644 lib/librte_mempool/rte_mempool_default.c
 create mode 100644 lib/librte_mempool/rte_mempool_handler.c
Signature · 1 line
2.1.4
#60 lore raw

PATCH v4 1/3 mempool: support external handler

From: David Hunt <[email protected]>

Until now, the objects stored in mempool mempool were internally stored a ring. This patch introduce the possibility to register external handlers replacing the ring.

The default behavior remains unchanged, but calling the new function rte_mempool_set_handler() right after rte_mempool_create_empty() allows to change the handler that will be used when populating the mempool.

---
 app/test/test_mempool_perf.c               |   1 -
 lib/librte_mempool/Makefile                |   2 +
 lib/librte_mempool/rte_mempool.c           |  72 ++++------
 lib/librte_mempool/rte_mempool.h           | 212 +++++++++++++++++++++++++----
 lib/librte_mempool/rte_mempool_default.c   | 147 ++++++++++++++++++++
 lib/librte_mempool/rte_mempool_handler.c   | 139 +++++++++++++++++++
 lib/librte_mempool/rte_mempool_version.map |   4 +
 7 files changed, 506 insertions(+), 71 deletions(-)
 create mode 100644 lib/librte_mempool/rte_mempool_default.c
 create mode 100644 lib/librte_mempool/rte_mempool_handler.c
app/test/test_mempool_perf.c +0 -1
index cdc02a0..091c1df 100644
@@ -161,7 +161,6 @@ per_lcore_mempool_test(__attribute__((unused)) void *arg)
 							   n_get_bulk);
 				if (unlikely(ret < 0)) {
 					rte_mempool_dump(stdout, mp);
-					rte_ring_dump(stdout, mp->ring);
 					/* in this case, objects are lost... */
 					return -1;
 				}
lib/librte_mempool/Makefile +2 -0
index 43423e0..f19366e 100644
@@ -42,6 +42,8 @@ LIBABIVER := 2
 
 # all source are stored in SRCS-y
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_handler.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_default.c
 # install includes
 SYMLINK-$(CONFIG_RTE_LIBRTE_MEMPOOL)-include := rte_mempool.h
 
lib/librte_mempool/rte_mempool.c +28 -44

130 lines not on this page · raw message

lib/librte_mempool/rte_mempool.h +186 -26

269 lines not on this page · raw message

lib/librte_mempool/rte_mempool_default.c new file +147 -0

147 lines not on this page · raw message

lib/librte_mempool/rte_mempool_handler.c new file +139 -0

139 lines not on this page · raw message

lib/librte_mempool/rte_mempool_version.map +4 -0
index 7d1f670..1ec9751 100644
@@ -19,6 +19,8 @@ DPDK_2.0 {
 DPDK_16.7 {
 	global:
 
+	rte_mempool_handler_table;
+
 	rte_mempool_obj_iter;
 	rte_mempool_mem_iter;
 	rte_mempool_create_empty;
@@ -28,6 +30,8 @@ DPDK_16.7 {
 	rte_mempool_populate_default;
 	rte_mempool_populate_anon;
 	rte_mempool_free;
+	rte_mempool_set_handler;
+	rte_mempool_handler_register;
 
 	local: *;
 } DPDK_2.0;
Signature · 1 line
2.1.4
#61 lore raw

PATCH v4 2/3 app/test: test external mempool handler

In reply to Olivier Matz

Use a minimal custom mempool external handler and check that it also passes basic mempool autotests.

---
 app/test/test_mempool.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 113 insertions(+)
app/test/test_mempool.c +113 -0
index c96ed27..09951cc 100644
@@ -85,6 +85,96 @@
 static rte_atomic32_t synchro;
 
 /*
+ * Simple example of custom mempool structure. Holds pointers to all the
+ * elements which are simply malloc'd in this example.
+ */
+struct custom_mempool {
+	rte_spinlock_t lock;
+	unsigned count;
+	unsigned size;
+	void *elts[];
+};
+
+/*
+ * Loop though all the element pointers and allocate a chunk of memory, then
+ * insert that memory into the ring.
+ */
+static void *
+custom_mempool_alloc(struct rte_mempool *mp)
+{
+	struct custom_mempool *cm;
+
+	cm = rte_zmalloc("custom_mempool",
+		sizeof(struct custom_mempool) + mp->size * sizeof(void *), 0);
+	if (cm == NULL)
+		return NULL;
+
+	rte_spinlock_init(&cm->lock);
+	cm->count = 0;
+	cm->size = mp->size;
+	return cm;
+}
+
+static void
+custom_mempool_free(void *p)
+{
+	rte_free(p);
+}
+
+static int
+custom_mempool_put(void *p, void * const *obj_table, unsigned n)
+{
+	struct custom_mempool *cm = (struct custom_mempool *)p;
+	int ret = 0;
+
+	rte_spinlock_lock(&cm->lock);
+	if (cm->count + n > cm->size) {
+		ret = -ENOBUFS;
+	} else {
+		memcpy(&cm->elts[cm->count], obj_table, sizeof(void *) * n);
+		cm->count += n;
+	}
+	rte_spinlock_unlock(&cm->lock);
+	return ret;
+}
+
+
+static int
+custom_mempool_get(void *p, void **obj_table, unsigned n)
+{
+	struct custom_mempool *cm = (struct custom_mempool *)p;
+	int ret = 0;
+
+	rte_spinlock_lock(&cm->lock);
+	if (n > cm->count) {
+		ret = -ENOENT;
+	} else {
+		cm->count -= n;
+		memcpy(obj_table, &cm->elts[cm->count], sizeof(void *) * n);
+	}
+	rte_spinlock_unlock(&cm->lock);
+	return ret;
+}
+
+static unsigned
+custom_mempool_get_count(void *p)
+{
+	struct custom_mempool *cm = (struct custom_mempool *)p;
+	return cm->count;
+}
+
+static struct rte_mempool_handler mempool_handler_custom = {
+	.name = "custom_handler",
+	.alloc = custom_mempool_alloc,
+	.free = custom_mempool_free,
+	.put = custom_mempool_put,
+	.get = custom_mempool_get,
+	.get_count = custom_mempool_get_count,
+};
+
+MEMPOOL_REGISTER_HANDLER(mempool_handler_custom);
+
+/*
  * save the object number in the first 4 bytes of object data. All
  * other bytes are set to 0.
  */
@@ -479,6 +569,7 @@ test_mempool(void)
 {
 	struct rte_mempool *mp_cache = NULL;
 	struct rte_mempool *mp_nocache = NULL;
+	struct rte_mempool *mp_ext = NULL;
 
 	rte_atomic32_init(&synchro);
 
@@ -507,6 +598,27 @@ test_mempool(void)
 		goto err;
 	}
 
+	/* create a mempool with an external handler */
+	mp_ext = rte_mempool_create_empty("test_ext",
+		MEMPOOL_SIZE,
+		MEMPOOL_ELT_SIZE,
+		RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
+		SOCKET_ID_ANY, 0);
+
+	if (mp_ext == NULL) {
+		printf("cannot allocate mp_ext mempool\n");
+		goto err;
+	}
+	if (rte_mempool_set_handler(mp_ext, "custom_handler") < 0) {
+		printf("cannot set custom handler\n");
+		goto err;
+	}
+	if (rte_mempool_populate_default(mp_ext) < 0) {
+		printf("cannot populate mp_ext mempool\n");
+		goto err;
+	}
+	rte_mempool_obj_iter(mp_ext, my_obj_init, NULL);
+
 	/* retrieve the mempool from its name */
 	if (rte_mempool_lookup("test_nocache") != mp_nocache) {
 		printf("Cannot lookup mempool from its name\n");
@@ -547,6 +659,7 @@ test_mempool(void)
 err:
 	rte_mempool_free(mp_nocache);
 	rte_mempool_free(mp_cache);
+	rte_mempool_free(mp_ext);
 	return -1;
 }
 
Signature · 1 line
2.1.4
#62 lore raw

PATCH v4 3/3 mbuf: get default mempool handler from configuration

In reply to Olivier Matz

From: David Hunt <[email protected]>

By default, the mempool handler used for mbuf allocations is a multi producer and multi consumer ring. We could imagine a target (maybe some network processors?) that provides an hardware-assisted pool mechanism. In this case, the default configuration for this architecture would contain a different value for RTE_MBUF_DEFAULT_MEMPOOL_HANDLER.

---
 config/common_base         |  1 +
 lib/librte_mbuf/rte_mbuf.c | 21 +++++++++++++++++----
 2 files changed, 18 insertions(+), 4 deletions(-)
config/common_base +1 -0
index 0124e86..178cb7e 100644
@@ -390,6 +390,7 @@ CONFIG_RTE_LIBRTE_MEMPOOL_DEBUG=n
 #
 CONFIG_RTE_LIBRTE_MBUF=y
 CONFIG_RTE_LIBRTE_MBUF_DEBUG=n
+CONFIG_RTE_MBUF_DEFAULT_MEMPOOL_HANDLER="ring_mp_mc"
 CONFIG_RTE_MBUF_REFCNT_ATOMIC=y
 CONFIG_RTE_PKTMBUF_HEADROOM=128
 
lib/librte_mbuf/rte_mbuf.c +17 -4
index dc0467c..a72f8f2 100644
@@ -153,6 +153,7 @@ rte_pktmbuf_pool_create(const char *name, unsigned n,
 	unsigned cache_size, uint16_t priv_size, uint16_t data_room_size,
 	int socket_id)
 {
+	struct rte_mempool *mp;
 	struct rte_pktmbuf_pool_private mbp_priv;
 	unsigned elt_size;
 
@@ -167,10 +168,22 @@ rte_pktmbuf_pool_create(const char *name, unsigned n,
 	mbp_priv.mbuf_data_room_size = data_room_size;
 	mbp_priv.mbuf_priv_size = priv_size;
 
-	return rte_mempool_create(name, n, elt_size,
-		cache_size, sizeof(struct rte_pktmbuf_pool_private),
-		rte_pktmbuf_pool_init, &mbp_priv, rte_pktmbuf_init, NULL,
-		socket_id, 0);
+	mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
+		 sizeof(struct rte_pktmbuf_pool_private), socket_id, 0);
+	if (mp == NULL)
+		return NULL;
+
+	rte_mempool_set_handler(mp, RTE_MBUF_DEFAULT_MEMPOOL_HANDLER);
+	rte_pktmbuf_pool_init(mp, &mbp_priv);
+
+	if (rte_mempool_populate_default(mp) < 0) {
+		rte_mempool_free(mp);
+		return NULL;
+	}
+
+	rte_mempool_obj_iter(mp, rte_pktmbuf_init, NULL);
+
+	return mp;
 }
 
 /* do some sanity checks on a mbuf: panic if it fails */
Signature · 1 line
2.1.4
author
#63 lore raw

mempool: external mempool manager

In reply to Olivier Matz

Here's the latest version of the External Mempool Manager patchset. It's re-based on top of the latest head as of 19/5/2016, including Olivier's 35-part patch series on mempool re-org [1]

[1] http://dpdk.org/ml/archives/dev/2016-May/039229.html

v5 changes:
* rebasing, as it is dependent on another patch series [1]

v4 changes (Olivier Matz):
* remove the rte_mempool_create_ext() function. To change the handler, the user has to do the following:
- mp = rte_mempool_create_empty()
- rte_mempool_set_handler(mp, "my_handler")
- rte_mempool_populate_default(mp)
This avoids to add another function with more than 10 arguments, duplicating the doxygen comments
* change the api of rte_mempool_alloc_t: only the mempool pointer is required as all information is available in it
* change the api of rte_mempool_free_t: remove return value
* move inline wrapper functions from the .c to the .h (else they won't be inlined). This implies to have one header file (rte_mempool.h), or it would have generate cross dependencies issues.
* remove now unused MEMPOOL_F_INT_HANDLER (note: it was misused anyway due to the use of && instead of &)
* fix build in debug mode (__MEMPOOL_STAT_ADD(mp, put_pool, n) remaining)
* fix build with shared libraries (global handler has to be declared in the .map file)
* rationalize #include order
* remove unused function rte_mempool_get_handler_name()
* rename some structures, fields, functions
* remove the static in front of rte_tailq_elem rte_mempool_tailq (comment from Yuanhan)
* test the ext mempool handler in the same file than standard mempool tests, avoiding to duplicate the code
* rework the custom handler in mempool_test
* rework a bit the patch selecting default mbuf pool handler
* fix some doxygen comments

v3 changes:
* simplified the file layout, renamed to rte_mempool_handler.[hc]
* moved the default handlers into rte_mempool_default.c
* moved the example handler out into app/test/test_ext_mempool.c
* removed is_mc/is_mp change, slight perf degredation on sp cached operation
* removed stack hanler, may re-introduce at a later date
* Changes out of code reviews

v2 changes:
* There was a lot of duplicate code between rte_mempool_xmem_create and rte_mempool_create_ext. This has now been refactored and is now hopefully cleaner.
* The RTE_NEXT_ABI define is now used to allow building of the library in a format that is compatible with binaries built against previous versions of DPDK.
* Changes out of code reviews. Hopefully I've got most of them included.

The External Mempool Manager is an extension to the mempool API that allows users to add and use an external mempool manager, which allows external memory subsystems such as external hardware memory management systems and software based memory allocators to be used with DPDK.

The existing API to the internal DPDK mempool manager will remain unchanged and will be backward compatible. However, there will be an ABI breakage, as the mempool struct is changing. These changes are all contained withing RTE_NEXT_ABI defs, and the current or next code can be changed with the CONFIG_RTE_NEXT_ABI config setting

There are two aspects to external mempool manager.
1. Adding the code for your new mempool handler. This is achieved by adding a new mempool handler source file into the librte_mempool library, and using the REGISTER_MEMPOOL_HANDLER macro.
2. Using the new API to call rte_mempool_create_empty and rte_mempool_set_handler to create a new mempool
using the name parameter to identify which handler to use.

New API calls added
1. A new rte_mempool_create_empty() function
2. rte_mempool_set_handler() which sets the mempool's handler
3. An rte_mempool_populate_default() and rte_mempool_populate_anon() functions which populates the mempool using the relevant handler

Several external mempool managers may be used in the same application. A new mempool can then be created by using the new 'create' function, providing the mempool handler name to point the mempool to the relevant mempool manager callback structure.

The old 'create' function can still be called by legacy programs, and will internally work out the mempool handle based on the flags provided (single producer, single consumer, etc). By default handles are created internally to implement the built-in DPDK mempool manager and mempool types.

The external mempool manager needs to provide the following functions.
1. alloc - allocates the mempool memory, and adds each object onto a ring
2. put - puts an object back into the mempool once an application has
finished with it
3. get - gets an object from the mempool for use by the application
4. get_count - gets the number of available objects in the mempool
5. free - frees the mempool memory

Every time a get/put/get_count is called from the application/PMD, the callback for that mempool is called. These functions are in the fastpath, and any unoptimised handlers may limit performance.

The new APIs are as follows:

1. rte_mempool_create_empty

struct rte_mempool *
rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
unsigned cache_size, unsigned private_data_size,
int socket_id, unsigned flags);

2. rte_mempool_set_handler()

int
rte_mempool_set_handler(struct rte_mempool *mp, const char *name);

3. rte_mempool_populate_default()

int rte_mempool_populate_default(struct rte_mempool *mp);

4. rte_mempool_populate_anon()

int rte_mempool_populate_anon(struct rte_mempool *mp);

Please see rte_mempool.h for further information on the parameters.

The important thing to note is that the mempool handler is passed by name to rte_mempool_set_handler, which looks through the handler array to get the handler index, which is then stored in the rte_memool structure. This allow multiple processes to use the same mempool, as the function pointers are accessed via handler index.

The mempool handler structure contains callbacks to the implementation of the handler, and is set up for registration as follows:

static const struct rte_mempool_handler handler_sp_mc = {
.name = "ring_sp_mc",
.alloc = rte_mempool_common_ring_alloc,
.put = common_ring_sp_put,
.get = common_ring_mc_get,
.get_count = common_ring_get_count,
.free = common_ring_free,
};

And then the following macro will register the handler in the array of handlers

REGISTER_MEMPOOL_HANDLER(handler_mp_mc);

For and example of a simple malloc based mempool manager, see lib/librte_mempool/custom_mempool.c

For an example of API usage, please see app/test/test_mempool.c, which implements a rudimentary "custom_handler" mempool manager using simple mallocs for each mempool object. This file also contains the callbacks and self registration for the new handler.

David Hunt (2):
mempool: support external handler
mbuf: get default mempool handler from configuration

Olivier Matz (1):
app/test: test external mempool handler

author
#64 lore raw

PATCH v5 1/3 mempool: support external handler

Until now, the objects stored in mempool mempool were internally stored a ring. This patch introduce the possibility to register external handlers replacing the ring.

The default behavior remains unchanged, but calling the new function rte_mempool_set_handler() right after rte_mempool_create_empty() allows to change the handler that will be used when populating the mempool.

v5 changes: rebasing on top of 35 patch set mempool work.

---
 app/test/test_mempool_perf.c               |   1 -
 lib/librte_mempool/Makefile                |   2 +
 lib/librte_mempool/rte_mempool.c           |  73 ++++------
 lib/librte_mempool/rte_mempool.h           | 212 +++++++++++++++++++++++++----
 lib/librte_mempool/rte_mempool_default.c   | 147 ++++++++++++++++++++
 lib/librte_mempool/rte_mempool_handler.c   | 139 +++++++++++++++++++
 lib/librte_mempool/rte_mempool_version.map |   4 +
 7 files changed, 506 insertions(+), 72 deletions(-)
 create mode 100644 lib/librte_mempool/rte_mempool_default.c
 create mode 100644 lib/librte_mempool/rte_mempool_handler.c
app/test/test_mempool_perf.c +0 -1
index cdc02a0..091c1df 100644
@@ -161,7 +161,6 @@ per_lcore_mempool_test(__attribute__((unused)) void *arg)
 							   n_get_bulk);
 				if (unlikely(ret < 0)) {
 					rte_mempool_dump(stdout, mp);
-					rte_ring_dump(stdout, mp->ring);
 					/* in this case, objects are lost... */
 					return -1;
 				}
lib/librte_mempool/Makefile +2 -0
index 43423e0..f19366e 100644
@@ -42,6 +42,8 @@ LIBABIVER := 2
 
 # all source are stored in SRCS-y
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_handler.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_default.c
 # install includes
 SYMLINK-$(CONFIG_RTE_LIBRTE_MEMPOOL)-include := rte_mempool.h
 
lib/librte_mempool/rte_mempool.c +28 -45

131 lines not on this page · raw message

lib/librte_mempool/rte_mempool.h +186 -26

269 lines not on this page · raw message

lib/librte_mempool/rte_mempool_default.c new file +147 -0

147 lines not on this page · raw message

lib/librte_mempool/rte_mempool_handler.c new file +139 -0

139 lines not on this page · raw message

lib/librte_mempool/rte_mempool_version.map +4 -0
index f63461b..a0e9aed 100644
@@ -19,6 +19,8 @@ DPDK_2.0 {
 DPDK_16.7 {
 	global:
 
+	rte_mempool_handler_table;
+
 	rte_mempool_check_cookies;
 	rte_mempool_obj_iter;
 	rte_mempool_mem_iter;
@@ -29,6 +31,8 @@ DPDK_16.7 {
 	rte_mempool_populate_default;
 	rte_mempool_populate_anon;
 	rte_mempool_free;
+	rte_mempool_set_handler;
+	rte_mempool_handler_register;
 
 	local: *;
 } DPDK_2.0;
Signature · 1 line
2.5.5
#65 lore raw

Re: [dpdk-dev,v5,1/3] mempool: support external handler

Hello David,

please, see my comments inline.

I didn't see the previous versions of the mempool (well, only very roughly) so I am probably missing some points... My point of view is as a user of the handler API. I need to understand the API to implement a custom handler for my purposes.

Quoted 1 line · On Thu, 19 May 2016 14:44:59 +0100 David Hunt <[email protected]> wrote:

Until now, the objects stored in mempool mempool were internally stored a

s/mempool mempool/mempool/

stored _in_ a ring?

Quoted 29 lines

ring. This patch introduce the possibility to register external handlers replacing the ring.

The default behavior remains unchanged, but calling the new function rte_mempool_set_handler() right after rte_mempool_create_empty() allows to change the handler that will be used when populating the mempool.

v5 changes: rebasing on top of 35 patch set mempool work.

---
app/test/test_mempool_perf.c | 1 -

 lib/librte_mempool/Makefile                |   2 +
 lib/librte_mempool/rte_mempool.c           |  73 ++++------
 lib/librte_mempool/rte_mempool.h           | 212 +++++++++++++++++++++++++----
 lib/librte_mempool/rte_mempool_default.c   | 147 ++++++++++++++++++++
 lib/librte_mempool/rte_mempool_handler.c   | 139 +++++++++++++++++++
 lib/librte_mempool/rte_mempool_version.map |   4 +
 7 files changed, 506 insertions(+), 72 deletions(-)
 create mode 100644 lib/librte_mempool/rte_mempool_default.c
 create mode 100644 lib/librte_mempool/rte_mempool_handler.c
app/test/test_mempool_perf.c +0 -0
index cdc02a0..091c1df 100644

app/test/test_mempool_perf.c per_lcore_mempool_test(__attribute__((unused)) void *arg) · line 166

Quoted 7 lines
app/test/test_mempool_perf.c +0 -1
@@ -161,7 +161,6 @@ per_lcore_mempool_test(__attribute__((unused)) void *arg)
 							   n_get_bulk);
 				if (unlikely(ret < 0)) {
 					rte_mempool_dump(stdout, mp);
-					rte_ring_dump(stdout, mp->ring);
 					/* in this case, objects are lost... */
 					return -1;
 				}

I think, this should be in a separate patch explaining the reason to remove it.

Quoted 18 lines
lib/librte_mempool/Makefile +2 -0
index 43423e0..f19366e 100644
@@ -42,6 +42,8 @@ LIBABIVER := 2
 
 # all source are stored in SRCS-y
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_handler.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_default.c
 # install includes
 SYMLINK-$(CONFIG_RTE_LIBRTE_MEMPOOL)-include := rte_mempool.h
 
lib/librte_mempool/rte_mempool.c +0 -0
index 1ab6701..6ec2b3f 100644

lib/librte_mempool/rte_mempool.c mempool_add_elem(struct rte_mempool *mp, void *obj, phys_addr_t physaddr) · line 151

Quoted 5 lines
lib/librte_mempool/rte_mempool.c +1 -1
@@ -148,7 +148,7 @@ mempool_add_elem(struct rte_mempool *mp, void *obj, phys_addr_t physaddr)
 #endif
 
 	/* enqueue in ring */
-	rte_ring_sp_enqueue(mp->ring, obj);
+	rte_mempool_ext_put_bulk(mp, &obj, 1);

I suppose this is OK, however, replacing "enqueue" by "put" (semantically) sounds to me like a bug. Enqueue is put into a queue. Put is to drop a reference.

Quoted 30 lines
lib/librte_mempool/rte_mempool.c +0 -23
 }
 
 /* call obj_cb() for each mempool element */
@@ -300,40 +300,6 @@ rte_mempool_xmem_usage(__rte_unused void *vaddr, uint32_t elt_num,
 	return (size_t)paddr_idx << pg_shift;
 }
 
-/* create the internal ring */
-static int
-rte_mempool_ring_create(struct rte_mempool *mp)
-{
-	int rg_flags = 0, ret;
-	char rg_name[RTE_RING_NAMESIZE];
-	struct rte_ring *r;
-
-	ret = snprintf(rg_name, sizeof(rg_name),
-		RTE_MEMPOOL_MZ_FORMAT, mp->name);
-	if (ret < 0 || ret >= (int)sizeof(rg_name))
-		return -ENAMETOOLONG;
-
-	/* ring flags */
-	if (mp->flags & MEMPOOL_F_SP_PUT)
-		rg_flags |= RING_F_SP_ENQ;
-	if (mp->flags & MEMPOOL_F_SC_GET)
-		rg_flags |= RING_F_SC_DEQ;
-
-	/* Allocate the ring that will be used to store objects.
-	 * Ring functions will return appropriate errors if we are
-	 * running as a secondary process etc., so no checks made
-	 * in this function for that condition.

lib/librte_mempool/rte_mempool.c rte_mempool_xmem_usage(__rte_unused void *vaddr, uint32_t elt_num, · line 335

Quoted 10 lines
lib/librte_mempool/rte_mempool.c +0 -10
-	 */
-	r = rte_ring_create(rg_name, rte_align32pow2(mp->size + 1),
-		mp->socket_id, rg_flags);
-	if (r == NULL)
-		return -rte_errno;
-
-	mp->ring = r;
-	mp->flags |= MEMPOOL_F_RING_CREATED;
-	return 0;
-}

This is a big change. I suggest (if possible) to make a separate patch with something like "replace rte_mempool_ring_create by ...". Where is this code placed now?

lib/librte_mempool/rte_mempool.c rte_mempool_free_memchunks(struct rte_mempool *mp) · line 320

Quoted 10 lines
lib/librte_mempool/rte_mempool.c +1 -2
-
 /* free a memchunk allocated with rte_memzone_reserve() */
 static void
 rte_mempool_memchunk_mz_free(__rte_unused struct rte_mempool_memhdr *memhdr,
@@ -351,7 +317,7 @@ rte_mempool_free_memchunks(struct rte_mempool *mp)
 	void *elt;
 
 	while (!STAILQ_EMPTY(&mp->elt_list)) {
-		rte_ring_sc_dequeue(mp->ring, &elt);
+		rte_mempool_ext_get_bulk(mp, &elt, 1);

Similar as for put_bulk... Replacing "dequeue" by "get" (semantically) sounds to me like a bug. Dequeue is drop from a queue. Get is to obtain a reference.

Quoted 14 lines
lib/librte_mempool/rte_mempool.c +0 -4
 		(void)elt;
 		STAILQ_REMOVE_HEAD(&mp->elt_list, next);
 		mp->populated_size--;
@@ -380,15 +346,18 @@ rte_mempool_populate_phys(struct rte_mempool *mp, char *vaddr,
 	unsigned i = 0;
 	size_t off;
 	struct rte_mempool_memhdr *memhdr;
-	int ret;
 
 	/* create the internal ring if not already done */
 	if ((mp->flags & MEMPOOL_F_RING_CREATED) == 0) {
-		ret = rte_mempool_ring_create(mp);
-		if (ret < 0)
-			return ret;

lib/librte_mempool/rte_mempool.c rte_mempool_populate_phys(struct rte_mempool *mp, char *vaddr, · line 391

Quoted 10 lines
lib/librte_mempool/rte_mempool.c +8 -1
+		rte_errno = 0;
+		mp->pool = rte_mempool_ext_alloc(mp);
+		if (mp->pool == NULL) {
+			if (rte_errno == 0)
+				return -EINVAL;
+			else
+				return -rte_errno;
+		}
 	}
-

Is this a whitespace change?

Quoted 20 lines
lib/librte_mempool/rte_mempool.c +5 -1
 	/* mempool is already populated */
 	if (mp->populated_size >= mp->size)
 		return -ENOSPC;
@@ -700,7 +669,7 @@ rte_mempool_free(struct rte_mempool *mp)
 	rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
 
 	rte_mempool_free_memchunks(mp);
-	rte_ring_free(mp->ring);
+	rte_mempool_ext_free(mp);
 	rte_memzone_free(mp->mz);
 }
 
@@ -812,6 +781,20 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
 		RTE_PTR_ADD(mp, MEMPOOL_HEADER_SIZE(mp, 0));
 
 	te->data = mp;
+
+	/*
+	 * Since we have 4 combinations of the SP/SC/MP/MC examine the flags to
+	 * set the correct index into the handler table.

lib/librte_mempool/rte_mempool.c rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, · line 797

Quoted 10 lines
lib/librte_mempool/rte_mempool.c +10 -0
+	 */
+	if (flags & (MEMPOOL_F_SP_PUT | MEMPOOL_F_SC_GET))
+		rte_mempool_set_handler(mp, "ring_sp_sc");
+	else if (flags & MEMPOOL_F_SP_PUT)
+		rte_mempool_set_handler(mp, "ring_sp_mc");
+	else if (flags & MEMPOOL_F_SC_GET)
+		rte_mempool_set_handler(mp, "ring_mp_sc");
+	else
+		rte_mempool_set_handler(mp, "ring_mp_mc");
+

Do I understand it well that this code preserves behaviour of the previous API? Because otherwise it looks strange.

Quoted 45 lines
lib/librte_mempool/rte_mempool.c +3 -3
 	rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
 	TAILQ_INSERT_TAIL(mempool_list, te, next);
 	rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
@@ -927,7 +910,7 @@ rte_mempool_count(const struct rte_mempool *mp)
 	unsigned count;
 	unsigned lcore_id;
 
-	count = rte_ring_count(mp->ring);
+	count = rte_mempool_ext_get_count(mp);
 
 	if (mp->cache_size == 0)
 		return count;
@@ -1120,7 +1103,7 @@ rte_mempool_dump(FILE *f, struct rte_mempool *mp)
 
 	fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
 	fprintf(f, "  flags=%x\n", mp->flags);
-	fprintf(f, "  ring=<%s>@%p\n", mp->ring->name, mp->ring);
+	fprintf(f, "  pool=%p\n", mp->pool);
 	fprintf(f, "  phys_addr=0x%" PRIx64 "\n", mp->mz->phys_addr);
 	fprintf(f, "  nb_mem_chunks=%u\n", mp->nb_mem_chunks);
 	fprintf(f, "  size=%"PRIu32"\n", mp->size);
@@ -1141,7 +1124,7 @@ rte_mempool_dump(FILE *f, struct rte_mempool *mp)
 	}
 
 	cache_count = rte_mempool_dump_cache(f, mp);
-	common_count = rte_ring_count(mp->ring);
+	common_count = rte_mempool_ext_get_count(mp);
 	if ((cache_count + common_count) > mp->size)
 		common_count = mp->size - cache_count;
 	fprintf(f, "  common_pool_count=%u\n", common_count);
lib/librte_mempool/rte_mempool.h +1 -0
index 60339bd..ed2c110 100644
@@ -67,6 +67,7 @@
 #include <inttypes.h>
 #include <sys/queue.h>
 
+#include <rte_spinlock.h>
 #include <rte_log.h>
 #include <rte_debug.h>
 #include <rte_lcore.h>
@@ -203,7 +204,15 @@ struct rte_mempool_memhdr {
  */
 struct rte_mempool {

lib/librte_mempool/rte_mempool.h struct rte_mempool_memhdr { · line 214

Quoted 10 lines
lib/librte_mempool/rte_mempool.h +8 -1
 	char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
-	struct rte_ring *ring;           /**< Ring to store objects. */
+	void *pool;                      /**< Ring or ext-pool to store objects. */
+	/**
+	 * Index into the array of structs containing callback fn pointers.
+	 * We're using an index here rather than pointers to the callbacks
+	 * to facilitate any secondary processes that may want to use
+	 * this mempool. Any function pointers stored in the mempool
+	 * directly would not be valid for secondary processes.
+	 */

I think, this comment should go to the rte_mempool_handler_table definition leaving a here a short note about it.

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 339

Quoted 11 lines
lib/librte_mempool/rte_mempool.h +4 -0
+	int32_t handler_idx;
 	const struct rte_memzone *mz;    /**< Memzone where pool is allocated */
 	int flags;                       /**< Flags of the mempool. */
 	int socket_id;                   /**< Socket id passed at mempool creation. */
@@ -325,6 +334,175 @@ void rte_mempool_check_cookies(const struct rte_mempool *mp,
 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
 
+#define RTE_MEMPOOL_HANDLER_NAMESIZE 32 /**< Max length of handler name. */
+
+/** Allocate the external pool. */

What is the purpose of this callback?
What exactly does it allocate?
Some rte_mempool internals?
Or the memory?
What does it return?

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 342

Quoted 3 lines
lib/librte_mempool/rte_mempool.h +3 -0
+typedef void *(*rte_mempool_alloc_t)(struct rte_mempool *mp);
+
+/** Free the external pool. */

Why this *_free callback does not accept the rte_mempool param?

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 345

Quoted 3 lines
lib/librte_mempool/rte_mempool.h +3 -0
+typedef void (*rte_mempool_free_t)(void *p);
+
+/** Put an object in the external pool. */

What is the *p pointer?
What is the obj_table?
Why is it void *?
Why is it const?

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 346

Quoted 1 line
lib/librte_mempool/rte_mempool.h +1 -0
+typedef int (*rte_mempool_put_t)(void *p, void * const *obj_table, unsigned n);

Probably, "unsigned int n" is better.

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 349

Quoted 3 lines
lib/librte_mempool/rte_mempool.h +3 -0
+
+/** Get an object from the external pool. */
+typedef int (*rte_mempool_get_t)(void *p, void **obj_table, unsigned n);

Probably, "unsigned int n" is better.

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 351

Quoted 2 lines
lib/librte_mempool/rte_mempool.h +2 -0
+
+/** Return the number of available objects in the external pool. */

What is the purpose of the *_get_count callback? I guess it can introduce race conditions...

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 352

Quoted 1 line
lib/librte_mempool/rte_mempool.h +1 -0
+typedef unsigned (*rte_mempool_get_count)(void *p);

unsigned int

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 354

Quoted 2 lines
lib/librte_mempool/rte_mempool.h +2 -0
+
+/** Structure defining a mempool handler. */

Later in the text, I suggested to rename rte_mempool_handler to rte_mempool_ops. I believe that it explains the purpose of this struct better. It would improve consistency in function names (the *_ext_* mark is very strange and inconsistent).

Quoted 8 lines
lib/librte_mempool/rte_mempool.h +8 -0
+struct rte_mempool_handler {
+	char name[RTE_MEMPOOL_HANDLER_NAMESIZE]; /**< Name of mempool handler */
+	rte_mempool_alloc_t alloc;       /**< Allocate the external pool. */
+	rte_mempool_free_t free;         /**< Free the external pool. */
+	rte_mempool_put_t put;           /**< Put an object. */
+	rte_mempool_get_t get;           /**< Get an object. */
+	rte_mempool_get_count get_count; /**< Get the number of available objs. */
+} __rte_cache_aligned;

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 372

Quoted 10 lines
lib/librte_mempool/rte_mempool.h +10 -0
+
+#define RTE_MEMPOOL_MAX_HANDLER_IDX 16  /**< Max number of registered handlers */
+
+/** Structure storing the table of registered handlers. */
+struct rte_mempool_handler_table {
+	rte_spinlock_t sl;     /**< Spinlock for add/delete. */
+	uint32_t num_handlers; /**< Number of handlers in the table. */
+	/** Storage for all possible handlers. */
+	struct rte_mempool_handler handler[RTE_MEMPOOL_MAX_HANDLER_IDX];
+};

The handlers are implemented as an array due to multi-process access. Is it correct? I'd expect a note about it here.

Quoted 7 lines
lib/librte_mempool/rte_mempool.h +7 -0
+
+/** Array of registered handlers */
+extern struct rte_mempool_handler_table rte_mempool_handler_table;
+
+/**
+ * @internal Get the mempool handler from its index.
+ *

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 389

Quoted 10 lines
lib/librte_mempool/rte_mempool.h +10 -0
+ * @param handler_idx
+ *   The index of the handler in the handler table. It must be a valid
+ *   index: (0 <= idx < num_handlers).
+ * @return
+ *   The pointer to the handler in the table.
+ */
+static struct rte_mempool_handler *
+rte_mempool_handler_get(int handler_idx)
+{
+	return &rte_mempool_handler_table.handler[handler_idx];

Is it always safe? Can we belive the handler_idx is inside the boundaries? At least some RTE_VERIFY would be nice here...

Quoted 15 lines
lib/librte_mempool/rte_mempool.h +15 -0
+}
+
+/**
+ * @internal wrapper for external mempool manager alloc callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @return
+ *   The opaque pointer to the external pool.
+ */
+void *
+rte_mempool_ext_alloc(struct rte_mempool *mp);
+
+/**
+ * @internal wrapper for external mempool manager get callback.

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 414

Quoted 10 lines
lib/librte_mempool/rte_mempool.h +10 -0
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @param obj_table
+ *   Pointer to a table of void * pointers (objects).
+ * @param n
+ *   Number of objects to get.
+ * @return
+ *   - 0: Success; got n objects.
+ *   - <0: Error; code of handler get function.

Should this doc be more specific about the possible failures?

Quoted 12 lines
lib/librte_mempool/rte_mempool.h +12 -0
+ */
+static inline int
+rte_mempool_ext_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
+{
+	struct rte_mempool_handler *handler;
+
+	handler = rte_mempool_handler_get(mp->handler_idx);
+	return handler->get(mp->pool, obj_table, n);
+}
+
+/**
+ * @internal wrapper for external mempool manager put callback.

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 436

Quoted 10 lines
lib/librte_mempool/rte_mempool.h +10 -0
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @param obj_table
+ *   Pointer to a table of void * pointers (objects).
+ * @param n
+ *   Number of objects to put.
+ * @return
+ *   - 0: Success; n objects supplied.
+ *   - <0: Error; code of handler put function.

Should this doc be more specific about the possible failures?

Quoted 10 lines
lib/librte_mempool/rte_mempool.h +10 -0
+ */
+static inline int
+rte_mempool_ext_put_bulk(struct rte_mempool *mp, void * const *obj_table,
+		unsigned n)
+{
+	struct rte_mempool_handler *handler;
+
+	handler = rte_mempool_handler_get(mp->handler_idx);
+	return handler->put(mp->pool, obj_table, n);
+}

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 456

Quoted 10 lines
lib/librte_mempool/rte_mempool.h +10 -0
+
+/**
+ * @internal wrapper for external mempool manager get_count callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @return
+ *   The number of available objects in the external pool.
+ */
+unsigned

unsigned int

Quoted 14 lines
lib/librte_mempool/rte_mempool.h +14 -0
+rte_mempool_ext_get_count(const struct rte_mempool *mp);
+
+/**
+ * @internal wrapper for external mempool manager free callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ */
+void
+rte_mempool_ext_free(struct rte_mempool *mp);
+
+/**
+ * Set the handler of a mempool
+ *

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 480

Quoted 10 lines
lib/librte_mempool/rte_mempool.h +10 -0
+ * This can only be done on a mempool that is not populated, i.e. just after
+ * a call to rte_mempool_create_empty().
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @param name
+ *   Name of the handler.
+ * @return
+ *   - 0: Sucess; the new handler is configured.
+ *   - <0: Error (errno)

Should this doc be more specific about the possible failures?

The body of rte_mempool_set_handler does not set errno at all. It returns e.g. -EEXIST.

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 492

Quoted 12 lines
lib/librte_mempool/rte_mempool.h +12 -0
+ */
+int
+rte_mempool_set_handler(struct rte_mempool *mp, const char *name);
+
+/**
+ * Register an external pool handler.
+ *
+ * @param h
+ *   Pointer to the external pool handler
+ * @return
+ *   - >=0: Sucess; return the index of the handler in the table.
+ *   - <0: Error (errno)

Should this doc be more specific about the possible failures?

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 505

Quoted 13 lines
lib/librte_mempool/rte_mempool.h +13 -0
+ */
+int rte_mempool_handler_register(struct rte_mempool_handler *h);
+
+/**
+ * Macro to statically register an external pool handler.
+ */
+#define MEMPOOL_REGISTER_HANDLER(h)					\
+	void mp_hdlr_init_##h(void);					\
+	void __attribute__((constructor, used)) mp_hdlr_init_##h(void)	\
+	{								\
+		rte_mempool_handler_register(&h);			\
+	}
+

There might be a little catch. If there is no more room for handlers, calling the rte_mempool_handler_register would fail silently as the error reporting does not work when calling a constructor (or at least, this is my experience).

Not a big deal but...

Quoted 33 lines
lib/librte_mempool/rte_mempool.h +2 -9
 /**
  * An object callback function for mempool.
  *
@@ -736,7 +914,7 @@ void rte_mempool_dump(FILE *f, struct rte_mempool *mp);
  */
 static inline void __attribute__((always_inline))
 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
-		    unsigned n, int is_mp)
+		    unsigned n, __rte_unused int is_mp)
 {
 	struct rte_mempool_cache *cache;
 	uint32_t index;
@@ -774,7 +952,7 @@ __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
 	cache->len += n;
 
 	if (cache->len >= flushthresh) {
-		rte_ring_mp_enqueue_bulk(mp->ring, &cache->objs[cache_size],
+		rte_mempool_ext_put_bulk(mp, &cache->objs[cache_size],
 				cache->len - cache_size);
 		cache->len = cache_size;
 	}
@@ -782,26 +960,10 @@ __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
 	return;
 
 ring_enqueue:
-
 	/* push remaining objects in ring */
-#ifdef RTE_LIBRTE_MEMPOOL_DEBUG
-	if (is_mp) {
-		if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
-			rte_panic("cannot put objects in mempool\n");
-	}
-	else {

lib/librte_mempool/rte_mempool.h __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table, · line 964

Quoted 10 lines
lib/librte_mempool/rte_mempool.h +1 -9
-		if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
-			rte_panic("cannot put objects in mempool\n");
-	}
-#else
-	if (is_mp)
-		rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
-	else
-		rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
-#endif
+	rte_mempool_ext_put_bulk(mp, obj_table, n);

This is a big change. Does it remove the RTE_LIBRTE_MEMPOOL_DEBUG config option entirely? If so, I suggest to first do this in a separated patch and then replace the original *_enqueue_bulk by your *_ext_put_bulk (or better *_ops_put_bulk as I explain below).

Quoted 176 lines
lib/librte_mempool/rte_mempool.h +4 -7
 }
 
-
 /**
  * Put several objects back in the mempool (multi-producers safe).
  *
@@ -922,7 +1084,7 @@ rte_mempool_put(struct rte_mempool *mp, void *obj)
  */
 static inline int __attribute__((always_inline))
 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
-		   unsigned n, int is_mc)
+		   unsigned n, __rte_unused int is_mc)
 {
 	int ret;
 	struct rte_mempool_cache *cache;
@@ -945,7 +1107,8 @@ __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
 		uint32_t req = n + (cache_size - cache->len);
 
 		/* How many do we require i.e. number to fill the cache + the request */
-		ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
+		ret = rte_mempool_ext_get_bulk(mp,
+			&cache->objs[cache->len], req);
 		if (unlikely(ret < 0)) {
 			/*
 			 * In the offchance that we are buffer constrained,
@@ -972,10 +1135,7 @@ __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
 ring_dequeue:
 
 	/* get remaining objects from ring */
-	if (is_mc)
-		ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
-	else
-		ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
+	ret = rte_mempool_ext_get_bulk(mp, obj_table, n);
 
 	if (ret < 0)
 		__MEMPOOL_STAT_ADD(mp, get_fail, n);
lib/librte_mempool/rte_mempool_default.c new file +133 -0
new file mode 100644
index 0000000..a6ac65a
@@ -0,0 +1,147 @@
+/*-
+ *   BSD LICENSE
+ *
+ *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include <rte_errno.h>
+#include <rte_ring.h>
+#include <rte_mempool.h>
+
+static int
+common_ring_mp_put(void *p, void * const *obj_table, unsigned n)
+{
+	return rte_ring_mp_enqueue_bulk((struct rte_ring *)p, obj_table, n);
+}
+
+static int
+common_ring_sp_put(void *p, void * const *obj_table, unsigned n)
+{
+	return rte_ring_sp_enqueue_bulk((struct rte_ring *)p, obj_table, n);
+}
+
+static int
+common_ring_mc_get(void *p, void **obj_table, unsigned n)
+{
+	return rte_ring_mc_dequeue_bulk((struct rte_ring *)p, obj_table, n);
+}
+
+static int
+common_ring_sc_get(void *p, void **obj_table, unsigned n)
+{
+	return rte_ring_sc_dequeue_bulk((struct rte_ring *)p, obj_table, n);
+}
+
+static unsigned
+common_ring_get_count(void *p)
+{
+	return rte_ring_count((struct rte_ring *)p);
+}
+
+
+static void *
+common_ring_alloc(struct rte_mempool *mp)
+{
+	int rg_flags = 0, ret;
+	char rg_name[RTE_RING_NAMESIZE];
+	struct rte_ring *r;
+
+	ret = snprintf(rg_name, sizeof(rg_name),
+		RTE_MEMPOOL_MZ_FORMAT, mp->name);
+	if (ret < 0 || ret >= (int)sizeof(rg_name)) {
+		rte_errno = ENAMETOOLONG;
+		return NULL;
+	}
+
+	/* ring flags */
+	if (mp->flags & MEMPOOL_F_SP_PUT)
+		rg_flags |= RING_F_SP_ENQ;
+	if (mp->flags & MEMPOOL_F_SC_GET)
+		rg_flags |= RING_F_SC_DEQ;
+
+	/* Allocate the ring that will be used to store objects.
+	 * Ring functions will return appropriate errors if we are
+	 * running as a secondary process etc., so no checks made
+	 * in this function for that condition. */
+	r = rte_ring_create(rg_name, rte_align32pow2(mp->size + 1),
+		mp->socket_id, rg_flags);
+
+	return r;
+}
+
+static void
+common_ring_free(void *p)
+{
+	rte_ring_free((struct rte_ring *)p);
+}
+
+static struct rte_mempool_handler handler_mp_mc = {
+	.name = "ring_mp_mc",
+	.alloc = common_ring_alloc,
+	.free = common_ring_free,
+	.put = common_ring_mp_put,
+	.get = common_ring_mc_get,
+	.get_count = common_ring_get_count,
+};
+
+static struct rte_mempool_handler handler_sp_sc = {
+	.name = "ring_sp_sc",
+	.alloc = common_ring_alloc,
+	.free = common_ring_free,
+	.put = common_ring_sp_put,
+	.get = common_ring_sc_get,
+	.get_count = common_ring_get_count,
+};
+
+static struct rte_mempool_handler handler_mp_sc = {
+	.name = "ring_mp_sc",
+	.alloc = common_ring_alloc,
+	.free = common_ring_free,
+	.put = common_ring_mp_put,
+	.get = common_ring_sc_get,
+	.get_count = common_ring_get_count,
+};

lib/librte_mempool/rte_mempool_default.c line 143

Quoted 10 lines
lib/librte_mempool/rte_mempool_default.c new file +10 -0
+
+static struct rte_mempool_handler handler_sp_mc = {
+	.name = "ring_sp_mc",
+	.alloc = common_ring_alloc,
+	.free = common_ring_free,
+	.put = common_ring_sp_put,
+	.get = common_ring_mc_get,
+	.get_count = common_ring_get_count,
+};
+

Introducing those handlers can go as a separate patch. IMHO, that would simplify the review process a lot. First introduce the mechanism, then add something inside.

I'd also note that those handlers are always available and what kind of memory do they use...

Quoted 46 lines
lib/librte_mempool/rte_mempool_default.c new file +4 -0
+MEMPOOL_REGISTER_HANDLER(handler_mp_mc);
+MEMPOOL_REGISTER_HANDLER(handler_sp_sc);
+MEMPOOL_REGISTER_HANDLER(handler_mp_sc);
+MEMPOOL_REGISTER_HANDLER(handler_sp_mc);
lib/librte_mempool/rte_mempool_handler.c new file +36 -0
new file mode 100644
index 0000000..78611f8
@@ -0,0 +1,139 @@
+/*-
+ *   BSD LICENSE
+ *
+ *   Copyright(c) 2016 Intel Corporation. All rights reserved.
+ *   Copyright(c) 2016 6WIND S.A.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <string.h>

lib/librte_mempool/rte_mempool_handler.c line 46

Quoted 10 lines
lib/librte_mempool/rte_mempool_handler.c new file +10 -0
+
+#include <rte_mempool.h>
+
+/* indirect jump table to support external memory pools */
+struct rte_mempool_handler_table rte_mempool_handler_table = {
+	.sl =  RTE_SPINLOCK_INITIALIZER ,
+	.num_handlers = 0
+};
+
+/* add a new handler in rte_mempool_handler_table, return its index */

It seems to me that there is no way how to put some opaque pointer into the handler. In such case I would expect I can do something like:

struct my_handler {
	struct rte_mempool_handler h;
	...
} handler;

rte_mempool_handler_register(&handler.h);

But I cannot because you copy the contents of the handler. By the way, this should be documented.

How can I pass an opaque pointer here? The only way I see is through the rte_mempool.pool. In that case, what about renaming the rte_mempool_handler to rte_mempool_ops? Because semantically, it is not a handler, it just holds the operations.

This would improve some namings:

rte_mempool_ext_alloc -> rte_mempool_ops_alloc rte_mempool_ext_free -> rte_mempool_ops_free rte_mempool_ext_get_count -> rte_mempool_ops_get_count rte_mempool_handler_register -> rte_mempool_ops_register

seems to be more readable to me. The *_ext_* mark does not say anything valuable. It just scares a bit :).

Quoted 60 lines
lib/librte_mempool/rte_mempool_handler.c new file +60 -0
+int
+rte_mempool_handler_register(struct rte_mempool_handler *h)
+{
+	struct rte_mempool_handler *handler;
+	int16_t handler_idx;
+
+	rte_spinlock_lock(&rte_mempool_handler_table.sl);
+
+	if (rte_mempool_handler_table.num_handlers >= RTE_MEMPOOL_MAX_HANDLER_IDX) {
+		rte_spinlock_unlock(&rte_mempool_handler_table.sl);
+		RTE_LOG(ERR, MEMPOOL,
+			"Maximum number of mempool handlers exceeded\n");
+		return -ENOSPC;
+	}
+
+	if (h->put == NULL || h->get == NULL || h->get_count == NULL) {
+		rte_spinlock_unlock(&rte_mempool_handler_table.sl);
+		RTE_LOG(ERR, MEMPOOL,
+			"Missing callback while registering mempool handler\n");
+		return -EINVAL;
+	}
+
+	handler_idx = rte_mempool_handler_table.num_handlers++;
+	handler = &rte_mempool_handler_table.handler[handler_idx];
+	snprintf(handler->name, sizeof(handler->name), "%s", h->name);
+	handler->alloc = h->alloc;
+	handler->put = h->put;
+	handler->get = h->get;
+	handler->get_count = h->get_count;
+
+	rte_spinlock_unlock(&rte_mempool_handler_table.sl);
+
+	return handler_idx;
+}
+
+/* wrapper to allocate an external pool handler */
+void *
+rte_mempool_ext_alloc(struct rte_mempool *mp)
+{
+	struct rte_mempool_handler *handler;
+
+	handler = rte_mempool_handler_get(mp->handler_idx);
+	if (handler->alloc == NULL)
+		return NULL;
+	return handler->alloc(mp);
+}
+
+/* wrapper to free an external pool handler */
+void
+rte_mempool_ext_free(struct rte_mempool *mp)
+{
+	struct rte_mempool_handler *handler;
+
+	handler = rte_mempool_handler_get(mp->handler_idx);
+	if (handler->free == NULL)
+		return;
+	return handler->free(mp);
+}
+
+/* wrapper to get available objects in an external pool handler */

lib/librte_mempool/rte_mempool_handler.c line 116

Quoted 10 lines
lib/librte_mempool/rte_mempool_handler.c new file +10 -0
+unsigned
+rte_mempool_ext_get_count(const struct rte_mempool *mp)
+{
+	struct rte_mempool_handler *handler;
+
+	handler = rte_mempool_handler_get(mp->handler_idx);
+	return handler->get_count(mp->pool);
+}
+
+/* set the handler of a mempool */

The doc comment should say "this sets a handler previously registered by the rte_mempool_handler_register function ...". I was confused and didn't understand how the handlers are inserted into the table.

Quoted 37 lines
lib/librte_mempool/rte_mempool_handler.c new file +23 -0
+int
+rte_mempool_set_handler(struct rte_mempool *mp, const char *name)
+{
+	struct rte_mempool_handler *handler = NULL;
+	unsigned i;
+
+	/* too late, the mempool is already populated */
+	if (mp->flags & MEMPOOL_F_RING_CREATED)
+		return -EEXIST;
+
+	for (i = 0; i < rte_mempool_handler_table.num_handlers; i++) {
+		if (!strcmp(name, rte_mempool_handler_table.handler[i].name)) {
+			handler = &rte_mempool_handler_table.handler[i];
+			break;
+		}
+	}
+
+	if (handler == NULL)
+		return -EINVAL;
+
+	mp->handler_idx = i;
+	return 0;
+}
lib/librte_mempool/rte_mempool_version.map +2 -0
index f63461b..a0e9aed 100644
@@ -19,6 +19,8 @@ DPDK_2.0 {
 DPDK_16.7 {
 	global:
 
+	rte_mempool_handler_table;
+
 	rte_mempool_check_cookies;
 	rte_mempool_obj_iter;
 	rte_mempool_mem_iter;

lib/librte_mempool/rte_mempool_version.map DPDK_16.7 { · line 38

Quoted 8 lines
lib/librte_mempool/rte_mempool_version.map +2 -0
@@ -29,6 +31,8 @@ DPDK_16.7 {
 	rte_mempool_populate_default;
 	rte_mempool_populate_anon;
 	rte_mempool_free;
+	rte_mempool_set_handler;
+	rte_mempool_handler_register;
 
 	local: *;
 } DPDK_2.0;

Regards
Jan

#66 lore raw

Re: [dpdk-dev,v5,1/3] mempool: support external handler

Quoted 7 lines · Jan Viktorin

Hello David,

please, see my comments inline.

I didn't see the previous versions of the mempool (well, only very roughly) so I am probably missing some points... My point of view is as a user of the handler API. I need to understand the API to implement a custom handler for my purposes.

Thanks for the review, Jan.

I'm working on the changes now, will post soon. I'll reply to each of you're emails when I'm ready with the patch.

Regards,
David.

#67 lore raw

Re: [dpdk-dev,v5,1/3] mempool: support external handler

In reply to Jan Viktorin

Hi Jan,

Quoted 4 lines · Jan Viktorin
Quoted 1 line

Until now, the objects stored in mempool mempool were internally stored a

s/mempool mempool/mempool/

stored _in_ a ring?

Fixed.

Quoted 10 lines
app/test/test_mempool_perf.c +0 -1
@@ -161,7 +161,6 @@ per_lcore_mempool_test(__attribute__((unused)) void *arg)
  							   n_get_bulk);
  				if (unlikely(ret < 0)) {
  					rte_mempool_dump(stdout, mp);
-					rte_ring_dump(stdout, mp->ring);
  					/* in this case, objects are lost... */
  					return -1;
  				}

I think, this should be in a separate patch explaining the reason to remove it.

Done. Moved.

Quoted 20 lines
Quoted 20 lines
lib/librte_mempool/Makefile +2 -0
index 43423e0..f19366e 100644
@@ -42,6 +42,8 @@ LIBABIVER := 2
 
  # all source are stored in SRCS-y
  SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_handler.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_default.c
  # install includes
  SYMLINK-$(CONFIG_RTE_LIBRTE_MEMPOOL)-include := rte_mempool.h
 
lib/librte_mempool/rte_mempool.c +0 -0
index 1ab6701..6ec2b3f 100644
@@ -148,7 +148,7 @@ mempool_add_elem(struct rte_mempool *mp, void *obj, phys_addr_t physaddr)
  #endif
 
Quoted 5 lines

lib/librte_mempool/rte_mempool.c mempool_add_elem(struct rte_mempool *mp, void *obj, phys_addr_t physaddr) · line 151

Quoted 3 lines
lib/librte_mempool/rte_mempool.c +1 -1
  	/* enqueue in ring */
-	rte_ring_sp_enqueue(mp->ring, obj);
+	rte_mempool_ext_put_bulk(mp, &obj, 1);

I suppose this is OK, however, replacing "enqueue" by "put" (semantically) sounds to me like a bug. Enqueue is put into a queue. Put is to drop a reference.

Yes, Makes sense. Changed 'put' and 'get' to 'enqueue' and 'dequeue'

Quoted 30 lines
Quoted 30 lines
lib/librte_mempool/rte_mempool.c +0 -28
-/* create the internal ring */
-static int
-rte_mempool_ring_create(struct rte_mempool *mp)
-{
-	int rg_flags = 0, ret;
-	char rg_name[RTE_RING_NAMESIZE];
-	struct rte_ring *r;
-
-	ret = snprintf(rg_name, sizeof(rg_name),
-		RTE_MEMPOOL_MZ_FORMAT, mp->name);
-	if (ret < 0 || ret >= (int)sizeof(rg_name))
-		return -ENAMETOOLONG;
-
-	/* ring flags */
-	if (mp->flags & MEMPOOL_F_SP_PUT)
-		rg_flags |= RING_F_SP_ENQ;
-	if (mp->flags & MEMPOOL_F_SC_GET)
-		rg_flags |= RING_F_SC_DEQ;
-
-	/* Allocate the ring that will be used to store objects.
-	 * Ring functions will return appropriate errors if we are
-	 * running as a secondary process etc., so no checks made
-	 * in this function for that condition.
-	 */
-	r = rte_ring_create(rg_name, rte_align32pow2(mp->size + 1),
-		mp->socket_id, rg_flags);
-	if (r == NULL)
-		return -rte_errno;
Quoted 8 lines

lib/librte_mempool/rte_mempool.c rte_mempool_xmem_usage(__rte_unused void *vaddr, uint32_t elt_num, · line 335

Quoted 5 lines
lib/librte_mempool/rte_mempool.c +0 -5
-
-	mp->ring = r;
-	mp->flags |= MEMPOOL_F_RING_CREATED;
-	return 0;
-}

This is a big change. I suggest (if possible) to make a separate patch with something like "replace rte_mempool_ring_create by ...". Where is this code placed now?

The code is not gone away, it's now part of the default handler, which uses a ring. It's in rte_mempool_default.c

Quoted 7 lines
Quoted 7 lines
lib/librte_mempool/rte_mempool.c +0 -1
-
  /* free a memchunk allocated with rte_memzone_reserve() */
  static void
  rte_mempool_memchunk_mz_free(__rte_unused struct rte_mempool_memhdr *memhdr,
@@ -351,7 +317,7 @@ rte_mempool_free_memchunks(struct rte_mempool *mp)
  	void *elt;
 
Quoted 5 lines

lib/librte_mempool/rte_mempool.c rte_mempool_free_memchunks(struct rte_mempool *mp) · line 320

Quoted 3 lines
lib/librte_mempool/rte_mempool.c +1 -1
  	while (!STAILQ_EMPTY(&mp->elt_list)) {
-		rte_ring_sc_dequeue(mp->ring, &elt);
+		rte_mempool_ext_get_bulk(mp, &elt, 1);

Similar as for put_bulk... Replacing "dequeue" by "get" (semantically) sounds to me like a bug. Dequeue is drop from a queue. Get is to obtain a reference.

Done

Quoted 9 lines
Quoted 9 lines
lib/librte_mempool/rte_mempool.c +0 -1
  		(void)elt;
  		STAILQ_REMOVE_HEAD(&mp->elt_list, next);
  		mp->populated_size--;
@@ -380,15 +346,18 @@ rte_mempool_populate_phys(struct rte_mempool *mp, char *vaddr,
  	unsigned i = 0;
  	size_t off;
  	struct rte_mempool_memhdr *memhdr;
-	int ret;
 
Quoted 16 lines
Quoted 5 lines
lib/librte_mempool/rte_mempool.c +0 -3
  	/* create the internal ring if not already done */
  	if ((mp->flags & MEMPOOL_F_RING_CREATED) == 0) {
-		ret = rte_mempool_ring_create(mp);
-		if (ret < 0)
-			return ret;

lib/librte_mempool/rte_mempool.c rte_mempool_populate_phys(struct rte_mempool *mp, char *vaddr, · line 391

Quoted 10 lines
lib/librte_mempool/rte_mempool.c +8 -1
+		rte_errno = 0;
+		mp->pool = rte_mempool_ext_alloc(mp);
+		if (mp->pool == NULL) {
+			if (rte_errno == 0)
+				return -EINVAL;
+			else
+				return -rte_errno;
+		}
  	}
-

Is this a whitespace change?

Accidental. Reverted

Quoted 8 lines
Quoted 8 lines
lib/librte_mempool/rte_mempool.c +8 -0
+
+	/*
+	 * Since we have 4 combinations of the SP/SC/MP/MC examine the flags to
+	 * set the correct index into the handler table.
+	 */
+	if (flags & (MEMPOOL_F_SP_PUT | MEMPOOL_F_SC_GET))
+		rte_mempool_set_handler(mp, "ring_sp_sc");
+	else if (flags & MEMPOOL_F_SP_PUT)
Quoted 8 lines

lib/librte_mempool/rte_mempool.c rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, · line 797

Quoted 6 lines
lib/librte_mempool/rte_mempool.c +6 -0
+		rte_mempool_set_handler(mp, "ring_sp_mc");
+	else if (flags & MEMPOOL_F_SC_GET)
+		rte_mempool_set_handler(mp, "ring_mp_sc");
+	else
+		rte_mempool_set_handler(mp, "ring_mp_mc");
+

Do I understand it well that this code preserves behaviour of the previous API? Because otherwise it looks strange.

Yes. it's just to keep backward compatibility. It will also move to somewhere more sensible in the latest patch, rte_mempool_create rather than rte_mempool_create_empty.

Quoted 5 lines
Quoted 5 lines
  struct rte_mempool {
  	char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
lib/librte_mempool/rte_mempool.c +2 -1
-	struct rte_ring *ring;           /**< Ring to store objects. */
+	void *pool;                      /**< Ring or ext-pool to store objects. */
+	/**
Quoted 8 lines

lib/librte_mempool/rte_mempool.c

Quoted 6 lines
lib/librte_mempool/rte_mempool.c +6 -0
+	 * Index into the array of structs containing callback fn pointers.
+	 * We're using an index here rather than pointers to the callbacks
+	 * to facilitate any secondary processes that may want to use
+	 * this mempool. Any function pointers stored in the mempool
+	 * directly would not be valid for secondary processes.
+	 */

I think, this comment should go to the rte_mempool_handler_table definition leaving a here a short note about it.

I've added a comment to rte_mempool_handler_table, and tweaked this comment somewhat.

Quoted 11 lines

lib/librte_mempool/rte_mempool.c void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 339

Quoted 11 lines
lib/librte_mempool/rte_mempool.c +4 -0
+	int32_t handler_idx;
  	const struct rte_memzone *mz;    /**< Memzone where pool is allocated */
  	int flags;                       /**< Flags of the mempool. */
  	int socket_id;                   /**< Socket id passed at mempool creation. */
@@ -325,6 +334,175 @@ void rte_mempool_check_cookies(const struct rte_mempool *mp,
  #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
  #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
 
+#define RTE_MEMPOOL_HANDLER_NAMESIZE 32 /**< Max length of handler name. */
+
+/** Allocate the external pool. */

Note: for the next few comments you switched to commenting above the code , I've moved the comments to below the code, and replied.

Quoted 8 lines

lib/librte_mempool/rte_mempool.c void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 342

Quoted 3 lines
lib/librte_mempool/rte_mempool.c +3 -0
+typedef void *(*rte_mempool_alloc_t)(struct rte_mempool *mp);
+
+/** Free the external pool. */

What is the purpose of this callback?
What exactly does it allocate?
Some rte_mempool internals?
Or the memory?
What does it return?

This is the main allocate function of the handler. It is up to the mempool handlers control. The handler's alloc function does whatever it needs to do to grab memory for this handler, and places a pointer to it in the *pool opaque pointer in the rte_mempool struct. In the default handler, *pool points to a ring, in other handlers, it will mostlikely point to a different type of data structure. It will be transparent to the application programmer.

Quoted 5 lines

lib/librte_mempool/rte_mempool.c void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 345

Quoted 3 lines
lib/librte_mempool/rte_mempool.c +3 -0
+typedef void (*rte_mempool_free_t)(void *p);
+
+/** Put an object in the external pool. */

Why this *_free callback does not accept the rte_mempool param?

We're freeing the pool opaque data here.

Quoted 6 lines

lib/librte_mempool/rte_mempool.c void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 346

Quoted 1 line
lib/librte_mempool/rte_mempool.c +1 -0
+typedef int (*rte_mempool_put_t)(void *p, void * const *obj_table, unsigned n);

What is the *p pointer?
What is the obj_table?
Why is it void *?
Why is it const?

The *p pointer is the opaque data for a given mempool handler (ring, array, linked list, etc)

Quoted 6 lines

Probably, "unsigned int n" is better.

lib/librte_mempool/rte_mempool.c void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 349

Quoted 3 lines
lib/librte_mempool/rte_mempool.c +3 -0
+
+/** Get an object from the external pool. */
+typedef int (*rte_mempool_get_t)(void *p, void **obj_table, unsigned n);

Probably, "unsigned int n" is better.

Done.

Quoted 4 lines

lib/librte_mempool/rte_mempool.c void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 351

Quoted 2 lines
lib/librte_mempool/rte_mempool.c +2 -0
+
+/** Return the number of available objects in the external pool. */

What is the purpose of the *_get_count callback? I guess it can introduce race conditions...

I think it depends on the implementation of the handlers get_count function, it must ensure not to return values greater than the size of the mempool.

Quoted 2 lines

lib/librte_mempool/rte_mempool.c void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 352

Quoted 1 line
lib/librte_mempool/rte_mempool.c +1 -0
+typedef unsigned (*rte_mempool_get_count)(void *p);

unsigned int

Sure.

Quoted 5 lines

lib/librte_mempool/rte_mempool.c void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 354

Quoted 2 lines
lib/librte_mempool/rte_mempool.c +2 -0
+
+/** Structure defining a mempool handler. */

Later in the text, I suggested to rename rte_mempool_handler to rte_mempool_ops. I believe that it explains the purpose of this struct better. It would improve consistency in function names (the *_ext_* mark is very strange and inconsistent).

I agree. I've gone through all the code and renamed to rte_mempool_handler_ops.

Quoted 12 lines
Quoted 12 lines
lib/librte_mempool/rte_mempool.c +12 -0
+struct rte_mempool_handler {
+	char name[RTE_MEMPOOL_HANDLER_NAMESIZE]; /**< Name of mempool handler */
+	rte_mempool_alloc_t alloc;       /**< Allocate the external pool. */
+	rte_mempool_free_t free;         /**< Free the external pool. */
+	rte_mempool_put_t put;           /**< Put an object. */
+	rte_mempool_get_t get;           /**< Get an object. */
+	rte_mempool_get_count get_count; /**< Get the number of available objs. */
+} __rte_cache_aligned;
+
+#define RTE_MEMPOOL_MAX_HANDLER_IDX 16  /**< Max number of registered handlers */
+
+/** Structure storing the table of registered handlers. */
Quoted 8 lines

lib/librte_mempool/rte_mempool.c void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 372

Quoted 6 lines
lib/librte_mempool/rte_mempool.c +6 -0
+struct rte_mempool_handler_table {
+	rte_spinlock_t sl;     /**< Spinlock for add/delete. */
+	uint32_t num_handlers; /**< Number of handlers in the table. */
+	/** Storage for all possible handlers. */
+	struct rte_mempool_handler handler[RTE_MEMPOOL_MAX_HANDLER_IDX];
+};

The handlers are implemented as an array due to multi-process access. Is it correct? I'd expect a note about it here.

Yes, you are correct. I've improved the comments.

Quoted 11 lines
Quoted 11 lines
lib/librte_mempool/rte_mempool.c +11 -0
+
+/** Array of registered handlers */
+extern struct rte_mempool_handler_table rte_mempool_handler_table;
+
+/**
+ * @internal Get the mempool handler from its index.
+ *
+ * @param handler_idx
+ *   The index of the handler in the handler table. It must be a valid
+ *   index: (0 <= idx < num_handlers).
+ * @return
Quoted 8 lines

lib/librte_mempool/rte_mempool.c void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 389

Quoted 6 lines
lib/librte_mempool/rte_mempool.c +6 -0
+ *   The pointer to the handler in the table.
+ */
+static struct rte_mempool_handler *
+rte_mempool_handler_get(int handler_idx)
+{
+	return &rte_mempool_handler_table.handler[handler_idx];

Is it always safe? Can we belive the handler_idx is inside the boundaries? At least some RTE_VERIFY would be nice here...

Agreed. Added:
RTE_VERIFY(handler_idx < RTE_MEMPOOL_MAX_HANDLER_IDX);

Quoted 18 lines
Quoted 18 lines
lib/librte_mempool/rte_mempool.c +18 -0
+}
+
+/**
+ * @internal wrapper for external mempool manager alloc callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @return
+ *   The opaque pointer to the external pool.
+ */
+void *
+rte_mempool_ext_alloc(struct rte_mempool *mp);
+
+/**
+ * @internal wrapper for external mempool manager get callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
Quoted 8 lines

lib/librte_mempool/rte_mempool.c void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 414

Quoted 7 lines
lib/librte_mempool/rte_mempool.c +7 -0
+ * @param obj_table
+ *   Pointer to a table of void * pointers (objects).
+ * @param n
+ *   Number of objects to get.
+ * @return
+ *   - 0: Success; got n objects.
+ *   - <0: Error; code of handler get function.

Should this doc be more specific about the possible failures?

This is up to the handler. We cannot know what codes will be returned at this stage.

Quoted 15 lines
Quoted 15 lines
lib/librte_mempool/rte_mempool.c +15 -0
+ */
+static inline int
+rte_mempool_ext_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
+{
+	struct rte_mempool_handler *handler;
+
+	handler = rte_mempool_handler_get(mp->handler_idx);
+	return handler->get(mp->pool, obj_table, n);
+}
+
+/**
+ * @internal wrapper for external mempool manager put callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
Quoted 8 lines

lib/librte_mempool/rte_mempool.c void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 436

Quoted 7 lines
lib/librte_mempool/rte_mempool.c +7 -0
+ * @param obj_table
+ *   Pointer to a table of void * pointers (objects).
+ * @param n
+ *   Number of objects to put.
+ * @return
+ *   - 0: Success; n objects supplied.
+ *   - <0: Error; code of handler put function.

Should this doc be more specific about the possible failures?

This is up to the handler. We cannot know what codes will be returned at this stage.

Quoted 13 lines
Quoted 13 lines
lib/librte_mempool/rte_mempool.c +13 -0
+ */
+static inline int
+rte_mempool_ext_put_bulk(struct rte_mempool *mp, void * const *obj_table,
+		unsigned n)
+{
+	struct rte_mempool_handler *handler;
+
+	handler = rte_mempool_handler_get(mp->handler_idx);
+	return handler->put(mp->pool, obj_table, n);
+}
+
+/**
+ * @internal wrapper for external mempool manager get_count callback.
Quoted 8 lines

lib/librte_mempool/rte_mempool.c void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 456

Quoted 7 lines
lib/librte_mempool/rte_mempool.c +7 -0
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @return
+ *   The number of available objects in the external pool.
+ */
+unsigned

unsigned int

Done.

Quoted 26 lines
Quoted 14 lines
lib/librte_mempool/rte_mempool.c +14 -0
+rte_mempool_ext_get_count(const struct rte_mempool *mp);
+
+/**
+ * @internal wrapper for external mempool manager free callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ */
+void
+rte_mempool_ext_free(struct rte_mempool *mp);
+
+/**
+ * Set the handler of a mempool
+ *

lib/librte_mempool/rte_mempool.c void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 480

Quoted 10 lines
lib/librte_mempool/rte_mempool.c +10 -0
+ * This can only be done on a mempool that is not populated, i.e. just after
+ * a call to rte_mempool_create_empty().
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @param name
+ *   Name of the handler.
+ * @return
+ *   - 0: Sucess; the new handler is configured.
+ *   - <0: Error (errno)

Should this doc be more specific about the possible failures?

Quoted 2 lines

The body of rte_mempool_set_handler does not set errno at all. It returns e.g. -EEXIST.

This is up to the handler. We cannot know what codes will be returned at this stage. errno was a cut-and paste error, this is now fixed.

Quoted 5 lines
Quoted 5 lines
lib/librte_mempool/rte_mempool.c +5 -0
+ */
+int
+rte_mempool_set_handler(struct rte_mempool *mp, const char *name);
+
+/**
Quoted 8 lines

lib/librte_mempool/rte_mempool.c void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 492

Quoted 7 lines
lib/librte_mempool/rte_mempool.c +7 -0
+ * Register an external pool handler.
+ *
+ * @param h
+ *   Pointer to the external pool handler
+ * @return
+ *   - >=0: Sucess; return the index of the handler in the table.
+ *   - <0: Error (errno)

Should this doc be more specific about the possible failures?

This is up to the handler. We cannot know what codes will be returned at this stage. errno was a cut-and paste error, this is now fixed.

Quoted 17 lines

lib/librte_mempool/rte_mempool.c void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 505

Quoted 13 lines
lib/librte_mempool/rte_mempool.c +13 -0
+ */
+int rte_mempool_handler_register(struct rte_mempool_handler *h);
+
+/**
+ * Macro to statically register an external pool handler.
+ */
+#define MEMPOOL_REGISTER_HANDLER(h)					\
+	void mp_hdlr_init_##h(void);					\
+	void __attribute__((constructor, used)) mp_hdlr_init_##h(void)	\
+	{								\
+		rte_mempool_handler_register(&h);			\
+	}
+

There might be a little catch. If there is no more room for handlers, calling the rte_mempool_handler_register would fail silently as the error reporting does not work when calling a constructor (or at least, this is my experience).

Quoted 1 line

Not a big deal but...

I would hope that the developer would check this when adding new handlers. If there is no room for new handlers, then their new one will never work...

Quoted 16 lines
Quoted 16 lines
lib/librte_mempool/rte_mempool.c +0 -13
  ring_enqueue:
-
  	/* push remaining objects in ring */
-#ifdef RTE_LIBRTE_MEMPOOL_DEBUG
-	if (is_mp) {
-		if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
-			rte_panic("cannot put objects in mempool\n");
-	}
-	else {
-		if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
-			rte_panic("cannot put objects in mempool\n");
-	}
-#else
-	if (is_mp)
-		rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
Quoted 8 lines

lib/librte_mempool/rte_mempool.c

Quoted 4 lines
lib/librte_mempool/rte_mempool.c +1 -3
-	else
-		rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
-#endif
+	rte_mempool_ext_put_bulk(mp, obj_table, n);

This is a big change. Does it remove the RTE_LIBRTE_MEMPOOL_DEBUG config option entirely? If so, I suggest to first do this in a separated patch and then replace the original *_enqueue_bulk by your *_ext_put_bulk (or better *_ops_put_bulk as I explain below).

Well spotted. I have reverted this change. The mp_enqueue and sp_enqueue have been replaced with the handlers version, and I've added back in the DEBUG check with the rte_panic.

Quoted 42 lines
Quoted 28 lines
lib/librte_mempool/rte_mempool.c +27 -0
+
+static struct rte_mempool_handler handler_mp_mc = {
+	.name = "ring_mp_mc",
+	.alloc = common_ring_alloc,
+	.free = common_ring_free,
+	.put = common_ring_mp_put,
+	.get = common_ring_mc_get,
+	.get_count = common_ring_get_count,
+};
+
+static struct rte_mempool_handler handler_sp_sc = {
+	.name = "ring_sp_sc",
+	.alloc = common_ring_alloc,
+	.free = common_ring_free,
+	.put = common_ring_sp_put,
+	.get = common_ring_sc_get,
+	.get_count = common_ring_get_count,
+};
+
+static struct rte_mempool_handler handler_mp_sc = {
+	.name = "ring_mp_sc",
+	.alloc = common_ring_alloc,
+	.free = common_ring_free,
+	.put = common_ring_mp_put,
+	.get = common_ring_sc_get,
+	.get_count = common_ring_get_count,
+};

lib/librte_mempool/rte_mempool.c

Quoted 10 lines
lib/librte_mempool/rte_mempool.c +10 -0
+
+static struct rte_mempool_handler handler_sp_mc = {
+	.name = "ring_sp_mc",
+	.alloc = common_ring_alloc,
+	.free = common_ring_free,
+	.put = common_ring_sp_put,
+	.get = common_ring_mc_get,
+	.get_count = common_ring_get_count,
+};
+

Introducing those handlers can go as a separate patch. IMHO, that would simplify the review process a lot. First introduce the mechanism, then add something inside.

Quoted 2 lines

I'd also note that those handlers are always available and what kind of memory do they use...

Done. Now added as a separate patch.

They don't use any memory unless they are used.

Quoted 20 lines

lib/librte_mempool/rte_mempool.c

Quoted 12 lines
lib/librte_mempool/rte_mempool.c +12 -0
+#include <stdio.h>
+#include <string.h>
+
+#include <rte_mempool.h>
+
+/* indirect jump table to support external memory pools */
+struct rte_mempool_handler_table rte_mempool_handler_table = {
+	.sl =  RTE_SPINLOCK_INITIALIZER ,
+	.num_handlers = 0
+};
+
+/* add a new handler in rte_mempool_handler_table, return its index */

It seems to me that there is no way how to put some opaque pointer into the handler. In such case I would expect I can do something like:

struct my_handler {
	struct rte_mempool_handler h;
	...
} handler;
Quoted 7 lines

rte_mempool_handler_register(&handler.h);

But I cannot because you copy the contents of the handler. By the way, this should be documented.

How can I pass an opaque pointer here? The only way I see is through the rte_mempool.pool.

I think have addressed this in a later patch, in the discussions with Jerin on the list. But rather than passing data at register time, I pass it at create time (or rather set_handler). And a new config void has been added to the mempool struct for this purpose.

Quoted 6 lines

In that case, what about renaming the rte_mempool_handler
to rte_mempool_ops? Because semantically, it is not a handler, it just holds the operations.

This would improve some namings:

Quoted 7 lines

rte_mempool_ext_alloc -> rte_mempool_ops_alloc rte_mempool_ext_free -> rte_mempool_ops_free rte_mempool_ext_get_count -> rte_mempool_ops_get_count rte_mempool_handler_register -> rte_mempool_ops_register

seems to be more readable to me. The *_ext_* mark does not say anything valuable. It just scares a bit :).

Agreed. Makes sense. The ext was intended to be 'external', but that's a bit too generic, and not very intuitive. the 'ops' tag seems better to me. I've change this in the latest patch.

Quoted 6 lines
Quoted 6 lines
lib/librte_mempool/rte_mempool.c +6 -0
+/* wrapper to get available objects in an external pool handler */
+unsigned
+rte_mempool_ext_get_count(const struct rte_mempool *mp)
+{
+	struct rte_mempool_handler *handler;
+
Quoted 8 lines

lib/librte_mempool/rte_mempool.c

Quoted 5 lines
lib/librte_mempool/rte_mempool.c +5 -0
+	handler = rte_mempool_handler_get(mp->handler_idx);
+	return handler->get_count(mp->pool);
+}
+
+/* set the handler of a mempool */

The doc comment should say "this sets a handler previously registered by the rte_mempool_handler_register function ...". I was confused and didn't understand how the handlers are inserted into the table.

Done.

--snip--

Quoted 2 lines

Regards
Jan

Thanks, Jan. Very comprehensive. I'll hopefully be pushing the latest patch to the list later today (Tuesday 31st)

Regard,
Dave.

#68 lore raw

Re: [dpdk-dev,v5,1/3] mempool: support external handler

Quoted 2 lines · David

Hi Jan,

[...]

Quoted 10 lines
Quoted 9 lines

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 342

Quoted 4 lines
lib/librte_mempool/rte_mempool.h +3 -0
+typedef void *(*rte_mempool_alloc_t)(struct rte_mempool *mp);
+
+/** Free the external pool. */  

What is the purpose of this callback?
What exactly does it allocate?
Some rte_mempool internals?
Or the memory?
What does it return?

Quoted 9 lines

This is the main allocate function of the handler. It is up to the mempool handlers control.
The handler's alloc function does whatever it needs to do to grab memory for this handler, and places
a pointer to it in the *pool opaque pointer in the rte_mempool struct. In the default handler, *pool
points to a ring, in other handlers, it will mostlikely point to a different type of data structure. It will
be transparent to the application programmer.

Thanks for explanation. Please, add doc comments there.

Should the *mp be const here? It is not clear to me whether the callback is expected to modify the mempool or not (eg. set the pool pointer).

Quoted 8 lines
Quoted 6 lines

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 345

Quoted 4 lines
lib/librte_mempool/rte_mempool.h +3 -0
+typedef void (*rte_mempool_free_t)(void *p);
+
+/** Put an object in the external pool. */  

Why this *_free callback does not accept the rte_mempool param?

We're freeing the pool opaque data here.

Add doc comments...

Quoted 11 lines
Quoted 8 lines
Quoted 3 lines

+typedef int (*rte_mempool_put_t)(void *p, void * const *obj_table, unsigned n);

What is the *p pointer?
What is the obj_table?
Why is it void *?
Why is it const?

The *p pointer is the opaque data for a given mempool handler (ring, array, linked list, etc)

Again, doc comments...

I don't like the obj_table representation to be an array of void *. I could see it already in DPDK for defining Ethernet driver queues, so, it's probably not an issue. I just say, I would prefer some basic type safety like

struct rte_mempool_obj {
	void *p;
};

Is there somebody with different opinions?

[...]

Quoted 8 lines
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+/** Structure defining a mempool handler. */  

Later in the text, I suggested to rename rte_mempool_handler to rte_mempool_ops. I believe that it explains the purpose of this struct better. It would improve consistency in function names (the *_ext_* mark is very strange and inconsistent).

I agree. I've gone through all the code and renamed to rte_mempool_handler_ops.

Ok. I meant rte_mempool_ops because I find the word "handler" to be redundant.

Quoted 1 line

[...]

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

+/**
+ * Set the handler of a mempool
+ *
+ * This can only be done on a mempool that is not populated, i.e. just after
+ * a call to rte_mempool_create_empty().
+ *
+ * @param mp
+ * Pointer to the memory pool.
+ * @param name
+ * Name of the handler.
+ * @return
+ * - 0: Sucess; the new handler is configured.
+ * - <0: Error (errno)

Quoted 8 lines
Quoted 4 lines

Should this doc be more specific about the possible failures?

The body of rte_mempool_set_handler does not set errno at all. It returns e.g. -EEXIST.

This is up to the handler. We cannot know what codes will be returned at this stage.
errno was a cut-and paste error, this is now fixed.

I don't think so. The rte_mempool_set_handler is not handler-specific:

116 /* set the handler of a mempool */
117 int
118 rte_mempool_set_handler(struct rte_mempool *mp, const char *name) 119 {
120 struct rte_mempool_handler *handler = NULL;
121 unsigned i;
122
123 /* too late, the mempool is already populated */
124 if (mp->flags & MEMPOOL_F_RING_CREATED)
125 return -EEXIST;

Here, it returns -EEXIST.

126
127 for (i = 0; i < rte_mempool_handler_table.num_handlers; i++) {
128 if (!strcmp(name, rte_mempool_handler_table.handler[i].name)) {
129 handler = &rte_mempool_handler_table.handler[i];
130 break;
131 }
132 }
133
134 if (handler == NULL)
135 return -EINVAL;

And here, it returns -EINVAL.

136
137 mp->handler_idx = i;
138 return 0;
139 }

So, it is possible to define those in the doc comment.

Quoted 16 lines
Quoted 15 lines
Quoted 4 lines

+ */

lib/librte_mempool/rte_mempool.h +1 -0
+int

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 492

Quoted 10 lines
lib/librte_mempool/rte_mempool.h +10 -0
+rte_mempool_set_handler(struct rte_mempool *mp, const char *name);
+
+/**
+ * Register an external pool handler.
+ *
+ * @param h
+ *   Pointer to the external pool handler
+ * @return
+ *   - >=0: Sucess; return the index of the handler in the table.
+ *   - <0: Error (errno)  

Should this doc be more specific about the possible failures?

Quoted 3 lines

This is up to the handler. We cannot know what codes will be returned at this stage.
errno was a cut-and paste error, this is now fixed.

Same, here... -ENOSPC, -EINVAL are returned in certain cases. And again, this call is not handler-specific.

Quoted 1 line

[...]

Quoted 42 lines
Quoted 42 lines
Quoted 28 lines
lib/librte_mempool/rte_mempool_default.c new file +27 -0
+
+static struct rte_mempool_handler handler_mp_mc = {
+	.name = "ring_mp_mc",
+	.alloc = common_ring_alloc,
+	.free = common_ring_free,
+	.put = common_ring_mp_put,
+	.get = common_ring_mc_get,
+	.get_count = common_ring_get_count,
+};
+
+static struct rte_mempool_handler handler_sp_sc = {
+	.name = "ring_sp_sc",
+	.alloc = common_ring_alloc,
+	.free = common_ring_free,
+	.put = common_ring_sp_put,
+	.get = common_ring_sc_get,
+	.get_count = common_ring_get_count,
+};
+
+static struct rte_mempool_handler handler_mp_sc = {
+	.name = "ring_mp_sc",
+	.alloc = common_ring_alloc,
+	.free = common_ring_free,
+	.put = common_ring_mp_put,
+	.get = common_ring_sc_get,
+	.get_count = common_ring_get_count,
+};

lib/librte_mempool/rte_mempool_default.c line 143

Quoted 10 lines
lib/librte_mempool/rte_mempool_default.c new file +10 -0
+
+static struct rte_mempool_handler handler_sp_mc = {
+	.name = "ring_sp_mc",
+	.alloc = common_ring_alloc,
+	.free = common_ring_free,
+	.put = common_ring_sp_put,
+	.get = common_ring_mc_get,
+	.get_count = common_ring_get_count,
+};
+  

Introducing those handlers can go as a separate patch. IMHO, that would simplify the review process a lot. First introduce the mechanism, then add something inside.

Quoted 6 lines
Quoted 2 lines

I'd also note that those handlers are always available and what kind of memory do they use...

Done. Now added as a separate patch.

They don't use any memory unless they are used.

Yes, that is what I've meant... What is the source of the allocations? Where does the common_ring_sc_get get memory from?

Anyway, any documentation describing the goal of those four declarations would be helpful.

Quoted 30 lines
Quoted 22 lines
Quoted 4 lines
lib/librte_mempool/rte_mempool_handler.c new file +2 -0
+#include <stdio.h>
+#include <string.h>

lib/librte_mempool/rte_mempool_handler.c line 46

Quoted 10 lines
lib/librte_mempool/rte_mempool_handler.c new file +10 -0
+
+#include <rte_mempool.h>
+
+/* indirect jump table to support external memory pools */
+struct rte_mempool_handler_table rte_mempool_handler_table = {
+	.sl =  RTE_SPINLOCK_INITIALIZER ,
+	.num_handlers = 0
+};
+
+/* add a new handler in rte_mempool_handler_table, return its index */  

It seems to me that there is no way how to put some opaque pointer into the handler. In such case I would expect I can do something like:

struct my_handler {
	struct rte_mempool_handler h;
	...
} handler;
Quoted 7 lines

rte_mempool_handler_register(&handler.h);

But I cannot because you copy the contents of the handler. By the way, this should be documented.

How can I pass an opaque pointer here? The only way I see is through the rte_mempool.pool.

Quoted 6 lines

I think have addressed this in a later patch, in the discussions with Jerin on the list. But
rather than passing data at register time, I pass it at create time (or rather set_handler).
And a new config void has been added to the mempool struct for this purpose.

Ok, sounds promising. It just should be well specified to avoid situations when accessing the the pool_config before it is set.

Quoted 12 lines
Quoted 12 lines

In that case, what about renaming the rte_mempool_handler
to rte_mempool_ops? Because semantically, it is not a handler, it just holds the operations.

This would improve some namings:

rte_mempool_ext_alloc -> rte_mempool_ops_alloc rte_mempool_ext_free -> rte_mempool_ops_free rte_mempool_ext_get_count -> rte_mempool_ops_get_count rte_mempool_handler_register -> rte_mempool_ops_register

Quoted 7 lines
Quoted 2 lines

seems to be more readable to me. The *_ext_* mark does not say anything valuable. It just scares a bit :).

Agreed. Makes sense. The ext was intended to be 'external', but that's a bit too generic, and not
very intuitive. the 'ops' tag seems better to me. I've change this in the latest patch.

Again, note that I've suggested to avoid the word _handler_ entirely.

[...]

Quoted 6 lines
Quoted 3 lines

Regards
Jan

Thanks, Jan. Very comprehensive. I'll hopefully be pushing the latest patch to the list later today (Tuesday 31st)

Cool, please CC me.

Jan

Quoted 5 lines

Regard,
Dave.

Signature · 4 lines
   Jan Viktorin                  E-mail: [email protected]
   System Architect              Web:    www.RehiveTech.com
   RehiveTech
   Brno, Czech Republic
#69 lore raw

Re: [dpdk-dev,v5,1/3] mempool: support external handler

Quoted 24 lines · Jan Viktorin
Quoted 2 lines · David

Hi Jan,

[...]

Quoted 8 lines
Quoted 8 lines

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 342

Quoted 3 lines
lib/librte_mempool/rte_mempool.h +3 -0
+typedef void *(*rte_mempool_alloc_t)(struct rte_mempool *mp);
+
+/** Free the external pool. */

What is the purpose of this callback?
What exactly does it allocate?
Some rte_mempool internals?
Or the memory?
What does it return?

Quoted 9 lines

This is the main allocate function of the handler. It is up to the mempool handlers control.
The handler's alloc function does whatever it needs to do to grab memory for this handler, and places
a pointer to it in the *pool opaque pointer in the rte_mempool struct. In the default handler, *pool
points to a ring, in other handlers, it will mostlikely point to a different type of data structure. It will
be transparent to the application programmer.

Quoted 4 lines

Thanks for explanation. Please, add doc comments there.

Should the *mp be const here? It is not clear to me whether the callback is expected to modify the mempool or not (eg. set the pool pointer).

Comment added. Not const, as the *pool is set.

Quoted 7 lines
Quoted 6 lines
Quoted 5 lines

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 345

Quoted 3 lines
lib/librte_mempool/rte_mempool.h +3 -0
+typedef void (*rte_mempool_free_t)(void *p);
+
+/** Put an object in the external pool. */

Why this *_free callback does not accept the rte_mempool param?

We're freeing the pool opaque data here.

Add doc comments...

Done.

Quoted 15 lines
Quoted 9 lines
Quoted 7 lines
Quoted 2 lines

+typedef int (*rte_mempool_put_t)(void *p, void * const *obj_table, unsigned n);

What is the *p pointer?
What is the obj_table?
Why is it void *?
Why is it const?

The *p pointer is the opaque data for a given mempool handler (ring, array, linked list, etc)

Again, doc comments...

I don't like the obj_table representation to be an array of void *. I could see it already in DPDK for defining Ethernet driver queues, so, it's probably not an issue. I just say, I would prefer some basic type safety like

Quoted 7 lines
struct rte_mempool_obj {
	void *p;
};

Is there somebody with different opinions?

[...]

Comments added. I've left as a void* for the moment.

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+/** Structure defining a mempool handler. */

Later in the text, I suggested to rename rte_mempool_handler to rte_mempool_ops. I believe that it explains the purpose of this struct better. It would improve consistency in function names (the *_ext_* mark is very strange and inconsistent).

I agree. I've gone through all the code and renamed to rte_mempool_handler_ops.

Ok. I meant rte_mempool_ops because I find the word "handler" to be redundant.

I prefer the use of the word handler, unless others also have opinions either way?

Quoted 22 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

+/**
+ * Set the handler of a mempool
+ *
+ * This can only be done on a mempool that is not populated, i.e. just after
+ * a call to rte_mempool_create_empty().
+ *
+ * @param mp
+ * Pointer to the memory pool.
+ * @param name
+ * Name of the handler.
+ * @return
+ * - 0: Sucess; the new handler is configured.
+ * - <0: Error (errno)

Quoted 7 lines
Quoted 4 lines

Should this doc be more specific about the possible failures?

The body of rte_mempool_set_handler does not set errno at all. It returns e.g. -EEXIST.

This is up to the handler. We cannot know what codes will be returned at this stage.
errno was a cut-and paste error, this is now fixed.

I don't think so. The rte_mempool_set_handler is not handler-specific:

[...]

Quoted 1 line

So, it is possible to define those in the doc comment.

Ah, I see now. My mistake, I assumed a different function. Added now.

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 492

Quoted 13 lines

+ */

lib/librte_mempool/rte_mempool.h +11 -0
+int
+rte_mempool_set_handler(struct rte_mempool *mp, const char *name);
+
+/**
+ * Register an external pool handler.
+ *
+ * @param h
+ *   Pointer to the external pool handler
+ * @return
+ *   - >=0: Sucess; return the index of the handler in the table.
+ *   - <0: Error (errno)

Should this doc be more specific about the possible failures?

Quoted 5 lines
Quoted 3 lines

This is up to the handler. We cannot know what codes will be returned at this stage.
errno was a cut-and paste error, this is now fixed.

Same, here... -ENOSPC, -EINVAL are returned in certain cases. And again, this call is not handler-specific.

Yes, Added now to comments.

Quoted 47 lines

[...]

Quoted 44 lines
Quoted 44 lines
Quoted 28 lines
lib/librte_mempool/rte_mempool_default.c new file +27 -0
+
+static struct rte_mempool_handler handler_mp_mc = {
+	.name = "ring_mp_mc",
+	.alloc = common_ring_alloc,
+	.free = common_ring_free,
+	.put = common_ring_mp_put,
+	.get = common_ring_mc_get,
+	.get_count = common_ring_get_count,
+};
+
+static struct rte_mempool_handler handler_sp_sc = {
+	.name = "ring_sp_sc",
+	.alloc = common_ring_alloc,
+	.free = common_ring_free,
+	.put = common_ring_sp_put,
+	.get = common_ring_sc_get,
+	.get_count = common_ring_get_count,
+};
+
+static struct rte_mempool_handler handler_mp_sc = {
+	.name = "ring_mp_sc",
+	.alloc = common_ring_alloc,
+	.free = common_ring_free,
+	.put = common_ring_mp_put,
+	.get = common_ring_sc_get,
+	.get_count = common_ring_get_count,
+};

lib/librte_mempool/rte_mempool_default.c line 143

Quoted 10 lines
lib/librte_mempool/rte_mempool_default.c new file +10 -0
+
+static struct rte_mempool_handler handler_sp_mc = {
+	.name = "ring_sp_mc",
+	.alloc = common_ring_alloc,
+	.free = common_ring_free,
+	.put = common_ring_sp_put,
+	.get = common_ring_mc_get,
+	.get_count = common_ring_get_count,
+};
+

Introducing those handlers can go as a separate patch. IMHO, that would simplify the review process a lot. First introduce the mechanism, then add something inside.

I'd also note that those handlers are always available and what kind of memory do they use...

Quoted 8 lines
Quoted 3 lines

Done. Now added as a separate patch.

They don't use any memory unless they are used.

Yes, that is what I've meant... What is the source of the allocations? Where does the common_ring_sc_get get memory from?

Anyway, any documentation describing the goal of those four declarations would be helpful.

For these handlers, the allocations are as per the original code before this patch. For new handlers, hardware allocators, stack allocators, etc.

I've added comments on the 4 declarations.

Quoted 56 lines
Quoted 26 lines
Quoted 26 lines

lib/librte_mempool/rte_mempool_handler.c line 46

Quoted 13 lines
lib/librte_mempool/rte_mempool_handler.c new file +12 -0
+#include <stdio.h>
+#include <string.h>
+
+#include <rte_mempool.h>
+
+/* indirect jump table to support external memory pools */
+struct rte_mempool_handler_table rte_mempool_handler_table = {
+	.sl =  RTE_SPINLOCK_INITIALIZER ,
+	.num_handlers = 0
+};
+
+/* add a new handler in rte_mempool_handler_table, return its index */

It seems to me that there is no way how to put some opaque pointer into the handler. In such case I would expect I can do something like:

struct my_handler {
	struct rte_mempool_handler h;
	...
} handler;

rte_mempool_handler_register(&handler.h);

But I cannot because you copy the contents of the handler. By the way, this should be documented.

Quoted 8 lines
Quoted 2 lines

How can I pass an opaque pointer here? The only way I see is through the rte_mempool.pool.

I think have addressed this in a later patch, in the discussions with Jerin on the list. But
rather than passing data at register time, I pass it at create time (or rather set_handler).
And a new config void has been added to the mempool struct for this purpose.

Ok, sounds promising. It just should be well specified to avoid situations when accessing the the pool_config before it is set.

Quoted 11 lines
Quoted 11 lines

In that case, what about renaming the rte_mempool_handler
to rte_mempool_ops? Because semantically, it is not a handler, it just holds the operations.

This would improve some namings:

rte_mempool_ext_alloc -> rte_mempool_ops_alloc rte_mempool_ext_free -> rte_mempool_ops_free rte_mempool_ext_get_count -> rte_mempool_ops_get_count rte_mempool_handler_register -> rte_mempool_ops_register

Quoted 6 lines
Quoted 2 lines

seems to be more readable to me. The *_ext_* mark does not say anything valuable. It just scares a bit :).

Agreed. Makes sense. The ext was intended to be 'external', but that's a bit too generic, and not
very intuitive. the 'ops' tag seems better to me. I've change this in the latest patch.

Again, note that I've suggested to avoid the word _handler_ entirely.

Quoted 7 lines

[...]

Quoted 4 lines
Quoted 2 lines

Regards
Jan

Thanks, Jan. Very comprehensive. I'll hopefully be pushing the latest patch to the list later today (Tuesday 31st)

Cool, please CC me.

Will do.

Rgds,
Dave.

#70 lore raw

Re: [dpdk-dev,v5,1/3] mempool: support external handler

Hi,

Quoted 18 lines · David
Quoted 17 lines · Jan Viktorin
Quoted 2 lines · David

The *p pointer is the opaque data for a given mempool handler (ring, array, linked list, etc)

Again, doc comments...

I don't like the obj_table representation to be an array of void *. I could see
it already in DPDK for defining Ethernet driver queues, so, it's probably not
an issue. I just say, I would prefer some basic type safety like

struct rte_mempool_obj {
    void *p;
};
Quoted 5 lines
Quoted 3 lines

Is there somebody with different opinions?

[...]

Comments added. I've left as a void* for the moment.

Jan, could you please detail why you think having a rte_mempool_obj structure brings more safety?

For now, I'm in favor of keeping the array of void *, because that's what we use in other mempool or ring functions.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

+/** Structure defining a mempool handler. */

Later in the text, I suggested to rename rte_mempool_handler to rte_mempool_ops.
I believe that it explains the purpose of this struct better. It would improve
consistency in function names (the *_ext_* mark is very strange and inconsistent).

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

I agree. I've gone through all the code and renamed to rte_mempool_handler_ops.

Ok. I meant rte_mempool_ops because I find the word "handler" to be redundant.

I prefer the use of the word handler, unless others also have opinions either way?

Well, I think rte_mempool_ops is clear enough, and shorter, so I'd vote for it.

Regards,
Olivier

#71 lore raw

Re: [dpdk-dev,v5,1/3] mempool: support external handler

On 5/31/2016 9:40 PM, Olivier MATZ wrote:

[...]

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

+/** Structure defining a mempool handler. */

Later in the text, I suggested to rename rte_mempool_handler to rte_mempool_ops.
I believe that it explains the purpose of this struct better. It would improve
consistency in function names (the *_ext_* mark is very strange and inconsistent).

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

I agree. I've gone through all the code and renamed to rte_mempool_handler_ops.

Ok. I meant rte_mempool_ops because I find the word "handler" to be redundant.

I prefer the use of the word handler, unless others also have opinions either way?

Well, I think rte_mempool_ops is clear enough, and shorter, so I'd vote for it.

OK, I've just changed it. It will be in next revision. :)

Regards,
Dave.

#72 lore raw

Re: [dpdk-dev,v5,1/3] mempool: support external handler

In reply to Olivier MATZ
Quoted 21 lines · On Tue, 31 May 2016 22:40:59 +0200 Olivier MATZ <[email protected]> wrote:

Hi,

Quoted 18 lines · David
Quoted 17 lines · Jan Viktorin
Quoted 2 lines · David

The *p pointer is the opaque data for a given mempool handler (ring, array, linked list, etc)

Again, doc comments...

I don't like the obj_table representation to be an array of void *. I could see
it already in DPDK for defining Ethernet driver queues, so, it's probably not
an issue. I just say, I would prefer some basic type safety like

struct rte_mempool_obj {
    void *p;
};
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Is there somebody with different opinions?

[...]

Comments added. I've left as a void* for the moment.

Jan, could you please detail why you think having a rte_mempool_obj structure brings more safety?

First, void * does not say anything about the purpose of the argument. So, anybody, who is not familiar with the mempool internals would be lost for a while (as I was when studying the Ethernet queue API of DPDK).

The type safety (in C, LoL) here is in the means of messing up order of arguments. When there are more void * args, you can accidently pass something wrong. DPDK has quite strict compiler settings, however, I don't consider it to be a good practice in general.

When you have a named struct or a typedef, its definition usually contains doc comments describing its purposes. Nobody usually writes good doc comments for void * arguments of functions.

Quoted 3 lines

For now, I'm in favor of keeping the array of void *, because that's what we use in other mempool or ring functions.

It was just a suggestion... I don't consider this to be an issue (as stated earlier).

Jan

Quoted 23 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 3 lines

+/** Structure defining a mempool handler. */

Later in the text, I suggested to rename rte_mempool_handler to rte_mempool_ops.
I believe that it explains the purpose of this struct better. It would improve
consistency in function names (the *_ext_* mark is very strange and inconsistent).

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

I agree. I've gone through all the code and renamed to rte_mempool_handler_ops.

Ok. I meant rte_mempool_ops because I find the word "handler" to be redundant.

I prefer the use of the word handler, unless others also have opinions either way?

Well, I think rte_mempool_ops is clear enough, and shorter, so I'd vote for it.

Regards,
Olivier

Signature · 4 lines
   Jan Viktorin                  E-mail: [email protected]
   System Architect              Web:    www.RehiveTech.com
   RehiveTech
   Brno, Czech Republic
#73 lore raw

Re: [PATCH v5 1/3] mempool: support external handler

In reply to David Hunt
Quoted 152 lines · David Hunt

Until now, the objects stored in mempool mempool were internally stored a ring. This patch introduce the possibility to register external handlers replacing the ring.

The default behavior remains unchanged, but calling the new function rte_mempool_set_handler() right after rte_mempool_create_empty() allows to change the handler that will be used when populating the mempool.

v5 changes: rebasing on top of 35 patch set mempool work.

---
 app/test/test_mempool_perf.c               |   1 -
 lib/librte_mempool/Makefile                |   2 +
 lib/librte_mempool/rte_mempool.c           |  73 ++++------
 lib/librte_mempool/rte_mempool.h           | 212 +++++++++++++++++++++++++----
 lib/librte_mempool/rte_mempool_default.c   | 147 ++++++++++++++++++++
 lib/librte_mempool/rte_mempool_handler.c   | 139 +++++++++++++++++++
 lib/librte_mempool/rte_mempool_version.map |   4 +
 7 files changed, 506 insertions(+), 72 deletions(-)
 create mode 100644 lib/librte_mempool/rte_mempool_default.c
 create mode 100644 lib/librte_mempool/rte_mempool_handler.c
app/test/test_mempool_perf.c +0 -1
index cdc02a0..091c1df 100644
@@ -161,7 +161,6 @@ per_lcore_mempool_test(__attribute__((unused)) void *arg)
 							   n_get_bulk);
 				if (unlikely(ret < 0)) {
 					rte_mempool_dump(stdout, mp);
-					rte_ring_dump(stdout, mp->ring);
 					/* in this case, objects are lost... */
 					return -1;
 				}
lib/librte_mempool/Makefile +2 -0
index 43423e0..f19366e 100644
@@ -42,6 +42,8 @@ LIBABIVER := 2
 
 # all source are stored in SRCS-y
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_handler.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_default.c
 # install includes
 SYMLINK-$(CONFIG_RTE_LIBRTE_MEMPOOL)-include := rte_mempool.h
 
lib/librte_mempool/rte_mempool.c +14 -42
index 1ab6701..6ec2b3f 100644
@@ -148,7 +148,7 @@ mempool_add_elem(struct rte_mempool *mp, void *obj, phys_addr_t physaddr)
 #endif
 
 	/* enqueue in ring */
-	rte_ring_sp_enqueue(mp->ring, obj);
+	rte_mempool_ext_put_bulk(mp, &obj, 1);
 }
 
 /* call obj_cb() for each mempool element */
@@ -300,40 +300,6 @@ rte_mempool_xmem_usage(__rte_unused void *vaddr, uint32_t elt_num,
 	return (size_t)paddr_idx << pg_shift;
 }
 
-/* create the internal ring */
-static int
-rte_mempool_ring_create(struct rte_mempool *mp)
-{
-	int rg_flags = 0, ret;
-	char rg_name[RTE_RING_NAMESIZE];
-	struct rte_ring *r;
-
-	ret = snprintf(rg_name, sizeof(rg_name),
-		RTE_MEMPOOL_MZ_FORMAT, mp->name);
-	if (ret < 0 || ret >= (int)sizeof(rg_name))
-		return -ENAMETOOLONG;
-
-	/* ring flags */
-	if (mp->flags & MEMPOOL_F_SP_PUT)
-		rg_flags |= RING_F_SP_ENQ;
-	if (mp->flags & MEMPOOL_F_SC_GET)
-		rg_flags |= RING_F_SC_DEQ;
-
-	/* Allocate the ring that will be used to store objects.
-	 * Ring functions will return appropriate errors if we are
-	 * running as a secondary process etc., so no checks made
-	 * in this function for that condition.
-	 */
-	r = rte_ring_create(rg_name, rte_align32pow2(mp->size + 1),
-		mp->socket_id, rg_flags);
-	if (r == NULL)
-		return -rte_errno;
-
-	mp->ring = r;
-	mp->flags |= MEMPOOL_F_RING_CREATED;
-	return 0;
-}
-
 /* free a memchunk allocated with rte_memzone_reserve() */
 static void
 rte_mempool_memchunk_mz_free(__rte_unused struct rte_mempool_memhdr *memhdr,
@@ -351,7 +317,7 @@ rte_mempool_free_memchunks(struct rte_mempool *mp)
 	void *elt;
 
 	while (!STAILQ_EMPTY(&mp->elt_list)) {
-		rte_ring_sc_dequeue(mp->ring, &elt);
+		rte_mempool_ext_get_bulk(mp, &elt, 1);
 		(void)elt;
 		STAILQ_REMOVE_HEAD(&mp->elt_list, next);
 		mp->populated_size--;
@@ -380,15 +346,18 @@ rte_mempool_populate_phys(struct rte_mempool *mp, char *vaddr,
 	unsigned i = 0;
 	size_t off;
 	struct rte_mempool_memhdr *memhdr;
-	int ret;
 
 	/* create the internal ring if not already done */
 	if ((mp->flags & MEMPOOL_F_RING_CREATED) == 0) {
-		ret = rte_mempool_ring_create(mp);
-		if (ret < 0)
-			return ret;
+		rte_errno = 0;
+		mp->pool = rte_mempool_ext_alloc(mp);
+		if (mp->pool == NULL) {
+			if (rte_errno == 0)
+				return -EINVAL;
+			else
+				return -rte_errno;
+		}
 	}
-
 	/* mempool is already populated */
 	if (mp->populated_size >= mp->size)
 		return -ENOSPC;
@@ -700,7 +669,7 @@ rte_mempool_free(struct rte_mempool *mp)
 	rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
 
 	rte_mempool_free_memchunks(mp);
-	rte_ring_free(mp->ring);
+	rte_mempool_ext_free(mp);
 	rte_memzone_free(mp->mz);
 }
 
@@ -812,6 +781,20 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
 		RTE_PTR_ADD(mp, MEMPOOL_HEADER_SIZE(mp, 0));
 
 	te->data = mp;
+
+	/*
+	 * Since we have 4 combinations of the SP/SC/MP/MC examine the flags to

lib/librte_mempool/rte_mempool.c rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, · line 796

Quoted 10 lines
lib/librte_mempool/rte_mempool.c +10 -0
+	 * set the correct index into the handler table.
+	 */
+	if (flags & (MEMPOOL_F_SP_PUT | MEMPOOL_F_SC_GET))
+		rte_mempool_set_handler(mp, "ring_sp_sc");
+	else if (flags & MEMPOOL_F_SP_PUT)
+		rte_mempool_set_handler(mp, "ring_sp_mc");
+	else if (flags & MEMPOOL_F_SC_GET)
+		rte_mempool_set_handler(mp, "ring_mp_sc");
+	else
+		rte_mempool_set_handler(mp, "ring_mp_mc");

IMO, We should decouple the implementation specific flags of _a_ external pool manager implementation from the generic rte_mempool_create_empty function as going further when we introduce new flags for custom HW accelerated external pool manager then this common code will be bloated.

Quoted 90 lines
lib/librte_mempool/rte_mempool.c +4 -3
+
 	rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
 	TAILQ_INSERT_TAIL(mempool_list, te, next);
 	rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
@@ -927,7 +910,7 @@ rte_mempool_count(const struct rte_mempool *mp)
 	unsigned count;
 	unsigned lcore_id;
 
-	count = rte_ring_count(mp->ring);
+	count = rte_mempool_ext_get_count(mp);
 
 	if (mp->cache_size == 0)
 		return count;
@@ -1120,7 +1103,7 @@ rte_mempool_dump(FILE *f, struct rte_mempool *mp)
 
 	fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
 	fprintf(f, "  flags=%x\n", mp->flags);
-	fprintf(f, "  ring=<%s>@%p\n", mp->ring->name, mp->ring);
+	fprintf(f, "  pool=%p\n", mp->pool);
 	fprintf(f, "  phys_addr=0x%" PRIx64 "\n", mp->mz->phys_addr);
 	fprintf(f, "  nb_mem_chunks=%u\n", mp->nb_mem_chunks);
 	fprintf(f, "  size=%"PRIu32"\n", mp->size);
@@ -1141,7 +1124,7 @@ rte_mempool_dump(FILE *f, struct rte_mempool *mp)
 	}
 
 	cache_count = rte_mempool_dump_cache(f, mp);
-	common_count = rte_ring_count(mp->ring);
+	common_count = rte_mempool_ext_get_count(mp);
 	if ((cache_count + common_count) > mp->size)
 		common_count = mp->size - cache_count;
 	fprintf(f, "  common_pool_count=%u\n", common_count);
lib/librte_mempool/rte_mempool.h +36 -1
index 60339bd..ed2c110 100644
@@ -67,6 +67,7 @@
 #include <inttypes.h>
 #include <sys/queue.h>
 
+#include <rte_spinlock.h>
 #include <rte_log.h>
 #include <rte_debug.h>
 #include <rte_lcore.h>
@@ -203,7 +204,15 @@ struct rte_mempool_memhdr {
  */
 struct rte_mempool {
 	char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
-	struct rte_ring *ring;           /**< Ring to store objects. */
+	void *pool;                      /**< Ring or ext-pool to store objects. */
+	/**
+	 * Index into the array of structs containing callback fn pointers.
+	 * We're using an index here rather than pointers to the callbacks
+	 * to facilitate any secondary processes that may want to use
+	 * this mempool. Any function pointers stored in the mempool
+	 * directly would not be valid for secondary processes.
+	 */
+	int32_t handler_idx;
 	const struct rte_memzone *mz;    /**< Memzone where pool is allocated */
 	int flags;                       /**< Flags of the mempool. */
 	int socket_id;                   /**< Socket id passed at mempool creation. */
@@ -325,6 +334,175 @@ void rte_mempool_check_cookies(const struct rte_mempool *mp,
 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
 
+#define RTE_MEMPOOL_HANDLER_NAMESIZE 32 /**< Max length of handler name. */
+
+/** Allocate the external pool. */
+typedef void *(*rte_mempool_alloc_t)(struct rte_mempool *mp);
+
+/** Free the external pool. */
+typedef void (*rte_mempool_free_t)(void *p);
+
+/** Put an object in the external pool. */
+typedef int (*rte_mempool_put_t)(void *p, void * const *obj_table, unsigned n);
+
+/** Get an object from the external pool. */
+typedef int (*rte_mempool_get_t)(void *p, void **obj_table, unsigned n);
+
+/** Return the number of available objects in the external pool. */
+typedef unsigned (*rte_mempool_get_count)(void *p);
+
+/** Structure defining a mempool handler. */
+struct rte_mempool_handler {
+	char name[RTE_MEMPOOL_HANDLER_NAMESIZE]; /**< Name of mempool handler */
+	rte_mempool_alloc_t alloc;       /**< Allocate the external pool. */
+	rte_mempool_free_t free;         /**< Free the external pool. */
+	rte_mempool_put_t put;           /**< Put an object. */
+	rte_mempool_get_t get;           /**< Get an object. */
+	rte_mempool_get_count get_count; /**< Get the number of available objs. */
+} __rte_cache_aligned;

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 372

Quoted 10 lines
lib/librte_mempool/rte_mempool.h +10 -0
+
+#define RTE_MEMPOOL_MAX_HANDLER_IDX 16  /**< Max number of registered handlers */
+
+/** Structure storing the table of registered handlers. */
+struct rte_mempool_handler_table {
+	rte_spinlock_t sl;     /**< Spinlock for add/delete. */
+	uint32_t num_handlers; /**< Number of handlers in the table. */
+	/** Storage for all possible handlers. */
+	struct rte_mempool_handler handler[RTE_MEMPOOL_MAX_HANDLER_IDX];
+};

add __rte_cache_aligned to this structure to avoid "handler" memory cacheline being shared with other variables

Quoted 4 lines
lib/librte_mempool/rte_mempool.h +4 -0
+
+/** Array of registered handlers */
+extern struct rte_mempool_handler_table rte_mempool_handler_table;
+

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 386

Quoted 10 lines
lib/librte_mempool/rte_mempool.h +10 -0
+/**
+ * @internal Get the mempool handler from its index.
+ *
+ * @param handler_idx
+ *   The index of the handler in the handler table. It must be a valid
+ *   index: (0 <= idx < num_handlers).
+ * @return
+ *   The pointer to the handler in the table.
+ */
+static struct rte_mempool_handler *

inline?

Quoted 178 lines
lib/librte_mempool/rte_mempool.h +123 -20
+rte_mempool_handler_get(int handler_idx)
+{
+	return &rte_mempool_handler_table.handler[handler_idx];
+}
+
+/**
+ * @internal wrapper for external mempool manager alloc callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @return
+ *   The opaque pointer to the external pool.
+ */
+void *
+rte_mempool_ext_alloc(struct rte_mempool *mp);
+
+/**
+ * @internal wrapper for external mempool manager get callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @param obj_table
+ *   Pointer to a table of void * pointers (objects).
+ * @param n
+ *   Number of objects to get.
+ * @return
+ *   - 0: Success; got n objects.
+ *   - <0: Error; code of handler get function.
+ */
+static inline int
+rte_mempool_ext_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
+{
+	struct rte_mempool_handler *handler;
+
+	handler = rte_mempool_handler_get(mp->handler_idx);
+	return handler->get(mp->pool, obj_table, n);
+}
+
+/**
+ * @internal wrapper for external mempool manager put callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @param obj_table
+ *   Pointer to a table of void * pointers (objects).
+ * @param n
+ *   Number of objects to put.
+ * @return
+ *   - 0: Success; n objects supplied.
+ *   - <0: Error; code of handler put function.
+ */
+static inline int
+rte_mempool_ext_put_bulk(struct rte_mempool *mp, void * const *obj_table,
+		unsigned n)
+{
+	struct rte_mempool_handler *handler;
+
+	handler = rte_mempool_handler_get(mp->handler_idx);
+	return handler->put(mp->pool, obj_table, n);
+}
+
+/**
+ * @internal wrapper for external mempool manager get_count callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @return
+ *   The number of available objects in the external pool.
+ */
+unsigned
+rte_mempool_ext_get_count(const struct rte_mempool *mp);
+
+/**
+ * @internal wrapper for external mempool manager free callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ */
+void
+rte_mempool_ext_free(struct rte_mempool *mp);
+
+/**
+ * Set the handler of a mempool
+ *
+ * This can only be done on a mempool that is not populated, i.e. just after
+ * a call to rte_mempool_create_empty().
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @param name
+ *   Name of the handler.
+ * @return
+ *   - 0: Sucess; the new handler is configured.
+ *   - <0: Error (errno)
+ */
+int
+rte_mempool_set_handler(struct rte_mempool *mp, const char *name);
+
+/**
+ * Register an external pool handler.
+ *
+ * @param h
+ *   Pointer to the external pool handler
+ * @return
+ *   - >=0: Sucess; return the index of the handler in the table.
+ *   - <0: Error (errno)
+ */
+int rte_mempool_handler_register(struct rte_mempool_handler *h);
+
+/**
+ * Macro to statically register an external pool handler.
+ */
+#define MEMPOOL_REGISTER_HANDLER(h)					\
+	void mp_hdlr_init_##h(void);					\
+	void __attribute__((constructor, used)) mp_hdlr_init_##h(void)	\
+	{								\
+		rte_mempool_handler_register(&h);			\
+	}
+
 /**
  * An object callback function for mempool.
  *
@@ -736,7 +914,7 @@ void rte_mempool_dump(FILE *f, struct rte_mempool *mp);
  */
 static inline void __attribute__((always_inline))
 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
-		    unsigned n, int is_mp)
+		    unsigned n, __rte_unused int is_mp)
 {
 	struct rte_mempool_cache *cache;
 	uint32_t index;
@@ -774,7 +952,7 @@ __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
 	cache->len += n;
 
 	if (cache->len >= flushthresh) {
-		rte_ring_mp_enqueue_bulk(mp->ring, &cache->objs[cache_size],
+		rte_mempool_ext_put_bulk(mp, &cache->objs[cache_size],
 				cache->len - cache_size);
 		cache->len = cache_size;
 	}
@@ -782,26 +960,10 @@ __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
 	return;
 
 ring_enqueue:
-
 	/* push remaining objects in ring */
-#ifdef RTE_LIBRTE_MEMPOOL_DEBUG
-	if (is_mp) {
-		if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
-			rte_panic("cannot put objects in mempool\n");
-	}
-	else {
-		if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
-			rte_panic("cannot put objects in mempool\n");
-	}
-#else
-	if (is_mp)
-		rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
-	else
-		rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
-#endif
+	rte_mempool_ext_put_bulk(mp, obj_table, n);
 }
 
-
 /**
  * Put several objects back in the mempool (multi-producers safe).
  *
@@ -922,7 +1084,7 @@ rte_mempool_put(struct rte_mempool *mp, void *obj)
  */
 static inline int __attribute__((always_inline))
 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
-		   unsigned n, int is_mc)
+		   unsigned n, __rte_unused int is_mc)
 {
 	int ret;
 	struct rte_mempool_cache *cache;

lib/librte_mempool/rte_mempool.h __mempool_get_bulk(struct rte_mempool *mp, void **obj_table, · line 1110

Quoted 5 lines
lib/librte_mempool/rte_mempool.h +1 -1
@@ -945,7 +1107,8 @@ __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
 		uint32_t req = n + (cache_size - cache->len);
 
 		/* How many do we require i.e. number to fill the cache + the request */
-		ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
+		ret = rte_mempool_ext_get_bulk(mp,

This makes inline function to a function pointer. Nothing wrong in that. However, Do you see any performance drop with "local cache" only use case?

http://dpdk.org/dev/patchwork/patch/12993/

Quoted 339 lines
lib/librte_mempool/rte_mempool.h +2 -4
+			&cache->objs[cache->len], req);
 		if (unlikely(ret < 0)) {
 			/*
 			 * In the offchance that we are buffer constrained,
@@ -972,10 +1135,7 @@ __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
 ring_dequeue:
 
 	/* get remaining objects from ring */
-	if (is_mc)
-		ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
-	else
-		ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
+	ret = rte_mempool_ext_get_bulk(mp, obj_table, n);
 
 	if (ret < 0)
 		__MEMPOOL_STAT_ADD(mp, get_fail, n);
lib/librte_mempool/rte_mempool_default.c new file +147 -0
new file mode 100644
index 0000000..a6ac65a
@@ -0,0 +1,147 @@
+/*-
+ *   BSD LICENSE
+ *
+ *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include <rte_errno.h>
+#include <rte_ring.h>
+#include <rte_mempool.h>
+
+static int
+common_ring_mp_put(void *p, void * const *obj_table, unsigned n)
+{
+	return rte_ring_mp_enqueue_bulk((struct rte_ring *)p, obj_table, n);
+}
+
+static int
+common_ring_sp_put(void *p, void * const *obj_table, unsigned n)
+{
+	return rte_ring_sp_enqueue_bulk((struct rte_ring *)p, obj_table, n);
+}
+
+static int
+common_ring_mc_get(void *p, void **obj_table, unsigned n)
+{
+	return rte_ring_mc_dequeue_bulk((struct rte_ring *)p, obj_table, n);
+}
+
+static int
+common_ring_sc_get(void *p, void **obj_table, unsigned n)
+{
+	return rte_ring_sc_dequeue_bulk((struct rte_ring *)p, obj_table, n);
+}
+
+static unsigned
+common_ring_get_count(void *p)
+{
+	return rte_ring_count((struct rte_ring *)p);
+}
+
+
+static void *
+common_ring_alloc(struct rte_mempool *mp)
+{
+	int rg_flags = 0, ret;
+	char rg_name[RTE_RING_NAMESIZE];
+	struct rte_ring *r;
+
+	ret = snprintf(rg_name, sizeof(rg_name),
+		RTE_MEMPOOL_MZ_FORMAT, mp->name);
+	if (ret < 0 || ret >= (int)sizeof(rg_name)) {
+		rte_errno = ENAMETOOLONG;
+		return NULL;
+	}
+
+	/* ring flags */
+	if (mp->flags & MEMPOOL_F_SP_PUT)
+		rg_flags |= RING_F_SP_ENQ;
+	if (mp->flags & MEMPOOL_F_SC_GET)
+		rg_flags |= RING_F_SC_DEQ;
+
+	/* Allocate the ring that will be used to store objects.
+	 * Ring functions will return appropriate errors if we are
+	 * running as a secondary process etc., so no checks made
+	 * in this function for that condition. */
+	r = rte_ring_create(rg_name, rte_align32pow2(mp->size + 1),
+		mp->socket_id, rg_flags);
+
+	return r;
+}
+
+static void
+common_ring_free(void *p)
+{
+	rte_ring_free((struct rte_ring *)p);
+}
+
+static struct rte_mempool_handler handler_mp_mc = {
+	.name = "ring_mp_mc",
+	.alloc = common_ring_alloc,
+	.free = common_ring_free,
+	.put = common_ring_mp_put,
+	.get = common_ring_mc_get,
+	.get_count = common_ring_get_count,
+};
+
+static struct rte_mempool_handler handler_sp_sc = {
+	.name = "ring_sp_sc",
+	.alloc = common_ring_alloc,
+	.free = common_ring_free,
+	.put = common_ring_sp_put,
+	.get = common_ring_sc_get,
+	.get_count = common_ring_get_count,
+};
+
+static struct rte_mempool_handler handler_mp_sc = {
+	.name = "ring_mp_sc",
+	.alloc = common_ring_alloc,
+	.free = common_ring_free,
+	.put = common_ring_mp_put,
+	.get = common_ring_sc_get,
+	.get_count = common_ring_get_count,
+};
+
+static struct rte_mempool_handler handler_sp_mc = {
+	.name = "ring_sp_mc",
+	.alloc = common_ring_alloc,
+	.free = common_ring_free,
+	.put = common_ring_sp_put,
+	.get = common_ring_mc_get,
+	.get_count = common_ring_get_count,
+};
+
+MEMPOOL_REGISTER_HANDLER(handler_mp_mc);
+MEMPOOL_REGISTER_HANDLER(handler_sp_sc);
+MEMPOOL_REGISTER_HANDLER(handler_mp_sc);
+MEMPOOL_REGISTER_HANDLER(handler_sp_mc);
lib/librte_mempool/rte_mempool_handler.c new file +139 -0
new file mode 100644
index 0000000..78611f8
@@ -0,0 +1,139 @@
+/*-
+ *   BSD LICENSE
+ *
+ *   Copyright(c) 2016 Intel Corporation. All rights reserved.
+ *   Copyright(c) 2016 6WIND S.A.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include <rte_mempool.h>
+
+/* indirect jump table to support external memory pools */
+struct rte_mempool_handler_table rte_mempool_handler_table = {
+	.sl =  RTE_SPINLOCK_INITIALIZER ,
+	.num_handlers = 0
+};
+
+/* add a new handler in rte_mempool_handler_table, return its index */
+int
+rte_mempool_handler_register(struct rte_mempool_handler *h)
+{
+	struct rte_mempool_handler *handler;
+	int16_t handler_idx;
+
+	rte_spinlock_lock(&rte_mempool_handler_table.sl);
+
+	if (rte_mempool_handler_table.num_handlers >= RTE_MEMPOOL_MAX_HANDLER_IDX) {
+		rte_spinlock_unlock(&rte_mempool_handler_table.sl);
+		RTE_LOG(ERR, MEMPOOL,
+			"Maximum number of mempool handlers exceeded\n");
+		return -ENOSPC;
+	}
+
+	if (h->put == NULL || h->get == NULL || h->get_count == NULL) {
+		rte_spinlock_unlock(&rte_mempool_handler_table.sl);
+		RTE_LOG(ERR, MEMPOOL,
+			"Missing callback while registering mempool handler\n");
+		return -EINVAL;
+	}
+
+	handler_idx = rte_mempool_handler_table.num_handlers++;
+	handler = &rte_mempool_handler_table.handler[handler_idx];
+	snprintf(handler->name, sizeof(handler->name), "%s", h->name);
+	handler->alloc = h->alloc;
+	handler->put = h->put;
+	handler->get = h->get;
+	handler->get_count = h->get_count;
+
+	rte_spinlock_unlock(&rte_mempool_handler_table.sl);
+
+	return handler_idx;
+}
+
+/* wrapper to allocate an external pool handler */
+void *
+rte_mempool_ext_alloc(struct rte_mempool *mp)
+{
+	struct rte_mempool_handler *handler;
+
+	handler = rte_mempool_handler_get(mp->handler_idx);
+	if (handler->alloc == NULL)
+		return NULL;
+	return handler->alloc(mp);
+}
+
+/* wrapper to free an external pool handler */
+void
+rte_mempool_ext_free(struct rte_mempool *mp)
+{
+	struct rte_mempool_handler *handler;
+
+	handler = rte_mempool_handler_get(mp->handler_idx);
+	if (handler->free == NULL)
+		return;
+	return handler->free(mp);
+}
+
+/* wrapper to get available objects in an external pool handler */
+unsigned
+rte_mempool_ext_get_count(const struct rte_mempool *mp)
+{
+	struct rte_mempool_handler *handler;
+
+	handler = rte_mempool_handler_get(mp->handler_idx);
+	return handler->get_count(mp->pool);
+}
+
+/* set the handler of a mempool */
+int
+rte_mempool_set_handler(struct rte_mempool *mp, const char *name)
+{
+	struct rte_mempool_handler *handler = NULL;
+	unsigned i;
+
+	/* too late, the mempool is already populated */
+	if (mp->flags & MEMPOOL_F_RING_CREATED)
+		return -EEXIST;
+
+	for (i = 0; i < rte_mempool_handler_table.num_handlers; i++) {
+		if (!strcmp(name, rte_mempool_handler_table.handler[i].name)) {
+			handler = &rte_mempool_handler_table.handler[i];
+			break;
+		}
+	}
+
+	if (handler == NULL)
+		return -EINVAL;
+
+	mp->handler_idx = i;
+	return 0;
+}
lib/librte_mempool/rte_mempool_version.map +4 -0
index f63461b..a0e9aed 100644
@@ -19,6 +19,8 @@ DPDK_2.0 {
 DPDK_16.7 {
 	global:
 
+	rte_mempool_handler_table;
+
 	rte_mempool_check_cookies;
 	rte_mempool_obj_iter;
 	rte_mempool_mem_iter;
@@ -29,6 +31,8 @@ DPDK_16.7 {
 	rte_mempool_populate_default;
 	rte_mempool_populate_anon;
 	rte_mempool_free;
+	rte_mempool_set_handler;
+	rte_mempool_handler_register;
 
 	local: *;
 } DPDK_2.0;
Signature · 1 line
2.5.5
#74 lore raw

Re: [PATCH v5 1/3] mempool: support external handler

Quoted 17 lines · Jerin Jacob

lib/librte_mempool/rte_mempool.c rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, · line 796

Quoted 12 lines · David Hunt
lib/librte_mempool/rte_mempool.c +12 -0
+	/*
+	 * Since we have 4 combinations of the SP/SC/MP/MC examine the flags to
+	 * set the correct index into the handler table.
+	 */
+	if (flags & (MEMPOOL_F_SP_PUT | MEMPOOL_F_SC_GET))
+		rte_mempool_set_handler(mp, "ring_sp_sc");
+	else if (flags & MEMPOOL_F_SP_PUT)
+		rte_mempool_set_handler(mp, "ring_sp_mc");
+	else if (flags & MEMPOOL_F_SC_GET)
+		rte_mempool_set_handler(mp, "ring_mp_sc");
+	else
+		rte_mempool_set_handler(mp, "ring_mp_mc");

IMO, We should decouple the implementation specific flags of _a_ external pool manager implementation from the generic rte_mempool_create_empty function as going further when we introduce new flags for custom HW accelerated external pool manager then this common code will be bloated.

These flags are only there to maintain backward compatibility for the default handlers. I would not envisage adding more flags to this, I would suggest just adding a new handler using the new API calls. So I would not see this code growing much in the future.

Quoted 9 lines

lib/librte_mempool/rte_mempool.h void rte_mempool_check_cookies(const struct rte_mempool *mp, · line 372

Quoted 7 lines
lib/librte_mempool/rte_mempool.h +7 -0
+/** Structure storing the table of registered handlers. */
+struct rte_mempool_handler_table {
+	rte_spinlock_t sl;     /**< Spinlock for add/delete. */
+	uint32_t num_handlers; /**< Number of handlers in the table. */
+	/** Storage for all possible handlers. */
+	struct rte_mempool_handler handler[RTE_MEMPOOL_MAX_HANDLER_IDX];
+};

add __rte_cache_aligned to this structure to avoid "handler" memory cacheline being shared with other variables

Will do.

Quoted 7 lines
Quoted 7 lines
lib/librte_mempool/rte_mempool.h +7 -0
+
+/** Array of registered handlers */
+extern struct rte_mempool_handler_table rte_mempool_handler_table;
+
+/**
+ * @internal Get the mempool handler from its index.
+ *
Quoted 8 lines
Quoted 7 lines

+ * @param handler_idx
+ * The index of the handler in the handler table. It must be a valid
+ * index: (0 <= idx < num_handlers).
+ * @return
+ * The pointer to the handler in the table.
+ */
+static struct rte_mempool_handler *

inline?

Will do.

Quoted 8 lines

lib/librte_mempool/rte_mempool.h __mempool_get_bulk(struct rte_mempool *mp, void **obj_table, · line 1110

Quoted 3 lines

/* How many do we require i.e. number to fill the cache + the request */

lib/librte_mempool/rte_mempool.h +1 -1
-		ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
+		ret = rte_mempool_ext_get_bulk(mp,

This makes inline function to a function pointer. Nothing wrong in that. However, Do you see any performance drop with "local cache" only use case?

http://dpdk.org/dev/patchwork/patch/12993/

With the latest mempool manager patch (without 12933), I see no performance degradation on my Haswell machine. However, when I apply patch 12933, I'm seeing a 200-300 kpps drop.

With 12933, the mempool_perf_autotest is showing 24% more enqueues/dequeues, but testpmd forwarding traffic between 2 40Gig interfaces from a hardware traffic generator with one core doing the forwarding is showing a drop of 200-300kpps.

Regards,
Dave.

---snip---

#75 lore raw

Re: [PATCH v5 1/3] mempool: support external handler

Quoted 21 lines · David
Quoted 17 lines · Jerin Jacob

lib/librte_mempool/rte_mempool.c rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, · line 796

Quoted 12 lines · David Hunt
lib/librte_mempool/rte_mempool.c +12 -0
+	/*
+	 * Since we have 4 combinations of the SP/SC/MP/MC examine the flags to
+	 * set the correct index into the handler table.
+	 */
+	if (flags & (MEMPOOL_F_SP_PUT | MEMPOOL_F_SC_GET))
+		rte_mempool_set_handler(mp, "ring_sp_sc");
+	else if (flags & MEMPOOL_F_SP_PUT)
+		rte_mempool_set_handler(mp, "ring_sp_mc");
+	else if (flags & MEMPOOL_F_SC_GET)
+		rte_mempool_set_handler(mp, "ring_mp_sc");
+	else
+		rte_mempool_set_handler(mp, "ring_mp_mc");

IMO, We should decouple the implementation specific flags of _a_ external pool manager implementation from the generic rte_mempool_create_empty function as going further when we introduce new flags for custom HW accelerated external pool manager then this common code will be bloated.

Quoted 5 lines

These flags are only there to maintain backward compatibility for the default handlers. I would not
envisage adding more flags to this, I would suggest just adding a new handler using the new API calls.
So I would not see this code growing much in the future.

IMHO, For _each_ HW accelerated external pool manager we may need to introduce specific flag to tune to specific use cases.i.e MEMPOOL_F_* flags for this exiting pool manager implemented in SW. For instance, when we add a new HW external pool manager we may need to add MEMPOOL_MYHW_DONT_FREE_ON_SEND (just a random name) to achieve certain functionally.

So I propose let "unsigned flags" in mempool create to be the opaque type and each external pool manager can define what it makes sense to that specific pool manager as there is NO other means to configure the pool manager.

For instance, on HW accelerated pool manager, the flag MEMPOOL_F_SP_PUT may not make much sense as it can work with MP without any additional settings in HW.

So instead of adding these checks in common code, IMO, lets move this to a pool manager specific "function pointer" function and invoke the function pointer from generic mempool create function.

What do you think?

Jerin

#76 lore raw

Re: [PATCH v5 1/3] mempool: support external handler

Quoted 39 lines · Jerin Jacob
Quoted 19 lines · David
Quoted 17 lines · Jerin Jacob

lib/librte_mempool/rte_mempool.c rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, · line 796

Quoted 12 lines · David Hunt
lib/librte_mempool/rte_mempool.c +12 -0
+	/*
+	 * Since we have 4 combinations of the SP/SC/MP/MC examine the flags to
+	 * set the correct index into the handler table.
+	 */
+	if (flags & (MEMPOOL_F_SP_PUT | MEMPOOL_F_SC_GET))
+		rte_mempool_set_handler(mp, "ring_sp_sc");
+	else if (flags & MEMPOOL_F_SP_PUT)
+		rte_mempool_set_handler(mp, "ring_sp_mc");
+	else if (flags & MEMPOOL_F_SC_GET)
+		rte_mempool_set_handler(mp, "ring_mp_sc");
+	else
+		rte_mempool_set_handler(mp, "ring_mp_mc");

IMO, We should decouple the implementation specific flags of _a_ external pool manager implementation from the generic rte_mempool_create_empty function as going further when we introduce new flags for custom HW accelerated external pool manager then this common code will be bloated.

Quoted 5 lines

These flags are only there to maintain backward compatibility for the default handlers. I would not
envisage adding more flags to this, I would suggest just adding a new handler using the new API calls.
So I would not see this code growing much in the future.

IMHO, For _each_ HW accelerated external pool manager we may need to introduce specific flag to tune to specific use cases.i.e MEMPOOL_F_* flags for this exiting pool manager implemented in SW. For instance, when we add a new HW external pool manager we may need to add MEMPOOL_MYHW_DONT_FREE_ON_SEND (just a random name) to achieve certain functionally.

So I propose let "unsigned flags" in mempool create to be the opaque type and each external pool manager can define what it makes sense to that specific pool manager as there is NO other means to configure the pool manager.

For instance, on HW accelerated pool manager, the flag MEMPOOL_F_SP_PUT may not make much sense as it can work with MP without any additional settings in HW.

Quoted 7 lines

So instead of adding these checks in common code, IMO, lets move this to a pool manager specific "function pointer" function and invoke the function pointer from generic mempool create function.

What do you think?

Jerin

Jerin,
That chunk of code above would be better moved all right. I'd
suggest moving it to the
rte_mempool_create function, as that's the one that needs the backward compatibility.

On the flags issue, each mempool handler can re-interpret the flags as needed. Maybe we could use the upper half of the bits for different handlers, changing the meaning of the bits depending on which handler is being set up. We can then keep the lower
half for bits that are common across all handlers? That way the user can just set the bits they are interested in for that handler. Also, the alloc function has access to the flags, so maybe the handler specific setup could be handled in the alloc function rather than adding a new function pointer?

Of course, that won't help if we need to pass in more data, in which case we'd probably need an opaque data pointer somewhere. It would probably be most useful to pass it in with the alloc, which may need the data. Any suggestions?

Regards,
Dave.

#77 lore raw

Re: [PATCH v5 1/3] mempool: support external handler

Quoted 2 lines · David

Hi David,
[snip]

Quoted 4 lines

That chunk of code above would be better moved all right. I'd suggest
moving it to the
rte_mempool_create function, as that's the one that needs the backward compatibility.

OK

Quoted 7 lines

On the flags issue, each mempool handler can re-interpret the flags as needed. Maybe we
could use the upper half of the bits for different handlers, changing the meaning of the
bits depending on which handler is being set up. We can then keep the lower half for bits that are common across all handlers? That way the user can

Common lower half bit in flags looks good.

Quoted 5 lines

just set the bits they
are interested in for that handler. Also, the alloc function has access to the flags, so maybe the
handler specific setup could be handled in the alloc function rather than adding a new function pointer?

Yes. I agree.

Quoted 6 lines

Of course, that won't help if we need to pass in more data, in which case we'd probably need an
opaque data pointer somewhere. It would probably be most useful to pass it in with the
alloc, which may need the data. Any suggestions?

But the top level rte_mempool_create() function needs to pass the data. Right? That would be an ABI change. IMO, we need to start thinking about passing a struct of config data to rte_mempool_create to create backward compatibility on new argument addition to rte_mempool_create()

Other points in HW assisted pool manager perspective,

1) May be RING can be replaced with some other higher abstraction name for the internal MEMPOOL_F_RING_CREATED flag
2) IMO, It is better to change void *pool in struct rte_mempool to anonymous union type, something like below, so that mempool implementation can choose the best type.

	union {
		void *pool;
		uint64_t val;
	}

3) int32_t handler_idx creates 4 byte hole in struct rte_mempool in 64 bit system. IMO it better to rearrange.(as const struct rte_memzone *mz comes next)

4) IMO, It is better to change ext_alloc/ext_free to ext_create/ext_destroy as their is no allocation in HW assisted pool manager case,
it will be mostly creating some HW initialization.

Quoted 21 lines

Regards,
Dave.

#78 lore raw

Re: [PATCH v5 1/3] mempool: support external handler

On 5/30/2016 10:41 AM, Jerin Jacob wrote:
--snip--

Quoted 9 lines
Quoted 5 lines

Of course, that won't help if we need to pass in more data, in which case we'd probably need an
opaque data pointer somewhere. It would probably be most useful to pass it in with the
alloc, which may need the data. Any suggestions?

But the top level rte_mempool_create() function needs to pass the data. Right? That would be an ABI change. IMO, we need to start thinking about passing a struct of config data to rte_mempool_create to create backward compatibility on new argument addition to rte_mempool_create()

New mempool handlers will use rte_mempool_create_empty(), rte_mempool_set_handler(), then rte_mempool_populate_*(). These three functions are new to this release, to no problem to add a parameter to one of them for the config data. Also since we're adding some new elements to the mempool structure, how about we add a new pointer for a void pointer to a config data structure, as defined by the handler.

So, new element in rte_mempool struct alongside the *pool

    void *pool;
    void *pool_config;

Then add a param to the rte_mempool_set_handler function:
int
rte_mempool_set_handler(struct rte_mempool *mp, const char *name, void *pool_config)

The function would simply set the pointer in the mempool struct, and the custom handler alloc/create function would use as apporopriate as needed. Handlers that do not need this data can be passed NULL.

Quoted 4 lines

Other points in HW assisted pool manager perspective,

1) May be RING can be replaced with some other higher abstraction name for the internal MEMPOOL_F_RING_CREATED flag

Agreed. I'll change to MEMPOOL_F_POOL_CREATED, since we're already changing the *ring element of the mempool struct to *pool

Quoted 7 lines

2) IMO, It is better to change void *pool in struct rte_mempool to anonymous union type, something like below, so that mempool implementation can choose the best type.

	union {
		void *pool;
		uint64_t val;
	}

Could we do this by using the union for the *pool_config suggested above, would that give you what you need?

Quoted 3 lines

3) int32_t handler_idx creates 4 byte hole in struct rte_mempool in 64 bit system. IMO it better to rearrange.(as const struct rte_memzone *mz comes next)

OK, Will look at this.

Quoted 3 lines

4) IMO, It is better to change ext_alloc/ext_free to ext_create/ext_destroy as their is no allocation in HW assisted pool manager case,
it will be mostly creating some HW initialization.

OK, I'll change. I think that makes more sense.

Regards,
Dave.

#79 lore raw

Re: [PATCH v5 1/3] mempool: support external handler

Quoted 5 lines · David

New mempool handlers will use rte_mempool_create_empty(), rte_mempool_set_handler(),
then rte_mempool_populate_*(). These three functions are new to this release, to no problem

Having separate APIs for external pool-manager create is worrisome in application perspective. Is it possible to have rte_mempool_[xmem]_create for the both external and existing SW pool manager and make rte_mempool_create_empty and rte_mempool_populate_* internal functions.

IMO, We can do that by selecting specific rte_mempool_set_handler() based on _flags_ encoding, something like below

bit 0 - 16   // generic bits uses across all the pool managers
bit 16 - 23  // pool handler specific flags bits
bit 24 - 31  // to select the specific pool manager(Up to 256 different flavors of

pool managers, For backward compatibility, make '0'(in 24-31) to select existing SW pool manager.

and applications can choose the handlers by selecting the flag in rte_mempool_[xmem]_create, That way it will be easy in testpmd or any other applications to choose the pool handler from command line etc in future.

and we can remove "mbuf: get default mempool handler from configuration" change-set OR just add if RTE_MBUF_DEFAULT_MEMPOOL_HANDLER is defined then set the same with rte_mempool_set_handler in rte_mempool_[xmem]_create.

What do you think?

Quoted 6 lines

to add a parameter to one of them for the config data. Also since we're adding some new
elements to the mempool structure, how about we add a new pointer for a void pointer to a
config data structure, as defined by the handler.

Quoted 8 lines

So, new element in rte_mempool struct alongside the *pool

    void *pool;
    void *pool_config;

Then add a param to the rte_mempool_set_handler function:
int
rte_mempool_set_handler(struct rte_mempool *mp, const char *name, void *pool_config)

IMO, Maybe we need to have _set_ and _get_.So I think we can have two separate callback in external pool-manger for that if required. IMO, For now, We can live with pool manager specific 8 bits(bit 16 -23) for the configuration as mentioned above and add the new callbacks for set and get when required?

Quoted 11 lines
Quoted 7 lines

2) IMO, It is better to change void *pool in struct rte_mempool to anonymous union type, something like below, so that mempool implementation can choose the best type.

	union {
		void *pool;
		uint64_t val;
	}

Could we do this by using the union for the *pool_config suggested above, would that give
you what you need?

It would be an extra overhead for external pool manager to _alloc_ memory and store the allocated pointer in mempool struct(as *pool) and use pool for pointing other data structures as some implementation need only limited bytes to store the external pool manager specific context.

In order to fix this problem, We may classify fast path and slow path elements in struct rte_mempool and move all fast path elements in first cache line and create an empty opaque space in the remaining bytes in the cache line so that if the external pool manager needs only limited space then it is not required to allocate the separate memory to save the per core cache in fast-path

something like below,

union {
	void *pool;
	uint64_t val;
	uint8_t extra_mem[16] // available free bytes in fast path cache line

}

Other points,

1) Is it possible to remove unused is_mp in __mempool_put_bulk function as it is just a internal function.

2) Considering "get" and "put" are the fast-path callbacks for pool-manger, Is it possible to avoid the extra overhead of the following _load_ and additional cache line on each call, rte_mempool_handler_table.handler[handler_idx]

I understand it is for multiprocess support but I am thing can we introduce something like ethernet API support for multiprocess and resolve "put" and "get" functions pointer on init and store in struct mempool. Some thinking like

file: drivers/net/ixgbe/ixgbe_ethdev.c
search for if (rte_eal_process_type() != RTE_PROC_PRIMARY) {

Jerin

#80 lore raw

Re: [PATCH v5 1/3] mempool: support external handler

Quoted 16 lines · Jerin Jacob
Quoted 4 lines · David

New mempool handlers will use rte_mempool_create_empty(), rte_mempool_set_handler(),
then rte_mempool_populate_*(). These three functions are new to this release, to no problem

Having separate APIs for external pool-manager create is worrisome in application perspective. Is it possible to have rte_mempool_[xmem]_create for the both external and existing SW pool manager and make rte_mempool_create_empty and rte_mempool_populate_* internal functions.

IMO, We can do that by selecting specific rte_mempool_set_handler() based on _flags_ encoding, something like below

bit 0 - 16   // generic bits uses across all the pool managers
bit 16 - 23  // pool handler specific flags bits
bit 24 - 31  // to select the specific pool manager(Up to 256 different flavors of
Quoted 6 lines

pool managers, For backward compatibility, make '0'(in 24-31) to select existing SW pool manager.

and applications can choose the handlers by selecting the flag in rte_mempool_[xmem]_create, That way it will be easy in testpmd or any other applications to choose the pool handler from command line etc in future.

There might be issues with the 8-bit handler number, as we'd have to add an api call to first get the index of a given hander by name, then OR it into the flags. That would mean also extra API calls for the non-default external handlers. I do agree with the handler-specific bits though.

Having the _empty and _set_handler APIs seems to me to be OK for the moment. Maybe Olivier could comment?

Quoted 5 lines

and we can remove "mbuf: get default mempool handler from configuration" change-set OR just add if RTE_MBUF_DEFAULT_MEMPOOL_HANDLER is defined then set the same with rte_mempool_set_handler in rte_mempool_[xmem]_create.

What do you think?

The "configuration" patch is to allow users to quickly change the mempool handler by changing RTE_MBUF_DEFAULT_MEMPOOL_HANDLER to another string of a known handler. It could just as easily be left out and use the rte_mempool_create.

Quoted 14 lines
Quoted 6 lines

to add a parameter to one of them for the config data. Also since we're adding some new
elements to the mempool structure, how about we add a new pointer for a void pointer to a
config data structure, as defined by the handler.

Quoted 8 lines

So, new element in rte_mempool struct alongside the *pool

     void *pool;
     void *pool_config;

Then add a param to the rte_mempool_set_handler function:
int
rte_mempool_set_handler(struct rte_mempool *mp, const char *name, void *pool_config)

Quoted 5 lines

IMO, Maybe we need to have _set_ and _get_.So I think we can have two separate callback in external pool-manger for that if required. IMO, For now, We can live with pool manager specific 8 bits(bit 16 -23) for the configuration as mentioned above and add the new callbacks for set and get when required?

OK, We'll keep the config to the 8 bits of the flags for now. That will also mean I won't add the pool_config void pointer either (for the moment)

Quoted 22 lines
Quoted 10 lines
Quoted 7 lines

2) IMO, It is better to change void *pool in struct rte_mempool to anonymous union type, something like below, so that mempool implementation can choose the best type.

	union {
		void *pool;
		uint64_t val;
	}

Could we do this by using the union for the *pool_config suggested above, would that give
you what you need?

It would be an extra overhead for external pool manager to _alloc_ memory and store the allocated pointer in mempool struct(as *pool) and use pool for pointing other data structures as some implementation need only limited bytes to store the external pool manager specific context.

In order to fix this problem, We may classify fast path and slow path elements in struct rte_mempool and move all fast path elements in first cache line and create an empty opaque space in the remaining bytes in the cache line so that if the external pool manager needs only limited space then it is not required to allocate the separate memory to save the per core cache in fast-path

Quoted 7 lines

something like below,

union {
	void *pool;
	uint64_t val;
	uint8_t extra_mem[16] // available free bytes in fast path cache line

}

Something for the future, perhaps? Will the 8-bits in the flags suffice for now?

Quoted 4 lines

Other points,

1) Is it possible to remove unused is_mp in __mempool_put_bulk function as it is just a internal function.

Fixed

Quoted 5 lines

2) Considering "get" and "put" are the fast-path callbacks for pool-manger, Is it possible to avoid the extra overhead of the following _load_ and additional cache line on each call, rte_mempool_handler_table.handler[handler_idx]

Quoted 7 lines

I understand it is for multiprocess support but I am thing can we introduce something like ethernet API support for multiprocess and resolve "put" and "get" functions pointer on init and store in struct mempool. Some thinking like

file: drivers/net/ixgbe/ixgbe_ethdev.c
search for if (rte_eal_process_type() != RTE_PROC_PRIMARY) {

I'll look at this one before posting the next version of the patch (soon). :)

Quoted 2 lines

Jerin

Thanks for your input on this, much appreciated.
Dave.

#81 lore raw

Re: [PATCH v5 1/3] mempool: support external handler

Quoted 26 lines · David
Quoted 16 lines · Jerin Jacob
Quoted 4 lines · David

New mempool handlers will use rte_mempool_create_empty(), rte_mempool_set_handler(),
then rte_mempool_populate_*(). These three functions are new to this release, to no problem

Having separate APIs for external pool-manager create is worrisome in application perspective. Is it possible to have rte_mempool_[xmem]_create for the both external and existing SW pool manager and make rte_mempool_create_empty and rte_mempool_populate_* internal functions.

IMO, We can do that by selecting specific rte_mempool_set_handler() based on _flags_ encoding, something like below

bit 0 - 16   // generic bits uses across all the pool managers
bit 16 - 23  // pool handler specific flags bits
bit 24 - 31  // to select the specific pool manager(Up to 256 different flavors of
Quoted 6 lines

pool managers, For backward compatibility, make '0'(in 24-31) to select existing SW pool manager.

and applications can choose the handlers by selecting the flag in rte_mempool_[xmem]_create, That way it will be easy in testpmd or any other applications to choose the pool handler from command line etc in future.

Quoted 7 lines

There might be issues with the 8-bit handler number, as we'd have to add an api call to
first get the index of a given hander by name, then OR it into the flags. That would mean
also extra API calls for the non-default external handlers. I do agree with the handler-specific
bits though.

That would be an internal API(upper 8 bits to handler name). Right ? Seems to be OK for me.

Quoted 4 lines

Having the _empty and _set_handler APIs seems to me to be OK for the moment. Maybe Olivier could comment?

But need 3 APIs. Right? _empty , _set_handler and _populate ? I believe it is better reduce the public API in spec where ever possible ?

Maybe Olivier could comment ?

Quoted 11 lines
Quoted 5 lines

and we can remove "mbuf: get default mempool handler from configuration" change-set OR just add if RTE_MBUF_DEFAULT_MEMPOOL_HANDLER is defined then set the same with rte_mempool_set_handler in rte_mempool_[xmem]_create.

What do you think?

The "configuration" patch is to allow users to quickly change the mempool handler
by changing RTE_MBUF_DEFAULT_MEMPOOL_HANDLER to another string of a known handler. It could just as easily be left out and use the rte_mempool_create.

Yes, I understand, but I am trying to avoid build time constant. IMO, It would be better by default RTE_MBUF_DEFAULT_MEMPOOL_HANDLER is not defined in config. and for quick change developers can introduce the build with RTE_MBUF_DEFAULT_MEMPOOL_HANDLER="specific handler"

Quoted 20 lines
Quoted 14 lines
Quoted 6 lines

to add a parameter to one of them for the config data. Also since we're adding some new
elements to the mempool structure, how about we add a new pointer for a void pointer to a
config data structure, as defined by the handler.

Quoted 8 lines

So, new element in rte_mempool struct alongside the *pool

     void *pool;
     void *pool_config;

Then add a param to the rte_mempool_set_handler function:
int
rte_mempool_set_handler(struct rte_mempool *mp, const char *name, void *pool_config)

Quoted 5 lines

IMO, Maybe we need to have _set_ and _get_.So I think we can have two separate callback in external pool-manger for that if required. IMO, For now, We can live with pool manager specific 8 bits(bit 16 -23) for the configuration as mentioned above and add the new callbacks for set and get when required?

Quoted 3 lines

OK, We'll keep the config to the 8 bits of the flags for now. That will also mean I won't
add the pool_config void pointer either (for the moment)

OK to me.

Quoted 29 lines
Quoted 29 lines
Quoted 11 lines
Quoted 8 lines

2) IMO, It is better to change void *pool in struct rte_mempool to anonymous union type, something like below, so that mempool implementation can choose the best type.

	union {
		void *pool;
		uint64_t val;
	}

Could we do this by using the union for the *pool_config suggested above, would that give
you what you need?

It would be an extra overhead for external pool manager to _alloc_ memory and store the allocated pointer in mempool struct(as *pool) and use pool for pointing other data structures as some implementation need only limited bytes to store the external pool manager specific context.

In order to fix this problem, We may classify fast path and slow path elements in struct rte_mempool and move all fast path elements in first cache line and create an empty opaque space in the remaining bytes in the cache line so that if the external pool manager needs only limited space then it is not required to allocate the separate memory to save the per core cache in fast-path

something like below,

union {
	void *pool;
	uint64_t val;
	uint8_t extra_mem[16] // available free bytes in fast path cache line
Quoted 4 lines
Quoted 1 line

}

Something for the future, perhaps? Will the 8-bits in the flags suffice for now?

OK. But simple anonymous union for same type should be OK add now? Not much change I believe, If its difficult then postpone it

union {
	void *pool;
	uint64_t val;
}
Quoted 8 lines
Quoted 6 lines

Other points,

1) Is it possible to remove unused is_mp in __mempool_put_bulk function as it is just a internal function.

Fixed

__mempool_get_bulk too.

Quoted 10 lines
Quoted 10 lines

2) Considering "get" and "put" are the fast-path callbacks for pool-manger, Is it possible to avoid the extra overhead of the following _load_ and additional cache line on each call, rte_mempool_handler_table.handler[handler_idx]

I understand it is for multiprocess support but I am thing can we introduce something like ethernet API support for multiprocess and resolve "put" and "get" functions pointer on init and store in struct mempool. Some thinking like

Quoted 5 lines
Quoted 2 lines

file: drivers/net/ixgbe/ixgbe_ethdev.c
search for if (rte_eal_process_type() != RTE_PROC_PRIMARY) {

I'll look at this one before posting the next version of the patch (soon). :)

OK

Quoted 7 lines
Quoted 4 lines

Jerin

Thanks for your input on this, much appreciated.
Dave.

#82 lore raw

Re: [PATCH v5 1/3] mempool: support external handler

Hi,

Quoted 39 lines · Jerin Jacob
Quoted 26 lines · David
Quoted 16 lines · Jerin Jacob
Quoted 4 lines · David

New mempool handlers will use rte_mempool_create_empty(), rte_mempool_set_handler(),
then rte_mempool_populate_*(). These three functions are new to this release, to no problem

Having separate APIs for external pool-manager create is worrisome in application perspective. Is it possible to have rte_mempool_[xmem]_create for the both external and existing SW pool manager and make rte_mempool_create_empty and rte_mempool_populate_* internal functions.

IMO, We can do that by selecting specific rte_mempool_set_handler() based on _flags_ encoding, something like below

bit 0 - 16   // generic bits uses across all the pool managers
bit 16 - 23  // pool handler specific flags bits
bit 24 - 31  // to select the specific pool manager(Up to 256 different flavors of
Quoted 6 lines

pool managers, For backward compatibility, make '0'(in 24-31) to select existing SW pool manager.

and applications can choose the handlers by selecting the flag in rte_mempool_[xmem]_create, That way it will be easy in testpmd or any other applications to choose the pool handler from command line etc in future.

Quoted 7 lines

There might be issues with the 8-bit handler number, as we'd have to add an api call to
first get the index of a given hander by name, then OR it into the flags. That would mean
also extra API calls for the non-default external handlers. I do agree with the handler-specific
bits though.

That would be an internal API(upper 8 bits to handler name). Right ? Seems to be OK for me.

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

Having the _empty and _set_handler APIs seems to me to be OK for the moment. Maybe Olivier could comment?

But need 3 APIs. Right? _empty , _set_handler and _populate ? I believe it is better reduce the public API in spec where ever possible ?

Maybe Olivier could comment ?

Well, I think having 3 different functions is not a problem if the API is clearer.

In my opinion, the following:
rte_mempool_create_empty()
rte_mempool_set_handler()
rte_mempool_populate()

is clearer than:
rte_mempool_create(15 args)

Splitting the flags into 3 groups, with one not beeing flags but a pool handler number looks overcomplicated from a user perspective.

Quoted 12 lines
Quoted 11 lines
Quoted 5 lines

and we can remove "mbuf: get default mempool handler from configuration" change-set OR just add if RTE_MBUF_DEFAULT_MEMPOOL_HANDLER is defined then set the same with rte_mempool_set_handler in rte_mempool_[xmem]_create.

What do you think?

The "configuration" patch is to allow users to quickly change the mempool handler
by changing RTE_MBUF_DEFAULT_MEMPOOL_HANDLER to another string of a known handler. It could just as easily be left out and use the rte_mempool_create.

Quoted 4 lines

Yes, I understand, but I am trying to avoid build time constant. IMO, It would be better by default RTE_MBUF_DEFAULT_MEMPOOL_HANDLER is not defined in config. and for quick change developers can introduce the build with RTE_MBUF_DEFAULT_MEMPOOL_HANDLER="specific handler"

My understanding of the compile-time configuration option was to allow a specific architecture to define a specific hw-assisted handler by default.

Indeed, if there is no such need for now, we may remove it. But we need a way to select another handler, at least in test-pmd (in command line arguments?).

Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Quoted 6 lines

to add a parameter to one of them for the config data. Also since we're adding some new
elements to the mempool structure, how about we add a new pointer for a void pointer to a
config data structure, as defined by the handler.

Quoted 8 lines

So, new element in rte_mempool struct alongside the *pool

     void *pool;
     void *pool_config;

Then add a param to the rte_mempool_set_handler function:
int
rte_mempool_set_handler(struct rte_mempool *mp, const char *name, void *pool_config)

IMO, Maybe we need to have _set_ and _get_.So I think we can have two separate callback in external pool-manger for that if required. IMO, For now, We can live with pool manager specific 8 bits(bit 16 -23) for the configuration as mentioned above and add the new callbacks for set and get when required?

Quoted 5 lines
Quoted 3 lines

OK, We'll keep the config to the 8 bits of the flags for now. That will also mean I won't
add the pool_config void pointer either (for the moment)

OK to me.

I'm not sure I'm getting it. Does it mean having something like this ?

rte_mempool_set_handler(struct rte_mempool *mp, const char *name,
unsigned int flags)

Or does it mean some of the flags passed to rte_mempool_create*() will be specific to some handlers?

Before adding handler-specific flags or config, can we ensure we will need them? What kind of handler-specific configuration/flags do you think we will need? Just an idea: what about having a global configuration for all mempools using a given handler?

Quoted 33 lines
Quoted 28 lines
Quoted 28 lines
Quoted 10 lines
Quoted 7 lines

2) IMO, It is better to change void *pool in struct rte_mempool to anonymous union type, something like below, so that mempool implementation can choose the best type.

	union {
		void *pool;
		uint64_t val;
	}

Could we do this by using the union for the *pool_config suggested above, would that give
you what you need?

It would be an extra overhead for external pool manager to _alloc_ memory and store the allocated pointer in mempool struct(as *pool) and use pool for pointing other data structures as some implementation need only limited bytes to store the external pool manager specific context.

In order to fix this problem, We may classify fast path and slow path elements in struct rte_mempool and move all fast path elements in first cache line and create an empty opaque space in the remaining bytes in the cache line so that if the external pool manager needs only limited space then it is not required to allocate the separate memory to save the per core cache in fast-path

something like below,

union {
	void *pool;
	uint64_t val;
	uint8_t extra_mem[16] // available free bytes in fast path cache line
Quoted 4 lines
Quoted 1 line

}

Something for the future, perhaps? Will the 8-bits in the flags suffice for now?

Quoted 7 lines

OK. But simple anonymous union for same type should be OK add now? Not much change I believe, If its difficult then postpone it

union {
	void *pool;
	uint64_t val;
}

I'm ok with the simple union with (void *) and (uint64_t). Maybe "val" should be replaced by something more appropriate. Is "pool_id" a better name?

Thanks David for working on this, and thanks Jerin and Jan for the good comments and suggestions!

Regards
Olivier

#83 lore raw

Re: [PATCH v5 1/3] mempool: support external handler

Quoted 54 lines · Olivier MATZ

Hi,

Quoted 39 lines · Jerin Jacob
Quoted 26 lines · David
Quoted 16 lines · Jerin Jacob
Quoted 4 lines · David

New mempool handlers will use rte_mempool_create_empty(), rte_mempool_set_handler(),
then rte_mempool_populate_*(). These three functions are new to this release, to no problem

Having separate APIs for external pool-manager create is worrisome in application perspective. Is it possible to have rte_mempool_[xmem]_create for the both external and existing SW pool manager and make rte_mempool_create_empty and rte_mempool_populate_* internal functions.

IMO, We can do that by selecting specific rte_mempool_set_handler() based on _flags_ encoding, something like below

bit 0 - 16   // generic bits uses across all the pool managers
bit 16 - 23  // pool handler specific flags bits
bit 24 - 31  // to select the specific pool manager(Up to 256 different flavors of
Quoted 6 lines

pool managers, For backward compatibility, make '0'(in 24-31) to select existing SW pool manager.

and applications can choose the handlers by selecting the flag in rte_mempool_[xmem]_create, That way it will be easy in testpmd or any other applications to choose the pool handler from command line etc in future.

Quoted 7 lines

There might be issues with the 8-bit handler number, as we'd have to add an api call to
first get the index of a given hander by name, then OR it into the flags. That would mean
also extra API calls for the non-default external handlers. I do agree with the handler-specific
bits though.

That would be an internal API(upper 8 bits to handler name). Right ? Seems to be OK for me.

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

Having the _empty and _set_handler APIs seems to me to be OK for the moment. Maybe Olivier could comment?

But need 3 APIs. Right? _empty , _set_handler and _populate ? I believe it is better reduce the public API in spec where ever possible ?

Maybe Olivier could comment ?

Well, I think having 3 different functions is not a problem if the API is clearer.

Quoted 7 lines

In my opinion, the following:
rte_mempool_create_empty()
rte_mempool_set_handler()
rte_mempool_populate()

is clearer than:
rte_mempool_create(15 args)

But proposed scheme is not adding any new arguments to rte_mempool_create. It just extending the existing flag.

rte_mempool_create(15 args) is still their as API for internal pool creation.

Quoted 3 lines

Splitting the flags into 3 groups, with one not beeing flags but a pool handler number looks overcomplicated from a user perspective.

I am concerned with seem less integration with existing applications, IMO, Its not worth having separate functions for external vs internal pool creation for application(now each every applications has to added this logic every where for no good reason), just my 2 cents.

Quoted 18 lines
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines

and we can remove "mbuf: get default mempool handler from configuration" change-set OR just add if RTE_MBUF_DEFAULT_MEMPOOL_HANDLER is defined then set the same with rte_mempool_set_handler in rte_mempool_[xmem]_create.

Quoted 7 lines
Quoted 1 line

What do you think?

The "configuration" patch is to allow users to quickly change the mempool handler
by changing RTE_MBUF_DEFAULT_MEMPOOL_HANDLER to another string of a known handler. It could just as easily be left out and use the rte_mempool_create.

Quoted 4 lines

Yes, I understand, but I am trying to avoid build time constant. IMO, It would be better by default RTE_MBUF_DEFAULT_MEMPOOL_HANDLER is not defined in config. and for quick change developers can introduce the build with RTE_MBUF_DEFAULT_MEMPOOL_HANDLER="specific handler"

Quoted 7 lines

My understanding of the compile-time configuration option was to allow a specific architecture to define a specific hw-assisted handler by default.

Indeed, if there is no such need for now, we may remove it. But we need a way to select another handler, at least in test-pmd (in command line arguments?).

like txflags in testpmd, IMO, mempool flags will help to select the handlers seamlessly as suggest above.

If we are _not_ taking the flags based selection scheme then it makes to keep RTE_MBUF_DEFAULT_MEMPOOL_HANDLER

Quoted 34 lines
Quoted 22 lines
Quoted 22 lines
Quoted 21 lines
Quoted 8 lines

to add a parameter to one of them for the config data. Also since we're adding some new
elements to the mempool structure, how about we add a new pointer for a void pointer to a
config data structure, as defined by the handler.

Quoted 8 lines

So, new element in rte_mempool struct alongside the *pool

     void *pool;
     void *pool_config;

Then add a param to the rte_mempool_set_handler function:
int
rte_mempool_set_handler(struct rte_mempool *mp, const char *name, void *pool_config)

IMO, Maybe we need to have _set_ and _get_.So I think we can have two separate callback in external pool-manger for that if required. IMO, For now, We can live with pool manager specific 8 bits(bit 16 -23) for the configuration as mentioned above and add the new callbacks for set and get when required?

Quoted 5 lines
Quoted 3 lines

OK, We'll keep the config to the 8 bits of the flags for now. That will also mean I won't
add the pool_config void pointer either (for the moment)

OK to me.

I'm not sure I'm getting it. Does it mean having something like this ?

rte_mempool_set_handler(struct rte_mempool *mp, const char *name,
unsigned int flags)

Quoted 8 lines

Or does it mean some of the flags passed to rte_mempool_create*() will be specific to some handlers?

Before adding handler-specific flags or config, can we ensure we will need them? What kind of handler-specific configuration/flags do you think we will need? Just an idea: what about having a global configuration for all mempools using a given handler?

We may need to configure external pool manager like don't free the packets back to pool after it has been send out(just an example of valid external HW pool manager configuration)

Quoted 39 lines
Quoted 39 lines
Quoted 31 lines
Quoted 31 lines
Quoted 10 lines
Quoted 10 lines

2) IMO, It is better to change void *pool in struct rte_mempool to anonymous union type, something like below, so that mempool implementation can choose the best type.

	union {
		void *pool;
		uint64_t val;
	}
Quoted 3 lines

Could we do this by using the union for the *pool_config suggested above, would that give
you what you need?

It would be an extra overhead for external pool manager to _alloc_ memory and store the allocated pointer in mempool struct(as *pool) and use pool for pointing other data structures as some implementation need only limited bytes to store the external pool manager specific context.

In order to fix this problem, We may classify fast path and slow path elements in struct rte_mempool and move all fast path elements in first cache line and create an empty opaque space in the remaining bytes in the cache line so that if the external pool manager needs only limited space then it is not required to allocate the separate memory to save the per core cache in fast-path

something like below,

union {
	void *pool;
	uint64_t val;
	uint8_t extra_mem[16] // available free bytes in fast path cache line
Quoted 4 lines
Quoted 1 line

}

Something for the future, perhaps? Will the 8-bits in the flags suffice for now?

OK. But simple anonymous union for same type should be OK add now? Not much change I believe, If its difficult then postpone it

Quoted 8 lines
Quoted 4 lines
union {
	void *pool;
	uint64_t val;
}

I'm ok with the simple union with (void *) and (uint64_t). Maybe "val" should be replaced by something more appropriate. Is "pool_id" a better name?

How about "opaque"?

Quoted 7 lines

Thanks David for working on this, and thanks Jerin and Jan for the good comments and suggestions!

Regards
Olivier

#84 lore raw

Re: [PATCH v5 1/3] mempool: support external handler

Quoted 62 lines · Jerin Jacob
Quoted 48 lines · Olivier MATZ

Hi,

Quoted 35 lines · Jerin Jacob
Quoted 24 lines · David
Quoted 16 lines · Jerin Jacob
Quoted 4 lines · David

New mempool handlers will use rte_mempool_create_empty(), rte_mempool_set_handler(),
then rte_mempool_populate_*(). These three functions are new to this release, to no problem

Having separate APIs for external pool-manager create is worrisome in application perspective. Is it possible to have rte_mempool_[xmem]_create for the both external and existing SW pool manager and make rte_mempool_create_empty and rte_mempool_populate_* internal functions.

IMO, We can do that by selecting specific rte_mempool_set_handler() based on _flags_ encoding, something like below

bit 0 - 16   // generic bits uses across all the pool managers
bit 16 - 23  // pool handler specific flags bits
bit 24 - 31  // to select the specific pool manager(Up to 256 different flavors of
Quoted 6 lines

pool managers, For backward compatibility, make '0'(in 24-31) to select existing SW pool manager.

and applications can choose the handlers by selecting the flag in rte_mempool_[xmem]_create, That way it will be easy in testpmd or any other applications to choose the pool handler from command line etc in future.

Quoted 7 lines

There might be issues with the 8-bit handler number, as we'd have to add an api call to
first get the index of a given hander by name, then OR it into the flags. That would mean
also extra API calls for the non-default external handlers. I do agree with the handler-specific
bits though.

That would be an internal API(upper 8 bits to handler name). Right ? Seems to be OK for me.

Quoted 7 lines
Quoted 3 lines

Having the _empty and _set_handler APIs seems to me to be OK for the moment. Maybe Olivier could comment?

But need 3 APIs. Right? _empty , _set_handler and _populate ? I believe it is better reduce the public API in spec where ever possible ?

Maybe Olivier could comment ?

Well, I think having 3 different functions is not a problem if the API is clearer.

Quoted 7 lines

In my opinion, the following:
rte_mempool_create_empty()
rte_mempool_set_handler()
rte_mempool_populate()

is clearer than:
rte_mempool_create(15 args)

But proposed scheme is not adding any new arguments to rte_mempool_create. It just extending the existing flag.

rte_mempool_create(15 args) is still their as API for internal pool creation.

Quoted 6 lines
Quoted 2 lines

Splitting the flags into 3 groups, with one not beeing flags but a pool handler number looks overcomplicated from a user perspective.

I am concerned with seem less integration with existing applications, IMO, Its not worth having separate functions for external vs internal pool creation for application(now each every applications has to added this logic every where for no good reason), just my 2 cents.

I think that there is always going to be some extra code in the applications
that want to use an external mempool. The _set_handler approach does
create, set_hander, populate. The Flags method queries the handler list to get the index, sets the flags bits, then calls create. Both methods will work.

But I think the _set_handler approach is more user friendly, therefore that it the method I would lean towards.

Quoted 18 lines
Quoted 18 lines
Quoted 10 lines
Quoted 10 lines
Quoted 5 lines

and we can remove "mbuf: get default mempool handler from configuration" change-set OR just add if RTE_MBUF_DEFAULT_MEMPOOL_HANDLER is defined then set the same with rte_mempool_set_handler in rte_mempool_[xmem]_create.

What do you think?

The "configuration" patch is to allow users to quickly change the mempool handler
by changing RTE_MBUF_DEFAULT_MEMPOOL_HANDLER to another string of a known handler. It could just as easily be left out and use the rte_mempool_create.

Quoted 4 lines

Yes, I understand, but I am trying to avoid build time constant. IMO, It would be better by default RTE_MBUF_DEFAULT_MEMPOOL_HANDLER is not defined in config. and for quick change developers can introduce the build with RTE_MBUF_DEFAULT_MEMPOOL_HANDLER="specific handler"

My understanding of the compile-time configuration option was to allow a specific architecture to define a specific hw-assisted handler by default.

Quoted 8 lines
Quoted 3 lines

Indeed, if there is no such need for now, we may remove it. But we need a way to select another handler, at least in test-pmd (in command line arguments?).

like txflags in testpmd, IMO, mempool flags will help to select the handlers seamlessly as suggest above.

If we are _not_ taking the flags based selection scheme then it makes to keep RTE_MBUF_DEFAULT_MEMPOOL_HANDLER

see comment above

Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
Quoted 28 lines
Quoted 28 lines
Quoted 10 lines
Quoted 7 lines

2) IMO, It is better to change void *pool in struct rte_mempool to anonymous union type, something like below, so that mempool implementation can choose the best type.

	union {
		void *pool;
		uint64_t val;
	}

Could we do this by using the union for the *pool_config suggested above, would that give
you what you need?

It would be an extra overhead for external pool manager to _alloc_ memory and store the allocated pointer in mempool struct(as *pool) and use pool for pointing other data structures as some implementation need only limited bytes to store the external pool manager specific context.

In order to fix this problem, We may classify fast path and slow path elements in struct rte_mempool and move all fast path elements in first cache line and create an empty opaque space in the remaining bytes in the cache line so that if the external pool manager needs only limited space then it is not required to allocate the separate memory to save the per core cache in fast-path

something like below,

union {
	void *pool;
	uint64_t val;
	uint8_t extra_mem[16] // available free bytes in fast path cache line
Quoted 3 lines
Quoted 1 line

}

Something for the future, perhaps? Will the 8-bits in the flags suffice for now?

OK. But simple anonymous union for same type should be OK add now? Not much change I believe, If its difficult then postpone it

Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
union {
	void *pool;
	uint64_t val;
}

I'm ok with the simple union with (void *) and (uint64_t). Maybe "val" should be replaced by something more appropriate. Is "pool_id" a better name?

How about "opaque"?

I think I would lean towards pool_id in this case.

Regards,
David.

#85 lore raw

Re: [PATCH v5 1/3] mempool: support external handler

Quoted 72 lines · David
Quoted 62 lines · Jerin Jacob
Quoted 48 lines · Olivier MATZ

Hi,

Quoted 35 lines · Jerin Jacob
Quoted 24 lines · David
Quoted 16 lines · Jerin Jacob
Quoted 4 lines · David

New mempool handlers will use rte_mempool_create_empty(), rte_mempool_set_handler(),
then rte_mempool_populate_*(). These three functions are new to this release, to no problem

Having separate APIs for external pool-manager create is worrisome in application perspective. Is it possible to have rte_mempool_[xmem]_create for the both external and existing SW pool manager and make rte_mempool_create_empty and rte_mempool_populate_* internal functions.

IMO, We can do that by selecting specific rte_mempool_set_handler() based on _flags_ encoding, something like below

bit 0 - 16   // generic bits uses across all the pool managers
bit 16 - 23  // pool handler specific flags bits
bit 24 - 31  // to select the specific pool manager(Up to 256 different flavors of
Quoted 6 lines

pool managers, For backward compatibility, make '0'(in 24-31) to select existing SW pool manager.

and applications can choose the handlers by selecting the flag in rte_mempool_[xmem]_create, That way it will be easy in testpmd or any other applications to choose the pool handler from command line etc in future.

Quoted 7 lines

There might be issues with the 8-bit handler number, as we'd have to add an api call to
first get the index of a given hander by name, then OR it into the flags. That would mean
also extra API calls for the non-default external handlers. I do agree with the handler-specific
bits though.

That would be an internal API(upper 8 bits to handler name). Right ? Seems to be OK for me.

Quoted 7 lines
Quoted 3 lines

Having the _empty and _set_handler APIs seems to me to be OK for the moment. Maybe Olivier could comment?

But need 3 APIs. Right? _empty , _set_handler and _populate ? I believe it is better reduce the public API in spec where ever possible ?

Maybe Olivier could comment ?

Well, I think having 3 different functions is not a problem if the API is clearer.

Quoted 7 lines

In my opinion, the following:
rte_mempool_create_empty()
rte_mempool_set_handler()
rte_mempool_populate()

is clearer than:
rte_mempool_create(15 args)

But proposed scheme is not adding any new arguments to rte_mempool_create. It just extending the existing flag.

rte_mempool_create(15 args) is still their as API for internal pool creation.

Quoted 6 lines
Quoted 2 lines

Splitting the flags into 3 groups, with one not beeing flags but a pool handler number looks overcomplicated from a user perspective.

I am concerned with seem less integration with existing applications, IMO, Its not worth having separate functions for external vs internal pool creation for application(now each every applications has to added this logic every where for no good reason), just my 2 cents.

Quoted 6 lines

I think that there is always going to be some extra code in the applications
that want to use an external mempool. The _set_handler approach does
create, set_hander, populate. The Flags method queries the handler list to get the index, sets the flags bits, then calls create. Both methods will work.

I was suggesting flags like TXQ in ethdev where application just selects the mode. Not sure why application has to get the index first.

some thing like,
#define ETH_TXQ_FLAGS_NOMULTSEGS 0x0001 /**< nb_segs=1 for all mbufs */ #define ETH_TXQ_FLAGS_NOREFCOUNT 0x0002 /**< refcnt can be ignored */ #define ETH_TXQ_FLAGS_NOMULTMEMP 0x0004 /**< all bufs come from same mempool */

Anyway, Looks like no one else much bothered about external pool manger creation API being different. So, I given up. No objections from my side :-)

Quoted 25 lines

But I think the _set_handler approach is more user friendly, therefore that it the method I would lean towards.

Quoted 21 lines
Quoted 21 lines
Quoted 10 lines
Quoted 10 lines
Quoted 5 lines

and we can remove "mbuf: get default mempool handler from configuration" change-set OR just add if RTE_MBUF_DEFAULT_MEMPOOL_HANDLER is defined then set the same with rte_mempool_set_handler in rte_mempool_[xmem]_create.

What do you think?

The "configuration" patch is to allow users to quickly change the mempool handler
by changing RTE_MBUF_DEFAULT_MEMPOOL_HANDLER to another string of a known handler. It could just as easily be left out and use the rte_mempool_create.

Quoted 4 lines

Yes, I understand, but I am trying to avoid build time constant. IMO, It would be better by default RTE_MBUF_DEFAULT_MEMPOOL_HANDLER is not defined in config. and for quick change developers can introduce the build with RTE_MBUF_DEFAULT_MEMPOOL_HANDLER="specific handler"

My understanding of the compile-time configuration option was to allow a specific architecture to define a specific hw-assisted handler by default.

Indeed, if there is no such need for now, we may remove it. But we need a way to select another handler, at least in test-pmd (in command line arguments?).

Quoted 7 lines
Quoted 5 lines

like txflags in testpmd, IMO, mempool flags will help to select the handlers seamlessly as suggest above.

If we are _not_ taking the flags based selection scheme then it makes to keep RTE_MBUF_DEFAULT_MEMPOOL_HANDLER

see comment above

Try to add some means to select the external handler for existing applications so that we can test existing applications in different modes.

Thanks,
Jerin

Quoted 49 lines
Quoted 35 lines
Quoted 35 lines
Quoted 35 lines
Quoted 29 lines
Quoted 29 lines
Quoted 11 lines
Quoted 8 lines

2) IMO, It is better to change void *pool in struct rte_mempool to anonymous union type, something like below, so that mempool implementation can choose the best type.

	union {
		void *pool;
		uint64_t val;
	}

Could we do this by using the union for the *pool_config suggested above, would that give
you what you need?

It would be an extra overhead for external pool manager to _alloc_ memory and store the allocated pointer in mempool struct(as *pool) and use pool for pointing other data structures as some implementation need only limited bytes to store the external pool manager specific context.

In order to fix this problem, We may classify fast path and slow path elements in struct rte_mempool and move all fast path elements in first cache line and create an empty opaque space in the remaining bytes in the cache line so that if the external pool manager needs only limited space then it is not required to allocate the separate memory to save the per core cache in fast-path

something like below,

union {
	void *pool;
	uint64_t val;
	uint8_t extra_mem[16] // available free bytes in fast path cache line
Quoted 3 lines
Quoted 1 line

}

Something for the future, perhaps? Will the 8-bits in the flags suffice for now?

OK. But simple anonymous union for same type should be OK add now? Not much change I believe, If its difficult then postpone it

Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
union {
	void *pool;
	uint64_t val;
}

I'm ok with the simple union with (void *) and (uint64_t). Maybe "val" should be replaced by something more appropriate. Is "pool_id" a better name?

How about "opaque"?

I think I would lean towards pool_id in this case.

Regards,
David.

author
#86 lore raw

PATCH v5 2/3 app/test: test external mempool handler

In reply to David Hunt

Use a minimal custom mempool external handler and check that it also passes basic mempool autotests.

---
 app/test/test_mempool.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 113 insertions(+)
app/test/test_mempool.c +113 -0
index 9f02758..f55d126 100644
@@ -85,6 +85,96 @@
 static rte_atomic32_t synchro;
 
 /*
+ * Simple example of custom mempool structure. Holds pointers to all the
+ * elements which are simply malloc'd in this example.
+ */
+struct custom_mempool {
+	rte_spinlock_t lock;
+	unsigned count;
+	unsigned size;
+	void *elts[];
+};
+
+/*
+ * Loop though all the element pointers and allocate a chunk of memory, then
+ * insert that memory into the ring.
+ */
+static void *
+custom_mempool_alloc(struct rte_mempool *mp)
+{
+	struct custom_mempool *cm;
+
+	cm = rte_zmalloc("custom_mempool",
+		sizeof(struct custom_mempool) + mp->size * sizeof(void *), 0);
+	if (cm == NULL)
+		return NULL;
+
+	rte_spinlock_init(&cm->lock);
+	cm->count = 0;
+	cm->size = mp->size;
+	return cm;
+}
+
+static void
+custom_mempool_free(void *p)
+{
+	rte_free(p);
+}
+
+static int
+custom_mempool_put(void *p, void * const *obj_table, unsigned n)
+{
+	struct custom_mempool *cm = (struct custom_mempool *)p;
+	int ret = 0;
+
+	rte_spinlock_lock(&cm->lock);
+	if (cm->count + n > cm->size) {
+		ret = -ENOBUFS;
+	} else {
+		memcpy(&cm->elts[cm->count], obj_table, sizeof(void *) * n);
+		cm->count += n;
+	}
+	rte_spinlock_unlock(&cm->lock);
+	return ret;
+}
+
+
+static int
+custom_mempool_get(void *p, void **obj_table, unsigned n)
+{
+	struct custom_mempool *cm = (struct custom_mempool *)p;
+	int ret = 0;
+
+	rte_spinlock_lock(&cm->lock);
+	if (n > cm->count) {
+		ret = -ENOENT;
+	} else {
+		cm->count -= n;
+		memcpy(obj_table, &cm->elts[cm->count], sizeof(void *) * n);
+	}
+	rte_spinlock_unlock(&cm->lock);
+	return ret;
+}
+
+static unsigned
+custom_mempool_get_count(void *p)
+{
+	struct custom_mempool *cm = (struct custom_mempool *)p;
+	return cm->count;
+}
+
+static struct rte_mempool_handler mempool_handler_custom = {
+	.name = "custom_handler",
+	.alloc = custom_mempool_alloc,
+	.free = custom_mempool_free,
+	.put = custom_mempool_put,
+	.get = custom_mempool_get,
+	.get_count = custom_mempool_get_count,
+};
+
+MEMPOOL_REGISTER_HANDLER(mempool_handler_custom);
+
+/*
  * save the object number in the first 4 bytes of object data. All
  * other bytes are set to 0.
  */
@@ -479,6 +569,7 @@ test_mempool(void)
 {
 	struct rte_mempool *mp_cache = NULL;
 	struct rte_mempool *mp_nocache = NULL;
+	struct rte_mempool *mp_ext = NULL;
 
 	rte_atomic32_init(&synchro);
 
@@ -507,6 +598,27 @@ test_mempool(void)
 		goto err;
 	}
 
+	/* create a mempool with an external handler */
+	mp_ext = rte_mempool_create_empty("test_ext",
+		MEMPOOL_SIZE,
+		MEMPOOL_ELT_SIZE,
+		RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
+		SOCKET_ID_ANY, 0);
+
+	if (mp_ext == NULL) {
+		printf("cannot allocate mp_ext mempool\n");
+		goto err;
+	}
+	if (rte_mempool_set_handler(mp_ext, "custom_handler") < 0) {
+		printf("cannot set custom handler\n");
+		goto err;
+	}
+	if (rte_mempool_populate_default(mp_ext) < 0) {
+		printf("cannot populate mp_ext mempool\n");
+		goto err;
+	}
+	rte_mempool_obj_iter(mp_ext, my_obj_init, NULL);
+
 	/* retrieve the mempool from its name */
 	if (rte_mempool_lookup("test_nocache") != mp_nocache) {
 		printf("Cannot lookup mempool from its name\n");
@@ -547,6 +659,7 @@ test_mempool(void)
 err:
 	rte_mempool_free(mp_nocache);
 	rte_mempool_free(mp_cache);
+	rte_mempool_free(mp_ext);
 	return -1;
 }
 
Signature · 1 line
2.5.5
#87 lore raw

Re: [dpdk-dev, v5, 2/3] app/test: test external mempool handler

Quoted 20 lines · On Thu, 19 May 2016 14:45:00 +0100 David Hunt <[email protected]> wrote:

Use a minimal custom mempool external handler and check that it also passes basic mempool autotests.

---
app/test/test_mempool.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++

 1 file changed, 113 insertions(+)
app/test/test_mempool.c +2 -0
index 9f02758..f55d126 100644
@@ -85,6 +85,96 @@
 static rte_atomic32_t synchro;
 
 /*
+ * Simple example of custom mempool structure. Holds pointers to all the
+ * elements which are simply malloc'd in this example.

app/test/test_mempool.c line 99

Quoted 10 lines
app/test/test_mempool.c +10 -0
+ */
+struct custom_mempool {
+	rte_spinlock_t lock;
+	unsigned count;
+	unsigned size;
+	void *elts[];
+};
+
+/*
+ * Loop though all the element pointers and allocate a chunk of memory, then

s/though/through/

Quoted 66 lines
app/test/test_mempool.c +66 -0
+ * insert that memory into the ring.
+ */
+static void *
+custom_mempool_alloc(struct rte_mempool *mp)
+{
+	struct custom_mempool *cm;
+
+	cm = rte_zmalloc("custom_mempool",
+		sizeof(struct custom_mempool) + mp->size * sizeof(void *), 0);
+	if (cm == NULL)
+		return NULL;
+
+	rte_spinlock_init(&cm->lock);
+	cm->count = 0;
+	cm->size = mp->size;
+	return cm;
+}
+
+static void
+custom_mempool_free(void *p)
+{
+	rte_free(p);
+}
+
+static int
+custom_mempool_put(void *p, void * const *obj_table, unsigned n)
+{
+	struct custom_mempool *cm = (struct custom_mempool *)p;
+	int ret = 0;
+
+	rte_spinlock_lock(&cm->lock);
+	if (cm->count + n > cm->size) {
+		ret = -ENOBUFS;
+	} else {
+		memcpy(&cm->elts[cm->count], obj_table, sizeof(void *) * n);
+		cm->count += n;
+	}
+	rte_spinlock_unlock(&cm->lock);
+	return ret;
+}
+
+
+static int
+custom_mempool_get(void *p, void **obj_table, unsigned n)
+{
+	struct custom_mempool *cm = (struct custom_mempool *)p;
+	int ret = 0;
+
+	rte_spinlock_lock(&cm->lock);
+	if (n > cm->count) {
+		ret = -ENOENT;
+	} else {
+		cm->count -= n;
+		memcpy(obj_table, &cm->elts[cm->count], sizeof(void *) * n);
+	}
+	rte_spinlock_unlock(&cm->lock);
+	return ret;
+}
+
+static unsigned
+custom_mempool_get_count(void *p)
+{
+	struct custom_mempool *cm = (struct custom_mempool *)p;
+	return cm->count;
+}
+

app/test/test_mempool.c line 175

Quoted 10 lines
app/test/test_mempool.c +10 -0
+static struct rte_mempool_handler mempool_handler_custom = {
+	.name = "custom_handler",
+	.alloc = custom_mempool_alloc,
+	.free = custom_mempool_free,
+	.put = custom_mempool_put,
+	.get = custom_mempool_get,
+	.get_count = custom_mempool_get_count,
+};
+
+MEMPOOL_REGISTER_HANDLER(mempool_handler_custom);

What about to drop the rte_mempool_handler.name field and derive the name from the variable name given to the MEMPOOL_REGISTER_HANDLER. The MEMPOOL_REGISTER_HANDLER sould do some macro magic inside and call

  rte_mempool_handler_register(name, handler);

Just an idea...

Quoted 28 lines
app/test/test_mempool.c +14 -0
+
+/*
  * save the object number in the first 4 bytes of object data. All
  * other bytes are set to 0.
  */
@@ -479,6 +569,7 @@ test_mempool(void)
 {
 	struct rte_mempool *mp_cache = NULL;
 	struct rte_mempool *mp_nocache = NULL;
+	struct rte_mempool *mp_ext = NULL;
 
 	rte_atomic32_init(&synchro);
 
@@ -507,6 +598,27 @@ test_mempool(void)
 		goto err;
 	}
 
+	/* create a mempool with an external handler */
+	mp_ext = rte_mempool_create_empty("test_ext",
+		MEMPOOL_SIZE,
+		MEMPOOL_ELT_SIZE,
+		RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
+		SOCKET_ID_ANY, 0);
+
+	if (mp_ext == NULL) {
+		printf("cannot allocate mp_ext mempool\n");
+		goto err;
+	}

app/test/test_mempool.c test_mempool(void) · line 621

Quoted 10 lines
app/test/test_mempool.c +10 -0
+	if (rte_mempool_set_handler(mp_ext, "custom_handler") < 0) {
+		printf("cannot set custom handler\n");
+		goto err;
+	}
+	if (rte_mempool_populate_default(mp_ext) < 0) {
+		printf("cannot populate mp_ext mempool\n");
+		goto err;
+	}
+	rte_mempool_obj_iter(mp_ext, my_obj_init, NULL);
+

The test becomes quite complex. What about having several smaller tests with a clear setup and cleanup steps?

Quoted 11 lines
app/test/test_mempool.c +1 -0
 	/* retrieve the mempool from its name */
 	if (rte_mempool_lookup("test_nocache") != mp_nocache) {
 		printf("Cannot lookup mempool from its name\n");
@@ -547,6 +659,7 @@ test_mempool(void)
 err:
 	rte_mempool_free(mp_nocache);
 	rte_mempool_free(mp_cache);
+	rte_mempool_free(mp_ext);
 	return -1;
 }
 
#88 lore raw

Re: [dpdk-dev, v5, 2/3] app/test: test external mempool handler

Hi Jan,

Quoted 2 lines · Jan Viktorin

On Thu, 19 May 2016 14:45:00 +0100
David Hunt <[email protected]> wrote:

--snip--

Quoted 2 lines
Quoted 1 line

+ * Loop though all the element pointers and allocate a chunk of memory, then

s/though/through/

Fixed.

Quoted 14 lines

app/test/test_mempool.c line 175

Quoted 10 lines
app/test/test_mempool.c +10 -0
+static struct rte_mempool_handler mempool_handler_custom = {
+	.name = "custom_handler",
+	.alloc = custom_mempool_alloc,
+	.free = custom_mempool_free,
+	.put = custom_mempool_put,
+	.get = custom_mempool_get,
+	.get_count = custom_mempool_get_count,
+};
+
+MEMPOOL_REGISTER_HANDLER(mempool_handler_custom);

What about to drop the rte_mempool_handler.name field and derive the name from the variable name given to the MEMPOOL_REGISTER_HANDLER. The MEMPOOL_REGISTER_HANDLER sould do some macro magic inside and call

Quoted 3 lines
   rte_mempool_handler_register(name, handler);

Just an idea...

Lets see if anyone else has any strong opinions on this :)

Quoted 32 lines
Quoted 32 lines
app/test/test_mempool.c +18 -0
+
+/*
   * save the object number in the first 4 bytes of object data. All
   * other bytes are set to 0.
   */
@@ -479,6 +569,7 @@ test_mempool(void)
  {
  	struct rte_mempool *mp_cache = NULL;
  	struct rte_mempool *mp_nocache = NULL;
+	struct rte_mempool *mp_ext = NULL;
 
  	rte_atomic32_init(&synchro);
 
@@ -507,6 +598,27 @@ test_mempool(void)
  		goto err;
  	}
 
+	/* create a mempool with an external handler */
+	mp_ext = rte_mempool_create_empty("test_ext",
+		MEMPOOL_SIZE,
+		MEMPOOL_ELT_SIZE,
+		RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
+		SOCKET_ID_ANY, 0);
+
+	if (mp_ext == NULL) {
+		printf("cannot allocate mp_ext mempool\n");
+		goto err;
+	}
+	if (rte_mempool_set_handler(mp_ext, "custom_handler") < 0) {
+		printf("cannot set custom handler\n");
+		goto err;
+	}
Quoted 8 lines

app/test/test_mempool.c test_mempool(void) · line 621

Quoted 6 lines
app/test/test_mempool.c +6 -0
+	if (rte_mempool_populate_default(mp_ext) < 0) {
+		printf("cannot populate mp_ext mempool\n");
+		goto err;
+	}
+	rte_mempool_obj_iter(mp_ext, my_obj_init, NULL);
+

The test becomes quite complex. What about having several smaller tests with a clear setup and cleanup steps?

I guess that's something we can look at in the future. For the moment can we leave it?

Thanks,
Dave.

#89 lore raw

Re: [dpdk-dev, v5, 2/3] app/test: test external mempool handler

Quoted 8 lines · David

Hi Jan,

Quoted 2 lines · Jan Viktorin

On Thu, 19 May 2016 14:45:00 +0100
David Hunt <[email protected]> wrote:

--snip--

[...]

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

+

The test becomes quite complex. What about having several smaller tests with a clear setup and cleanup steps?

I guess that's something we can look at in the future. For the moment can we leave it?

Yes, just a suggestion. I think, Olivier (maintainer) should request this if needed.

Quoted 6 lines

Thanks,
Dave.

Signature · 4 lines
   Jan Viktorin                  E-mail: [email protected]
   System Architect              Web:    www.RehiveTech.com
   RehiveTech
   Brno, Czech Republic
#90 lore raw

Re: [dpdk-dev, v5, 2/3] app/test: test external mempool handler

Hi,

Quoted 10 lines · Jan Viktorin
Quoted 6 lines · David
Quoted 2 lines · Jan Viktorin

The test becomes quite complex. What about having several smaller tests with a clear setup and cleanup steps?

I guess that's something we can look at in the future. For the moment can we leave it?

Yes, just a suggestion. I think, Olivier (maintainer) should request this if needed.

Yes, I think we can let it as is for now. But I agree this is something we could enhance.

Thanks
Olivier

author
#91 lore raw

PATCH v5 3/3 mbuf: get default mempool handler from configuration

In reply to David Hunt

By default, the mempool handler used for mbuf allocations is a multi producer and multi consumer ring. We could imagine a target (maybe some network processors?) that provides an hardware-assisted pool mechanism. In this case, the default configuration for this architecture would contain a different value for RTE_MBUF_DEFAULT_MEMPOOL_HANDLER.

---
 config/common_base         |  1 +
 lib/librte_mbuf/rte_mbuf.c | 21 +++++++++++++++++----
 2 files changed, 18 insertions(+), 4 deletions(-)
config/common_base +1 -0
index 3535c6e..5cf5e52 100644
@@ -394,6 +394,7 @@ CONFIG_RTE_LIBRTE_MEMPOOL_DEBUG=n
 #
 CONFIG_RTE_LIBRTE_MBUF=y
 CONFIG_RTE_LIBRTE_MBUF_DEBUG=n
+CONFIG_RTE_MBUF_DEFAULT_MEMPOOL_HANDLER="ring_mp_mc"
 CONFIG_RTE_MBUF_REFCNT_ATOMIC=y
 CONFIG_RTE_PKTMBUF_HEADROOM=128
 
lib/librte_mbuf/rte_mbuf.c +17 -4
index eec1456..5dcdc05 100644
@@ -153,6 +153,7 @@ rte_pktmbuf_pool_create(const char *name, unsigned n,
 	unsigned cache_size, uint16_t priv_size, uint16_t data_room_size,
 	int socket_id)
 {
+	struct rte_mempool *mp;
 	struct rte_pktmbuf_pool_private mbp_priv;
 	unsigned elt_size;
 
@@ -167,10 +168,22 @@ rte_pktmbuf_pool_create(const char *name, unsigned n,
 	mbp_priv.mbuf_data_room_size = data_room_size;
 	mbp_priv.mbuf_priv_size = priv_size;
 
-	return rte_mempool_create(name, n, elt_size,
-		cache_size, sizeof(struct rte_pktmbuf_pool_private),
-		rte_pktmbuf_pool_init, &mbp_priv, rte_pktmbuf_init, NULL,
-		socket_id, 0);
+	mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
+		 sizeof(struct rte_pktmbuf_pool_private), socket_id, 0);
+	if (mp == NULL)
+		return NULL;
+
+	rte_mempool_set_handler(mp, RTE_MBUF_DEFAULT_MEMPOOL_HANDLER);
+	rte_pktmbuf_pool_init(mp, &mbp_priv);
+
+	if (rte_mempool_populate_default(mp) < 0) {
+		rte_mempool_free(mp);
+		return NULL;
+	}
+
+	rte_mempool_obj_iter(mp, rte_pktmbuf_init, NULL);
+
+	return mp;
 }
 
 /* do some sanity checks on a mbuf: panic if it fails */
Signature · 1 line
2.5.5
#92 lore raw

Re: [dpdk-dev, v5, 3/3] mbuf: get default mempool handler from configuration

Quoted 42 lines · On Thu, 19 May 2016 14:45:01 +0100 David Hunt <[email protected]> wrote:

By default, the mempool handler used for mbuf allocations is a multi producer and multi consumer ring. We could imagine a target (maybe some network processors?) that provides an hardware-assisted pool mechanism. In this case, the default configuration for this architecture would contain a different value for RTE_MBUF_DEFAULT_MEMPOOL_HANDLER.

---
config/common_base | 1 +

 lib/librte_mbuf/rte_mbuf.c | 21 +++++++++++++++++----
 2 files changed, 18 insertions(+), 4 deletions(-)
config/common_base +1 -0
index 3535c6e..5cf5e52 100644
@@ -394,6 +394,7 @@ CONFIG_RTE_LIBRTE_MEMPOOL_DEBUG=n
 #
 CONFIG_RTE_LIBRTE_MBUF=y
 CONFIG_RTE_LIBRTE_MBUF_DEBUG=n
+CONFIG_RTE_MBUF_DEFAULT_MEMPOOL_HANDLER="ring_mp_mc"
 CONFIG_RTE_MBUF_REFCNT_ATOMIC=y
 CONFIG_RTE_PKTMBUF_HEADROOM=128
 
lib/librte_mbuf/rte_mbuf.c +1 -0
index eec1456..5dcdc05 100644
@@ -153,6 +153,7 @@ rte_pktmbuf_pool_create(const char *name, unsigned n,
 	unsigned cache_size, uint16_t priv_size, uint16_t data_room_size,
 	int socket_id)
 {
+	struct rte_mempool *mp;
 	struct rte_pktmbuf_pool_private mbp_priv;
 	unsigned elt_size;
 
@@ -167,10 +168,22 @@ rte_pktmbuf_pool_create(const char *name, unsigned n,
 	mbp_priv.mbuf_data_room_size = data_room_size;
 	mbp_priv.mbuf_priv_size = priv_size;
 

lib/librte_mbuf/rte_mbuf.c rte_pktmbuf_pool_create(const char *name, unsigned n, · line 176

Quoted 10 lines
lib/librte_mbuf/rte_mbuf.c +6 -4
-	return rte_mempool_create(name, n, elt_size,
-		cache_size, sizeof(struct rte_pktmbuf_pool_private),
-		rte_pktmbuf_pool_init, &mbp_priv, rte_pktmbuf_init, NULL,
-		socket_id, 0);
+	mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
+		 sizeof(struct rte_pktmbuf_pool_private), socket_id, 0);
+	if (mp == NULL)
+		return NULL;
+
+	rte_mempool_set_handler(mp, RTE_MBUF_DEFAULT_MEMPOOL_HANDLER);

Check for a failure is missing here. Especially -EEXIST.

Quoted 13 lines
lib/librte_mbuf/rte_mbuf.c +10 -0
+	rte_pktmbuf_pool_init(mp, &mbp_priv);
+
+	if (rte_mempool_populate_default(mp) < 0) {
+		rte_mempool_free(mp);
+		return NULL;
+	}
+
+	rte_mempool_obj_iter(mp, rte_pktmbuf_init, NULL);
+
+	return mp;
 }
 
 /* do some sanity checks on a mbuf: panic if it fails */
Signature · 4 lines
   Jan Viktorin                  E-mail: [email protected]
   System Architect              Web:    www.RehiveTech.com
   RehiveTech
   Brno, Czech Republic
#93 lore raw

Re: [dpdk-dev, v5, 3/3] mbuf: get default mempool handler from configuration

Quoted 2 lines · Jan Viktorin

On Thu, 19 May 2016 14:45:01 +0100
David Hunt <[email protected]> wrote:

--snip--

Quoted 7 lines

lib/librte_mbuf/rte_mbuf.c rte_pktmbuf_pool_create(const char *name, unsigned n, · line 176

Quoted 6 lines
lib/librte_mbuf/rte_mbuf.c +6 -0
+	mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
+		 sizeof(struct rte_pktmbuf_pool_private), socket_id, 0);
+	if (mp == NULL)
+		return NULL;
+
+	rte_mempool_set_handler(mp, RTE_MBUF_DEFAULT_MEMPOOL_HANDLER);

Check for a failure is missing here. Especially -EEXIST.

Done.

--snip--

Thanks,
Dave.

author
#94 lore raw

v6 cover mempool: add external mempool manager

In reply to David Hunt

Here's the latest version of the External Mempool Manager patchset. It's re-based on top of the latest head as of 1st June 2016, including Olivier's 35-part patch series on mempool re-org [1]

[1] http://dpdk.org/ml/archives/dev/2016-May/039229.html

Note: After applying the last patch, run "make config ..." before compiling. It introduces a config file change.

Note: Hopefully I've addressed all the extensive comments over the last week. If I've missed any, please let me know, as it would not have been intentional. I hop I've responded to all comments via email on the mailing list.

v6 changes:

* Moved the flags handling from rte_mempool_create_empty to rte_mempool_create, as it's only there for backward compatibility
* Various comment additions and cleanup
* Renamed rte_mempool_handler to rte_mempool_ops
* Added a union for *pool and u64 pool_id in struct rte_mempool
* split the original patch into a few parts for easier review.
* rename functions with _ext_ to _ops_.
* addressed review comments
* renamed put and get functions to enqueue and dequeue
* changed occurences of rte_mempool_ops to const, as they contain function pointers (security)
* split out the default external mempool handler into a separate patch for easier review

v5 changes:
* rebasing, as it is dependent on another patch series [1]

v4 changes (Olivier Matz):
* remove the rte_mempool_create_ext() function. To change the handler, the user has to do the following:
- mp = rte_mempool_create_empty()
- rte_mempool_set_handler(mp, "my_handler")
- rte_mempool_populate_default(mp)
This avoids to add another function with more than 10 arguments, duplicating the doxygen comments
* change the api of rte_mempool_alloc_t: only the mempool pointer is required as all information is available in it
* change the api of rte_mempool_free_t: remove return value
* move inline wrapper functions from the .c to the .h (else they won't be inlined). This implies to have one header file (rte_mempool.h), or it would have generate cross dependencies issues.
* remove now unused MEMPOOL_F_INT_HANDLER (note: it was misused anyway due to the use of && instead of &)
* fix build in debug mode (__MEMPOOL_STAT_ADD(mp, put_pool, n) remaining)
* fix build with shared libraries (global handler has to be declared in the .map file)
* rationalize #include order
* remove unused function rte_mempool_get_handler_name()
* rename some structures, fields, functions
* remove the static in front of rte_tailq_elem rte_mempool_tailq (comment from Yuanhan)
* test the ext mempool handler in the same file than standard mempool tests, avoiding to duplicate the code
* rework the custom handler in mempool_test
* rework a bit the patch selecting default mbuf pool handler
* fix some doxygen comments

v3 changes:
* simplified the file layout, renamed to rte_mempool_handler.[hc]
* moved the default handlers into rte_mempool_default.c
* moved the example handler out into app/test/test_ext_mempool.c
* removed is_mc/is_mp change, slight perf degredation on sp cached operation
* removed stack hanler, may re-introduce at a later date
* Changes out of code reviews

v2 changes:
* There was a lot of duplicate code between rte_mempool_xmem_create and rte_mempool_create_ext. This has now been refactored and is now hopefully cleaner.
* The RTE_NEXT_ABI define is now used to allow building of the library in a format that is compatible with binaries built against previous versions of DPDK.
* Changes out of code reviews. Hopefully I've got most of them included.

The External Mempool Manager is an extension to the mempool API that allows users to add and use an external mempool manager, which allows external memory subsystems such as external hardware memory management systems and software based memory allocators to be used with DPDK.

The existing API to the internal DPDK mempool manager will remain unchanged and will be backward compatible. However, there will be an ABI breakage, as the mempool struct is changing. These changes are all contained withing RTE_NEXT_ABI defs, and the current or next code can be changed with the CONFIG_RTE_NEXT_ABI config setting

There are two aspects to external mempool manager.
1. Adding the code for your new mempool handler. This is achieved by adding a new mempool handler source file into the librte_mempool library, and using the REGISTER_MEMPOOL_HANDLER macro.
2. Using the new API to call rte_mempool_create_empty and rte_mempool_set_handler to create a new mempool
using the name parameter to identify which handler to use.

New API calls added
1. A new rte_mempool_create_empty() function
2. rte_mempool_set_handler() which sets the mempool's handler
3. An rte_mempool_populate_default() and rte_mempool_populate_anon() functions which populates the mempool using the relevant handler

Several external mempool managers may be used in the same application. A new mempool can then be created by using the new 'create' function, providing the mempool handler name to point the mempool to the relevant mempool manager callback structure.

The old 'create' function can still be called by legacy programs, and will internally work out the mempool handle based on the flags provided (single producer, single consumer, etc). By default handles are created internally to implement the built-in DPDK mempool manager and mempool types.

The external mempool manager needs to provide the following functions.
1. alloc - allocates the mempool memory, and adds each object onto a ring
2. put - puts an object back into the mempool once an application has
finished with it
3. get - gets an object from the mempool for use by the application
4. get_count - gets the number of available objects in the mempool
5. free - frees the mempool memory

Every time a get/put/get_count is called from the application/PMD, the callback for that mempool is called. These functions are in the fastpath, and any unoptimised handlers may limit performance.

The new APIs are as follows:

1. rte_mempool_create_empty

struct rte_mempool *
rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
unsigned cache_size, unsigned private_data_size,
int socket_id, unsigned flags);

2. rte_mempool_set_handler()

int
rte_mempool_set_handler(struct rte_mempool *mp, const char *name);

3. rte_mempool_populate_default()

int rte_mempool_populate_default(struct rte_mempool *mp);

4. rte_mempool_populate_anon()

int rte_mempool_populate_anon(struct rte_mempool *mp);

Please see rte_mempool.h for further information on the parameters.

The important thing to note is that the mempool handler is passed by name to rte_mempool_set_handler, which looks through the handler array to get the handler index, which is then stored in the rte_memool structure. This allow multiple processes to use the same mempool, as the function pointers are accessed via handler index.

The mempool handler structure contains callbacks to the implementation of the handler, and is set up for registration as follows:

static const struct rte_mempool_handler handler_sp_mc = {
.name = "ring_sp_mc",
.alloc = rte_mempool_common_ring_alloc,
.put = common_ring_sp_put,
.get = common_ring_mc_get,
.get_count = common_ring_get_count,
.free = common_ring_free,
};

And then the following macro will register the handler in the array of handlers

REGISTER_MEMPOOL_HANDLER(handler_mp_mc);

For and example of a simple malloc based mempool manager, see lib/librte_mempool/custom_mempool.c

For an example of API usage, please see app/test/test_mempool.c, which implements a rudimentary "custom_handler" mempool manager using simple mallocs for each mempool object. This file also contains the callbacks and self registration for the new handler.

David Hunt (4):
mempool: support external handler
mempool: remove rte_ring from rte_mempool struct
mempool: add default external mempool handler
mbuf: get default mempool handler from configuration

Olivier Matz (1):
app/test: test external mempool handler

author
#95 lore raw

PATCH v6 1/5 mempool: support external handler

Until now, the objects stored in a mempool were internally stored in a ring. This patch introduces the possibility to register external handlers replacing the ring.

The default behavior remains unchanged, but calling the new function rte_mempool_set_handler() right after rte_mempool_create_empty() allows the user to change the handler that will be used when populating the mempool.

v7 changes:
* Moved the flags handling from rte_mempool_create_empty to rte_mempool_create, as it's only there for backward compatibility
* Various comment additions and cleanup
* Renamed rte_mempool_handler to rte_mempool_ops
* Added a union for *pool and u64 pool_id in struct rte_mempool

v6 changes:
* split the original patch into a few parts for easier review.
* rename functions with _ext_ to _ops_.
* addressed some review comments
* renamed put and get functions to enqueue and dequeue
* renamed rte_mempool_handler struct to rte_mempool_handler_ops
* changed occurences of rte_mempool_handler_ops to const, as they contain function pointers (security)
* added some extra comments

v5 changes: rebasing on top of 35 patch set mempool work.

---
 lib/librte_mempool/Makefile              |   1 +
 lib/librte_mempool/rte_mempool.c         |  71 ++++------
 lib/librte_mempool/rte_mempool.h         | 235 ++++++++++++++++++++++++++++---
 lib/librte_mempool/rte_mempool_handler.c | 141 +++++++++++++++++++
 4 files changed, 384 insertions(+), 64 deletions(-)
 create mode 100644 lib/librte_mempool/rte_mempool_handler.c
lib/librte_mempool/Makefile +1 -0
index 43423e0..793745f 100644
@@ -42,6 +42,7 @@ LIBABIVER := 2
 
 # all source are stored in SRCS-y
 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_handler.c
 # install includes
 SYMLINK-$(CONFIG_RTE_LIBRTE_MEMPOOL)-include := rte_mempool.h
 
lib/librte_mempool/rte_mempool.c +27 -44

128 lines not on this page · raw message

lib/librte_mempool/rte_mempool.h +215 -20

292 lines not on this page · raw message

lib/librte_mempool/rte_mempool_handler.c new file +141 -0

141 lines not on this page · raw message

Signature · 1 line
2.5.5
#96 lore raw

Re: [PATCH v6 1/5] mempool: support external handler

Quoted 9 lines · David Hunt

Until now, the objects stored in a mempool were internally stored in a ring. This patch introduces the possibility to register external handlers replacing the ring.

The default behavior remains unchanged, but calling the new function rte_mempool_set_handler() right after rte_mempool_create_empty() allows the user to change the handler that will be used when populating the mempool.

Quoted 6 lines

v7 changes:
* Moved the flags handling from rte_mempool_create_empty to rte_mempool_create, as it's only there for backward compatibility
* Various comment additions and cleanup
* Renamed rte_mempool_handler to rte_mempool_ops
* Added a union for *pool and u64 pool_id in struct rte_mempool

These v7 changes should me merged with the v6 changes below as this is a v6 patch. Or removed altogether, as they are in the cover letter.

Quoted 11 lines

v6 changes:
* split the original patch into a few parts for easier review.
* rename functions with _ext_ to _ops_.
* addressed some review comments
* renamed put and get functions to enqueue and dequeue
* renamed rte_mempool_handler struct to rte_mempool_handler_ops
* changed occurences of rte_mempool_handler_ops to const, as they contain function pointers (security)
* added some extra comments

[...]