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