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 struct rte_tailq_elem rte_acl_tailq = {
44 EAL_REGISTER_TAILQ(rte_acl_tailq)
47 * If the compiler doesn't support AVX2 instructions,
48 * then the dummy one would be used instead for AVX2 classify method.
50 int __attribute__ ((weak))
51 rte_acl_classify_avx2(__rte_unused const struct rte_acl_ctx *ctx,
52 __rte_unused const uint8_t **data,
53 __rte_unused uint32_t *results,
54 __rte_unused uint32_t num,
55 __rte_unused uint32_t categories)
60 static const rte_acl_classify_t classify_fns[] = {
61 [RTE_ACL_CLASSIFY_DEFAULT] = rte_acl_classify_scalar,
62 [RTE_ACL_CLASSIFY_SCALAR] = rte_acl_classify_scalar,
63 [RTE_ACL_CLASSIFY_SSE] = rte_acl_classify_sse,
64 [RTE_ACL_CLASSIFY_AVX2] = rte_acl_classify_avx2,
67 /* by default, use always available scalar code path. */
68 static enum rte_acl_classify_alg rte_acl_default_classify =
69 RTE_ACL_CLASSIFY_SCALAR;
72 rte_acl_set_default_classify(enum rte_acl_classify_alg alg)
74 rte_acl_default_classify = alg;
78 rte_acl_set_ctx_classify(struct rte_acl_ctx *ctx, enum rte_acl_classify_alg alg)
80 if (ctx == NULL || (uint32_t)alg >= RTE_DIM(classify_fns))
88 * Select highest available classify method as default one.
89 * Note that CLASSIFY_AVX2 should be set as a default only
90 * if both conditions are met:
91 * at build time compiler supports AVX2 and target cpu supports AVX2.
93 static void __attribute__((constructor))
96 enum rte_acl_classify_alg alg = RTE_ACL_CLASSIFY_DEFAULT;
98 #ifdef CC_AVX2_SUPPORT
99 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
100 alg = RTE_ACL_CLASSIFY_AVX2;
101 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
103 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
105 alg = RTE_ACL_CLASSIFY_SSE;
107 rte_acl_set_default_classify(alg);
111 rte_acl_classify_alg(const struct rte_acl_ctx *ctx, const uint8_t **data,
112 uint32_t *results, uint32_t num, uint32_t categories,
113 enum rte_acl_classify_alg alg)
115 if (categories != 1 &&
116 ((RTE_ACL_RESULTS_MULTIPLIER - 1) & categories) != 0)
119 return classify_fns[alg](ctx, data, results, num, categories);
123 rte_acl_classify(const struct rte_acl_ctx *ctx, const uint8_t **data,
124 uint32_t *results, uint32_t num, uint32_t categories)
126 return rte_acl_classify_alg(ctx, data, results, num, categories,
131 rte_acl_find_existing(const char *name)
133 struct rte_acl_ctx *ctx = NULL;
134 struct rte_acl_list *acl_list;
135 struct rte_tailq_entry *te;
137 acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
139 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
140 TAILQ_FOREACH(te, acl_list, next) {
141 ctx = (struct rte_acl_ctx *) te->data;
142 if (strncmp(name, ctx->name, sizeof(ctx->name)) == 0)
145 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
155 rte_acl_free(struct rte_acl_ctx *ctx)
157 struct rte_acl_list *acl_list;
158 struct rte_tailq_entry *te;
163 acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
165 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
167 /* find our tailq entry */
168 TAILQ_FOREACH(te, acl_list, next) {
169 if (te->data == (void *) ctx)
173 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
177 TAILQ_REMOVE(acl_list, te, next);
179 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
187 rte_acl_create(const struct rte_acl_param *param)
190 struct rte_acl_ctx *ctx;
191 struct rte_acl_list *acl_list;
192 struct rte_tailq_entry *te;
193 char name[sizeof(ctx->name)];
195 acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
197 /* check that input parameters are valid. */
198 if (param == NULL || param->name == NULL) {
203 snprintf(name, sizeof(name), "ACL_%s", param->name);
205 /* calculate amount of memory required for pattern set. */
206 sz = sizeof(*ctx) + param->max_rule_num * param->rule_size;
208 /* get EAL TAILQ lock. */
209 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
211 /* if we already have one with that name */
212 TAILQ_FOREACH(te, acl_list, next) {
213 ctx = (struct rte_acl_ctx *) te->data;
214 if (strncmp(param->name, ctx->name, sizeof(ctx->name)) == 0)
218 /* if ACL with such name doesn't exist, then create a new one. */
221 te = rte_zmalloc("ACL_TAILQ_ENTRY", sizeof(*te), 0);
224 RTE_LOG(ERR, ACL, "Cannot allocate tailq entry!\n");
228 ctx = rte_zmalloc_socket(name, sz, RTE_CACHE_LINE_SIZE, param->socket_id);
232 "allocation of %zu bytes on socket %d for %s failed\n",
233 sz, param->socket_id, name);
237 /* init new allocated context. */
238 ctx->rules = ctx + 1;
239 ctx->max_rules = param->max_rule_num;
240 ctx->rule_sz = param->rule_size;
241 ctx->socket_id = param->socket_id;
242 ctx->alg = rte_acl_default_classify;
243 snprintf(ctx->name, sizeof(ctx->name), "%s", param->name);
245 te->data = (void *) ctx;
247 TAILQ_INSERT_TAIL(acl_list, te, next);
251 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
256 acl_add_rules(struct rte_acl_ctx *ctx, const void *rules, uint32_t num)
260 if (num + ctx->num_rules > ctx->max_rules)
264 pos += ctx->rule_sz * ctx->num_rules;
265 memcpy(pos, rules, num * ctx->rule_sz);
266 ctx->num_rules += num;
272 acl_check_rule(const struct rte_acl_rule_data *rd)
274 if ((RTE_LEN2MASK(RTE_ACL_MAX_CATEGORIES, typeof(rd->category_mask)) &
275 rd->category_mask) == 0 ||
276 rd->priority > RTE_ACL_MAX_PRIORITY ||
277 rd->priority < RTE_ACL_MIN_PRIORITY ||
278 rd->userdata == RTE_ACL_INVALID_USERDATA)
284 rte_acl_add_rules(struct rte_acl_ctx *ctx, const struct rte_acl_rule *rules,
287 const struct rte_acl_rule *rv;
291 if (ctx == NULL || rules == NULL || 0 == ctx->rule_sz)
294 for (i = 0; i != num; i++) {
295 rv = (const struct rte_acl_rule *)
296 ((uintptr_t)rules + i * ctx->rule_sz);
297 rc = acl_check_rule(&rv->data);
299 RTE_LOG(ERR, ACL, "%s(%s): rule #%u is invalid\n",
300 __func__, ctx->name, i + 1);
305 return acl_add_rules(ctx, rules, num);
310 * Note that RT structures are not affected.
313 rte_acl_reset_rules(struct rte_acl_ctx *ctx)
320 * Reset all rules and destroys RT structures.
323 rte_acl_reset(struct rte_acl_ctx *ctx)
326 rte_acl_reset_rules(ctx);
327 rte_acl_build(ctx, &ctx->config);
332 * Dump ACL context to the stdout.
335 rte_acl_dump(const struct rte_acl_ctx *ctx)
339 printf("acl context <%s>@%p\n", ctx->name, ctx);
340 printf(" socket_id=%"PRId32"\n", ctx->socket_id);
341 printf(" alg=%"PRId32"\n", ctx->alg);
342 printf(" max_rules=%"PRIu32"\n", ctx->max_rules);
343 printf(" rule_size=%"PRIu32"\n", ctx->rule_sz);
344 printf(" num_rules=%"PRIu32"\n", ctx->num_rules);
345 printf(" num_categories=%"PRIu32"\n", ctx->num_categories);
346 printf(" num_tries=%"PRIu32"\n", ctx->num_tries);
350 * Dump all ACL contexts to the stdout.
353 rte_acl_list_dump(void)
355 struct rte_acl_ctx *ctx;
356 struct rte_acl_list *acl_list;
357 struct rte_tailq_entry *te;
359 acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
361 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
362 TAILQ_FOREACH(te, acl_list, next) {
363 ctx = (struct rte_acl_ctx *) te->data;
366 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
370 * Support for legacy ipv4vlan rules.
373 RTE_ACL_RULE_DEF(acl_ipv4vlan_rule, RTE_ACL_IPV4VLAN_NUM_FIELDS);
376 acl_ipv4vlan_check_rule(const struct rte_acl_ipv4vlan_rule *rule)
378 if (rule->src_port_low > rule->src_port_high ||
379 rule->dst_port_low > rule->dst_port_high ||
380 rule->src_mask_len > BIT_SIZEOF(rule->src_addr) ||
381 rule->dst_mask_len > BIT_SIZEOF(rule->dst_addr))
384 return acl_check_rule(&rule->data);
388 acl_ipv4vlan_convert_rule(const struct rte_acl_ipv4vlan_rule *ri,
389 struct acl_ipv4vlan_rule *ro)
393 ro->field[RTE_ACL_IPV4VLAN_PROTO_FIELD].value.u8 = ri->proto;
394 ro->field[RTE_ACL_IPV4VLAN_VLAN1_FIELD].value.u16 = ri->vlan;
395 ro->field[RTE_ACL_IPV4VLAN_VLAN2_FIELD].value.u16 = ri->domain;
396 ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].value.u32 = ri->src_addr;
397 ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].value.u32 = ri->dst_addr;
398 ro->field[RTE_ACL_IPV4VLAN_SRCP_FIELD].value.u16 = ri->src_port_low;
399 ro->field[RTE_ACL_IPV4VLAN_DSTP_FIELD].value.u16 = ri->dst_port_low;
401 ro->field[RTE_ACL_IPV4VLAN_PROTO_FIELD].mask_range.u8 = ri->proto_mask;
402 ro->field[RTE_ACL_IPV4VLAN_VLAN1_FIELD].mask_range.u16 = ri->vlan_mask;
403 ro->field[RTE_ACL_IPV4VLAN_VLAN2_FIELD].mask_range.u16 =
405 ro->field[RTE_ACL_IPV4VLAN_SRC_FIELD].mask_range.u32 =
407 ro->field[RTE_ACL_IPV4VLAN_DST_FIELD].mask_range.u32 = ri->dst_mask_len;
408 ro->field[RTE_ACL_IPV4VLAN_SRCP_FIELD].mask_range.u16 =
410 ro->field[RTE_ACL_IPV4VLAN_DSTP_FIELD].mask_range.u16 =
415 rte_acl_ipv4vlan_add_rules(struct rte_acl_ctx *ctx,
416 const struct rte_acl_ipv4vlan_rule *rules,
421 struct acl_ipv4vlan_rule rv;
423 if (ctx == NULL || rules == NULL || ctx->rule_sz != sizeof(rv))
426 /* check input rules. */
427 for (i = 0; i != num; i++) {
428 rc = acl_ipv4vlan_check_rule(rules + i);
430 RTE_LOG(ERR, ACL, "%s(%s): rule #%u is invalid\n",
431 __func__, ctx->name, i + 1);
436 if (num + ctx->num_rules > ctx->max_rules)
439 /* perform conversion to the internal format and add to the context. */
440 for (i = 0, rc = 0; i != num && rc == 0; i++) {
441 acl_ipv4vlan_convert_rule(rules + i, &rv);
442 rc = acl_add_rules(ctx, &rv, 1);
449 acl_ipv4vlan_config(struct rte_acl_config *cfg,
450 const uint32_t layout[RTE_ACL_IPV4VLAN_NUM],
451 uint32_t num_categories)
453 static const struct rte_acl_field_def
454 ipv4_defs[RTE_ACL_IPV4VLAN_NUM_FIELDS] = {
456 .type = RTE_ACL_FIELD_TYPE_BITMASK,
457 .size = sizeof(uint8_t),
458 .field_index = RTE_ACL_IPV4VLAN_PROTO_FIELD,
459 .input_index = RTE_ACL_IPV4VLAN_PROTO,
462 .type = RTE_ACL_FIELD_TYPE_BITMASK,
463 .size = sizeof(uint16_t),
464 .field_index = RTE_ACL_IPV4VLAN_VLAN1_FIELD,
465 .input_index = RTE_ACL_IPV4VLAN_VLAN,
468 .type = RTE_ACL_FIELD_TYPE_BITMASK,
469 .size = sizeof(uint16_t),
470 .field_index = RTE_ACL_IPV4VLAN_VLAN2_FIELD,
471 .input_index = RTE_ACL_IPV4VLAN_VLAN,
474 .type = RTE_ACL_FIELD_TYPE_MASK,
475 .size = sizeof(uint32_t),
476 .field_index = RTE_ACL_IPV4VLAN_SRC_FIELD,
477 .input_index = RTE_ACL_IPV4VLAN_SRC,
480 .type = RTE_ACL_FIELD_TYPE_MASK,
481 .size = sizeof(uint32_t),
482 .field_index = RTE_ACL_IPV4VLAN_DST_FIELD,
483 .input_index = RTE_ACL_IPV4VLAN_DST,
486 .type = RTE_ACL_FIELD_TYPE_RANGE,
487 .size = sizeof(uint16_t),
488 .field_index = RTE_ACL_IPV4VLAN_SRCP_FIELD,
489 .input_index = RTE_ACL_IPV4VLAN_PORTS,
492 .type = RTE_ACL_FIELD_TYPE_RANGE,
493 .size = sizeof(uint16_t),
494 .field_index = RTE_ACL_IPV4VLAN_DSTP_FIELD,
495 .input_index = RTE_ACL_IPV4VLAN_PORTS,
499 memcpy(&cfg->defs, ipv4_defs, sizeof(ipv4_defs));
500 cfg->num_fields = RTE_DIM(ipv4_defs);
502 cfg->defs[RTE_ACL_IPV4VLAN_PROTO_FIELD].offset =
503 layout[RTE_ACL_IPV4VLAN_PROTO];
504 cfg->defs[RTE_ACL_IPV4VLAN_VLAN1_FIELD].offset =
505 layout[RTE_ACL_IPV4VLAN_VLAN];
506 cfg->defs[RTE_ACL_IPV4VLAN_VLAN2_FIELD].offset =
507 layout[RTE_ACL_IPV4VLAN_VLAN] +
508 cfg->defs[RTE_ACL_IPV4VLAN_VLAN1_FIELD].size;
509 cfg->defs[RTE_ACL_IPV4VLAN_SRC_FIELD].offset =
510 layout[RTE_ACL_IPV4VLAN_SRC];
511 cfg->defs[RTE_ACL_IPV4VLAN_DST_FIELD].offset =
512 layout[RTE_ACL_IPV4VLAN_DST];
513 cfg->defs[RTE_ACL_IPV4VLAN_SRCP_FIELD].offset =
514 layout[RTE_ACL_IPV4VLAN_PORTS];
515 cfg->defs[RTE_ACL_IPV4VLAN_DSTP_FIELD].offset =
516 layout[RTE_ACL_IPV4VLAN_PORTS] +
517 cfg->defs[RTE_ACL_IPV4VLAN_SRCP_FIELD].size;
519 cfg->num_categories = num_categories;
523 rte_acl_ipv4vlan_build(struct rte_acl_ctx *ctx,
524 const uint32_t layout[RTE_ACL_IPV4VLAN_NUM],
525 uint32_t num_categories)
527 struct rte_acl_config cfg;
529 if (ctx == NULL || layout == NULL)
532 memset(&cfg, 0, sizeof(cfg));
533 acl_ipv4vlan_config(&cfg, layout, num_categories);
534 return rte_acl_build(ctx, &cfg);