1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2021 Intel Corporation
9 #include <rte_random.h>
10 #include <rte_memcpy.h>
11 #include <rte_errno.h>
12 #include <rte_eal_memconfig.h>
14 #include <rte_malloc.h>
16 #define THASH_NAME_LEN 64
17 #define TOEPLITZ_HASH_LEN 32
19 #define RETA_SZ_IN_RANGE(reta_sz) ((reta_sz >= RTE_THASH_RETA_SZ_MIN) &&\
20 (reta_sz <= RTE_THASH_RETA_SZ_MAX))
22 TAILQ_HEAD(rte_thash_list, rte_tailq_entry);
23 static struct rte_tailq_elem rte_thash_tailq = {
26 EAL_REGISTER_TAILQ(rte_thash_tailq)
29 * Table of some irreducible polinomials over GF(2).
30 * For lfsr they are represented in BE bit order, and
32 * For example, poly x^5 + x^2 + 1 will be represented
33 * as (101001b & 11111b) = 01001b = 0x9
35 static const uint32_t irreducible_poly_table[][4] = {
36 {0, 0, 0, 0}, /** < degree 0 */
37 {1, 1, 1, 1}, /** < degree 1 */
38 {0x3, 0x3, 0x3, 0x3}, /** < degree 2 and so on... */
41 {0x9, 0x1b, 0xf, 0x5},
42 {0x21, 0x33, 0x1b, 0x2d},
43 {0x41, 0x11, 0x71, 0x9},
44 {0x71, 0xa9, 0xf5, 0x8d},
45 {0x21, 0xd1, 0x69, 0x1d9},
46 {0x81, 0x2c1, 0x3b1, 0x185},
47 {0x201, 0x541, 0x341, 0x461},
48 {0x941, 0x609, 0xe19, 0x45d},
49 {0x1601, 0x1f51, 0x1171, 0x359},
50 {0x2141, 0x2111, 0x2db1, 0x2109},
51 {0x4001, 0x801, 0x101, 0x7301},
52 {0x7781, 0xa011, 0x4211, 0x86d9},
58 /**< polynomial associated with the lfsr */
60 /**< polynomial to generate the sequence in reverse direction */
62 /**< current state of the lfsr */
64 /**< current state of the lfsr for reverse direction */
65 uint32_t deg; /**< polynomial degree*/
66 uint32_t bits_cnt; /**< number of bits generated by lfsr*/
69 struct rte_thash_subtuple_helper {
70 char name[THASH_NAME_LEN]; /** < Name of subtuple configuration */
71 LIST_ENTRY(rte_thash_subtuple_helper) next;
72 struct thash_lfsr *lfsr;
73 uint32_t offset; /** < Offset of the m-sequence */
74 uint32_t len; /** < Length of the m-sequence */
75 uint32_t tuple_offset; /** < Offset in bits of the subtuple */
76 uint32_t tuple_len; /** < Length in bits of the subtuple */
77 uint32_t lsb_msk; /** < (1 << reta_sz_log) - 1 */
78 __extension__ uint32_t compl_table[0] __rte_cache_aligned;
79 /** < Complementary table */
82 struct rte_thash_ctx {
83 char name[THASH_NAME_LEN];
84 LIST_HEAD(, rte_thash_subtuple_helper) head;
85 uint32_t key_len; /** < Length of the NIC RSS hash key */
86 uint32_t reta_sz_log; /** < size of the RSS ReTa in bits */
87 uint32_t subtuples_nb; /** < number of subtuples */
90 /**< matrices used with rte_thash_gfni implementation */
95 rte_thash_gfni_supported(void)
97 #ifdef RTE_THASH_GFNI_DEFINED
98 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_GFNI) &&
99 (rte_vect_get_max_simd_bitwidth() >=
108 rte_thash_complete_matrix(uint64_t *matrixes, const uint8_t *rss_key, int size)
111 uint8_t *m = (uint8_t *)matrixes;
112 uint8_t left_part, right_part;
114 for (i = 0; i < size; i++) {
115 for (j = 0; j < 8; j++) {
116 left_part = rss_key[i] << j;
117 right_part = (uint16_t)(rss_key[(i + 1) % size]) >>
119 m[i * 8 + j] = left_part|right_part;
124 static inline uint32_t
125 get_bit_lfsr(struct thash_lfsr *lfsr)
130 * masking the TAP bits defined by the polynomial and
133 bit = __builtin_popcount(lfsr->state & lfsr->poly) & 0x1;
134 ret = lfsr->state & 0x1;
135 lfsr->state = ((lfsr->state >> 1) | (bit << (lfsr->deg - 1))) &
136 ((1 << lfsr->deg) - 1);
142 static inline uint32_t
143 get_rev_bit_lfsr(struct thash_lfsr *lfsr)
147 bit = __builtin_popcount(lfsr->rev_state & lfsr->rev_poly) & 0x1;
148 ret = lfsr->rev_state & (1 << (lfsr->deg - 1));
149 lfsr->rev_state = ((lfsr->rev_state << 1) | bit) &
150 ((1 << lfsr->deg) - 1);
156 static inline uint32_t
157 thash_get_rand_poly(uint32_t poly_degree)
159 return irreducible_poly_table[poly_degree][rte_rand() %
160 RTE_DIM(irreducible_poly_table[poly_degree])];
163 static struct thash_lfsr *
164 alloc_lfsr(struct rte_thash_ctx *ctx)
166 struct thash_lfsr *lfsr;
172 lfsr = rte_zmalloc(NULL, sizeof(struct thash_lfsr), 0);
176 lfsr->deg = ctx->reta_sz_log;
177 lfsr->poly = thash_get_rand_poly(lfsr->deg);
179 lfsr->state = rte_rand() & ((1 << lfsr->deg) - 1);
180 } while (lfsr->state == 0);
181 /* init reverse order polynomial */
182 lfsr->rev_poly = (lfsr->poly >> 1) | (1 << (lfsr->deg - 1));
183 /* init proper rev_state*/
184 lfsr->rev_state = lfsr->state;
185 for (i = 0; i <= lfsr->deg; i++)
186 get_rev_bit_lfsr(lfsr);
188 /* clear bits_cnt after rev_state was inited */
196 attach_lfsr(struct rte_thash_subtuple_helper *h, struct thash_lfsr *lfsr)
203 free_lfsr(struct thash_lfsr *lfsr)
206 if (lfsr->ref_cnt == 0)
210 struct rte_thash_ctx *
211 rte_thash_init_ctx(const char *name, uint32_t key_len, uint32_t reta_sz,
212 uint8_t *key, uint32_t flags)
214 struct rte_thash_ctx *ctx;
215 struct rte_tailq_entry *te;
216 struct rte_thash_list *thash_list;
219 if ((name == NULL) || (key_len == 0) || !RETA_SZ_IN_RANGE(reta_sz)) {
224 thash_list = RTE_TAILQ_CAST(rte_thash_tailq.head, rte_thash_list);
226 rte_mcfg_tailq_write_lock();
228 /* guarantee there's no existing */
229 TAILQ_FOREACH(te, thash_list, next) {
230 ctx = (struct rte_thash_ctx *)te->data;
231 if (strncmp(name, ctx->name, sizeof(ctx->name)) == 0)
240 /* allocate tailq entry */
241 te = rte_zmalloc("THASH_TAILQ_ENTRY", sizeof(*te), 0);
244 "Can not allocate tailq entry for thash context %s\n",
250 ctx = rte_zmalloc(NULL, sizeof(struct rte_thash_ctx) + key_len, 0);
252 RTE_LOG(ERR, HASH, "thash ctx %s memory allocation failed\n",
258 rte_strlcpy(ctx->name, name, sizeof(ctx->name));
259 ctx->key_len = key_len;
260 ctx->reta_sz_log = reta_sz;
261 LIST_INIT(&ctx->head);
265 rte_memcpy(ctx->hash_key, key, key_len);
267 for (i = 0; i < key_len; i++)
268 ctx->hash_key[i] = rte_rand();
271 if (rte_thash_gfni_supported()) {
272 ctx->matrices = rte_zmalloc(NULL, key_len * sizeof(uint64_t),
273 RTE_CACHE_LINE_SIZE);
274 if (ctx->matrices == NULL) {
275 RTE_LOG(ERR, HASH, "Cannot allocate matrices\n");
280 rte_thash_complete_matrix(ctx->matrices, ctx->hash_key,
284 te->data = (void *)ctx;
285 TAILQ_INSERT_TAIL(thash_list, te, next);
287 rte_mcfg_tailq_write_unlock();
296 rte_mcfg_tailq_write_unlock();
300 struct rte_thash_ctx *
301 rte_thash_find_existing(const char *name)
303 struct rte_thash_ctx *ctx;
304 struct rte_tailq_entry *te;
305 struct rte_thash_list *thash_list;
307 thash_list = RTE_TAILQ_CAST(rte_thash_tailq.head, rte_thash_list);
309 rte_mcfg_tailq_read_lock();
310 TAILQ_FOREACH(te, thash_list, next) {
311 ctx = (struct rte_thash_ctx *)te->data;
312 if (strncmp(name, ctx->name, sizeof(ctx->name)) == 0)
316 rte_mcfg_tailq_read_unlock();
327 rte_thash_free_ctx(struct rte_thash_ctx *ctx)
329 struct rte_tailq_entry *te;
330 struct rte_thash_list *thash_list;
331 struct rte_thash_subtuple_helper *ent, *tmp;
336 thash_list = RTE_TAILQ_CAST(rte_thash_tailq.head, rte_thash_list);
337 rte_mcfg_tailq_write_lock();
338 TAILQ_FOREACH(te, thash_list, next) {
339 if (te->data == (void *)ctx)
344 TAILQ_REMOVE(thash_list, te, next);
346 rte_mcfg_tailq_write_unlock();
347 ent = LIST_FIRST(&(ctx->head));
349 free_lfsr(ent->lfsr);
351 ent = LIST_NEXT(ent, next);
352 LIST_REMOVE(tmp, next);
361 set_bit(uint8_t *ptr, uint32_t bit, uint32_t pos)
363 uint32_t byte_idx = pos / CHAR_BIT;
364 /* index of the bit int byte, indexing starts from MSB */
365 uint32_t bit_idx = (CHAR_BIT - 1) - (pos & (CHAR_BIT - 1));
369 tmp &= ~(1 << bit_idx);
370 tmp |= bit << bit_idx;
375 * writes m-sequence to the hash_key for range [start, end]
376 * (i.e. including start and end positions)
379 generate_subkey(struct rte_thash_ctx *ctx, struct thash_lfsr *lfsr,
380 uint32_t start, uint32_t end)
383 uint32_t req_bits = (start < end) ? (end - start) : (start - end);
384 req_bits++; /* due to including end */
386 /* check if lfsr overflow period of the m-sequence */
387 if (((lfsr->bits_cnt + req_bits) > (1ULL << lfsr->deg) - 1) &&
388 ((ctx->flags & RTE_THASH_IGNORE_PERIOD_OVERFLOW) !=
389 RTE_THASH_IGNORE_PERIOD_OVERFLOW)) {
391 "Can't generate m-sequence due to period overflow\n");
396 /* original direction (from left to right)*/
397 for (i = start; i <= end; i++)
398 set_bit(ctx->hash_key, get_bit_lfsr(lfsr), i);
401 /* reverse direction (from right to left) */
402 for (i = end; i >= start; i--)
403 set_bit(ctx->hash_key, get_rev_bit_lfsr(lfsr), i);
406 if (ctx->matrices != NULL)
407 rte_thash_complete_matrix(ctx->matrices, ctx->hash_key,
413 static inline uint32_t
414 get_subvalue(struct rte_thash_ctx *ctx, uint32_t offset)
418 tmp = (uint32_t *)(&ctx->hash_key[offset >> 3]);
419 val = rte_be_to_cpu_32(*tmp);
420 val >>= (TOEPLITZ_HASH_LEN - ((offset & (CHAR_BIT - 1)) +
423 return val & ((1 << ctx->reta_sz_log) - 1);
427 generate_complement_table(struct rte_thash_ctx *ctx,
428 struct rte_thash_subtuple_helper *h)
434 start = h->offset + h->len - (2 * ctx->reta_sz_log - 1);
436 for (i = 1; i < (1 << ctx->reta_sz_log); i++) {
438 for (j = i; j; j &= (j - 1)) {
440 val ^= get_subvalue(ctx, start - k +
441 ctx->reta_sz_log - 1);
443 h->compl_table[val] = i;
448 insert_before(struct rte_thash_ctx *ctx,
449 struct rte_thash_subtuple_helper *ent,
450 struct rte_thash_subtuple_helper *cur_ent,
451 struct rte_thash_subtuple_helper *next_ent,
452 uint32_t start, uint32_t end, uint32_t range_end)
456 if (end < cur_ent->offset) {
457 ent->lfsr = alloc_lfsr(ctx);
458 if (ent->lfsr == NULL) {
462 /* generate nonoverlapping range [start, end) */
463 ret = generate_subkey(ctx, ent->lfsr, start, end - 1);
465 free_lfsr(ent->lfsr);
469 } else if ((next_ent != NULL) && (end > next_ent->offset)) {
471 "Can't add helper %s due to conflict with existing"
472 " helper %s\n", ent->name, next_ent->name);
476 attach_lfsr(ent, cur_ent->lfsr);
479 * generate partially overlapping range
480 * [start, cur_ent->start) in reverse order
482 ret = generate_subkey(ctx, ent->lfsr, cur_ent->offset - 1, start);
484 free_lfsr(ent->lfsr);
489 if (end > range_end) {
491 * generate partially overlapping range
494 ret = generate_subkey(ctx, ent->lfsr, range_end, end - 1);
496 free_lfsr(ent->lfsr);
502 LIST_INSERT_BEFORE(cur_ent, ent, next);
503 generate_complement_table(ctx, ent);
509 insert_after(struct rte_thash_ctx *ctx,
510 struct rte_thash_subtuple_helper *ent,
511 struct rte_thash_subtuple_helper *cur_ent,
512 struct rte_thash_subtuple_helper *next_ent,
513 struct rte_thash_subtuple_helper *prev_ent,
514 uint32_t end, uint32_t range_end)
518 if ((next_ent != NULL) && (end > next_ent->offset)) {
520 "Can't add helper %s due to conflict with existing"
521 " helper %s\n", ent->name, next_ent->name);
526 attach_lfsr(ent, cur_ent->lfsr);
527 if (end > range_end) {
529 * generate partially overlapping range
532 ret = generate_subkey(ctx, ent->lfsr, range_end, end - 1);
534 free_lfsr(ent->lfsr);
540 LIST_INSERT_AFTER(prev_ent, ent, next);
541 generate_complement_table(ctx, ent);
548 rte_thash_add_helper(struct rte_thash_ctx *ctx, const char *name, uint32_t len,
551 struct rte_thash_subtuple_helper *ent, *cur_ent, *prev_ent, *next_ent;
555 if ((ctx == NULL) || (name == NULL) || (len < ctx->reta_sz_log) ||
556 ((offset + len + TOEPLITZ_HASH_LEN - 1) >
557 ctx->key_len * CHAR_BIT))
560 /* Check for existing name*/
561 LIST_FOREACH(cur_ent, &ctx->head, next) {
562 if (strncmp(name, cur_ent->name, sizeof(cur_ent->name)) == 0)
566 end = offset + len + TOEPLITZ_HASH_LEN - 1;
567 start = ((ctx->flags & RTE_THASH_MINIMAL_SEQ) ==
568 RTE_THASH_MINIMAL_SEQ) ? (end - (2 * ctx->reta_sz_log - 1)) :
571 ent = rte_zmalloc(NULL, sizeof(struct rte_thash_subtuple_helper) +
572 sizeof(uint32_t) * (1 << ctx->reta_sz_log),
573 RTE_CACHE_LINE_SIZE);
577 rte_strlcpy(ent->name, name, sizeof(ent->name));
579 ent->len = end - start;
580 ent->tuple_offset = offset;
581 ent->tuple_len = len;
582 ent->lsb_msk = (1 << ctx->reta_sz_log) - 1;
584 cur_ent = LIST_FIRST(&ctx->head);
586 uint32_t range_end = cur_ent->offset + cur_ent->len;
587 next_ent = LIST_NEXT(cur_ent, next);
589 /* Iterate through overlapping ranges */
590 while ((next_ent != NULL) && (next_ent->offset < range_end)) {
591 range_end = RTE_MAX(next_ent->offset + next_ent->len,
593 if (start > next_ent->offset)
596 next_ent = LIST_NEXT(next_ent, next);
599 if (start < cur_ent->offset)
600 return insert_before(ctx, ent, cur_ent, next_ent,
601 start, end, range_end);
602 else if (start < range_end)
603 return insert_after(ctx, ent, cur_ent, next_ent,
604 prev_ent, end, range_end);
610 ent->lfsr = alloc_lfsr(ctx);
611 if (ent->lfsr == NULL) {
616 /* generate nonoverlapping range [start, end) */
617 ret = generate_subkey(ctx, ent->lfsr, start, end - 1);
619 free_lfsr(ent->lfsr);
623 if (LIST_EMPTY(&ctx->head)) {
624 LIST_INSERT_HEAD(&ctx->head, ent, next);
626 LIST_FOREACH(next_ent, &ctx->head, next)
629 LIST_INSERT_AFTER(prev_ent, ent, next);
631 generate_complement_table(ctx, ent);
637 struct rte_thash_subtuple_helper *
638 rte_thash_get_helper(struct rte_thash_ctx *ctx, const char *name)
640 struct rte_thash_subtuple_helper *ent;
642 if ((ctx == NULL) || (name == NULL))
645 LIST_FOREACH(ent, &ctx->head, next) {
646 if (strncmp(name, ent->name, sizeof(ent->name)) == 0)
654 rte_thash_get_complement(struct rte_thash_subtuple_helper *h,
655 uint32_t hash, uint32_t desired_hash)
657 return h->compl_table[(hash ^ desired_hash) & h->lsb_msk];
661 rte_thash_get_key(struct rte_thash_ctx *ctx)
663 return ctx->hash_key;
667 rte_thash_get_gfni_matrices(struct rte_thash_ctx *ctx)
669 return ctx->matrices;
672 static inline uint8_t
673 read_unaligned_byte(uint8_t *ptr, unsigned int len, unsigned int offset)
677 ret = ptr[offset / CHAR_BIT];
678 if (offset % CHAR_BIT) {
679 ret <<= (offset % CHAR_BIT);
680 ret |= ptr[(offset / CHAR_BIT) + 1] >>
681 (CHAR_BIT - (offset % CHAR_BIT));
684 return ret >> (CHAR_BIT - len);
687 static inline uint32_t
688 read_unaligned_bits(uint8_t *ptr, int len, int offset)
692 len = RTE_MAX(len, 0);
693 len = RTE_MIN(len, (int)(sizeof(uint32_t) * CHAR_BIT));
698 ret |= read_unaligned_byte(ptr, RTE_MIN(len, CHAR_BIT),
707 /* returns mask for len bits with given offset inside byte */
708 static inline uint8_t
709 get_bits_mask(unsigned int len, unsigned int offset)
711 unsigned int last_bit;
714 /* last bit within byte */
715 last_bit = RTE_MIN((unsigned int)CHAR_BIT, offset + len);
717 return ((1 << (CHAR_BIT - offset)) - 1) ^
718 ((1 << (CHAR_BIT - last_bit)) - 1);
722 write_unaligned_byte(uint8_t *ptr, unsigned int len,
723 unsigned int offset, uint8_t val)
727 tmp = ptr[offset / CHAR_BIT];
728 tmp &= ~get_bits_mask(len, offset);
729 tmp |= ((val << (CHAR_BIT - len)) >> (offset % CHAR_BIT));
730 ptr[offset / CHAR_BIT] = tmp;
731 if (((offset + len) / CHAR_BIT) != (offset / CHAR_BIT)) {
732 int rest_len = (offset + len) % CHAR_BIT;
733 tmp = ptr[(offset + len) / CHAR_BIT];
734 tmp &= ~get_bits_mask(rest_len, 0);
735 tmp |= val << (CHAR_BIT - rest_len);
736 ptr[(offset + len) / CHAR_BIT] = tmp;
741 write_unaligned_bits(uint8_t *ptr, int len, int offset, uint32_t val)
744 unsigned int part_len;
746 len = RTE_MAX(len, 0);
747 len = RTE_MIN(len, (int)(sizeof(uint32_t) * CHAR_BIT));
750 part_len = RTE_MIN(CHAR_BIT, len);
751 tmp = (uint8_t)val & ((1 << part_len) - 1);
752 write_unaligned_byte(ptr, part_len,
753 offset + len - part_len, tmp);
760 rte_thash_adjust_tuple(struct rte_thash_ctx *ctx,
761 struct rte_thash_subtuple_helper *h,
762 uint8_t *tuple, unsigned int tuple_len,
763 uint32_t desired_value, unsigned int attempts,
764 rte_thash_check_tuple_t fn, void *userdata)
766 uint32_t tmp_tuple[tuple_len / sizeof(uint32_t)];
767 unsigned int i, j, ret = 0;
768 uint32_t hash, adj_bits;
769 const uint8_t *hash_key;
774 if ((ctx == NULL) || (h == NULL) || (tuple == NULL) ||
775 (tuple_len % sizeof(uint32_t) != 0) || (attempts <= 0))
778 hash_key = rte_thash_get_key(ctx);
780 attempts = RTE_MIN(attempts, 1U << (h->tuple_len - ctx->reta_sz_log));
782 for (i = 0; i < attempts; i++) {
783 if (ctx->matrices != NULL)
784 hash = rte_thash_gfni(ctx->matrices, tuple, tuple_len);
786 for (j = 0; j < (tuple_len / 4); j++)
789 *(uint32_t *)&tuple[j * 4]);
791 hash = rte_softrss(tmp_tuple, tuple_len / 4, hash_key);
794 adj_bits = rte_thash_get_complement(h, hash, desired_value);
797 * Hint: LSB of adj_bits corresponds to
798 * offset + len bit of the subtuple
800 offset = h->tuple_offset + h->tuple_len - ctx->reta_sz_log;
801 tmp = read_unaligned_bits(tuple, ctx->reta_sz_log, offset);
803 write_unaligned_bits(tuple, ctx->reta_sz_log, offset, tmp);
806 ret = (fn(userdata, tuple)) ? 0 : -EEXIST;
809 else if (i < (attempts - 1)) {
810 /* increment subtuple part by 1 */
811 tmp_len = RTE_MIN(sizeof(uint32_t) * CHAR_BIT,
812 h->tuple_len - ctx->reta_sz_log);
814 tmp = read_unaligned_bits(tuple, tmp_len,
817 tmp &= (1 << tmp_len) - 1;
818 write_unaligned_bits(tuple, tmp_len, offset,