aboutsummaryrefslogtreecommitdiffstats
path: root/lib/librte_table/rte_table_hash_lru.c
blob: 407c62ab8f020c93db503fb3bc7e760c11948e47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
/*-
 *	 BSD LICENSE
 *
 *	 Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
 *	 All rights reserved.
 *
 *	 Redistribution and use in source and binary forms, with or without
 *	 modification, are permitted provided that the following conditions
 *	 are met:
 *
 *	* Redistributions of source code must retain the above copyright
 *		 notice, this list of conditions and the following disclaimer.
 *	* Redistributions in binary form must reproduce the above copyright
 *		 notice, this list of conditions and the following disclaimer in
 *		 the documentation and/or other materials provided with the
 *		 distribution.
 *	* Neither the name of Intel Corporation nor the names of its
 *		 contributors may be used to endorse or promote products derived
 *		 from this software without specific prior written permission.
 *
 *	 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *	 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *	 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *	 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *	 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *	 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *	 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *	 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *	 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *	 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *	 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <string.h>
#include <stdio.h>

#include <rte_common.h>
#include <rte_mbuf.h>
#include <rte_memory.h>
#include <rte_malloc.h>
#include <rte_log.h>

#include "rte_table_hash.h"
#include "rte_lru.h"

#define KEYS_PER_BUCKET	4

#ifdef RTE_TABLE_STATS_COLLECT

#define RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(table, val) \
	table->stats.n_pkts_in += val
#define RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(table, val) \
	table->stats.n_pkts_lookup_miss += val

#else

#define RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(table, val)
#define RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(table, val)

#endif

struct bucket {
	union {
		struct bucket *next;
		uint64_t lru_list;
	};
	uint16_t sig[KEYS_PER_BUCKET];
	uint32_t key_pos[KEYS_PER_BUCKET];
};

struct grinder {
	struct bucket *bkt;
	uint64_t sig;
	uint64_t match;
	uint64_t match_pos;
	uint32_t key_index;
};

struct rte_table_hash {
	struct rte_table_stats stats;

	/* Input parameters */
	uint32_t key_size;
	uint32_t entry_size;
	uint32_t n_keys;
	uint32_t n_buckets;
	rte_table_hash_op_hash f_hash;
	uint64_t seed;
	uint32_t signature_offset;
	uint32_t key_offset;

	/* Internal */
	uint64_t bucket_mask;
	uint32_t key_size_shl;
	uint32_t data_size_shl;
	uint32_t key_stack_tos;

	/* Grinder */
	struct grinder grinders[RTE_PORT_IN_BURST_SIZE_MAX];

	/* Tables */
	struct bucket *buckets;
	uint8_t *key_mem;
	uint8_t *data_mem;
	uint32_t *key_stack;

	/* Table memory */
	uint8_t memory[0] __rte_cache_aligned;
};

static int
check_params_create(struct rte_table_hash_lru_params *params)
{
	uint32_t n_buckets_min;

	/* key_size */
	if ((params->key_size == 0) ||
		(!rte_is_power_of_2(params->key_size))) {
		RTE_LOG(ERR, TABLE, "%s: key_size invalid value\n", __func__);
		return -EINVAL;
	}

	/* n_keys */
	if ((params->n_keys == 0) ||
		(!rte_is_power_of_2(params->n_keys))) {
		RTE_LOG(ERR, TABLE, "%s: n_keys invalid value\n", __func__);
		return -EINVAL;
	}

	/* n_buckets */
	n_buckets_min = (params->n_keys + KEYS_PER_BUCKET - 1) / params->n_keys;
	if ((params->n_buckets == 0) ||
		(!rte_is_power_of_2(params->n_keys)) ||
		(params->n_buckets < n_buckets_min)) {
		RTE_LOG(ERR, TABLE, "%s: n_buckets invalid value\n", __func__);
		return -EINVAL;
	}

	/* f_hash */
	if (params->f_hash == NULL) {
		RTE_LOG(ERR, TABLE, "%s: f_hash invalid value\n", __func__);
		return -EINVAL;
	}

	return 0;
}

static void *
rte_table_hash_lru_create(void *params, int socket_id, uint32_t entry_size)
{
	struct rte_table_hash_lru_params *p =
		(struct rte_table_hash_lru_params *) params;
	struct rte_table_hash *t;
	uint32_t total_size, table_meta_sz;
	uint32_t bucket_sz, key_sz, key_stack_sz, data_sz;
	uint32_t bucket_offset, key_offset, key_stack_offset, data_offset;
	uint32_t i;

	/* Check input parameters */
	if ((check_params_create(p) != 0) ||
		(!rte_is_power_of_2(entry_size)) ||
		((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
		(sizeof(struct bucket) != (RTE_CACHE_LINE_SIZE / 2))) {
		return NULL;
	}

	/* Memory allocation */
	table_meta_sz = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_table_hash));
	bucket_sz = RTE_CACHE_LINE_ROUNDUP(p->n_buckets * sizeof(struct bucket));
	key_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * p->key_size);
	key_stack_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * sizeof(uint32_t));
	data_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * entry_size);
	total_size = table_meta_sz + bucket_sz + key_sz + key_stack_sz +
		data_sz;

	t = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE, socket_id);
	if (t == NULL) {
		RTE_LOG(ERR, TABLE,
			"%s: Cannot allocate %u bytes for hash table\n",
			__func__, total_size);
		return NULL;
	}
	RTE_LOG(INFO, TABLE, "%s (%u-byte key): Hash table memory footprint is "
		"%u bytes\n", __func__, p->key_size, total_size);

	/* Memory initialization */
	t->key_size = p->key_size;
	t->entry_size = entry_size;
	t->n_keys = p->n_keys;
	t->n_buckets = p->n_buckets;
	t->f_hash = p->f_hash;
	t->seed = p->seed;
	t->signature_offset = p->signature_offset;
	t->key_offset = p->key_offset;

	/* Internal */
	t->bucket_mask = t->n_buckets - 1;
	t->key_size_shl = __builtin_ctzl(p->key_size);
	t->data_size_shl = __builtin_ctzl(entry_size);

	/* Tables */
	bucket_offset = 0;
	key_offset = bucket_offset + bucket_sz;
	key_stack_offset = key_offset + key_sz;
	data_offset = key_stack_offset + key_stack_sz;

	t->buckets = (struct bucket *) &t->memory[bucket_offset];
	t->key_mem = &t->memory[key_offset];
	t->key_stack = (uint32_t *) &t->memory[key_stack_offset];
	t->data_mem = &t->memory[data_offset];

	/* Key stack */
	for (i = 0; i < t->n_keys; i++)
		t->key_stack[i] = t->n_keys - 1 - i;
	t->key_stack_tos = t->n_keys;

	/* LRU */
	for (i = 0; i < t->n_buckets; i++) {
		struct bucket *bkt = &t->buckets[i];

		lru_init(bkt);
	}

	return t;
}

static int
rte_table_hash_lru_free(void *table)
{
	struct rte_table_hash *t = (struct rte_table_hash *) table;

	/* Check input parameters */
	if (t == NULL)
		return -EINVAL;

	rte_free(t);
	return 0;
}

static int
rte_table_hash_lru_entry_add(void *table, void *key, void *entry,
	int *key_found, void **entry_ptr)
{
	struct rte_table_hash *t = (struct rte_table_hash *) table;
	struct bucket *bkt;
	uint64_t sig;
	uint32_t bkt_index, i;

	sig = t->f_hash(key, t->key_size, t->seed);
	bkt_index = sig & t->bucket_mask;
	bkt = &t->buckets[bkt_index];
	sig = (sig >> 16) | 1LLU;

	/* Key is present in the bucket */
	for (i = 0; i < KEYS_PER_BUCKET; i++) {
		uint64_t bkt_sig = (uint64_t) bkt->sig[i];
		uint32_t bkt_key_index = bkt->key_pos[i];
		uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
			t->key_size_shl];

		if ((sig == bkt_sig) && (memcmp(key, bkt_key, t->key_size)
			== 0)) {
			uint8_t *data = &t->data_mem[bkt_key_index <<
				t->data_size_shl];

			memcpy(data, entry, t->entry_size);
			lru_update(bkt, i);
			*key_found = 1;
			*entry_ptr = (void *) data;
			return 0;
		}
	}

	/* Key is not present in the bucket */
	for (i = 0; i < KEYS_PER_BUCKET; i++) {
		uint64_t bkt_sig = (uint64_t) bkt->sig[i];

		if (bkt_sig == 0) {
			uint32_t bkt_key_index;
			uint8_t *bkt_key, *data;

			/* Allocate new key */
			if (t->key_stack_tos == 0) {
				/* No keys available */
				return -ENOSPC;
			}
			bkt_key_index = t->key_stack[--t->key_stack_tos];

			/* Install new key */
			bkt_key = &t->key_mem[bkt_key_index << t->key_size_shl];
			data = &t->data_mem[bkt_key_index << t->data_size_shl];

			bkt->sig[i] = (uint16_t) sig;
			bkt->key_pos[i] = bkt_key_index;
			memcpy(bkt_key, key, t->key_size);
			memcpy(data, entry, t->entry_size);
			lru_update(bkt, i);

			*key_found = 0;
			*entry_ptr = (void *) data;
			return 0;
		}
	}

	/* Bucket full */
	{
		uint64_t pos = lru_pos(bkt);
		uint32_t bkt_key_index = bkt->key_pos[pos];
		uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
			t->key_size_shl];
		uint8_t *data = &t->data_mem[bkt_key_index << t->data_size_shl];

		bkt->sig[pos] = (uint16_t) sig;
		memcpy(bkt_key, key, t->key_size);
		memcpy(data, entry, t->entry_size);
		lru_update(bkt, pos);

		*key_found = 0;
		*entry_ptr = (void *) data;
		return 0;
	}
}

static int
rte_table_hash_lru_entry_delete(void *table, void *key, int *key_found,
	void *entry)
{
	struct rte_table_hash *t = (struct rte_table_hash *) table;
	struct bucket *bkt;
	uint64_t sig;
	uint32_t bkt_index, i;

	sig = t->f_hash(key, t->key_size, t->seed);
	bkt_index = sig & t->bucket_mask;
	bkt = &t->buckets[bkt_index];
	sig = (sig >> 16) | 1LLU;

	/* Key is present in the bucket */
	for (i = 0; i < KEYS_PER_BUCKET; i++) {
		uint64_t bkt_sig = (uint64_t) bkt->sig[i];
		uint32_t bkt_key_index = bkt->key_pos[i];
		uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
			t->key_size_shl];

		if ((sig == bkt_sig) &&
			(memcmp(key, bkt_key, t->key_size) == 0)) {
			uint8_t *data = &t->data_mem[bkt_key_index <<
				t->data_size_shl];

			bkt->sig[i] = 0;
			t->key_stack[t->key_stack_tos++] = bkt_key_index;
			*key_found = 1;
			memcpy(entry, data, t->entry_size);
			return 0;
		}
	}

	/* Key is not present in the bucket */
	*key_found = 0;
	return 0;
}

static int rte_table_hash_lru_lookup_unoptimized(
	void *table,
	struct rte_mbuf **pkts,
	uint64_t pkts_mask,
	uint64_t *lookup_hit_mask,
	void **entries,
	int dosig)
{
	struct rte_table_hash *t = (struct rte_table_hash *) table;
	uint64_t pkts_mask_out = 0;

	__rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
	RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(t, n_pkts_in);

	for ( ; pkts_mask; ) {
		struct bucket *bkt;
		struct rte_mbuf *pkt;
		uint8_t *key;
		uint64_t pkt_mask, sig;
		uint32_t pkt_index, bkt_index, i;

		pkt_index = __builtin_ctzll(pkts_mask);
		pkt_mask = 1LLU << pkt_index;
		pkts_mask &= ~pkt_mask;

		pkt = pkts[pkt_index];
		key = RTE_MBUF_METADATA_UINT8_PTR(pkt, t->key_offset);
		if (dosig)
			sig = (uint64_t) t->f_hash(key, t->key_size, t->seed);
		else
			sig = RTE_MBUF_METADATA_UINT32(pkt,
				t->signature_offset);

		bkt_index = sig & t->bucket_mask;
		bkt = &t->buckets[bkt_index];
		sig = (sig >> 16) | 1LLU;

		/* Key is present in the bucket */
		for (i = 0; i < KEYS_PER_BUCKET; i++) {
			uint64_t bkt_sig = (uint64_t) bkt->sig[i];
			uint32_t bkt_key_index = bkt->key_pos[i];
			uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
				t->key_size_shl];

			if ((sig == bkt_sig) && (memcmp(key, bkt_key,
				t->key_size) == 0)) {
				uint8_t *data = &t->data_mem[bkt_key_index <<
					t->data_size_shl];

				lru_update(bkt, i);
				pkts_mask_out |= pkt_mask;
				entries[pkt_index] = (void *) data;
				break;
			}
		}
	}

	*lookup_hit_mask = pkts_mask_out;
	RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(t, n_pkts_in - __builtin_popcountll(pkts_mask_out));
	return 0;
}

/***
*
* mask = match bitmask
* match = at least one match
* match_many = more than one match
* match_pos = position of first match
*
* ----------------------------------------
* mask		 match	 match_many	  match_pos
* ----------------------------------------
* 0000		 0		 0			  00
* 0001		 1		 0			  00
* 0010		 1		 0			  01
* 0011		 1		 1			  00
* ----------------------------------------
* 0100		 1		 0			  10
* 0101		 1		 1			  00
* 0110		 1		 1			  01
* 0111		 1		 1			  00
* ----------------------------------------
* 1000		 1		 0			  11
* 1001		 1		 1			  00
* 1010		 1		 1			  01
* 1011		 1		 1			  00
* ----------------------------------------
* 1100		 1		 1			  10
* 1101		 1		 1			  00
* 1110		 1		 1			  01
* 1111		 1		 1			  00
* ----------------------------------------
*
* match = 1111_1111_1111_1110
* match_many = 1111_1110_1110_1000
* match_pos = 0001_0010_0001_0011__0001_0010_0001_0000
*
* match = 0xFFFELLU
* match_many = 0xFEE8LLU
* match_pos = 0x12131210LLU
*
***/

#define LUT_MATCH						0xFFFELLU
#define LUT_MATCH_MANY						0xFEE8LLU
#define LUT_MATCH_POS						0x12131210LLU

#define lookup_cmp_sig(mbuf_sig, bucket, match, match_many, match_pos)\
{								\
	uint64_t bucket_sig[4], mask[4], mask_all;		\
								\
	bucket_sig[0] = bucket->sig[0];				\
	bucket_sig[1] = bucket->sig[1];				\
	bucket_sig[2] = bucket->sig[2];				\
	bucket_sig[3] = bucket->sig[3];				\
								\
	bucket_sig[0] ^= mbuf_sig;				\
	bucket_sig[1] ^= mbuf_sig;				\
	bucket_sig[2] ^= mbuf_sig;				\
	bucket_sig[3] ^= mbuf_sig;				\
								\
	mask[0] = 0;						\
	mask[1] = 0;						\
	mask[2] = 0;						\
	mask[3] = 0;						\
								\
	if (bucket_sig[0] == 0)					\
		mask[0] = 1;					\
	if (bucket_sig[1] == 0)					\
		mask[1] = 2;					\
	if (bucket_sig[2] == 0)					\
		mask[2] = 4;					\
	if (bucket_sig[3] == 0)					\
		mask[3] = 8;					\
								\
	mask_all = (mask[0] | mask[1]) | (mask[2] | mask[3]);	\
								\
	match = (LUT_MATCH >> mask_all) & 1;			\
	match_many = (LUT_MATCH_MANY >> mask_all) & 1;		\
	match_pos = (LUT_MATCH_POS >> (mask_all << 1)) & 3;	\
}

#define lookup_cmp_key(mbuf, key, match_key, f)			\
{								\
	uint64_t *pkt_key = RTE_MBUF_METADATA_UINT64_PTR(mbuf, f->key_offset);\
	uint64_t *bkt_key = (uint64_t *) key;			\
								\
	switch (f->key_size) {					\
	case 8:							\
	{							\
		uint64_t xor = pkt_key[0] ^ bkt_key[0];		\
		match_key = 0;					\
		if (xor == 0)					\
			match_key = 1;				\
	}							\
	break;							\
								\
	case 16:						\
	{							\
		uint64_t xor[2], or;				\
								\
		xor[0] = pkt_key[0] ^ bkt_key[0];		\
		xor[1] = pkt_key[1] ^ bkt_key[1];		\
		or = xor[0] | xor[1];				\
		match_key = 0;					\
		if (or == 0)					\
			match_key = 1;				\
	}							\
	break;							\
								\
	case 32:						\
	{							\
		uint64_t xor[4], or;				\
								\
		xor[0] = pkt_key[0] ^ bkt_key[0];		\
		xor[1] = pkt_key[1] ^ bkt_key[1];		\
		xor[2] = pkt_key[2] ^ bkt_key[2];		\
		xor[3] = pkt_key[3] ^ bkt_key[3];		\
		or = xor[0] | xor[1] | xor[2] | xor[3];		\
		match_key = 0;					\
		if (or == 0)					\
			match_key = 1;				\
	}							\
	break;							\
								\
	case 64:						\
	{							\
		uint64_t xor[8], or;				\
								\
		xor[0] = pkt_key[0] ^ bkt_key[0];		\
		xor[1] = pkt_key[1] ^ bkt_key[1];		\
		xor[2] = pkt_key[2] ^ bkt_key[2];		\
		xor[3] = pkt_key[3] ^ bkt_key[3];		\
		xor[4] = pkt_key[4] ^ bkt_key[4];		\
		xor[5] = pkt_key[5] ^ bkt_key[5];		\
		xor[6] = pkt_key[6] ^ bkt_key[6];		\
		xor[7] = pkt_key[7] ^ bkt_key[7];		\
		or = xor[0] | xor[1] | xor[2] | xor[3] |	\
			xor[4] | xor[5] | xor[6] | xor[7];	\
		match_key = 0;					\
		if (or == 0)					\
			match_key = 1;				\
	}							\
	break;							\
								\
	default:						\
		match_key = 0;					\
		if (memcmp(pkt_key, bkt_key, f->key_size) == 0)	\
			match_key = 1;				\
	}							\
}

#define lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index)\
{								\
	uint64_t pkt00_mask, pkt01_mask;			\
	struct rte_mbuf *mbuf00, *mbuf01;			\
	uint32_t key_offset = t->key_offset;		\
								\
	pkt00_index = __builtin_ctzll(pkts_mask);		\
	pkt00_mask = 1LLU << pkt00_index;			\
	pkts_mask &= ~pkt00_mask;				\
	mbuf00 = pkts[pkt00_index];				\
								\
	pkt01_index = __builtin_ctzll(pkts_mask);		\
	pkt01_mask = 1LLU << pkt01_index;			\
	pkts_mask &= ~pkt01_mask;				\
	mbuf01 = pkts[pkt01_index];				\
								\
	rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
	rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
}

#define lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask, pkt00_index, \
	pkt01_index)						\
{								\
	uint64_t pkt00_mask, pkt01_mask;			\
	struct rte_mbuf *mbuf00, *mbuf01;			\
	uint32_t key_offset = t->key_offset;		\
								\
	pkt00_index = __builtin_ctzll(pkts_mask);		\
	pkt00_mask = 1LLU << pkt00_index;			\
	pkts_mask &= ~pkt00_mask;				\
	mbuf00 = pkts[pkt00_index];				\
								\
	pkt01_index = __builtin_ctzll(pkts_mask);		\
	if (pkts_mask == 0)					\
		pkt01_index = pkt00_index;			\
								\
	pkt01_mask = 1LLU << pkt01_index;			\
	pkts_mask &= ~pkt01_mask;				\
	mbuf01 = pkts[pkt01_index];				\
								\
	rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
	rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
}

#define lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index)	\
{								\
	struct grinder *g10, *g11;				\
	uint64_t sig10, sig11, bkt10_index, bkt11_index;	\
	struct rte_mbuf *mbuf10, *mbuf11;			\
	struct bucket *bkt10, *bkt11, *buckets = t->buckets;	\
	uint64_t bucket_mask = t->bucket_mask;			\
	uint32_t signature_offset = t->signature_offset;	\
								\
	mbuf10 = pkts[pkt10_index];				\
	sig10 = (uint64_t) RTE_MBUF_METADATA_UINT32(mbuf10, signature_offset);\
	bkt10_index = sig10 & bucket_mask;			\
	bkt10 = &buckets[bkt10_index];				\
								\
	mbuf11 = pkts[pkt11_index];				\
	sig11 = (uint64_t) RTE_MBUF_METADATA_UINT32(mbuf11, signature_offset);\
	bkt11_index = sig11 & bucket_mask;			\
	bkt11 = &buckets[bkt11_index];				\
								\
	rte_prefetch0(bkt10);					\
	rte_prefetch0(bkt11);					\
								\
	g10 = &g[pkt10_index];					\
	g10->sig = sig10;					\
	g10->bkt = bkt10;					\
								\
	g11 = &g[pkt11_index];					\
	g11->sig = sig11;					\
	g11->bkt = bkt11;					\
}

#define lookup2_stage1_dosig(t, g, pkts, pkt10_index, pkt11_index)\
{								\
	struct grinder *g10, *g11;				\
	uint64_t sig10, sig11, bkt10_index, bkt11_index;	\
	struct rte_mbuf *mbuf10, *mbuf11;			\
	struct bucket *bkt10, *bkt11, *buckets = t->buckets;	\
	uint8_t *key10, *key11;					\
	uint64_t bucket_mask = t->bucket_mask;			\
	rte_table_hash_op_hash f_hash = t->f_hash;		\
	uint64_t seed = t->seed;				\
	uint32_t key_size = t->key_size;			\
	uint32_t key_offset = t->key_offset;			\
								\
	mbuf10 = pkts[pkt10_index];				\
	key10 = RTE_MBUF_METADATA_UINT8_PTR(mbuf10, key_offset);\
	sig10 = (uint64_t) f_hash(key10, key_size, seed);	\
	bkt10_index = sig10 & bucket_mask;			\
	bkt10 = &buckets[bkt10_index];				\
								\
	mbuf11 = pkts[pkt11_index];				\
	key11 = RTE_MBUF_METADATA_UINT8_PTR(mbuf11, key_offset);\
	sig11 = (uint64_t) f_hash(key11, key_size, seed);	\
	bkt11_index = sig11 & bucket_mask;			\
	bkt11 = &buckets[bkt11_index];				\
								\
	rte_prefetch0(bkt10);					\
	rte_prefetch0(bkt11);					\
								\
	g10 = &g[pkt10_index];					\
	g10->sig = sig10;					\
	g10->bkt = bkt10;					\
								\
	g11 = &g[pkt11_index];					\
	g11->sig = sig11;					\
	g11->bkt = bkt11;					\
}

#define lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many)\
{								\
	struct grinder *g20, *g21;				\
	uint64_t sig20, sig21;					\
	struct bucket *bkt20, *bkt21;				\
	uint8_t *key20, *key21, *key_mem = t->key_mem;		\
	uint64_t match20, match21, match_many20, match_many21;	\
	uint64_t match_pos20, match_pos21;			\
	uint32_t key20_index, key21_index, key_size_shl = t->key_size_shl;\
								\
	g20 = &g[pkt20_index];					\
	sig20 = g20->sig;					\
	bkt20 = g20->bkt;					\
	sig20 = (sig20 >> 16) | 1LLU;				\
	lookup_cmp_sig(sig20, bkt20, match20, match_many20, match_pos20);\
	match20 <<= pkt20_index;				\
	match_many20 <<= pkt20_index;				\
	key20_index = bkt20->key_pos[match_pos20];		\
	key20 = &key_mem[key20_index << key_size_shl];		\
								\
	g21 = &g[pkt21_index];					\
	sig21 = g21->sig;					\
	bkt21 = g21->bkt;					\
	sig21 = (sig21 >> 16) | 1LLU;				\
	lookup_cmp_sig(sig21, bkt21, match21, match_many21, match_pos21);\
	match21 <<= pkt21_index;				\
	match_many21 <<= pkt21_index;				\
	key21_index = bkt21->key_pos[match_pos21];		\
	key21 = &key_mem[key21_index << key_size_shl];		\
								\
	rte_prefetch0(key20);					\
	rte_prefetch0(key21);					\
								\
	pkts_mask_match_many |= match_many20 | match_many21;	\
								\
	g20->match = match20;					\
	g20->match_pos = match_pos20;				\
	g20->key_index = key20_index;				\
								\
	g21->match = match21;					\
	g21->match_pos = match_pos21;				\
	g21->key_index = key21_index;				\
}

#define lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out, \
	entries)						\
{								\
	struct grinder *g30, *g31;				\
	struct rte_mbuf *mbuf30, *mbuf31;			\
	struct bucket *bkt30, *bkt31;				\
	uint8_t *key30, *key31, *key_mem = t->key_mem;		\
	uint8_t *data30, *data31, *data_mem = t->data_mem;	\
	uint64_t match30, match31, match_pos30, match_pos31;	\
	uint64_t match_key30, match_key31, match_keys;		\
	uint32_t key30_index, key31_index;			\
	uint32_t key_size_shl = t->key_size_shl;		\
	uint32_t data_size_shl = t->data_size_shl;		\
								\
	mbuf30 = pkts[pkt30_index];				\
	g30 = &g[pkt30_index];					\
	bkt30 = g30->bkt;					\
	match30 = g30->match;					\
	match_pos30 = g30->match_pos;				\
	key30_index = g30->key_index;				\
	key30 = &key_mem[key30_index << key_size_shl];		\
	lookup_cmp_key(mbuf30, key30, match_key30, t);		\
	match_key30 <<= pkt30_index;				\
	match_key30 &= match30;					\
	data30 = &data_mem[key30_index << data_size_shl];	\
	entries[pkt30_index] = data30;				\
								\
	mbuf31 = pkts[pkt31_index];				\
	g31 = &g[pkt31_index];					\
	bkt31 = g31->bkt;					\
	match31 = g31->match;					\
	match_pos31 = g31->match_pos;				\
	key31_index = g31->key_index;				\
	key31 = &key_mem[key31_index << key_size_shl];		\
	lookup_cmp_key(mbuf31, key31, match_key31, t);		\
	match_key31 <<= pkt31_index;				\
	match_key31 &= match31;					\
	data31 = &data_mem[key31_index << data_size_shl];	\
	entries[pkt31_index] = data31;				\
								\
	rte_prefetch0(data30);					\
	rte_prefetch0(data31);					\
								\
	match_keys = match_key30 | match_key31;			\
	pkts_mask_out |= match_keys;				\
								\
	if (match_key30 == 0)					\
		match_pos30 = 4;				\
	lru_update(bkt30, match_pos30);				\
								\
	if (match_key31 == 0)					\
		match_pos31 = 4;				\
	lru_update(bkt31, match_pos31);				\
}

/***
* The lookup function implements a 4-stage pipeline, with each stage processing
* two different packets. The purpose of pipelined implementation is to hide the
* latency of prefetching the data structures and loosen the data dependency
* between instructions.
*
*   p00  _______   p10  _______   p20  _______   p30  _______
* ----->|       |----->|       |----->|       |----->|       |----->
*       |   0   |      |   1   |      |   2   |      |   3   |
* ----->|_______|----->|_______|----->|_______|----->|_______|----->
*   p01            p11            p21            p31
*
* The naming convention is:
*	  pXY = packet Y of stage X, X = 0 .. 3, Y = 0 .. 1
*
***/
static int rte_table_hash_lru_lookup(
	void *table,
	struct rte_mbuf **pkts,
	uint64_t pkts_mask,
	uint64_t *lookup_hit_mask,
	void **entries)
{
	struct rte_table_hash *t = (struct rte_table_hash *) table;
	struct grinder *g = t->grinders;
	uint64_t pkt00_index, pkt01_index, pkt10_index, pkt11_index;
	uint64_t pkt20_index, pkt21_index, pkt30_index, pkt31_index;
	uint64_t pkts_mask_out = 0, pkts_mask_match_many = 0;
	int status = 0;

	__rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
	RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(t, n_pkts_in);

	/* Cannot run the pipeline with less than 7 packets */
	if (__builtin_popcountll(pkts_mask) < 7)
		return rte_table_hash_lru_lookup_unoptimized(table, pkts,
			pkts_mask, lookup_hit_mask, entries, 0);

	/* Pipeline stage 0 */
	lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);

	/* Pipeline feed */
	pkt10_index = pkt00_index;
	pkt11_index = pkt01_index;

	/* Pipeline stage 0 */
	lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);

	/* Pipeline stage 1 */
	lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);

	/* Pipeline feed */
	pkt20_index = pkt10_index;
	pkt21_index = pkt11_index;
	pkt10_index = pkt00_index;
	pkt11_index = pkt01_index;

	/* Pipeline stage 0 */
	lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);

	/* Pipeline stage 1 */
	lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);

	/* Pipeline stage 2 */
	lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);

	/*
	* Pipeline run
	*
	*/
	for ( ; pkts_mask; ) {
		/* Pipeline feed */
		pkt30_index = pkt20_index;
		pkt31_index = pkt21_index;
		pkt20_index = pkt10_index;
		pkt21_index = pkt11_index;
		pkt10_index = pkt00_index;
		pkt11_index = pkt01_index;

		/* Pipeline stage 0 */
		lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask,
			pkt00_index, pkt01_index);

		/* Pipeline stage 1 */
		lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);

		/* Pipeline stage 2 */
		lookup2_stage2(t, g, pkt20_index, pkt21_index,
			pkts_mask_match_many);

		/* Pipeline stage 3 */
		lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index,
			pkts_mask_out, entries);
	}

	/* Pipeline feed */
	pkt30_index = pkt20_index;
	pkt31_index = pkt21_index;
	pkt20_index = pkt10_index;
	pkt21_index = pkt11_index;
	pkt10_index = pkt00_index;
	pkt11_index = pkt01_index;

	/* Pipeline stage 1 */
	lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);

	/* Pipeline stage 2 */
	lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);

	/* Pipeline stage 3 */
	lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
		entries);

	/* Pipeline feed */
	pkt30_index = pkt20_index;
	pkt31_index = pkt21_index;
	pkt20_index = pkt10_index;
	pkt21_index = pkt11_index;

	/* Pipeline stage 2 */
	lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);

	/* Pipeline stage 3 */
	lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
		entries);

	/* Pipeline feed */
	pkt30_index = pkt20_index;
	pkt31_index = pkt21_index;

	/* Pipeline stage 3 */
	lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
		entries);

	/* Slow path */
	pkts_mask_match_many &= ~pkts_mask_out;
	if (pkts_mask_match_many) {
		uint64_t pkts_mask_out_slow = 0;

		status = rte_table_hash_lru_lookup_unoptimized(table, pkts,
			pkts_mask_match_many, &pkts_mask_out_slow, entries, 0);
		pkts_mask_out |= pkts_mask_out_slow;
	}

	*lookup_hit_mask = pkts_mask_out;
	RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(t, n_pkts_in - __builtin_popcountll(pkts_mask_out));
	return status;
}

static int rte_table_hash_lru_lookup_dosig(
	void *table,
	struct rte_mbuf **pkts,
	uint64_t pkts_mask,
	uint64_t *lookup_hit_mask,
	void **entries)
{
	struct rte_table_hash *t = (struct rte_table_hash *) table;
	struct grinder *g = t->grinders;
	uint64_t pkt00_index, pkt01_index, pkt10_index, pkt11_index;
	uint64_t pkt20_index, pkt21_index, pkt30_index, pkt31_index;
	uint64_t pkts_mask_out = 0, pkts_mask_match_many = 0;
	int status = 0;

	__rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
	RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(t, n_pkts_in);

	/* Cannot run the pipeline with less than 7 packets */
	if (__builtin_popcountll(pkts_mask) < 7)
		return rte_table_hash_lru_lookup_unoptimized(table, pkts,
			pkts_mask, lookup_hit_mask, entries, 1);

	/* Pipeline stage 0 */
	lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);

	/* Pipeline feed */
	pkt10_index = pkt00_index;
	pkt11_index = pkt01_index;

	/* Pipeline stage 0 */
	lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);

	/* Pipeline stage 1 */
	lookup2_stage1_dosig(t, g, pkts, pkt10_index, pkt11_index);

	/* Pipeline feed */
	pkt20_index = pkt10_index;
	pkt21_index = pkt11_index;
	pkt10_index = pkt00_index;
	pkt11_index = pkt01_index;

	/* Pipeline stage 0 */
	lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);

	/* Pipeline stage 1 */
	lookup2_stage1_dosig(t, g, pkts, pkt10_index, pkt11_index);

	/* Pipeline stage 2 */
	lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);

	/*
	* Pipeline run
	*
	*/
	for ( ; pkts_mask; ) {
		/* Pipeline feed */
		pkt30_index = pkt20_index;
		pkt31_index = pkt21_index;
		pkt20_index = pkt10_index;
		pkt21_index = pkt11_index;
		pkt10_index = pkt00_index;
		pkt11_index = pkt01_index;

		/* Pipeline stage 0 */
		lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask,
			pkt00_index, pkt01_index);

		/* Pipeline stage 1 */
		lookup2_stage1_dosig(t, g, pkts, pkt10_index, pkt11_index);

		/* Pipeline stage 2 */
		lookup2_stage2(t, g, pkt20_index, pkt21_index,
			pkts_mask_match_many);

		/* Pipeline stage 3 */
		lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index,
			pkts_mask_out, entries);
	}

	/* Pipeline feed */
	pkt30_index = pkt20_index;
	pkt31_index = pkt21_index;
	pkt20_index = pkt10_index;
	pkt21_index = pkt11_index;
	pkt10_index = pkt00_index;
	pkt11_index = pkt01_index;

	/* Pipeline stage 1 */
	lookup2_stage1_dosig(t, g, pkts, pkt10_index, pkt11_index);

	/* Pipeline stage 2 */
	lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);

	/* Pipeline stage 3 */
	lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
		entries);

	/* Pipeline feed */
	pkt30_index = pkt20_index;
	pkt31_index = pkt21_index;
	pkt20_index = pkt10_index;
	pkt21_index = pkt11_index;

	/* Pipeline stage 2 */
	lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);

	/* Pipeline stage 3 */
	lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
		entries);

	/* Pipeline feed */
	pkt30_index = pkt20_index;
	pkt31_index = pkt21_index;

	/* Pipeline stage 3 */
	lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
		entries);

	/* Slow path */
	pkts_mask_match_many &= ~pkts_mask_out;
	if (pkts_mask_match_many) {
		uint64_t pkts_mask_out_slow = 0;

		status = rte_table_hash_lru_lookup_unoptimized(table, pkts,
			pkts_mask_match_many, &pkts_mask_out_slow, entries, 1);
		pkts_mask_out |= pkts_mask_out_slow;
	}

	*lookup_hit_mask = pkts_mask_out;
	RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(t, n_pkts_in - __builtin_popcountll(pkts_mask_out));
	return status;
}

static int
rte_table_hash_lru_stats_read(void *table, struct rte_table_stats *stats, int clear)
{
	struct rte_table_hash *t = (struct rte_table_hash *) table;

	if (stats != NULL)
		memcpy(stats, &t->stats, sizeof(t->stats));

	if (clear)
		memset(&t->stats, 0, sizeof(t->stats));

	return 0;
}

struct rte_table_ops rte_table_hash_lru_ops = {
	.f_create = rte_table_hash_lru_create,
	.f_free = rte_table_hash_lru_free,
	.f_add = rte_table_hash_lru_entry_add,
	.f_delete = rte_table_hash_lru_entry_delete,
	.f_add_bulk = NULL,
	.f_delete_bulk = NULL,
	.f_lookup = rte_table_hash_lru_lookup,
	.f_stats = rte_table_hash_lru_stats_read,
};

struct rte_table_ops rte_table_hash_lru_dosig_ops = {
	.f_create = rte_table_hash_lru_create,
	.f_free = rte_table_hash_lru_free,
	.f_add = rte_table_hash_lru_entry_add,
	.f_delete = rte_table_hash_lru_entry_delete,
	.f_add_bulk = NULL,
	.f_delete_bulk = NULL,
	.f_lookup = rte_table_hash_lru_lookup_dosig,
	.f_stats = rte_table_hash_lru_stats_read,
};