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 #include <rte_common.h>
39 #include <rte_memory.h>
40 #include <rte_malloc.h>
43 #include "rte_table_acl.h"
44 #include <rte_ether.h>
46 struct rte_table_acl {
47 /* Low-level ACL table */
48 char name[2][RTE_ACL_NAMESIZE];
49 struct rte_acl_param acl_params; /* for creating low level acl table */
50 struct rte_acl_config cfg; /* Holds the field definitions (metadata) */
51 struct rte_acl_ctx *ctx;
54 /* Input parameters */
59 uint8_t *action_table;
60 struct rte_acl_rule **acl_rule_list; /* Array of pointers to rules */
61 uint8_t *acl_rule_memory; /* Memory to store the rules */
63 /* Memory to store the action table and stack of free entries */
64 uint8_t memory[0] __rte_cache_aligned;
74 struct rte_table_acl_params *p = (struct rte_table_acl_params *) params;
75 struct rte_table_acl *acl;
76 uint32_t action_table_size, acl_rule_list_size, acl_rule_memory_size;
79 RTE_BUILD_BUG_ON(((sizeof(struct rte_table_acl) % RTE_CACHE_LINE_SIZE)
82 /* Check input parameters */
84 RTE_LOG(ERR, TABLE, "%s: Invalid value for params\n", __func__);
87 if (p->name == NULL) {
88 RTE_LOG(ERR, TABLE, "%s: Invalid value for name\n", __func__);
91 if (p->n_rules == 0) {
92 RTE_LOG(ERR, TABLE, "%s: Invalid value for n_rules\n",
96 if ((p->n_rule_fields == 0) ||
97 (p->n_rule_fields > RTE_ACL_MAX_FIELDS)) {
98 RTE_LOG(ERR, TABLE, "%s: Invalid value for n_rule_fields\n",
103 entry_size = RTE_ALIGN(entry_size, sizeof(uint64_t));
105 /* Memory allocation */
106 action_table_size = RTE_CACHE_LINE_ROUNDUP(p->n_rules * entry_size);
108 RTE_CACHE_LINE_ROUNDUP(p->n_rules * sizeof(struct rte_acl_rule *));
109 acl_rule_memory_size = RTE_CACHE_LINE_ROUNDUP(p->n_rules *
110 RTE_ACL_RULE_SZ(p->n_rule_fields));
111 total_size = sizeof(struct rte_table_acl) + action_table_size +
112 acl_rule_list_size + acl_rule_memory_size;
114 acl = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE,
118 "%s: Cannot allocate %u bytes for ACL table\n",
119 __func__, total_size);
123 acl->action_table = &acl->memory[0];
125 (struct rte_acl_rule **) &acl->memory[action_table_size];
126 acl->acl_rule_memory = (uint8_t *)
127 &acl->memory[action_table_size + acl_rule_list_size];
129 /* Initialization of internal fields */
130 snprintf(acl->name[0], RTE_ACL_NAMESIZE, "%s_a", p->name);
131 snprintf(acl->name[1], RTE_ACL_NAMESIZE, "%s_b", p->name);
134 acl->acl_params.name = acl->name[acl->name_id];
135 acl->acl_params.socket_id = socket_id;
136 acl->acl_params.rule_size = RTE_ACL_RULE_SZ(p->n_rule_fields);
137 acl->acl_params.max_rule_num = p->n_rules;
139 acl->cfg.num_categories = 1;
140 acl->cfg.num_fields = p->n_rule_fields;
141 memcpy(&acl->cfg.defs[0], &p->field_format[0],
142 p->n_rule_fields * sizeof(struct rte_acl_field_def));
146 acl->n_rules = p->n_rules;
147 acl->entry_size = entry_size;
153 rte_table_acl_free(void *table)
155 struct rte_table_acl *acl = (struct rte_table_acl *) table;
157 /* Check input parameters */
159 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
163 /* Free previously allocated resources */
164 if (acl->ctx != NULL)
165 rte_acl_free(acl->ctx);
172 RTE_ACL_RULE_DEF(rte_pipeline_acl_rule, RTE_ACL_MAX_FIELDS);
175 rte_table_acl_build(struct rte_table_acl *acl, struct rte_acl_ctx **acl_ctx)
177 struct rte_acl_ctx *ctx = NULL;
181 /* Create low level ACL table */
182 ctx = rte_acl_create(&acl->acl_params);
184 RTE_LOG(ERR, TABLE, "%s: Cannot create low level ACL table\n",
189 /* Add rules to low level ACL table */
191 for (i = 1; i < acl->n_rules; i++) {
192 if (acl->acl_rule_list[i] != NULL) {
193 status = rte_acl_add_rules(ctx, acl->acl_rule_list[i],
197 "%s: Cannot add rule to low level ACL table\n",
213 /* Build low level ACl table */
214 status = rte_acl_build(ctx, &acl->cfg);
217 "%s: Cannot build the low level ACL table\n",
230 rte_table_acl_entry_add(
237 struct rte_table_acl *acl = (struct rte_table_acl *) table;
238 struct rte_table_acl_rule_add_params *rule =
239 (struct rte_table_acl_rule_add_params *) key;
240 struct rte_pipeline_acl_rule acl_rule;
241 struct rte_acl_rule *rule_location;
242 struct rte_acl_ctx *ctx;
243 uint32_t free_pos, free_pos_valid, i;
246 /* Check input parameters */
248 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
252 RTE_LOG(ERR, TABLE, "%s: key parameter is NULL\n", __func__);
256 RTE_LOG(ERR, TABLE, "%s: entry parameter is NULL\n", __func__);
259 if (key_found == NULL) {
260 RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n",
264 if (entry_ptr == NULL) {
265 RTE_LOG(ERR, TABLE, "%s: entry_ptr parameter is NULL\n",
269 if (rule->priority > RTE_ACL_MAX_PRIORITY) {
270 RTE_LOG(ERR, TABLE, "%s: Priority is too high\n", __func__);
274 /* Setup rule data structure */
275 memset(&acl_rule, 0, sizeof(acl_rule));
276 acl_rule.data.category_mask = 1;
277 acl_rule.data.priority = RTE_ACL_MAX_PRIORITY - rule->priority;
278 acl_rule.data.userdata = 0; /* To be set up later */
279 memcpy(&acl_rule.field[0],
280 &rule->field_value[0],
281 acl->cfg.num_fields * sizeof(struct rte_acl_field));
283 /* Look to see if the rule exists already in the table */
286 for (i = 1; i < acl->n_rules; i++) {
287 if (acl->acl_rule_list[i] == NULL) {
288 if (free_pos_valid == 0) {
296 /* Compare the key fields */
297 status = memcmp(&acl->acl_rule_list[i]->field[0],
298 &rule->field_value[0],
299 acl->cfg.num_fields * sizeof(struct rte_acl_field));
301 /* Rule found: update data associated with the rule */
304 *entry_ptr = &acl->memory[i * acl->entry_size];
305 memcpy(*entry_ptr, entry, acl->entry_size);
311 /* Return if max rules */
312 if (free_pos_valid == 0) {
313 RTE_LOG(ERR, TABLE, "%s: Max number of rules reached\n",
318 /* Add the new rule to the rule set */
319 acl_rule.data.userdata = free_pos;
320 rule_location = (struct rte_acl_rule *)
321 &acl->acl_rule_memory[free_pos * acl->acl_params.rule_size];
322 memcpy(rule_location, &acl_rule, acl->acl_params.rule_size);
323 acl->acl_rule_list[free_pos] = rule_location;
325 /* Build low level ACL table */
327 acl->acl_params.name = acl->name[acl->name_id];
328 status = rte_table_acl_build(acl, &ctx);
330 /* Roll back changes */
331 acl->acl_rule_list[free_pos] = NULL;
338 if (acl->ctx != NULL)
339 rte_acl_free(acl->ctx);
342 *entry_ptr = &acl->memory[free_pos * acl->entry_size];
343 memcpy(*entry_ptr, entry, acl->entry_size);
349 rte_table_acl_entry_delete(
355 struct rte_table_acl *acl = (struct rte_table_acl *) table;
356 struct rte_table_acl_rule_delete_params *rule =
357 (struct rte_table_acl_rule_delete_params *) key;
358 struct rte_acl_rule *deleted_rule = NULL;
359 struct rte_acl_ctx *ctx;
360 uint32_t pos, pos_valid, i;
363 /* Check input parameters */
365 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
369 RTE_LOG(ERR, TABLE, "%s: key parameter is NULL\n", __func__);
372 if (key_found == NULL) {
373 RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n",
378 /* Look for the rule in the table */
381 for (i = 1; i < acl->n_rules; i++) {
382 if (acl->acl_rule_list[i] != NULL) {
383 /* Compare the key fields */
384 status = memcmp(&acl->acl_rule_list[i]->field[0],
385 &rule->field_value[0], acl->cfg.num_fields *
386 sizeof(struct rte_acl_field));
388 /* Rule found: remove from table */
393 deleted_rule = acl->acl_rule_list[i];
394 acl->acl_rule_list[i] = NULL;
399 /* Return if rule not found */
400 if (pos_valid == 0) {
405 /* Build low level ACL table */
407 acl->acl_params.name = acl->name[acl->name_id];
408 status = rte_table_acl_build(acl, &ctx);
410 /* Roll back changes */
411 acl->acl_rule_list[pos] = deleted_rule;
418 if (acl->ctx != NULL)
419 rte_acl_free(acl->ctx);
424 memcpy(entry, &acl->memory[pos * acl->entry_size],
431 rte_table_acl_lookup(
433 struct rte_mbuf **pkts,
435 uint64_t *lookup_hit_mask,
438 struct rte_table_acl *acl = (struct rte_table_acl *) table;
439 const uint8_t *pkts_data[RTE_PORT_IN_BURST_SIZE_MAX];
440 uint32_t results[RTE_PORT_IN_BURST_SIZE_MAX];
441 uint64_t pkts_out_mask;
442 uint32_t n_pkts, i, j;
444 /* Input conversion */
445 for (i = 0, j = 0; i < (uint32_t)(RTE_PORT_IN_BURST_SIZE_MAX -
446 __builtin_clzll(pkts_mask)); i++) {
447 uint64_t pkt_mask = 1LLU << i;
449 if (pkt_mask & pkts_mask) {
450 pkts_data[j] = rte_pktmbuf_mtod(pkts[i], uint8_t *);
456 /* Low-level ACL table lookup */
457 if (acl->ctx != NULL)
458 rte_acl_classify(acl->ctx, pkts_data, results, n_pkts, 1);
462 /* Output conversion */
464 for (i = 0; i < n_pkts; i++) {
465 uint32_t action_table_pos = results[i];
466 uint32_t pkt_pos = __builtin_ctzll(pkts_mask);
467 uint64_t pkt_mask = 1LLU << pkt_pos;
469 pkts_mask &= ~pkt_mask;
471 if (action_table_pos != RTE_ACL_INVALID_USERDATA) {
472 pkts_out_mask |= pkt_mask;
473 entries[pkt_pos] = (void *)
474 &acl->memory[action_table_pos *
476 rte_prefetch0(entries[pkt_pos]);
480 *lookup_hit_mask = pkts_out_mask;
485 struct rte_table_ops rte_table_acl_ops = {
486 .f_create = rte_table_acl_create,
487 .f_free = rte_table_acl_free,
488 .f_add = rte_table_acl_entry_add,
489 .f_delete = rte_table_acl_entry_delete,
490 .f_lookup = rte_table_acl_lookup,