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 */
93 static inline uint32_t
94 get_bit_lfsr(struct thash_lfsr *lfsr)
99 * masking the TAP bits defined by the polynomial and
102 bit = __builtin_popcount(lfsr->state & lfsr->poly) & 0x1;
103 ret = lfsr->state & 0x1;
104 lfsr->state = ((lfsr->state >> 1) | (bit << (lfsr->deg - 1))) &
105 ((1 << lfsr->deg) - 1);
111 static inline uint32_t
112 get_rev_bit_lfsr(struct thash_lfsr *lfsr)
116 bit = __builtin_popcount(lfsr->rev_state & lfsr->rev_poly) & 0x1;
117 ret = lfsr->rev_state & (1 << (lfsr->deg - 1));
118 lfsr->rev_state = ((lfsr->rev_state << 1) | bit) &
119 ((1 << lfsr->deg) - 1);
125 static inline uint32_t
126 thash_get_rand_poly(uint32_t poly_degree)
128 return irreducible_poly_table[poly_degree][rte_rand() %
129 RTE_DIM(irreducible_poly_table[poly_degree])];
132 static struct thash_lfsr *
133 alloc_lfsr(struct rte_thash_ctx *ctx)
135 struct thash_lfsr *lfsr;
141 lfsr = rte_zmalloc(NULL, sizeof(struct thash_lfsr), 0);
145 lfsr->deg = ctx->reta_sz_log;
146 lfsr->poly = thash_get_rand_poly(lfsr->deg);
148 lfsr->state = rte_rand() & ((1 << lfsr->deg) - 1);
149 } while (lfsr->state == 0);
150 /* init reverse order polynomial */
151 lfsr->rev_poly = (lfsr->poly >> 1) | (1 << (lfsr->deg - 1));
152 /* init proper rev_state*/
153 lfsr->rev_state = lfsr->state;
154 for (i = 0; i <= lfsr->deg; i++)
155 get_rev_bit_lfsr(lfsr);
157 /* clear bits_cnt after rev_state was inited */
165 attach_lfsr(struct rte_thash_subtuple_helper *h, struct thash_lfsr *lfsr)
172 free_lfsr(struct thash_lfsr *lfsr)
175 if (lfsr->ref_cnt == 0)
179 struct rte_thash_ctx *
180 rte_thash_init_ctx(const char *name, uint32_t key_len, uint32_t reta_sz,
181 uint8_t *key, uint32_t flags)
183 struct rte_thash_ctx *ctx;
184 struct rte_tailq_entry *te;
185 struct rte_thash_list *thash_list;
188 if ((name == NULL) || (key_len == 0) || !RETA_SZ_IN_RANGE(reta_sz)) {
193 thash_list = RTE_TAILQ_CAST(rte_thash_tailq.head, rte_thash_list);
195 rte_mcfg_tailq_write_lock();
197 /* guarantee there's no existing */
198 TAILQ_FOREACH(te, thash_list, next) {
199 ctx = (struct rte_thash_ctx *)te->data;
200 if (strncmp(name, ctx->name, sizeof(ctx->name)) == 0)
209 /* allocate tailq entry */
210 te = rte_zmalloc("THASH_TAILQ_ENTRY", sizeof(*te), 0);
213 "Can not allocate tailq entry for thash context %s\n",
219 ctx = rte_zmalloc(NULL, sizeof(struct rte_thash_ctx) + key_len, 0);
221 RTE_LOG(ERR, HASH, "thash ctx %s memory allocation failed\n",
227 rte_strlcpy(ctx->name, name, sizeof(ctx->name));
228 ctx->key_len = key_len;
229 ctx->reta_sz_log = reta_sz;
230 LIST_INIT(&ctx->head);
234 rte_memcpy(ctx->hash_key, key, key_len);
236 for (i = 0; i < key_len; i++)
237 ctx->hash_key[i] = rte_rand();
240 te->data = (void *)ctx;
241 TAILQ_INSERT_TAIL(thash_list, te, next);
243 rte_mcfg_tailq_write_unlock();
249 rte_mcfg_tailq_write_unlock();
253 struct rte_thash_ctx *
254 rte_thash_find_existing(const char *name)
256 struct rte_thash_ctx *ctx;
257 struct rte_tailq_entry *te;
258 struct rte_thash_list *thash_list;
260 thash_list = RTE_TAILQ_CAST(rte_thash_tailq.head, rte_thash_list);
262 rte_mcfg_tailq_read_lock();
263 TAILQ_FOREACH(te, thash_list, next) {
264 ctx = (struct rte_thash_ctx *)te->data;
265 if (strncmp(name, ctx->name, sizeof(ctx->name)) == 0)
269 rte_mcfg_tailq_read_unlock();
280 rte_thash_free_ctx(struct rte_thash_ctx *ctx)
282 struct rte_tailq_entry *te;
283 struct rte_thash_list *thash_list;
284 struct rte_thash_subtuple_helper *ent, *tmp;
289 thash_list = RTE_TAILQ_CAST(rte_thash_tailq.head, rte_thash_list);
290 rte_mcfg_tailq_write_lock();
291 TAILQ_FOREACH(te, thash_list, next) {
292 if (te->data == (void *)ctx)
297 TAILQ_REMOVE(thash_list, te, next);
299 rte_mcfg_tailq_write_unlock();
300 ent = LIST_FIRST(&(ctx->head));
302 free_lfsr(ent->lfsr);
304 ent = LIST_NEXT(ent, next);
305 LIST_REMOVE(tmp, next);
314 set_bit(uint8_t *ptr, uint32_t bit, uint32_t pos)
316 uint32_t byte_idx = pos / CHAR_BIT;
317 /* index of the bit int byte, indexing starts from MSB */
318 uint32_t bit_idx = (CHAR_BIT - 1) - (pos & (CHAR_BIT - 1));
322 tmp &= ~(1 << bit_idx);
323 tmp |= bit << bit_idx;
328 * writes m-sequence to the hash_key for range [start, end]
329 * (i.e. including start and end positions)
332 generate_subkey(struct rte_thash_ctx *ctx, struct thash_lfsr *lfsr,
333 uint32_t start, uint32_t end)
336 uint32_t req_bits = (start < end) ? (end - start) : (start - end);
337 req_bits++; /* due to including end */
339 /* check if lfsr overflow period of the m-sequence */
340 if (((lfsr->bits_cnt + req_bits) > (1ULL << lfsr->deg) - 1) &&
341 ((ctx->flags & RTE_THASH_IGNORE_PERIOD_OVERFLOW) !=
342 RTE_THASH_IGNORE_PERIOD_OVERFLOW)) {
344 "Can't generate m-sequence due to period overflow\n");
349 /* original direction (from left to right)*/
350 for (i = start; i <= end; i++)
351 set_bit(ctx->hash_key, get_bit_lfsr(lfsr), i);
354 /* reverse direction (from right to left) */
355 for (i = end; i >= start; i--)
356 set_bit(ctx->hash_key, get_rev_bit_lfsr(lfsr), i);
362 static inline uint32_t
363 get_subvalue(struct rte_thash_ctx *ctx, uint32_t offset)
367 tmp = (uint32_t *)(&ctx->hash_key[offset >> 3]);
368 val = rte_be_to_cpu_32(*tmp);
369 val >>= (TOEPLITZ_HASH_LEN - ((offset & (CHAR_BIT - 1)) +
372 return val & ((1 << ctx->reta_sz_log) - 1);
376 generate_complement_table(struct rte_thash_ctx *ctx,
377 struct rte_thash_subtuple_helper *h)
383 start = h->offset + h->len - (2 * ctx->reta_sz_log - 1);
385 for (i = 1; i < (1 << ctx->reta_sz_log); i++) {
387 for (j = i; j; j &= (j - 1)) {
389 val ^= get_subvalue(ctx, start - k +
390 ctx->reta_sz_log - 1);
392 h->compl_table[val] = i;
397 insert_before(struct rte_thash_ctx *ctx,
398 struct rte_thash_subtuple_helper *ent,
399 struct rte_thash_subtuple_helper *cur_ent,
400 struct rte_thash_subtuple_helper *next_ent,
401 uint32_t start, uint32_t end, uint32_t range_end)
405 if (end < cur_ent->offset) {
406 ent->lfsr = alloc_lfsr(ctx);
407 if (ent->lfsr == NULL) {
411 /* generate nonoverlapping range [start, end) */
412 ret = generate_subkey(ctx, ent->lfsr, start, end - 1);
414 free_lfsr(ent->lfsr);
418 } else if ((next_ent != NULL) && (end > next_ent->offset)) {
421 "Can't add helper %s due to conflict with existing"
422 " helper %s\n", ent->name, next_ent->name);
425 attach_lfsr(ent, cur_ent->lfsr);
428 * generate partially overlapping range
429 * [start, cur_ent->start) in reverse order
431 ret = generate_subkey(ctx, ent->lfsr, cur_ent->offset - 1, start);
433 free_lfsr(ent->lfsr);
438 if (end > range_end) {
440 * generate partially overlapping range
443 ret = generate_subkey(ctx, ent->lfsr, range_end, end - 1);
445 free_lfsr(ent->lfsr);
451 LIST_INSERT_BEFORE(cur_ent, ent, next);
452 generate_complement_table(ctx, ent);
458 insert_after(struct rte_thash_ctx *ctx,
459 struct rte_thash_subtuple_helper *ent,
460 struct rte_thash_subtuple_helper *cur_ent,
461 struct rte_thash_subtuple_helper *next_ent,
462 struct rte_thash_subtuple_helper *prev_ent,
463 uint32_t end, uint32_t range_end)
467 if ((next_ent != NULL) && (end > next_ent->offset)) {
470 "Can't add helper %s due to conflict with existing"
471 " helper %s\n", ent->name, next_ent->name);
475 attach_lfsr(ent, cur_ent->lfsr);
476 if (end > range_end) {
478 * generate partially overlapping range
481 ret = generate_subkey(ctx, ent->lfsr, range_end, end - 1);
483 free_lfsr(ent->lfsr);
489 LIST_INSERT_AFTER(prev_ent, ent, next);
490 generate_complement_table(ctx, ent);
497 rte_thash_add_helper(struct rte_thash_ctx *ctx, const char *name, uint32_t len,
500 struct rte_thash_subtuple_helper *ent, *cur_ent, *prev_ent, *next_ent;
504 if ((ctx == NULL) || (name == NULL) || (len < ctx->reta_sz_log) ||
505 ((offset + len + TOEPLITZ_HASH_LEN - 1) >
506 ctx->key_len * CHAR_BIT))
509 /* Check for existing name*/
510 LIST_FOREACH(cur_ent, &ctx->head, next) {
511 if (strncmp(name, cur_ent->name, sizeof(cur_ent->name)) == 0)
515 end = offset + len + TOEPLITZ_HASH_LEN - 1;
516 start = ((ctx->flags & RTE_THASH_MINIMAL_SEQ) ==
517 RTE_THASH_MINIMAL_SEQ) ? (end - (2 * ctx->reta_sz_log - 1)) :
520 ent = rte_zmalloc(NULL, sizeof(struct rte_thash_subtuple_helper) +
521 sizeof(uint32_t) * (1 << ctx->reta_sz_log),
522 RTE_CACHE_LINE_SIZE);
526 rte_strlcpy(ent->name, name, sizeof(ent->name));
528 ent->len = end - start;
529 ent->tuple_offset = offset;
530 ent->tuple_len = len;
531 ent->lsb_msk = (1 << ctx->reta_sz_log) - 1;
533 cur_ent = LIST_FIRST(&ctx->head);
535 uint32_t range_end = cur_ent->offset + cur_ent->len;
536 next_ent = LIST_NEXT(cur_ent, next);
538 /* Iterate through overlapping ranges */
539 while ((next_ent != NULL) && (next_ent->offset < range_end)) {
540 range_end = RTE_MAX(next_ent->offset + next_ent->len,
542 if (start > next_ent->offset)
545 next_ent = LIST_NEXT(next_ent, next);
548 if (start < cur_ent->offset)
549 return insert_before(ctx, ent, cur_ent, next_ent,
550 start, end, range_end);
551 else if (start < range_end)
552 return insert_after(ctx, ent, cur_ent, next_ent,
553 prev_ent, end, range_end);
559 ent->lfsr = alloc_lfsr(ctx);
560 if (ent->lfsr == NULL) {
565 /* generate nonoverlapping range [start, end) */
566 ret = generate_subkey(ctx, ent->lfsr, start, end - 1);
568 free_lfsr(ent->lfsr);
572 if (LIST_EMPTY(&ctx->head)) {
573 LIST_INSERT_HEAD(&ctx->head, ent, next);
575 LIST_FOREACH(next_ent, &ctx->head, next)
578 LIST_INSERT_AFTER(prev_ent, ent, next);
580 generate_complement_table(ctx, ent);
586 struct rte_thash_subtuple_helper *
587 rte_thash_get_helper(struct rte_thash_ctx *ctx, const char *name)
589 struct rte_thash_subtuple_helper *ent;
591 if ((ctx == NULL) || (name == NULL))
594 LIST_FOREACH(ent, &ctx->head, next) {
595 if (strncmp(name, ent->name, sizeof(ent->name)) == 0)
603 rte_thash_get_complement(struct rte_thash_subtuple_helper *h,
604 uint32_t hash, uint32_t desired_hash)
606 return h->compl_table[(hash ^ desired_hash) & h->lsb_msk];
610 rte_thash_get_key(struct rte_thash_ctx *ctx)
612 return ctx->hash_key;
615 static inline uint8_t
616 read_unaligned_byte(uint8_t *ptr, unsigned int len, unsigned int offset)
620 ret = ptr[offset / CHAR_BIT];
621 if (offset % CHAR_BIT) {
622 ret <<= (offset % CHAR_BIT);
623 ret |= ptr[(offset / CHAR_BIT) + 1] >>
624 (CHAR_BIT - (offset % CHAR_BIT));
627 return ret >> (CHAR_BIT - len);
630 static inline uint32_t
631 read_unaligned_bits(uint8_t *ptr, int len, int offset)
635 len = RTE_MAX(len, 0);
636 len = RTE_MIN(len, (int)(sizeof(uint32_t) * CHAR_BIT));
641 ret |= read_unaligned_byte(ptr, RTE_MIN(len, CHAR_BIT),
650 /* returns mask for len bits with given offset inside byte */
651 static inline uint8_t
652 get_bits_mask(unsigned int len, unsigned int offset)
654 unsigned int last_bit;
657 /* last bit within byte */
658 last_bit = RTE_MIN((unsigned int)CHAR_BIT, offset + len);
660 return ((1 << (CHAR_BIT - offset)) - 1) ^
661 ((1 << (CHAR_BIT - last_bit)) - 1);
665 write_unaligned_byte(uint8_t *ptr, unsigned int len,
666 unsigned int offset, uint8_t val)
670 tmp = ptr[offset / CHAR_BIT];
671 tmp &= ~get_bits_mask(len, offset);
672 tmp |= ((val << (CHAR_BIT - len)) >> (offset % CHAR_BIT));
673 ptr[offset / CHAR_BIT] = tmp;
674 if (((offset + len) / CHAR_BIT) != (offset / CHAR_BIT)) {
675 int rest_len = (offset + len) % CHAR_BIT;
676 tmp = ptr[(offset + len) / CHAR_BIT];
677 tmp &= ~get_bits_mask(rest_len, 0);
678 tmp |= val << (CHAR_BIT - rest_len);
679 ptr[(offset + len) / CHAR_BIT] = tmp;
684 write_unaligned_bits(uint8_t *ptr, int len, int offset, uint32_t val)
687 unsigned int part_len;
689 len = RTE_MAX(len, 0);
690 len = RTE_MIN(len, (int)(sizeof(uint32_t) * CHAR_BIT));
693 part_len = RTE_MIN(CHAR_BIT, len);
694 tmp = (uint8_t)val & ((1 << part_len) - 1);
695 write_unaligned_byte(ptr, part_len,
696 offset + len - part_len, tmp);
703 rte_thash_adjust_tuple(struct rte_thash_ctx *ctx,
704 struct rte_thash_subtuple_helper *h,
705 uint8_t *tuple, unsigned int tuple_len,
706 uint32_t desired_value, unsigned int attempts,
707 rte_thash_check_tuple_t fn, void *userdata)
709 uint32_t tmp_tuple[tuple_len / sizeof(uint32_t)];
710 unsigned int i, j, ret = 0;
711 uint32_t hash, adj_bits;
712 const uint8_t *hash_key;
717 if ((ctx == NULL) || (h == NULL) || (tuple == NULL) ||
718 (tuple_len % sizeof(uint32_t) != 0) || (attempts <= 0))
721 hash_key = rte_thash_get_key(ctx);
723 attempts = RTE_MIN(attempts, 1U << (h->tuple_len - ctx->reta_sz_log));
725 for (i = 0; i < attempts; i++) {
726 for (j = 0; j < (tuple_len / 4); j++)
728 rte_be_to_cpu_32(*(uint32_t *)&tuple[j * 4]);
730 hash = rte_softrss(tmp_tuple, tuple_len / 4, hash_key);
731 adj_bits = rte_thash_get_complement(h, hash, desired_value);
734 * Hint: LSB of adj_bits corresponds to
735 * offset + len bit of the subtuple
737 offset = h->tuple_offset + h->tuple_len - ctx->reta_sz_log;
738 tmp = read_unaligned_bits(tuple, ctx->reta_sz_log, offset);
740 write_unaligned_bits(tuple, ctx->reta_sz_log, offset, tmp);
743 ret = (fn(userdata, tuple)) ? 0 : -EEXIST;
746 else if (i < (attempts - 1)) {
747 /* increment subtuple part by 1 */
748 tmp_len = RTE_MIN(sizeof(uint32_t) * CHAR_BIT,
749 h->tuple_len - ctx->reta_sz_log);
751 tmp = read_unaligned_bits(tuple, tmp_len,
754 tmp &= (1 << tmp_len) - 1;
755 write_unaligned_bits(tuple, tmp_len, offset,