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>
13 #include <rte_eal_memconfig.h>
15 #include <rte_malloc.h>
17 #define THASH_NAME_LEN 64
18 #define TOEPLITZ_HASH_LEN 32
20 #define RETA_SZ_IN_RANGE(reta_sz) ((reta_sz >= RTE_THASH_RETA_SZ_MIN) &&\
21 (reta_sz <= RTE_THASH_RETA_SZ_MAX))
23 TAILQ_HEAD(rte_thash_list, rte_tailq_entry);
24 static struct rte_tailq_elem rte_thash_tailq = {
27 EAL_REGISTER_TAILQ(rte_thash_tailq)
30 * Table of some irreducible polinomials over GF(2).
31 * For lfsr they are reperesented in BE bit order, and
33 * For example, poly x^5 + x^2 + 1 will be represented
34 * as (101001b & 11111b) = 01001b = 0x9
36 static const uint32_t irreducible_poly_table[][4] = {
37 {0, 0, 0, 0}, /** < degree 0 */
38 {1, 1, 1, 1}, /** < degree 1 */
39 {0x3, 0x3, 0x3, 0x3}, /** < degree 2 and so on... */
42 {0x9, 0x1b, 0xf, 0x5},
43 {0x21, 0x33, 0x1b, 0x2d},
44 {0x41, 0x11, 0x71, 0x9},
45 {0x71, 0xa9, 0xf5, 0x8d},
46 {0x21, 0xd1, 0x69, 0x1d9},
47 {0x81, 0x2c1, 0x3b1, 0x185},
48 {0x201, 0x541, 0x341, 0x461},
49 {0x941, 0x609, 0xe19, 0x45d},
50 {0x1601, 0x1f51, 0x1171, 0x359},
51 {0x2141, 0x2111, 0x2db1, 0x2109},
52 {0x4001, 0x801, 0x101, 0x7301},
53 {0x7781, 0xa011, 0x4211, 0x86d9},
59 /**< polynomial associated with the lfsr */
61 /**< polynomial to generate the sequence in reverse direction */
63 /**< current state of the lfsr */
65 /**< current state of the lfsr for reverse direction */
66 uint32_t deg; /**< polynomial degree*/
67 uint32_t bits_cnt; /**< number of bits generated by lfsr*/
70 struct rte_thash_subtuple_helper {
71 char name[THASH_NAME_LEN]; /** < Name of subtuple configuration */
72 LIST_ENTRY(rte_thash_subtuple_helper) next;
73 struct thash_lfsr *lfsr;
74 uint32_t offset; /** < Offset of the m-sequence */
75 uint32_t len; /** < Length of the m-sequence */
76 uint32_t tuple_offset; /** < Offset in bits of the subtuple */
77 uint32_t tuple_len; /** < Length in bits of the subtuple */
78 uint32_t lsb_msk; /** < (1 << reta_sz_log) - 1 */
79 __extension__ uint32_t compl_table[0] __rte_cache_aligned;
80 /** < Complementary table */
83 struct rte_thash_ctx {
84 char name[THASH_NAME_LEN];
85 LIST_HEAD(, rte_thash_subtuple_helper) head;
86 uint32_t key_len; /** < Length of the NIC RSS hash key */
87 uint32_t reta_sz_log; /** < size of the RSS ReTa in bits */
88 uint32_t subtuples_nb; /** < number of subtuples */
94 rte_thash_gfni_supported(void)
96 #ifdef RTE_THASH_GFNI_DEFINED
97 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_GFNI) &&
98 (rte_vect_get_max_simd_bitwidth() >=
107 rte_thash_complete_matrix(uint64_t *matrixes, const uint8_t *rss_key, int size)
110 uint8_t *m = (uint8_t *)matrixes;
111 uint8_t left_part, right_part;
113 for (i = 0; i < size; i++) {
114 for (j = 0; j < 8; j++) {
115 left_part = rss_key[i] << j;
116 right_part = (uint16_t)(rss_key[(i + 1) % size]) >>
118 m[i * 8 + j] = left_part|right_part;
123 static inline uint32_t
124 get_bit_lfsr(struct thash_lfsr *lfsr)
129 * masking the TAP bits defined by the polynomial and
132 bit = __builtin_popcount(lfsr->state & lfsr->poly) & 0x1;
133 ret = lfsr->state & 0x1;
134 lfsr->state = ((lfsr->state >> 1) | (bit << (lfsr->deg - 1))) &
135 ((1 << lfsr->deg) - 1);
141 static inline uint32_t
142 get_rev_bit_lfsr(struct thash_lfsr *lfsr)
146 bit = __builtin_popcount(lfsr->rev_state & lfsr->rev_poly) & 0x1;
147 ret = lfsr->rev_state & (1 << (lfsr->deg - 1));
148 lfsr->rev_state = ((lfsr->rev_state << 1) | bit) &
149 ((1 << lfsr->deg) - 1);
155 static inline uint32_t
156 thash_get_rand_poly(uint32_t poly_degree)
158 return irreducible_poly_table[poly_degree][rte_rand() %
159 RTE_DIM(irreducible_poly_table[poly_degree])];
162 static struct thash_lfsr *
163 alloc_lfsr(struct rte_thash_ctx *ctx)
165 struct thash_lfsr *lfsr;
171 lfsr = rte_zmalloc(NULL, sizeof(struct thash_lfsr), 0);
175 lfsr->deg = ctx->reta_sz_log;
176 lfsr->poly = thash_get_rand_poly(lfsr->deg);
178 lfsr->state = rte_rand() & ((1 << lfsr->deg) - 1);
179 } while (lfsr->state == 0);
180 /* init reverse order polynomial */
181 lfsr->rev_poly = (lfsr->poly >> 1) | (1 << (lfsr->deg - 1));
182 /* init proper rev_state*/
183 lfsr->rev_state = lfsr->state;
184 for (i = 0; i <= lfsr->deg; i++)
185 get_rev_bit_lfsr(lfsr);
187 /* clear bits_cnt after rev_state was inited */
195 attach_lfsr(struct rte_thash_subtuple_helper *h, struct thash_lfsr *lfsr)
202 free_lfsr(struct thash_lfsr *lfsr)
205 if (lfsr->ref_cnt == 0)
209 struct rte_thash_ctx *
210 rte_thash_init_ctx(const char *name, uint32_t key_len, uint32_t reta_sz,
211 uint8_t *key, uint32_t flags)
213 struct rte_thash_ctx *ctx;
214 struct rte_tailq_entry *te;
215 struct rte_thash_list *thash_list;
218 if ((name == NULL) || (key_len == 0) || !RETA_SZ_IN_RANGE(reta_sz)) {
223 thash_list = RTE_TAILQ_CAST(rte_thash_tailq.head, rte_thash_list);
225 rte_mcfg_tailq_write_lock();
227 /* guarantee there's no existing */
228 TAILQ_FOREACH(te, thash_list, next) {
229 ctx = (struct rte_thash_ctx *)te->data;
230 if (strncmp(name, ctx->name, sizeof(ctx->name)) == 0)
239 /* allocate tailq entry */
240 te = rte_zmalloc("THASH_TAILQ_ENTRY", sizeof(*te), 0);
243 "Can not allocate tailq entry for thash context %s\n",
249 ctx = rte_zmalloc(NULL, sizeof(struct rte_thash_ctx) + key_len, 0);
251 RTE_LOG(ERR, HASH, "thash ctx %s memory allocation failed\n",
257 rte_strlcpy(ctx->name, name, sizeof(ctx->name));
258 ctx->key_len = key_len;
259 ctx->reta_sz_log = reta_sz;
260 LIST_INIT(&ctx->head);
264 rte_memcpy(ctx->hash_key, key, key_len);
266 for (i = 0; i < key_len; i++)
267 ctx->hash_key[i] = rte_rand();
270 te->data = (void *)ctx;
271 TAILQ_INSERT_TAIL(thash_list, te, next);
273 rte_mcfg_tailq_write_unlock();
279 rte_mcfg_tailq_write_unlock();
283 struct rte_thash_ctx *
284 rte_thash_find_existing(const char *name)
286 struct rte_thash_ctx *ctx;
287 struct rte_tailq_entry *te;
288 struct rte_thash_list *thash_list;
290 thash_list = RTE_TAILQ_CAST(rte_thash_tailq.head, rte_thash_list);
292 rte_mcfg_tailq_read_lock();
293 TAILQ_FOREACH(te, thash_list, next) {
294 ctx = (struct rte_thash_ctx *)te->data;
295 if (strncmp(name, ctx->name, sizeof(ctx->name)) == 0)
299 rte_mcfg_tailq_read_unlock();
310 rte_thash_free_ctx(struct rte_thash_ctx *ctx)
312 struct rte_tailq_entry *te;
313 struct rte_thash_list *thash_list;
314 struct rte_thash_subtuple_helper *ent, *tmp;
319 thash_list = RTE_TAILQ_CAST(rte_thash_tailq.head, rte_thash_list);
320 rte_mcfg_tailq_write_lock();
321 TAILQ_FOREACH(te, thash_list, next) {
322 if (te->data == (void *)ctx)
327 TAILQ_REMOVE(thash_list, te, next);
329 rte_mcfg_tailq_write_unlock();
330 ent = LIST_FIRST(&(ctx->head));
332 free_lfsr(ent->lfsr);
334 ent = LIST_NEXT(ent, next);
335 LIST_REMOVE(tmp, next);
344 set_bit(uint8_t *ptr, uint32_t bit, uint32_t pos)
346 uint32_t byte_idx = pos / CHAR_BIT;
347 /* index of the bit int byte, indexing starts from MSB */
348 uint32_t bit_idx = (CHAR_BIT - 1) - (pos & (CHAR_BIT - 1));
352 tmp &= ~(1 << bit_idx);
353 tmp |= bit << bit_idx;
358 * writes m-sequence to the hash_key for range [start, end]
359 * (i.e. including start and end positions)
362 generate_subkey(struct rte_thash_ctx *ctx, struct thash_lfsr *lfsr,
363 uint32_t start, uint32_t end)
366 uint32_t req_bits = (start < end) ? (end - start) : (start - end);
367 req_bits++; /* due to including end */
369 /* check if lfsr overflow period of the m-sequence */
370 if (((lfsr->bits_cnt + req_bits) > (1ULL << lfsr->deg) - 1) &&
371 ((ctx->flags & RTE_THASH_IGNORE_PERIOD_OVERFLOW) !=
372 RTE_THASH_IGNORE_PERIOD_OVERFLOW)) {
374 "Can't generate m-sequence due to period overflow\n");
379 /* original direction (from left to right)*/
380 for (i = start; i <= end; i++)
381 set_bit(ctx->hash_key, get_bit_lfsr(lfsr), i);
384 /* reverse direction (from right to left) */
385 for (i = end; i >= start; i--)
386 set_bit(ctx->hash_key, get_rev_bit_lfsr(lfsr), i);
392 static inline uint32_t
393 get_subvalue(struct rte_thash_ctx *ctx, uint32_t offset)
397 tmp = (uint32_t *)(&ctx->hash_key[offset >> 3]);
398 val = rte_be_to_cpu_32(*tmp);
399 val >>= (TOEPLITZ_HASH_LEN - ((offset & (CHAR_BIT - 1)) +
402 return val & ((1 << ctx->reta_sz_log) - 1);
406 generate_complement_table(struct rte_thash_ctx *ctx,
407 struct rte_thash_subtuple_helper *h)
413 start = h->offset + h->len - (2 * ctx->reta_sz_log - 1);
415 for (i = 1; i < (1 << ctx->reta_sz_log); i++) {
417 for (j = i; j; j &= (j - 1)) {
419 val ^= get_subvalue(ctx, start - k +
420 ctx->reta_sz_log - 1);
422 h->compl_table[val] = i;
427 insert_before(struct rte_thash_ctx *ctx,
428 struct rte_thash_subtuple_helper *ent,
429 struct rte_thash_subtuple_helper *cur_ent,
430 struct rte_thash_subtuple_helper *next_ent,
431 uint32_t start, uint32_t end, uint32_t range_end)
435 if (end < cur_ent->offset) {
436 ent->lfsr = alloc_lfsr(ctx);
437 if (ent->lfsr == NULL) {
441 /* generate nonoverlapping range [start, end) */
442 ret = generate_subkey(ctx, ent->lfsr, start, end - 1);
444 free_lfsr(ent->lfsr);
448 } else if ((next_ent != NULL) && (end > next_ent->offset)) {
451 "Can't add helper %s due to conflict with existing"
452 " helper %s\n", ent->name, next_ent->name);
455 attach_lfsr(ent, cur_ent->lfsr);
458 * generate partially overlapping range
459 * [start, cur_ent->start) in reverse order
461 ret = generate_subkey(ctx, ent->lfsr, cur_ent->offset - 1, start);
463 free_lfsr(ent->lfsr);
468 if (end > range_end) {
470 * generate partially overlapping range
473 ret = generate_subkey(ctx, ent->lfsr, range_end, end - 1);
475 free_lfsr(ent->lfsr);
481 LIST_INSERT_BEFORE(cur_ent, ent, next);
482 generate_complement_table(ctx, ent);
488 insert_after(struct rte_thash_ctx *ctx,
489 struct rte_thash_subtuple_helper *ent,
490 struct rte_thash_subtuple_helper *cur_ent,
491 struct rte_thash_subtuple_helper *next_ent,
492 struct rte_thash_subtuple_helper *prev_ent,
493 uint32_t end, uint32_t range_end)
497 if ((next_ent != NULL) && (end > next_ent->offset)) {
500 "Can't add helper %s due to conflict with existing"
501 " helper %s\n", ent->name, next_ent->name);
505 attach_lfsr(ent, cur_ent->lfsr);
506 if (end > range_end) {
508 * generate partially overlapping range
511 ret = generate_subkey(ctx, ent->lfsr, range_end, end - 1);
513 free_lfsr(ent->lfsr);
519 LIST_INSERT_AFTER(prev_ent, ent, next);
520 generate_complement_table(ctx, ent);
527 rte_thash_add_helper(struct rte_thash_ctx *ctx, const char *name, uint32_t len,
530 struct rte_thash_subtuple_helper *ent, *cur_ent, *prev_ent, *next_ent;
534 if ((ctx == NULL) || (name == NULL) || (len < ctx->reta_sz_log) ||
535 ((offset + len + TOEPLITZ_HASH_LEN - 1) >
536 ctx->key_len * CHAR_BIT))
539 /* Check for existing name*/
540 LIST_FOREACH(cur_ent, &ctx->head, next) {
541 if (strncmp(name, cur_ent->name, sizeof(cur_ent->name)) == 0)
545 end = offset + len + TOEPLITZ_HASH_LEN - 1;
546 start = ((ctx->flags & RTE_THASH_MINIMAL_SEQ) ==
547 RTE_THASH_MINIMAL_SEQ) ? (end - (2 * ctx->reta_sz_log - 1)) :
550 ent = rte_zmalloc(NULL, sizeof(struct rte_thash_subtuple_helper) +
551 sizeof(uint32_t) * (1 << ctx->reta_sz_log),
552 RTE_CACHE_LINE_SIZE);
556 rte_strlcpy(ent->name, name, sizeof(ent->name));
558 ent->len = end - start;
559 ent->tuple_offset = offset;
560 ent->tuple_len = len;
561 ent->lsb_msk = (1 << ctx->reta_sz_log) - 1;
563 cur_ent = LIST_FIRST(&ctx->head);
565 uint32_t range_end = cur_ent->offset + cur_ent->len;
566 next_ent = LIST_NEXT(cur_ent, next);
568 /* Iterate through overlapping ranges */
569 while ((next_ent != NULL) && (next_ent->offset < range_end)) {
570 range_end = RTE_MAX(next_ent->offset + next_ent->len,
572 if (start > next_ent->offset)
575 next_ent = LIST_NEXT(next_ent, next);
578 if (start < cur_ent->offset)
579 return insert_before(ctx, ent, cur_ent, next_ent,
580 start, end, range_end);
581 else if (start < range_end)
582 return insert_after(ctx, ent, cur_ent, next_ent,
583 prev_ent, end, range_end);
589 ent->lfsr = alloc_lfsr(ctx);
590 if (ent->lfsr == NULL) {
595 /* generate nonoverlapping range [start, end) */
596 ret = generate_subkey(ctx, ent->lfsr, start, end - 1);
598 free_lfsr(ent->lfsr);
602 if (LIST_EMPTY(&ctx->head)) {
603 LIST_INSERT_HEAD(&ctx->head, ent, next);
605 LIST_FOREACH(next_ent, &ctx->head, next)
608 LIST_INSERT_AFTER(prev_ent, ent, next);
610 generate_complement_table(ctx, ent);
616 struct rte_thash_subtuple_helper *
617 rte_thash_get_helper(struct rte_thash_ctx *ctx, const char *name)
619 struct rte_thash_subtuple_helper *ent;
621 if ((ctx == NULL) || (name == NULL))
624 LIST_FOREACH(ent, &ctx->head, next) {
625 if (strncmp(name, ent->name, sizeof(ent->name)) == 0)
633 rte_thash_get_complement(struct rte_thash_subtuple_helper *h,
634 uint32_t hash, uint32_t desired_hash)
636 return h->compl_table[(hash ^ desired_hash) & h->lsb_msk];
640 rte_thash_get_key(struct rte_thash_ctx *ctx)
642 return ctx->hash_key;
645 static inline uint8_t
646 read_unaligned_byte(uint8_t *ptr, unsigned int len, unsigned int offset)
650 ret = ptr[offset / CHAR_BIT];
651 if (offset % CHAR_BIT) {
652 ret <<= (offset % CHAR_BIT);
653 ret |= ptr[(offset / CHAR_BIT) + 1] >>
654 (CHAR_BIT - (offset % CHAR_BIT));
657 return ret >> (CHAR_BIT - len);
660 static inline uint32_t
661 read_unaligned_bits(uint8_t *ptr, int len, int offset)
665 len = RTE_MAX(len, 0);
666 len = RTE_MIN(len, (int)(sizeof(uint32_t) * CHAR_BIT));
671 ret |= read_unaligned_byte(ptr, RTE_MIN(len, CHAR_BIT),
680 /* returns mask for len bits with given offset inside byte */
681 static inline uint8_t
682 get_bits_mask(unsigned int len, unsigned int offset)
684 unsigned int last_bit;
687 /* last bit within byte */
688 last_bit = RTE_MIN((unsigned int)CHAR_BIT, offset + len);
690 return ((1 << (CHAR_BIT - offset)) - 1) ^
691 ((1 << (CHAR_BIT - last_bit)) - 1);
695 write_unaligned_byte(uint8_t *ptr, unsigned int len,
696 unsigned int offset, uint8_t val)
700 tmp = ptr[offset / CHAR_BIT];
701 tmp &= ~get_bits_mask(len, offset);
702 tmp |= ((val << (CHAR_BIT - len)) >> (offset % CHAR_BIT));
703 ptr[offset / CHAR_BIT] = tmp;
704 if (((offset + len) / CHAR_BIT) != (offset / CHAR_BIT)) {
705 int rest_len = (offset + len) % CHAR_BIT;
706 tmp = ptr[(offset + len) / CHAR_BIT];
707 tmp &= ~get_bits_mask(rest_len, 0);
708 tmp |= val << (CHAR_BIT - rest_len);
709 ptr[(offset + len) / CHAR_BIT] = tmp;
714 write_unaligned_bits(uint8_t *ptr, int len, int offset, uint32_t val)
717 unsigned int part_len;
719 len = RTE_MAX(len, 0);
720 len = RTE_MIN(len, (int)(sizeof(uint32_t) * CHAR_BIT));
723 part_len = RTE_MIN(CHAR_BIT, len);
724 tmp = (uint8_t)val & ((1 << part_len) - 1);
725 write_unaligned_byte(ptr, part_len,
726 offset + len - part_len, tmp);
733 rte_thash_adjust_tuple(struct rte_thash_ctx *ctx,
734 struct rte_thash_subtuple_helper *h,
735 uint8_t *tuple, unsigned int tuple_len,
736 uint32_t desired_value, unsigned int attempts,
737 rte_thash_check_tuple_t fn, void *userdata)
739 uint32_t tmp_tuple[tuple_len / sizeof(uint32_t)];
740 unsigned int i, j, ret = 0;
741 uint32_t hash, adj_bits;
742 const uint8_t *hash_key;
747 if ((ctx == NULL) || (h == NULL) || (tuple == NULL) ||
748 (tuple_len % sizeof(uint32_t) != 0) || (attempts <= 0))
751 hash_key = rte_thash_get_key(ctx);
753 attempts = RTE_MIN(attempts, 1U << (h->tuple_len - ctx->reta_sz_log));
755 for (i = 0; i < attempts; i++) {
756 for (j = 0; j < (tuple_len / 4); j++)
758 rte_be_to_cpu_32(*(uint32_t *)&tuple[j * 4]);
760 hash = rte_softrss(tmp_tuple, tuple_len / 4, hash_key);
761 adj_bits = rte_thash_get_complement(h, hash, desired_value);
764 * Hint: LSB of adj_bits corresponds to
765 * offset + len bit of the subtuple
767 offset = h->tuple_offset + h->tuple_len - ctx->reta_sz_log;
768 tmp = read_unaligned_bits(tuple, ctx->reta_sz_log, offset);
770 write_unaligned_bits(tuple, ctx->reta_sz_log, offset, tmp);
773 ret = (fn(userdata, tuple)) ? 0 : -EEXIST;
776 else if (i < (attempts - 1)) {
777 /* increment subtuple part by 1 */
778 tmp_len = RTE_MIN(sizeof(uint32_t) * CHAR_BIT,
779 h->tuple_len - ctx->reta_sz_log);
781 tmp = read_unaligned_bits(tuple, tmp_len,
784 tmp &= (1 << tmp_len) - 1;
785 write_unaligned_bits(tuple, tmp_len, offset,