Imported Upstream version 16.04
[deb_dpdk.git] / lib / librte_acl / rte_acl.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <rte_acl.h>
35 #include "acl.h"
36
37 TAILQ_HEAD(rte_acl_list, rte_tailq_entry);
38
39 static struct rte_tailq_elem rte_acl_tailq = {
40         .name = "RTE_ACL",
41 };
42 EAL_REGISTER_TAILQ(rte_acl_tailq)
43
44 /*
45  * If the compiler doesn't support AVX2 instructions,
46  * then the dummy one would be used instead for AVX2 classify method.
47  */
48 int __attribute__ ((weak))
49 rte_acl_classify_avx2(__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
58 int __attribute__ ((weak))
59 rte_acl_classify_sse(__rte_unused const struct rte_acl_ctx *ctx,
60         __rte_unused const uint8_t **data,
61         __rte_unused uint32_t *results,
62         __rte_unused uint32_t num,
63         __rte_unused uint32_t categories)
64 {
65         return -ENOTSUP;
66 }
67
68 int __attribute__ ((weak))
69 rte_acl_classify_neon(__rte_unused const struct rte_acl_ctx *ctx,
70         __rte_unused const uint8_t **data,
71         __rte_unused uint32_t *results,
72         __rte_unused uint32_t num,
73         __rte_unused uint32_t categories)
74 {
75         return -ENOTSUP;
76 }
77
78 static const rte_acl_classify_t classify_fns[] = {
79         [RTE_ACL_CLASSIFY_DEFAULT] = rte_acl_classify_scalar,
80         [RTE_ACL_CLASSIFY_SCALAR] = rte_acl_classify_scalar,
81         [RTE_ACL_CLASSIFY_SSE] = rte_acl_classify_sse,
82         [RTE_ACL_CLASSIFY_AVX2] = rte_acl_classify_avx2,
83         [RTE_ACL_CLASSIFY_NEON] = rte_acl_classify_neon,
84 };
85
86 /* by default, use always available scalar code path. */
87 static enum rte_acl_classify_alg rte_acl_default_classify =
88         RTE_ACL_CLASSIFY_SCALAR;
89
90 static void
91 rte_acl_set_default_classify(enum rte_acl_classify_alg alg)
92 {
93         rte_acl_default_classify = alg;
94 }
95
96 extern int
97 rte_acl_set_ctx_classify(struct rte_acl_ctx *ctx, enum rte_acl_classify_alg alg)
98 {
99         if (ctx == NULL || (uint32_t)alg >= RTE_DIM(classify_fns))
100                 return -EINVAL;
101
102         ctx->alg = alg;
103         return 0;
104 }
105
106 /*
107  * Select highest available classify method as default one.
108  * Note that CLASSIFY_AVX2 should be set as a default only
109  * if both conditions are met:
110  * at build time compiler supports AVX2 and target cpu supports AVX2.
111  */
112 static void __attribute__((constructor))
113 rte_acl_init(void)
114 {
115         enum rte_acl_classify_alg alg = RTE_ACL_CLASSIFY_DEFAULT;
116
117 #if defined(RTE_ARCH_ARM64)
118         alg =  RTE_ACL_CLASSIFY_NEON;
119 #elif defined(RTE_ARCH_ARM)
120         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON))
121                 alg =  RTE_ACL_CLASSIFY_NEON;
122 #else
123 #ifdef CC_AVX2_SUPPORT
124         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
125                 alg = RTE_ACL_CLASSIFY_AVX2;
126         else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
127 #else
128         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
129 #endif
130                 alg = RTE_ACL_CLASSIFY_SSE;
131
132 #endif
133         rte_acl_set_default_classify(alg);
134 }
135
136 int
137 rte_acl_classify_alg(const struct rte_acl_ctx *ctx, const uint8_t **data,
138         uint32_t *results, uint32_t num, uint32_t categories,
139         enum rte_acl_classify_alg alg)
140 {
141         if (categories != 1 &&
142                         ((RTE_ACL_RESULTS_MULTIPLIER - 1) & categories) != 0)
143                 return -EINVAL;
144
145         return classify_fns[alg](ctx, data, results, num, categories);
146 }
147
148 int
149 rte_acl_classify(const struct rte_acl_ctx *ctx, const uint8_t **data,
150         uint32_t *results, uint32_t num, uint32_t categories)
151 {
152         return rte_acl_classify_alg(ctx, data, results, num, categories,
153                 ctx->alg);
154 }
155
156 struct rte_acl_ctx *
157 rte_acl_find_existing(const char *name)
158 {
159         struct rte_acl_ctx *ctx = NULL;
160         struct rte_acl_list *acl_list;
161         struct rte_tailq_entry *te;
162
163         acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
164
165         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
166         TAILQ_FOREACH(te, acl_list, next) {
167                 ctx = (struct rte_acl_ctx *) te->data;
168                 if (strncmp(name, ctx->name, sizeof(ctx->name)) == 0)
169                         break;
170         }
171         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
172
173         if (te == NULL) {
174                 rte_errno = ENOENT;
175                 return NULL;
176         }
177         return ctx;
178 }
179
180 void
181 rte_acl_free(struct rte_acl_ctx *ctx)
182 {
183         struct rte_acl_list *acl_list;
184         struct rte_tailq_entry *te;
185
186         if (ctx == NULL)
187                 return;
188
189         acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
190
191         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
192
193         /* find our tailq entry */
194         TAILQ_FOREACH(te, acl_list, next) {
195                 if (te->data == (void *) ctx)
196                         break;
197         }
198         if (te == NULL) {
199                 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
200                 return;
201         }
202
203         TAILQ_REMOVE(acl_list, te, next);
204
205         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
206
207         rte_free(ctx->mem);
208         rte_free(ctx);
209         rte_free(te);
210 }
211
212 struct rte_acl_ctx *
213 rte_acl_create(const struct rte_acl_param *param)
214 {
215         size_t sz;
216         struct rte_acl_ctx *ctx;
217         struct rte_acl_list *acl_list;
218         struct rte_tailq_entry *te;
219         char name[sizeof(ctx->name)];
220
221         acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
222
223         /* check that input parameters are valid. */
224         if (param == NULL || param->name == NULL) {
225                 rte_errno = EINVAL;
226                 return NULL;
227         }
228
229         snprintf(name, sizeof(name), "ACL_%s", param->name);
230
231         /* calculate amount of memory required for pattern set. */
232         sz = sizeof(*ctx) + param->max_rule_num * param->rule_size;
233
234         /* get EAL TAILQ lock. */
235         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
236
237         /* if we already have one with that name */
238         TAILQ_FOREACH(te, acl_list, next) {
239                 ctx = (struct rte_acl_ctx *) te->data;
240                 if (strncmp(param->name, ctx->name, sizeof(ctx->name)) == 0)
241                         break;
242         }
243
244         /* if ACL with such name doesn't exist, then create a new one. */
245         if (te == NULL) {
246                 ctx = NULL;
247                 te = rte_zmalloc("ACL_TAILQ_ENTRY", sizeof(*te), 0);
248
249                 if (te == NULL) {
250                         RTE_LOG(ERR, ACL, "Cannot allocate tailq entry!\n");
251                         goto exit;
252                 }
253
254                 ctx = rte_zmalloc_socket(name, sz, RTE_CACHE_LINE_SIZE, param->socket_id);
255
256                 if (ctx == NULL) {
257                         RTE_LOG(ERR, ACL,
258                                 "allocation of %zu bytes on socket %d for %s failed\n",
259                                 sz, param->socket_id, name);
260                         rte_free(te);
261                         goto exit;
262                 }
263                 /* init new allocated context. */
264                 ctx->rules = ctx + 1;
265                 ctx->max_rules = param->max_rule_num;
266                 ctx->rule_sz = param->rule_size;
267                 ctx->socket_id = param->socket_id;
268                 ctx->alg = rte_acl_default_classify;
269                 snprintf(ctx->name, sizeof(ctx->name), "%s", param->name);
270
271                 te->data = (void *) ctx;
272
273                 TAILQ_INSERT_TAIL(acl_list, te, next);
274         }
275
276 exit:
277         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
278         return ctx;
279 }
280
281 static int
282 acl_add_rules(struct rte_acl_ctx *ctx, const void *rules, uint32_t num)
283 {
284         uint8_t *pos;
285
286         if (num + ctx->num_rules > ctx->max_rules)
287                 return -ENOMEM;
288
289         pos = ctx->rules;
290         pos += ctx->rule_sz * ctx->num_rules;
291         memcpy(pos, rules, num * ctx->rule_sz);
292         ctx->num_rules += num;
293
294         return 0;
295 }
296
297 static int
298 acl_check_rule(const struct rte_acl_rule_data *rd)
299 {
300         if ((RTE_LEN2MASK(RTE_ACL_MAX_CATEGORIES, typeof(rd->category_mask)) &
301                         rd->category_mask) == 0 ||
302                         rd->priority > RTE_ACL_MAX_PRIORITY ||
303                         rd->priority < RTE_ACL_MIN_PRIORITY ||
304                         rd->userdata == RTE_ACL_INVALID_USERDATA)
305                 return -EINVAL;
306         return 0;
307 }
308
309 int
310 rte_acl_add_rules(struct rte_acl_ctx *ctx, const struct rte_acl_rule *rules,
311         uint32_t num)
312 {
313         const struct rte_acl_rule *rv;
314         uint32_t i;
315         int32_t rc;
316
317         if (ctx == NULL || rules == NULL || 0 == ctx->rule_sz)
318                 return -EINVAL;
319
320         for (i = 0; i != num; i++) {
321                 rv = (const struct rte_acl_rule *)
322                         ((uintptr_t)rules + i * ctx->rule_sz);
323                 rc = acl_check_rule(&rv->data);
324                 if (rc != 0) {
325                         RTE_LOG(ERR, ACL, "%s(%s): rule #%u is invalid\n",
326                                 __func__, ctx->name, i + 1);
327                         return rc;
328                 }
329         }
330
331         return acl_add_rules(ctx, rules, num);
332 }
333
334 /*
335  * Reset all rules.
336  * Note that RT structures are not affected.
337  */
338 void
339 rte_acl_reset_rules(struct rte_acl_ctx *ctx)
340 {
341         if (ctx != NULL)
342                 ctx->num_rules = 0;
343 }
344
345 /*
346  * Reset all rules and destroys RT structures.
347  */
348 void
349 rte_acl_reset(struct rte_acl_ctx *ctx)
350 {
351         if (ctx != NULL) {
352                 rte_acl_reset_rules(ctx);
353                 rte_acl_build(ctx, &ctx->config);
354         }
355 }
356
357 /*
358  * Dump ACL context to the stdout.
359  */
360 void
361 rte_acl_dump(const struct rte_acl_ctx *ctx)
362 {
363         if (!ctx)
364                 return;
365         printf("acl context <%s>@%p\n", ctx->name, ctx);
366         printf("  socket_id=%"PRId32"\n", ctx->socket_id);
367         printf("  alg=%"PRId32"\n", ctx->alg);
368         printf("  max_rules=%"PRIu32"\n", ctx->max_rules);
369         printf("  rule_size=%"PRIu32"\n", ctx->rule_sz);
370         printf("  num_rules=%"PRIu32"\n", ctx->num_rules);
371         printf("  num_categories=%"PRIu32"\n", ctx->num_categories);
372         printf("  num_tries=%"PRIu32"\n", ctx->num_tries);
373 }
374
375 /*
376  * Dump all ACL contexts to the stdout.
377  */
378 void
379 rte_acl_list_dump(void)
380 {
381         struct rte_acl_ctx *ctx;
382         struct rte_acl_list *acl_list;
383         struct rte_tailq_entry *te;
384
385         acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
386
387         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
388         TAILQ_FOREACH(te, acl_list, next) {
389                 ctx = (struct rte_acl_ctx *) te->data;
390                 rte_acl_dump(ctx);
391         }
392         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
393 }