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 TAILQ_HEAD(rte_acl_list, rte_tailq_entry);
39 static struct rte_tailq_elem rte_acl_tailq = {
42 EAL_REGISTER_TAILQ(rte_acl_tailq)
45 * If the compiler doesn't support AVX2 instructions,
46 * then the dummy one would be used instead for AVX2 classify method.
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)
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)
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)
78 int __attribute__ ((weak))
79 rte_acl_classify_altivec(__rte_unused const struct rte_acl_ctx *ctx,
80 __rte_unused const uint8_t **data,
81 __rte_unused uint32_t *results,
82 __rte_unused uint32_t num,
83 __rte_unused uint32_t categories)
88 static const rte_acl_classify_t classify_fns[] = {
89 [RTE_ACL_CLASSIFY_DEFAULT] = rte_acl_classify_scalar,
90 [RTE_ACL_CLASSIFY_SCALAR] = rte_acl_classify_scalar,
91 [RTE_ACL_CLASSIFY_SSE] = rte_acl_classify_sse,
92 [RTE_ACL_CLASSIFY_AVX2] = rte_acl_classify_avx2,
93 [RTE_ACL_CLASSIFY_NEON] = rte_acl_classify_neon,
94 [RTE_ACL_CLASSIFY_ALTIVEC] = rte_acl_classify_altivec,
97 /* by default, use always available scalar code path. */
98 static enum rte_acl_classify_alg rte_acl_default_classify =
99 RTE_ACL_CLASSIFY_SCALAR;
102 rte_acl_set_default_classify(enum rte_acl_classify_alg alg)
104 rte_acl_default_classify = alg;
108 rte_acl_set_ctx_classify(struct rte_acl_ctx *ctx, enum rte_acl_classify_alg alg)
110 if (ctx == NULL || (uint32_t)alg >= RTE_DIM(classify_fns))
118 * Select highest available classify method as default one.
119 * Note that CLASSIFY_AVX2 should be set as a default only
120 * if both conditions are met:
121 * at build time compiler supports AVX2 and target cpu supports AVX2.
123 static void __attribute__((constructor))
126 enum rte_acl_classify_alg alg = RTE_ACL_CLASSIFY_DEFAULT;
128 #if defined(RTE_ARCH_ARM64)
129 alg = RTE_ACL_CLASSIFY_NEON;
130 #elif defined(RTE_ARCH_ARM)
131 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON))
132 alg = RTE_ACL_CLASSIFY_NEON;
133 #elif defined(RTE_ARCH_PPC_64)
134 alg = RTE_ACL_CLASSIFY_ALTIVEC;
136 #ifdef CC_AVX2_SUPPORT
137 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
138 alg = RTE_ACL_CLASSIFY_AVX2;
139 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
141 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
143 alg = RTE_ACL_CLASSIFY_SSE;
146 rte_acl_set_default_classify(alg);
150 rte_acl_classify_alg(const struct rte_acl_ctx *ctx, const uint8_t **data,
151 uint32_t *results, uint32_t num, uint32_t categories,
152 enum rte_acl_classify_alg alg)
154 if (categories != 1 &&
155 ((RTE_ACL_RESULTS_MULTIPLIER - 1) & categories) != 0)
158 return classify_fns[alg](ctx, data, results, num, categories);
162 rte_acl_classify(const struct rte_acl_ctx *ctx, const uint8_t **data,
163 uint32_t *results, uint32_t num, uint32_t categories)
165 return rte_acl_classify_alg(ctx, data, results, num, categories,
170 rte_acl_find_existing(const char *name)
172 struct rte_acl_ctx *ctx = NULL;
173 struct rte_acl_list *acl_list;
174 struct rte_tailq_entry *te;
176 acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
178 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
179 TAILQ_FOREACH(te, acl_list, next) {
180 ctx = (struct rte_acl_ctx *) te->data;
181 if (strncmp(name, ctx->name, sizeof(ctx->name)) == 0)
184 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
194 rte_acl_free(struct rte_acl_ctx *ctx)
196 struct rte_acl_list *acl_list;
197 struct rte_tailq_entry *te;
202 acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
204 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
206 /* find our tailq entry */
207 TAILQ_FOREACH(te, acl_list, next) {
208 if (te->data == (void *) ctx)
212 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
216 TAILQ_REMOVE(acl_list, te, next);
218 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
226 rte_acl_create(const struct rte_acl_param *param)
229 struct rte_acl_ctx *ctx;
230 struct rte_acl_list *acl_list;
231 struct rte_tailq_entry *te;
232 char name[sizeof(ctx->name)];
234 acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
236 /* check that input parameters are valid. */
237 if (param == NULL || param->name == NULL) {
242 snprintf(name, sizeof(name), "ACL_%s", param->name);
244 /* calculate amount of memory required for pattern set. */
245 sz = sizeof(*ctx) + param->max_rule_num * param->rule_size;
247 /* get EAL TAILQ lock. */
248 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
250 /* if we already have one with that name */
251 TAILQ_FOREACH(te, acl_list, next) {
252 ctx = (struct rte_acl_ctx *) te->data;
253 if (strncmp(param->name, ctx->name, sizeof(ctx->name)) == 0)
257 /* if ACL with such name doesn't exist, then create a new one. */
260 te = rte_zmalloc("ACL_TAILQ_ENTRY", sizeof(*te), 0);
263 RTE_LOG(ERR, ACL, "Cannot allocate tailq entry!\n");
267 ctx = rte_zmalloc_socket(name, sz, RTE_CACHE_LINE_SIZE, param->socket_id);
271 "allocation of %zu bytes on socket %d for %s failed\n",
272 sz, param->socket_id, name);
276 /* init new allocated context. */
277 ctx->rules = ctx + 1;
278 ctx->max_rules = param->max_rule_num;
279 ctx->rule_sz = param->rule_size;
280 ctx->socket_id = param->socket_id;
281 ctx->alg = rte_acl_default_classify;
282 snprintf(ctx->name, sizeof(ctx->name), "%s", param->name);
284 te->data = (void *) ctx;
286 TAILQ_INSERT_TAIL(acl_list, te, next);
290 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
295 acl_add_rules(struct rte_acl_ctx *ctx, const void *rules, uint32_t num)
299 if (num + ctx->num_rules > ctx->max_rules)
303 pos += ctx->rule_sz * ctx->num_rules;
304 memcpy(pos, rules, num * ctx->rule_sz);
305 ctx->num_rules += num;
311 acl_check_rule(const struct rte_acl_rule_data *rd)
313 if ((RTE_LEN2MASK(RTE_ACL_MAX_CATEGORIES, typeof(rd->category_mask)) &
314 rd->category_mask) == 0 ||
315 rd->priority > RTE_ACL_MAX_PRIORITY ||
316 rd->priority < RTE_ACL_MIN_PRIORITY)
322 rte_acl_add_rules(struct rte_acl_ctx *ctx, const struct rte_acl_rule *rules,
325 const struct rte_acl_rule *rv;
329 if (ctx == NULL || rules == NULL || 0 == ctx->rule_sz)
332 for (i = 0; i != num; i++) {
333 rv = (const struct rte_acl_rule *)
334 ((uintptr_t)rules + i * ctx->rule_sz);
335 rc = acl_check_rule(&rv->data);
337 RTE_LOG(ERR, ACL, "%s(%s): rule #%u is invalid\n",
338 __func__, ctx->name, i + 1);
343 return acl_add_rules(ctx, rules, num);
348 * Note that RT structures are not affected.
351 rte_acl_reset_rules(struct rte_acl_ctx *ctx)
358 * Reset all rules and destroys RT structures.
361 rte_acl_reset(struct rte_acl_ctx *ctx)
364 rte_acl_reset_rules(ctx);
365 rte_acl_build(ctx, &ctx->config);
370 * Dump ACL context to the stdout.
373 rte_acl_dump(const struct rte_acl_ctx *ctx)
377 printf("acl context <%s>@%p\n", ctx->name, ctx);
378 printf(" socket_id=%"PRId32"\n", ctx->socket_id);
379 printf(" alg=%"PRId32"\n", ctx->alg);
380 printf(" max_rules=%"PRIu32"\n", ctx->max_rules);
381 printf(" rule_size=%"PRIu32"\n", ctx->rule_sz);
382 printf(" num_rules=%"PRIu32"\n", ctx->num_rules);
383 printf(" num_categories=%"PRIu32"\n", ctx->num_categories);
384 printf(" num_tries=%"PRIu32"\n", ctx->num_tries);
388 * Dump all ACL contexts to the stdout.
391 rte_acl_list_dump(void)
393 struct rte_acl_ctx *ctx;
394 struct rte_acl_list *acl_list;
395 struct rte_tailq_entry *te;
397 acl_list = RTE_TAILQ_CAST(rte_acl_tailq.head, rte_acl_list);
399 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
400 TAILQ_FOREACH(te, acl_list, next) {
401 ctx = (struct rte_acl_ctx *) te->data;
404 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);