mempool: support handler operations
[dpdk.git] / lib / librte_mempool / rte_mempool.h
index 60339bd..0a1777c 100644 (file)
@@ -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,10 +204,14 @@ struct rte_mempool_memhdr {
  */
 struct rte_mempool {
        char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
-       struct rte_ring *ring;           /**< Ring to store objects. */
-       const struct rte_memzone *mz;    /**< Memzone where pool is allocated */
+       union {
+               void *pool_data;         /**< Ring or pool to store objects. */
+               uint64_t pool_id;        /**< External mempool identifier. */
+       };
+       void *pool_config;               /**< optional args for ops alloc. */
+       const struct rte_memzone *mz;    /**< Memzone where pool is alloc'd. */
        int flags;                       /**< Flags of the mempool. */
-       int socket_id;                   /**< Socket id passed at mempool creation. */
+       int socket_id;                   /**< Socket id passed at create. */
        uint32_t size;                   /**< Max size of the mempool. */
        uint32_t cache_size;             /**< Size of per-lcore local cache. */
        uint32_t cache_flushthresh;
@@ -217,6 +222,14 @@ struct rte_mempool {
        uint32_t trailer_size;           /**< Size of trailer (after elt). */
 
        unsigned private_data_size;      /**< Size of private data. */
+       /**
+        * Index into rte_mempool_ops_table array of mempool ops
+        * structs, which contain callback function 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.
+        */
+       int32_t ops_index;
 
        struct rte_mempool_cache *local_cache; /**< Per-lcore local cache */
 
@@ -235,7 +248,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_RING_CREATED   0x0010 /**< Internal: ring is created */
+#define MEMPOOL_F_POOL_CREATED   0x0010 /**< Internal: pool is created. */
 #define MEMPOOL_F_NO_PHYS_CONTIG 0x0020 /**< Don't need physically contiguous objs. */
 
 /**
@@ -325,6 +338,215 @@ 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_OPS_NAMESIZE 32 /**< Max length of ops struct name. */
+
+/**
+ * Prototype for implementation specific data provisioning function.
+ *
+ * The function should provide the implementation specific memory for
+ * for use by the other mempool ops functions in a given mempool ops struct.
+ * E.g. the default ops provides an instance of the rte_ring for this purpose.
+ * it will most likely point to a different type of data structure, and
+ * will be transparent to the application programmer.
+ * This function should set mp->pool_data.
+ */
+typedef int (*rte_mempool_alloc_t)(struct rte_mempool *mp);
+
+/**
+ * Free the opaque private data pointed to by mp->pool_data pointer.
+ */
+typedef void (*rte_mempool_free_t)(struct rte_mempool *mp);
+
+/**
+ * Enqueue an object into the external pool.
+ */
+typedef int (*rte_mempool_enqueue_t)(struct rte_mempool *mp,
+               void * const *obj_table, unsigned int n);
+
+/**
+ * Dequeue an object from the external pool.
+ */
+typedef int (*rte_mempool_dequeue_t)(struct rte_mempool *mp,
+               void **obj_table, unsigned int n);
+
+/**
+ * Return the number of available objects in the external pool.
+ */
+typedef unsigned (*rte_mempool_get_count)(const struct rte_mempool *mp);
+
+/** Structure defining mempool operations structure */
+struct rte_mempool_ops {
+       char name[RTE_MEMPOOL_OPS_NAMESIZE]; /**< Name of mempool ops struct. */
+       rte_mempool_alloc_t alloc;       /**< Allocate private data. */
+       rte_mempool_free_t free;         /**< Free the external pool. */
+       rte_mempool_enqueue_t enqueue;   /**< Enqueue an object. */
+       rte_mempool_dequeue_t dequeue;   /**< Dequeue an object. */
+       rte_mempool_get_count get_count; /**< Get qty of available objs. */
+} __rte_cache_aligned;
+
+#define RTE_MEMPOOL_MAX_OPS_IDX 16  /**< Max registered ops structs */
+
+/**
+ * Structure storing the table of registered ops structs, each of which contain
+ * the function pointers for the mempool ops functions.
+ * Each process has its own storage for this ops struct array so that
+ * the mempools can be shared across primary and secondary processes.
+ * The indices used to access the array are valid across processes, whereas
+ * any function pointers stored directly in the mempool struct would not be.
+ * This results in us simply having "ops_index" in the mempool struct.
+ */
+struct rte_mempool_ops_table {
+       rte_spinlock_t sl;     /**< Spinlock for add/delete. */
+       uint32_t num_ops;      /**< Number of used ops structs in the table. */
+       /**
+        * Storage for all possible ops structs.
+        */
+       struct rte_mempool_ops ops[RTE_MEMPOOL_MAX_OPS_IDX];
+} __rte_cache_aligned;
+
+/** Array of registered ops structs. */
+extern struct rte_mempool_ops_table rte_mempool_ops_table;
+
+/**
+ * @internal Get the mempool ops struct from its index.
+ *
+ * @param ops_index
+ *   The index of the ops struct in the ops struct table. It must be a valid
+ *   index: (0 <= idx < num_ops).
+ * @return
+ *   The pointer to the ops struct in the table.
+ */
+static inline struct rte_mempool_ops *
+rte_mempool_get_ops(int ops_index)
+{
+       RTE_VERIFY((ops_index >= 0) && (ops_index < RTE_MEMPOOL_MAX_OPS_IDX));
+
+       return &rte_mempool_ops_table.ops[ops_index];
+}
+
+/**
+ * @internal Wrapper for mempool_ops alloc callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @return
+ *   - 0: Success; successfully allocated mempool pool_data.
+ *   - <0: Error; code of alloc function.
+ */
+int
+rte_mempool_ops_alloc(struct rte_mempool *mp);
+
+/**
+ * @internal Wrapper for mempool_ops dequeue 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 dequeue function.
+ */
+static inline int
+rte_mempool_ops_dequeue_bulk(struct rte_mempool *mp,
+               void **obj_table, unsigned n)
+{
+       struct rte_mempool_ops *ops;
+
+       ops = rte_mempool_get_ops(mp->ops_index);
+       return ops->dequeue(mp, obj_table, n);
+}
+
+/**
+ * @internal wrapper for mempool_ops enqueue 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 enqueue function.
+ */
+static inline int
+rte_mempool_ops_enqueue_bulk(struct rte_mempool *mp, void * const *obj_table,
+               unsigned n)
+{
+       struct rte_mempool_ops *ops;
+
+       ops = rte_mempool_get_ops(mp->ops_index);
+       return ops->enqueue(mp, obj_table, n);
+}
+
+/**
+ * @internal wrapper for mempool_ops get_count callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ * @return
+ *   The number of available objects in the external pool.
+ */
+unsigned
+rte_mempool_ops_get_count(const struct rte_mempool *mp);
+
+/**
+ * @internal wrapper for mempool_ops free callback.
+ *
+ * @param mp
+ *   Pointer to the memory pool.
+ */
+void
+rte_mempool_ops_free(struct rte_mempool *mp);
+
+/**
+ * Set the ops 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 ops structure to use for this mempool.
+ * @param pool_config
+ *   Opaque data that can be passed by the application to the ops functions.
+ * @return
+ *   - 0: Success; the mempool is now using the requested ops functions.
+ *   - -EINVAL - Invalid ops struct name provided.
+ *   - -EEXIST - mempool already has an ops struct assigned.
+ */
+int
+rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name,
+               void *pool_config);
+
+/**
+ * Register mempool operations.
+ *
+ * @param ops
+ *   Pointer to an ops structure to register.
+ * @return
+ *   - >=0: Success; return the index of the ops struct in the table.
+ *   - -EINVAL - some missing callbacks while registering ops struct.
+ *   - -ENOSPC - the maximum number of ops structs has been reached.
+ */
+int rte_mempool_register_ops(const struct rte_mempool_ops *ops);
+
+/**
+ * Macro to statically register the ops of a mempool handler.
+ * Note that the rte_mempool_register_ops fails silently here when
+ * more then RTE_MEMPOOL_MAX_OPS_IDX is registered.
+ */
+#define MEMPOOL_REGISTER_OPS(ops)                                      \
+       void mp_hdlr_init_##ops(void);                                  \
+       void __attribute__((constructor, used)) mp_hdlr_init_##ops(void)\
+       {                                                               \
+               rte_mempool_register_ops(&ops);                 \
+       }
+
 /**
  * An object callback function for mempool.
  *
@@ -774,7 +996,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_ops_enqueue_bulk(mp, &cache->objs[cache_size],
                                cache->len - cache_size);
                cache->len = cache_size;
        }
@@ -785,19 +1007,10 @@ 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");
-       }
+       if (rte_mempool_ops_enqueue_bulk(mp, 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);
+       rte_mempool_ops_enqueue_bulk(mp, obj_table, n);
 #endif
 }
 
@@ -945,7 +1158,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_ops_dequeue_bulk(mp,
+                       &cache->objs[cache->len], req);
                if (unlikely(ret < 0)) {
                        /*
                         * In the offchance that we are buffer constrained,
@@ -972,10 +1186,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_ops_dequeue_bulk(mp, obj_table, n);
 
        if (ret < 0)
                __MEMPOOL_STAT_ADD(mp, get_fail, n);