1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation.
3 * Copyright(c) 2016 6WIND S.A.
9 #include <rte_string_fns.h>
10 #include <rte_mempool.h>
11 #include <rte_errno.h>
14 #include "rte_mempool_trace.h"
16 /* indirect jump table to support external memory pools. */
17 struct rte_mempool_ops_table rte_mempool_ops_table = {
18 .sl = RTE_SPINLOCK_INITIALIZER,
22 /* add a new ops struct in rte_mempool_ops_table, return its index. */
24 rte_mempool_register_ops(const struct rte_mempool_ops *h)
26 struct rte_mempool_ops *ops;
29 rte_spinlock_lock(&rte_mempool_ops_table.sl);
31 if (rte_mempool_ops_table.num_ops >=
32 RTE_MEMPOOL_MAX_OPS_IDX) {
33 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
35 "Maximum number of mempool ops structs exceeded\n");
39 if (h->alloc == NULL || h->enqueue == NULL ||
40 h->dequeue == NULL || h->get_count == NULL) {
41 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
43 "Missing callback while registering mempool ops\n");
47 if (strlen(h->name) >= sizeof(ops->name) - 1) {
48 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
49 RTE_LOG(DEBUG, EAL, "%s(): mempool_ops <%s>: name too long\n",
55 ops_index = rte_mempool_ops_table.num_ops++;
56 ops = &rte_mempool_ops_table.ops[ops_index];
57 strlcpy(ops->name, h->name, sizeof(ops->name));
58 ops->alloc = h->alloc;
60 ops->enqueue = h->enqueue;
61 ops->dequeue = h->dequeue;
62 ops->get_count = h->get_count;
63 ops->calc_mem_size = h->calc_mem_size;
64 ops->populate = h->populate;
65 ops->get_info = h->get_info;
66 ops->dequeue_contig_blocks = h->dequeue_contig_blocks;
68 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
73 /* wrapper to allocate an external mempool's private (pool) data. */
75 rte_mempool_ops_alloc(struct rte_mempool *mp)
77 struct rte_mempool_ops *ops;
79 rte_mempool_trace_ops_alloc(mp);
80 ops = rte_mempool_get_ops(mp->ops_index);
81 return ops->alloc(mp);
84 /* wrapper to free an external pool ops. */
86 rte_mempool_ops_free(struct rte_mempool *mp)
88 struct rte_mempool_ops *ops;
90 rte_mempool_trace_ops_free(mp);
91 ops = rte_mempool_get_ops(mp->ops_index);
92 if (ops->free == NULL)
97 /* wrapper to get available objects in an external mempool. */
99 rte_mempool_ops_get_count(const struct rte_mempool *mp)
101 struct rte_mempool_ops *ops;
103 ops = rte_mempool_get_ops(mp->ops_index);
104 return ops->get_count(mp);
107 /* wrapper to calculate the memory size required to store given number
111 rte_mempool_ops_calc_mem_size(const struct rte_mempool *mp,
112 uint32_t obj_num, uint32_t pg_shift,
113 size_t *min_chunk_size, size_t *align)
115 struct rte_mempool_ops *ops;
117 ops = rte_mempool_get_ops(mp->ops_index);
119 if (ops->calc_mem_size == NULL)
120 return rte_mempool_op_calc_mem_size_default(mp, obj_num,
121 pg_shift, min_chunk_size, align);
123 return ops->calc_mem_size(mp, obj_num, pg_shift, min_chunk_size, align);
126 /* wrapper to populate memory pool objects using provided memory chunk */
128 rte_mempool_ops_populate(struct rte_mempool *mp, unsigned int max_objs,
129 void *vaddr, rte_iova_t iova, size_t len,
130 rte_mempool_populate_obj_cb_t *obj_cb,
133 struct rte_mempool_ops *ops;
135 ops = rte_mempool_get_ops(mp->ops_index);
137 rte_mempool_trace_ops_populate(mp, max_objs, vaddr, iova, len, obj_cb,
139 if (ops->populate == NULL)
140 return rte_mempool_op_populate_default(mp, max_objs, vaddr,
144 return ops->populate(mp, max_objs, vaddr, iova, len, obj_cb,
148 /* wrapper to get additional mempool info */
150 rte_mempool_ops_get_info(const struct rte_mempool *mp,
151 struct rte_mempool_info *info)
153 struct rte_mempool_ops *ops;
155 ops = rte_mempool_get_ops(mp->ops_index);
157 RTE_FUNC_PTR_OR_ERR_RET(ops->get_info, -ENOTSUP);
158 return ops->get_info(mp, info);
162 /* sets mempool ops previously registered by rte_mempool_register_ops. */
164 rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name,
167 struct rte_mempool_ops *ops = NULL;
170 /* too late, the mempool is already populated. */
171 if (mp->flags & MEMPOOL_F_POOL_CREATED)
174 for (i = 0; i < rte_mempool_ops_table.num_ops; i++) {
176 rte_mempool_ops_table.ops[i].name)) {
177 ops = &rte_mempool_ops_table.ops[i];
186 mp->pool_config = pool_config;
187 rte_mempool_trace_set_ops_byname(mp, name, pool_config);