Imported Upstream version 16.07-rc1
[deb_dpdk.git] / lib / librte_mempool / rte_mempool_ops.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 Intel Corporation. All rights reserved.
5  *   Copyright(c) 2016 6WIND S.A.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include <stdio.h>
36 #include <string.h>
37
38 #include <rte_mempool.h>
39 #include <rte_errno.h>
40
41 /* indirect jump table to support external memory pools. */
42 struct rte_mempool_ops_table rte_mempool_ops_table = {
43         .sl =  RTE_SPINLOCK_INITIALIZER,
44         .num_ops = 0
45 };
46
47 /* add a new ops struct in rte_mempool_ops_table, return its index. */
48 int
49 rte_mempool_register_ops(const struct rte_mempool_ops *h)
50 {
51         struct rte_mempool_ops *ops;
52         int16_t ops_index;
53
54         rte_spinlock_lock(&rte_mempool_ops_table.sl);
55
56         if (rte_mempool_ops_table.num_ops >=
57                         RTE_MEMPOOL_MAX_OPS_IDX) {
58                 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
59                 RTE_LOG(ERR, MEMPOOL,
60                         "Maximum number of mempool ops structs exceeded\n");
61                 return -ENOSPC;
62         }
63
64         if (h->alloc == NULL || h->enqueue == NULL ||
65                         h->dequeue == NULL || h->get_count == NULL) {
66                 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
67                 RTE_LOG(ERR, MEMPOOL,
68                         "Missing callback while registering mempool ops\n");
69                 return -EINVAL;
70         }
71
72         if (strlen(h->name) >= sizeof(ops->name) - 1) {
73                 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
74                 RTE_LOG(DEBUG, EAL, "%s(): mempool_ops <%s>: name too long\n",
75                                 __func__, h->name);
76                 rte_errno = EEXIST;
77                 return -EEXIST;
78         }
79
80         ops_index = rte_mempool_ops_table.num_ops++;
81         ops = &rte_mempool_ops_table.ops[ops_index];
82         snprintf(ops->name, sizeof(ops->name), "%s", h->name);
83         ops->alloc = h->alloc;
84         ops->enqueue = h->enqueue;
85         ops->dequeue = h->dequeue;
86         ops->get_count = h->get_count;
87
88         rte_spinlock_unlock(&rte_mempool_ops_table.sl);
89
90         return ops_index;
91 }
92
93 /* wrapper to allocate an external mempool's private (pool) data. */
94 int
95 rte_mempool_ops_alloc(struct rte_mempool *mp)
96 {
97         struct rte_mempool_ops *ops;
98
99         ops = rte_mempool_get_ops(mp->ops_index);
100         return ops->alloc(mp);
101 }
102
103 /* wrapper to free an external pool ops. */
104 void
105 rte_mempool_ops_free(struct rte_mempool *mp)
106 {
107         struct rte_mempool_ops *ops;
108
109         ops = rte_mempool_get_ops(mp->ops_index);
110         if (ops->free == NULL)
111                 return;
112         ops->free(mp);
113 }
114
115 /* wrapper to get available objects in an external mempool. */
116 unsigned int
117 rte_mempool_ops_get_count(const struct rte_mempool *mp)
118 {
119         struct rte_mempool_ops *ops;
120
121         ops = rte_mempool_get_ops(mp->ops_index);
122         return ops->get_count(mp);
123 }
124
125 /* sets mempool ops previously registered by rte_mempool_register_ops. */
126 int
127 rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name,
128         void *pool_config)
129 {
130         struct rte_mempool_ops *ops = NULL;
131         unsigned i;
132
133         /* too late, the mempool is already populated. */
134         if (mp->flags & MEMPOOL_F_POOL_CREATED)
135                 return -EEXIST;
136
137         for (i = 0; i < rte_mempool_ops_table.num_ops; i++) {
138                 if (!strcmp(name,
139                                 rte_mempool_ops_table.ops[i].name)) {
140                         ops = &rte_mempool_ops_table.ops[i];
141                         break;
142                 }
143         }
144
145         if (ops == NULL)
146                 return -EINVAL;
147
148         mp->ops_index = i;
149         mp->pool_config = pool_config;
150         return 0;
151 }