bd7247cc3d502257cf6b882c87bfa3ba2ea6c6e4
[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_string_fns.h>
6 #include <rte_acl.h>
7 #include <rte_tailq.h>
8
9 #include "acl.h"
10
11 TAILQ_HEAD(rte_acl_list, rte_tailq_entry);
12
13 static struct rte_tailq_elem rte_acl_tailq = {
14         .name = "RTE_ACL",
15 };
16 EAL_REGISTER_TAILQ(rte_acl_tailq)
17
18 #ifndef RTE_ARCH_X86
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 int
36 rte_acl_classify_sse(__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)
41 {
42         return -ENOTSUP;
43 }
44 #endif
45
46 #ifndef RTE_ARCH_ARM
47 #ifndef RTE_ARCH_ARM64
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 #endif
59
60 #ifndef RTE_ARCH_PPC_64
61 int
62 rte_acl_classify_altivec(__rte_unused const struct rte_acl_ctx *ctx,
63         __rte_unused const uint8_t **data,
64         __rte_unused uint32_t *results,
65         __rte_unused uint32_t num,
66         __rte_unused uint32_t categories)
67 {
68         return -ENOTSUP;
69 }
70 #endif
71
72 static const rte_acl_classify_t classify_fns[] = {
73         [RTE_ACL_CLASSIFY_DEFAULT] = rte_acl_classify_scalar,
74         [RTE_ACL_CLASSIFY_SCALAR] = rte_acl_classify_scalar,
75         [RTE_ACL_CLASSIFY_SSE] = rte_acl_classify_sse,
76         [RTE_ACL_CLASSIFY_AVX2] = rte_acl_classify_avx2,
77         [RTE_ACL_CLASSIFY_NEON] = rte_acl_classify_neon,
78         [RTE_ACL_CLASSIFY_ALTIVEC] = rte_acl_classify_altivec,
79 };
80
81 /* by default, use always available scalar code path. */
82 static enum rte_acl_classify_alg rte_acl_default_classify =
83         RTE_ACL_CLASSIFY_SCALAR;
84
85 static void
86 rte_acl_set_default_classify(enum rte_acl_classify_alg alg)
87 {
88         rte_acl_default_classify = alg;
89 }
90
91 extern int
92 rte_acl_set_ctx_classify(struct rte_acl_ctx *ctx, enum rte_acl_classify_alg alg)
93 {
94         if (ctx == NULL || (uint32_t)alg >= RTE_DIM(classify_fns))
95                 return -EINVAL;
96
97         ctx->alg = alg;
98         return 0;
99 }
100
101 /*
102  * Select highest available classify method as default one.
103  * Note that CLASSIFY_AVX2 should be set as a default only
104  * if both conditions are met:
105  * at build time compiler supports AVX2 and target cpu supports AVX2.
106  */
107 RTE_INIT(rte_acl_init)
108 {
109         enum rte_acl_classify_alg alg = RTE_ACL_CLASSIFY_DEFAULT;
110
111 #if defined(RTE_ARCH_ARM64)
112         alg =  RTE_ACL_CLASSIFY_NEON;
113 #elif defined(RTE_ARCH_ARM)
114         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON))
115                 alg =  RTE_ACL_CLASSIFY_NEON;
116 #elif defined(RTE_ARCH_PPC_64)
117         alg = RTE_ACL_CLASSIFY_ALTIVEC;
118 #else
119 #ifdef CC_AVX2_SUPPORT
120         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
121                 alg = RTE_ACL_CLASSIFY_AVX2;
122         else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
123 #else
124         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
125 #endif
126                 alg = RTE_ACL_CLASSIFY_SSE;
127
128 #endif
129         rte_acl_set_default_classify(alg);
130 }
131
132 int
133 rte_acl_classify_alg(const struct rte_acl_ctx *ctx, const uint8_t **data,
134         uint32_t *results, uint32_t num, uint32_t categories,
135         enum rte_acl_classify_alg alg)
136 {
137         if (categories != 1 &&
138                         ((RTE_ACL_RESULTS_MULTIPLIER - 1) & categories) != 0)
139                 return -EINVAL;
140
141         return classify_fns[alg](ctx, data, results, num, categories);
142 }
143
144 int
145 rte_acl_classify(const struct rte_acl_ctx *ctx, const uint8_t **data,
146         uint32_t *results, uint32_t num, uint32_t categories)
147 {
148         return rte_acl_classify_alg(ctx, data, results, num, categories,
149                 ctx->alg);
150 }
151
152 struct rte_acl_ctx *
153 rte_acl_find_existing(const char *name)
154 {
155         struct rte_acl_ctx *ctx = NULL;
156         struct rte_acl_list *acl_list;
157         struct rte_tailq_entry *te;
158
159         acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
160
161         rte_mcfg_tailq_read_lock();
162         TAILQ_FOREACH(te, acl_list, next) {
163                 ctx = (struct rte_acl_ctx *) te->data;
164                 if (strncmp(name, ctx->name, sizeof(ctx->name)) == 0)
165                         break;
166         }
167         rte_mcfg_tailq_read_unlock();
168
169         if (te == NULL) {
170                 rte_errno = ENOENT;
171                 return NULL;
172         }
173         return ctx;
174 }
175
176 void
177 rte_acl_free(struct rte_acl_ctx *ctx)
178 {
179         struct rte_acl_list *acl_list;
180         struct rte_tailq_entry *te;
181
182         if (ctx == NULL)
183                 return;
184
185         acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
186
187         rte_mcfg_tailq_write_lock();
188
189         /* find our tailq entry */
190         TAILQ_FOREACH(te, acl_list, next) {
191                 if (te->data == (void *) ctx)
192                         break;
193         }
194         if (te == NULL) {
195                 rte_mcfg_tailq_write_unlock();
196                 return;
197         }
198
199         TAILQ_REMOVE(acl_list, te, next);
200
201         rte_mcfg_tailq_write_unlock();
202
203         rte_free(ctx->mem);
204         rte_free(ctx);
205         rte_free(te);
206 }
207
208 struct rte_acl_ctx *
209 rte_acl_create(const struct rte_acl_param *param)
210 {
211         size_t sz;
212         struct rte_acl_ctx *ctx;
213         struct rte_acl_list *acl_list;
214         struct rte_tailq_entry *te;
215         char name[sizeof(ctx->name)];
216
217         acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
218
219         /* check that input parameters are valid. */
220         if (param == NULL || param->name == NULL) {
221                 rte_errno = EINVAL;
222                 return NULL;
223         }
224
225         snprintf(name, sizeof(name), "ACL_%s", param->name);
226
227         /* calculate amount of memory required for pattern set. */
228         sz = sizeof(*ctx) + param->max_rule_num * param->rule_size;
229
230         /* get EAL TAILQ lock. */
231         rte_mcfg_tailq_write_lock();
232
233         /* if we already have one with that name */
234         TAILQ_FOREACH(te, acl_list, next) {
235                 ctx = (struct rte_acl_ctx *) te->data;
236                 if (strncmp(param->name, ctx->name, sizeof(ctx->name)) == 0)
237                         break;
238         }
239
240         /* if ACL with such name doesn't exist, then create a new one. */
241         if (te == NULL) {
242                 ctx = NULL;
243                 te = rte_zmalloc("ACL_TAILQ_ENTRY", sizeof(*te), 0);
244
245                 if (te == NULL) {
246                         RTE_LOG(ERR, ACL, "Cannot allocate tailq entry!\n");
247                         goto exit;
248                 }
249
250                 ctx = rte_zmalloc_socket(name, sz, RTE_CACHE_LINE_SIZE, param->socket_id);
251
252                 if (ctx == NULL) {
253                         RTE_LOG(ERR, ACL,
254                                 "allocation of %zu bytes on socket %d for %s failed\n",
255                                 sz, param->socket_id, name);
256                         rte_free(te);
257                         goto exit;
258                 }
259                 /* init new allocated context. */
260                 ctx->rules = ctx + 1;
261                 ctx->max_rules = param->max_rule_num;
262                 ctx->rule_sz = param->rule_size;
263                 ctx->socket_id = param->socket_id;
264                 ctx->alg = rte_acl_default_classify;
265                 strlcpy(ctx->name, param->name, sizeof(ctx->name));
266
267                 te->data = (void *) ctx;
268
269                 TAILQ_INSERT_TAIL(acl_list, te, next);
270         }
271
272 exit:
273         rte_mcfg_tailq_write_unlock();
274         return ctx;
275 }
276
277 static int
278 acl_add_rules(struct rte_acl_ctx *ctx, const void *rules, uint32_t num)
279 {
280         uint8_t *pos;
281
282         if (num + ctx->num_rules > ctx->max_rules)
283                 return -ENOMEM;
284
285         pos = ctx->rules;
286         pos += ctx->rule_sz * ctx->num_rules;
287         memcpy(pos, rules, num * ctx->rule_sz);
288         ctx->num_rules += num;
289
290         return 0;
291 }
292
293 static int
294 acl_check_rule(const struct rte_acl_rule_data *rd)
295 {
296         if ((RTE_LEN2MASK(RTE_ACL_MAX_CATEGORIES, typeof(rd->category_mask)) &
297                         rd->category_mask) == 0 ||
298                         rd->priority > RTE_ACL_MAX_PRIORITY ||
299                         rd->priority < RTE_ACL_MIN_PRIORITY)
300                 return -EINVAL;
301         return 0;
302 }
303
304 int
305 rte_acl_add_rules(struct rte_acl_ctx *ctx, const struct rte_acl_rule *rules,
306         uint32_t num)
307 {
308         const struct rte_acl_rule *rv;
309         uint32_t i;
310         int32_t rc;
311
312         if (ctx == NULL || rules == NULL || 0 == ctx->rule_sz)
313                 return -EINVAL;
314
315         for (i = 0; i != num; i++) {
316                 rv = (const struct rte_acl_rule *)
317                         ((uintptr_t)rules + i * ctx->rule_sz);
318                 rc = acl_check_rule(&rv->data);
319                 if (rc != 0) {
320                         RTE_LOG(ERR, ACL, "%s(%s): rule #%u is invalid\n",
321                                 __func__, ctx->name, i + 1);
322                         return rc;
323                 }
324         }
325
326         return acl_add_rules(ctx, rules, num);
327 }
328
329 /*
330  * Reset all rules.
331  * Note that RT structures are not affected.
332  */
333 void
334 rte_acl_reset_rules(struct rte_acl_ctx *ctx)
335 {
336         if (ctx != NULL)
337                 ctx->num_rules = 0;
338 }
339
340 /*
341  * Reset all rules and destroys RT structures.
342  */
343 void
344 rte_acl_reset(struct rte_acl_ctx *ctx)
345 {
346         if (ctx != NULL) {
347                 rte_acl_reset_rules(ctx);
348                 rte_acl_build(ctx, &ctx->config);
349         }
350 }
351
352 /*
353  * Dump ACL context to the stdout.
354  */
355 void
356 rte_acl_dump(const struct rte_acl_ctx *ctx)
357 {
358         if (!ctx)
359                 return;
360         printf("acl context <%s>@%p\n", ctx->name, ctx);
361         printf("  socket_id=%"PRId32"\n", ctx->socket_id);
362         printf("  alg=%"PRId32"\n", ctx->alg);
363         printf("  max_rules=%"PRIu32"\n", ctx->max_rules);
364         printf("  rule_size=%"PRIu32"\n", ctx->rule_sz);
365         printf("  num_rules=%"PRIu32"\n", ctx->num_rules);
366         printf("  num_categories=%"PRIu32"\n", ctx->num_categories);
367         printf("  num_tries=%"PRIu32"\n", ctx->num_tries);
368 }
369
370 /*
371  * Dump all ACL contexts to the stdout.
372  */
373 void
374 rte_acl_list_dump(void)
375 {
376         struct rte_acl_ctx *ctx;
377         struct rte_acl_list *acl_list;
378         struct rte_tailq_entry *te;
379
380         acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
381
382         rte_mcfg_tailq_read_lock();
383         TAILQ_FOREACH(te, acl_list, next) {
384                 ctx = (struct rte_acl_ctx *) te->data;
385                 rte_acl_dump(ctx);
386         }
387         rte_mcfg_tailq_read_unlock();
388 }