acl: select 256-bit AVX512 classify method by default
[dpdk.git] / lib / librte_acl / rte_acl.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <rte_eal_memconfig.h>
6 #include <rte_string_fns.h>
7 #include <rte_acl.h>
8 #include <rte_tailq.h>
9
10 #include "acl.h"
11
12 TAILQ_HEAD(rte_acl_list, rte_tailq_entry);
13
14 static struct rte_tailq_elem rte_acl_tailq = {
15         .name = "RTE_ACL",
16 };
17 EAL_REGISTER_TAILQ(rte_acl_tailq)
18
19 #ifndef CC_AVX512_SUPPORT
20 /*
21  * If the compiler doesn't support AVX512 instructions,
22  * then the dummy one would be used instead for AVX512 classify method.
23  */
24 int
25 rte_acl_classify_avx512x16(__rte_unused const struct rte_acl_ctx *ctx,
26         __rte_unused const uint8_t **data,
27         __rte_unused uint32_t *results,
28         __rte_unused uint32_t num,
29         __rte_unused uint32_t categories)
30 {
31         return -ENOTSUP;
32 }
33
34 int
35 rte_acl_classify_avx512x32(__rte_unused const struct rte_acl_ctx *ctx,
36         __rte_unused const uint8_t **data,
37         __rte_unused uint32_t *results,
38         __rte_unused uint32_t num,
39         __rte_unused uint32_t categories)
40 {
41         return -ENOTSUP;
42 }
43 #endif
44
45 #ifndef CC_AVX2_SUPPORT
46 /*
47  * If the compiler doesn't support AVX2 instructions,
48  * then the dummy one would be used instead for AVX2 classify method.
49  */
50 int
51 rte_acl_classify_avx2(__rte_unused const struct rte_acl_ctx *ctx,
52         __rte_unused const uint8_t **data,
53         __rte_unused uint32_t *results,
54         __rte_unused uint32_t num,
55         __rte_unused uint32_t categories)
56 {
57         return -ENOTSUP;
58 }
59 #endif
60
61 #ifndef RTE_ARCH_X86
62 int
63 rte_acl_classify_sse(__rte_unused const struct rte_acl_ctx *ctx,
64         __rte_unused const uint8_t **data,
65         __rte_unused uint32_t *results,
66         __rte_unused uint32_t num,
67         __rte_unused uint32_t categories)
68 {
69         return -ENOTSUP;
70 }
71 #endif
72
73 #ifndef RTE_ARCH_ARM
74 int
75 rte_acl_classify_neon(__rte_unused const struct rte_acl_ctx *ctx,
76         __rte_unused const uint8_t **data,
77         __rte_unused uint32_t *results,
78         __rte_unused uint32_t num,
79         __rte_unused uint32_t categories)
80 {
81         return -ENOTSUP;
82 }
83 #endif
84
85 #ifndef RTE_ARCH_PPC_64
86 int
87 rte_acl_classify_altivec(__rte_unused const struct rte_acl_ctx *ctx,
88         __rte_unused const uint8_t **data,
89         __rte_unused uint32_t *results,
90         __rte_unused uint32_t num,
91         __rte_unused uint32_t categories)
92 {
93         return -ENOTSUP;
94 }
95 #endif
96
97 static const rte_acl_classify_t classify_fns[] = {
98         [RTE_ACL_CLASSIFY_DEFAULT] = rte_acl_classify_scalar,
99         [RTE_ACL_CLASSIFY_SCALAR] = rte_acl_classify_scalar,
100         [RTE_ACL_CLASSIFY_SSE] = rte_acl_classify_sse,
101         [RTE_ACL_CLASSIFY_AVX2] = rte_acl_classify_avx2,
102         [RTE_ACL_CLASSIFY_NEON] = rte_acl_classify_neon,
103         [RTE_ACL_CLASSIFY_ALTIVEC] = rte_acl_classify_altivec,
104         [RTE_ACL_CLASSIFY_AVX512X16] = rte_acl_classify_avx512x16,
105         [RTE_ACL_CLASSIFY_AVX512X32] = rte_acl_classify_avx512x32,
106 };
107
108 /*
109  * Helper function for acl_check_alg.
110  * Check support for ARM specific classify methods.
111  */
112 static int
113 acl_check_alg_arm(enum rte_acl_classify_alg alg)
114 {
115         if (alg == RTE_ACL_CLASSIFY_NEON) {
116 #if defined(RTE_ARCH_ARM64)
117                 return 0;
118 #elif defined(RTE_ARCH_ARM)
119                 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON))
120                         return 0;
121                 return -ENOTSUP;
122 #else
123                 return -ENOTSUP;
124 #endif
125         }
126
127         return -EINVAL;
128 }
129
130 /*
131  * Helper function for acl_check_alg.
132  * Check support for PPC specific classify methods.
133  */
134 static int
135 acl_check_alg_ppc(enum rte_acl_classify_alg alg)
136 {
137         if (alg == RTE_ACL_CLASSIFY_ALTIVEC) {
138 #if defined(RTE_ARCH_PPC_64)
139                 return 0;
140 #else
141                 return -ENOTSUP;
142 #endif
143         }
144
145         return -EINVAL;
146 }
147
148 /*
149  * Helper function for acl_check_alg.
150  * Check support for x86 specific classify methods.
151  */
152 static int
153 acl_check_alg_x86(enum rte_acl_classify_alg alg)
154 {
155         if (alg == RTE_ACL_CLASSIFY_AVX512X16 ||
156                         alg == RTE_ACL_CLASSIFY_AVX512X32) {
157 #ifdef CC_AVX512_SUPPORT
158                 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F) &&
159                         rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512VL) &&
160                         rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512CD) &&
161                         rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512BW))
162                         return 0;
163 #endif
164                 return -ENOTSUP;
165         }
166
167         if (alg == RTE_ACL_CLASSIFY_AVX2) {
168 #ifdef CC_AVX2_SUPPORT
169                 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
170                         return 0;
171 #endif
172                 return -ENOTSUP;
173         }
174
175         if (alg == RTE_ACL_CLASSIFY_SSE) {
176 #ifdef RTE_ARCH_X86
177                 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
178                         return 0;
179 #endif
180                 return -ENOTSUP;
181         }
182
183         return -EINVAL;
184 }
185
186 /*
187  * Check if input alg is supported by given platform/binary.
188  * Note that both conditions should be met:
189  * - at build time compiler supports ISA used by given methods
190  * - at run time target cpu supports necessary ISA.
191  */
192 static int
193 acl_check_alg(enum rte_acl_classify_alg alg)
194 {
195         switch (alg) {
196         case RTE_ACL_CLASSIFY_NEON:
197                 return acl_check_alg_arm(alg);
198         case RTE_ACL_CLASSIFY_ALTIVEC:
199                 return acl_check_alg_ppc(alg);
200         case RTE_ACL_CLASSIFY_AVX512X32:
201         case RTE_ACL_CLASSIFY_AVX512X16:
202         case RTE_ACL_CLASSIFY_AVX2:
203         case RTE_ACL_CLASSIFY_SSE:
204                 return acl_check_alg_x86(alg);
205         /* scalar method is supported on all platforms */
206         case RTE_ACL_CLASSIFY_SCALAR:
207                 return 0;
208         default:
209                 return -EINVAL;
210         }
211 }
212
213 /*
214  * Get preferred alg for given platform.
215  */
216 static enum rte_acl_classify_alg
217 acl_get_best_alg(void)
218 {
219         /*
220          * array of supported methods for each platform.
221          * Note that order is important - from most to less preferable.
222          */
223         static const enum rte_acl_classify_alg alg[] = {
224 #if defined(RTE_ARCH_ARM)
225                 RTE_ACL_CLASSIFY_NEON,
226 #elif defined(RTE_ARCH_PPC_64)
227                 RTE_ACL_CLASSIFY_ALTIVEC,
228 #elif defined(RTE_ARCH_X86)
229                 RTE_ACL_CLASSIFY_AVX512X16,
230                 RTE_ACL_CLASSIFY_AVX2,
231                 RTE_ACL_CLASSIFY_SSE,
232 #endif
233                 RTE_ACL_CLASSIFY_SCALAR,
234         };
235
236         uint32_t i;
237
238         /* find best possible alg */
239         for (i = 0; i != RTE_DIM(alg) && acl_check_alg(alg[i]) != 0; i++)
240                 ;
241
242         /* we always have to find something suitable */
243         RTE_VERIFY(i != RTE_DIM(alg));
244         return alg[i];
245 }
246
247 extern int
248 rte_acl_set_ctx_classify(struct rte_acl_ctx *ctx, enum rte_acl_classify_alg alg)
249 {
250         int32_t rc;
251
252         /* formal parameters check */
253         if (ctx == NULL || (uint32_t)alg >= RTE_DIM(classify_fns))
254                 return -EINVAL;
255
256         /* user asked us to select the *best* one */
257         if (alg == RTE_ACL_CLASSIFY_DEFAULT)
258                 alg = acl_get_best_alg();
259
260         /* check that given alg is supported */
261         rc = acl_check_alg(alg);
262         if (rc != 0)
263                 return rc;
264
265         ctx->alg = alg;
266         return 0;
267 }
268
269 int
270 rte_acl_classify_alg(const struct rte_acl_ctx *ctx, const uint8_t **data,
271         uint32_t *results, uint32_t num, uint32_t categories,
272         enum rte_acl_classify_alg alg)
273 {
274         if (categories != 1 &&
275                         ((RTE_ACL_RESULTS_MULTIPLIER - 1) & categories) != 0)
276                 return -EINVAL;
277
278         return classify_fns[alg](ctx, data, results, num, categories);
279 }
280
281 int
282 rte_acl_classify(const struct rte_acl_ctx *ctx, const uint8_t **data,
283         uint32_t *results, uint32_t num, uint32_t categories)
284 {
285         return rte_acl_classify_alg(ctx, data, results, num, categories,
286                 ctx->alg);
287 }
288
289 struct rte_acl_ctx *
290 rte_acl_find_existing(const char *name)
291 {
292         struct rte_acl_ctx *ctx = NULL;
293         struct rte_acl_list *acl_list;
294         struct rte_tailq_entry *te;
295
296         acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
297
298         rte_mcfg_tailq_read_lock();
299         TAILQ_FOREACH(te, acl_list, next) {
300                 ctx = (struct rte_acl_ctx *) te->data;
301                 if (strncmp(name, ctx->name, sizeof(ctx->name)) == 0)
302                         break;
303         }
304         rte_mcfg_tailq_read_unlock();
305
306         if (te == NULL) {
307                 rte_errno = ENOENT;
308                 return NULL;
309         }
310         return ctx;
311 }
312
313 void
314 rte_acl_free(struct rte_acl_ctx *ctx)
315 {
316         struct rte_acl_list *acl_list;
317         struct rte_tailq_entry *te;
318
319         if (ctx == NULL)
320                 return;
321
322         acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
323
324         rte_mcfg_tailq_write_lock();
325
326         /* find our tailq entry */
327         TAILQ_FOREACH(te, acl_list, next) {
328                 if (te->data == (void *) ctx)
329                         break;
330         }
331         if (te == NULL) {
332                 rte_mcfg_tailq_write_unlock();
333                 return;
334         }
335
336         TAILQ_REMOVE(acl_list, te, next);
337
338         rte_mcfg_tailq_write_unlock();
339
340         rte_free(ctx->mem);
341         rte_free(ctx);
342         rte_free(te);
343 }
344
345 struct rte_acl_ctx *
346 rte_acl_create(const struct rte_acl_param *param)
347 {
348         size_t sz;
349         struct rte_acl_ctx *ctx;
350         struct rte_acl_list *acl_list;
351         struct rte_tailq_entry *te;
352         char name[sizeof(ctx->name)];
353
354         acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
355
356         /* check that input parameters are valid. */
357         if (param == NULL || param->name == NULL) {
358                 rte_errno = EINVAL;
359                 return NULL;
360         }
361
362         snprintf(name, sizeof(name), "ACL_%s", param->name);
363
364         /* calculate amount of memory required for pattern set. */
365         sz = sizeof(*ctx) + param->max_rule_num * param->rule_size;
366
367         /* get EAL TAILQ lock. */
368         rte_mcfg_tailq_write_lock();
369
370         /* if we already have one with that name */
371         TAILQ_FOREACH(te, acl_list, next) {
372                 ctx = (struct rte_acl_ctx *) te->data;
373                 if (strncmp(param->name, ctx->name, sizeof(ctx->name)) == 0)
374                         break;
375         }
376
377         /* if ACL with such name doesn't exist, then create a new one. */
378         if (te == NULL) {
379                 ctx = NULL;
380                 te = rte_zmalloc("ACL_TAILQ_ENTRY", sizeof(*te), 0);
381
382                 if (te == NULL) {
383                         RTE_LOG(ERR, ACL, "Cannot allocate tailq entry!\n");
384                         goto exit;
385                 }
386
387                 ctx = rte_zmalloc_socket(name, sz, RTE_CACHE_LINE_SIZE, param->socket_id);
388
389                 if (ctx == NULL) {
390                         RTE_LOG(ERR, ACL,
391                                 "allocation of %zu bytes on socket %d for %s failed\n",
392                                 sz, param->socket_id, name);
393                         rte_free(te);
394                         goto exit;
395                 }
396                 /* init new allocated context. */
397                 ctx->rules = ctx + 1;
398                 ctx->max_rules = param->max_rule_num;
399                 ctx->rule_sz = param->rule_size;
400                 ctx->socket_id = param->socket_id;
401                 ctx->alg = acl_get_best_alg();
402                 strlcpy(ctx->name, param->name, sizeof(ctx->name));
403
404                 te->data = (void *) ctx;
405
406                 TAILQ_INSERT_TAIL(acl_list, te, next);
407         }
408
409 exit:
410         rte_mcfg_tailq_write_unlock();
411         return ctx;
412 }
413
414 static int
415 acl_add_rules(struct rte_acl_ctx *ctx, const void *rules, uint32_t num)
416 {
417         uint8_t *pos;
418
419         if (num + ctx->num_rules > ctx->max_rules)
420                 return -ENOMEM;
421
422         pos = ctx->rules;
423         pos += ctx->rule_sz * ctx->num_rules;
424         memcpy(pos, rules, num * ctx->rule_sz);
425         ctx->num_rules += num;
426
427         return 0;
428 }
429
430 static int
431 acl_check_rule(const struct rte_acl_rule_data *rd)
432 {
433         if ((RTE_LEN2MASK(RTE_ACL_MAX_CATEGORIES, typeof(rd->category_mask)) &
434                         rd->category_mask) == 0 ||
435                         rd->priority > RTE_ACL_MAX_PRIORITY ||
436                         rd->priority < RTE_ACL_MIN_PRIORITY)
437                 return -EINVAL;
438         return 0;
439 }
440
441 int
442 rte_acl_add_rules(struct rte_acl_ctx *ctx, const struct rte_acl_rule *rules,
443         uint32_t num)
444 {
445         const struct rte_acl_rule *rv;
446         uint32_t i;
447         int32_t rc;
448
449         if (ctx == NULL || rules == NULL || 0 == ctx->rule_sz)
450                 return -EINVAL;
451
452         for (i = 0; i != num; i++) {
453                 rv = (const struct rte_acl_rule *)
454                         ((uintptr_t)rules + i * ctx->rule_sz);
455                 rc = acl_check_rule(&rv->data);
456                 if (rc != 0) {
457                         RTE_LOG(ERR, ACL, "%s(%s): rule #%u is invalid\n",
458                                 __func__, ctx->name, i + 1);
459                         return rc;
460                 }
461         }
462
463         return acl_add_rules(ctx, rules, num);
464 }
465
466 /*
467  * Reset all rules.
468  * Note that RT structures are not affected.
469  */
470 void
471 rte_acl_reset_rules(struct rte_acl_ctx *ctx)
472 {
473         if (ctx != NULL)
474                 ctx->num_rules = 0;
475 }
476
477 /*
478  * Reset all rules and destroys RT structures.
479  */
480 void
481 rte_acl_reset(struct rte_acl_ctx *ctx)
482 {
483         if (ctx != NULL) {
484                 rte_acl_reset_rules(ctx);
485                 rte_acl_build(ctx, &ctx->config);
486         }
487 }
488
489 /*
490  * Dump ACL context to the stdout.
491  */
492 void
493 rte_acl_dump(const struct rte_acl_ctx *ctx)
494 {
495         if (!ctx)
496                 return;
497         printf("acl context <%s>@%p\n", ctx->name, ctx);
498         printf("  socket_id=%"PRId32"\n", ctx->socket_id);
499         printf("  alg=%"PRId32"\n", ctx->alg);
500         printf("  max_rules=%"PRIu32"\n", ctx->max_rules);
501         printf("  rule_size=%"PRIu32"\n", ctx->rule_sz);
502         printf("  num_rules=%"PRIu32"\n", ctx->num_rules);
503         printf("  num_categories=%"PRIu32"\n", ctx->num_categories);
504         printf("  num_tries=%"PRIu32"\n", ctx->num_tries);
505 }
506
507 /*
508  * Dump all ACL contexts to the stdout.
509  */
510 void
511 rte_acl_list_dump(void)
512 {
513         struct rte_acl_ctx *ctx;
514         struct rte_acl_list *acl_list;
515         struct rte_tailq_entry *te;
516
517         acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
518
519         rte_mcfg_tailq_read_lock();
520         TAILQ_FOREACH(te, acl_list, next) {
521                 ctx = (struct rte_acl_ctx *) te->data;
522                 rte_acl_dump(ctx);
523         }
524         rte_mcfg_tailq_read_unlock();
525 }