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
|
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2010-2017 Intel Corporation
*/
#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 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 */
uint64_t *key_mask;
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
keycmp(void *a, void *b, void *b_mask, uint32_t n_bytes)
{
uint64_t *a64 = a, *b64 = b, *b_mask64 = b_mask;
uint32_t i;
for (i = 0; i < n_bytes / sizeof(uint64_t); i++)
if (a64[i] != (b64[i] & b_mask64[i]))
return 1;
return 0;
}
static void
keycpy(void *dst, void *src, void *src_mask, uint32_t n_bytes)
{
uint64_t *dst64 = dst, *src64 = src, *src_mask64 = src_mask;
uint32_t i;
for (i = 0; i < n_bytes / sizeof(uint64_t); i++)
dst64[i] = src64[i] & src_mask64[i];
}
static int
check_params_create(struct rte_table_hash_params *params)
{
/* name */
if (params->name == NULL) {
RTE_LOG(ERR, TABLE, "%s: name invalid value\n", __func__);
return -EINVAL;
}
/* key_size */
if ((params->key_size < sizeof(uint64_t)) ||
(!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_LOG(ERR, TABLE, "%s: n_keys invalid value\n", __func__);
return -EINVAL;
}
/* n_buckets */
if ((params->n_buckets == 0) ||
(!rte_is_power_of_2(params->n_buckets))) {
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_params *p = params;
struct rte_table_hash *t;
uint64_t table_meta_sz, key_mask_sz, bucket_sz, key_sz, key_stack_sz;
uint64_t data_sz, total_size;
uint64_t key_mask_offset, bucket_offset, key_offset, key_stack_offset;
uint64_t data_offset;
uint32_t n_buckets, 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;
}
/*
* Table dimensioning
*
* Objective: Pick the number of buckets (n_buckets) so that there a chance
* to store n_keys keys in the table.
*
* Note: Since the buckets do not get extended, it is not possible to
* guarantee that n_keys keys can be stored in the table at any time. In the
* worst case scenario when all the n_keys fall into the same bucket, only
* a maximum of KEYS_PER_BUCKET keys will be stored in the table. This case
* defeats the purpose of the hash table. It indicates unsuitable f_hash or
* n_keys to n_buckets ratio.
*
* MIN(n_buckets) = (n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET
*/
n_buckets = rte_align32pow2(
(p->n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET);
n_buckets = RTE_MAX(n_buckets, p->n_buckets);
/* Memory allocation */
table_meta_sz = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_table_hash));
key_mask_sz = RTE_CACHE_LINE_ROUNDUP(p->key_size);
bucket_sz = RTE_CACHE_LINE_ROUNDUP(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 + key_mask_sz + bucket_sz + key_sz +
key_stack_sz + data_sz;
if (total_size > SIZE_MAX) {
RTE_LOG(ERR, TABLE,
"%s: Cannot allocate %" PRIu64 " bytes for hash "
"table %s\n",
__func__, total_size, p->name);
return NULL;
}
t = rte_zmalloc_socket(p->name,
(size_t)total_size,
RTE_CACHE_LINE_SIZE,
socket_id);
if (t == NULL) {
RTE_LOG(ERR, TABLE,
"%s: Cannot allocate %" PRIu64 " bytes for hash "
"table %s\n",
__func__, total_size, p->name);
return NULL;
}
RTE_LOG(INFO, TABLE, "%s (%u-byte key): Hash table %s memory footprint"
" is %" PRIu64 " bytes\n",
__func__, p->key_size, p->name, total_size);
/* Memory initialization */
t->key_size = p->key_size;
t->entry_size = entry_size;
t->n_keys = p->n_keys;
t->n_buckets = n_buckets;
t->f_hash = p->f_hash;
t->seed = p->seed;
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 */
key_mask_offset = 0;
bucket_offset = key_mask_offset + key_mask_sz;
key_offset = bucket_offset + bucket_sz;
key_stack_offset = key_offset + key_sz;
data_offset = key_stack_offset + key_stack_sz;
t->key_mask = (uint64_t *) &t->memory[key_mask_offset];
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 mask */
if (p->key_mask == NULL)
memset(t->key_mask, 0xFF, p->key_size);
else
memcpy(t->key_mask, p->key_mask, p->key_size);
/* 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 = 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 = table;
struct bucket *bkt;
uint64_t sig;
uint32_t bkt_index, i;
sig = t->f_hash(key, t->key_mask, 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) && (keycmp(bkt_key, key, t->key_mask,
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;
keycpy(bkt_key, key, t->key_mask, 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;
keycpy(bkt_key, key, t->key_mask, 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 = table;
struct bucket *bkt;
uint64_t sig;
uint32_t bkt_index, i;
sig = t->f_hash(key, t->key_mask, 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) &&
(keycmp(bkt_key, key, t->key_mask, 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;
if (entry)
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)
{
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);
sig = (uint64_t) t->f_hash(key, t->key_mask, 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) && (keycmp(bkt_key, key, t->key_mask,
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; \
uint64_t *key_mask = f->key_mask; \
\
switch (f->key_size) { \
case 8: \
{ \
uint64_t xor = (pkt_key[0] & key_mask[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] & key_mask[0]) ^ bkt_key[0]; \
xor[1] = (pkt_key[1] & key_mask[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] & key_mask[0]) ^ bkt_key[0]; \
xor[1] = (pkt_key[1] & key_mask[1]) ^ bkt_key[1]; \
xor[2] = (pkt_key[2] & key_mask[2]) ^ bkt_key[2]; \
xor[3] = (pkt_key[3] & key_mask[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] & key_mask[0]) ^ bkt_key[0]; \
xor[1] = (pkt_key[1] & key_mask[1]) ^ bkt_key[1]; \
xor[2] = (pkt_key[2] & key_mask[2]) ^ bkt_key[2]; \
xor[3] = (pkt_key[3] & key_mask[3]) ^ bkt_key[3]; \
xor[4] = (pkt_key[4] & key_mask[4]) ^ bkt_key[4]; \
xor[5] = (pkt_key[5] & key_mask[5]) ^ bkt_key[5]; \
xor[6] = (pkt_key[6] & key_mask[6]) ^ bkt_key[6]; \
xor[7] = (pkt_key[7] & key_mask[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 (keycmp(bkt_key, pkt_key, key_mask, 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; \
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, t->key_mask, 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, t->key_mask, 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);
/* 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);
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 = 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,
};
|