1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
3 */
4
5 #include <string.h>
6 #include <stdio.h>
7
8 #include <rte_common.h>
9 #include <rte_malloc.h>
10 #include <rte_log.h>
11
12 #include "rte_table_array.h"
13
14 #ifdef RTE_TABLE_STATS_COLLECT
15
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
20
21 #else
22
23 #define RTE_TABLE_ARRAY_STATS_PKTS_IN_ADD(table, val)
24 #define RTE_TABLE_ARRAY_STATS_PKTS_LOOKUP_MISS(table, val)
25
26 #endif
27
28 struct rte_table_array {
29 struct rte_table_stats stats;
30
31 /* Input parameters */
32 uint32_t entry_size;
33 uint32_t n_entries;
34 uint32_t offset;
35
36 /* Internal fields */
37 uint32_t entry_pos_mask;
38
39 /* Internal table */
40 uint8_t array[0] __rte_cache_aligned;
41 } __rte_cache_aligned;
42
43 static void *
rte_table_array_create(void * params,int socket_id,uint32_t entry_size)44 rte_table_array_create(void *params, int socket_id, uint32_t entry_size)
45 {
46 struct rte_table_array_params *p = params;
47 struct rte_table_array *t;
48 uint32_t total_cl_size, total_size;
49
50 /* Check input parameters */
51 if ((p == NULL) ||
52 (p->n_entries == 0) ||
53 (!rte_is_power_of_2(p->n_entries)))
54 return NULL;
55
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);
63 if (t == NULL) {
64 RTE_LOG(ERR, TABLE,
65 "%s: Cannot allocate %u bytes for array table\n",
66 __func__, total_size);
67 return NULL;
68 }
69
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;
75
76 return t;
77 }
78
79 static int
rte_table_array_free(void * table)80 rte_table_array_free(void *table)
81 {
82 struct rte_table_array *t = table;
83
84 /* Check input parameters */
85 if (t == NULL) {
86 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
87 return -EINVAL;
88 }
89
90 /* Free previously allocated resources */
91 rte_free(t);
92
93 return 0;
94 }
95
96 static int
rte_table_array_entry_add(void * table,void * key,void * entry,int * key_found,void ** entry_ptr)97 rte_table_array_entry_add(
98 void *table,
99 void *key,
100 void *entry,
101 int *key_found,
102 void **entry_ptr)
103 {
104 struct rte_table_array *t = table;
105 struct rte_table_array_key *k = key;
106 uint8_t *table_entry;
107
108 /* Check input parameters */
109 if (table == NULL) {
110 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
111 return -EINVAL;
112 }
113 if (key == NULL) {
114 RTE_LOG(ERR, TABLE, "%s: key parameter is NULL\n", __func__);
115 return -EINVAL;
116 }
117 if (entry == NULL) {
118 RTE_LOG(ERR, TABLE, "%s: entry parameter is NULL\n", __func__);
119 return -EINVAL;
120 }
121 if (key_found == NULL) {
122 RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n",
123 __func__);
124 return -EINVAL;
125 }
126 if (entry_ptr == NULL) {
127 RTE_LOG(ERR, TABLE, "%s: entry_ptr parameter is NULL\n",
128 __func__);
129 return -EINVAL;
130 }
131
132 table_entry = &t->array[k->pos * t->entry_size];
133 memcpy(table_entry, entry, t->entry_size);
134 *key_found = 1;
135 *entry_ptr = (void *) table_entry;
136
137 return 0;
138 }
139
140 static int
rte_table_array_lookup(void * table,struct rte_mbuf ** pkts,uint64_t pkts_mask,uint64_t * lookup_hit_mask,void ** entries)141 rte_table_array_lookup(
142 void *table,
143 struct rte_mbuf **pkts,
144 uint64_t pkts_mask,
145 uint64_t *lookup_hit_mask,
146 void **entries)
147 {
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;
152
153 if ((pkts_mask & (pkts_mask + 1)) == 0) {
154 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
155 uint32_t i;
156
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;
161
162 entries[i] = (void *) &t->array[entry_pos *
163 t->entry_size];
164 }
165 } else {
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;
172
173 entries[pkt_index] = (void *) &t->array[entry_pos *
174 t->entry_size];
175 pkts_mask &= ~pkt_mask;
176 }
177 }
178
179 return 0;
180 }
181
182 static int
rte_table_array_stats_read(void * table,struct rte_table_stats * stats,int clear)183 rte_table_array_stats_read(void *table, struct rte_table_stats *stats, int clear)
184 {
185 struct rte_table_array *array = table;
186
187 if (stats != NULL)
188 memcpy(stats, &array->stats, sizeof(array->stats));
189
190 if (clear)
191 memset(&array->stats, 0, sizeof(array->stats));
192
193 return 0;
194 }
195
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,
200 .f_delete = NULL,
201 .f_add_bulk = NULL,
202 .f_delete_bulk = NULL,
203 .f_lookup = rte_table_array_lookup,
204 .f_stats = rte_table_array_stats_read,
205 };
206