Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2017 Intel Corporation
3 : : */
4 : :
5 : : #include <stdalign.h>
6 : : #include <stdio.h>
7 : : #include <string.h>
8 : :
9 : : #include <eal_export.h>
10 : : #include <rte_common.h>
11 : : #include <rte_malloc.h>
12 : : #include <rte_log.h>
13 : :
14 : : #include "rte_table_hash.h"
15 : :
16 : : #include "table_log.h"
17 : :
18 : : #define KEYS_PER_BUCKET 4
19 : :
20 : : struct bucket {
21 : : union {
22 : : uintptr_t next;
23 : : uint64_t lru_list;
24 : : };
25 : : uint16_t sig[KEYS_PER_BUCKET];
26 : : uint32_t key_pos[KEYS_PER_BUCKET];
27 : : };
28 : :
29 : : #define BUCKET_NEXT(bucket) \
30 : : ((void *) ((bucket)->next & (~1LU)))
31 : :
32 : : #define BUCKET_NEXT_VALID(bucket) \
33 : : ((bucket)->next & 1LU)
34 : :
35 : : #define BUCKET_NEXT_SET(bucket, bucket_next) \
36 : : do \
37 : : (bucket)->next = (((uintptr_t) ((void *) (bucket_next))) | 1LU);\
38 : : while (0)
39 : :
40 : : #define BUCKET_NEXT_SET_NULL(bucket) \
41 : : do \
42 : : (bucket)->next = 0; \
43 : : while (0)
44 : :
45 : : #define BUCKET_NEXT_COPY(bucket, bucket2) \
46 : : do \
47 : : (bucket)->next = (bucket2)->next; \
48 : : while (0)
49 : :
50 : : #ifdef RTE_TABLE_STATS_COLLECT
51 : :
52 : : #define RTE_TABLE_HASH_EXT_STATS_PKTS_IN_ADD(table, val) \
53 : : table->stats.n_pkts_in += val
54 : : #define RTE_TABLE_HASH_EXT_STATS_PKTS_LOOKUP_MISS(table, val) \
55 : : table->stats.n_pkts_lookup_miss += val
56 : :
57 : : #else
58 : :
59 : : #define RTE_TABLE_HASH_EXT_STATS_PKTS_IN_ADD(table, val)
60 : : #define RTE_TABLE_HASH_EXT_STATS_PKTS_LOOKUP_MISS(table, val)
61 : :
62 : : #endif
63 : :
64 : : struct grinder {
65 : : struct bucket *bkt;
66 : : uint64_t sig;
67 : : uint64_t match;
68 : : uint32_t key_index;
69 : : };
70 : :
71 : : struct rte_table_hash {
72 : : struct rte_table_stats stats;
73 : :
74 : : /* Input parameters */
75 : : uint32_t key_size;
76 : : uint32_t entry_size;
77 : : uint32_t n_keys;
78 : : uint32_t n_buckets;
79 : : uint32_t n_buckets_ext;
80 : : rte_table_hash_op_hash f_hash;
81 : : uint64_t seed;
82 : : uint32_t key_offset;
83 : :
84 : : /* Internal */
85 : : uint64_t bucket_mask;
86 : : uint32_t key_size_shl;
87 : : uint32_t data_size_shl;
88 : : uint32_t key_stack_tos;
89 : : uint32_t bkt_ext_stack_tos;
90 : :
91 : : /* Grinder */
92 : : struct grinder grinders[RTE_PORT_IN_BURST_SIZE_MAX];
93 : :
94 : : /* Tables */
95 : : uint64_t *key_mask;
96 : : struct bucket *buckets;
97 : : struct bucket *buckets_ext;
98 : : uint8_t *key_mem;
99 : : uint8_t *data_mem;
100 : : uint32_t *key_stack;
101 : : uint32_t *bkt_ext_stack;
102 : :
103 : : /* Table memory */
104 : : alignas(RTE_CACHE_LINE_SIZE) uint8_t memory[];
105 : : };
106 : :
107 : : static int
108 : : keycmp(void *a, void *b, void *b_mask, uint32_t n_bytes)
109 : : {
110 : : uint64_t *a64 = a, *b64 = b, *b_mask64 = b_mask;
111 : : uint32_t i;
112 : :
113 [ # # # # : 0 : for (i = 0; i < n_bytes / sizeof(uint64_t); i++)
# # # # #
# # # # #
# # # # #
# # # ]
114 [ # # # # : 0 : if (a64[i] != (b64[i] & b_mask64[i]))
# # # # #
# # # # #
# # # # #
# # # ]
115 : : return 1;
116 : :
117 : : return 0;
118 : : }
119 : :
120 : : static void
121 : : keycpy(void *dst, void *src, void *src_mask, uint32_t n_bytes)
122 : : {
123 : : uint64_t *dst64 = dst, *src64 = src, *src_mask64 = src_mask;
124 : : uint32_t i;
125 : :
126 [ # # # # ]: 0 : for (i = 0; i < n_bytes / sizeof(uint64_t); i++)
127 : 0 : dst64[i] = src64[i] & src_mask64[i];
128 : : }
129 : :
130 : : static int
131 : 0 : check_params_create(struct rte_table_hash_params *params)
132 : : {
133 : : /* name */
134 [ # # ]: 0 : if (params->name == NULL) {
135 : 0 : TABLE_LOG(ERR, "%s: name invalid value", __func__);
136 : 0 : return -EINVAL;
137 : : }
138 : :
139 : : /* key_size */
140 [ # # ]: 0 : if ((params->key_size < sizeof(uint64_t)) ||
141 : : (!rte_is_power_of_2(params->key_size))) {
142 : 0 : TABLE_LOG(ERR, "%s: key_size invalid value", __func__);
143 : 0 : return -EINVAL;
144 : : }
145 : :
146 : : /* n_keys */
147 [ # # ]: 0 : if (params->n_keys == 0) {
148 : 0 : TABLE_LOG(ERR, "%s: n_keys invalid value", __func__);
149 : 0 : return -EINVAL;
150 : : }
151 : :
152 : : /* n_buckets */
153 [ # # ]: 0 : if ((params->n_buckets == 0) ||
154 : : (!rte_is_power_of_2(params->n_buckets))) {
155 : 0 : TABLE_LOG(ERR, "%s: n_buckets invalid value", __func__);
156 : 0 : return -EINVAL;
157 : : }
158 : :
159 : : /* f_hash */
160 [ # # ]: 0 : if (params->f_hash == NULL) {
161 : 0 : TABLE_LOG(ERR, "%s: f_hash invalid value", __func__);
162 : 0 : return -EINVAL;
163 : : }
164 : :
165 : : return 0;
166 : : }
167 : :
168 : : static void *
169 : 0 : rte_table_hash_ext_create(void *params, int socket_id, uint32_t entry_size)
170 : : {
171 : : struct rte_table_hash_params *p = params;
172 : : struct rte_table_hash *t;
173 : : uint64_t table_meta_sz, key_mask_sz, bucket_sz, bucket_ext_sz, key_sz;
174 : : uint64_t key_stack_sz, bkt_ext_stack_sz, data_sz, total_size;
175 : : uint64_t key_mask_offset, bucket_offset, bucket_ext_offset, key_offset;
176 : : uint64_t key_stack_offset, bkt_ext_stack_offset, data_offset;
177 : : uint32_t n_buckets_ext, i;
178 : :
179 : : /* Check input parameters */
180 [ # # ]: 0 : if ((check_params_create(p) != 0) ||
181 : : (!rte_is_power_of_2(entry_size)) ||
182 : : ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
183 : : (sizeof(struct bucket) != (RTE_CACHE_LINE_SIZE / 2)))
184 : : return NULL;
185 : :
186 : : /*
187 : : * Table dimensioning
188 : : *
189 : : * Objective: Pick the number of bucket extensions (n_buckets_ext) so that
190 : : * it is guaranteed that n_keys keys can be stored in the table at any time.
191 : : *
192 : : * The worst case scenario takes place when all the n_keys keys fall into
193 : : * the same bucket. Actually, due to the KEYS_PER_BUCKET scheme, the worst
194 : : * case takes place when (n_keys - KEYS_PER_BUCKET + 1) keys fall into the
195 : : * same bucket, while the remaining (KEYS_PER_BUCKET - 1) keys each fall
196 : : * into a different bucket. This case defeats the purpose of the hash table.
197 : : * It indicates unsuitable f_hash or n_keys to n_buckets ratio.
198 : : *
199 : : * n_buckets_ext = n_keys / KEYS_PER_BUCKET + KEYS_PER_BUCKET - 1
200 : : */
201 : 0 : n_buckets_ext = p->n_keys / KEYS_PER_BUCKET + KEYS_PER_BUCKET - 1;
202 : :
203 : : /* Memory allocation */
204 : : table_meta_sz = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_table_hash));
205 : 0 : key_mask_sz = RTE_CACHE_LINE_ROUNDUP(p->key_size);
206 : 0 : bucket_sz = RTE_CACHE_LINE_ROUNDUP(p->n_buckets * sizeof(struct bucket));
207 : 0 : bucket_ext_sz =
208 : 0 : RTE_CACHE_LINE_ROUNDUP(n_buckets_ext * sizeof(struct bucket));
209 : 0 : key_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * p->key_size);
210 : 0 : key_stack_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * sizeof(uint32_t));
211 : 0 : bkt_ext_stack_sz =
212 : 0 : RTE_CACHE_LINE_ROUNDUP(n_buckets_ext * sizeof(uint32_t));
213 : 0 : data_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * entry_size);
214 : 0 : total_size = table_meta_sz + key_mask_sz + bucket_sz + bucket_ext_sz +
215 : 0 : key_sz + key_stack_sz + bkt_ext_stack_sz + data_sz;
216 : :
217 : : if (total_size > SIZE_MAX) {
218 : : TABLE_LOG(ERR, "%s: Cannot allocate %" PRIu64 " bytes"
219 : : " for hash table %s",
220 : : __func__, total_size, p->name);
221 : : return NULL;
222 : : }
223 : :
224 : 0 : t = rte_zmalloc_socket(p->name,
225 : : (size_t)total_size,
226 : : RTE_CACHE_LINE_SIZE,
227 : : socket_id);
228 [ # # ]: 0 : if (t == NULL) {
229 : 0 : TABLE_LOG(ERR, "%s: Cannot allocate %" PRIu64 " bytes"
230 : : " for hash table %s",
231 : : __func__, total_size, p->name);
232 : 0 : return NULL;
233 : : }
234 : 0 : TABLE_LOG(INFO, "%s (%u-byte key): Hash table %s memory "
235 : : "footprint is %" PRIu64 " bytes",
236 : : __func__, p->key_size, p->name, total_size);
237 : :
238 : : /* Memory initialization */
239 : 0 : t->key_size = p->key_size;
240 : 0 : t->entry_size = entry_size;
241 : 0 : t->n_keys = p->n_keys;
242 : 0 : t->n_buckets = p->n_buckets;
243 : 0 : t->n_buckets_ext = n_buckets_ext;
244 : 0 : t->f_hash = p->f_hash;
245 : 0 : t->seed = p->seed;
246 : 0 : t->key_offset = p->key_offset;
247 : :
248 : : /* Internal */
249 [ # # ]: 0 : t->bucket_mask = t->n_buckets - 1;
250 : 0 : t->key_size_shl = rte_ctz32(p->key_size);
251 : 0 : t->data_size_shl = rte_ctz32(entry_size);
252 : :
253 : : /* Tables */
254 : : key_mask_offset = 0;
255 : : bucket_offset = key_mask_offset + key_mask_sz;
256 : 0 : bucket_ext_offset = bucket_offset + bucket_sz;
257 : 0 : key_offset = bucket_ext_offset + bucket_ext_sz;
258 : 0 : key_stack_offset = key_offset + key_sz;
259 : 0 : bkt_ext_stack_offset = key_stack_offset + key_stack_sz;
260 : 0 : data_offset = bkt_ext_stack_offset + bkt_ext_stack_sz;
261 : :
262 : 0 : t->key_mask = (uint64_t *) &t->memory[key_mask_offset];
263 : 0 : t->buckets = (struct bucket *) &t->memory[bucket_offset];
264 : 0 : t->buckets_ext = (struct bucket *) &t->memory[bucket_ext_offset];
265 : 0 : t->key_mem = &t->memory[key_offset];
266 : 0 : t->key_stack = (uint32_t *) &t->memory[key_stack_offset];
267 : 0 : t->bkt_ext_stack = (uint32_t *) &t->memory[bkt_ext_stack_offset];
268 : 0 : t->data_mem = &t->memory[data_offset];
269 : :
270 : : /* Key mask */
271 [ # # ]: 0 : if (p->key_mask == NULL)
272 : 0 : memset(t->key_mask, 0xFF, p->key_size);
273 : : else
274 : 0 : memcpy(t->key_mask, p->key_mask, p->key_size);
275 : :
276 : : /* Key stack */
277 [ # # ]: 0 : for (i = 0; i < t->n_keys; i++)
278 : 0 : t->key_stack[i] = t->n_keys - 1 - i;
279 : 0 : t->key_stack_tos = t->n_keys;
280 : :
281 : : /* Bucket ext stack */
282 [ # # ]: 0 : for (i = 0; i < t->n_buckets_ext; i++)
283 : 0 : t->bkt_ext_stack[i] = t->n_buckets_ext - 1 - i;
284 : 0 : t->bkt_ext_stack_tos = t->n_buckets_ext;
285 : :
286 : 0 : return t;
287 : : }
288 : :
289 : : static int
290 : 0 : rte_table_hash_ext_free(void *table)
291 : : {
292 : : struct rte_table_hash *t = table;
293 : :
294 : : /* Check input parameters */
295 [ # # ]: 0 : if (t == NULL)
296 : : return -EINVAL;
297 : :
298 : 0 : rte_free(t);
299 : 0 : return 0;
300 : : }
301 : :
302 : : static int
303 : 0 : rte_table_hash_ext_entry_add(void *table, void *key, void *entry,
304 : : int *key_found, void **entry_ptr)
305 : : {
306 : : struct rte_table_hash *t = table;
307 : : struct bucket *bkt0, *bkt, *bkt_prev;
308 : : uint64_t sig;
309 : : uint32_t bkt_index, i;
310 : :
311 : 0 : sig = t->f_hash(key, t->key_mask, t->key_size, t->seed);
312 : 0 : bkt_index = sig & t->bucket_mask;
313 : 0 : bkt0 = &t->buckets[bkt_index];
314 : 0 : sig = (sig >> 16) | 1LLU;
315 : :
316 : : /* Key is present in the bucket */
317 [ # # ]: 0 : for (bkt = bkt0; bkt != NULL; bkt = BUCKET_NEXT(bkt))
318 [ # # ]: 0 : for (i = 0; i < KEYS_PER_BUCKET; i++) {
319 : 0 : uint64_t bkt_sig = (uint64_t) bkt->sig[i];
320 : 0 : uint32_t bkt_key_index = bkt->key_pos[i];
321 : : uint8_t *bkt_key =
322 : 0 : &t->key_mem[bkt_key_index << t->key_size_shl];
323 : :
324 [ # # # # ]: 0 : if ((sig == bkt_sig) && (keycmp(bkt_key, key, t->key_mask,
325 : : t->key_size) == 0)) {
326 : 0 : uint8_t *data = &t->data_mem[bkt_key_index <<
327 : 0 : t->data_size_shl];
328 : :
329 : 0 : memcpy(data, entry, t->entry_size);
330 : 0 : *key_found = 1;
331 : 0 : *entry_ptr = (void *) data;
332 : 0 : return 0;
333 : : }
334 : : }
335 : :
336 : : /* Key is not present in the bucket */
337 [ # # ]: 0 : for (bkt_prev = NULL, bkt = bkt0; bkt != NULL; bkt_prev = bkt,
338 : 0 : bkt = BUCKET_NEXT(bkt))
339 [ # # ]: 0 : for (i = 0; i < KEYS_PER_BUCKET; i++) {
340 : 0 : uint64_t bkt_sig = (uint64_t) bkt->sig[i];
341 : :
342 [ # # ]: 0 : if (bkt_sig == 0) {
343 : : uint32_t bkt_key_index;
344 : : uint8_t *bkt_key, *data;
345 : :
346 : : /* Allocate new key */
347 [ # # ]: 0 : if (t->key_stack_tos == 0) /* No free keys */
348 : : return -ENOSPC;
349 : :
350 : 0 : bkt_key_index = t->key_stack[
351 : 0 : --t->key_stack_tos];
352 : :
353 : : /* Install new key */
354 : 0 : bkt_key = &t->key_mem[bkt_key_index <<
355 : 0 : t->key_size_shl];
356 : 0 : data = &t->data_mem[bkt_key_index <<
357 : 0 : t->data_size_shl];
358 : :
359 : 0 : bkt->sig[i] = (uint16_t) sig;
360 : 0 : bkt->key_pos[i] = bkt_key_index;
361 : 0 : keycpy(bkt_key, key, t->key_mask, t->key_size);
362 : 0 : memcpy(data, entry, t->entry_size);
363 : :
364 : 0 : *key_found = 0;
365 : 0 : *entry_ptr = (void *) data;
366 : 0 : return 0;
367 : : }
368 : : }
369 : :
370 : : /* Bucket full: extend bucket */
371 [ # # # # ]: 0 : if ((t->bkt_ext_stack_tos > 0) && (t->key_stack_tos > 0)) {
372 : : uint32_t bkt_key_index;
373 : : uint8_t *bkt_key, *data;
374 : :
375 : : /* Allocate new bucket ext */
376 : 0 : bkt_index = t->bkt_ext_stack[--t->bkt_ext_stack_tos];
377 : 0 : bkt = &t->buckets_ext[bkt_index];
378 : :
379 : : /* Chain the new bucket ext */
380 : 0 : BUCKET_NEXT_SET(bkt_prev, bkt);
381 : 0 : BUCKET_NEXT_SET_NULL(bkt);
382 : :
383 : : /* Allocate new key */
384 : 0 : bkt_key_index = t->key_stack[--t->key_stack_tos];
385 : 0 : bkt_key = &t->key_mem[bkt_key_index << t->key_size_shl];
386 : :
387 : 0 : data = &t->data_mem[bkt_key_index << t->data_size_shl];
388 : :
389 : : /* Install new key into bucket */
390 : 0 : bkt->sig[0] = (uint16_t) sig;
391 : 0 : bkt->key_pos[0] = bkt_key_index;
392 : 0 : keycpy(bkt_key, key, t->key_mask, t->key_size);
393 : 0 : memcpy(data, entry, t->entry_size);
394 : :
395 : 0 : *key_found = 0;
396 : 0 : *entry_ptr = (void *) data;
397 : 0 : return 0;
398 : : }
399 : :
400 : : return -ENOSPC;
401 : : }
402 : :
403 : : static int
404 : 0 : rte_table_hash_ext_entry_delete(void *table, void *key, int *key_found,
405 : : void *entry)
406 : : {
407 : : struct rte_table_hash *t = table;
408 : : struct bucket *bkt0, *bkt, *bkt_prev;
409 : : uint64_t sig;
410 : : uint32_t bkt_index, i;
411 : :
412 : 0 : sig = t->f_hash(key, t->key_mask, t->key_size, t->seed);
413 : 0 : bkt_index = sig & t->bucket_mask;
414 : 0 : bkt0 = &t->buckets[bkt_index];
415 : 0 : sig = (sig >> 16) | 1LLU;
416 : :
417 : : /* Key is present in the bucket */
418 [ # # ]: 0 : for (bkt_prev = NULL, bkt = bkt0; bkt != NULL; bkt_prev = bkt,
419 : 0 : bkt = BUCKET_NEXT(bkt))
420 [ # # ]: 0 : for (i = 0; i < KEYS_PER_BUCKET; i++) {
421 : 0 : uint64_t bkt_sig = (uint64_t) bkt->sig[i];
422 : 0 : uint32_t bkt_key_index = bkt->key_pos[i];
423 : 0 : uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
424 : 0 : t->key_size_shl];
425 : :
426 [ # # # # ]: 0 : if ((sig == bkt_sig) && (keycmp(bkt_key, key, t->key_mask,
427 : : t->key_size) == 0)) {
428 : 0 : uint8_t *data = &t->data_mem[bkt_key_index <<
429 : 0 : t->data_size_shl];
430 : :
431 : : /* Uninstall key from bucket */
432 : 0 : bkt->sig[i] = 0;
433 : 0 : *key_found = 1;
434 [ # # ]: 0 : if (entry)
435 : 0 : memcpy(entry, data, t->entry_size);
436 : :
437 : : /* Free key */
438 : 0 : t->key_stack[t->key_stack_tos++] =
439 : : bkt_key_index;
440 : :
441 : : /*Check if bucket is unused */
442 [ # # ]: 0 : if ((bkt_prev != NULL) &&
443 : : (bkt->sig[0] == 0) && (bkt->sig[1] == 0) &&
444 [ # # ]: 0 : (bkt->sig[2] == 0) && (bkt->sig[3] == 0)) {
445 : : /* Unchain bucket */
446 : 0 : BUCKET_NEXT_COPY(bkt_prev, bkt);
447 : :
448 : : /* Clear bucket */
449 : : memset(bkt, 0, sizeof(struct bucket));
450 : :
451 : : /* Free bucket back to buckets ext */
452 : 0 : bkt_index = bkt - t->buckets_ext;
453 : 0 : t->bkt_ext_stack[t->bkt_ext_stack_tos++]
454 : 0 : = bkt_index;
455 : : }
456 : :
457 : 0 : return 0;
458 : : }
459 : : }
460 : :
461 : : /* Key is not present in the bucket */
462 : 0 : *key_found = 0;
463 : 0 : return 0;
464 : : }
465 : :
466 : 0 : static int rte_table_hash_ext_lookup_unoptimized(
467 : : void *table,
468 : : struct rte_mbuf **pkts,
469 : : uint64_t pkts_mask,
470 : : uint64_t *lookup_hit_mask,
471 : : void **entries)
472 : : {
473 : : struct rte_table_hash *t = (struct rte_table_hash *) table;
474 : : uint64_t pkts_mask_out = 0;
475 : :
476 : : __rte_unused uint32_t n_pkts_in = rte_popcount64(pkts_mask);
477 : :
478 [ # # ]: 0 : for ( ; pkts_mask; ) {
479 : : struct bucket *bkt0, *bkt;
480 : : struct rte_mbuf *pkt;
481 : : uint8_t *key;
482 : : uint64_t pkt_mask, sig;
483 : : uint32_t pkt_index, bkt_index, i;
484 : :
485 : : pkt_index = rte_ctz64(pkts_mask);
486 : 0 : pkt_mask = 1LLU << pkt_index;
487 : 0 : pkts_mask &= ~pkt_mask;
488 : :
489 : 0 : pkt = pkts[pkt_index];
490 : 0 : key = RTE_MBUF_METADATA_UINT8_PTR(pkt, t->key_offset);
491 : 0 : sig = (uint64_t) t->f_hash(key, t->key_mask, t->key_size, t->seed);
492 : :
493 : 0 : bkt_index = sig & t->bucket_mask;
494 : 0 : bkt0 = &t->buckets[bkt_index];
495 : 0 : sig = (sig >> 16) | 1LLU;
496 : :
497 : : /* Key is present in the bucket */
498 [ # # ]: 0 : for (bkt = bkt0; bkt != NULL; bkt = BUCKET_NEXT(bkt))
499 [ # # ]: 0 : for (i = 0; i < KEYS_PER_BUCKET; i++) {
500 : 0 : uint64_t bkt_sig = (uint64_t) bkt->sig[i];
501 : 0 : uint32_t bkt_key_index = bkt->key_pos[i];
502 : 0 : uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
503 : 0 : t->key_size_shl];
504 : :
505 [ # # # # ]: 0 : if ((sig == bkt_sig) && (keycmp(bkt_key, key,
506 : 0 : t->key_mask, t->key_size) == 0)) {
507 : 0 : uint8_t *data = &t->data_mem[
508 : 0 : bkt_key_index << t->data_size_shl];
509 : :
510 : 0 : pkts_mask_out |= pkt_mask;
511 : 0 : entries[pkt_index] = (void *) data;
512 : 0 : break;
513 : : }
514 : : }
515 : : }
516 : :
517 : 0 : *lookup_hit_mask = pkts_mask_out;
518 : 0 : return 0;
519 : : }
520 : :
521 : : /*
522 : : * mask = match bitmask
523 : : * match = at least one match
524 : : * match_many = more than one match
525 : : * match_pos = position of first match
526 : : *
527 : : *----------------------------------------
528 : : * mask match match_many match_pos
529 : : *----------------------------------------
530 : : * 0000 0 0 00
531 : : * 0001 1 0 00
532 : : * 0010 1 0 01
533 : : * 0011 1 1 00
534 : : *----------------------------------------
535 : : * 0100 1 0 10
536 : : * 0101 1 1 00
537 : : * 0110 1 1 01
538 : : * 0111 1 1 00
539 : : *----------------------------------------
540 : : * 1000 1 0 11
541 : : * 1001 1 1 00
542 : : * 1010 1 1 01
543 : : * 1011 1 1 00
544 : : *----------------------------------------
545 : : * 1100 1 1 10
546 : : * 1101 1 1 00
547 : : * 1110 1 1 01
548 : : * 1111 1 1 00
549 : : *----------------------------------------
550 : : *
551 : : * match = 1111_1111_1111_1110
552 : : * match_many = 1111_1110_1110_1000
553 : : * match_pos = 0001_0010_0001_0011__0001_0010_0001_0000
554 : : *
555 : : * match = 0xFFFELLU
556 : : * match_many = 0xFEE8LLU
557 : : * match_pos = 0x12131210LLU
558 : : */
559 : :
560 : : #define LUT_MATCH 0xFFFELLU
561 : : #define LUT_MATCH_MANY 0xFEE8LLU
562 : : #define LUT_MATCH_POS 0x12131210LLU
563 : :
564 : : #define lookup_cmp_sig(mbuf_sig, bucket, match, match_many, match_pos) \
565 : : { \
566 : : uint64_t bucket_sig[4], mask[4], mask_all; \
567 : : \
568 : : bucket_sig[0] = bucket->sig[0]; \
569 : : bucket_sig[1] = bucket->sig[1]; \
570 : : bucket_sig[2] = bucket->sig[2]; \
571 : : bucket_sig[3] = bucket->sig[3]; \
572 : : \
573 : : bucket_sig[0] ^= mbuf_sig; \
574 : : bucket_sig[1] ^= mbuf_sig; \
575 : : bucket_sig[2] ^= mbuf_sig; \
576 : : bucket_sig[3] ^= mbuf_sig; \
577 : : \
578 : : mask[0] = 0; \
579 : : mask[1] = 0; \
580 : : mask[2] = 0; \
581 : : mask[3] = 0; \
582 : : \
583 : : if (bucket_sig[0] == 0) \
584 : : mask[0] = 1; \
585 : : if (bucket_sig[1] == 0) \
586 : : mask[1] = 2; \
587 : : if (bucket_sig[2] == 0) \
588 : : mask[2] = 4; \
589 : : if (bucket_sig[3] == 0) \
590 : : mask[3] = 8; \
591 : : \
592 : : mask_all = (mask[0] | mask[1]) | (mask[2] | mask[3]); \
593 : : \
594 : : match = (LUT_MATCH >> mask_all) & 1; \
595 : : match_many = (LUT_MATCH_MANY >> mask_all) & 1; \
596 : : match_pos = (LUT_MATCH_POS >> (mask_all << 1)) & 3; \
597 : : }
598 : :
599 : : #define lookup_cmp_key(mbuf, key, match_key, f) \
600 : : { \
601 : : uint64_t *pkt_key = RTE_MBUF_METADATA_UINT64_PTR(mbuf, f->key_offset);\
602 : : uint64_t *bkt_key = (uint64_t *) key; \
603 : : uint64_t *key_mask = f->key_mask; \
604 : : \
605 : : switch (f->key_size) { \
606 : : case 8: \
607 : : { \
608 : : uint64_t xor = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \
609 : : match_key = 0; \
610 : : if (xor == 0) \
611 : : match_key = 1; \
612 : : } \
613 : : break; \
614 : : \
615 : : case 16: \
616 : : { \
617 : : uint64_t xor[2], or; \
618 : : \
619 : : xor[0] = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \
620 : : xor[1] = (pkt_key[1] & key_mask[1]) ^ bkt_key[1]; \
621 : : or = xor[0] | xor[1]; \
622 : : match_key = 0; \
623 : : if (or == 0) \
624 : : match_key = 1; \
625 : : } \
626 : : break; \
627 : : \
628 : : case 32: \
629 : : { \
630 : : uint64_t xor[4], or; \
631 : : \
632 : : xor[0] = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \
633 : : xor[1] = (pkt_key[1] & key_mask[1]) ^ bkt_key[1]; \
634 : : xor[2] = (pkt_key[2] & key_mask[2]) ^ bkt_key[2]; \
635 : : xor[3] = (pkt_key[3] & key_mask[3]) ^ bkt_key[3]; \
636 : : or = xor[0] | xor[1] | xor[2] | xor[3]; \
637 : : match_key = 0; \
638 : : if (or == 0) \
639 : : match_key = 1; \
640 : : } \
641 : : break; \
642 : : \
643 : : case 64: \
644 : : { \
645 : : uint64_t xor[8], or; \
646 : : \
647 : : xor[0] = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \
648 : : xor[1] = (pkt_key[1] & key_mask[1]) ^ bkt_key[1]; \
649 : : xor[2] = (pkt_key[2] & key_mask[2]) ^ bkt_key[2]; \
650 : : xor[3] = (pkt_key[3] & key_mask[3]) ^ bkt_key[3]; \
651 : : xor[4] = (pkt_key[4] & key_mask[4]) ^ bkt_key[4]; \
652 : : xor[5] = (pkt_key[5] & key_mask[5]) ^ bkt_key[5]; \
653 : : xor[6] = (pkt_key[6] & key_mask[6]) ^ bkt_key[6]; \
654 : : xor[7] = (pkt_key[7] & key_mask[7]) ^ bkt_key[7]; \
655 : : or = xor[0] | xor[1] | xor[2] | xor[3] | \
656 : : xor[4] | xor[5] | xor[6] | xor[7]; \
657 : : match_key = 0; \
658 : : if (or == 0) \
659 : : match_key = 1; \
660 : : } \
661 : : break; \
662 : : \
663 : : default: \
664 : : match_key = 0; \
665 : : if (keycmp(bkt_key, pkt_key, key_mask, f->key_size) == 0) \
666 : : match_key = 1; \
667 : : } \
668 : : }
669 : :
670 : : #define lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index) \
671 : : { \
672 : : uint64_t pkt00_mask, pkt01_mask; \
673 : : struct rte_mbuf *mbuf00, *mbuf01; \
674 : : uint32_t key_offset = t->key_offset; \
675 : : \
676 : : pkt00_index = rte_ctz64(pkts_mask); \
677 : : pkt00_mask = 1LLU << pkt00_index; \
678 : : pkts_mask &= ~pkt00_mask; \
679 : : mbuf00 = pkts[pkt00_index]; \
680 : : \
681 : : pkt01_index = rte_ctz64(pkts_mask); \
682 : : pkt01_mask = 1LLU << pkt01_index; \
683 : : pkts_mask &= ~pkt01_mask; \
684 : : mbuf01 = pkts[pkt01_index]; \
685 : : \
686 : : rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
687 : : rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
688 : : }
689 : :
690 : : #define lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask, pkt00_index, \
691 : : pkt01_index) \
692 : : { \
693 : : uint64_t pkt00_mask, pkt01_mask; \
694 : : struct rte_mbuf *mbuf00, *mbuf01; \
695 : : uint32_t key_offset = t->key_offset; \
696 : : \
697 : : pkt00_index = rte_ctz64(pkts_mask); \
698 : : pkt00_mask = 1LLU << pkt00_index; \
699 : : pkts_mask &= ~pkt00_mask; \
700 : : mbuf00 = pkts[pkt00_index]; \
701 : : \
702 : : pkt01_index = rte_ctz64(pkts_mask); \
703 : : if (pkts_mask == 0) \
704 : : pkt01_index = pkt00_index; \
705 : : pkt01_mask = 1LLU << pkt01_index; \
706 : : pkts_mask &= ~pkt01_mask; \
707 : : mbuf01 = pkts[pkt01_index]; \
708 : : \
709 : : rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
710 : : rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
711 : : }
712 : :
713 : : #define lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index) \
714 : : { \
715 : : struct grinder *g10, *g11; \
716 : : uint64_t sig10, sig11, bkt10_index, bkt11_index; \
717 : : struct rte_mbuf *mbuf10, *mbuf11; \
718 : : struct bucket *bkt10, *bkt11, *buckets = t->buckets; \
719 : : uint8_t *key10, *key11; \
720 : : uint64_t bucket_mask = t->bucket_mask; \
721 : : rte_table_hash_op_hash f_hash = t->f_hash; \
722 : : uint64_t seed = t->seed; \
723 : : uint32_t key_size = t->key_size; \
724 : : uint32_t key_offset = t->key_offset; \
725 : : \
726 : : mbuf10 = pkts[pkt10_index]; \
727 : : key10 = RTE_MBUF_METADATA_UINT8_PTR(mbuf10, key_offset); \
728 : : sig10 = (uint64_t) f_hash(key10, t->key_mask, key_size, seed); \
729 : : bkt10_index = sig10 & bucket_mask; \
730 : : bkt10 = &buckets[bkt10_index]; \
731 : : \
732 : : mbuf11 = pkts[pkt11_index]; \
733 : : key11 = RTE_MBUF_METADATA_UINT8_PTR(mbuf11, key_offset); \
734 : : sig11 = (uint64_t) f_hash(key11, t->key_mask, key_size, seed); \
735 : : bkt11_index = sig11 & bucket_mask; \
736 : : bkt11 = &buckets[bkt11_index]; \
737 : : \
738 : : rte_prefetch0(bkt10); \
739 : : rte_prefetch0(bkt11); \
740 : : \
741 : : g10 = &g[pkt10_index]; \
742 : : g10->sig = sig10; \
743 : : g10->bkt = bkt10; \
744 : : \
745 : : g11 = &g[pkt11_index]; \
746 : : g11->sig = sig11; \
747 : : g11->bkt = bkt11; \
748 : : }
749 : :
750 : : #define lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many)\
751 : : { \
752 : : struct grinder *g20, *g21; \
753 : : uint64_t sig20, sig21; \
754 : : struct bucket *bkt20, *bkt21; \
755 : : uint8_t *key20, *key21, *key_mem = t->key_mem; \
756 : : uint64_t match20, match21, match_many20, match_many21; \
757 : : uint64_t match_pos20, match_pos21; \
758 : : uint32_t key20_index, key21_index, key_size_shl = t->key_size_shl;\
759 : : \
760 : : g20 = &g[pkt20_index]; \
761 : : sig20 = g20->sig; \
762 : : bkt20 = g20->bkt; \
763 : : sig20 = (sig20 >> 16) | 1LLU; \
764 : : lookup_cmp_sig(sig20, bkt20, match20, match_many20, match_pos20);\
765 : : match20 <<= pkt20_index; \
766 : : match_many20 |= BUCKET_NEXT_VALID(bkt20); \
767 : : match_many20 <<= pkt20_index; \
768 : : key20_index = bkt20->key_pos[match_pos20]; \
769 : : key20 = &key_mem[key20_index << key_size_shl]; \
770 : : \
771 : : g21 = &g[pkt21_index]; \
772 : : sig21 = g21->sig; \
773 : : bkt21 = g21->bkt; \
774 : : sig21 = (sig21 >> 16) | 1LLU; \
775 : : lookup_cmp_sig(sig21, bkt21, match21, match_many21, match_pos21);\
776 : : match21 <<= pkt21_index; \
777 : : match_many21 |= BUCKET_NEXT_VALID(bkt21); \
778 : : match_many21 <<= pkt21_index; \
779 : : key21_index = bkt21->key_pos[match_pos21]; \
780 : : key21 = &key_mem[key21_index << key_size_shl]; \
781 : : \
782 : : rte_prefetch0(key20); \
783 : : rte_prefetch0(key21); \
784 : : \
785 : : pkts_mask_match_many |= match_many20 | match_many21; \
786 : : \
787 : : g20->match = match20; \
788 : : g20->key_index = key20_index; \
789 : : \
790 : : g21->match = match21; \
791 : : g21->key_index = key21_index; \
792 : : }
793 : :
794 : : #define lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out, \
795 : : entries) \
796 : : { \
797 : : struct grinder *g30, *g31; \
798 : : struct rte_mbuf *mbuf30, *mbuf31; \
799 : : uint8_t *key30, *key31, *key_mem = t->key_mem; \
800 : : uint8_t *data30, *data31, *data_mem = t->data_mem; \
801 : : uint64_t match30, match31, match_key30, match_key31, match_keys;\
802 : : uint32_t key30_index, key31_index; \
803 : : uint32_t key_size_shl = t->key_size_shl; \
804 : : uint32_t data_size_shl = t->data_size_shl; \
805 : : \
806 : : mbuf30 = pkts[pkt30_index]; \
807 : : g30 = &g[pkt30_index]; \
808 : : match30 = g30->match; \
809 : : key30_index = g30->key_index; \
810 : : key30 = &key_mem[key30_index << key_size_shl]; \
811 : : lookup_cmp_key(mbuf30, key30, match_key30, t); \
812 : : match_key30 <<= pkt30_index; \
813 : : match_key30 &= match30; \
814 : : data30 = &data_mem[key30_index << data_size_shl]; \
815 : : entries[pkt30_index] = data30; \
816 : : \
817 : : mbuf31 = pkts[pkt31_index]; \
818 : : g31 = &g[pkt31_index]; \
819 : : match31 = g31->match; \
820 : : key31_index = g31->key_index; \
821 : : key31 = &key_mem[key31_index << key_size_shl]; \
822 : : lookup_cmp_key(mbuf31, key31, match_key31, t); \
823 : : match_key31 <<= pkt31_index; \
824 : : match_key31 &= match31; \
825 : : data31 = &data_mem[key31_index << data_size_shl]; \
826 : : entries[pkt31_index] = data31; \
827 : : \
828 : : rte_prefetch0(data30); \
829 : : rte_prefetch0(data31); \
830 : : \
831 : : match_keys = match_key30 | match_key31; \
832 : : pkts_mask_out |= match_keys; \
833 : : }
834 : :
835 : : /*
836 : : * The lookup function implements a 4-stage pipeline, with each stage processing
837 : : * two different packets. The purpose of pipelined implementation is to hide the
838 : : * latency of prefetching the data structures and loosen the data dependency
839 : : * between instructions.
840 : : *
841 : : * p00 _______ p10 _______ p20 _______ p30 _______
842 : : *----->| |----->| |----->| |----->| |----->
843 : : * | 0 | | 1 | | 2 | | 3 |
844 : : *----->|_______|----->|_______|----->|_______|----->|_______|----->
845 : : * p01 p11 p21 p31
846 : : *
847 : : * The naming convention is:
848 : : * pXY = packet Y of stage X, X = 0 .. 3, Y = 0 .. 1
849 : : */
850 : 0 : static int rte_table_hash_ext_lookup(
851 : : void *table,
852 : : struct rte_mbuf **pkts,
853 : : uint64_t pkts_mask,
854 : : uint64_t *lookup_hit_mask,
855 : : void **entries)
856 : : {
857 : : struct rte_table_hash *t = (struct rte_table_hash *) table;
858 [ # # ]: 0 : struct grinder *g = t->grinders;
859 : : uint64_t pkt00_index, pkt01_index, pkt10_index, pkt11_index;
860 : : uint64_t pkt20_index, pkt21_index, pkt30_index, pkt31_index;
861 : : uint64_t pkts_mask_out = 0, pkts_mask_match_many = 0;
862 : : int status = 0;
863 : :
864 : : __rte_unused uint32_t n_pkts_in = rte_popcount64(pkts_mask);
865 : : RTE_TABLE_HASH_EXT_STATS_PKTS_IN_ADD(t, n_pkts_in);
866 : :
867 : : /* Cannot run the pipeline with less than 7 packets */
868 [ # # ]: 0 : if (rte_popcount64(pkts_mask) < 7) {
869 : 0 : status = rte_table_hash_ext_lookup_unoptimized(table, pkts,
870 : : pkts_mask, lookup_hit_mask, entries);
871 : : RTE_TABLE_HASH_EXT_STATS_PKTS_LOOKUP_MISS(t, n_pkts_in -
872 : : rte_popcount64(*lookup_hit_mask));
873 : 0 : return status;
874 : : }
875 : :
876 : : /* Pipeline stage 0 */
877 : 0 : lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
878 : :
879 : : /* Pipeline feed */
880 : : pkt10_index = pkt00_index;
881 : : pkt11_index = pkt01_index;
882 : :
883 : : /* Pipeline stage 0 */
884 : 0 : lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
885 : :
886 : : /* Pipeline stage 1 */
887 : 0 : lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
888 : :
889 : : /* Pipeline feed */
890 : : pkt20_index = pkt10_index;
891 : : pkt21_index = pkt11_index;
892 : : pkt10_index = pkt00_index;
893 : : pkt11_index = pkt01_index;
894 : :
895 : : /* Pipeline stage 0 */
896 : 0 : lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
897 : :
898 : : /* Pipeline stage 1 */
899 : 0 : lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
900 : :
901 : : /* Pipeline stage 2 */
902 [ # # # # : 0 : lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
# # # # #
# # # # #
# # ]
903 : :
904 : : /*
905 : : * Pipeline run
906 : : *
907 : : */
908 [ # # ]: 0 : for ( ; pkts_mask; ) {
909 : : /* Pipeline feed */
910 : : pkt30_index = pkt20_index;
911 : : pkt31_index = pkt21_index;
912 : : pkt20_index = pkt10_index;
913 : : pkt21_index = pkt11_index;
914 : : pkt10_index = pkt00_index;
915 : : pkt11_index = pkt01_index;
916 : :
917 : : /* Pipeline stage 0 */
918 [ # # ]: 0 : lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask,
919 : : pkt00_index, pkt01_index);
920 : :
921 : : /* Pipeline stage 1 */
922 : 0 : lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
923 : :
924 : : /* Pipeline stage 2 */
925 [ # # # # : 0 : lookup2_stage2(t, g, pkt20_index, pkt21_index,
# # # # #
# # # # #
# # ]
926 : : pkts_mask_match_many);
927 : :
928 : : /* Pipeline stage 3 */
929 [ # # # # : 0 : lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index,
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
930 : : pkts_mask_out, entries);
931 : : }
932 : :
933 : : /* Pipeline feed */
934 : : pkt30_index = pkt20_index;
935 : : pkt31_index = pkt21_index;
936 : : pkt20_index = pkt10_index;
937 : : pkt21_index = pkt11_index;
938 : : pkt10_index = pkt00_index;
939 : : pkt11_index = pkt01_index;
940 : :
941 : : /* Pipeline stage 1 */
942 : 0 : lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
943 : :
944 : : /* Pipeline stage 2 */
945 [ # # # # : 0 : lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
# # # # #
# # # # #
# # ]
946 : :
947 : : /* Pipeline stage 3 */
948 [ # # # # : 0 : lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
949 : : entries);
950 : :
951 : : /* Pipeline feed */
952 : : pkt30_index = pkt20_index;
953 : : pkt31_index = pkt21_index;
954 : : pkt20_index = pkt10_index;
955 : : pkt21_index = pkt11_index;
956 : :
957 : : /* Pipeline stage 2 */
958 [ # # # # : 0 : lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
# # # # #
# # # # #
# # ]
959 : :
960 : : /* Pipeline stage 3 */
961 [ # # # # : 0 : lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
962 : : entries);
963 : :
964 : : /* Pipeline feed */
965 : : pkt30_index = pkt20_index;
966 : : pkt31_index = pkt21_index;
967 : :
968 : : /* Pipeline stage 3 */
969 [ # # # # : 0 : lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
970 : : entries);
971 : :
972 : : /* Slow path */
973 : 0 : pkts_mask_match_many &= ~pkts_mask_out;
974 [ # # ]: 0 : if (pkts_mask_match_many) {
975 : 0 : uint64_t pkts_mask_out_slow = 0;
976 : :
977 : 0 : status = rte_table_hash_ext_lookup_unoptimized(table, pkts,
978 : : pkts_mask_match_many, &pkts_mask_out_slow, entries);
979 : 0 : pkts_mask_out |= pkts_mask_out_slow;
980 : : }
981 : :
982 : 0 : *lookup_hit_mask = pkts_mask_out;
983 : : RTE_TABLE_HASH_EXT_STATS_PKTS_LOOKUP_MISS(t, n_pkts_in - rte_popcount64(pkts_mask_out));
984 : 0 : return status;
985 : : }
986 : :
987 : : static int
988 : 0 : rte_table_hash_ext_stats_read(void *table, struct rte_table_stats *stats, int clear)
989 : : {
990 : : struct rte_table_hash *t = table;
991 : :
992 [ # # ]: 0 : if (stats != NULL)
993 : 0 : memcpy(stats, &t->stats, sizeof(t->stats));
994 : :
995 [ # # ]: 0 : if (clear)
996 : 0 : memset(&t->stats, 0, sizeof(t->stats));
997 : :
998 : 0 : return 0;
999 : : }
1000 : :
1001 : : RTE_EXPORT_SYMBOL(rte_table_hash_ext_ops)
1002 : : struct rte_table_ops rte_table_hash_ext_ops = {
1003 : : .f_create = rte_table_hash_ext_create,
1004 : : .f_free = rte_table_hash_ext_free,
1005 : : .f_add = rte_table_hash_ext_entry_add,
1006 : : .f_delete = rte_table_hash_ext_entry_delete,
1007 : : .f_add_bulk = NULL,
1008 : : .f_delete_bulk = NULL,
1009 : : .f_lookup = rte_table_hash_ext_lookup,
1010 : : .f_stats = rte_table_hash_ext_stats_read,
1011 : : };
|