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 /* indirect jump table to support external memory pools. */
15 struct rte_mempool_ops_table rte_mempool_ops_table = {
16 .sl = RTE_SPINLOCK_INITIALIZER,
20 /* add a new ops struct in rte_mempool_ops_table, return its index. */
22 rte_mempool_register_ops(const struct rte_mempool_ops *h)
24 struct rte_mempool_ops *ops;
27 rte_spinlock_lock(&rte_mempool_ops_table.sl);
29 if (rte_mempool_ops_table.num_ops >=
30 RTE_MEMPOOL_MAX_OPS_IDX) {
31 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
33 "Maximum number of mempool ops structs exceeded\n");
37 if (h->alloc == NULL || h->enqueue == NULL ||
38 h->dequeue == NULL || h->get_count == NULL) {
39 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
41 "Missing callback while registering mempool ops\n");
45 if (strlen(h->name) >= sizeof(ops->name) - 1) {
46 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
47 RTE_LOG(DEBUG, EAL, "%s(): mempool_ops <%s>: name too long\n",
53 ops_index = rte_mempool_ops_table.num_ops++;
54 ops = &rte_mempool_ops_table.ops[ops_index];
55 strlcpy(ops->name, h->name, sizeof(ops->name));
56 ops->alloc = h->alloc;
58 ops->enqueue = h->enqueue;
59 ops->dequeue = h->dequeue;
60 ops->get_count = h->get_count;
61 ops->calc_mem_size = h->calc_mem_size;
62 ops->populate = h->populate;
63 ops->get_info = h->get_info;
64 ops->dequeue_contig_blocks = h->dequeue_contig_blocks;
66 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
71 /* wrapper to allocate an external mempool's private (pool) data. */
73 rte_mempool_ops_alloc(struct rte_mempool *mp)
75 struct rte_mempool_ops *ops;
77 ops = rte_mempool_get_ops(mp->ops_index);
78 return ops->alloc(mp);
81 /* wrapper to free an external pool ops. */
83 rte_mempool_ops_free(struct rte_mempool *mp)
85 struct rte_mempool_ops *ops;
87 ops = rte_mempool_get_ops(mp->ops_index);
88 if (ops->free == NULL)
93 /* wrapper to get available objects in an external mempool. */
95 rte_mempool_ops_get_count(const struct rte_mempool *mp)
97 struct rte_mempool_ops *ops;
99 ops = rte_mempool_get_ops(mp->ops_index);
100 return ops->get_count(mp);
103 /* wrapper to calculate the memory size required to store given number
107 rte_mempool_ops_calc_mem_size(const struct rte_mempool *mp,
108 uint32_t obj_num, uint32_t pg_shift,
109 size_t *min_chunk_size, size_t *align)
111 struct rte_mempool_ops *ops;
113 ops = rte_mempool_get_ops(mp->ops_index);
115 if (ops->calc_mem_size == NULL)
116 return rte_mempool_op_calc_mem_size_default(mp, obj_num,
117 pg_shift, min_chunk_size, align);
119 return ops->calc_mem_size(mp, obj_num, pg_shift, min_chunk_size, align);
122 /* wrapper to populate memory pool objects using provided memory chunk */
124 rte_mempool_ops_populate(struct rte_mempool *mp, unsigned int max_objs,
125 void *vaddr, rte_iova_t iova, size_t len,
126 rte_mempool_populate_obj_cb_t *obj_cb,
129 struct rte_mempool_ops *ops;
131 ops = rte_mempool_get_ops(mp->ops_index);
133 if (ops->populate == NULL)
134 return rte_mempool_op_populate_default(mp, max_objs, vaddr,
138 return ops->populate(mp, max_objs, vaddr, iova, len, obj_cb,
142 /* wrapper to get additional mempool info */
144 rte_mempool_ops_get_info(const struct rte_mempool *mp,
145 struct rte_mempool_info *info)
147 struct rte_mempool_ops *ops;
149 ops = rte_mempool_get_ops(mp->ops_index);
151 RTE_FUNC_PTR_OR_ERR_RET(ops->get_info, -ENOTSUP);
152 return ops->get_info(mp, info);
156 /* sets mempool ops previously registered by rte_mempool_register_ops. */
158 rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name,
161 struct rte_mempool_ops *ops = NULL;
164 /* too late, the mempool is already populated. */
165 if (mp->flags & MEMPOOL_F_POOL_CREATED)
168 for (i = 0; i < rte_mempool_ops_table.num_ops; i++) {
170 rte_mempool_ops_table.ops[i].name)) {
171 ops = &rte_mempool_ops_table.ops[i];
180 mp->pool_config = pool_config;