xref: /dpdk/lib/table/rte_table_hash_lru.c (revision daa02b5cddbb8e11b31d41e2bf7bb1ae64dcae2f)
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_mbuf.h>
10 #include <rte_memory.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 #define KEYS_PER_BUCKET	4
18 
19 #ifdef RTE_TABLE_STATS_COLLECT
20 
21 #define RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(table, val) \
22 	table->stats.n_pkts_in += val
23 #define RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(table, val) \
24 	table->stats.n_pkts_lookup_miss += val
25 
26 #else
27 
28 #define RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(table, val)
29 #define RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(table, val)
30 
31 #endif
32 
33 struct bucket {
34 	union {
35 		struct bucket *next;
36 		uint64_t lru_list;
37 	};
38 	uint16_t sig[KEYS_PER_BUCKET];
39 	uint32_t key_pos[KEYS_PER_BUCKET];
40 };
41 
42 struct grinder {
43 	struct bucket *bkt;
44 	uint64_t sig;
45 	uint64_t match;
46 	uint64_t match_pos;
47 	uint32_t key_index;
48 };
49 
50 struct rte_table_hash {
51 	struct rte_table_stats stats;
52 
53 	/* Input parameters */
54 	uint32_t key_size;
55 	uint32_t entry_size;
56 	uint32_t n_keys;
57 	uint32_t n_buckets;
58 	rte_table_hash_op_hash f_hash;
59 	uint64_t seed;
60 	uint32_t key_offset;
61 
62 	/* Internal */
63 	uint64_t bucket_mask;
64 	uint32_t key_size_shl;
65 	uint32_t data_size_shl;
66 	uint32_t key_stack_tos;
67 
68 	/* Grinder */
69 	struct grinder grinders[RTE_PORT_IN_BURST_SIZE_MAX];
70 
71 	/* Tables */
72 	uint64_t *key_mask;
73 	struct bucket *buckets;
74 	uint8_t *key_mem;
75 	uint8_t *data_mem;
76 	uint32_t *key_stack;
77 
78 	/* Table memory */
79 	uint8_t memory[0] __rte_cache_aligned;
80 };
81 
82 static int
83 keycmp(void *a, void *b, void *b_mask, uint32_t n_bytes)
84 {
85 	uint64_t *a64 = a, *b64 = b, *b_mask64 = b_mask;
86 	uint32_t i;
87 
88 	for (i = 0; i < n_bytes / sizeof(uint64_t); i++)
89 		if (a64[i] != (b64[i] & b_mask64[i]))
90 			return 1;
91 
92 	return 0;
93 }
94 
95 static void
96 keycpy(void *dst, void *src, void *src_mask, uint32_t n_bytes)
97 {
98 	uint64_t *dst64 = dst, *src64 = src, *src_mask64 = src_mask;
99 	uint32_t i;
100 
101 	for (i = 0; i < n_bytes / sizeof(uint64_t); i++)
102 		dst64[i] = src64[i] & src_mask64[i];
103 }
104 
105 static int
106 check_params_create(struct rte_table_hash_params *params)
107 {
108 	/* name */
109 	if (params->name == NULL) {
110 		RTE_LOG(ERR, TABLE, "%s: name invalid value\n", __func__);
111 		return -EINVAL;
112 	}
113 
114 	/* key_size */
115 	if ((params->key_size < sizeof(uint64_t)) ||
116 		(!rte_is_power_of_2(params->key_size))) {
117 		RTE_LOG(ERR, TABLE, "%s: key_size invalid value\n", __func__);
118 		return -EINVAL;
119 	}
120 
121 	/* n_keys */
122 	if (params->n_keys == 0) {
123 		RTE_LOG(ERR, TABLE, "%s: n_keys invalid value\n", __func__);
124 		return -EINVAL;
125 	}
126 
127 	/* n_buckets */
128 	if ((params->n_buckets == 0) ||
129 		(!rte_is_power_of_2(params->n_buckets))) {
130 		RTE_LOG(ERR, TABLE, "%s: n_buckets invalid value\n", __func__);
131 		return -EINVAL;
132 	}
133 
134 	/* f_hash */
135 	if (params->f_hash == NULL) {
136 		RTE_LOG(ERR, TABLE, "%s: f_hash invalid value\n", __func__);
137 		return -EINVAL;
138 	}
139 
140 	return 0;
141 }
142 
143 static void *
144 rte_table_hash_lru_create(void *params, int socket_id, uint32_t entry_size)
145 {
146 	struct rte_table_hash_params *p = params;
147 	struct rte_table_hash *t;
148 	uint64_t table_meta_sz, key_mask_sz, bucket_sz, key_sz, key_stack_sz;
149 	uint64_t data_sz, total_size;
150 	uint64_t key_mask_offset, bucket_offset, key_offset, key_stack_offset;
151 	uint64_t data_offset;
152 	uint32_t n_buckets, i;
153 
154 	/* Check input parameters */
155 	if ((check_params_create(p) != 0) ||
156 		(!rte_is_power_of_2(entry_size)) ||
157 		((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
158 		(sizeof(struct bucket) != (RTE_CACHE_LINE_SIZE / 2))) {
159 		return NULL;
160 	}
161 
162 	/*
163 	 * Table dimensioning
164 	 *
165 	 * Objective: Pick the number of buckets (n_buckets) so that there a chance
166 	 * to store n_keys keys in the table.
167 	 *
168 	 * Note: Since the buckets do not get extended, it is not possible to
169 	 * guarantee that n_keys keys can be stored in the table at any time. In the
170 	 * worst case scenario when all the n_keys fall into the same bucket, only
171 	 * a maximum of KEYS_PER_BUCKET keys will be stored in the table. This case
172 	 * defeats the purpose of the hash table. It indicates unsuitable f_hash or
173 	 * n_keys to n_buckets ratio.
174 	 *
175 	 * MIN(n_buckets) = (n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET
176 	 */
177 	n_buckets = rte_align32pow2(
178 		(p->n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET);
179 	n_buckets = RTE_MAX(n_buckets, p->n_buckets);
180 
181 	/* Memory allocation */
182 	table_meta_sz = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_table_hash));
183 	key_mask_sz = RTE_CACHE_LINE_ROUNDUP(p->key_size);
184 	bucket_sz = RTE_CACHE_LINE_ROUNDUP(n_buckets * sizeof(struct bucket));
185 	key_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * p->key_size);
186 	key_stack_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * sizeof(uint32_t));
187 	data_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * entry_size);
188 	total_size = table_meta_sz + key_mask_sz + bucket_sz + key_sz +
189 		key_stack_sz + data_sz;
190 
191 	if (total_size > SIZE_MAX) {
192 		RTE_LOG(ERR, TABLE,
193 			"%s: Cannot allocate %" PRIu64 " bytes for hash "
194 			"table %s\n",
195 			__func__, total_size, p->name);
196 		return NULL;
197 	}
198 
199 	t = rte_zmalloc_socket(p->name,
200 		(size_t)total_size,
201 		RTE_CACHE_LINE_SIZE,
202 		socket_id);
203 	if (t == NULL) {
204 		RTE_LOG(ERR, TABLE,
205 			"%s: Cannot allocate %" PRIu64 " bytes for hash "
206 			"table %s\n",
207 			__func__, total_size, p->name);
208 		return NULL;
209 	}
210 	RTE_LOG(INFO, TABLE, "%s (%u-byte key): Hash table %s memory footprint"
211 		" is %" PRIu64 " bytes\n",
212 		__func__, p->key_size, p->name, total_size);
213 
214 	/* Memory initialization */
215 	t->key_size = p->key_size;
216 	t->entry_size = entry_size;
217 	t->n_keys = p->n_keys;
218 	t->n_buckets = n_buckets;
219 	t->f_hash = p->f_hash;
220 	t->seed = p->seed;
221 	t->key_offset = p->key_offset;
222 
223 	/* Internal */
224 	t->bucket_mask = t->n_buckets - 1;
225 	t->key_size_shl = __builtin_ctzl(p->key_size);
226 	t->data_size_shl = __builtin_ctzl(entry_size);
227 
228 	/* Tables */
229 	key_mask_offset = 0;
230 	bucket_offset = key_mask_offset + key_mask_sz;
231 	key_offset = bucket_offset + bucket_sz;
232 	key_stack_offset = key_offset + key_sz;
233 	data_offset = key_stack_offset + key_stack_sz;
234 
235 	t->key_mask = (uint64_t *) &t->memory[key_mask_offset];
236 	t->buckets = (struct bucket *) &t->memory[bucket_offset];
237 	t->key_mem = &t->memory[key_offset];
238 	t->key_stack = (uint32_t *) &t->memory[key_stack_offset];
239 	t->data_mem = &t->memory[data_offset];
240 
241 	/* Key mask */
242 	if (p->key_mask == NULL)
243 		memset(t->key_mask, 0xFF, p->key_size);
244 	else
245 		memcpy(t->key_mask, p->key_mask, p->key_size);
246 
247 	/* Key stack */
248 	for (i = 0; i < t->n_keys; i++)
249 		t->key_stack[i] = t->n_keys - 1 - i;
250 	t->key_stack_tos = t->n_keys;
251 
252 	/* LRU */
253 	for (i = 0; i < t->n_buckets; i++) {
254 		struct bucket *bkt = &t->buckets[i];
255 
256 		lru_init(bkt);
257 	}
258 
259 	return t;
260 }
261 
262 static int
263 rte_table_hash_lru_free(void *table)
264 {
265 	struct rte_table_hash *t = table;
266 
267 	/* Check input parameters */
268 	if (t == NULL)
269 		return -EINVAL;
270 
271 	rte_free(t);
272 	return 0;
273 }
274 
275 static int
276 rte_table_hash_lru_entry_add(void *table, void *key, void *entry,
277 	int *key_found, void **entry_ptr)
278 {
279 	struct rte_table_hash *t = table;
280 	struct bucket *bkt;
281 	uint64_t sig;
282 	uint32_t bkt_index, i;
283 
284 	sig = t->f_hash(key, t->key_mask, t->key_size, t->seed);
285 	bkt_index = sig & t->bucket_mask;
286 	bkt = &t->buckets[bkt_index];
287 	sig = (sig >> 16) | 1LLU;
288 
289 	/* Key is present in the bucket */
290 	for (i = 0; i < KEYS_PER_BUCKET; i++) {
291 		uint64_t bkt_sig = (uint64_t) bkt->sig[i];
292 		uint32_t bkt_key_index = bkt->key_pos[i];
293 		uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
294 			t->key_size_shl];
295 
296 		if ((sig == bkt_sig) && (keycmp(bkt_key, key, t->key_mask,
297 			t->key_size) == 0)) {
298 			uint8_t *data = &t->data_mem[bkt_key_index <<
299 				t->data_size_shl];
300 
301 			memcpy(data, entry, t->entry_size);
302 			lru_update(bkt, i);
303 			*key_found = 1;
304 			*entry_ptr = (void *) data;
305 			return 0;
306 		}
307 	}
308 
309 	/* Key is not present in the bucket */
310 	for (i = 0; i < KEYS_PER_BUCKET; i++) {
311 		uint64_t bkt_sig = (uint64_t) bkt->sig[i];
312 
313 		if (bkt_sig == 0) {
314 			uint32_t bkt_key_index;
315 			uint8_t *bkt_key, *data;
316 
317 			/* Allocate new key */
318 			if (t->key_stack_tos == 0) {
319 				/* No keys available */
320 				return -ENOSPC;
321 			}
322 			bkt_key_index = t->key_stack[--t->key_stack_tos];
323 
324 			/* Install new key */
325 			bkt_key = &t->key_mem[bkt_key_index << t->key_size_shl];
326 			data = &t->data_mem[bkt_key_index << t->data_size_shl];
327 
328 			bkt->sig[i] = (uint16_t) sig;
329 			bkt->key_pos[i] = bkt_key_index;
330 			keycpy(bkt_key, key, t->key_mask, t->key_size);
331 			memcpy(data, entry, t->entry_size);
332 			lru_update(bkt, i);
333 
334 			*key_found = 0;
335 			*entry_ptr = (void *) data;
336 			return 0;
337 		}
338 	}
339 
340 	/* Bucket full */
341 	{
342 		uint64_t pos = lru_pos(bkt);
343 		uint32_t bkt_key_index = bkt->key_pos[pos];
344 		uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
345 			t->key_size_shl];
346 		uint8_t *data = &t->data_mem[bkt_key_index << t->data_size_shl];
347 
348 		bkt->sig[pos] = (uint16_t) sig;
349 		keycpy(bkt_key, key, t->key_mask, t->key_size);
350 		memcpy(data, entry, t->entry_size);
351 		lru_update(bkt, pos);
352 
353 		*key_found = 0;
354 		*entry_ptr = (void *) data;
355 		return 0;
356 	}
357 }
358 
359 static int
360 rte_table_hash_lru_entry_delete(void *table, void *key, int *key_found,
361 	void *entry)
362 {
363 	struct rte_table_hash *t = table;
364 	struct bucket *bkt;
365 	uint64_t sig;
366 	uint32_t bkt_index, i;
367 
368 	sig = t->f_hash(key, t->key_mask, t->key_size, t->seed);
369 	bkt_index = sig & t->bucket_mask;
370 	bkt = &t->buckets[bkt_index];
371 	sig = (sig >> 16) | 1LLU;
372 
373 	/* Key is present in the bucket */
374 	for (i = 0; i < KEYS_PER_BUCKET; i++) {
375 		uint64_t bkt_sig = (uint64_t) bkt->sig[i];
376 		uint32_t bkt_key_index = bkt->key_pos[i];
377 		uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
378 			t->key_size_shl];
379 
380 		if ((sig == bkt_sig) &&
381 			(keycmp(bkt_key, key, t->key_mask, t->key_size) == 0)) {
382 			uint8_t *data = &t->data_mem[bkt_key_index <<
383 				t->data_size_shl];
384 
385 			bkt->sig[i] = 0;
386 			t->key_stack[t->key_stack_tos++] = bkt_key_index;
387 			*key_found = 1;
388 			if (entry)
389 				memcpy(entry, data, t->entry_size);
390 			return 0;
391 		}
392 	}
393 
394 	/* Key is not present in the bucket */
395 	*key_found = 0;
396 	return 0;
397 }
398 
399 static int rte_table_hash_lru_lookup_unoptimized(
400 	void *table,
401 	struct rte_mbuf **pkts,
402 	uint64_t pkts_mask,
403 	uint64_t *lookup_hit_mask,
404 	void **entries)
405 {
406 	struct rte_table_hash *t = (struct rte_table_hash *) table;
407 	uint64_t pkts_mask_out = 0;
408 
409 	__rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
410 	RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(t, n_pkts_in);
411 
412 	for ( ; pkts_mask; ) {
413 		struct bucket *bkt;
414 		struct rte_mbuf *pkt;
415 		uint8_t *key;
416 		uint64_t pkt_mask, sig;
417 		uint32_t pkt_index, bkt_index, i;
418 
419 		pkt_index = __builtin_ctzll(pkts_mask);
420 		pkt_mask = 1LLU << pkt_index;
421 		pkts_mask &= ~pkt_mask;
422 
423 		pkt = pkts[pkt_index];
424 		key = RTE_MBUF_METADATA_UINT8_PTR(pkt, t->key_offset);
425 		sig = (uint64_t) t->f_hash(key, t->key_mask, t->key_size, t->seed);
426 
427 		bkt_index = sig & t->bucket_mask;
428 		bkt = &t->buckets[bkt_index];
429 		sig = (sig >> 16) | 1LLU;
430 
431 		/* Key is present in the bucket */
432 		for (i = 0; i < KEYS_PER_BUCKET; i++) {
433 			uint64_t bkt_sig = (uint64_t) bkt->sig[i];
434 			uint32_t bkt_key_index = bkt->key_pos[i];
435 			uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
436 				t->key_size_shl];
437 
438 			if ((sig == bkt_sig) && (keycmp(bkt_key, key, t->key_mask,
439 				t->key_size) == 0)) {
440 				uint8_t *data = &t->data_mem[bkt_key_index <<
441 					t->data_size_shl];
442 
443 				lru_update(bkt, i);
444 				pkts_mask_out |= pkt_mask;
445 				entries[pkt_index] = (void *) data;
446 				break;
447 			}
448 		}
449 	}
450 
451 	*lookup_hit_mask = pkts_mask_out;
452 	RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(t, n_pkts_in - __builtin_popcountll(pkts_mask_out));
453 	return 0;
454 }
455 
456 /***
457 *
458 * mask = match bitmask
459 * match = at least one match
460 * match_many = more than one match
461 * match_pos = position of first match
462 *
463 * ----------------------------------------
464 * mask		 match	 match_many	  match_pos
465 * ----------------------------------------
466 * 0000		 0		 0			  00
467 * 0001		 1		 0			  00
468 * 0010		 1		 0			  01
469 * 0011		 1		 1			  00
470 * ----------------------------------------
471 * 0100		 1		 0			  10
472 * 0101		 1		 1			  00
473 * 0110		 1		 1			  01
474 * 0111		 1		 1			  00
475 * ----------------------------------------
476 * 1000		 1		 0			  11
477 * 1001		 1		 1			  00
478 * 1010		 1		 1			  01
479 * 1011		 1		 1			  00
480 * ----------------------------------------
481 * 1100		 1		 1			  10
482 * 1101		 1		 1			  00
483 * 1110		 1		 1			  01
484 * 1111		 1		 1			  00
485 * ----------------------------------------
486 *
487 * match = 1111_1111_1111_1110
488 * match_many = 1111_1110_1110_1000
489 * match_pos = 0001_0010_0001_0011__0001_0010_0001_0000
490 *
491 * match = 0xFFFELLU
492 * match_many = 0xFEE8LLU
493 * match_pos = 0x12131210LLU
494 *
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 = __builtin_ctzll(pkts_mask);		\
614 	pkt00_mask = 1LLU << pkt00_index;			\
615 	pkts_mask &= ~pkt00_mask;				\
616 	mbuf00 = pkts[pkt00_index];				\
617 								\
618 	pkt01_index = __builtin_ctzll(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 = __builtin_ctzll(pkts_mask);		\
635 	pkt00_mask = 1LLU << pkt00_index;			\
636 	pkts_mask &= ~pkt00_mask;				\
637 	mbuf00 = pkts[pkt00_index];				\
638 								\
639 	pkt01_index = __builtin_ctzll(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 ***/
803 static int rte_table_hash_lru_lookup(
804 	void *table,
805 	struct rte_mbuf **pkts,
806 	uint64_t pkts_mask,
807 	uint64_t *lookup_hit_mask,
808 	void **entries)
809 {
810 	struct rte_table_hash *t = (struct rte_table_hash *) table;
811 	struct grinder *g = t->grinders;
812 	uint64_t pkt00_index, pkt01_index, pkt10_index, pkt11_index;
813 	uint64_t pkt20_index, pkt21_index, pkt30_index, pkt31_index;
814 	uint64_t pkts_mask_out = 0, pkts_mask_match_many = 0;
815 	int status = 0;
816 
817 	__rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
818 	RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(t, n_pkts_in);
819 
820 	/* Cannot run the pipeline with less than 7 packets */
821 	if (__builtin_popcountll(pkts_mask) < 7)
822 		return rte_table_hash_lru_lookup_unoptimized(table, pkts,
823 			pkts_mask, lookup_hit_mask, entries);
824 
825 	/* Pipeline stage 0 */
826 	lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
827 
828 	/* Pipeline feed */
829 	pkt10_index = pkt00_index;
830 	pkt11_index = pkt01_index;
831 
832 	/* Pipeline stage 0 */
833 	lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
834 
835 	/* Pipeline stage 1 */
836 	lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
837 
838 	/* Pipeline feed */
839 	pkt20_index = pkt10_index;
840 	pkt21_index = pkt11_index;
841 	pkt10_index = pkt00_index;
842 	pkt11_index = pkt01_index;
843 
844 	/* Pipeline stage 0 */
845 	lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
846 
847 	/* Pipeline stage 1 */
848 	lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
849 
850 	/* Pipeline stage 2 */
851 	lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
852 
853 	/*
854 	* Pipeline run
855 	*
856 	*/
857 	for ( ; pkts_mask; ) {
858 		/* Pipeline feed */
859 		pkt30_index = pkt20_index;
860 		pkt31_index = pkt21_index;
861 		pkt20_index = pkt10_index;
862 		pkt21_index = pkt11_index;
863 		pkt10_index = pkt00_index;
864 		pkt11_index = pkt01_index;
865 
866 		/* Pipeline stage 0 */
867 		lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask,
868 			pkt00_index, pkt01_index);
869 
870 		/* Pipeline stage 1 */
871 		lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
872 
873 		/* Pipeline stage 2 */
874 		lookup2_stage2(t, g, pkt20_index, pkt21_index,
875 			pkts_mask_match_many);
876 
877 		/* Pipeline stage 3 */
878 		lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index,
879 			pkts_mask_out, entries);
880 	}
881 
882 	/* Pipeline feed */
883 	pkt30_index = pkt20_index;
884 	pkt31_index = pkt21_index;
885 	pkt20_index = pkt10_index;
886 	pkt21_index = pkt11_index;
887 	pkt10_index = pkt00_index;
888 	pkt11_index = pkt01_index;
889 
890 	/* Pipeline stage 1 */
891 	lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
892 
893 	/* Pipeline stage 2 */
894 	lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
895 
896 	/* Pipeline stage 3 */
897 	lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
898 		entries);
899 
900 	/* Pipeline feed */
901 	pkt30_index = pkt20_index;
902 	pkt31_index = pkt21_index;
903 	pkt20_index = pkt10_index;
904 	pkt21_index = pkt11_index;
905 
906 	/* Pipeline stage 2 */
907 	lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
908 
909 	/* Pipeline stage 3 */
910 	lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
911 		entries);
912 
913 	/* Pipeline feed */
914 	pkt30_index = pkt20_index;
915 	pkt31_index = pkt21_index;
916 
917 	/* Pipeline stage 3 */
918 	lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
919 		entries);
920 
921 	/* Slow path */
922 	pkts_mask_match_many &= ~pkts_mask_out;
923 	if (pkts_mask_match_many) {
924 		uint64_t pkts_mask_out_slow = 0;
925 
926 		status = rte_table_hash_lru_lookup_unoptimized(table, pkts,
927 			pkts_mask_match_many, &pkts_mask_out_slow, entries);
928 		pkts_mask_out |= pkts_mask_out_slow;
929 	}
930 
931 	*lookup_hit_mask = pkts_mask_out;
932 	RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(t, n_pkts_in - __builtin_popcountll(pkts_mask_out));
933 	return status;
934 }
935 
936 static int
937 rte_table_hash_lru_stats_read(void *table, struct rte_table_stats *stats, int clear)
938 {
939 	struct rte_table_hash *t = table;
940 
941 	if (stats != NULL)
942 		memcpy(stats, &t->stats, sizeof(t->stats));
943 
944 	if (clear)
945 		memset(&t->stats, 0, sizeof(t->stats));
946 
947 	return 0;
948 }
949 
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 };
960