1
2
3
4
5#include <string.h>
6#include <stdio.h>
7
8#include <rte_common.h>
9#include <rte_mbuf.h>
10#include <rte_memory.h>
11#include <rte_malloc.h>
12#include <rte_log.h>
13
14#include "rte_table_array.h"
15
16#ifdef RTE_TABLE_STATS_COLLECT
17
18#define RTE_TABLE_ARRAY_STATS_PKTS_IN_ADD(table, val) \
19 table->stats.n_pkts_in += val
20#define RTE_TABLE_ARRAY_STATS_PKTS_LOOKUP_MISS(table, val) \
21 table->stats.n_pkts_lookup_miss += val
22
23#else
24
25#define RTE_TABLE_ARRAY_STATS_PKTS_IN_ADD(table, val)
26#define RTE_TABLE_ARRAY_STATS_PKTS_LOOKUP_MISS(table, val)
27
28#endif
29
30struct rte_table_array {
31 struct rte_table_stats stats;
32
33
34 uint32_t entry_size;
35 uint32_t n_entries;
36 uint32_t offset;
37
38
39 uint32_t entry_pos_mask;
40
41
42 uint8_t array[0] __rte_cache_aligned;
43} __rte_cache_aligned;
44
45static void *
46rte_table_array_create(void *params, int socket_id, uint32_t entry_size)
47{
48 struct rte_table_array_params *p = params;
49 struct rte_table_array *t;
50 uint32_t total_cl_size, total_size;
51
52
53 if ((p == NULL) ||
54 (p->n_entries == 0) ||
55 (!rte_is_power_of_2(p->n_entries)))
56 return NULL;
57
58
59 total_cl_size = (sizeof(struct rte_table_array) +
60 RTE_CACHE_LINE_SIZE) / RTE_CACHE_LINE_SIZE;
61 total_cl_size += (p->n_entries * entry_size +
62 RTE_CACHE_LINE_SIZE) / RTE_CACHE_LINE_SIZE;
63 total_size = total_cl_size * RTE_CACHE_LINE_SIZE;
64 t = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE, socket_id);
65 if (t == NULL) {
66 RTE_LOG(ERR, TABLE,
67 "%s: Cannot allocate %u bytes for array table\n",
68 __func__, total_size);
69 return NULL;
70 }
71
72
73 t->entry_size = entry_size;
74 t->n_entries = p->n_entries;
75 t->offset = p->offset;
76 t->entry_pos_mask = t->n_entries - 1;
77
78 return t;
79}
80
81static int
82rte_table_array_free(void *table)
83{
84 struct rte_table_array *t = table;
85
86
87 if (t == NULL) {
88 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
89 return -EINVAL;
90 }
91
92
93 rte_free(t);
94
95 return 0;
96}
97
98static int
99rte_table_array_entry_add(
100 void *table,
101 void *key,
102 void *entry,
103 int *key_found,
104 void **entry_ptr)
105{
106 struct rte_table_array *t = table;
107 struct rte_table_array_key *k = key;
108 uint8_t *table_entry;
109
110
111 if (table == NULL) {
112 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
113 return -EINVAL;
114 }
115 if (key == NULL) {
116 RTE_LOG(ERR, TABLE, "%s: key parameter is NULL\n", __func__);
117 return -EINVAL;
118 }
119 if (entry == NULL) {
120 RTE_LOG(ERR, TABLE, "%s: entry parameter is NULL\n", __func__);
121 return -EINVAL;
122 }
123 if (key_found == NULL) {
124 RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n",
125 __func__);
126 return -EINVAL;
127 }
128 if (entry_ptr == NULL) {
129 RTE_LOG(ERR, TABLE, "%s: entry_ptr parameter is NULL\n",
130 __func__);
131 return -EINVAL;
132 }
133
134 table_entry = &t->array[k->pos * t->entry_size];
135 memcpy(table_entry, entry, t->entry_size);
136 *key_found = 1;
137 *entry_ptr = (void *) table_entry;
138
139 return 0;
140}
141
142static int
143rte_table_array_lookup(
144 void *table,
145 struct rte_mbuf **pkts,
146 uint64_t pkts_mask,
147 uint64_t *lookup_hit_mask,
148 void **entries)
149{
150 struct rte_table_array *t = (struct rte_table_array *) table;
151 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
152 RTE_TABLE_ARRAY_STATS_PKTS_IN_ADD(t, n_pkts_in);
153 *lookup_hit_mask = pkts_mask;
154
155 if ((pkts_mask & (pkts_mask + 1)) == 0) {
156 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
157 uint32_t i;
158
159 for (i = 0; i < n_pkts; i++) {
160 struct rte_mbuf *pkt = pkts[i];
161 uint32_t entry_pos = RTE_MBUF_METADATA_UINT32(pkt,
162 t->offset) & t->entry_pos_mask;
163
164 entries[i] = (void *) &t->array[entry_pos *
165 t->entry_size];
166 }
167 } else {
168 for ( ; pkts_mask; ) {
169 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
170 uint64_t pkt_mask = 1LLU << pkt_index;
171 struct rte_mbuf *pkt = pkts[pkt_index];
172 uint32_t entry_pos = RTE_MBUF_METADATA_UINT32(pkt,
173 t->offset) & t->entry_pos_mask;
174
175 entries[pkt_index] = (void *) &t->array[entry_pos *
176 t->entry_size];
177 pkts_mask &= ~pkt_mask;
178 }
179 }
180
181 return 0;
182}
183
184static int
185rte_table_array_stats_read(void *table, struct rte_table_stats *stats, int clear)
186{
187 struct rte_table_array *array = table;
188
189 if (stats != NULL)
190 memcpy(stats, &array->stats, sizeof(array->stats));
191
192 if (clear)
193 memset(&array->stats, 0, sizeof(array->stats));
194
195 return 0;
196}
197
198struct 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_add_bulk = NULL,
204 .f_delete_bulk = NULL,
205 .f_lookup = rte_table_array_lookup,
206 .f_stats = rte_table_array_stats_read,
207};
208