1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
5 #include <rte_eal_memconfig.h>
6 #include <rte_string_fns.h>
13 TAILQ_HEAD(rte_acl_list, rte_tailq_entry);
15 static struct rte_tailq_elem rte_acl_tailq = {
18 EAL_REGISTER_TAILQ(rte_acl_tailq)
20 #ifndef CC_AVX512_SUPPORT
22 * If the compiler doesn't support AVX512 instructions,
23 * then the dummy one would be used instead for AVX512 classify method.
26 rte_acl_classify_avx512x16(__rte_unused const struct rte_acl_ctx *ctx,
27 __rte_unused const uint8_t **data,
28 __rte_unused uint32_t *results,
29 __rte_unused uint32_t num,
30 __rte_unused uint32_t categories)
36 rte_acl_classify_avx512x32(__rte_unused const struct rte_acl_ctx *ctx,
37 __rte_unused const uint8_t **data,
38 __rte_unused uint32_t *results,
39 __rte_unused uint32_t num,
40 __rte_unused uint32_t categories)
46 #ifndef CC_AVX2_SUPPORT
48 * If the compiler doesn't support AVX2 instructions,
49 * then the dummy one would be used instead for AVX2 classify method.
52 rte_acl_classify_avx2(__rte_unused const struct rte_acl_ctx *ctx,
53 __rte_unused const uint8_t **data,
54 __rte_unused uint32_t *results,
55 __rte_unused uint32_t num,
56 __rte_unused uint32_t categories)
64 rte_acl_classify_sse(__rte_unused const struct rte_acl_ctx *ctx,
65 __rte_unused const uint8_t **data,
66 __rte_unused uint32_t *results,
67 __rte_unused uint32_t num,
68 __rte_unused uint32_t categories)
76 rte_acl_classify_neon(__rte_unused const struct rte_acl_ctx *ctx,
77 __rte_unused const uint8_t **data,
78 __rte_unused uint32_t *results,
79 __rte_unused uint32_t num,
80 __rte_unused uint32_t categories)
86 #ifndef RTE_ARCH_PPC_64
88 rte_acl_classify_altivec(__rte_unused const struct rte_acl_ctx *ctx,
89 __rte_unused const uint8_t **data,
90 __rte_unused uint32_t *results,
91 __rte_unused uint32_t num,
92 __rte_unused uint32_t categories)
98 static const rte_acl_classify_t classify_fns[] = {
99 [RTE_ACL_CLASSIFY_DEFAULT] = rte_acl_classify_scalar,
100 [RTE_ACL_CLASSIFY_SCALAR] = rte_acl_classify_scalar,
101 [RTE_ACL_CLASSIFY_SSE] = rte_acl_classify_sse,
102 [RTE_ACL_CLASSIFY_AVX2] = rte_acl_classify_avx2,
103 [RTE_ACL_CLASSIFY_NEON] = rte_acl_classify_neon,
104 [RTE_ACL_CLASSIFY_ALTIVEC] = rte_acl_classify_altivec,
105 [RTE_ACL_CLASSIFY_AVX512X16] = rte_acl_classify_avx512x16,
106 [RTE_ACL_CLASSIFY_AVX512X32] = rte_acl_classify_avx512x32,
110 * Helper function for acl_check_alg.
111 * Check support for ARM specific classify methods.
114 acl_check_alg_arm(enum rte_acl_classify_alg alg)
116 if (alg == RTE_ACL_CLASSIFY_NEON) {
117 #if defined(RTE_ARCH_ARM64)
118 if (rte_vect_get_max_simd_bitwidth() >= RTE_VECT_SIMD_128)
120 #elif defined(RTE_ARCH_ARM)
121 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON) &&
122 rte_vect_get_max_simd_bitwidth() >= RTE_VECT_SIMD_128)
132 * Helper function for acl_check_alg.
133 * Check support for PPC specific classify methods.
136 acl_check_alg_ppc(enum rte_acl_classify_alg alg)
138 if (alg == RTE_ACL_CLASSIFY_ALTIVEC) {
139 #if defined(RTE_ARCH_PPC_64)
140 if (rte_vect_get_max_simd_bitwidth() >= RTE_VECT_SIMD_128)
149 #ifdef CC_AVX512_SUPPORT
151 acl_check_avx512_cpu_flags(void)
153 return (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F) &&
154 rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512VL) &&
155 rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512CD) &&
156 rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512BW));
161 * Helper function for acl_check_alg.
162 * Check support for x86 specific classify methods.
165 acl_check_alg_x86(enum rte_acl_classify_alg alg)
167 if (alg == RTE_ACL_CLASSIFY_AVX512X32) {
168 #ifdef CC_AVX512_SUPPORT
169 if (acl_check_avx512_cpu_flags() != 0 &&
170 rte_vect_get_max_simd_bitwidth() >= RTE_VECT_SIMD_512)
176 if (alg == RTE_ACL_CLASSIFY_AVX512X16) {
177 #ifdef CC_AVX512_SUPPORT
178 if (acl_check_avx512_cpu_flags() != 0 &&
179 rte_vect_get_max_simd_bitwidth() >= RTE_VECT_SIMD_256)
185 if (alg == RTE_ACL_CLASSIFY_AVX2) {
186 #ifdef CC_AVX2_SUPPORT
187 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2) &&
188 rte_vect_get_max_simd_bitwidth() >= RTE_VECT_SIMD_256)
194 if (alg == RTE_ACL_CLASSIFY_SSE) {
196 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1) &&
197 rte_vect_get_max_simd_bitwidth() >= RTE_VECT_SIMD_128)
207 * Check if input alg is supported by given platform/binary.
208 * Note that both conditions should be met:
209 * - at build time compiler supports ISA used by given methods
210 * - at run time target cpu supports necessary ISA.
213 acl_check_alg(enum rte_acl_classify_alg alg)
216 case RTE_ACL_CLASSIFY_NEON:
217 return acl_check_alg_arm(alg);
218 case RTE_ACL_CLASSIFY_ALTIVEC:
219 return acl_check_alg_ppc(alg);
220 case RTE_ACL_CLASSIFY_AVX512X32:
221 case RTE_ACL_CLASSIFY_AVX512X16:
222 case RTE_ACL_CLASSIFY_AVX2:
223 case RTE_ACL_CLASSIFY_SSE:
224 return acl_check_alg_x86(alg);
225 /* scalar method is supported on all platforms */
226 case RTE_ACL_CLASSIFY_SCALAR:
234 * Get preferred alg for given platform.
236 static enum rte_acl_classify_alg
237 acl_get_best_alg(void)
240 * array of supported methods for each platform.
241 * Note that order is important - from most to less preferable.
243 static const enum rte_acl_classify_alg alg[] = {
244 #if defined(RTE_ARCH_ARM)
245 RTE_ACL_CLASSIFY_NEON,
246 #elif defined(RTE_ARCH_PPC_64)
247 RTE_ACL_CLASSIFY_ALTIVEC,
248 #elif defined(RTE_ARCH_X86)
249 RTE_ACL_CLASSIFY_AVX512X32,
250 RTE_ACL_CLASSIFY_AVX512X16,
251 RTE_ACL_CLASSIFY_AVX2,
252 RTE_ACL_CLASSIFY_SSE,
254 RTE_ACL_CLASSIFY_SCALAR,
259 /* find best possible alg */
260 for (i = 0; i != RTE_DIM(alg) && acl_check_alg(alg[i]) != 0; i++)
263 /* we always have to find something suitable */
264 RTE_VERIFY(i != RTE_DIM(alg));
269 rte_acl_set_ctx_classify(struct rte_acl_ctx *ctx, enum rte_acl_classify_alg alg)
273 /* formal parameters check */
274 if (ctx == NULL || (uint32_t)alg >= RTE_DIM(classify_fns))
277 /* user asked us to select the *best* one */
278 if (alg == RTE_ACL_CLASSIFY_DEFAULT)
279 alg = acl_get_best_alg();
281 /* check that given alg is supported */
282 rc = acl_check_alg(alg);
291 rte_acl_classify_alg(const struct rte_acl_ctx *ctx, const uint8_t **data,
292 uint32_t *results, uint32_t num, uint32_t categories,
293 enum rte_acl_classify_alg alg)
295 if (categories != 1 &&
296 ((RTE_ACL_RESULTS_MULTIPLIER - 1) & categories) != 0)
299 return classify_fns[alg](ctx, data, results, num, categories);
303 rte_acl_classify(const struct rte_acl_ctx *ctx, const uint8_t **data,
304 uint32_t *results, uint32_t num, uint32_t categories)
306 return rte_acl_classify_alg(ctx, data, results, num, categories,
311 rte_acl_find_existing(const char *name)
313 struct rte_acl_ctx *ctx = NULL;
314 struct rte_acl_list *acl_list;
315 struct rte_tailq_entry *te;
317 acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
319 rte_mcfg_tailq_read_lock();
320 TAILQ_FOREACH(te, acl_list, next) {
321 ctx = (struct rte_acl_ctx *) te->data;
322 if (strncmp(name, ctx->name, sizeof(ctx->name)) == 0)
325 rte_mcfg_tailq_read_unlock();
335 rte_acl_free(struct rte_acl_ctx *ctx)
337 struct rte_acl_list *acl_list;
338 struct rte_tailq_entry *te;
343 acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
345 rte_mcfg_tailq_write_lock();
347 /* find our tailq entry */
348 TAILQ_FOREACH(te, acl_list, next) {
349 if (te->data == (void *) ctx)
353 rte_mcfg_tailq_write_unlock();
357 TAILQ_REMOVE(acl_list, te, next);
359 rte_mcfg_tailq_write_unlock();
367 rte_acl_create(const struct rte_acl_param *param)
370 struct rte_acl_ctx *ctx;
371 struct rte_acl_list *acl_list;
372 struct rte_tailq_entry *te;
373 char name[sizeof(ctx->name)];
375 acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
377 /* check that input parameters are valid. */
378 if (param == NULL || param->name == NULL) {
383 snprintf(name, sizeof(name), "ACL_%s", param->name);
385 /* calculate amount of memory required for pattern set. */
386 sz = sizeof(*ctx) + param->max_rule_num * param->rule_size;
388 /* get EAL TAILQ lock. */
389 rte_mcfg_tailq_write_lock();
391 /* if we already have one with that name */
392 TAILQ_FOREACH(te, acl_list, next) {
393 ctx = (struct rte_acl_ctx *) te->data;
394 if (strncmp(param->name, ctx->name, sizeof(ctx->name)) == 0)
398 /* if ACL with such name doesn't exist, then create a new one. */
401 te = rte_zmalloc("ACL_TAILQ_ENTRY", sizeof(*te), 0);
404 RTE_LOG(ERR, ACL, "Cannot allocate tailq entry!\n");
408 ctx = rte_zmalloc_socket(name, sz, RTE_CACHE_LINE_SIZE, param->socket_id);
412 "allocation of %zu bytes on socket %d for %s failed\n",
413 sz, param->socket_id, name);
417 /* init new allocated context. */
418 ctx->rules = ctx + 1;
419 ctx->max_rules = param->max_rule_num;
420 ctx->rule_sz = param->rule_size;
421 ctx->socket_id = param->socket_id;
422 ctx->alg = acl_get_best_alg();
423 strlcpy(ctx->name, param->name, sizeof(ctx->name));
425 te->data = (void *) ctx;
427 TAILQ_INSERT_TAIL(acl_list, te, next);
431 rte_mcfg_tailq_write_unlock();
436 acl_add_rules(struct rte_acl_ctx *ctx, const void *rules, uint32_t num)
440 if (num + ctx->num_rules > ctx->max_rules)
444 pos += ctx->rule_sz * ctx->num_rules;
445 memcpy(pos, rules, num * ctx->rule_sz);
446 ctx->num_rules += num;
452 acl_check_rule(const struct rte_acl_rule_data *rd)
454 if ((RTE_LEN2MASK(RTE_ACL_MAX_CATEGORIES, typeof(rd->category_mask)) &
455 rd->category_mask) == 0 ||
456 rd->priority > RTE_ACL_MAX_PRIORITY ||
457 rd->priority < RTE_ACL_MIN_PRIORITY)
463 rte_acl_add_rules(struct rte_acl_ctx *ctx, const struct rte_acl_rule *rules,
466 const struct rte_acl_rule *rv;
470 if (ctx == NULL || rules == NULL || 0 == ctx->rule_sz)
473 for (i = 0; i != num; i++) {
474 rv = (const struct rte_acl_rule *)
475 ((uintptr_t)rules + i * ctx->rule_sz);
476 rc = acl_check_rule(&rv->data);
478 RTE_LOG(ERR, ACL, "%s(%s): rule #%u is invalid\n",
479 __func__, ctx->name, i + 1);
484 return acl_add_rules(ctx, rules, num);
489 * Note that RT structures are not affected.
492 rte_acl_reset_rules(struct rte_acl_ctx *ctx)
499 * Reset all rules and destroys RT structures.
502 rte_acl_reset(struct rte_acl_ctx *ctx)
505 rte_acl_reset_rules(ctx);
506 rte_acl_build(ctx, &ctx->config);
511 * Dump ACL context to the stdout.
514 rte_acl_dump(const struct rte_acl_ctx *ctx)
518 printf("acl context <%s>@%p\n", ctx->name, ctx);
519 printf(" socket_id=%"PRId32"\n", ctx->socket_id);
520 printf(" alg=%"PRId32"\n", ctx->alg);
521 printf(" first_load_sz=%"PRIu32"\n", ctx->first_load_sz);
522 printf(" max_rules=%"PRIu32"\n", ctx->max_rules);
523 printf(" rule_size=%"PRIu32"\n", ctx->rule_sz);
524 printf(" num_rules=%"PRIu32"\n", ctx->num_rules);
525 printf(" num_categories=%"PRIu32"\n", ctx->num_categories);
526 printf(" num_tries=%"PRIu32"\n", ctx->num_tries);
530 * Dump all ACL contexts to the stdout.
533 rte_acl_list_dump(void)
535 struct rte_acl_ctx *ctx;
536 struct rte_acl_list *acl_list;
537 struct rte_tailq_entry *te;
539 acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
541 rte_mcfg_tailq_read_lock();
542 TAILQ_FOREACH(te, acl_list, next) {
543 ctx = (struct rte_acl_ctx *) te->data;
546 rte_mcfg_tailq_read_unlock();