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.
38 #define ACL_POOL_ALIGN 8
39 #define ACL_POOL_ALLOC_MIN 0x800000
41 /* number of pointers per alloc */
42 #define ACL_PTR_ALLOC 32
44 /* variable for dividing rule sets */
46 #define NODE_PERCENTAGE (0.40)
47 #define RULE_PERCENTAGE (0.40)
49 /* TALLY are statistics per field */
51 TALLY_0 = 0, /* number of rules that are 0% or more wild. */
52 TALLY_25, /* number of rules that are 25% or more wild. */
56 TALLY_DEACTIVATED, /* deactivated fields (100% wild in all rules). */
58 /* number of rules that are 100% wild for this field and higher. */
62 static const uint32_t wild_limits[TALLY_DEACTIVATED] = {0, 25, 50, 75, 100};
65 ACL_INTERSECT_NONE = 0,
66 ACL_INTERSECT_A = 1, /* set A is a superset of A and B intersect */
67 ACL_INTERSECT_B = 2, /* set B is a superset of A and B intersect */
68 ACL_INTERSECT = 4, /* sets A and B intersect */
72 ACL_PRIORITY_EQUAL = 0,
73 ACL_PRIORITY_NODE_A = 1,
74 ACL_PRIORITY_NODE_B = 2,
75 ACL_PRIORITY_MIXED = 3
79 struct acl_mem_block {
84 #define MEM_BLOCK_NUM 16
86 /* Single ACL rule, build representation.*/
87 struct rte_acl_build_rule {
88 struct rte_acl_build_rule *next;
89 struct rte_acl_config *config;
90 /**< configuration for each field in the rule. */
91 const struct rte_acl_rule *f;
95 /* Context for build phase */
96 struct acl_build_context {
97 const struct rte_acl_ctx *acx;
98 struct rte_acl_build_rule *build_rules;
99 struct rte_acl_config cfg;
102 uint32_t category_mask;
106 uint32_t num_build_rules;
108 struct tb_mem_pool pool;
109 struct rte_acl_trie tries[RTE_ACL_MAX_TRIES];
110 struct rte_acl_bld_trie bld_tries[RTE_ACL_MAX_TRIES];
111 uint32_t data_indexes[RTE_ACL_MAX_TRIES][RTE_ACL_MAX_FIELDS];
113 /* memory free lists for nodes and blocks used for node ptrs */
114 struct acl_mem_block blocks[MEM_BLOCK_NUM];
115 struct rte_acl_node *node_free_list;
118 static int acl_merge_trie(struct acl_build_context *context,
119 struct rte_acl_node *node_a, struct rte_acl_node *node_b,
120 uint32_t level, uint32_t subtree_id, struct rte_acl_node **node_c);
122 static int acl_merge(struct acl_build_context *context,
123 struct rte_acl_node *node_a, struct rte_acl_node *node_b,
124 int move, int a_subset, int level);
127 acl_deref_ptr(struct acl_build_context *context,
128 struct rte_acl_node *node, int index);
131 acl_build_alloc(struct acl_build_context *context, size_t n, size_t s)
135 size_t alloc_size = n * s;
138 * look for memory in free lists
140 for (m = 0; m < RTE_DIM(context->blocks); m++) {
141 if (context->blocks[m].block_size ==
142 alloc_size && context->blocks[m].mem_ptr != NULL) {
143 p = context->blocks[m].mem_ptr;
144 context->blocks[m].mem_ptr = *((void **)p);
145 memset(p, 0, alloc_size);
151 * return allocation from memory pool
153 p = tb_alloc(&context->pool, alloc_size);
158 * Free memory blocks (kept in context for reuse).
161 acl_build_free(struct acl_build_context *context, size_t s, void *p)
165 for (n = 0; n < RTE_DIM(context->blocks); n++) {
166 if (context->blocks[n].block_size == s) {
167 *((void **)p) = context->blocks[n].mem_ptr;
168 context->blocks[n].mem_ptr = p;
172 for (n = 0; n < RTE_DIM(context->blocks); n++) {
173 if (context->blocks[n].block_size == 0) {
174 context->blocks[n].block_size = s;
175 *((void **)p) = NULL;
176 context->blocks[n].mem_ptr = p;
183 * Allocate and initialize a new node.
185 static struct rte_acl_node *
186 acl_alloc_node(struct acl_build_context *context, int level)
188 struct rte_acl_node *node;
190 if (context->node_free_list != NULL) {
191 node = context->node_free_list;
192 context->node_free_list = node->next;
193 memset(node, 0, sizeof(struct rte_acl_node));
195 node = acl_build_alloc(context, sizeof(struct rte_acl_node), 1);
201 node->node_type = RTE_ACL_NODE_UNDEFINED;
202 node->node_index = RTE_ACL_NODE_UNDEFINED;
203 context->num_nodes++;
204 node->id = context->node_id++;
210 * Dereference all nodes to which this node points
213 acl_free_node(struct acl_build_context *context,
214 struct rte_acl_node *node)
218 if (node->prev != NULL)
219 node->prev->next = NULL;
220 for (n = 0; n < node->num_ptrs; n++)
221 acl_deref_ptr(context, node, n);
223 /* free mrt if this is a match node */
224 if (node->mrt != NULL) {
225 acl_build_free(context, sizeof(struct rte_acl_match_results),
230 /* free transitions to other nodes */
231 if (node->ptrs != NULL) {
232 acl_build_free(context,
233 node->max_ptrs * sizeof(struct rte_acl_ptr_set),
238 /* put it on the free list */
239 context->num_nodes--;
240 node->next = context->node_free_list;
241 context->node_free_list = node;
246 * Include src bitset in dst bitset
249 acl_include(struct rte_acl_bitset *dst, struct rte_acl_bitset *src, bits_t mask)
253 for (n = 0; n < RTE_ACL_BIT_SET_SIZE; n++)
254 dst->bits[n] = (dst->bits[n] & mask) | src->bits[n];
258 * Set dst to bits of src1 that are not in src2
261 acl_exclude(struct rte_acl_bitset *dst,
262 struct rte_acl_bitset *src1,
263 struct rte_acl_bitset *src2)
268 for (n = 0; n < RTE_ACL_BIT_SET_SIZE; n++) {
269 dst->bits[n] = src1->bits[n] & ~src2->bits[n];
270 all_bits |= dst->bits[n];
272 return all_bits != 0;
276 * Add a pointer (ptr) to a node.
279 acl_add_ptr(struct acl_build_context *context,
280 struct rte_acl_node *node,
281 struct rte_acl_node *ptr,
282 struct rte_acl_bitset *bits)
284 uint32_t n, num_ptrs;
285 struct rte_acl_ptr_set *ptrs = NULL;
288 * If there's already a pointer to the same node, just add to the bitset
290 for (n = 0; n < node->num_ptrs; n++) {
291 if (node->ptrs[n].ptr != NULL) {
292 if (node->ptrs[n].ptr == ptr) {
293 acl_include(&node->ptrs[n].values, bits, -1);
294 acl_include(&node->values, bits, -1);
300 /* if there's no room for another pointer, make room */
301 if (node->num_ptrs >= node->max_ptrs) {
302 /* add room for more pointers */
303 num_ptrs = node->max_ptrs + ACL_PTR_ALLOC;
304 ptrs = acl_build_alloc(context, num_ptrs, sizeof(*ptrs));
308 /* copy current points to new memory allocation */
309 if (node->ptrs != NULL) {
310 memcpy(ptrs, node->ptrs,
311 node->num_ptrs * sizeof(*ptrs));
312 acl_build_free(context, node->max_ptrs * sizeof(*ptrs),
316 node->max_ptrs = num_ptrs;
319 /* Find available ptr and add a new pointer to this node */
320 for (n = node->min_add; n < node->max_ptrs; n++) {
321 if (node->ptrs[n].ptr == NULL) {
322 node->ptrs[n].ptr = ptr;
323 acl_include(&node->ptrs[n].values, bits, 0);
324 acl_include(&node->values, bits, -1);
327 if (node->num_ptrs <= n)
328 node->num_ptrs = n + 1;
337 * Add a pointer for a range of values
340 acl_add_ptr_range(struct acl_build_context *context,
341 struct rte_acl_node *root,
342 struct rte_acl_node *node,
347 struct rte_acl_bitset bitset;
349 /* clear the bitset values */
350 for (n = 0; n < RTE_ACL_BIT_SET_SIZE; n++)
353 /* for each bit in range, add bit to set */
354 for (n = 0; n < UINT8_MAX + 1; n++)
355 if (n >= low && n <= high)
356 bitset.bits[n / (sizeof(bits_t) * 8)] |=
357 1 << (n % (sizeof(bits_t) * 8));
359 return acl_add_ptr(context, root, node, &bitset);
363 * Generate a bitset from a byte value and mask.
366 acl_gen_mask(struct rte_acl_bitset *bitset, uint32_t value, uint32_t mask)
371 /* clear the bitset values */
372 for (n = 0; n < RTE_ACL_BIT_SET_SIZE; n++)
375 /* for each bit in value/mask, add bit to set */
376 for (n = 0; n < UINT8_MAX + 1; n++) {
377 if ((n & mask) == value) {
379 bitset->bits[n / (sizeof(bits_t) * 8)] |=
380 1 << (n % (sizeof(bits_t) * 8));
387 * Determine how A and B intersect.
388 * Determine if A and/or B are supersets of the intersection.
391 acl_intersect_type(struct rte_acl_bitset *a_bits,
392 struct rte_acl_bitset *b_bits,
393 struct rte_acl_bitset *intersect)
396 bits_t intersect_bits = 0;
397 bits_t a_superset = 0;
398 bits_t b_superset = 0;
401 * calculate and store intersection and check if A and/or B have
402 * bits outside the intersection (superset)
404 for (n = 0; n < RTE_ACL_BIT_SET_SIZE; n++) {
405 intersect->bits[n] = a_bits->bits[n] & b_bits->bits[n];
406 a_superset |= a_bits->bits[n] ^ intersect->bits[n];
407 b_superset |= b_bits->bits[n] ^ intersect->bits[n];
408 intersect_bits |= intersect->bits[n];
411 n = (intersect_bits == 0 ? ACL_INTERSECT_NONE : ACL_INTERSECT) |
412 (b_superset == 0 ? 0 : ACL_INTERSECT_B) |
413 (a_superset == 0 ? 0 : ACL_INTERSECT_A);
419 * Check if all bits in the bitset are on
422 acl_full(struct rte_acl_node *node)
425 bits_t all_bits = -1;
427 for (n = 0; n < RTE_ACL_BIT_SET_SIZE; n++)
428 all_bits &= node->values.bits[n];
429 return all_bits == -1;
433 * Check if all bits in the bitset are off
436 acl_empty(struct rte_acl_node *node)
440 if (node->ref_count == 0) {
441 for (n = 0; n < RTE_ACL_BIT_SET_SIZE; n++) {
442 if (0 != node->values.bits[n])
452 * Compute intersection of A and B
453 * return 1 if there is an intersection else 0.
456 acl_intersect(struct rte_acl_bitset *a_bits,
457 struct rte_acl_bitset *b_bits,
458 struct rte_acl_bitset *intersect)
463 for (n = 0; n < RTE_ACL_BIT_SET_SIZE; n++) {
464 intersect->bits[n] = a_bits->bits[n] & b_bits->bits[n];
465 all_bits |= intersect->bits[n];
467 return all_bits != 0;
473 static struct rte_acl_node *
474 acl_dup_node(struct acl_build_context *context, struct rte_acl_node *node)
477 struct rte_acl_node *next;
479 next = acl_alloc_node(context, node->level);
483 /* allocate the pointers */
484 if (node->num_ptrs > 0) {
485 next->ptrs = acl_build_alloc(context,
487 sizeof(struct rte_acl_ptr_set));
488 if (next->ptrs == NULL)
490 next->max_ptrs = node->max_ptrs;
493 /* copy over the pointers */
494 for (n = 0; n < node->num_ptrs; n++) {
495 if (node->ptrs[n].ptr != NULL) {
496 next->ptrs[n].ptr = node->ptrs[n].ptr;
497 next->ptrs[n].ptr->ref_count++;
498 acl_include(&next->ptrs[n].values,
499 &node->ptrs[n].values, -1);
503 next->num_ptrs = node->num_ptrs;
505 /* copy over node's match results */
506 if (node->match_flag == 0)
507 next->match_flag = 0;
509 next->match_flag = -1;
510 next->mrt = acl_build_alloc(context, 1, sizeof(*next->mrt));
511 memcpy(next->mrt, node->mrt, sizeof(*next->mrt));
514 /* copy over node's bitset */
515 acl_include(&next->values, &node->values, -1);
524 * Dereference a pointer from a node
527 acl_deref_ptr(struct acl_build_context *context,
528 struct rte_acl_node *node, int index)
530 struct rte_acl_node *ref_node;
532 /* De-reference the node at the specified pointer */
533 if (node != NULL && node->ptrs[index].ptr != NULL) {
534 ref_node = node->ptrs[index].ptr;
535 ref_node->ref_count--;
536 if (ref_node->ref_count == 0)
537 acl_free_node(context, ref_node);
542 * Exclude bitset from a node pointer
543 * returns 0 if poiter was deref'd
547 acl_exclude_ptr(struct acl_build_context *context,
548 struct rte_acl_node *node,
550 struct rte_acl_bitset *b_bits)
555 * remove bitset from node pointer and deref
556 * if the bitset becomes empty.
558 if (!acl_exclude(&node->ptrs[index].values,
559 &node->ptrs[index].values,
561 acl_deref_ptr(context, node, index);
562 node->ptrs[index].ptr = NULL;
566 /* exclude bits from the composite bits for the node */
567 acl_exclude(&node->values, &node->values, b_bits);
572 * Remove a bitset from src ptr and move remaining ptr to dst
575 acl_move_ptr(struct acl_build_context *context,
576 struct rte_acl_node *dst,
577 struct rte_acl_node *src,
579 struct rte_acl_bitset *b_bits)
584 if (!acl_exclude_ptr(context, src, index, b_bits))
587 /* add src pointer to dst node */
588 rc = acl_add_ptr(context, dst, src->ptrs[index].ptr,
589 &src->ptrs[index].values);
593 /* remove ptr from src */
594 acl_exclude_ptr(context, src, index, &src->ptrs[index].values);
599 * acl_exclude rte_acl_bitset from src and copy remaining pointer to dst
602 acl_copy_ptr(struct acl_build_context *context,
603 struct rte_acl_node *dst,
604 struct rte_acl_node *src,
606 struct rte_acl_bitset *b_bits)
609 struct rte_acl_bitset bits;
612 if (!acl_exclude(&bits, &src->ptrs[index].values, b_bits))
615 rc = acl_add_ptr(context, dst, src->ptrs[index].ptr, &bits);
622 * Fill in gaps in ptrs list with the ptr at the end of the list
625 acl_compact_node_ptrs(struct rte_acl_node *node_a)
628 int min_add = node_a->min_add;
630 while (node_a->num_ptrs > 0 &&
631 node_a->ptrs[node_a->num_ptrs - 1].ptr == NULL)
634 for (n = min_add; n + 1 < node_a->num_ptrs; n++) {
636 /* if this entry is empty */
637 if (node_a->ptrs[n].ptr == NULL) {
639 /* move the last pointer to this entry */
640 acl_include(&node_a->ptrs[n].values,
641 &node_a->ptrs[node_a->num_ptrs - 1].values,
643 node_a->ptrs[n].ptr =
644 node_a->ptrs[node_a->num_ptrs - 1].ptr;
647 * mark the end as empty and adjust the number
648 * of used pointer enum_tries
650 node_a->ptrs[node_a->num_ptrs - 1].ptr = NULL;
651 while (node_a->num_ptrs > 0 &&
652 node_a->ptrs[node_a->num_ptrs - 1].ptr == NULL)
659 * acl_merge helper routine.
662 acl_merge_intersect(struct acl_build_context *context,
663 struct rte_acl_node *node_a, uint32_t idx_a,
664 struct rte_acl_node *node_b, uint32_t idx_b,
665 int next_move, int level,
666 struct rte_acl_bitset *intersect_ptr)
668 struct rte_acl_node *node_c;
670 /* Duplicate A for intersection */
671 node_c = acl_dup_node(context, node_a->ptrs[idx_a].ptr);
675 /* Remove intersection from A */
676 acl_exclude_ptr(context, node_a, idx_a, intersect_ptr);
679 * Added link from A to C for all transitions
680 * in the intersection
682 if (acl_add_ptr(context, node_a, node_c, intersect_ptr) < 0)
685 /* merge B->node into C */
686 return acl_merge(context, node_c, node_b->ptrs[idx_b].ptr, next_move,
692 * Merge the children of nodes A and B together.
696 * node A result = highest priority result
697 * if any pointers in A intersect with any in B
698 * For each intersection
699 * C = copy of node that A points to
700 * remove intersection from A pointer
701 * add a pointer to A that points to C for the intersection
702 * Merge C and node that B points to
703 * Compact the pointers in A and B
705 * If B has only one reference
706 * Move B pointers to A
708 * Copy B pointers to A
711 acl_merge(struct acl_build_context *context,
712 struct rte_acl_node *node_a, struct rte_acl_node *node_b,
713 int move, int a_subset, int level)
715 uint32_t n, m, ptrs_a, ptrs_b;
716 uint32_t min_add_a, min_add_b;
718 int node_intersect_type;
719 int b_full, next_move, rc;
720 struct rte_acl_bitset intersect_values;
721 struct rte_acl_bitset intersect_ptr;
726 node_intersect_type = 0;
732 * Resolve match priorities
734 if (node_a->match_flag != 0 || node_b->match_flag != 0) {
736 if (node_a->match_flag == 0 || node_b->match_flag == 0)
737 RTE_LOG(ERR, ACL, "Not both matches\n");
739 if (node_b->match_flag < node_a->match_flag)
740 RTE_LOG(ERR, ACL, "Not same match\n");
742 for (n = 0; n < context->cfg.num_categories; n++) {
743 if (node_a->mrt->priority[n] <
744 node_b->mrt->priority[n]) {
745 node_a->mrt->priority[n] =
746 node_b->mrt->priority[n];
747 node_a->mrt->results[n] =
748 node_b->mrt->results[n];
754 * If the two node transitions intersect then merge the transitions.
755 * Check intersection for entire node (all pointers)
757 node_intersect_type = acl_intersect_type(&node_a->values,
761 if (node_intersect_type & ACL_INTERSECT) {
763 b_full = acl_full(node_b);
765 min_add_b = node_b->min_add;
766 node_b->min_add = node_b->num_ptrs;
767 ptrs_b = node_b->num_ptrs;
769 min_add_a = node_a->min_add;
770 node_a->min_add = node_a->num_ptrs;
771 ptrs_a = node_a->num_ptrs;
773 for (n = 0; n < ptrs_a; n++) {
774 for (m = 0; m < ptrs_b; m++) {
776 if (node_a->ptrs[n].ptr == NULL ||
777 node_b->ptrs[m].ptr == NULL ||
778 node_a->ptrs[n].ptr ==
782 intersect_type = acl_intersect_type(
783 &node_a->ptrs[n].values,
784 &node_b->ptrs[m].values,
787 /* If this node is not a 'match' node */
788 if ((intersect_type & ACL_INTERSECT) &&
789 (context->cfg.num_categories != 1 ||
790 !(node_a->ptrs[n].ptr->match_flag))) {
793 * next merge is a 'move' pointer,
794 * if this one is and B is a
795 * subset of the intersection.
799 ACL_INTERSECT_B) == 0;
801 if (a_subset && b_full) {
802 rc = acl_merge(context,
810 rc = acl_merge_intersect(
812 node_b, m, next_move,
813 level, &intersect_ptr);
822 /* Compact pointers */
823 node_a->min_add = min_add_a;
824 acl_compact_node_ptrs(node_a);
825 node_b->min_add = min_add_b;
826 acl_compact_node_ptrs(node_b);
829 * Either COPY or MOVE pointers from B to A
831 acl_intersect(&node_a->values, &node_b->values, &intersect_values);
833 if (move && node_b->ref_count == 1) {
834 for (m = 0; m < node_b->num_ptrs; m++) {
835 if (node_b->ptrs[m].ptr != NULL &&
836 acl_move_ptr(context, node_a, node_b, m,
837 &intersect_values) < 0)
841 for (m = 0; m < node_b->num_ptrs; m++) {
842 if (node_b->ptrs[m].ptr != NULL &&
843 acl_copy_ptr(context, node_a, node_b, m,
844 &intersect_values) < 0)
850 * Free node if its empty (no longer used)
852 if (acl_empty(node_b))
853 acl_free_node(context, node_b);
858 acl_resolve_leaf(struct acl_build_context *context,
859 struct rte_acl_node *node_a,
860 struct rte_acl_node *node_b,
861 struct rte_acl_node **node_c)
864 int combined_priority = ACL_PRIORITY_EQUAL;
866 for (n = 0; n < context->cfg.num_categories; n++) {
867 if (node_a->mrt->priority[n] != node_b->mrt->priority[n]) {
868 combined_priority |= (node_a->mrt->priority[n] >
869 node_b->mrt->priority[n]) ?
870 ACL_PRIORITY_NODE_A : ACL_PRIORITY_NODE_B;
875 * if node a is higher or equal priority for all categories,
876 * then return node_a.
878 if (combined_priority == ACL_PRIORITY_NODE_A ||
879 combined_priority == ACL_PRIORITY_EQUAL) {
885 * if node b is higher or equal priority for all categories,
886 * then return node_b.
888 if (combined_priority == ACL_PRIORITY_NODE_B) {
894 * mixed priorities - create a new node with the highest priority
898 /* force new duplication. */
901 *node_c = acl_dup_node(context, node_a);
902 for (n = 0; n < context->cfg.num_categories; n++) {
903 if ((*node_c)->mrt->priority[n] < node_b->mrt->priority[n]) {
904 (*node_c)->mrt->priority[n] = node_b->mrt->priority[n];
905 (*node_c)->mrt->results[n] = node_b->mrt->results[n];
912 * Within the existing trie structure, determine which nodes are
913 * part of the subtree of the trie to be merged.
915 * For these purposes, a subtree is defined as the set of nodes that
916 * are 1) not a superset of the intersection with the same level of
917 * the merging tree, and 2) do not have any references from a node
918 * outside of the subtree.
921 mark_subtree(struct rte_acl_node *node,
922 struct rte_acl_bitset *level_bits,
928 /* mark this node as part of the subtree */
929 node->subtree_id = id | RTE_ACL_SUBTREE_NODE;
931 for (n = 0; n < node->num_ptrs; n++) {
933 if (node->ptrs[n].ptr != NULL) {
935 struct rte_acl_bitset intersect_bits;
940 * check if this child pointer is not a superset of the
941 * same level of the merging tree.
943 intersect = acl_intersect_type(&node->ptrs[n].values,
947 if ((intersect & ACL_INTERSECT_A) == 0) {
949 struct rte_acl_node *child = node->ptrs[n].ptr;
952 * reset subtree reference if this is
953 * the first visit by this subtree.
955 if (child->subtree_id != id) {
956 child->subtree_id = id;
957 child->subtree_ref_count = 0;
962 * increment the subtree reference count and if
963 * all references are from this subtree then
964 * recurse to that child
966 child->subtree_ref_count++;
967 if (child->subtree_ref_count ==
969 mark_subtree(child, level_bits,
977 * Build the set of bits that define the set of transitions
978 * for each level of a trie.
981 build_subset_mask(struct rte_acl_node *node,
982 struct rte_acl_bitset *level_bits,
987 /* Add this node's transitions to the set for this level */
988 for (n = 0; n < RTE_ACL_BIT_SET_SIZE; n++)
989 level_bits[level].bits[n] &= node->values.bits[n];
991 /* For each child, add the transitions for the next level */
992 for (n = 0; n < node->num_ptrs; n++)
993 if (node->ptrs[n].ptr != NULL)
994 build_subset_mask(node->ptrs[n].ptr, level_bits,
1000 * Merge nodes A and B together,
1001 * returns a node that is the path for the intersection
1003 * If match node (leaf on trie)
1005 * return node = highest priority result
1007 * Create C as a duplicate of A to point to child intersections
1008 * If any pointers in C intersect with any in B
1009 * For each intersection
1011 * remove intersection from C pointer
1012 * add a pointer from C to child intersection node
1013 * Compact the pointers in A and B
1014 * Copy any B pointers that are outside of the intersection to C
1015 * If C has no references to the B trie
1016 * free C and return A
1017 * Else If C has no references to the A trie
1018 * free C and return B
1023 acl_merge_trie(struct acl_build_context *context,
1024 struct rte_acl_node *node_a, struct rte_acl_node *node_b,
1025 uint32_t level, uint32_t subtree_id, struct rte_acl_node **return_c)
1027 uint32_t n, m, ptrs_c, ptrs_b;
1028 uint32_t min_add_c, min_add_b;
1029 int node_intersect_type;
1030 struct rte_acl_bitset node_intersect;
1031 struct rte_acl_node *node_c;
1032 struct rte_acl_node *node_a_next;
1037 node_a_next = node_a->next;
1040 node_a_refs = node_a->num_ptrs;
1042 node_intersect_type = 0;
1044 /* Resolve leaf nodes (matches) */
1045 if (node_a->match_flag != 0) {
1046 acl_resolve_leaf(context, node_a, node_b, return_c);
1051 * Create node C as a copy of node A if node A is not part of
1052 * a subtree of the merging tree (node B side). Otherwise,
1056 node_a->subtree_id !=
1057 (subtree_id | RTE_ACL_SUBTREE_NODE)) {
1058 node_c = acl_dup_node(context, node_a);
1059 node_c->subtree_id = subtree_id | RTE_ACL_SUBTREE_NODE;
1063 * If the two node transitions intersect then merge the transitions.
1064 * Check intersection for entire node (all pointers)
1066 node_intersect_type = acl_intersect_type(&node_c->values,
1070 if (node_intersect_type & ACL_INTERSECT) {
1072 min_add_b = node_b->min_add;
1073 node_b->min_add = node_b->num_ptrs;
1074 ptrs_b = node_b->num_ptrs;
1076 min_add_c = node_c->min_add;
1077 node_c->min_add = node_c->num_ptrs;
1078 ptrs_c = node_c->num_ptrs;
1080 for (n = 0; n < ptrs_c; n++) {
1081 if (node_c->ptrs[n].ptr == NULL) {
1085 node_c->ptrs[n].ptr->next = NULL;
1086 for (m = 0; m < ptrs_b; m++) {
1088 struct rte_acl_bitset child_intersect;
1089 int child_intersect_type;
1090 struct rte_acl_node *child_node_c = NULL;
1092 if (node_b->ptrs[m].ptr == NULL ||
1093 node_c->ptrs[n].ptr ==
1094 node_b->ptrs[m].ptr)
1097 child_intersect_type = acl_intersect_type(
1098 &node_c->ptrs[n].values,
1099 &node_b->ptrs[m].values,
1102 if ((child_intersect_type & ACL_INTERSECT) !=
1104 if (acl_merge_trie(context,
1105 node_c->ptrs[n].ptr,
1106 node_b->ptrs[m].ptr,
1107 level + 1, subtree_id,
1111 if (child_node_c != NULL &&
1113 node_c->ptrs[n].ptr) {
1118 * Added link from C to
1119 * child_C for all transitions
1120 * in the intersection.
1122 acl_add_ptr(context, node_c,
1127 * inc refs if pointer is not
1130 node_a_refs += (child_node_c !=
1131 node_b->ptrs[m].ptr);
1134 * Remove intersection from C
1138 &node_c->ptrs[n].values,
1139 &node_c->ptrs[n].values,
1140 &child_intersect)) {
1141 acl_deref_ptr(context,
1143 node_c->ptrs[n].ptr =
1152 /* Compact pointers */
1153 node_c->min_add = min_add_c;
1154 acl_compact_node_ptrs(node_c);
1155 node_b->min_add = min_add_b;
1156 acl_compact_node_ptrs(node_b);
1160 * Copy pointers outside of the intersection from B to C
1162 if ((node_intersect_type & ACL_INTERSECT_B) != 0) {
1164 for (m = 0; m < node_b->num_ptrs; m++)
1165 if (node_b->ptrs[m].ptr != NULL)
1166 acl_copy_ptr(context, node_c,
1167 node_b, m, &node_intersect);
1171 * Free node C if top of trie is contained in A or B
1172 * if node C is a duplicate of node A &&
1173 * node C was not an existing duplicate
1175 if (node_c != node_a && node_c != node_a_next) {
1178 * if the intersection has no references to the
1179 * B side, then it is contained in A
1181 if (node_b_refs == 0) {
1182 acl_free_node(context, node_c);
1186 * if the intersection has no references to the
1187 * A side, then it is contained in B.
1189 if (node_a_refs == 0) {
1190 acl_free_node(context, node_c);
1196 if (return_c != NULL)
1200 acl_free_node(context, node_b);
1206 * Reset current runtime fields before next build:
1207 * - free allocated RT memory.
1208 * - reset all RT related fields to zero.
1211 acl_build_reset(struct rte_acl_ctx *ctx)
1214 memset(&ctx->num_categories, 0,
1215 sizeof(*ctx) - offsetof(struct rte_acl_ctx, num_categories));
1219 acl_gen_range(struct acl_build_context *context,
1220 const uint8_t *hi, const uint8_t *lo, int size, int level,
1221 struct rte_acl_node *root, struct rte_acl_node *end)
1223 struct rte_acl_node *node, *prev;
1227 for (n = size - 1; n > 0; n--) {
1228 node = acl_alloc_node(context, level++);
1229 acl_add_ptr_range(context, prev, node, lo[n], hi[n]);
1232 acl_add_ptr_range(context, prev, end, lo[0], hi[0]);
1235 static struct rte_acl_node *
1236 acl_gen_range_trie(struct acl_build_context *context,
1237 const void *min, const void *max,
1238 int size, int level, struct rte_acl_node **pend)
1241 struct rte_acl_node *root;
1242 const uint8_t *lo = (const uint8_t *)min;
1243 const uint8_t *hi = (const uint8_t *)max;
1245 *pend = acl_alloc_node(context, level+size);
1246 root = acl_alloc_node(context, level++);
1248 if (lo[size - 1] == hi[size - 1]) {
1249 acl_gen_range(context, hi, lo, size, level, root, *pend);
1251 uint8_t limit_lo[64];
1252 uint8_t limit_hi[64];
1253 uint8_t hi_ff = UINT8_MAX;
1256 memset(limit_lo, 0, RTE_DIM(limit_lo));
1257 memset(limit_hi, UINT8_MAX, RTE_DIM(limit_hi));
1259 for (n = size - 2; n >= 0; n--) {
1260 hi_ff = (uint8_t)(hi_ff & hi[n]);
1261 lo_00 = (uint8_t)(lo_00 | lo[n]);
1264 if (hi_ff != UINT8_MAX) {
1265 limit_lo[size - 1] = hi[size - 1];
1266 acl_gen_range(context, hi, limit_lo, size, level,
1271 limit_hi[size - 1] = lo[size - 1];
1272 acl_gen_range(context, limit_hi, lo, size, level,
1276 if (hi[size - 1] - lo[size - 1] > 1 ||
1278 hi_ff == UINT8_MAX) {
1279 limit_lo[size-1] = (uint8_t)(lo[size-1] + (lo_00 != 0));
1280 limit_hi[size-1] = (uint8_t)(hi[size-1] -
1281 (hi_ff != UINT8_MAX));
1282 acl_gen_range(context, limit_hi, limit_lo, size,
1283 level, root, *pend);
1289 static struct rte_acl_node *
1290 acl_gen_mask_trie(struct acl_build_context *context,
1291 const void *value, const void *mask,
1292 int size, int level, struct rte_acl_node **pend)
1295 struct rte_acl_node *root;
1296 struct rte_acl_node *node, *prev;
1297 struct rte_acl_bitset bits;
1298 const uint8_t *val = (const uint8_t *)value;
1299 const uint8_t *msk = (const uint8_t *)mask;
1301 root = acl_alloc_node(context, level++);
1304 for (n = size - 1; n >= 0; n--) {
1305 node = acl_alloc_node(context, level++);
1306 acl_gen_mask(&bits, val[n] & msk[n], msk[n]);
1307 acl_add_ptr(context, prev, node, &bits);
1315 static struct rte_acl_node *
1316 build_trie(struct acl_build_context *context, struct rte_acl_build_rule *head,
1317 struct rte_acl_build_rule **last, uint32_t *count)
1320 int field_index, node_count;
1321 struct rte_acl_node *trie;
1322 struct rte_acl_build_rule *prev, *rule;
1323 struct rte_acl_node *end, *merge, *root, *end_prev;
1324 const struct rte_acl_field *fld;
1325 struct rte_acl_bitset level_bits[RTE_ACL_MAX_LEVELS];
1330 trie = acl_alloc_node(context, 0);
1334 while (rule != NULL) {
1336 root = acl_alloc_node(context, 0);
1340 root->ref_count = 1;
1343 for (n = 0; n < rule->config->num_fields; n++) {
1345 field_index = rule->config->defs[n].field_index;
1346 fld = rule->f->field + field_index;
1349 /* build a mini-trie for this field */
1350 switch (rule->config->defs[n].type) {
1352 case RTE_ACL_FIELD_TYPE_BITMASK:
1353 merge = acl_gen_mask_trie(context,
1356 rule->config->defs[n].size,
1361 case RTE_ACL_FIELD_TYPE_MASK:
1364 * set msb for the size of the field and
1369 if (fld->mask_range.u32 == 0) {
1373 * arithmetic right shift for the length of
1374 * the mask less the msb.
1378 (rule->config->defs[n].size *
1379 CHAR_BIT - fld->mask_range.u32);
1382 /* gen a mini-trie for this field */
1383 merge = acl_gen_mask_trie(context,
1386 rule->config->defs[n].size,
1392 case RTE_ACL_FIELD_TYPE_RANGE:
1393 merge = acl_gen_range_trie(context,
1394 &rule->f->field[field_index].value,
1395 &rule->f->field[field_index].mask_range,
1396 rule->config->defs[n].size,
1403 "Error in rule[%u] type - %hhu\n",
1404 rule->f->data.userdata,
1405 rule->config->defs[n].type);
1409 /* merge this field on to the end of the rule */
1410 if (acl_merge_trie(context, end_prev, merge, 0,
1416 end->match_flag = ++context->num_build_rules;
1419 * Setup the results for this rule.
1420 * The result and priority of each category.
1422 if (end->mrt == NULL &&
1423 (end->mrt = acl_build_alloc(context, 1,
1424 sizeof(*end->mrt))) == NULL)
1427 for (m = 0; m < context->cfg.num_categories; m++) {
1428 if (rule->f->data.category_mask & (1 << m)) {
1429 end->mrt->results[m] = rule->f->data.userdata;
1430 end->mrt->priority[m] = rule->f->data.priority;
1432 end->mrt->results[m] = 0;
1433 end->mrt->priority[m] = 0;
1437 node_count = context->num_nodes;
1439 memset(&level_bits[0], UINT8_MAX, sizeof(level_bits));
1440 build_subset_mask(root, &level_bits[0], 0);
1441 mark_subtree(trie, &level_bits[0], 0, end->match_flag);
1444 /* merge this rule into the trie */
1445 if (acl_merge_trie(context, trie, root, 0, end->match_flag,
1449 node_count = context->num_nodes - node_count;
1450 if (node_count > NODE_MAX) {
1464 acl_calc_wildness(struct rte_acl_build_rule *head,
1465 const struct rte_acl_config *config)
1468 struct rte_acl_build_rule *rule;
1470 for (rule = head; rule != NULL; rule = rule->next) {
1472 for (n = 0; n < config->num_fields; n++) {
1475 double size = CHAR_BIT * config->defs[n].size;
1476 int field_index = config->defs[n].field_index;
1477 const struct rte_acl_field *fld = rule->f->field +
1480 switch (rule->config->defs[n].type) {
1481 case RTE_ACL_FIELD_TYPE_BITMASK:
1482 wild = (size - __builtin_popcount(
1483 fld->mask_range.u8)) /
1487 case RTE_ACL_FIELD_TYPE_MASK:
1488 wild = (size - fld->mask_range.u32) / size;
1491 case RTE_ACL_FIELD_TYPE_RANGE:
1492 switch (rule->config->defs[n].size) {
1493 case sizeof(uint8_t):
1494 wild = ((double)fld->mask_range.u8 -
1495 fld->value.u8) / UINT8_MAX;
1497 case sizeof(uint16_t):
1498 wild = ((double)fld->mask_range.u16 -
1499 fld->value.u16) / UINT16_MAX;
1501 case sizeof(uint32_t):
1502 wild = ((double)fld->mask_range.u32 -
1503 fld->value.u32) / UINT32_MAX;
1505 case sizeof(uint64_t):
1506 wild = ((double)fld->mask_range.u64 -
1507 fld->value.u64) / UINT64_MAX;
1511 "%s(rule: %u) invalid %u-th "
1512 "field, type: %hhu, "
1513 "unknown size: %hhu\n",
1515 rule->f->data.userdata,
1517 rule->config->defs[n].type,
1518 rule->config->defs[n].size);
1525 "%s(rule: %u) invalid %u-th "
1526 "field, unknown type: %hhu\n",
1528 rule->f->data.userdata,
1530 rule->config->defs[n].type);
1535 rule->wildness[field_index] = (uint32_t)(wild * 100);
1543 acl_rule_stats(struct rte_acl_build_rule *head, struct rte_acl_config *config,
1544 uint32_t *wild_limit)
1547 struct rte_acl_build_rule *rule;
1548 uint32_t n, m, fields_deactivated = 0;
1549 uint32_t start = 0, deactivate = 0;
1550 int tally[RTE_ACL_MAX_LEVELS][TALLY_NUM];
1552 memset(tally, 0, sizeof(tally));
1554 for (rule = head; rule != NULL; rule = rule->next) {
1556 for (n = 0; n < config->num_fields; n++) {
1557 uint32_t field_index = config->defs[n].field_index;
1559 tally[n][TALLY_0]++;
1560 for (m = 1; m < RTE_DIM(wild_limits); m++) {
1561 if (rule->wildness[field_index] >=
1567 for (n = config->num_fields - 1; n > 0; n--) {
1568 uint32_t field_index = config->defs[n].field_index;
1570 if (rule->wildness[field_index] == 100)
1571 tally[n][TALLY_DEPTH]++;
1578 * Look for any field that is always wild and drop it from the config
1579 * Only deactivate if all fields for a given input loop are deactivated.
1581 for (n = 1; n < config->num_fields; n++) {
1582 if (config->defs[n].input_index !=
1583 config->defs[n - 1].input_index) {
1584 for (m = start; m < n; m++)
1585 tally[m][TALLY_DEACTIVATED] = deactivate;
1586 fields_deactivated += deactivate;
1591 /* if the field is not always completely wild */
1592 if (tally[n][TALLY_100] != tally[n][TALLY_0])
1596 for (m = start; m < n; m++)
1597 tally[m][TALLY_DEACTIVATED] = deactivate;
1599 fields_deactivated += deactivate;
1601 /* remove deactivated fields */
1602 if (fields_deactivated) {
1605 for (k = 0; k < config->num_fields; k++) {
1606 if (tally[k][TALLY_DEACTIVATED] == 0) {
1607 memcpy(&tally[l][0], &tally[k][0],
1608 TALLY_NUM * sizeof(tally[0][0]));
1609 memcpy(&config->defs[l++],
1611 sizeof(struct rte_acl_field_def));
1614 config->num_fields = l;
1617 min = RTE_ACL_SINGLE_TRIE_SIZE;
1618 if (config->num_fields == 2)
1620 else if (config->num_fields == 3)
1622 else if (config->num_fields == 4)
1625 if (tally[0][TALLY_0] < min)
1627 for (n = 0; n < config->num_fields; n++)
1631 * If trailing fields are 100% wild, group those together.
1632 * This allows the search length of the trie to be shortened.
1634 for (n = 1; n < config->num_fields; n++) {
1636 double rule_percentage = (double)tally[n][TALLY_DEPTH] /
1639 if (rule_percentage > RULE_PERCENTAGE) {
1640 /* if it crosses an input boundary then round up */
1641 while (config->defs[n - 1].input_index ==
1642 config->defs[n].input_index)
1645 /* set the limit for selecting rules */
1646 while (n < config->num_fields)
1647 wild_limit[n++] = 100;
1649 if (wild_limit[n - 1] == 100)
1654 /* look for the most wild that's 40% or more of the rules */
1655 for (n = 1; n < config->num_fields; n++) {
1656 for (m = TALLY_100; m > 0; m--) {
1658 double rule_percentage = (double)tally[n][m] /
1661 if (tally[n][TALLY_DEACTIVATED] == 0 &&
1663 RTE_ACL_SINGLE_TRIE_SIZE &&
1664 rule_percentage > NODE_PERCENTAGE &&
1665 rule_percentage < 0.80) {
1666 wild_limit[n] = wild_limits[m];
1675 order(struct rte_acl_build_rule **insert, struct rte_acl_build_rule *rule)
1678 struct rte_acl_build_rule *left = *insert;
1683 for (n = 1; n < left->config->num_fields; n++) {
1684 int field_index = left->config->defs[n].field_index;
1686 if (left->wildness[field_index] != rule->wildness[field_index])
1687 return (left->wildness[field_index] >=
1688 rule->wildness[field_index]);
1693 static struct rte_acl_build_rule *
1694 ordered_insert_rule(struct rte_acl_build_rule *head,
1695 struct rte_acl_build_rule *rule)
1697 struct rte_acl_build_rule **insert;
1707 while (order(insert, rule))
1708 insert = &(*insert)->next;
1710 rule->next = *insert;
1715 static struct rte_acl_build_rule *
1716 sort_rules(struct rte_acl_build_rule *head)
1718 struct rte_acl_build_rule *rule, *reordered_head = NULL;
1719 struct rte_acl_build_rule *last_rule = NULL;
1721 for (rule = head; rule != NULL; rule = rule->next) {
1722 reordered_head = ordered_insert_rule(reordered_head, last_rule);
1726 if (last_rule != reordered_head)
1727 reordered_head = ordered_insert_rule(reordered_head, last_rule);
1729 return reordered_head;
1733 acl_build_index(const struct rte_acl_config *config, uint32_t *data_index)
1736 int32_t last_header;
1741 for (n = 0; n < config->num_fields; n++) {
1742 if (last_header != config->defs[n].input_index) {
1743 last_header = config->defs[n].input_index;
1744 data_index[m++] = config->defs[n].offset;
1752 acl_build_tries(struct acl_build_context *context,
1753 struct rte_acl_build_rule *head)
1756 uint32_t n, m, num_tries;
1757 struct rte_acl_config *config;
1758 struct rte_acl_build_rule *last, *rule;
1759 uint32_t wild_limit[RTE_ACL_MAX_LEVELS];
1760 struct rte_acl_build_rule *rule_sets[RTE_ACL_MAX_TRIES];
1762 config = head->config;
1764 rule_sets[0] = head;
1767 /* initialize tries */
1768 for (n = 0; n < RTE_DIM(context->tries); n++) {
1769 context->tries[n].type = RTE_ACL_UNUSED_TRIE;
1770 context->bld_tries[n].trie = NULL;
1771 context->tries[n].count = 0;
1772 context->tries[n].smallest = INT32_MAX;
1775 context->tries[0].type = RTE_ACL_FULL_TRIE;
1777 /* calc wildness of each field of each rule */
1778 rc = acl_calc_wildness(head, config);
1782 n = acl_rule_stats(head, config, &wild_limit[0]);
1784 /* put all rules that fit the wildness criteria into a seperate trie */
1785 while (n > 0 && num_tries < RTE_ACL_MAX_TRIES) {
1787 struct rte_acl_config *new_config;
1788 struct rte_acl_build_rule **prev = &rule_sets[num_tries - 1];
1789 struct rte_acl_build_rule *next = head->next;
1791 new_config = acl_build_alloc(context, 1, sizeof(*new_config));
1792 if (new_config == NULL) {
1794 "Failed to get space for new config\n");
1798 memcpy(new_config, config, sizeof(*new_config));
1799 config = new_config;
1800 rule_sets[num_tries] = NULL;
1802 for (rule = head; rule != NULL; rule = next) {
1807 for (m = 0; m < config->num_fields; m++) {
1808 int x = config->defs[m].field_index;
1809 if (rule->wildness[x] < wild_limit[m]) {
1816 rule->config = new_config;
1817 rule->next = rule_sets[num_tries];
1818 rule_sets[num_tries] = rule;
1824 head = rule_sets[num_tries];
1825 n = acl_rule_stats(rule_sets[num_tries], config,
1832 "Number of tries(%d) exceeded.\n", RTE_ACL_MAX_TRIES);
1834 for (n = 0; n < num_tries; n++) {
1836 rule_sets[n] = sort_rules(rule_sets[n]);
1837 context->tries[n].type = RTE_ACL_FULL_TRIE;
1838 context->tries[n].count = 0;
1839 context->tries[n].num_data_indexes =
1840 acl_build_index(rule_sets[n]->config,
1841 context->data_indexes[n]);
1842 context->tries[n].data_index = context->data_indexes[n];
1844 context->bld_tries[n].trie =
1845 build_trie(context, rule_sets[n],
1846 &last, &context->tries[n].count);
1847 if (context->bld_tries[n].trie == NULL) {
1848 RTE_LOG(ERR, ACL, "Build of %u-th trie failed\n", n);
1853 rule_sets[num_tries++] = last->next;
1855 acl_free_node(context, context->bld_tries[n].trie);
1856 context->tries[n].count = 0;
1858 context->bld_tries[n].trie =
1859 build_trie(context, rule_sets[n],
1860 &last, &context->tries[n].count);
1861 if (context->bld_tries[n].trie == NULL) {
1863 "Build of %u-th trie failed\n", n);
1869 context->num_tries = num_tries;
1874 acl_build_log(const struct acl_build_context *ctx)
1878 RTE_LOG(DEBUG, ACL, "Build phase for ACL \"%s\":\n"
1879 "memory consumed: %zu\n",
1883 for (n = 0; n < RTE_DIM(ctx->tries); n++) {
1884 if (ctx->tries[n].count != 0)
1886 "trie %u: number of rules: %u\n",
1887 n, ctx->tries[n].count);
1892 acl_build_rules(struct acl_build_context *bcx)
1894 struct rte_acl_build_rule *br, *head;
1895 const struct rte_acl_rule *rule;
1897 uint32_t fn, i, n, num;
1900 fn = bcx->cfg.num_fields;
1901 n = bcx->acx->num_rules;
1902 ofs = n * sizeof(*br);
1903 sz = ofs + n * fn * sizeof(*wp);
1905 br = tb_alloc(&bcx->pool, sz);
1907 RTE_LOG(ERR, ACL, "ACL context %s: failed to create a copy "
1908 "of %u build rules (%zu bytes)\n",
1909 bcx->acx->name, n, sz);
1913 wp = (uint32_t *)((uintptr_t)br + ofs);
1917 for (i = 0; i != n; i++) {
1918 rule = (const struct rte_acl_rule *)
1919 ((uintptr_t)bcx->acx->rules + bcx->acx->rule_sz * i);
1920 if ((rule->data.category_mask & bcx->category_mask) != 0) {
1921 br[num].next = head;
1922 br[num].config = &bcx->cfg;
1924 br[num].wildness = wp;
1931 bcx->num_rules = num;
1932 bcx->build_rules = head;
1938 * Copy data_indexes for each trie into RT location.
1941 acl_set_data_indexes(struct rte_acl_ctx *ctx)
1946 for (i = 0; i != ctx->num_tries; i++) {
1947 n = ctx->trie[i].num_data_indexes;
1948 memcpy(ctx->data_indexes + ofs, ctx->trie[i].data_index,
1949 n * sizeof(ctx->data_indexes[0]));
1950 ctx->trie[i].data_index = ctx->data_indexes + ofs;
1957 rte_acl_build(struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg)
1960 struct acl_build_context bcx;
1962 if (ctx == NULL || cfg == NULL || cfg->num_categories == 0 ||
1963 cfg->num_categories > RTE_ACL_MAX_CATEGORIES)
1966 acl_build_reset(ctx);
1968 memset(&bcx, 0, sizeof(bcx));
1970 bcx.pool.alignment = ACL_POOL_ALIGN;
1971 bcx.pool.min_alloc = ACL_POOL_ALLOC_MIN;
1973 bcx.category_mask = LEN2MASK(bcx.cfg.num_categories);
1976 /* Create a build rules copy. */
1977 rc = acl_build_rules(&bcx);
1981 /* No rules to build for that context+config */
1982 if (bcx.build_rules == NULL) {
1985 /* build internal trie representation. */
1986 } else if ((rc = acl_build_tries(&bcx, bcx.build_rules)) == 0) {
1988 /* allocate and fill run-time structures. */
1989 rc = rte_acl_gen(ctx, bcx.tries, bcx.bld_tries,
1990 bcx.num_tries, bcx.cfg.num_categories,
1991 RTE_ACL_IPV4VLAN_NUM * RTE_DIM(bcx.tries),
1992 bcx.num_build_rules);
1995 /* set data indexes. */
1996 acl_set_data_indexes(ctx);
1998 /* copy in build config. */
2003 acl_build_log(&bcx);
2005 /* cleanup after build. */
2006 tb_free_pool(&bcx.pool);