add prefix to cache line macros
[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_malloc.h>
40 #include <rte_log.h>
41
42 #include "rte_table_array.h"
43
44 struct rte_table_array {
45         /* Input parameters */
46         uint32_t entry_size;
47         uint32_t n_entries;
48         uint32_t offset;
49
50         /* Internal fields */
51         uint32_t entry_pos_mask;
52
53         /* Internal table */
54         uint8_t array[0] __rte_cache_aligned;
55 } __rte_cache_aligned;
56
57 static void *
58 rte_table_array_create(void *params, int socket_id, uint32_t entry_size)
59 {
60         struct rte_table_array_params *p =
61                 (struct rte_table_array_params *) params;
62         struct rte_table_array *t;
63         uint32_t total_cl_size, total_size;
64
65         /* Check input parameters */
66         if ((p == NULL) ||
67             (p->n_entries == 0) ||
68                 (!rte_is_power_of_2(p->n_entries)) ||
69                 ((p->offset & 0x3) != 0)) {
70                 return NULL;
71         }
72
73         /* Memory allocation */
74         total_cl_size = (sizeof(struct rte_table_array) +
75                         RTE_CACHE_LINE_SIZE) / RTE_CACHE_LINE_SIZE;
76         total_cl_size += (p->n_entries * entry_size +
77                         RTE_CACHE_LINE_SIZE) / RTE_CACHE_LINE_SIZE;
78         total_size = total_cl_size * RTE_CACHE_LINE_SIZE;
79         t = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE, socket_id);
80         if (t == NULL) {
81                 RTE_LOG(ERR, TABLE,
82                         "%s: Cannot allocate %u bytes for array table\n",
83                         __func__, total_size);
84                 return NULL;
85         }
86
87         /* Memory initialization */
88         t->entry_size = entry_size;
89         t->n_entries = p->n_entries;
90         t->offset = p->offset;
91         t->entry_pos_mask = t->n_entries - 1;
92
93         return t;
94 }
95
96 static int
97 rte_table_array_free(void *table)
98 {
99         struct rte_table_array *t = (struct rte_table_array *) table;
100
101         /* Check input parameters */
102         if (t == NULL) {
103                 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
104                 return -EINVAL;
105         }
106
107         /* Free previously allocated resources */
108         rte_free(t);
109
110         return 0;
111 }
112
113 static int
114 rte_table_array_entry_add(
115         void *table,
116         void *key,
117         void *entry,
118         int *key_found,
119         void **entry_ptr)
120 {
121         struct rte_table_array *t = (struct rte_table_array *) table;
122         struct rte_table_array_key *k = (struct rte_table_array_key *) key;
123         uint8_t *table_entry;
124
125         /* Check input parameters */
126         if (table == NULL) {
127                 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
128                 return -EINVAL;
129         }
130         if (key == NULL) {
131                 RTE_LOG(ERR, TABLE, "%s: key parameter is NULL\n", __func__);
132                 return -EINVAL;
133         }
134         if (entry == NULL) {
135                 RTE_LOG(ERR, TABLE, "%s: entry parameter is NULL\n", __func__);
136                 return -EINVAL;
137         }
138         if (key_found == NULL) {
139                 RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n",
140                         __func__);
141                 return -EINVAL;
142         }
143         if (entry_ptr == NULL) {
144                 RTE_LOG(ERR, TABLE, "%s: entry_ptr parameter is NULL\n",
145                         __func__);
146                 return -EINVAL;
147         }
148
149         table_entry = &t->array[k->pos * t->entry_size];
150         memcpy(table_entry, entry, t->entry_size);
151         *key_found = 1;
152         *entry_ptr = (void *) table_entry;
153
154         return 0;
155 }
156
157 static int
158 rte_table_array_lookup(
159         void *table,
160         struct rte_mbuf **pkts,
161         uint64_t pkts_mask,
162         uint64_t *lookup_hit_mask,
163         void **entries)
164 {
165         struct rte_table_array *t = (struct rte_table_array *) table;
166
167         if ((pkts_mask & (pkts_mask + 1)) == 0) {
168                 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
169                 uint32_t i;
170
171                 for (i = 0; i < n_pkts; i++) {
172                         struct rte_mbuf *pkt = pkts[i];
173                         uint32_t entry_pos = RTE_MBUF_METADATA_UINT32(pkt,
174                                 t->offset) & t->entry_pos_mask;
175
176                         entries[i] = (void *) &t->array[entry_pos *
177                                 t->entry_size];
178                 }
179         } else {
180                 for ( ; pkts_mask; ) {
181                         uint32_t pkt_index = __builtin_ctzll(pkts_mask);
182                         uint64_t pkt_mask = 1LLU << pkt_index;
183                         struct rte_mbuf *pkt = pkts[pkt_index];
184                         uint32_t entry_pos = RTE_MBUF_METADATA_UINT32(pkt,
185                                 t->offset) & t->entry_pos_mask;
186
187                         entries[pkt_index] = (void *) &t->array[entry_pos *
188                                 t->entry_size];
189                         pkts_mask &= ~pkt_mask;
190                 }
191         }
192
193         *lookup_hit_mask = pkts_mask;
194
195         return 0;
196 }
197
198 struct rte_table_ops rte_table_array_ops = {
199         .f_create = rte_table_array_create,
200         .f_free = rte_table_array_free,
201         .f_add = rte_table_array_entry_add,
202         .f_delete = NULL,
203         .f_lookup = rte_table_array_lookup,
204 };