cf7be88ae5a61623c04a02983e2e4d0040cacccd
[deb_dpdk.git] / lib / librte_table / rte_table_array.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <string.h>
35 #include <stdio.h>
36
37 #include <rte_common.h>
38 #include <rte_mbuf.h>
39 #include <rte_memory.h>
40 #include <rte_malloc.h>
41 #include <rte_log.h>
42
43 #include "rte_table_array.h"
44
45 #ifdef RTE_TABLE_STATS_COLLECT
46
47 #define RTE_TABLE_ARRAY_STATS_PKTS_IN_ADD(table, val) \
48         table->stats.n_pkts_in += val
49 #define RTE_TABLE_ARRAY_STATS_PKTS_LOOKUP_MISS(table, val) \
50         table->stats.n_pkts_lookup_miss += val
51
52 #else
53
54 #define RTE_TABLE_ARRAY_STATS_PKTS_IN_ADD(table, val)
55 #define RTE_TABLE_ARRAY_STATS_PKTS_LOOKUP_MISS(table, val)
56
57 #endif
58
59 struct rte_table_array {
60         struct rte_table_stats stats;
61
62         /* Input parameters */
63         uint32_t entry_size;
64         uint32_t n_entries;
65         uint32_t offset;
66
67         /* Internal fields */
68         uint32_t entry_pos_mask;
69
70         /* Internal table */
71         uint8_t array[0] __rte_cache_aligned;
72 } __rte_cache_aligned;
73
74 static void *
75 rte_table_array_create(void *params, int socket_id, uint32_t entry_size)
76 {
77         struct rte_table_array_params *p = params;
78         struct rte_table_array *t;
79         uint32_t total_cl_size, total_size;
80
81         /* Check input parameters */
82         if ((p == NULL) ||
83             (p->n_entries == 0) ||
84                 (!rte_is_power_of_2(p->n_entries)))
85                 return NULL;
86
87         /* Memory allocation */
88         total_cl_size = (sizeof(struct rte_table_array) +
89                         RTE_CACHE_LINE_SIZE) / RTE_CACHE_LINE_SIZE;
90         total_cl_size += (p->n_entries * entry_size +
91                         RTE_CACHE_LINE_SIZE) / RTE_CACHE_LINE_SIZE;
92         total_size = total_cl_size * RTE_CACHE_LINE_SIZE;
93         t = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE, socket_id);
94         if (t == NULL) {
95                 RTE_LOG(ERR, TABLE,
96                         "%s: Cannot allocate %u bytes for array table\n",
97                         __func__, total_size);
98                 return NULL;
99         }
100
101         /* Memory initialization */
102         t->entry_size = entry_size;
103         t->n_entries = p->n_entries;
104         t->offset = p->offset;
105         t->entry_pos_mask = t->n_entries - 1;
106
107         return t;
108 }
109
110 static int
111 rte_table_array_free(void *table)
112 {
113         struct rte_table_array *t = table;
114
115         /* Check input parameters */
116         if (t == NULL) {
117                 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
118                 return -EINVAL;
119         }
120
121         /* Free previously allocated resources */
122         rte_free(t);
123
124         return 0;
125 }
126
127 static int
128 rte_table_array_entry_add(
129         void *table,
130         void *key,
131         void *entry,
132         int *key_found,
133         void **entry_ptr)
134 {
135         struct rte_table_array *t = table;
136         struct rte_table_array_key *k = key;
137         uint8_t *table_entry;
138
139         /* Check input parameters */
140         if (table == NULL) {
141                 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
142                 return -EINVAL;
143         }
144         if (key == NULL) {
145                 RTE_LOG(ERR, TABLE, "%s: key parameter is NULL\n", __func__);
146                 return -EINVAL;
147         }
148         if (entry == NULL) {
149                 RTE_LOG(ERR, TABLE, "%s: entry parameter is NULL\n", __func__);
150                 return -EINVAL;
151         }
152         if (key_found == NULL) {
153                 RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n",
154                         __func__);
155                 return -EINVAL;
156         }
157         if (entry_ptr == NULL) {
158                 RTE_LOG(ERR, TABLE, "%s: entry_ptr parameter is NULL\n",
159                         __func__);
160                 return -EINVAL;
161         }
162
163         table_entry = &t->array[k->pos * t->entry_size];
164         memcpy(table_entry, entry, t->entry_size);
165         *key_found = 1;
166         *entry_ptr = (void *) table_entry;
167
168         return 0;
169 }
170
171 static int
172 rte_table_array_lookup(
173         void *table,
174         struct rte_mbuf **pkts,
175         uint64_t pkts_mask,
176         uint64_t *lookup_hit_mask,
177         void **entries)
178 {
179         struct rte_table_array *t = (struct rte_table_array *) table;
180         __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
181         RTE_TABLE_ARRAY_STATS_PKTS_IN_ADD(t, n_pkts_in);
182         *lookup_hit_mask = pkts_mask;
183
184         if ((pkts_mask & (pkts_mask + 1)) == 0) {
185                 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
186                 uint32_t i;
187
188                 for (i = 0; i < n_pkts; i++) {
189                         struct rte_mbuf *pkt = pkts[i];
190                         uint32_t entry_pos = RTE_MBUF_METADATA_UINT32(pkt,
191                                 t->offset) & t->entry_pos_mask;
192
193                         entries[i] = (void *) &t->array[entry_pos *
194                                 t->entry_size];
195                 }
196         } else {
197                 for ( ; pkts_mask; ) {
198                         uint32_t pkt_index = __builtin_ctzll(pkts_mask);
199                         uint64_t pkt_mask = 1LLU << pkt_index;
200                         struct rte_mbuf *pkt = pkts[pkt_index];
201                         uint32_t entry_pos = RTE_MBUF_METADATA_UINT32(pkt,
202                                 t->offset) & t->entry_pos_mask;
203
204                         entries[pkt_index] = (void *) &t->array[entry_pos *
205                                 t->entry_size];
206                         pkts_mask &= ~pkt_mask;
207                 }
208         }
209
210         return 0;
211 }
212
213 static int
214 rte_table_array_stats_read(void *table, struct rte_table_stats *stats, int clear)
215 {
216         struct rte_table_array *array = table;
217
218         if (stats != NULL)
219                 memcpy(stats, &array->stats, sizeof(array->stats));
220
221         if (clear)
222                 memset(&array->stats, 0, sizeof(array->stats));
223
224         return 0;
225 }
226
227 struct rte_table_ops rte_table_array_ops = {
228         .f_create = rte_table_array_create,
229         .f_free = rte_table_array_free,
230         .f_add = rte_table_array_entry_add,
231         .f_delete = NULL,
232         .f_add_bulk = NULL,
233         .f_delete_bulk = NULL,
234         .f_lookup = rte_table_array_lookup,
235         .f_stats = rte_table_array_stats_read,
236 };