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