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