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