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 3 of 3

lore
Share & tools
lore
author
#225 lore raw

PATCH v15 2/3 app/test: test mempool handler

In reply to David Hunt

Create a minimal custom mempool handler and check that it passes basic mempool autotests.

---
 app/test/test_mempool.c | 122 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 120 insertions(+), 2 deletions(-)
app/test/test_mempool.c +120 -2
index b586249..31582d8 100644
@@ -83,6 +83,99 @@
 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 through all the element pointers and allocate a chunk of memory, then
+ * insert that memory into the ring.
+ */
+static int
+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 -ENOMEM;
+
+	rte_spinlock_init(&cm->lock);
+	cm->count = 0;
+	cm->size = mp->size;
+	mp->pool_data = cm;
+	return 0;
+}
+
+static void
+custom_mempool_free(struct rte_mempool *mp)
+{
+	rte_free((void *)(mp->pool_data));
+}
+
+static int
+custom_mempool_enqueue(struct rte_mempool *mp, void * const *obj_table,
+		unsigned n)
+{
+	struct custom_mempool *cm = (struct custom_mempool *)(mp->pool_data);
+	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_dequeue(struct rte_mempool *mp, void **obj_table, unsigned n)
+{
+	struct custom_mempool *cm = (struct custom_mempool *)(mp->pool_data);
+	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(const struct rte_mempool *mp)
+{
+	struct custom_mempool *cm = (struct custom_mempool *)(mp->pool_data);
+
+	return cm->count;
+}
+
+static struct rte_mempool_ops mempool_ops_custom = {
+	.name = "custom_handler",
+	.alloc = custom_mempool_alloc,
+	.free = custom_mempool_free,
+	.enqueue = custom_mempool_enqueue,
+	.dequeue = custom_mempool_dequeue,
+	.get_count = custom_mempool_get_count,
+};
+
+MEMPOOL_REGISTER_OPS(mempool_ops_custom);
+
+/*
  * save the object number in the first 4 bytes of object data. All
  * other bytes are set to 0.
  */
@@ -292,12 +385,14 @@ static int test_mempool_single_consumer(void)
  * test function for mempool test based on singple consumer and single producer,
  * can run on one lcore only
  */
-static int test_mempool_launch_single_consumer(__attribute__((unused)) void *arg)
+static int
+test_mempool_launch_single_consumer(__attribute__((unused)) void *arg)
 {
 	return test_mempool_single_consumer();
 }
 
-static void my_mp_init(struct rte_mempool * mp, __attribute__((unused)) void * arg)
+static void
+my_mp_init(struct rte_mempool *mp, __attribute__((unused)) void *arg)
 {
 	printf("mempool name is %s\n", mp->name);
 	/* nothing to be implemented here*/
@@ -477,6 +572,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);
 
@@ -505,6 +601,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_ops_byname(mp_ext, "custom_handler", NULL) < 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");
@@ -545,6 +662,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
author
#226 lore raw

PATCH v15 3/3 mbuf: make default mempool ops configurable at build

In reply to David Hunt

By default, the mempool ops 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_OPS.

---
 config/common_base         |  1 +
 lib/librte_mbuf/rte_mbuf.c | 26 ++++++++++++++++++++++----
 2 files changed, 23 insertions(+), 4 deletions(-)
config/common_base +1 -0
index 11ac81e..5f230db 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_OPS="ring_mp_mc"
 CONFIG_RTE_MBUF_REFCNT_ATOMIC=y
 CONFIG_RTE_PKTMBUF_HEADROOM=128
 
lib/librte_mbuf/rte_mbuf.c +22 -4
index 2ece742..8cf5436 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,27 @@ 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_errno = rte_mempool_set_ops_byname(mp,
+			RTE_MBUF_DEFAULT_MEMPOOL_OPS, NULL);
+	if (rte_errno != 0) {
+		RTE_LOG(ERR, MBUF, "error setting mempool handler\n");
+		return NULL;
+	}
+	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
#227 lore raw

Re: [PATCH v15 0/3] mempool: add mempool handler feature

In reply to David Hunt

2016-06-19 13:05, David Hunt:

Quoted 3 lines

v15 changes:

* Changed rte_mempool_ops_get() to rte_mempool_get_ops()

I don't find this change in the patch.
But I wonder wether it is really needed.

#228 lore raw

Re: [PATCH v15 0/3] mempool: add mempool handler feature

2016-06-22 09:56, Thomas Monjalon:

Quoted 7 lines

2016-06-19 13:05, David Hunt:

Quoted 3 lines

v15 changes:

* Changed rte_mempool_ops_get() to rte_mempool_get_ops()

I don't find this change in the patch.
But I wonder wether it is really needed.

If we assume that rte_mempool_ops_* are wrappers on top of handlers, rte_mempool_ops_get and rte_mempool_ops_register should be renamed to rte_mempool_get_ops and rte_mempool_register_ops.

author
#229 lore raw

v16 cover mempool: add mempool handler feature

In reply to David Hunt

Here's the latest version of the Mempool Handler patch set. It's re-based on top of the latest head as of 20/6/2016, including Olivier's 35-part patch series on mempool re-org [1]

[1] http://dpdk.org/ml/archives/dev/2016-May/039229.html

v16 changes:

* Changed rte_mempool_ops_get() to rte_mempool_get_ops()
* Changed rte_mempool_ops_register() to rte_mempool_register_ops()
* Applied missing changes that should have been in v15

v15 changes:

* Changed rte_mempool_ops_get() to rte_mempool_get_ops()
* Did some minor tweaks to comments after the previous change of function names from put/get to enqueue/dequeue
* Added missing spinlock_unlock in rte_mempool_ops_register()
* Added check for null in ops_free
* removed un-needed return statement

v14 changes:

* set MEMPOOL_F_RING_CREATED flag after rte_mempool_ring_create() is called.
* Changed name of feature from "external mempool manager" to "mempool handler" and updated comments and release notes accordingly.
* Added a comment for newly added pool_config param in rte_mempool_set_ops_byname.

v13 changes:

* Added in extra opaque data (pool_config) to mempool struct for mempool configuration by the ops functions. For example, this can be used to pass device names or device flags to the underlying alloc function.
* Added mempool_config param to rte_mempool_set_ops_byname()

v12 changes:

* Fixed a comment (function pram h -> ops)
* fixed a typo (callbacki)

v11 changes:

* Fixed comments (added '.' where needed for consistency)
* removed ABI breakage notice for mempool manager in deprecation.rst
* Added description of the external mempool manager functionality to doc/guides/prog_guide/mempool_lib.rst (John Mc reviewed)
* renamed rte_mempool_default.c to rte_mempool_ring.c

v10 changes:

* changed the _put/_get op names to _enqueue/_dequeue to be consistent with the function names
* some rte_errno cleanup
* comment tweaks about when to set pool_data
* removed an un-needed check for ops->alloc == NULL

v9 changes:

* added a check for NULL alloc in rte_mempool_ops_register
* rte_mempool_alloc_t now returns int instead of void*
* fixed some comment typo's
* removed some unneeded typecasts
* changed a return NULL to return -EEXIST in rte_mempool_ops_register
* fixed rte_mempool_version.map file so builds ok as shared libs
* moved flags check from rte_mempool_create_empty to rte_mempool_create

v8 changes:

* merged first three patches in the series into one.
* changed parameters to ops callback to all be rte_mempool pointer rather than than pointer to opaque data or uint64.
* comment fixes.
* fixed parameter to _free function (was inconsistent).
* changed MEMPOOL_F_RING_CREATED to MEMPOOL_F_POOL_CREATED

v7 changes:

* Changed rte_mempool_handler_table to rte_mempool_ops_table
* Changed hander_idx to ops_index in rte_mempool struct
* Reworked comments in rte_mempool.h around ops functions
* Changed rte_mempool_hander.c to rte_mempool_ops.c
* Changed all functions containing _handler_ to _ops_
* Now there is no mention of 'handler' left
* Other small changes out of review of 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 Mempool Handler feature is an extension to the mempool API that allows users to add and use an alternative mempool handler, 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 handler will remain unchanged and will be backward compatible. However, there will be an ABI breakage, as the mempool struct is changing.

There are two aspects to mempool handlers.
1. Adding the code for your new mempool operations (ops). This is achieved by adding a new mempool ops source file into the librte_mempool library, and using the REGISTER_MEMPOOL_OPS macro.
2. Using the new API to call rte_mempool_create_empty and rte_mempool_set_ops_byname to create a new mempool using the name parameter to identify which ops to use.

New API calls added
1. A new rte_mempool_create_empty() function
2. rte_mempool_set_ops_byname() which sets the mempools ops (functions)
3. An rte_mempool_populate_default() and rte_mempool_populate_anon() functions which populates the mempool using the relevant ops

Several mempool handlers may be used in the same application. A new mempool can then be created by using the new rte_mempool_create_empty function, then calling rte_mempool_set_ops_byname to point the mempool to the relevant mempool handler callback (ops) structure.

Legacy applications will continue to use the old rte_mempool_create API call, which uses a ring based mempool handler by default. These applications will need to be modified to use a new mempool handler.

A mempool handler needs to provide the following functions.
1. alloc - allocates the mempool memory, and adds each object onto a ring
2. enqueue - puts an object back into the mempool once an application has
finished with it
3. dequeue - 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 an enqueue/dequeue/get_count is called from the application/PMD, the callback for that mempool is called. These functions are in the fastpath, and any unoptimised ops 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_ops_byname()

int
rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name

    void *pool_config);

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 ops struct is passed by name to rte_mempool_set_ops_byname, which looks through the ops struct array to get the ops_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 ops index.

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

static const struct rte_mempool_ops ops_sp_mc = {
.name = "ring_sp_mc",
.alloc = rte_mempool_common_ring_alloc,
.enqueue = common_ring_sp_enqueue,
.dequeue = common_ring_mc_dequeue,
.get_count = common_ring_get_count,
.free = common_ring_free,
};

And then the following macro will register the ops in the array of ops structures

REGISTER_MEMPOOL_OPS(ops_mp_mc);

For an example of API usage, please see app/test/test_mempool.c, which implements a rudimentary "custom_handler" mempool handler 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 mempool handler operations
mbuf: make default mempool ops configurable at build

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

author
#230 lore raw

PATCH v16 1/3 mempool: support mempool handler operations

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_ops_byname() right after rte_mempool_create_empty() allows the user to change the handler that will be used when populating the mempool.

This patch also adds a set of default ops (function callbacks) based on rte_ring.

---
 app/test/test_mempool_perf.c               |   1 -
 doc/guides/prog_guide/mempool_lib.rst      |  32 +++-
 doc/guides/rel_notes/deprecation.rst       |   9 -
 lib/librte_mempool/Makefile                |   2 +
 lib/librte_mempool/rte_mempool.c           |  67 +++-----
 lib/librte_mempool/rte_mempool.h           | 255 ++++++++++++++++++++++++++---
 lib/librte_mempool/rte_mempool_ops.c       | 151 +++++++++++++++++
 lib/librte_mempool/rte_mempool_ring.c      | 161 ++++++++++++++++++
 lib/librte_mempool/rte_mempool_version.map |  13 +-
 9 files changed, 610 insertions(+), 81 deletions(-)
 create mode 100644 lib/librte_mempool/rte_mempool_ops.c
 create mode 100644 lib/librte_mempool/rte_mempool_ring.c
app/test/test_mempool_perf.c +0 -1
index c5e3576..c5f8455 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;
 				}
doc/guides/prog_guide/mempool_lib.rst +31 -1
index c3afc2e..1943fc4 100644
@@ -34,7 +34,8 @@ Mempool Library
 ===============
 
 A memory pool is an allocator of a fixed-sized object.
-In the DPDK, it is identified by name and uses a ring to store free objects.
+In the DPDK, it is identified by name and uses a mempool handler to store free objects.
+The default mempool handler is ring based.
 It provides some other optional services such as a per-core object cache and
 an alignment helper to ensure that objects are padded to spread them equally on all DRAM or DDR3 channels.
 
@@ -127,6 +128,35 @@ The maximum size of the cache is static and is defined at compilation time (CONF
    A mempool in Memory with its Associated Ring
 
 
+Mempool Handlers
+------------------------
+
+This allows external memory subsystems, such as external hardware memory
+management systems and software based memory allocators, to be used with DPDK.
+
+There are two aspects to a mempool handler.
+
+* Adding the code for your new mempool operations (ops). This is achieved by
+  adding a new mempool ops code, and using the ``REGISTER_MEMPOOL_OPS`` macro.
+
+* Using the new API to call ``rte_mempool_create_empty()`` and
+  ``rte_mempool_set_ops_byname()`` to create a new mempool and specifying which
+  ops to use.
+
+Several different mempool handlers may be used in the same application. A new
+mempool can be created by using the ``rte_mempool_create_empty()`` function,
+then using ``rte_mempool_set_ops_byname()`` to point the mempool to the
+relevant mempool handler callback (ops) structure.
+
+Legacy applications may continue to use the old ``rte_mempool_create()`` API
+call, which uses a ring based mempool handler by default. These applications
+will need to be modified to use a new mempool handler.
+
+For applications that use ``rte_pktmbuf_create()``, there is a config setting
+(``RTE_MBUF_DEFAULT_MEMPOOL_OPS``) that allows the application to make use of
+an alternative mempool handler.
+
+
 Use Cases
 ---------
 
doc/guides/rel_notes/deprecation.rst +0 -9
index f75183f..3cbc19e 100644
@@ -34,15 +34,6 @@ Deprecation Notices
   compact API. The ones that remain are backwards compatible and use the
   per-lcore default cache if available. This change targets release 16.07.
 
-* The rte_mempool struct will be changed in 16.07 to facilitate the new
-  external mempool manager functionality.
-  The ring element will be replaced with a more generic 'pool' opaque pointer
-  to allow new mempool handlers to use their own user-defined mempool
-  layout. Also newly added to rte_mempool is a handler index.
-  The existing API will be backward compatible, but there will be new API
-  functions added to facilitate the creation of mempools using an external
-  handler. The 16.07 release will contain these changes.
-
 * A librte_vhost public structures refactor is planned for DPDK 16.07
   that requires both ABI and API change.
   The proposed refactor would expose DPDK vhost dev to applications as
lib/librte_mempool/Makefile +2 -0
index 43423e0..a4c089e 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_ops.c
+SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) +=  rte_mempool_ring.c
 # install includes
 SYMLINK-$(CONFIG_RTE_LIBRTE_MEMPOOL)-include := rte_mempool.h
 
lib/librte_mempool/rte_mempool.c +24 -43

128 lines not on this page · raw message

lib/librte_mempool/rte_mempool.h +233 -22

311 lines not on this page · raw message

lib/librte_mempool/rte_mempool_ops.c new file +151 -0

151 lines not on this page · raw message

lib/librte_mempool/rte_mempool_ring.c new file +161 -0

161 lines not on this page · raw message

lib/librte_mempool/rte_mempool_version.map +8 -5
index f63461b..a4a6c1f 100644
@@ -20,15 +20,18 @@ DPDK_16.7 {
 	global:
 
 	rte_mempool_check_cookies;
-	rte_mempool_obj_iter;
-	rte_mempool_mem_iter;
 	rte_mempool_create_empty;
+	rte_mempool_free;
+	rte_mempool_mem_iter;
+	rte_mempool_obj_iter;
+	rte_mempool_ops_table;
+	rte_mempool_populate_anon;
+	rte_mempool_populate_default;
 	rte_mempool_populate_phys;
 	rte_mempool_populate_phys_tab;
 	rte_mempool_populate_virt;
-	rte_mempool_populate_default;
-	rte_mempool_populate_anon;
-	rte_mempool_free;
+	rte_mempool_register_ops;
+	rte_mempool_set_ops_byname;
 
 	local: *;
 } DPDK_2.0;
Signature · 1 line
2.5.5
author
#231 lore raw

PATCH v16 2/3 app/test: test mempool handler

In reply to David Hunt

Create a minimal custom mempool handler and check that it passes basic mempool autotests.

---
 app/test/test_mempool.c | 122 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 120 insertions(+), 2 deletions(-)
app/test/test_mempool.c +120 -2
index b586249..31582d8 100644
@@ -83,6 +83,99 @@
 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 through all the element pointers and allocate a chunk of memory, then
+ * insert that memory into the ring.
+ */
+static int
+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 -ENOMEM;
+
+	rte_spinlock_init(&cm->lock);
+	cm->count = 0;
+	cm->size = mp->size;
+	mp->pool_data = cm;
+	return 0;
+}
+
+static void
+custom_mempool_free(struct rte_mempool *mp)
+{
+	rte_free((void *)(mp->pool_data));
+}
+
+static int
+custom_mempool_enqueue(struct rte_mempool *mp, void * const *obj_table,
+		unsigned n)
+{
+	struct custom_mempool *cm = (struct custom_mempool *)(mp->pool_data);
+	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_dequeue(struct rte_mempool *mp, void **obj_table, unsigned n)
+{
+	struct custom_mempool *cm = (struct custom_mempool *)(mp->pool_data);
+	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(const struct rte_mempool *mp)
+{
+	struct custom_mempool *cm = (struct custom_mempool *)(mp->pool_data);
+
+	return cm->count;
+}
+
+static struct rte_mempool_ops mempool_ops_custom = {
+	.name = "custom_handler",
+	.alloc = custom_mempool_alloc,
+	.free = custom_mempool_free,
+	.enqueue = custom_mempool_enqueue,
+	.dequeue = custom_mempool_dequeue,
+	.get_count = custom_mempool_get_count,
+};
+
+MEMPOOL_REGISTER_OPS(mempool_ops_custom);
+
+/*
  * save the object number in the first 4 bytes of object data. All
  * other bytes are set to 0.
  */
@@ -292,12 +385,14 @@ static int test_mempool_single_consumer(void)
  * test function for mempool test based on singple consumer and single producer,
  * can run on one lcore only
  */
-static int test_mempool_launch_single_consumer(__attribute__((unused)) void *arg)
+static int
+test_mempool_launch_single_consumer(__attribute__((unused)) void *arg)
 {
 	return test_mempool_single_consumer();
 }
 
-static void my_mp_init(struct rte_mempool * mp, __attribute__((unused)) void * arg)
+static void
+my_mp_init(struct rte_mempool *mp, __attribute__((unused)) void *arg)
 {
 	printf("mempool name is %s\n", mp->name);
 	/* nothing to be implemented here*/
@@ -477,6 +572,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);
 
@@ -505,6 +601,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_ops_byname(mp_ext, "custom_handler", NULL) < 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");
@@ -545,6 +662,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
author
#232 lore raw

PATCH v16 3/3 mbuf: make default mempool ops configurable at build

In reply to David Hunt

By default, the mempool ops 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_OPS.

---
 config/common_base         |  1 +
 lib/librte_mbuf/rte_mbuf.c | 26 ++++++++++++++++++++++----
 2 files changed, 23 insertions(+), 4 deletions(-)
config/common_base +1 -0
index 11ac81e..5f230db 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_OPS="ring_mp_mc"
 CONFIG_RTE_MBUF_REFCNT_ATOMIC=y
 CONFIG_RTE_PKTMBUF_HEADROOM=128
 
lib/librte_mbuf/rte_mbuf.c +22 -4
index 2ece742..8cf5436 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,27 @@ 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_errno = rte_mempool_set_ops_byname(mp,
+			RTE_MBUF_DEFAULT_MEMPOOL_OPS, NULL);
+	if (rte_errno != 0) {
+		RTE_LOG(ERR, MBUF, "error setting mempool handler\n");
+		return NULL;
+	}
+	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
#233 lore raw

Re: [PATCH v16 0/3] mempool: add mempool handler feature

In reply to David Hunt
Quoted 4 lines

David Hunt (2):
mempool: support mempool handler operations app/test: test mempool handler
mbuf: make default mempool ops configurable at build

Applied, thanks for the nice feature

I'm sorry David, the revision record is v17 ;)

#234 lore raw

Re: [PATCH v16 0/3] mempool: add mempool handler feature

Quoted 8 lines · dev on behalf of Thomas Monjalon
Quoted 4 lines

David Hunt (2):
mempool: support mempool handler operations app/test: test mempool handler
mbuf: make default mempool ops configurable at build

Applied, thanks for the nice feature

I'm sorry David, the revision record is v17 ;)

Quick David, make two more updates to the patch ☺

Thanks David and Great work !!!

Quoted 2 lines
#235 lore raw

Re: [PATCH v16 0/3] mempool: add mempool handler feature

Quoted 7 lines · Keith
Quoted 5 lines · dev on behalf of Thomas Monjalon
Quoted 4 lines

David Hunt (2):
mempool: support mempool handler operations app/test: test mempool handler
mbuf: make default mempool ops configurable at build

Quoted 10 lines
Quoted 3 lines

Applied, thanks for the nice feature

I'm sorry David, the revision record is v17 ;)

Quick David, make two more updates to the patch ☺

Thanks David and Great work !!!

Quoted 2 lines

Hello David,

thanks for the patchset. I am sorry, I didn't have any time for DPDK this week and didn't test it before applying. The current master produces the following error in my regular builds:

INSTALL-LIB librte_eal.a
== Build lib/librte_ring
CC rte_ring.o
AR librte_ring.a
SYMLINK-FILE include/rte_ring.h
INSTALL-LIB librte_ring.a
== Build lib/librte_mempool
CC rte_mempool.o
make[3]: *** No rule to make target rte_mempool_ops.o', needed by librte_mempool.a'. Stop. make[2]: *** [librte_mempool] Error 2
make[1]: *** [lib] Error 2
make: *** [all] Error 2
Build step 'Execute shell' marked build as failure
[WARNINGS] Skipping publisher since build result is FAILURE

I have no idea about the reason at the moment. I'll check it soon.

Regards
Jan

#236 lore raw

Re: [PATCH v16 0/3] mempool: add mempool handler feature

2016-06-24 13:20, Jan Viktorin:

Quoted 4 lines

thanks for the patchset. I am sorry, I didn't have any time for DPDK this week and didn't test it before applying. The current master produces the following error in my regular builds:

Quoted 9 lines

INSTALL-LIB librte_eal.a
== Build lib/librte_ring
CC rte_ring.o
AR librte_ring.a
SYMLINK-FILE include/rte_ring.h
INSTALL-LIB librte_ring.a
== Build lib/librte_mempool
CC rte_mempool.o
make[3]: *** No rule to make target rte_mempool_ops.o', needed by librte_mempool.a'. Stop.

It should be fixed now.

#237 lore raw

Re: [PATCH v16 0/3] mempool: add mempool handler feature

Quoted 15 lines · On Fri, 24 Jun 2016 13:24:56 +0200 Thomas Monjalon <[email protected]>…

2016-06-24 13:20, Jan Viktorin:

Quoted 4 lines

thanks for the patchset. I am sorry, I didn't have any time for DPDK this week and didn't test it before applying. The current master produces the following error in my regular builds:

Quoted 9 lines

INSTALL-LIB librte_eal.a
== Build lib/librte_ring
CC rte_ring.o
AR librte_ring.a
SYMLINK-FILE include/rte_ring.h
INSTALL-LIB librte_ring.a
== Build lib/librte_mempool
CC rte_mempool.o
make[3]: *** No rule to make target rte_mempool_ops.o', needed by librte_mempool.a'. Stop.

Quoted 1 line

It should be fixed now.

OK, confirmed. It seems that I only receive notifications of failures :).

Jan