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