1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 #include <rte_common.h>
9 #include <rte_malloc.h>
12 #include "rte_table_array.h"
14 #ifdef RTE_TABLE_STATS_COLLECT
16 #define RTE_TABLE_ARRAY_STATS_PKTS_IN_ADD(table, val) \
17 table->stats.n_pkts_in += val
18 #define RTE_TABLE_ARRAY_STATS_PKTS_LOOKUP_MISS(table, val) \
19 table->stats.n_pkts_lookup_miss += val
23 #define RTE_TABLE_ARRAY_STATS_PKTS_IN_ADD(table, val)
24 #define RTE_TABLE_ARRAY_STATS_PKTS_LOOKUP_MISS(table, val)
28 struct rte_table_array {
29 struct rte_table_stats stats;
31 /* Input parameters */
37 uint32_t entry_pos_mask;
40 uint8_t array[0] __rte_cache_aligned;
41 } __rte_cache_aligned;
44 rte_table_array_create(void *params, int socket_id, uint32_t entry_size)
46 struct rte_table_array_params *p = params;
47 struct rte_table_array *t;
48 uint32_t total_cl_size, total_size;
50 /* Check input parameters */
52 (p->n_entries == 0) ||
53 (!rte_is_power_of_2(p->n_entries)))
56 /* Memory allocation */
57 total_cl_size = (sizeof(struct rte_table_array) +
58 RTE_CACHE_LINE_SIZE) / RTE_CACHE_LINE_SIZE;
59 total_cl_size += (p->n_entries * entry_size +
60 RTE_CACHE_LINE_SIZE) / RTE_CACHE_LINE_SIZE;
61 total_size = total_cl_size * RTE_CACHE_LINE_SIZE;
62 t = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE, socket_id);
65 "%s: Cannot allocate %u bytes for array table\n",
66 __func__, total_size);
70 /* Memory initialization */
71 t->entry_size = entry_size;
72 t->n_entries = p->n_entries;
73 t->offset = p->offset;
74 t->entry_pos_mask = t->n_entries - 1;
80 rte_table_array_free(void *table)
82 struct rte_table_array *t = table;
84 /* Check input parameters */
86 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
90 /* Free previously allocated resources */
97 rte_table_array_entry_add(
104 struct rte_table_array *t = table;
105 struct rte_table_array_key *k = key;
106 uint8_t *table_entry;
108 /* Check input parameters */
110 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
114 RTE_LOG(ERR, TABLE, "%s: key parameter is NULL\n", __func__);
118 RTE_LOG(ERR, TABLE, "%s: entry parameter is NULL\n", __func__);
121 if (key_found == NULL) {
122 RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n",
126 if (entry_ptr == NULL) {
127 RTE_LOG(ERR, TABLE, "%s: entry_ptr parameter is NULL\n",
132 table_entry = &t->array[k->pos * t->entry_size];
133 memcpy(table_entry, entry, t->entry_size);
135 *entry_ptr = (void *) table_entry;
141 rte_table_array_lookup(
143 struct rte_mbuf **pkts,
145 uint64_t *lookup_hit_mask,
148 struct rte_table_array *t = (struct rte_table_array *) table;
149 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
150 RTE_TABLE_ARRAY_STATS_PKTS_IN_ADD(t, n_pkts_in);
151 *lookup_hit_mask = pkts_mask;
153 if ((pkts_mask & (pkts_mask + 1)) == 0) {
154 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
157 for (i = 0; i < n_pkts; i++) {
158 struct rte_mbuf *pkt = pkts[i];
159 uint32_t entry_pos = RTE_MBUF_METADATA_UINT32(pkt,
160 t->offset) & t->entry_pos_mask;
162 entries[i] = (void *) &t->array[entry_pos *
166 for ( ; pkts_mask; ) {
167 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
168 uint64_t pkt_mask = 1LLU << pkt_index;
169 struct rte_mbuf *pkt = pkts[pkt_index];
170 uint32_t entry_pos = RTE_MBUF_METADATA_UINT32(pkt,
171 t->offset) & t->entry_pos_mask;
173 entries[pkt_index] = (void *) &t->array[entry_pos *
175 pkts_mask &= ~pkt_mask;
183 rte_table_array_stats_read(void *table, struct rte_table_stats *stats, int clear)
185 struct rte_table_array *array = table;
188 memcpy(stats, &array->stats, sizeof(array->stats));
191 memset(&array->stats, 0, sizeof(array->stats));
196 struct rte_table_ops rte_table_array_ops = {
197 .f_create = rte_table_array_create,
198 .f_free = rte_table_array_free,
199 .f_add = rte_table_array_entry_add,
202 .f_delete_bulk = NULL,
203 .f_lookup = rte_table_array_lookup,
204 .f_stats = rte_table_array_stats_read,