4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
37 #include <rte_common.h>
39 #include <rte_memory.h>
40 #include <rte_malloc.h>
43 #include "rte_table_array.h"
45 struct rte_table_array {
46 /* Input parameters */
52 uint32_t entry_pos_mask;
55 uint8_t array[0] __rte_cache_aligned;
56 } __rte_cache_aligned;
59 rte_table_array_create(void *params, int socket_id, uint32_t entry_size)
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;
66 /* Check input parameters */
68 (p->n_entries == 0) ||
69 (!rte_is_power_of_2(p->n_entries)) ||
70 ((p->offset & 0x3) != 0)) {
74 /* Memory allocation */
75 total_cl_size = (sizeof(struct rte_table_array) +
76 RTE_CACHE_LINE_SIZE) / RTE_CACHE_LINE_SIZE;
77 total_cl_size += (p->n_entries * entry_size +
78 RTE_CACHE_LINE_SIZE) / RTE_CACHE_LINE_SIZE;
79 total_size = total_cl_size * RTE_CACHE_LINE_SIZE;
80 t = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE, socket_id);
83 "%s: Cannot allocate %u bytes for array table\n",
84 __func__, total_size);
88 /* Memory initialization */
89 t->entry_size = entry_size;
90 t->n_entries = p->n_entries;
91 t->offset = p->offset;
92 t->entry_pos_mask = t->n_entries - 1;
98 rte_table_array_free(void *table)
100 struct rte_table_array *t = (struct rte_table_array *) table;
102 /* Check input parameters */
104 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
108 /* Free previously allocated resources */
115 rte_table_array_entry_add(
122 struct rte_table_array *t = (struct rte_table_array *) table;
123 struct rte_table_array_key *k = (struct rte_table_array_key *) key;
124 uint8_t *table_entry;
126 /* Check input parameters */
128 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
132 RTE_LOG(ERR, TABLE, "%s: key parameter is NULL\n", __func__);
136 RTE_LOG(ERR, TABLE, "%s: entry parameter is NULL\n", __func__);
139 if (key_found == NULL) {
140 RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n",
144 if (entry_ptr == NULL) {
145 RTE_LOG(ERR, TABLE, "%s: entry_ptr parameter is NULL\n",
150 table_entry = &t->array[k->pos * t->entry_size];
151 memcpy(table_entry, entry, t->entry_size);
153 *entry_ptr = (void *) table_entry;
159 rte_table_array_lookup(
161 struct rte_mbuf **pkts,
163 uint64_t *lookup_hit_mask,
166 struct rte_table_array *t = (struct rte_table_array *) table;
168 *lookup_hit_mask = pkts_mask;
170 if ((pkts_mask & (pkts_mask + 1)) == 0) {
171 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
174 for (i = 0; i < n_pkts; i++) {
175 struct rte_mbuf *pkt = pkts[i];
176 uint32_t entry_pos = RTE_MBUF_METADATA_UINT32(pkt,
177 t->offset) & t->entry_pos_mask;
179 entries[i] = (void *) &t->array[entry_pos *
183 for ( ; pkts_mask; ) {
184 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
185 uint64_t pkt_mask = 1LLU << pkt_index;
186 struct rte_mbuf *pkt = pkts[pkt_index];
187 uint32_t entry_pos = RTE_MBUF_METADATA_UINT32(pkt,
188 t->offset) & t->entry_pos_mask;
190 entries[pkt_index] = (void *) &t->array[entry_pos *
192 pkts_mask &= ~pkt_mask;
199 struct rte_table_ops rte_table_array_ops = {
200 .f_create = rte_table_array_create,
201 .f_free = rte_table_array_free,
202 .f_add = rte_table_array_entry_add,
204 .f_lookup = rte_table_array_lookup,