4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
37 #define BIT_SIZEOF(x) (sizeof(x) * CHAR_BIT)
39 TAILQ_HEAD(rte_acl_list, rte_tailq_entry);
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,
47 /* by default, use always avaialbe scalar code path. */
48 static enum rte_acl_classify_alg rte_acl_default_classify =
49 RTE_ACL_CLASSIFY_SCALAR;
52 rte_acl_set_default_classify(enum rte_acl_classify_alg alg)
54 rte_acl_default_classify = alg;
58 rte_acl_set_ctx_classify(struct rte_acl_ctx *ctx, enum rte_acl_classify_alg alg)
60 if (ctx == NULL || (uint32_t)alg >= RTE_DIM(classify_fns))
67 static void __attribute__((constructor))
70 enum rte_acl_classify_alg alg = RTE_ACL_CLASSIFY_DEFAULT;
72 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
73 alg = RTE_ACL_CLASSIFY_SSE;
75 rte_acl_set_default_classify(alg);
79 rte_acl_classify(const struct rte_acl_ctx *ctx, const uint8_t **data,
80 uint32_t *results, uint32_t num, uint32_t categories)
82 return classify_fns[ctx->alg](ctx, data, results, num, categories);
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)
90 return classify_fns[alg](ctx, data, results, num, categories);
94 rte_acl_find_existing(const char *name)
96 struct rte_acl_ctx *ctx = NULL;
97 struct rte_acl_list *acl_list;
98 struct rte_tailq_entry *te;
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;
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)
113 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
123 rte_acl_free(struct rte_acl_ctx *ctx)
125 struct rte_acl_list *acl_list;
126 struct rte_tailq_entry *te;
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;
138 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
140 /* find our tailq entry */
141 TAILQ_FOREACH(te, acl_list, next) {
142 if (te->data == (void *) ctx)
146 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
150 TAILQ_REMOVE(acl_list, te, next);
152 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
160 rte_acl_create(const struct rte_acl_param *param)
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)];
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;
175 /* check that input parameters are valid. */
176 if (param == NULL || param->name == NULL) {
181 snprintf(name, sizeof(name), "ACL_%s", param->name);
183 /* calculate amount of memory required for pattern set. */
184 sz = sizeof(*ctx) + param->max_rule_num * param->rule_size;
186 /* get EAL TAILQ lock. */
187 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
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)
196 /* if ACL with such name doesn't exist, then create a new one. */
199 te = rte_zmalloc("ACL_TAILQ_ENTRY", sizeof(*te), 0);
202 RTE_LOG(ERR, ACL, "Cannot allocate tailq entry!\n");
206 ctx = rte_zmalloc_socket(name, sz, CACHE_LINE_SIZE, param->socket_id);
210 "allocation of %zu bytes on socket %d for %s failed\n",
211 sz, param->socket_id, name);
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);
223 te->data = (void *) ctx;
225 TAILQ_INSERT_TAIL(acl_list, te, next);
229 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
234 acl_add_rules(struct rte_acl_ctx *ctx, const void *rules, uint32_t num)
238 if (num + ctx->num_rules > ctx->max_rules)
242 pos += ctx->rule_sz * ctx->num_rules;
243 memcpy(pos, rules, num * ctx->rule_sz);
244 ctx->num_rules += num;
250 acl_check_rule(const struct rte_acl_rule_data *rd)
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)
261 rte_acl_add_rules(struct rte_acl_ctx *ctx, const struct rte_acl_rule *rules,
264 const struct rte_acl_rule *rv;
268 if (ctx == NULL || rules == NULL || 0 == ctx->rule_sz)
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);
276 RTE_LOG(ERR, ACL, "%s(%s): rule #%u is invalid\n",
277 __func__, ctx->name, i + 1);
282 return acl_add_rules(ctx, rules, num);
287 * Note that RT structures are not affected.
290 rte_acl_reset_rules(struct rte_acl_ctx *ctx)
297 * Reset all rules and destroys RT structures.
300 rte_acl_reset(struct rte_acl_ctx *ctx)
303 rte_acl_reset_rules(ctx);
304 rte_acl_build(ctx, &ctx->config);
309 * Dump ACL context to the stdout.
312 rte_acl_dump(const struct rte_acl_ctx *ctx)
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);
327 * Dump all ACL contexts to the stdout.
330 rte_acl_list_dump(void)
332 struct rte_acl_ctx *ctx;
333 struct rte_acl_list *acl_list;
334 struct rte_tailq_entry *te;
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;
343 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
344 TAILQ_FOREACH(te, acl_list, next) {
345 ctx = (struct rte_acl_ctx *) te->data;
348 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
352 * Support for legacy ipv4vlan rules.
355 RTE_ACL_RULE_DEF(acl_ipv4vlan_rule, RTE_ACL_IPV4VLAN_NUM_FIELDS);
358 acl_ipv4vlan_check_rule(const struct rte_acl_ipv4vlan_rule *rule)
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))
366 return acl_check_rule(&rule->data);
370 acl_ipv4vlan_convert_rule(const struct rte_acl_ipv4vlan_rule *ri,
371 struct acl_ipv4vlan_rule *ro)
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;
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 =
387 ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].mask_range.u32 =
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 =
392 ro->field[RTE_ACL_IPV4VLAN_DSTP_FIELD].mask_range.u16 =
397 rte_acl_ipv4vlan_add_rules(struct rte_acl_ctx *ctx,
398 const struct rte_acl_ipv4vlan_rule *rules,
403 struct acl_ipv4vlan_rule rv;
405 if (ctx == NULL || rules == NULL || ctx->rule_sz != sizeof(rv))
408 /* check input rules. */
409 for (i = 0; i != num; i++) {
410 rc = acl_ipv4vlan_check_rule(rules + i);
412 RTE_LOG(ERR, ACL, "%s(%s): rule #%u is invalid\n",
413 __func__, ctx->name, i + 1);
418 if (num + ctx->num_rules > ctx->max_rules)
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);
431 acl_ipv4vlan_config(struct rte_acl_config *cfg,
432 const uint32_t layout[RTE_ACL_IPV4VLAN_NUM],
433 uint32_t num_categories)
435 static const struct rte_acl_field_def
436 ipv4_defs[RTE_ACL_IPV4VLAN_NUM_FIELDS] = {
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,
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,
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,
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,
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,
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,
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,
481 memcpy(&cfg->defs, ipv4_defs, sizeof(ipv4_defs));
482 cfg->num_fields = RTE_DIM(ipv4_defs);
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;
501 cfg->num_categories = num_categories;
505 rte_acl_ipv4vlan_build(struct rte_acl_ctx *ctx,
506 const uint32_t layout[RTE_ACL_IPV4VLAN_NUM],
507 uint32_t num_categories)
509 struct rte_acl_config cfg;
511 if (ctx == NULL || layout == NULL)
514 acl_ipv4vlan_config(&cfg, layout, num_categories);
515 return rte_acl_build(ctx, &cfg);