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