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