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_malloc.h>
42 #include "rte_table_acl.h"
43 #include <rte_ether.h>
45 struct rte_table_acl {
46 /* Low-level ACL table */
47 char name[2][RTE_ACL_NAMESIZE];
48 struct rte_acl_param acl_params; /* for creating low level acl table */
49 struct rte_acl_config cfg; /* Holds the field definitions (metadata) */
50 struct rte_acl_ctx *ctx;
53 /* Input parameters */
58 uint8_t *action_table;
59 struct rte_acl_rule **acl_rule_list; /* Array of pointers to rules */
60 uint8_t *acl_rule_memory; /* Memory to store the rules */
62 /* Memory to store the action table and stack of free entries */
63 uint8_t memory[0] __rte_cache_aligned;
73 struct rte_table_acl_params *p = (struct rte_table_acl_params *) params;
74 struct rte_table_acl *acl;
75 uint32_t action_table_size, acl_rule_list_size, acl_rule_memory_size;
78 RTE_BUILD_BUG_ON(((sizeof(struct rte_table_acl) % RTE_CACHE_LINE_SIZE)
81 /* Check input parameters */
83 RTE_LOG(ERR, TABLE, "%s: Invalid value for params\n", __func__);
86 if (p->name == NULL) {
87 RTE_LOG(ERR, TABLE, "%s: Invalid value for name\n", __func__);
90 if (p->n_rules == 0) {
91 RTE_LOG(ERR, TABLE, "%s: Invalid value for n_rules\n",
95 if ((p->n_rule_fields == 0) ||
96 (p->n_rule_fields > RTE_ACL_MAX_FIELDS)) {
97 RTE_LOG(ERR, TABLE, "%s: Invalid value for n_rule_fields\n",
102 entry_size = RTE_ALIGN(entry_size, sizeof(uint64_t));
104 /* Memory allocation */
105 action_table_size = RTE_CACHE_LINE_ROUNDUP(p->n_rules * entry_size);
107 RTE_CACHE_LINE_ROUNDUP(p->n_rules * sizeof(struct rte_acl_rule *));
108 acl_rule_memory_size = RTE_CACHE_LINE_ROUNDUP(p->n_rules *
109 RTE_ACL_RULE_SZ(p->n_rule_fields));
110 total_size = sizeof(struct rte_table_acl) + action_table_size +
111 acl_rule_list_size + acl_rule_memory_size;
113 acl = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE,
117 "%s: Cannot allocate %u bytes for ACL table\n",
118 __func__, total_size);
122 acl->action_table = &acl->memory[0];
124 (struct rte_acl_rule **) &acl->memory[action_table_size];
125 acl->acl_rule_memory = (uint8_t *)
126 &acl->memory[action_table_size + acl_rule_list_size];
128 /* Initialization of internal fields */
129 snprintf(acl->name[0], RTE_ACL_NAMESIZE, "%s_a", p->name);
130 snprintf(acl->name[1], RTE_ACL_NAMESIZE, "%s_b", p->name);
133 acl->acl_params.name = acl->name[acl->name_id];
134 acl->acl_params.socket_id = socket_id;
135 acl->acl_params.rule_size = RTE_ACL_RULE_SZ(p->n_rule_fields);
136 acl->acl_params.max_rule_num = p->n_rules;
138 acl->cfg.num_categories = 1;
139 acl->cfg.num_fields = p->n_rule_fields;
140 memcpy(&acl->cfg.defs[0], &p->field_format[0],
141 p->n_rule_fields * sizeof(struct rte_acl_field_def));
145 acl->n_rules = p->n_rules;
146 acl->entry_size = entry_size;
152 rte_table_acl_free(void *table)
154 struct rte_table_acl *acl = (struct rte_table_acl *) table;
156 /* Check input parameters */
158 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
162 /* Free previously allocated resources */
163 if (acl->ctx != NULL)
164 rte_acl_free(acl->ctx);
171 RTE_ACL_RULE_DEF(rte_pipeline_acl_rule, RTE_ACL_MAX_FIELDS);
174 rte_table_acl_build(struct rte_table_acl *acl, struct rte_acl_ctx **acl_ctx)
176 struct rte_acl_ctx *ctx = NULL;
180 /* Create low level ACL table */
181 ctx = rte_acl_create(&acl->acl_params);
183 RTE_LOG(ERR, TABLE, "%s: Cannot create low level ACL table\n",
188 /* Add rules to low level ACL table */
190 for (i = 1; i < acl->n_rules; i++) {
191 if (acl->acl_rule_list[i] != NULL) {
192 status = rte_acl_add_rules(ctx, acl->acl_rule_list[i],
196 "%s: Cannot add rule to low level ACL table\n",
212 /* Build low level ACl table */
213 status = rte_acl_build(ctx, &acl->cfg);
216 "%s: Cannot build the low level ACL table\n",
229 rte_table_acl_entry_add(
236 struct rte_table_acl *acl = (struct rte_table_acl *) table;
237 struct rte_table_acl_rule_add_params *rule =
238 (struct rte_table_acl_rule_add_params *) key;
239 struct rte_pipeline_acl_rule acl_rule;
240 struct rte_acl_rule *rule_location;
241 struct rte_acl_ctx *ctx;
242 uint32_t free_pos, free_pos_valid, i;
245 /* Check input parameters */
247 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
251 RTE_LOG(ERR, TABLE, "%s: key parameter is NULL\n", __func__);
255 RTE_LOG(ERR, TABLE, "%s: entry parameter is NULL\n", __func__);
258 if (key_found == NULL) {
259 RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n",
263 if (entry_ptr == NULL) {
264 RTE_LOG(ERR, TABLE, "%s: entry_ptr parameter is NULL\n",
268 if (rule->priority > RTE_ACL_MAX_PRIORITY) {
269 RTE_LOG(ERR, TABLE, "%s: Priority is too high\n", __func__);
273 /* Setup rule data structure */
274 memset(&acl_rule, 0, sizeof(acl_rule));
275 acl_rule.data.category_mask = 1;
276 acl_rule.data.priority = RTE_ACL_MAX_PRIORITY - rule->priority;
277 acl_rule.data.userdata = 0; /* To be set up later */
278 memcpy(&acl_rule.field[0],
279 &rule->field_value[0],
280 acl->cfg.num_fields * sizeof(struct rte_acl_field));
282 /* Look to see if the rule exists already in the table */
285 for (i = 1; i < acl->n_rules; i++) {
286 if (acl->acl_rule_list[i] == NULL) {
287 if (free_pos_valid == 0) {
295 /* Compare the key fields */
296 status = memcmp(&acl->acl_rule_list[i]->field[0],
297 &rule->field_value[0],
298 acl->cfg.num_fields * sizeof(struct rte_acl_field));
300 /* Rule found: update data associated with the rule */
303 *entry_ptr = &acl->memory[i * acl->entry_size];
304 memcpy(*entry_ptr, entry, acl->entry_size);
310 /* Return if max rules */
311 if (free_pos_valid == 0) {
312 RTE_LOG(ERR, TABLE, "%s: Max number of rules reached\n",
317 /* Add the new rule to the rule set */
318 acl_rule.data.userdata = free_pos;
319 rule_location = (struct rte_acl_rule *)
320 &acl->acl_rule_memory[free_pos * acl->acl_params.rule_size];
321 memcpy(rule_location, &acl_rule, acl->acl_params.rule_size);
322 acl->acl_rule_list[free_pos] = rule_location;
324 /* Build low level ACL table */
326 acl->acl_params.name = acl->name[acl->name_id];
327 status = rte_table_acl_build(acl, &ctx);
329 /* Roll back changes */
330 acl->acl_rule_list[free_pos] = NULL;
337 if (acl->ctx != NULL)
338 rte_acl_free(acl->ctx);
341 *entry_ptr = &acl->memory[free_pos * acl->entry_size];
342 memcpy(*entry_ptr, entry, acl->entry_size);
348 rte_table_acl_entry_delete(
354 struct rte_table_acl *acl = (struct rte_table_acl *) table;
355 struct rte_table_acl_rule_delete_params *rule =
356 (struct rte_table_acl_rule_delete_params *) key;
357 struct rte_acl_rule *deleted_rule = NULL;
358 struct rte_acl_ctx *ctx;
359 uint32_t pos, pos_valid, i;
362 /* Check input parameters */
364 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
368 RTE_LOG(ERR, TABLE, "%s: key parameter is NULL\n", __func__);
371 if (key_found == NULL) {
372 RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n",
377 /* Look for the rule in the table */
380 for (i = 1; i < acl->n_rules; i++) {
381 if (acl->acl_rule_list[i] != NULL) {
382 /* Compare the key fields */
383 status = memcmp(&acl->acl_rule_list[i]->field[0],
384 &rule->field_value[0], acl->cfg.num_fields *
385 sizeof(struct rte_acl_field));
387 /* Rule found: remove from table */
392 deleted_rule = acl->acl_rule_list[i];
393 acl->acl_rule_list[i] = NULL;
398 /* Return if rule not found */
399 if (pos_valid == 0) {
404 /* Build low level ACL table */
406 acl->acl_params.name = acl->name[acl->name_id];
407 status = rte_table_acl_build(acl, &ctx);
409 /* Roll back changes */
410 acl->acl_rule_list[pos] = deleted_rule;
417 if (acl->ctx != NULL)
418 rte_acl_free(acl->ctx);
423 memcpy(entry, &acl->memory[pos * acl->entry_size],
430 rte_table_acl_lookup(
432 struct rte_mbuf **pkts,
434 uint64_t *lookup_hit_mask,
437 struct rte_table_acl *acl = (struct rte_table_acl *) table;
438 const uint8_t *pkts_data[RTE_PORT_IN_BURST_SIZE_MAX];
439 uint32_t results[RTE_PORT_IN_BURST_SIZE_MAX];
440 uint64_t pkts_out_mask;
441 uint32_t n_pkts, i, j;
443 /* Input conversion */
444 for (i = 0, j = 0; i < (uint32_t)(RTE_PORT_IN_BURST_SIZE_MAX -
445 __builtin_clzll(pkts_mask)); i++) {
446 uint64_t pkt_mask = 1LLU << i;
448 if (pkt_mask & pkts_mask) {
449 pkts_data[j] = rte_pktmbuf_mtod(pkts[i], uint8_t *);
455 /* Low-level ACL table lookup */
456 if (acl->ctx != NULL)
457 rte_acl_classify(acl->ctx, pkts_data, results, n_pkts, 1);
461 /* Output conversion */
463 for (i = 0; i < n_pkts; i++) {
464 uint32_t action_table_pos = results[i];
465 uint32_t pkt_pos = __builtin_ctzll(pkts_mask);
466 uint64_t pkt_mask = 1LLU << pkt_pos;
468 pkts_mask &= ~pkt_mask;
470 if (action_table_pos != RTE_ACL_INVALID_USERDATA) {
471 pkts_out_mask |= pkt_mask;
472 entries[pkt_pos] = (void *)
473 &acl->memory[action_table_pos *
475 rte_prefetch0(entries[pkt_pos]);
479 *lookup_hit_mask = pkts_out_mask;
484 struct rte_table_ops rte_table_acl_ops = {
485 .f_create = rte_table_acl_create,
486 .f_free = rte_table_acl_free,
487 .f_add = rte_table_acl_entry_add,
488 .f_delete = rte_table_acl_entry_delete,
489 .f_lookup = rte_table_acl_lookup,