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_entry_add_bulk(
455 struct rte_table_acl *acl = (struct rte_table_acl *) table;
456 struct rte_acl_ctx *ctx;
457 uint32_t rule_pos[n_keys];
459 int err = 0, build = 0;
462 /* Check input parameters */
464 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
468 RTE_LOG(ERR, TABLE, "%s: keys parameter is NULL\n", __func__);
471 if (entries == NULL) {
472 RTE_LOG(ERR, TABLE, "%s: entries parameter is NULL\n", __func__);
476 RTE_LOG(ERR, TABLE, "%s: 0 rules to add\n", __func__);
479 if (key_found == NULL) {
480 RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n",
484 if (entries_ptr == NULL) {
485 RTE_LOG(ERR, TABLE, "%s: entries_ptr parameter is NULL\n",
490 /* Check input parameters in arrays */
491 for (i = 0; i < n_keys; i++) {
492 struct rte_table_acl_rule_add_params *rule;
494 if (keys[i] == NULL) {
495 RTE_LOG(ERR, TABLE, "%s: keys[%" PRIu32 "] parameter is NULL\n",
500 if (entries[i] == NULL) {
501 RTE_LOG(ERR, TABLE, "%s: entries[%" PRIu32 "] parameter is NULL\n",
506 if (entries_ptr[i] == NULL) {
507 RTE_LOG(ERR, TABLE, "%s: entries_ptr[%" PRIu32 "] parameter is NULL\n",
512 rule = (struct rte_table_acl_rule_add_params *) keys[i];
513 if (rule->priority > RTE_ACL_MAX_PRIORITY) {
514 RTE_LOG(ERR, TABLE, "%s: Priority is too high\n", __func__);
519 memset(rule_pos, 0, n_keys * sizeof(uint32_t));
520 memset(key_found, 0, n_keys * sizeof(int));
521 for (i = 0; i < n_keys; i++) {
522 struct rte_table_acl_rule_add_params *rule =
523 (struct rte_table_acl_rule_add_params *) keys[i];
524 struct rte_pipeline_acl_rule acl_rule;
525 struct rte_acl_rule *rule_location;
526 uint32_t free_pos, free_pos_valid, j;
528 /* Setup rule data structure */
529 memset(&acl_rule, 0, sizeof(acl_rule));
530 acl_rule.data.category_mask = 1;
531 acl_rule.data.priority = RTE_ACL_MAX_PRIORITY - rule->priority;
532 acl_rule.data.userdata = 0; /* To be set up later */
533 memcpy(&acl_rule.field[0],
534 &rule->field_value[0],
535 acl->cfg.num_fields * sizeof(struct rte_acl_field));
537 /* Look to see if the rule exists already in the table */
540 for (j = 1; j < acl->n_rules; j++) {
541 if (acl->acl_rule_list[j] == NULL) {
542 if (free_pos_valid == 0) {
550 /* Compare the key fields */
551 status = memcmp(&acl->acl_rule_list[j]->field[0],
552 &rule->field_value[0],
553 acl->cfg.num_fields * sizeof(struct rte_acl_field));
555 /* Rule found: update data associated with the rule */
558 entries_ptr[i] = &acl->memory[j * acl->entry_size];
559 memcpy(entries_ptr[i], entries[i], acl->entry_size);
565 /* Key already in the table */
566 if (key_found[i] != 0)
569 /* Maximum number of rules reached */
570 if (free_pos_valid == 0) {
575 /* Add the new rule to the rule set */
576 acl_rule.data.userdata = free_pos;
577 rule_location = (struct rte_acl_rule *)
578 &acl->acl_rule_memory[free_pos * acl->acl_params.rule_size];
579 memcpy(rule_location, &acl_rule, acl->acl_params.rule_size);
580 acl->acl_rule_list[free_pos] = rule_location;
581 rule_pos[i] = free_pos;
586 for (i = 0; i < n_keys; i++) {
587 if (rule_pos[i] == 0)
590 acl->acl_rule_list[rule_pos[i]] = NULL;
599 /* Build low level ACL table */
601 acl->acl_params.name = acl->name[acl->name_id];
602 status = rte_table_acl_build(acl, &ctx);
604 /* Roll back changes */
605 for (i = 0; i < n_keys; i++) {
606 if (rule_pos[i] == 0)
609 acl->acl_rule_list[rule_pos[i]] = NULL;
617 if (acl->ctx != NULL)
618 rte_acl_free(acl->ctx);
621 for (i = 0; i < n_keys; i++) {
622 if (rule_pos[i] == 0)
626 entries_ptr[i] = &acl->memory[rule_pos[i] * acl->entry_size];
627 memcpy(entries_ptr[i], entries[i], acl->entry_size);
634 rte_table_acl_entry_delete_bulk(
641 struct rte_table_acl *acl = (struct rte_table_acl *) table;
642 struct rte_acl_rule *deleted_rules[n_keys];
643 uint32_t rule_pos[n_keys];
644 struct rte_acl_ctx *ctx;
649 /* Check input parameters */
651 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
655 RTE_LOG(ERR, TABLE, "%s: key parameter is NULL\n", __func__);
659 RTE_LOG(ERR, TABLE, "%s: 0 rules to delete\n", __func__);
662 if (key_found == NULL) {
663 RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n",
668 for (i = 0; i < n_keys; i++) {
669 if (keys[i] == NULL) {
670 RTE_LOG(ERR, TABLE, "%s: keys[%" PRIu32 "] parameter is NULL\n",
676 memset(deleted_rules, 0, n_keys * sizeof(struct rte_acl_rule *));
677 memset(rule_pos, 0, n_keys * sizeof(uint32_t));
678 for (i = 0; i < n_keys; i++) {
679 struct rte_table_acl_rule_delete_params *rule =
680 (struct rte_table_acl_rule_delete_params *) keys[i];
681 uint32_t pos_valid, j;
683 /* Look for the rule in the table */
685 for (j = 1; j < acl->n_rules; j++) {
686 if (acl->acl_rule_list[j] == NULL)
689 /* Compare the key fields */
690 status = memcmp(&acl->acl_rule_list[j]->field[0],
691 &rule->field_value[0],
692 acl->cfg.num_fields * sizeof(struct rte_acl_field));
694 /* Rule found: remove from table */
698 deleted_rules[i] = acl->acl_rule_list[j];
699 acl->acl_rule_list[j] = NULL;
706 if (pos_valid == 0) {
712 /* Return if no changes to acl table */
717 /* Build low level ACL table */
719 acl->acl_params.name = acl->name[acl->name_id];
720 status = rte_table_acl_build(acl, &ctx);
722 /* Roll back changes */
723 for (i = 0; i < n_keys; i++) {
724 if (rule_pos[i] == 0)
727 acl->acl_rule_list[rule_pos[i]] = deleted_rules[i];
736 if (acl->ctx != NULL)
737 rte_acl_free(acl->ctx);
740 for (i = 0; i < n_keys; i++) {
741 if (rule_pos[i] == 0)
745 if (entries != NULL && entries[i] != NULL)
746 memcpy(entries[i], &acl->memory[rule_pos[i] * acl->entry_size],
754 rte_table_acl_lookup(
756 struct rte_mbuf **pkts,
758 uint64_t *lookup_hit_mask,
761 struct rte_table_acl *acl = (struct rte_table_acl *) table;
762 const uint8_t *pkts_data[RTE_PORT_IN_BURST_SIZE_MAX];
763 uint32_t results[RTE_PORT_IN_BURST_SIZE_MAX];
764 uint64_t pkts_out_mask;
765 uint32_t n_pkts, i, j;
767 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
768 RTE_TABLE_ACL_STATS_PKTS_IN_ADD(acl, n_pkts_in);
770 /* Input conversion */
771 for (i = 0, j = 0; i < (uint32_t)(RTE_PORT_IN_BURST_SIZE_MAX -
772 __builtin_clzll(pkts_mask)); i++) {
773 uint64_t pkt_mask = 1LLU << i;
775 if (pkt_mask & pkts_mask) {
776 pkts_data[j] = rte_pktmbuf_mtod(pkts[i], uint8_t *);
782 /* Low-level ACL table lookup */
783 if (acl->ctx != NULL)
784 rte_acl_classify(acl->ctx, pkts_data, results, n_pkts, 1);
788 /* Output conversion */
790 for (i = 0; i < n_pkts; i++) {
791 uint32_t action_table_pos = results[i];
792 uint32_t pkt_pos = __builtin_ctzll(pkts_mask);
793 uint64_t pkt_mask = 1LLU << pkt_pos;
795 pkts_mask &= ~pkt_mask;
797 if (action_table_pos != RTE_ACL_INVALID_USERDATA) {
798 pkts_out_mask |= pkt_mask;
799 entries[pkt_pos] = (void *)
800 &acl->memory[action_table_pos *
802 rte_prefetch0(entries[pkt_pos]);
806 *lookup_hit_mask = pkts_out_mask;
807 RTE_TABLE_ACL_STATS_PKTS_LOOKUP_MISS(acl, n_pkts_in - __builtin_popcountll(pkts_out_mask));
813 rte_table_acl_stats_read(void *table, struct rte_table_stats *stats, int clear)
815 struct rte_table_acl *acl = (struct rte_table_acl *) table;
818 memcpy(stats, &acl->stats, sizeof(acl->stats));
821 memset(&acl->stats, 0, sizeof(acl->stats));
826 struct rte_table_ops rte_table_acl_ops = {
827 .f_create = rte_table_acl_create,
828 .f_free = rte_table_acl_free,
829 .f_add = rte_table_acl_entry_add,
830 .f_delete = rte_table_acl_entry_delete,
831 .f_add_bulk = rte_table_acl_entry_add_bulk,
832 .f_delete_bulk = rte_table_acl_entry_delete_bulk,
833 .f_lookup = rte_table_acl_lookup,
834 .f_stats = rte_table_acl_stats_read,