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