Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2021 Intel Corporation
3 : : */
4 : :
5 : : #include <sys/queue.h>
6 : :
7 : : #include <rte_thash.h>
8 : : #include <rte_tailq.h>
9 : : #include <rte_random.h>
10 : : #include <rte_memcpy.h>
11 : : #include <rte_errno.h>
12 : : #include <rte_eal_memconfig.h>
13 : : #include <rte_log.h>
14 : : #include <rte_malloc.h>
15 : :
16 [ - + ]: 235 : RTE_LOG_REGISTER_SUFFIX(thash_logtype, thash, INFO);
17 : : #define RTE_LOGTYPE_HASH thash_logtype
18 : : #define HASH_LOG(level, ...) \
19 : : RTE_LOG_LINE(level, HASH, "" __VA_ARGS__)
20 : :
21 : : #define THASH_NAME_LEN 64
22 : : #define TOEPLITZ_HASH_LEN 32
23 : :
24 : : #define RETA_SZ_IN_RANGE(reta_sz) ((reta_sz >= RTE_THASH_RETA_SZ_MIN) &&\
25 : : (reta_sz <= RTE_THASH_RETA_SZ_MAX))
26 : :
27 : : TAILQ_HEAD(rte_thash_list, rte_tailq_entry);
28 : : static struct rte_tailq_elem rte_thash_tailq = {
29 : : .name = "RTE_THASH",
30 : : };
31 [ - + ]: 235 : EAL_REGISTER_TAILQ(rte_thash_tailq)
32 : :
33 : : /**
34 : : * Table of some irreducible polinomials over GF(2).
35 : : * For lfsr they are represented in BE bit order, and
36 : : * x^0 is masked out.
37 : : * For example, poly x^5 + x^2 + 1 will be represented
38 : : * as (101001b & 11111b) = 01001b = 0x9
39 : : */
40 : : static const uint32_t irreducible_poly_table[][4] = {
41 : : {0, 0, 0, 0}, /** < degree 0 */
42 : : {1, 1, 1, 1}, /** < degree 1 */
43 : : {0x3, 0x3, 0x3, 0x3}, /** < degree 2 and so on... */
44 : : {0x5, 0x3, 0x5, 0x3},
45 : : {0x9, 0x3, 0x9, 0x3},
46 : : {0x9, 0x1b, 0xf, 0x5},
47 : : {0x21, 0x33, 0x1b, 0x2d},
48 : : {0x41, 0x11, 0x71, 0x9},
49 : : {0x71, 0xa9, 0xf5, 0x8d},
50 : : {0x21, 0xd1, 0x69, 0x1d9},
51 : : {0x81, 0x2c1, 0x3b1, 0x185},
52 : : {0x201, 0x541, 0x341, 0x461},
53 : : {0x941, 0x609, 0xe19, 0x45d},
54 : : {0x1601, 0x1f51, 0x1171, 0x359},
55 : : {0x2141, 0x2111, 0x2db1, 0x2109},
56 : : {0x4001, 0x801, 0x101, 0x7301},
57 : : {0x7781, 0xa011, 0x4211, 0x86d9},
58 : : };
59 : :
60 : : struct thash_lfsr {
61 : : uint32_t ref_cnt;
62 : : uint32_t poly;
63 : : /**< polynomial associated with the lfsr */
64 : : uint32_t rev_poly;
65 : : /**< polynomial to generate the sequence in reverse direction */
66 : : uint32_t state;
67 : : /**< current state of the lfsr */
68 : : uint32_t rev_state;
69 : : /**< current state of the lfsr for reverse direction */
70 : : uint32_t deg; /**< polynomial degree*/
71 : : uint32_t bits_cnt; /**< number of bits generated by lfsr*/
72 : : };
73 : :
74 : : struct rte_thash_subtuple_helper {
75 : : char name[THASH_NAME_LEN]; /** < Name of subtuple configuration */
76 : : LIST_ENTRY(rte_thash_subtuple_helper) next;
77 : : struct thash_lfsr *lfsr;
78 : : uint32_t offset; /** < Offset of the m-sequence */
79 : : uint32_t len; /** < Length of the m-sequence */
80 : : uint32_t tuple_offset; /** < Offset in bits of the subtuple */
81 : : uint32_t tuple_len; /** < Length in bits of the subtuple */
82 : : uint32_t lsb_msk; /** < (1 << reta_sz_log) - 1 */
83 : : __extension__ uint32_t compl_table[0] __rte_cache_aligned;
84 : : /** < Complementary table */
85 : : };
86 : :
87 : : struct rte_thash_ctx {
88 : : char name[THASH_NAME_LEN];
89 : : LIST_HEAD(, rte_thash_subtuple_helper) head;
90 : : uint32_t key_len; /** < Length of the NIC RSS hash key */
91 : : uint32_t reta_sz_log; /** < size of the RSS ReTa in bits */
92 : : uint32_t subtuples_nb; /** < number of subtuples */
93 : : uint32_t flags;
94 : : uint64_t *matrices;
95 : : /**< matrices used with rte_thash_gfni implementation */
96 : : uint8_t hash_key[0];
97 : : };
98 : :
99 : : int
100 : 173 : rte_thash_gfni_supported(void)
101 : : {
102 : : #ifdef RTE_THASH_GFNI_DEFINED
103 : : if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_GFNI) &&
104 : : (rte_vect_get_max_simd_bitwidth() >=
105 : : RTE_VECT_SIMD_512))
106 : : return 1;
107 : : #endif
108 : :
109 : 173 : return 0;
110 : : };
111 : :
112 : : void
113 : 0 : rte_thash_complete_matrix(uint64_t *matrixes, const uint8_t *rss_key, int size)
114 : : {
115 : : int i, j;
116 : : uint8_t *m = (uint8_t *)matrixes;
117 : : uint8_t left_part, right_part;
118 : :
119 [ # # ]: 0 : for (i = 0; i < size; i++) {
120 [ # # ]: 0 : for (j = 0; j < 8; j++) {
121 : 0 : left_part = rss_key[i] << j;
122 : 0 : right_part = (uint16_t)(rss_key[(i + 1) % size]) >>
123 : 0 : (8 - j);
124 : 0 : m[i * 8 + j] = left_part|right_part;
125 : : }
126 : : }
127 : 0 : }
128 : :
129 : : static inline uint32_t
130 : : get_bit_lfsr(struct thash_lfsr *lfsr)
131 : : {
132 : : uint32_t bit, ret;
133 : :
134 : : /*
135 : : * masking the TAP bits defined by the polynomial and
136 : : * calculating parity
137 : : */
138 : 3254 : bit = rte_popcount32(lfsr->state & lfsr->poly) & 0x1;
139 : 3254 : ret = lfsr->state & 0x1;
140 : 3254 : lfsr->state = ((lfsr->state >> 1) | (bit << (lfsr->deg - 1))) &
141 : 3254 : ((1 << lfsr->deg) - 1);
142 : :
143 : 3254 : lfsr->bits_cnt++;
144 : : return ret;
145 : : }
146 : :
147 : : static inline uint32_t
148 : : get_rev_bit_lfsr(struct thash_lfsr *lfsr)
149 : : {
150 : : uint32_t bit, ret;
151 : :
152 : 724 : bit = rte_popcount32(lfsr->rev_state & lfsr->rev_poly) & 0x1;
153 : 0 : ret = lfsr->rev_state & (1 << (lfsr->deg - 1));
154 : 724 : lfsr->rev_state = ((lfsr->rev_state << 1) | bit) &
155 : 0 : ((1 << lfsr->deg) - 1);
156 : :
157 : 724 : lfsr->bits_cnt++;
158 : : return ret;
159 : : }
160 : :
161 : : static inline uint32_t
162 : : thash_get_rand_poly(uint32_t poly_degree)
163 : : {
164 : 142 : return irreducible_poly_table[poly_degree][rte_rand() %
165 : : RTE_DIM(irreducible_poly_table[poly_degree])];
166 : : }
167 : :
168 : : static struct thash_lfsr *
169 : 71 : alloc_lfsr(struct rte_thash_ctx *ctx)
170 : : {
171 : : struct thash_lfsr *lfsr;
172 : : uint32_t i;
173 : :
174 [ + - ]: 71 : if (ctx == NULL)
175 : : return NULL;
176 : :
177 : 71 : lfsr = rte_zmalloc(NULL, sizeof(struct thash_lfsr), 0);
178 [ + - ]: 71 : if (lfsr == NULL)
179 : : return NULL;
180 : :
181 : 71 : lfsr->deg = ctx->reta_sz_log;
182 : 71 : lfsr->poly = thash_get_rand_poly(lfsr->deg);
183 : : do {
184 : 71 : lfsr->state = rte_rand() & ((1 << lfsr->deg) - 1);
185 [ - + ]: 71 : } while (lfsr->state == 0);
186 : : /* init reverse order polynomial */
187 : 71 : lfsr->rev_poly = (lfsr->poly >> 1) | (1 << (lfsr->deg - 1));
188 : : /* init proper rev_state*/
189 : 71 : lfsr->rev_state = lfsr->state;
190 [ + + ]: 795 : for (i = 0; i <= lfsr->deg; i++)
191 : : get_rev_bit_lfsr(lfsr);
192 : :
193 : : /* clear bits_cnt after rev_state was inited */
194 : 71 : lfsr->bits_cnt = 0;
195 : 71 : lfsr->ref_cnt = 1;
196 : :
197 : 71 : return lfsr;
198 : : }
199 : :
200 : : static void
201 : : attach_lfsr(struct rte_thash_subtuple_helper *h, struct thash_lfsr *lfsr)
202 : : {
203 : 5 : lfsr->ref_cnt++;
204 : 5 : h->lfsr = lfsr;
205 : : }
206 : :
207 : : static void
208 : : free_lfsr(struct thash_lfsr *lfsr)
209 : : {
210 : 75 : lfsr->ref_cnt--;
211 : 75 : if (lfsr->ref_cnt == 0)
212 : 70 : rte_free(lfsr);
213 : : }
214 : :
215 : : struct rte_thash_ctx *
216 : 173 : rte_thash_init_ctx(const char *name, uint32_t key_len, uint32_t reta_sz,
217 : : uint8_t *key, uint32_t flags)
218 : : {
219 : : struct rte_thash_ctx *ctx;
220 : : struct rte_tailq_entry *te;
221 : : struct rte_thash_list *thash_list;
222 : : uint32_t i;
223 : :
224 [ + + - + ]: 173 : if ((name == NULL) || (key_len == 0) || !RETA_SZ_IN_RANGE(reta_sz)) {
225 : 4 : rte_errno = EINVAL;
226 : 4 : return NULL;
227 : : }
228 : :
229 : 169 : thash_list = RTE_TAILQ_CAST(rte_thash_tailq.head, rte_thash_list);
230 : :
231 : 169 : rte_mcfg_tailq_write_lock();
232 : :
233 : : /* guarantee there's no existing */
234 [ - + ]: 169 : TAILQ_FOREACH(te, thash_list, next) {
235 : 0 : ctx = (struct rte_thash_ctx *)te->data;
236 [ # # ]: 0 : if (strncmp(name, ctx->name, sizeof(ctx->name)) == 0)
237 : : break;
238 : : }
239 : : ctx = NULL;
240 [ - + ]: 169 : if (te != NULL) {
241 : 0 : rte_errno = EEXIST;
242 : 0 : goto exit;
243 : : }
244 : :
245 : : /* allocate tailq entry */
246 : 169 : te = rte_zmalloc("THASH_TAILQ_ENTRY", sizeof(*te), 0);
247 [ - + ]: 169 : if (te == NULL) {
248 : 0 : HASH_LOG(ERR,
249 : : "Can not allocate tailq entry for thash context %s",
250 : : name);
251 : 0 : rte_errno = ENOMEM;
252 : 0 : goto exit;
253 : : }
254 : :
255 : 169 : ctx = rte_zmalloc(NULL, sizeof(struct rte_thash_ctx) + key_len, 0);
256 [ - + ]: 169 : if (ctx == NULL) {
257 : 0 : HASH_LOG(ERR, "thash ctx %s memory allocation failed",
258 : : name);
259 : 0 : rte_errno = ENOMEM;
260 : 0 : goto free_te;
261 : : }
262 : :
263 [ + + ]: 169 : rte_strlcpy(ctx->name, name, sizeof(ctx->name));
264 : 169 : ctx->key_len = key_len;
265 : 169 : ctx->reta_sz_log = reta_sz;
266 : 169 : LIST_INIT(&ctx->head);
267 : 169 : ctx->flags = flags;
268 : :
269 [ + + ]: 169 : if (key)
270 [ - + ]: 1 : rte_memcpy(ctx->hash_key, key, key_len);
271 : : else {
272 [ + + ]: 6888 : for (i = 0; i < key_len; i++)
273 : 6720 : ctx->hash_key[i] = rte_rand();
274 : : }
275 : :
276 [ - + ]: 169 : if (rte_thash_gfni_supported()) {
277 : 0 : ctx->matrices = rte_zmalloc(NULL, key_len * sizeof(uint64_t),
278 : : RTE_CACHE_LINE_SIZE);
279 [ # # ]: 0 : if (ctx->matrices == NULL) {
280 : 0 : HASH_LOG(ERR, "Cannot allocate matrices");
281 : 0 : rte_errno = ENOMEM;
282 : 0 : goto free_ctx;
283 : : }
284 : :
285 : 0 : rte_thash_complete_matrix(ctx->matrices, ctx->hash_key,
286 : : key_len);
287 : : }
288 : :
289 : 169 : te->data = (void *)ctx;
290 : 169 : TAILQ_INSERT_TAIL(thash_list, te, next);
291 : :
292 : 169 : rte_mcfg_tailq_write_unlock();
293 : :
294 : 169 : return ctx;
295 : :
296 : : free_ctx:
297 : 0 : rte_free(ctx);
298 : 0 : free_te:
299 : 0 : rte_free(te);
300 : 0 : exit:
301 : 0 : rte_mcfg_tailq_write_unlock();
302 : 0 : return NULL;
303 : : }
304 : :
305 : : struct rte_thash_ctx *
306 : 1 : rte_thash_find_existing(const char *name)
307 : : {
308 : : struct rte_thash_ctx *ctx;
309 : : struct rte_tailq_entry *te;
310 : : struct rte_thash_list *thash_list;
311 : :
312 : 1 : thash_list = RTE_TAILQ_CAST(rte_thash_tailq.head, rte_thash_list);
313 : :
314 : 1 : rte_mcfg_tailq_read_lock();
315 [ + - ]: 1 : TAILQ_FOREACH(te, thash_list, next) {
316 : 1 : ctx = (struct rte_thash_ctx *)te->data;
317 [ - + ]: 1 : if (strncmp(name, ctx->name, sizeof(ctx->name)) == 0)
318 : : break;
319 : : }
320 : :
321 : 1 : rte_mcfg_tailq_read_unlock();
322 : :
323 [ - + ]: 1 : if (te == NULL) {
324 : 0 : rte_errno = ENOENT;
325 : 0 : return NULL;
326 : : }
327 : :
328 : : return ctx;
329 : : }
330 : :
331 : : void
332 : 170 : rte_thash_free_ctx(struct rte_thash_ctx *ctx)
333 : : {
334 : : struct rte_tailq_entry *te;
335 : : struct rte_thash_list *thash_list;
336 : : struct rte_thash_subtuple_helper *ent, *tmp;
337 : :
338 [ + + ]: 170 : if (ctx == NULL)
339 : : return;
340 : :
341 : 169 : thash_list = RTE_TAILQ_CAST(rte_thash_tailq.head, rte_thash_list);
342 : 169 : rte_mcfg_tailq_write_lock();
343 [ + - ]: 169 : TAILQ_FOREACH(te, thash_list, next) {
344 [ - + ]: 169 : if (te->data == (void *)ctx)
345 : : break;
346 : : }
347 : :
348 [ + - ]: 169 : if (te != NULL)
349 [ - + ]: 169 : TAILQ_REMOVE(thash_list, te, next);
350 : :
351 : 169 : rte_mcfg_tailq_write_unlock();
352 : 169 : ent = LIST_FIRST(&(ctx->head));
353 [ + + ]: 243 : while (ent) {
354 [ + + ]: 74 : free_lfsr(ent->lfsr);
355 : : tmp = ent;
356 : 74 : ent = LIST_NEXT(ent, next);
357 [ + + ]: 74 : LIST_REMOVE(tmp, next);
358 : 74 : rte_free(tmp);
359 : : }
360 : :
361 : 169 : rte_free(ctx);
362 : 169 : rte_free(te);
363 : : }
364 : :
365 : : static inline void
366 : : set_bit(uint8_t *ptr, uint32_t bit, uint32_t pos)
367 : : {
368 : 3254 : uint32_t byte_idx = pos / CHAR_BIT;
369 : : /* index of the bit int byte, indexing starts from MSB */
370 : 3254 : uint32_t bit_idx = (CHAR_BIT - 1) - (pos & (CHAR_BIT - 1));
371 : : uint8_t tmp;
372 : :
373 : 3254 : tmp = ptr[byte_idx];
374 : 3254 : tmp &= ~(1 << bit_idx);
375 : 3254 : tmp |= bit << bit_idx;
376 : 3254 : ptr[byte_idx] = tmp;
377 : : }
378 : :
379 : : /**
380 : : * writes m-sequence to the hash_key for range [start, end]
381 : : * (i.e. including start and end positions)
382 : : */
383 : : static int
384 : 76 : generate_subkey(struct rte_thash_ctx *ctx, struct thash_lfsr *lfsr,
385 : : uint32_t start, uint32_t end)
386 : : {
387 : : uint32_t i;
388 [ + + ]: 76 : uint32_t req_bits = (start < end) ? (end - start) : (start - end);
389 : 76 : req_bits++; /* due to including end */
390 : :
391 : : /* check if lfsr overflow period of the m-sequence */
392 [ + + ]: 76 : if (((lfsr->bits_cnt + req_bits) > (1ULL << lfsr->deg) - 1) &&
393 [ + + ]: 2 : ((ctx->flags & RTE_THASH_IGNORE_PERIOD_OVERFLOW) !=
394 : : RTE_THASH_IGNORE_PERIOD_OVERFLOW)) {
395 : 1 : HASH_LOG(ERR,
396 : : "Can't generate m-sequence due to period overflow");
397 : 1 : return -ENOSPC;
398 : : }
399 : :
400 [ + + ]: 75 : if (start < end) {
401 : : /* original direction (from left to right)*/
402 [ + + ]: 3326 : for (i = start; i <= end; i++)
403 : 3254 : set_bit(ctx->hash_key, get_bit_lfsr(lfsr), i);
404 : :
405 : : } else {
406 : : /* reverse direction (from right to left) */
407 [ - + ]: 3 : for (i = end; i >= start; i--)
408 : 0 : set_bit(ctx->hash_key, get_rev_bit_lfsr(lfsr), i);
409 : : }
410 : :
411 [ - + ]: 75 : if (ctx->matrices != NULL)
412 : 0 : rte_thash_complete_matrix(ctx->matrices, ctx->hash_key,
413 : 0 : ctx->key_len);
414 : :
415 : : return 0;
416 : : }
417 : :
418 : : static inline uint32_t
419 : 1837504 : get_subvalue(struct rte_thash_ctx *ctx, uint32_t offset)
420 : : {
421 : : uint32_t *tmp, val;
422 : :
423 : 1837504 : tmp = (uint32_t *)(&ctx->hash_key[offset >> 3]);
424 [ - + ]: 1837504 : val = rte_be_to_cpu_32(*tmp);
425 : 1837504 : val >>= (TOEPLITZ_HASH_LEN - ((offset & (CHAR_BIT - 1)) +
426 : 1837504 : ctx->reta_sz_log));
427 : :
428 : 1837504 : return val & ((1 << ctx->reta_sz_log) - 1);
429 : : }
430 : :
431 : : static inline void
432 : 74 : generate_complement_table(struct rte_thash_ctx *ctx,
433 : : struct rte_thash_subtuple_helper *h)
434 : : {
435 : : int i, j, k;
436 : : uint32_t val;
437 : : uint32_t start;
438 : :
439 : 74 : start = h->offset + h->len - (2 * ctx->reta_sz_log - 1);
440 : :
441 [ + + ]: 262464 : for (i = 1; i < (1 << ctx->reta_sz_log); i++) {
442 : : val = 0;
443 [ + + ]: 2099894 : for (j = i; j; j &= (j - 1)) {
444 : 1837504 : k = rte_bsf32(j);
445 : 1837504 : val ^= get_subvalue(ctx, start - k +
446 : : ctx->reta_sz_log - 1);
447 : : }
448 : 262390 : h->compl_table[val] = i;
449 : : }
450 : 74 : }
451 : :
452 : : static inline int
453 : 3 : insert_before(struct rte_thash_ctx *ctx,
454 : : struct rte_thash_subtuple_helper *ent,
455 : : struct rte_thash_subtuple_helper *cur_ent,
456 : : struct rte_thash_subtuple_helper *next_ent,
457 : : uint32_t start, uint32_t end, uint32_t range_end)
458 : : {
459 : : int ret;
460 : :
461 [ + + ]: 3 : if (end < cur_ent->offset) {
462 : 1 : ent->lfsr = alloc_lfsr(ctx);
463 [ - + ]: 1 : if (ent->lfsr == NULL) {
464 : 0 : rte_free(ent);
465 : 0 : return -ENOMEM;
466 : : }
467 : : /* generate nonoverlapping range [start, end) */
468 : 1 : ret = generate_subkey(ctx, ent->lfsr, start, end - 1);
469 [ - + ]: 1 : if (ret != 0) {
470 [ # # ]: 0 : free_lfsr(ent->lfsr);
471 : 0 : rte_free(ent);
472 : 0 : return ret;
473 : : }
474 [ + - - + ]: 2 : } else if ((next_ent != NULL) && (end > next_ent->offset)) {
475 : 0 : HASH_LOG(ERR,
476 : : "Can't add helper %s due to conflict with existing"
477 : : " helper %s", ent->name, next_ent->name);
478 : 0 : rte_free(ent);
479 : 0 : return -ENOSPC;
480 : : }
481 : 3 : attach_lfsr(ent, cur_ent->lfsr);
482 : :
483 : : /**
484 : : * generate partially overlapping range
485 : : * [start, cur_ent->start) in reverse order
486 : : */
487 : 3 : ret = generate_subkey(ctx, ent->lfsr, cur_ent->offset - 1, start);
488 [ - + ]: 3 : if (ret != 0) {
489 [ # # ]: 0 : free_lfsr(ent->lfsr);
490 : 0 : rte_free(ent);
491 : 0 : return ret;
492 : : }
493 : :
494 [ + + ]: 3 : if (end > range_end) {
495 : : /**
496 : : * generate partially overlapping range
497 : : * (range_end, end)
498 : : */
499 : 1 : ret = generate_subkey(ctx, ent->lfsr, range_end, end - 1);
500 [ - + ]: 1 : if (ret != 0) {
501 [ # # ]: 0 : free_lfsr(ent->lfsr);
502 : 0 : rte_free(ent);
503 : 0 : return ret;
504 : : }
505 : : }
506 : :
507 : 3 : LIST_INSERT_BEFORE(cur_ent, ent, next);
508 : 3 : generate_complement_table(ctx, ent);
509 : 3 : ctx->subtuples_nb++;
510 : 3 : return 0;
511 : : }
512 : :
513 : : static inline int
514 : 3 : insert_after(struct rte_thash_ctx *ctx,
515 : : struct rte_thash_subtuple_helper *ent,
516 : : struct rte_thash_subtuple_helper *cur_ent,
517 : : struct rte_thash_subtuple_helper *next_ent,
518 : : struct rte_thash_subtuple_helper *prev_ent,
519 : : uint32_t end, uint32_t range_end)
520 : : {
521 : : int ret;
522 : :
523 [ + + + + ]: 3 : if ((next_ent != NULL) && (end > next_ent->offset)) {
524 : 1 : HASH_LOG(ERR,
525 : : "Can't add helper %s due to conflict with existing"
526 : : " helper %s", ent->name, next_ent->name);
527 : 1 : rte_free(ent);
528 : 1 : return -EEXIST;
529 : : }
530 : :
531 : 2 : attach_lfsr(ent, cur_ent->lfsr);
532 [ + + ]: 2 : if (end > range_end) {
533 : : /**
534 : : * generate partially overlapping range
535 : : * (range_end, end)
536 : : */
537 : 1 : ret = generate_subkey(ctx, ent->lfsr, range_end, end - 1);
538 [ - + ]: 1 : if (ret != 0) {
539 [ # # ]: 0 : free_lfsr(ent->lfsr);
540 : 0 : rte_free(ent);
541 : 0 : return ret;
542 : : }
543 : : }
544 : :
545 [ + + ]: 2 : LIST_INSERT_AFTER(prev_ent, ent, next);
546 : 2 : generate_complement_table(ctx, ent);
547 : 2 : ctx->subtuples_nb++;
548 : :
549 : 2 : return 0;
550 : : }
551 : :
552 : : int
553 : 81 : rte_thash_add_helper(struct rte_thash_ctx *ctx, const char *name, uint32_t len,
554 : : uint32_t offset)
555 : : {
556 : : struct rte_thash_subtuple_helper *ent, *cur_ent, *prev_ent, *next_ent;
557 : : uint32_t start, end;
558 : : int ret;
559 : :
560 [ + + + + ]: 81 : if ((ctx == NULL) || (name == NULL) || (len < ctx->reta_sz_log) ||
561 : 78 : ((offset + len + TOEPLITZ_HASH_LEN - 1) >
562 [ + + ]: 78 : ctx->key_len * CHAR_BIT))
563 : : return -EINVAL;
564 : :
565 : : /* Check for existing name*/
566 [ + + ]: 101 : LIST_FOREACH(cur_ent, &ctx->head, next) {
567 [ + + ]: 25 : if (strncmp(name, cur_ent->name, sizeof(cur_ent->name)) == 0)
568 : : return -EEXIST;
569 : : }
570 : :
571 : : end = offset + len + TOEPLITZ_HASH_LEN - 1;
572 : 76 : start = ((ctx->flags & RTE_THASH_MINIMAL_SEQ) ==
573 [ + + ]: 76 : RTE_THASH_MINIMAL_SEQ) ? (end - (2 * ctx->reta_sz_log - 1)) :
574 : : offset;
575 : :
576 : 76 : ent = rte_zmalloc(NULL, sizeof(struct rte_thash_subtuple_helper) +
577 : 76 : sizeof(uint32_t) * (1 << ctx->reta_sz_log),
578 : : RTE_CACHE_LINE_SIZE);
579 [ + - ]: 76 : if (ent == NULL)
580 : : return -ENOMEM;
581 : :
582 : 76 : rte_strlcpy(ent->name, name, sizeof(ent->name));
583 : 76 : ent->offset = start;
584 : 76 : ent->len = end - start;
585 : 76 : ent->tuple_offset = offset;
586 : 76 : ent->tuple_len = len;
587 : 76 : ent->lsb_msk = (1 << ctx->reta_sz_log) - 1;
588 : :
589 : 76 : cur_ent = LIST_FIRST(&ctx->head);
590 [ + + ]: 78 : while (cur_ent) {
591 : 8 : uint32_t range_end = cur_ent->offset + cur_ent->len;
592 : 8 : next_ent = LIST_NEXT(cur_ent, next);
593 : : prev_ent = cur_ent;
594 : : /* Iterate through overlapping ranges */
595 [ + + + + ]: 19 : while ((next_ent != NULL) && (next_ent->offset < range_end)) {
596 : 11 : range_end = RTE_MAX(next_ent->offset + next_ent->len,
597 : : range_end);
598 [ + + ]: 11 : if (start > next_ent->offset)
599 : : prev_ent = next_ent;
600 : :
601 : 11 : next_ent = LIST_NEXT(next_ent, next);
602 : : }
603 : :
604 [ + + ]: 8 : if (start < cur_ent->offset)
605 : 3 : return insert_before(ctx, ent, cur_ent, next_ent,
606 : : start, end, range_end);
607 [ + + ]: 5 : else if (start < range_end)
608 : 3 : return insert_after(ctx, ent, cur_ent, next_ent,
609 : : prev_ent, end, range_end);
610 : :
611 : : cur_ent = next_ent;
612 : 2 : continue;
613 : : }
614 : :
615 : 70 : ent->lfsr = alloc_lfsr(ctx);
616 [ - + ]: 70 : if (ent->lfsr == NULL) {
617 : 0 : rte_free(ent);
618 : 0 : return -ENOMEM;
619 : : }
620 : :
621 : : /* generate nonoverlapping range [start, end) */
622 : 70 : ret = generate_subkey(ctx, ent->lfsr, start, end - 1);
623 [ + + ]: 70 : if (ret != 0) {
624 [ + - ]: 1 : free_lfsr(ent->lfsr);
625 : 1 : rte_free(ent);
626 : 1 : return ret;
627 : : }
628 [ + + ]: 69 : if (LIST_EMPTY(&ctx->head)) {
629 : 67 : LIST_INSERT_HEAD(&ctx->head, ent, next);
630 : : } else {
631 [ + + ]: 5 : LIST_FOREACH(next_ent, &ctx->head, next)
632 : : prev_ent = next_ent;
633 : :
634 [ - + ]: 2 : LIST_INSERT_AFTER(prev_ent, ent, next);
635 : : }
636 : 69 : generate_complement_table(ctx, ent);
637 : 69 : ctx->subtuples_nb++;
638 : :
639 : 69 : return 0;
640 : : }
641 : :
642 : : struct rte_thash_subtuple_helper *
643 : 72 : rte_thash_get_helper(struct rte_thash_ctx *ctx, const char *name)
644 : : {
645 : : struct rte_thash_subtuple_helper *ent;
646 : :
647 [ + + ]: 72 : if ((ctx == NULL) || (name == NULL))
648 : : return NULL;
649 : :
650 [ + - ]: 76 : LIST_FOREACH(ent, &ctx->head, next) {
651 [ + + ]: 76 : if (strncmp(name, ent->name, sizeof(ent->name)) == 0)
652 : 70 : return ent;
653 : : }
654 : :
655 : : return NULL;
656 : : }
657 : :
658 : : uint32_t
659 : 825 : rte_thash_get_complement(struct rte_thash_subtuple_helper *h,
660 : : uint32_t hash, uint32_t desired_hash)
661 : : {
662 : 825 : return h->compl_table[(hash ^ desired_hash) & h->lsb_msk];
663 : : }
664 : :
665 : : const uint8_t *
666 : 126 : rte_thash_get_key(struct rte_thash_ctx *ctx)
667 : : {
668 : 126 : return ctx->hash_key;
669 : : }
670 : :
671 : : const uint64_t *
672 : 0 : rte_thash_get_gfni_matrices(struct rte_thash_ctx *ctx)
673 : : {
674 : 0 : return ctx->matrices;
675 : : }
676 : :
677 : : static inline uint8_t
678 : : read_unaligned_byte(uint8_t *ptr, unsigned int offset)
679 : : {
680 : : uint8_t ret = 0;
681 : :
682 : 101 : ret = ptr[offset / CHAR_BIT];
683 : 101 : if (offset % CHAR_BIT) {
684 : 82 : ret <<= (offset % CHAR_BIT);
685 : 82 : ret |= ptr[(offset / CHAR_BIT) + 1] >>
686 : 82 : (CHAR_BIT - (offset % CHAR_BIT));
687 : : }
688 : :
689 : : return ret;
690 : : }
691 : :
692 : : static inline uint32_t
693 : 65 : read_unaligned_bits(uint8_t *ptr, int len, int offset)
694 : : {
695 : : uint32_t ret = 0;
696 : : int shift;
697 : :
698 : 65 : len = RTE_MAX(len, 0);
699 : 65 : len = RTE_MIN(len, (int)(sizeof(uint32_t) * CHAR_BIT));
700 : :
701 [ + + ]: 166 : while (len > 0) {
702 : 101 : ret <<= CHAR_BIT;
703 : :
704 [ + + ]: 101 : ret |= read_unaligned_byte(ptr, offset);
705 : 101 : offset += CHAR_BIT;
706 : 101 : len -= CHAR_BIT;
707 : : }
708 : :
709 [ + + ]: 65 : shift = (len == 0) ? 0 :
710 : 51 : (CHAR_BIT - ((len + CHAR_BIT) % CHAR_BIT));
711 : 65 : return ret >> shift;
712 : : }
713 : :
714 : : /* returns mask for len bits with given offset inside byte */
715 : : static inline uint8_t
716 : : get_bits_mask(unsigned int len, unsigned int offset)
717 : : {
718 : : unsigned int last_bit;
719 : :
720 : 101 : offset %= CHAR_BIT;
721 : : /* last bit within byte */
722 : 171 : last_bit = RTE_MIN((unsigned int)CHAR_BIT, offset + len);
723 : :
724 : 101 : return ((1 << (CHAR_BIT - offset)) - 1) ^
725 : 171 : ((1 << (CHAR_BIT - last_bit)) - 1);
726 : : }
727 : :
728 : : static inline void
729 : 101 : write_unaligned_byte(uint8_t *ptr, unsigned int len,
730 : : unsigned int offset, uint8_t val)
731 : : {
732 : : uint8_t tmp;
733 : :
734 : 101 : tmp = ptr[offset / CHAR_BIT];
735 : 101 : tmp &= ~get_bits_mask(len, offset);
736 : 101 : tmp |= ((val << (CHAR_BIT - len)) >> (offset % CHAR_BIT));
737 : 101 : ptr[offset / CHAR_BIT] = tmp;
738 [ + + ]: 101 : if (((offset + len) / CHAR_BIT) != (offset / CHAR_BIT)) {
739 : 70 : int rest_len = (offset + len) % CHAR_BIT;
740 : 70 : tmp = ptr[(offset + len) / CHAR_BIT];
741 : 70 : tmp &= ~get_bits_mask(rest_len, 0);
742 : 70 : tmp |= val << (CHAR_BIT - rest_len);
743 : 70 : ptr[(offset + len) / CHAR_BIT] = tmp;
744 : : }
745 : 101 : }
746 : :
747 : : static inline void
748 : 65 : write_unaligned_bits(uint8_t *ptr, int len, int offset, uint32_t val)
749 : : {
750 : : uint8_t tmp;
751 : : unsigned int part_len;
752 : :
753 : 65 : len = RTE_MAX(len, 0);
754 : 65 : len = RTE_MIN(len, (int)(sizeof(uint32_t) * CHAR_BIT));
755 : :
756 [ + + ]: 166 : while (len > 0) {
757 : 101 : part_len = RTE_MIN(CHAR_BIT, len);
758 : 101 : tmp = (uint8_t)val & ((1 << part_len) - 1);
759 : 101 : write_unaligned_byte(ptr, part_len,
760 : 101 : offset + len - part_len, tmp);
761 : 101 : len -= CHAR_BIT;
762 : 101 : val >>= CHAR_BIT;
763 : : }
764 : 65 : }
765 : :
766 : : int
767 : 63 : rte_thash_adjust_tuple(struct rte_thash_ctx *ctx,
768 : : struct rte_thash_subtuple_helper *h,
769 : : uint8_t *tuple, unsigned int tuple_len,
770 : : uint32_t desired_value, unsigned int attempts,
771 : : rte_thash_check_tuple_t fn, void *userdata)
772 : 63 : {
773 : 63 : uint32_t tmp_tuple[tuple_len / sizeof(uint32_t)];
774 : : unsigned int i, j, ret = 0;
775 : : uint32_t hash, adj_bits;
776 : : const uint8_t *hash_key;
777 : : uint32_t tmp;
778 : : int offset;
779 : : int tmp_len;
780 : :
781 [ + - + - ]: 63 : if ((ctx == NULL) || (h == NULL) || (tuple == NULL) ||
782 [ + - + - ]: 63 : (tuple_len % sizeof(uint32_t) != 0) || (attempts <= 0))
783 : : return -EINVAL;
784 : :
785 : 63 : hash_key = rte_thash_get_key(ctx);
786 : :
787 : 63 : attempts = RTE_MIN(attempts, 1U << (h->tuple_len - ctx->reta_sz_log));
788 : :
789 [ + + ]: 65 : for (i = 0; i < attempts; i++) {
790 [ - + ]: 64 : if (ctx->matrices != NULL)
791 : 0 : hash = rte_thash_gfni(ctx->matrices, tuple, tuple_len);
792 : : else {
793 [ + + ]: 256 : for (j = 0; j < (tuple_len / 4); j++)
794 : 192 : tmp_tuple[j] =
795 [ - + ]: 192 : rte_be_to_cpu_32(
796 : : *(uint32_t *)&tuple[j * 4]);
797 : :
798 : 64 : hash = rte_softrss(tmp_tuple, tuple_len / 4, hash_key);
799 : : }
800 : :
801 : 64 : adj_bits = rte_thash_get_complement(h, hash, desired_value);
802 : :
803 : : /*
804 : : * Hint: LSB of adj_bits corresponds to
805 : : * offset + len bit of the subtuple
806 : : */
807 : 64 : offset = h->tuple_offset + h->tuple_len - ctx->reta_sz_log;
808 : 64 : tmp = read_unaligned_bits(tuple, ctx->reta_sz_log, offset);
809 : 64 : tmp ^= adj_bits;
810 : 64 : write_unaligned_bits(tuple, ctx->reta_sz_log, offset, tmp);
811 : :
812 [ + + ]: 64 : if (fn != NULL) {
813 [ + + ]: 3 : ret = (fn(userdata, tuple)) ? 0 : -EEXIST;
814 : : if (ret == 0)
815 : : return 0;
816 [ + + ]: 2 : else if (i < (attempts - 1)) {
817 : : /* increment subtuple part by 1 */
818 : 1 : tmp_len = RTE_MIN(sizeof(uint32_t) * CHAR_BIT,
819 : : h->tuple_len - ctx->reta_sz_log);
820 : 1 : offset -= tmp_len;
821 : 1 : tmp = read_unaligned_bits(tuple, tmp_len,
822 : : offset);
823 : 1 : tmp++;
824 : 1 : tmp &= (1 << tmp_len) - 1;
825 : 1 : write_unaligned_bits(tuple, tmp_len, offset,
826 : : tmp);
827 : : }
828 : : } else
829 : : return 0;
830 : : }
831 : :
832 : 1 : return ret;
833 : : }
|