mempool: implement abstract mempool info API
[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_mempool.h>
10 #include <rte_errno.h>
11 #include <rte_dev.h>
12
13 /* indirect jump table to support external memory pools. */
14 struct rte_mempool_ops_table rte_mempool_ops_table = {
15         .sl =  RTE_SPINLOCK_INITIALIZER,
16         .num_ops = 0
17 };
18
19 /* add a new ops struct in rte_mempool_ops_table, return its index. */
20 int
21 rte_mempool_register_ops(const struct rte_mempool_ops *h)
22 {
23         struct rte_mempool_ops *ops;
24         int16_t ops_index;
25
26         rte_spinlock_lock(&rte_mempool_ops_table.sl);
27
28         if (rte_mempool_ops_table.num_ops >=
29                         RTE_MEMPOOL_MAX_OPS_IDX) {
30                 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
31                 RTE_LOG(ERR, MEMPOOL,
32                         "Maximum number of mempool ops structs exceeded\n");
33                 return -ENOSPC;
34         }
35
36         if (h->alloc == NULL || h->enqueue == NULL ||
37                         h->dequeue == NULL || h->get_count == NULL) {
38                 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
39                 RTE_LOG(ERR, MEMPOOL,
40                         "Missing callback while registering mempool ops\n");
41                 return -EINVAL;
42         }
43
44         if (strlen(h->name) >= sizeof(ops->name) - 1) {
45                 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
46                 RTE_LOG(DEBUG, EAL, "%s(): mempool_ops <%s>: name too long\n",
47                                 __func__, h->name);
48                 rte_errno = EEXIST;
49                 return -EEXIST;
50         }
51
52         ops_index = rte_mempool_ops_table.num_ops++;
53         ops = &rte_mempool_ops_table.ops[ops_index];
54         snprintf(ops->name, sizeof(ops->name), "%s", h->name);
55         ops->alloc = h->alloc;
56         ops->free = h->free;
57         ops->enqueue = h->enqueue;
58         ops->dequeue = h->dequeue;
59         ops->get_count = h->get_count;
60         ops->calc_mem_size = h->calc_mem_size;
61         ops->populate = h->populate;
62         ops->get_info = h->get_info;
63
64         rte_spinlock_unlock(&rte_mempool_ops_table.sl);
65
66         return ops_index;
67 }
68
69 /* wrapper to allocate an external mempool's private (pool) data. */
70 int
71 rte_mempool_ops_alloc(struct rte_mempool *mp)
72 {
73         struct rte_mempool_ops *ops;
74
75         ops = rte_mempool_get_ops(mp->ops_index);
76         return ops->alloc(mp);
77 }
78
79 /* wrapper to free an external pool ops. */
80 void
81 rte_mempool_ops_free(struct rte_mempool *mp)
82 {
83         struct rte_mempool_ops *ops;
84
85         ops = rte_mempool_get_ops(mp->ops_index);
86         if (ops->free == NULL)
87                 return;
88         ops->free(mp);
89 }
90
91 /* wrapper to get available objects in an external mempool. */
92 unsigned int
93 rte_mempool_ops_get_count(const struct rte_mempool *mp)
94 {
95         struct rte_mempool_ops *ops;
96
97         ops = rte_mempool_get_ops(mp->ops_index);
98         return ops->get_count(mp);
99 }
100
101 /* wrapper to notify new memory area to external mempool */
102 ssize_t
103 rte_mempool_ops_calc_mem_size(const struct rte_mempool *mp,
104                                 uint32_t obj_num, uint32_t pg_shift,
105                                 size_t *min_chunk_size, size_t *align)
106 {
107         struct rte_mempool_ops *ops;
108
109         ops = rte_mempool_get_ops(mp->ops_index);
110
111         if (ops->calc_mem_size == NULL)
112                 return rte_mempool_op_calc_mem_size_default(mp, obj_num,
113                                 pg_shift, min_chunk_size, align);
114
115         return ops->calc_mem_size(mp, obj_num, pg_shift, min_chunk_size, align);
116 }
117
118 /* wrapper to populate memory pool objects using provided memory chunk */
119 int
120 rte_mempool_ops_populate(struct rte_mempool *mp, unsigned int max_objs,
121                                 void *vaddr, rte_iova_t iova, size_t len,
122                                 rte_mempool_populate_obj_cb_t *obj_cb,
123                                 void *obj_cb_arg)
124 {
125         struct rte_mempool_ops *ops;
126
127         ops = rte_mempool_get_ops(mp->ops_index);
128
129         if (ops->populate == NULL)
130                 return rte_mempool_op_populate_default(mp, max_objs, vaddr,
131                                                        iova, len, obj_cb,
132                                                        obj_cb_arg);
133
134         return ops->populate(mp, max_objs, vaddr, iova, len, obj_cb,
135                              obj_cb_arg);
136 }
137
138 /* wrapper to get additional mempool info */
139 int
140 rte_mempool_ops_get_info(const struct rte_mempool *mp,
141                          struct rte_mempool_info *info)
142 {
143         struct rte_mempool_ops *ops;
144
145         ops = rte_mempool_get_ops(mp->ops_index);
146
147         RTE_FUNC_PTR_OR_ERR_RET(ops->get_info, -ENOTSUP);
148         return ops->get_info(mp, info);
149 }
150
151
152 /* sets mempool ops previously registered by rte_mempool_register_ops. */
153 int
154 rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name,
155         void *pool_config)
156 {
157         struct rte_mempool_ops *ops = NULL;
158         unsigned i;
159
160         /* too late, the mempool is already populated. */
161         if (mp->flags & MEMPOOL_F_POOL_CREATED)
162                 return -EEXIST;
163
164         for (i = 0; i < rte_mempool_ops_table.num_ops; i++) {
165                 if (!strcmp(name,
166                                 rte_mempool_ops_table.ops[i].name)) {
167                         ops = &rte_mempool_ops_table.ops[i];
168                         break;
169                 }
170         }
171
172         if (ops == NULL)
173                 return -EINVAL;
174
175         mp->ops_index = i;
176         mp->pool_config = pool_config;
177         return 0;
178 }