mempool: reduce wasted space on populate
[dpdk.git] / lib / librte_mempool / rte_mempool_ops.c
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 /* indirect jump table to support external memory pools. */
15 struct rte_mempool_ops_table rte_mempool_ops_table = {
16         .sl =  RTE_SPINLOCK_INITIALIZER,
17         .num_ops = 0
18 };
19
20 /* add a new ops struct in rte_mempool_ops_table, return its index. */
21 int
22 rte_mempool_register_ops(const struct rte_mempool_ops *h)
23 {
24         struct rte_mempool_ops *ops;
25         int16_t ops_index;
26
27         rte_spinlock_lock(&rte_mempool_ops_table.sl);
28
29         if (rte_mempool_ops_table.num_ops >=
30                         RTE_MEMPOOL_MAX_OPS_IDX) {
31                 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
32                 RTE_LOG(ERR, MEMPOOL,
33                         "Maximum number of mempool ops structs exceeded\n");
34                 return -ENOSPC;
35         }
36
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);
40                 RTE_LOG(ERR, MEMPOOL,
41                         "Missing callback while registering mempool ops\n");
42                 return -EINVAL;
43         }
44
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",
48                                 __func__, h->name);
49                 rte_errno = EEXIST;
50                 return -EEXIST;
51         }
52
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;
57         ops->free = h->free;
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;
65
66         rte_spinlock_unlock(&rte_mempool_ops_table.sl);
67
68         return ops_index;
69 }
70
71 /* wrapper to allocate an external mempool's private (pool) data. */
72 int
73 rte_mempool_ops_alloc(struct rte_mempool *mp)
74 {
75         struct rte_mempool_ops *ops;
76
77         ops = rte_mempool_get_ops(mp->ops_index);
78         return ops->alloc(mp);
79 }
80
81 /* wrapper to free an external pool ops. */
82 void
83 rte_mempool_ops_free(struct rte_mempool *mp)
84 {
85         struct rte_mempool_ops *ops;
86
87         ops = rte_mempool_get_ops(mp->ops_index);
88         if (ops->free == NULL)
89                 return;
90         ops->free(mp);
91 }
92
93 /* wrapper to get available objects in an external mempool. */
94 unsigned int
95 rte_mempool_ops_get_count(const struct rte_mempool *mp)
96 {
97         struct rte_mempool_ops *ops;
98
99         ops = rte_mempool_get_ops(mp->ops_index);
100         return ops->get_count(mp);
101 }
102
103 /* wrapper to calculate the memory size required to store given number
104  * of objects
105  */
106 ssize_t
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)
110 {
111         struct rte_mempool_ops *ops;
112
113         ops = rte_mempool_get_ops(mp->ops_index);
114
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);
118
119         return ops->calc_mem_size(mp, obj_num, pg_shift, min_chunk_size, align);
120 }
121
122 /* wrapper to populate memory pool objects using provided memory chunk */
123 int
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,
127                                 void *obj_cb_arg)
128 {
129         struct rte_mempool_ops *ops;
130
131         ops = rte_mempool_get_ops(mp->ops_index);
132
133         if (ops->populate == NULL)
134                 return rte_mempool_op_populate_default(mp, max_objs, vaddr,
135                                                        iova, len, obj_cb,
136                                                        obj_cb_arg);
137
138         return ops->populate(mp, max_objs, vaddr, iova, len, obj_cb,
139                              obj_cb_arg);
140 }
141
142 /* wrapper to get additional mempool info */
143 int
144 rte_mempool_ops_get_info(const struct rte_mempool *mp,
145                          struct rte_mempool_info *info)
146 {
147         struct rte_mempool_ops *ops;
148
149         ops = rte_mempool_get_ops(mp->ops_index);
150
151         RTE_FUNC_PTR_OR_ERR_RET(ops->get_info, -ENOTSUP);
152         return ops->get_info(mp, info);
153 }
154
155
156 /* sets mempool ops previously registered by rte_mempool_register_ops. */
157 int
158 rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name,
159         void *pool_config)
160 {
161         struct rte_mempool_ops *ops = NULL;
162         unsigned i;
163
164         /* too late, the mempool is already populated. */
165         if (mp->flags & MEMPOOL_F_POOL_CREATED)
166                 return -EEXIST;
167
168         for (i = 0; i < rte_mempool_ops_table.num_ops; i++) {
169                 if (!strcmp(name,
170                                 rte_mempool_ops_table.ops[i].name)) {
171                         ops = &rte_mempool_ops_table.ops[i];
172                         break;
173                 }
174         }
175
176         if (ops == NULL)
177                 return -EINVAL;
178
179         mp->ops_index = i;
180         mp->pool_config = pool_config;
181         return 0;
182 }