add prefix to cache line macros
[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 #define BIT_SIZEOF(x)   (sizeof(x) * CHAR_BIT)
38
39 TAILQ_HEAD(rte_acl_list, rte_tailq_entry);
40
41 static const rte_acl_classify_t classify_fns[] = {
42         [RTE_ACL_CLASSIFY_DEFAULT] = rte_acl_classify_scalar,
43         [RTE_ACL_CLASSIFY_SCALAR] = rte_acl_classify_scalar,
44         [RTE_ACL_CLASSIFY_SSE] = rte_acl_classify_sse,
45 };
46
47 /* by default, use always available scalar code path. */
48 static enum rte_acl_classify_alg rte_acl_default_classify =
49         RTE_ACL_CLASSIFY_SCALAR;
50
51 static void
52 rte_acl_set_default_classify(enum rte_acl_classify_alg alg)
53 {
54         rte_acl_default_classify = alg;
55 }
56
57 extern int
58 rte_acl_set_ctx_classify(struct rte_acl_ctx *ctx, enum rte_acl_classify_alg alg)
59 {
60         if (ctx == NULL || (uint32_t)alg >= RTE_DIM(classify_fns))
61                 return -EINVAL;
62
63         ctx->alg = alg;
64         return 0;
65 }
66
67 static void __attribute__((constructor))
68 rte_acl_init(void)
69 {
70         enum rte_acl_classify_alg alg = RTE_ACL_CLASSIFY_DEFAULT;
71
72         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
73                 alg = RTE_ACL_CLASSIFY_SSE;
74
75         rte_acl_set_default_classify(alg);
76 }
77
78 int
79 rte_acl_classify(const struct rte_acl_ctx *ctx, const uint8_t **data,
80         uint32_t *results, uint32_t num, uint32_t categories)
81 {
82         return classify_fns[ctx->alg](ctx, data, results, num, categories);
83 }
84
85 int
86 rte_acl_classify_alg(const struct rte_acl_ctx *ctx, const uint8_t **data,
87         uint32_t *results, uint32_t num, uint32_t categories,
88         enum rte_acl_classify_alg alg)
89 {
90         return classify_fns[alg](ctx, data, results, num, categories);
91 }
92
93 struct rte_acl_ctx *
94 rte_acl_find_existing(const char *name)
95 {
96         struct rte_acl_ctx *ctx = NULL;
97         struct rte_acl_list *acl_list;
98         struct rte_tailq_entry *te;
99
100         /* check that we have an initialised tail queue */
101         acl_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_ACL, rte_acl_list);
102         if (acl_list == NULL) {
103                 rte_errno = E_RTE_NO_TAILQ;
104                 return NULL;
105         }
106
107         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
108         TAILQ_FOREACH(te, acl_list, next) {
109                 ctx = (struct rte_acl_ctx *) te->data;
110                 if (strncmp(name, ctx->name, sizeof(ctx->name)) == 0)
111                         break;
112         }
113         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
114
115         if (te == NULL) {
116                 rte_errno = ENOENT;
117                 return NULL;
118         }
119         return ctx;
120 }
121
122 void
123 rte_acl_free(struct rte_acl_ctx *ctx)
124 {
125         struct rte_acl_list *acl_list;
126         struct rte_tailq_entry *te;
127
128         if (ctx == NULL)
129                 return;
130
131         /* check that we have an initialised tail queue */
132         acl_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_ACL, rte_acl_list);
133         if (acl_list == NULL) {
134                 rte_errno = E_RTE_NO_TAILQ;
135                 return;
136         }
137
138         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
139
140         /* find our tailq entry */
141         TAILQ_FOREACH(te, acl_list, next) {
142                 if (te->data == (void *) ctx)
143                         break;
144         }
145         if (te == NULL) {
146                 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
147                 return;
148         }
149
150         TAILQ_REMOVE(acl_list, te, next);
151
152         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
153
154         rte_free(ctx->mem);
155         rte_free(ctx);
156         rte_free(te);
157 }
158
159 struct rte_acl_ctx *
160 rte_acl_create(const struct rte_acl_param *param)
161 {
162         size_t sz;
163         struct rte_acl_ctx *ctx;
164         struct rte_acl_list *acl_list;
165         struct rte_tailq_entry *te;
166         char name[sizeof(ctx->name)];
167
168         /* check that we have an initialised tail queue */
169         acl_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_ACL, rte_acl_list);
170         if (acl_list == NULL) {
171                 rte_errno = E_RTE_NO_TAILQ;
172                 return NULL;
173         }
174
175         /* check that input parameters are valid. */
176         if (param == NULL || param->name == NULL) {
177                 rte_errno = EINVAL;
178                 return NULL;
179         }
180
181         snprintf(name, sizeof(name), "ACL_%s", param->name);
182
183         /* calculate amount of memory required for pattern set. */
184         sz = sizeof(*ctx) + param->max_rule_num * param->rule_size;
185
186         /* get EAL TAILQ lock. */
187         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
188
189         /* if we already have one with that name */
190         TAILQ_FOREACH(te, acl_list, next) {
191                 ctx = (struct rte_acl_ctx *) te->data;
192                 if (strncmp(param->name, ctx->name, sizeof(ctx->name)) == 0)
193                         break;
194         }
195
196         /* if ACL with such name doesn't exist, then create a new one. */
197         if (te == NULL) {
198                 ctx = NULL;
199                 te = rte_zmalloc("ACL_TAILQ_ENTRY", sizeof(*te), 0);
200
201                 if (te == NULL) {
202                         RTE_LOG(ERR, ACL, "Cannot allocate tailq entry!\n");
203                         goto exit;
204                 }
205
206                 ctx = rte_zmalloc_socket(name, sz, RTE_CACHE_LINE_SIZE, param->socket_id);
207
208                 if (ctx == NULL) {
209                         RTE_LOG(ERR, ACL,
210                                 "allocation of %zu bytes on socket %d for %s failed\n",
211                                 sz, param->socket_id, name);
212                         rte_free(te);
213                         goto exit;
214                 }
215                 /* init new allocated context. */
216                 ctx->rules = ctx + 1;
217                 ctx->max_rules = param->max_rule_num;
218                 ctx->rule_sz = param->rule_size;
219                 ctx->socket_id = param->socket_id;
220                 ctx->alg = rte_acl_default_classify;
221                 snprintf(ctx->name, sizeof(ctx->name), "%s", param->name);
222
223                 te->data = (void *) ctx;
224
225                 TAILQ_INSERT_TAIL(acl_list, te, next);
226         }
227
228 exit:
229         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
230         return ctx;
231 }
232
233 static int
234 acl_add_rules(struct rte_acl_ctx *ctx, const void *rules, uint32_t num)
235 {
236         uint8_t *pos;
237
238         if (num + ctx->num_rules > ctx->max_rules)
239                 return -ENOMEM;
240
241         pos = ctx->rules;
242         pos += ctx->rule_sz * ctx->num_rules;
243         memcpy(pos, rules, num * ctx->rule_sz);
244         ctx->num_rules += num;
245
246         return 0;
247 }
248
249 static int
250 acl_check_rule(const struct rte_acl_rule_data *rd)
251 {
252         if ((rd->category_mask & LEN2MASK(RTE_ACL_MAX_CATEGORIES)) == 0 ||
253                         rd->priority > RTE_ACL_MAX_PRIORITY ||
254                         rd->priority < RTE_ACL_MIN_PRIORITY ||
255                         rd->userdata == RTE_ACL_INVALID_USERDATA)
256                 return -EINVAL;
257         return 0;
258 }
259
260 int
261 rte_acl_add_rules(struct rte_acl_ctx *ctx, const struct rte_acl_rule *rules,
262         uint32_t num)
263 {
264         const struct rte_acl_rule *rv;
265         uint32_t i;
266         int32_t rc;
267
268         if (ctx == NULL || rules == NULL || 0 == ctx->rule_sz)
269                 return -EINVAL;
270
271         for (i = 0; i != num; i++) {
272                 rv = (const struct rte_acl_rule *)
273                         ((uintptr_t)rules + i * ctx->rule_sz);
274                 rc = acl_check_rule(&rv->data);
275                 if (rc != 0) {
276                         RTE_LOG(ERR, ACL, "%s(%s): rule #%u is invalid\n",
277                                 __func__, ctx->name, i + 1);
278                         return rc;
279                 }
280         }
281
282         return acl_add_rules(ctx, rules, num);
283 }
284
285 /*
286  * Reset all rules.
287  * Note that RT structures are not affected.
288  */
289 void
290 rte_acl_reset_rules(struct rte_acl_ctx *ctx)
291 {
292         if (ctx != NULL)
293                 ctx->num_rules = 0;
294 }
295
296 /*
297  * Reset all rules and destroys RT structures.
298  */
299 void
300 rte_acl_reset(struct rte_acl_ctx *ctx)
301 {
302         if (ctx != NULL) {
303                 rte_acl_reset_rules(ctx);
304                 rte_acl_build(ctx, &ctx->config);
305         }
306 }
307
308 /*
309  * Dump ACL context to the stdout.
310  */
311 void
312 rte_acl_dump(const struct rte_acl_ctx *ctx)
313 {
314         if (!ctx)
315                 return;
316         printf("acl context <%s>@%p\n", ctx->name, ctx);
317         printf("  socket_id=%"PRId32"\n", ctx->socket_id);
318         printf("  alg=%"PRId32"\n", ctx->alg);
319         printf("  max_rules=%"PRIu32"\n", ctx->max_rules);
320         printf("  rule_size=%"PRIu32"\n", ctx->rule_sz);
321         printf("  num_rules=%"PRIu32"\n", ctx->num_rules);
322         printf("  num_categories=%"PRIu32"\n", ctx->num_categories);
323         printf("  num_tries=%"PRIu32"\n", ctx->num_tries);
324 }
325
326 /*
327  * Dump all ACL contexts to the stdout.
328  */
329 void
330 rte_acl_list_dump(void)
331 {
332         struct rte_acl_ctx *ctx;
333         struct rte_acl_list *acl_list;
334         struct rte_tailq_entry *te;
335
336         /* check that we have an initialised tail queue */
337         acl_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_ACL, rte_acl_list);
338         if (acl_list == NULL) {
339                 rte_errno = E_RTE_NO_TAILQ;
340                 return;
341         }
342
343         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
344         TAILQ_FOREACH(te, acl_list, next) {
345                 ctx = (struct rte_acl_ctx *) te->data;
346                 rte_acl_dump(ctx);
347         }
348         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
349 }
350
351 /*
352  * Support for legacy ipv4vlan rules.
353  */
354
355 RTE_ACL_RULE_DEF(acl_ipv4vlan_rule, RTE_ACL_IPV4VLAN_NUM_FIELDS);
356
357 static int
358 acl_ipv4vlan_check_rule(const struct rte_acl_ipv4vlan_rule *rule)
359 {
360         if (rule->src_port_low > rule->src_port_high ||
361                         rule->dst_port_low > rule->dst_port_high ||
362                         rule->src_mask_len > BIT_SIZEOF(rule->src_addr) ||
363                         rule->dst_mask_len > BIT_SIZEOF(rule->dst_addr))
364                 return -EINVAL;
365
366         return acl_check_rule(&rule->data);
367 }
368
369 static void
370 acl_ipv4vlan_convert_rule(const struct rte_acl_ipv4vlan_rule *ri,
371         struct acl_ipv4vlan_rule *ro)
372 {
373         ro->data = ri->data;
374
375         ro->field[RTE_ACL_IPV4VLAN_PROTO_FIELD].value.u8 = ri->proto;
376         ro->field[RTE_ACL_IPV4VLAN_VLAN1_FIELD].value.u16 = ri->vlan;
377         ro->field[RTE_ACL_IPV4VLAN_VLAN2_FIELD].value.u16 = ri->domain;
378         ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].value.u32 = ri->src_addr;
379         ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].value.u32 = ri->dst_addr;
380         ro->field[RTE_ACL_IPV4VLAN_SRCP_FIELD].value.u16 = ri->src_port_low;
381         ro->field[RTE_ACL_IPV4VLAN_DSTP_FIELD].value.u16 = ri->dst_port_low;
382
383         ro->field[RTE_ACL_IPV4VLAN_PROTO_FIELD].mask_range.u8 = ri->proto_mask;
384         ro->field[RTE_ACL_IPV4VLAN_VLAN1_FIELD].mask_range.u16 = ri->vlan_mask;
385         ro->field[RTE_ACL_IPV4VLAN_VLAN2_FIELD].mask_range.u16 =
386                 ri->domain_mask;
387         ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].mask_range.u32 =
388                 ri->src_mask_len;
389         ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].mask_range.u32 = ri->dst_mask_len;
390         ro->field[RTE_ACL_IPV4VLAN_SRCP_FIELD].mask_range.u16 =
391                 ri->src_port_high;
392         ro->field[RTE_ACL_IPV4VLAN_DSTP_FIELD].mask_range.u16 =
393                 ri->dst_port_high;
394 }
395
396 int
397 rte_acl_ipv4vlan_add_rules(struct rte_acl_ctx *ctx,
398         const struct rte_acl_ipv4vlan_rule *rules,
399         uint32_t num)
400 {
401         int32_t rc;
402         uint32_t i;
403         struct acl_ipv4vlan_rule rv;
404
405         if (ctx == NULL || rules == NULL || ctx->rule_sz != sizeof(rv))
406                 return -EINVAL;
407
408         /* check input rules. */
409         for (i = 0; i != num; i++) {
410                 rc = acl_ipv4vlan_check_rule(rules + i);
411                 if (rc != 0) {
412                         RTE_LOG(ERR, ACL, "%s(%s): rule #%u is invalid\n",
413                                 __func__, ctx->name, i + 1);
414                         return rc;
415                 }
416         }
417
418         if (num + ctx->num_rules > ctx->max_rules)
419                 return -ENOMEM;
420
421         /* perform conversion to the internal format and add to the context. */
422         for (i = 0, rc = 0; i != num && rc == 0; i++) {
423                 acl_ipv4vlan_convert_rule(rules + i, &rv);
424                 rc = acl_add_rules(ctx, &rv, 1);
425         }
426
427         return rc;
428 }
429
430 static void
431 acl_ipv4vlan_config(struct rte_acl_config *cfg,
432         const uint32_t layout[RTE_ACL_IPV4VLAN_NUM],
433         uint32_t num_categories)
434 {
435         static const struct rte_acl_field_def
436                 ipv4_defs[RTE_ACL_IPV4VLAN_NUM_FIELDS] = {
437                 {
438                         .type = RTE_ACL_FIELD_TYPE_BITMASK,
439                         .size = sizeof(uint8_t),
440                         .field_index = RTE_ACL_IPV4VLAN_PROTO_FIELD,
441                         .input_index = RTE_ACL_IPV4VLAN_PROTO,
442                 },
443                 {
444                         .type = RTE_ACL_FIELD_TYPE_BITMASK,
445                         .size = sizeof(uint16_t),
446                         .field_index = RTE_ACL_IPV4VLAN_VLAN1_FIELD,
447                         .input_index = RTE_ACL_IPV4VLAN_VLAN,
448                 },
449                 {
450                         .type = RTE_ACL_FIELD_TYPE_BITMASK,
451                         .size = sizeof(uint16_t),
452                         .field_index = RTE_ACL_IPV4VLAN_VLAN2_FIELD,
453                         .input_index = RTE_ACL_IPV4VLAN_VLAN,
454                 },
455                 {
456                         .type = RTE_ACL_FIELD_TYPE_MASK,
457                         .size = sizeof(uint32_t),
458                         .field_index = RTE_ACL_IPV4VLAN_SRC_FIELD,
459                         .input_index = RTE_ACL_IPV4VLAN_SRC,
460                 },
461                 {
462                         .type = RTE_ACL_FIELD_TYPE_MASK,
463                         .size = sizeof(uint32_t),
464                         .field_index = RTE_ACL_IPV4VLAN_DST_FIELD,
465                         .input_index = RTE_ACL_IPV4VLAN_DST,
466                 },
467                 {
468                         .type = RTE_ACL_FIELD_TYPE_RANGE,
469                         .size = sizeof(uint16_t),
470                         .field_index = RTE_ACL_IPV4VLAN_SRCP_FIELD,
471                         .input_index = RTE_ACL_IPV4VLAN_PORTS,
472                 },
473                 {
474                         .type = RTE_ACL_FIELD_TYPE_RANGE,
475                         .size = sizeof(uint16_t),
476                         .field_index = RTE_ACL_IPV4VLAN_DSTP_FIELD,
477                         .input_index = RTE_ACL_IPV4VLAN_PORTS,
478                 },
479         };
480
481         memcpy(&cfg->defs, ipv4_defs, sizeof(ipv4_defs));
482         cfg->num_fields = RTE_DIM(ipv4_defs);
483
484         cfg->defs[RTE_ACL_IPV4VLAN_PROTO_FIELD].offset =
485                 layout[RTE_ACL_IPV4VLAN_PROTO];
486         cfg->defs[RTE_ACL_IPV4VLAN_VLAN1_FIELD].offset =
487                 layout[RTE_ACL_IPV4VLAN_VLAN];
488         cfg->defs[RTE_ACL_IPV4VLAN_VLAN2_FIELD].offset =
489                 layout[RTE_ACL_IPV4VLAN_VLAN] +
490                 cfg->defs[RTE_ACL_IPV4VLAN_VLAN1_FIELD].size;
491         cfg->defs[RTE_ACL_IPV4VLAN_SRC_FIELD].offset =
492                 layout[RTE_ACL_IPV4VLAN_SRC];
493         cfg->defs[RTE_ACL_IPV4VLAN_DST_FIELD].offset =
494                 layout[RTE_ACL_IPV4VLAN_DST];
495         cfg->defs[RTE_ACL_IPV4VLAN_SRCP_FIELD].offset =
496                 layout[RTE_ACL_IPV4VLAN_PORTS];
497         cfg->defs[RTE_ACL_IPV4VLAN_DSTP_FIELD].offset =
498                 layout[RTE_ACL_IPV4VLAN_PORTS] +
499                 cfg->defs[RTE_ACL_IPV4VLAN_SRCP_FIELD].size;
500
501         cfg->num_categories = num_categories;
502 }
503
504 int
505 rte_acl_ipv4vlan_build(struct rte_acl_ctx *ctx,
506         const uint32_t layout[RTE_ACL_IPV4VLAN_NUM],
507         uint32_t num_categories)
508 {
509         struct rte_acl_config cfg;
510
511         if (ctx == NULL || layout == NULL)
512                 return -EINVAL;
513
514         acl_ipv4vlan_config(&cfg, layout, num_categories);
515         return rte_acl_build(ctx, &cfg);
516 }