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 #ifdef RTE_TABLE_STATS_COLLECT
48 #define RTE_TABLE_ACL_STATS_PKTS_IN_ADD(table, val) \
49 table->stats.n_pkts_in += val
50 #define RTE_TABLE_ACL_STATS_PKTS_LOOKUP_MISS(table, val) \
51 table->stats.n_pkts_lookup_miss += val
55 #define RTE_TABLE_ACL_STATS_PKTS_IN_ADD(table, val)
56 #define RTE_TABLE_ACL_STATS_PKTS_LOOKUP_MISS(table, val)
60 struct rte_table_acl {
61 struct rte_table_stats stats;
63 /* Low-level ACL table */
64 char name[2][RTE_ACL_NAMESIZE];
65 struct rte_acl_param acl_params; /* for creating low level acl table */
66 struct rte_acl_config cfg; /* Holds the field definitions (metadata) */
67 struct rte_acl_ctx *ctx;
70 /* Input parameters */
75 uint8_t *action_table;
76 struct rte_acl_rule **acl_rule_list; /* Array of pointers to rules */
77 uint8_t *acl_rule_memory; /* Memory to store the rules */
79 /* Memory to store the action table and stack of free entries */
80 uint8_t memory[0] __rte_cache_aligned;
90 struct rte_table_acl_params *p = (struct rte_table_acl_params *) params;
91 struct rte_table_acl *acl;
92 uint32_t action_table_size, acl_rule_list_size, acl_rule_memory_size;
95 RTE_BUILD_BUG_ON(((sizeof(struct rte_table_acl) % RTE_CACHE_LINE_SIZE)
98 /* Check input parameters */
100 RTE_LOG(ERR, TABLE, "%s: Invalid value for params\n", __func__);
103 if (p->name == NULL) {
104 RTE_LOG(ERR, TABLE, "%s: Invalid value for name\n", __func__);
107 if (p->n_rules == 0) {
108 RTE_LOG(ERR, TABLE, "%s: Invalid value for n_rules\n",
112 if ((p->n_rule_fields == 0) ||
113 (p->n_rule_fields > RTE_ACL_MAX_FIELDS)) {
114 RTE_LOG(ERR, TABLE, "%s: Invalid value for n_rule_fields\n",
119 entry_size = RTE_ALIGN(entry_size, sizeof(uint64_t));
121 /* Memory allocation */
122 action_table_size = RTE_CACHE_LINE_ROUNDUP(p->n_rules * entry_size);
124 RTE_CACHE_LINE_ROUNDUP(p->n_rules * sizeof(struct rte_acl_rule *));
125 acl_rule_memory_size = RTE_CACHE_LINE_ROUNDUP(p->n_rules *
126 RTE_ACL_RULE_SZ(p->n_rule_fields));
127 total_size = sizeof(struct rte_table_acl) + action_table_size +
128 acl_rule_list_size + acl_rule_memory_size;
130 acl = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE,
134 "%s: Cannot allocate %u bytes for ACL table\n",
135 __func__, total_size);
139 acl->action_table = &acl->memory[0];
141 (struct rte_acl_rule **) &acl->memory[action_table_size];
142 acl->acl_rule_memory = (uint8_t *)
143 &acl->memory[action_table_size + acl_rule_list_size];
145 /* Initialization of internal fields */
146 snprintf(acl->name[0], RTE_ACL_NAMESIZE, "%s_a", p->name);
147 snprintf(acl->name[1], RTE_ACL_NAMESIZE, "%s_b", p->name);
150 acl->acl_params.name = acl->name[acl->name_id];
151 acl->acl_params.socket_id = socket_id;
152 acl->acl_params.rule_size = RTE_ACL_RULE_SZ(p->n_rule_fields);
153 acl->acl_params.max_rule_num = p->n_rules;
155 acl->cfg.num_categories = 1;
156 acl->cfg.num_fields = p->n_rule_fields;
157 memcpy(&acl->cfg.defs[0], &p->field_format[0],
158 p->n_rule_fields * sizeof(struct rte_acl_field_def));
162 acl->n_rules = p->n_rules;
163 acl->entry_size = entry_size;
169 rte_table_acl_free(void *table)
171 struct rte_table_acl *acl = (struct rte_table_acl *) table;
173 /* Check input parameters */
175 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
179 /* Free previously allocated resources */
180 if (acl->ctx != NULL)
181 rte_acl_free(acl->ctx);
188 RTE_ACL_RULE_DEF(rte_pipeline_acl_rule, RTE_ACL_MAX_FIELDS);
191 rte_table_acl_build(struct rte_table_acl *acl, struct rte_acl_ctx **acl_ctx)
193 struct rte_acl_ctx *ctx = NULL;
197 /* Create low level ACL table */
198 ctx = rte_acl_create(&acl->acl_params);
200 RTE_LOG(ERR, TABLE, "%s: Cannot create low level ACL table\n",
205 /* Add rules to low level ACL table */
207 for (i = 1; i < acl->n_rules; i++) {
208 if (acl->acl_rule_list[i] != NULL) {
209 status = rte_acl_add_rules(ctx, acl->acl_rule_list[i],
213 "%s: Cannot add rule to low level ACL table\n",
229 /* Build low level ACl table */
230 status = rte_acl_build(ctx, &acl->cfg);
233 "%s: Cannot build the low level ACL table\n",
246 rte_table_acl_entry_add(
253 struct rte_table_acl *acl = (struct rte_table_acl *) table;
254 struct rte_table_acl_rule_add_params *rule =
255 (struct rte_table_acl_rule_add_params *) key;
256 struct rte_pipeline_acl_rule acl_rule;
257 struct rte_acl_rule *rule_location;
258 struct rte_acl_ctx *ctx;
259 uint32_t free_pos, free_pos_valid, i;
262 /* Check input parameters */
264 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
268 RTE_LOG(ERR, TABLE, "%s: key parameter is NULL\n", __func__);
272 RTE_LOG(ERR, TABLE, "%s: entry parameter is NULL\n", __func__);
275 if (key_found == NULL) {
276 RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n",
280 if (entry_ptr == NULL) {
281 RTE_LOG(ERR, TABLE, "%s: entry_ptr parameter is NULL\n",
285 if (rule->priority > RTE_ACL_MAX_PRIORITY) {
286 RTE_LOG(ERR, TABLE, "%s: Priority is too high\n", __func__);
290 /* Setup rule data structure */
291 memset(&acl_rule, 0, sizeof(acl_rule));
292 acl_rule.data.category_mask = 1;
293 acl_rule.data.priority = RTE_ACL_MAX_PRIORITY - rule->priority;
294 acl_rule.data.userdata = 0; /* To be set up later */
295 memcpy(&acl_rule.field[0],
296 &rule->field_value[0],
297 acl->cfg.num_fields * sizeof(struct rte_acl_field));
299 /* Look to see if the rule exists already in the table */
302 for (i = 1; i < acl->n_rules; i++) {
303 if (acl->acl_rule_list[i] == NULL) {
304 if (free_pos_valid == 0) {
312 /* Compare the key fields */
313 status = memcmp(&acl->acl_rule_list[i]->field[0],
314 &rule->field_value[0],
315 acl->cfg.num_fields * sizeof(struct rte_acl_field));
317 /* Rule found: update data associated with the rule */
320 *entry_ptr = &acl->memory[i * acl->entry_size];
321 memcpy(*entry_ptr, entry, acl->entry_size);
327 /* Return if max rules */
328 if (free_pos_valid == 0) {
329 RTE_LOG(ERR, TABLE, "%s: Max number of rules reached\n",
334 /* Add the new rule to the rule set */
335 acl_rule.data.userdata = free_pos;
336 rule_location = (struct rte_acl_rule *)
337 &acl->acl_rule_memory[free_pos * acl->acl_params.rule_size];
338 memcpy(rule_location, &acl_rule, acl->acl_params.rule_size);
339 acl->acl_rule_list[free_pos] = rule_location;
341 /* Build low level ACL table */
343 acl->acl_params.name = acl->name[acl->name_id];
344 status = rte_table_acl_build(acl, &ctx);
346 /* Roll back changes */
347 acl->acl_rule_list[free_pos] = NULL;
354 if (acl->ctx != NULL)
355 rte_acl_free(acl->ctx);
358 *entry_ptr = &acl->memory[free_pos * acl->entry_size];
359 memcpy(*entry_ptr, entry, acl->entry_size);
365 rte_table_acl_entry_delete(
371 struct rte_table_acl *acl = (struct rte_table_acl *) table;
372 struct rte_table_acl_rule_delete_params *rule =
373 (struct rte_table_acl_rule_delete_params *) key;
374 struct rte_acl_rule *deleted_rule = NULL;
375 struct rte_acl_ctx *ctx;
376 uint32_t pos, pos_valid, i;
379 /* Check input parameters */
381 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
385 RTE_LOG(ERR, TABLE, "%s: key parameter is NULL\n", __func__);
388 if (key_found == NULL) {
389 RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n",
394 /* Look for the rule in the table */
397 for (i = 1; i < acl->n_rules; i++) {
398 if (acl->acl_rule_list[i] != NULL) {
399 /* Compare the key fields */
400 status = memcmp(&acl->acl_rule_list[i]->field[0],
401 &rule->field_value[0], acl->cfg.num_fields *
402 sizeof(struct rte_acl_field));
404 /* Rule found: remove from table */
409 deleted_rule = acl->acl_rule_list[i];
410 acl->acl_rule_list[i] = NULL;
415 /* Return if rule not found */
416 if (pos_valid == 0) {
421 /* Build low level ACL table */
423 acl->acl_params.name = acl->name[acl->name_id];
424 status = rte_table_acl_build(acl, &ctx);
426 /* Roll back changes */
427 acl->acl_rule_list[pos] = deleted_rule;
434 if (acl->ctx != NULL)
435 rte_acl_free(acl->ctx);
440 memcpy(entry, &acl->memory[pos * acl->entry_size],
447 rte_table_acl_lookup(
449 struct rte_mbuf **pkts,
451 uint64_t *lookup_hit_mask,
454 struct rte_table_acl *acl = (struct rte_table_acl *) table;
455 const uint8_t *pkts_data[RTE_PORT_IN_BURST_SIZE_MAX];
456 uint32_t results[RTE_PORT_IN_BURST_SIZE_MAX];
457 uint64_t pkts_out_mask;
458 uint32_t n_pkts, i, j;
460 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
461 RTE_TABLE_ACL_STATS_PKTS_IN_ADD(acl, n_pkts_in);
463 /* Input conversion */
464 for (i = 0, j = 0; i < (uint32_t)(RTE_PORT_IN_BURST_SIZE_MAX -
465 __builtin_clzll(pkts_mask)); i++) {
466 uint64_t pkt_mask = 1LLU << i;
468 if (pkt_mask & pkts_mask) {
469 pkts_data[j] = rte_pktmbuf_mtod(pkts[i], uint8_t *);
475 /* Low-level ACL table lookup */
476 if (acl->ctx != NULL)
477 rte_acl_classify(acl->ctx, pkts_data, results, n_pkts, 1);
481 /* Output conversion */
483 for (i = 0; i < n_pkts; i++) {
484 uint32_t action_table_pos = results[i];
485 uint32_t pkt_pos = __builtin_ctzll(pkts_mask);
486 uint64_t pkt_mask = 1LLU << pkt_pos;
488 pkts_mask &= ~pkt_mask;
490 if (action_table_pos != RTE_ACL_INVALID_USERDATA) {
491 pkts_out_mask |= pkt_mask;
492 entries[pkt_pos] = (void *)
493 &acl->memory[action_table_pos *
495 rte_prefetch0(entries[pkt_pos]);
499 *lookup_hit_mask = pkts_out_mask;
500 RTE_TABLE_ACL_STATS_PKTS_LOOKUP_MISS(acl, n_pkts_in - __builtin_popcountll(pkts_out_mask));
506 rte_table_acl_stats_read(void *table, struct rte_table_stats *stats, int clear)
508 struct rte_table_acl *acl = (struct rte_table_acl *) table;
511 memcpy(stats, &acl->stats, sizeof(acl->stats));
514 memset(&acl->stats, 0, sizeof(acl->stats));
519 struct rte_table_ops rte_table_acl_ops = {
520 .f_create = rte_table_acl_create,
521 .f_free = rte_table_acl_free,
522 .f_add = rte_table_acl_entry_add,
523 .f_delete = rte_table_acl_entry_delete,
524 .f_lookup = rte_table_acl_lookup,
525 .f_stats = rte_table_acl_stats_read,