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",
244 rte_table_acl_entry_add(
251 struct rte_table_acl
*acl
= (struct rte_table_acl
*) table
;
252 struct rte_table_acl_rule_add_params
*rule
=
253 (struct rte_table_acl_rule_add_params
*) key
;
254 struct rte_pipeline_acl_rule acl_rule
;
255 struct rte_acl_rule
*rule_location
;
256 struct rte_acl_ctx
*ctx
;
257 uint32_t free_pos
, free_pos_valid
, i
;
260 /* Check input parameters */
262 RTE_LOG(ERR
, TABLE
, "%s: table parameter is NULL\n", __func__
);
266 RTE_LOG(ERR
, TABLE
, "%s: key parameter is NULL\n", __func__
);
270 RTE_LOG(ERR
, TABLE
, "%s: entry parameter is NULL\n", __func__
);
273 if (key_found
== NULL
) {
274 RTE_LOG(ERR
, TABLE
, "%s: key_found parameter is NULL\n",
278 if (entry_ptr
== NULL
) {
279 RTE_LOG(ERR
, TABLE
, "%s: entry_ptr parameter is NULL\n",
283 if (rule
->priority
> RTE_ACL_MAX_PRIORITY
) {
284 RTE_LOG(ERR
, TABLE
, "%s: Priority is too high\n", __func__
);
288 /* Setup rule data structure */
289 memset(&acl_rule
, 0, sizeof(acl_rule
));
290 acl_rule
.data
.category_mask
= 1;
291 acl_rule
.data
.priority
= RTE_ACL_MAX_PRIORITY
- rule
->priority
;
292 acl_rule
.data
.userdata
= 0; /* To be set up later */
293 memcpy(&acl_rule
.field
[0],
294 &rule
->field_value
[0],
295 acl
->cfg
.num_fields
* sizeof(struct rte_acl_field
));
297 /* Look to see if the rule exists already in the table */
300 for (i
= 1; i
< acl
->n_rules
; i
++) {
301 if (acl
->acl_rule_list
[i
] == NULL
) {
302 if (free_pos_valid
== 0) {
310 /* Compare the key fields */
311 status
= memcmp(&acl
->acl_rule_list
[i
]->field
[0],
312 &rule
->field_value
[0],
313 acl
->cfg
.num_fields
* sizeof(struct rte_acl_field
));
315 /* Rule found: update data associated with the rule */
318 *entry_ptr
= &acl
->memory
[i
* acl
->entry_size
];
319 memcpy(*entry_ptr
, entry
, acl
->entry_size
);
325 /* Return if max rules */
326 if (free_pos_valid
== 0) {
327 RTE_LOG(ERR
, TABLE
, "%s: Max number of rules reached\n",
332 /* Add the new rule to the rule set */
333 acl_rule
.data
.userdata
= free_pos
;
334 rule_location
= (struct rte_acl_rule
*)
335 &acl
->acl_rule_memory
[free_pos
* acl
->acl_params
.rule_size
];
336 memcpy(rule_location
, &acl_rule
, acl
->acl_params
.rule_size
);
337 acl
->acl_rule_list
[free_pos
] = rule_location
;
339 /* Build low level ACL table */
341 acl
->acl_params
.name
= acl
->name
[acl
->name_id
];
342 status
= rte_table_acl_build(acl
, &ctx
);
344 /* Roll back changes */
345 acl
->acl_rule_list
[free_pos
] = NULL
;
352 if (acl
->ctx
!= NULL
)
353 rte_acl_free(acl
->ctx
);
356 *entry_ptr
= &acl
->memory
[free_pos
* acl
->entry_size
];
357 memcpy(*entry_ptr
, entry
, acl
->entry_size
);
363 rte_table_acl_entry_delete(
369 struct rte_table_acl
*acl
= (struct rte_table_acl
*) table
;
370 struct rte_table_acl_rule_delete_params
*rule
=
371 (struct rte_table_acl_rule_delete_params
*) key
;
372 struct rte_acl_rule
*deleted_rule
= NULL
;
373 struct rte_acl_ctx
*ctx
;
374 uint32_t pos
, pos_valid
, i
;
377 /* Check input parameters */
379 RTE_LOG(ERR
, TABLE
, "%s: table parameter is NULL\n", __func__
);
383 RTE_LOG(ERR
, TABLE
, "%s: key parameter is NULL\n", __func__
);
386 if (key_found
== NULL
) {
387 RTE_LOG(ERR
, TABLE
, "%s: key_found parameter is NULL\n",
392 /* Look for the rule in the table */
395 for (i
= 1; i
< acl
->n_rules
; i
++) {
396 if (acl
->acl_rule_list
[i
] != NULL
) {
397 /* Compare the key fields */
398 status
= memcmp(&acl
->acl_rule_list
[i
]->field
[0],
399 &rule
->field_value
[0], acl
->cfg
.num_fields
*
400 sizeof(struct rte_acl_field
));
402 /* Rule found: remove from table */
407 deleted_rule
= acl
->acl_rule_list
[i
];
408 acl
->acl_rule_list
[i
] = NULL
;
413 /* Return if rule not found */
414 if (pos_valid
== 0) {
419 /* Build low level ACL table */
421 acl
->acl_params
.name
= acl
->name
[acl
->name_id
];
422 status
= rte_table_acl_build(acl
, &ctx
);
424 /* Roll back changes */
425 acl
->acl_rule_list
[pos
] = deleted_rule
;
432 if (acl
->ctx
!= NULL
)
433 rte_acl_free(acl
->ctx
);
438 memcpy(entry
, &acl
->memory
[pos
* acl
->entry_size
],
445 rte_table_acl_entry_add_bulk(
453 struct rte_table_acl
*acl
= (struct rte_table_acl
*) table
;
454 struct rte_acl_ctx
*ctx
;
455 uint32_t rule_pos
[n_keys
];
457 int err
= 0, build
= 0;
460 /* Check input parameters */
462 RTE_LOG(ERR
, TABLE
, "%s: table parameter is NULL\n", __func__
);
466 RTE_LOG(ERR
, TABLE
, "%s: keys parameter is NULL\n", __func__
);
469 if (entries
== NULL
) {
470 RTE_LOG(ERR
, TABLE
, "%s: entries parameter is NULL\n", __func__
);
474 RTE_LOG(ERR
, TABLE
, "%s: 0 rules to add\n", __func__
);
477 if (key_found
== NULL
) {
478 RTE_LOG(ERR
, TABLE
, "%s: key_found parameter is NULL\n",
482 if (entries_ptr
== NULL
) {
483 RTE_LOG(ERR
, TABLE
, "%s: entries_ptr parameter is NULL\n",
488 /* Check input parameters in arrays */
489 for (i
= 0; i
< n_keys
; i
++) {
490 struct rte_table_acl_rule_add_params
*rule
;
492 if (keys
[i
] == NULL
) {
493 RTE_LOG(ERR
, TABLE
, "%s: keys[%" PRIu32
"] parameter is NULL\n",
498 if (entries
[i
] == NULL
) {
499 RTE_LOG(ERR
, TABLE
, "%s: entries[%" PRIu32
"] parameter is NULL\n",
504 if (entries_ptr
[i
] == NULL
) {
505 RTE_LOG(ERR
, TABLE
, "%s: entries_ptr[%" PRIu32
"] parameter is NULL\n",
510 rule
= (struct rte_table_acl_rule_add_params
*) keys
[i
];
511 if (rule
->priority
> RTE_ACL_MAX_PRIORITY
) {
512 RTE_LOG(ERR
, TABLE
, "%s: Priority is too high\n", __func__
);
517 memset(rule_pos
, 0, n_keys
* sizeof(uint32_t));
518 memset(key_found
, 0, n_keys
* sizeof(int));
519 for (i
= 0; i
< n_keys
; i
++) {
520 struct rte_table_acl_rule_add_params
*rule
=
521 (struct rte_table_acl_rule_add_params
*) keys
[i
];
522 struct rte_pipeline_acl_rule acl_rule
;
523 struct rte_acl_rule
*rule_location
;
524 uint32_t free_pos
, free_pos_valid
, j
;
526 /* Setup rule data structure */
527 memset(&acl_rule
, 0, sizeof(acl_rule
));
528 acl_rule
.data
.category_mask
= 1;
529 acl_rule
.data
.priority
= RTE_ACL_MAX_PRIORITY
- rule
->priority
;
530 acl_rule
.data
.userdata
= 0; /* To be set up later */
531 memcpy(&acl_rule
.field
[0],
532 &rule
->field_value
[0],
533 acl
->cfg
.num_fields
* sizeof(struct rte_acl_field
));
535 /* Look to see if the rule exists already in the table */
538 for (j
= 1; j
< acl
->n_rules
; j
++) {
539 if (acl
->acl_rule_list
[j
] == NULL
) {
540 if (free_pos_valid
== 0) {
548 /* Compare the key fields */
549 status
= memcmp(&acl
->acl_rule_list
[j
]->field
[0],
550 &rule
->field_value
[0],
551 acl
->cfg
.num_fields
* sizeof(struct rte_acl_field
));
553 /* Rule found: update data associated with the rule */
556 entries_ptr
[i
] = &acl
->memory
[j
* acl
->entry_size
];
557 memcpy(entries_ptr
[i
], entries
[i
], acl
->entry_size
);
563 /* Key already in the table */
564 if (key_found
[i
] != 0)
567 /* Maximum number of rules reached */
568 if (free_pos_valid
== 0) {
573 /* Add the new rule to the rule set */
574 acl_rule
.data
.userdata
= free_pos
;
575 rule_location
= (struct rte_acl_rule
*)
576 &acl
->acl_rule_memory
[free_pos
* acl
->acl_params
.rule_size
];
577 memcpy(rule_location
, &acl_rule
, acl
->acl_params
.rule_size
);
578 acl
->acl_rule_list
[free_pos
] = rule_location
;
579 rule_pos
[i
] = free_pos
;
584 for (i
= 0; i
< n_keys
; i
++) {
585 if (rule_pos
[i
] == 0)
588 acl
->acl_rule_list
[rule_pos
[i
]] = NULL
;
597 /* Build low level ACL table */
599 acl
->acl_params
.name
= acl
->name
[acl
->name_id
];
600 status
= rte_table_acl_build(acl
, &ctx
);
602 /* Roll back changes */
603 for (i
= 0; i
< n_keys
; i
++) {
604 if (rule_pos
[i
] == 0)
607 acl
->acl_rule_list
[rule_pos
[i
]] = NULL
;
615 if (acl
->ctx
!= NULL
)
616 rte_acl_free(acl
->ctx
);
619 for (i
= 0; i
< n_keys
; i
++) {
620 if (rule_pos
[i
] == 0)
624 entries_ptr
[i
] = &acl
->memory
[rule_pos
[i
] * acl
->entry_size
];
625 memcpy(entries_ptr
[i
], entries
[i
], acl
->entry_size
);
632 rte_table_acl_entry_delete_bulk(
639 struct rte_table_acl
*acl
= (struct rte_table_acl
*) table
;
640 struct rte_acl_rule
*deleted_rules
[n_keys
];
641 uint32_t rule_pos
[n_keys
];
642 struct rte_acl_ctx
*ctx
;
647 /* Check input parameters */
649 RTE_LOG(ERR
, TABLE
, "%s: table parameter is NULL\n", __func__
);
653 RTE_LOG(ERR
, TABLE
, "%s: key parameter is NULL\n", __func__
);
657 RTE_LOG(ERR
, TABLE
, "%s: 0 rules to delete\n", __func__
);
660 if (key_found
== NULL
) {
661 RTE_LOG(ERR
, TABLE
, "%s: key_found parameter is NULL\n",
666 for (i
= 0; i
< n_keys
; i
++) {
667 if (keys
[i
] == NULL
) {
668 RTE_LOG(ERR
, TABLE
, "%s: keys[%" PRIu32
"] parameter is NULL\n",
674 memset(deleted_rules
, 0, n_keys
* sizeof(struct rte_acl_rule
*));
675 memset(rule_pos
, 0, n_keys
* sizeof(uint32_t));
676 for (i
= 0; i
< n_keys
; i
++) {
677 struct rte_table_acl_rule_delete_params
*rule
=
678 (struct rte_table_acl_rule_delete_params
*) keys
[i
];
679 uint32_t pos_valid
, j
;
681 /* Look for the rule in the table */
683 for (j
= 1; j
< acl
->n_rules
; j
++) {
684 if (acl
->acl_rule_list
[j
] == NULL
)
687 /* Compare the key fields */
688 status
= memcmp(&acl
->acl_rule_list
[j
]->field
[0],
689 &rule
->field_value
[0],
690 acl
->cfg
.num_fields
* sizeof(struct rte_acl_field
));
692 /* Rule found: remove from table */
696 deleted_rules
[i
] = acl
->acl_rule_list
[j
];
697 acl
->acl_rule_list
[j
] = NULL
;
704 if (pos_valid
== 0) {
710 /* Return if no changes to acl table */
715 /* Build low level ACL table */
717 acl
->acl_params
.name
= acl
->name
[acl
->name_id
];
718 status
= rte_table_acl_build(acl
, &ctx
);
720 /* Roll back changes */
721 for (i
= 0; i
< n_keys
; i
++) {
722 if (rule_pos
[i
] == 0)
725 acl
->acl_rule_list
[rule_pos
[i
]] = deleted_rules
[i
];
734 if (acl
->ctx
!= NULL
)
735 rte_acl_free(acl
->ctx
);
738 for (i
= 0; i
< n_keys
; i
++) {
739 if (rule_pos
[i
] == 0)
743 if (entries
!= NULL
&& entries
[i
] != NULL
)
744 memcpy(entries
[i
], &acl
->memory
[rule_pos
[i
] * acl
->entry_size
],
752 rte_table_acl_lookup(
754 struct rte_mbuf
**pkts
,
756 uint64_t *lookup_hit_mask
,
759 struct rte_table_acl
*acl
= (struct rte_table_acl
*) table
;
760 const uint8_t *pkts_data
[RTE_PORT_IN_BURST_SIZE_MAX
];
761 uint32_t results
[RTE_PORT_IN_BURST_SIZE_MAX
];
762 uint64_t pkts_out_mask
;
763 uint32_t n_pkts
, i
, j
;
765 __rte_unused
uint32_t n_pkts_in
= __builtin_popcountll(pkts_mask
);
766 RTE_TABLE_ACL_STATS_PKTS_IN_ADD(acl
, n_pkts_in
);
768 /* Input conversion */
769 for (i
= 0, j
= 0; i
< (uint32_t)(RTE_PORT_IN_BURST_SIZE_MAX
-
770 __builtin_clzll(pkts_mask
)); i
++) {
771 uint64_t pkt_mask
= 1LLU << i
;
773 if (pkt_mask
& pkts_mask
) {
774 pkts_data
[j
] = rte_pktmbuf_mtod(pkts
[i
], uint8_t *);
780 /* Low-level ACL table lookup */
781 if (acl
->ctx
!= NULL
)
782 rte_acl_classify(acl
->ctx
, pkts_data
, results
, n_pkts
, 1);
786 /* Output conversion */
788 for (i
= 0; i
< n_pkts
; i
++) {
789 uint32_t action_table_pos
= results
[i
];
790 uint32_t pkt_pos
= __builtin_ctzll(pkts_mask
);
791 uint64_t pkt_mask
= 1LLU << pkt_pos
;
793 pkts_mask
&= ~pkt_mask
;
795 if (action_table_pos
!= RTE_ACL_INVALID_USERDATA
) {
796 pkts_out_mask
|= pkt_mask
;
797 entries
[pkt_pos
] = (void *)
798 &acl
->memory
[action_table_pos
*
800 rte_prefetch0(entries
[pkt_pos
]);
804 *lookup_hit_mask
= pkts_out_mask
;
805 RTE_TABLE_ACL_STATS_PKTS_LOOKUP_MISS(acl
, n_pkts_in
- __builtin_popcountll(pkts_out_mask
));
811 rte_table_acl_stats_read(void *table
, struct rte_table_stats
*stats
, int clear
)
813 struct rte_table_acl
*acl
= (struct rte_table_acl
*) table
;
816 memcpy(stats
, &acl
->stats
, sizeof(acl
->stats
));
819 memset(&acl
->stats
, 0, sizeof(acl
->stats
));
824 struct rte_table_ops rte_table_acl_ops
= {
825 .f_create
= rte_table_acl_create
,
826 .f_free
= rte_table_acl_free
,
827 .f_add
= rte_table_acl_entry_add
,
828 .f_delete
= rte_table_acl_entry_delete
,
829 .f_add_bulk
= rte_table_acl_entry_add_bulk
,
830 .f_delete_bulk
= rte_table_acl_entry_delete_bulk
,
831 .f_lookup
= rte_table_acl_lookup
,
832 .f_stats
= rte_table_acl_stats_read
,