1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2016 Intel Corporation. 3 * Copyright(c) 2016 6WIND S.A. 4 */ 5 6 #include <stdio.h> 7 #include <string.h> 8 9 #include <rte_string_fns.h> 10 #include <rte_mempool.h> 11 #include <rte_errno.h> 12 #include <rte_dev.h> 13 14 #include "rte_mempool_trace.h" 15 16 /* indirect jump table to support external memory pools. */ 17 struct rte_mempool_ops_table rte_mempool_ops_table = { 18 .sl = RTE_SPINLOCK_INITIALIZER, 19 .num_ops = 0 20 }; 21 22 /* add a new ops struct in rte_mempool_ops_table, return its index. */ 23 int 24 rte_mempool_register_ops(const struct rte_mempool_ops *h) 25 { 26 struct rte_mempool_ops *ops; 27 int16_t ops_index; 28 29 rte_spinlock_lock(&rte_mempool_ops_table.sl); 30 31 if (rte_mempool_ops_table.num_ops >= 32 RTE_MEMPOOL_MAX_OPS_IDX) { 33 rte_spinlock_unlock(&rte_mempool_ops_table.sl); 34 RTE_LOG(ERR, MEMPOOL, 35 "Maximum number of mempool ops structs exceeded\n"); 36 return -ENOSPC; 37 } 38 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); 42 RTE_LOG(ERR, MEMPOOL, 43 "Missing callback while registering mempool ops\n"); 44 return -EINVAL; 45 } 46 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", 50 __func__, h->name); 51 rte_errno = EEXIST; 52 return -EEXIST; 53 } 54 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; 59 ops->free = h->free; 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; 67 68 rte_spinlock_unlock(&rte_mempool_ops_table.sl); 69 70 return ops_index; 71 } 72 73 /* wrapper to allocate an external mempool's private (pool) data. */ 74 int 75 rte_mempool_ops_alloc(struct rte_mempool *mp) 76 { 77 struct rte_mempool_ops *ops; 78 79 rte_mempool_trace_ops_alloc(mp); 80 ops = rte_mempool_get_ops(mp->ops_index); 81 return ops->alloc(mp); 82 } 83 84 /* wrapper to free an external pool ops. */ 85 void 86 rte_mempool_ops_free(struct rte_mempool *mp) 87 { 88 struct rte_mempool_ops *ops; 89 90 rte_mempool_trace_ops_free(mp); 91 ops = rte_mempool_get_ops(mp->ops_index); 92 if (ops->free == NULL) 93 return; 94 ops->free(mp); 95 } 96 97 /* wrapper to get available objects in an external mempool. */ 98 unsigned int 99 rte_mempool_ops_get_count(const struct rte_mempool *mp) 100 { 101 struct rte_mempool_ops *ops; 102 103 ops = rte_mempool_get_ops(mp->ops_index); 104 return ops->get_count(mp); 105 } 106 107 /* wrapper to calculate the memory size required to store given number 108 * of objects 109 */ 110 ssize_t 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) 114 { 115 struct rte_mempool_ops *ops; 116 117 ops = rte_mempool_get_ops(mp->ops_index); 118 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); 122 123 return ops->calc_mem_size(mp, obj_num, pg_shift, min_chunk_size, align); 124 } 125 126 /* wrapper to populate memory pool objects using provided memory chunk */ 127 int 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, 131 void *obj_cb_arg) 132 { 133 struct rte_mempool_ops *ops; 134 135 ops = rte_mempool_get_ops(mp->ops_index); 136 137 rte_mempool_trace_ops_populate(mp, max_objs, vaddr, iova, len, obj_cb, 138 obj_cb_arg); 139 if (ops->populate == NULL) 140 return rte_mempool_op_populate_default(mp, max_objs, vaddr, 141 iova, len, obj_cb, 142 obj_cb_arg); 143 144 return ops->populate(mp, max_objs, vaddr, iova, len, obj_cb, 145 obj_cb_arg); 146 } 147 148 /* wrapper to get additional mempool info */ 149 int 150 rte_mempool_ops_get_info(const struct rte_mempool *mp, 151 struct rte_mempool_info *info) 152 { 153 struct rte_mempool_ops *ops; 154 155 ops = rte_mempool_get_ops(mp->ops_index); 156 157 RTE_FUNC_PTR_OR_ERR_RET(ops->get_info, -ENOTSUP); 158 return ops->get_info(mp, info); 159 } 160 161 162 /* sets mempool ops previously registered by rte_mempool_register_ops. */ 163 int 164 rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name, 165 void *pool_config) 166 { 167 struct rte_mempool_ops *ops = NULL; 168 unsigned i; 169 170 /* too late, the mempool is already populated. */ 171 if (mp->flags & RTE_MEMPOOL_F_POOL_CREATED) 172 return -EEXIST; 173 174 for (i = 0; i < rte_mempool_ops_table.num_ops; i++) { 175 if (!strcmp(name, 176 rte_mempool_ops_table.ops[i].name)) { 177 ops = &rte_mempool_ops_table.ops[i]; 178 break; 179 } 180 } 181 182 if (ops == NULL) 183 return -EINVAL; 184 185 mp->ops_index = i; 186 mp->pool_config = pool_config; 187 rte_mempool_trace_set_ops_byname(mp, name, pool_config); 188 return 0; 189 } 190