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