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