199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause 299a2dd95SBruce Richardson * Copyright(c) 2010-2014 Intel Corporation 399a2dd95SBruce Richardson */ 499a2dd95SBruce Richardson 599a2dd95SBruce Richardson #include <rte_acl.h> 699a2dd95SBruce Richardson #include "tb_mem.h" 799a2dd95SBruce Richardson #include "acl.h" 899a2dd95SBruce Richardson 999a2dd95SBruce Richardson #define ACL_POOL_ALIGN 8 1099a2dd95SBruce Richardson #define ACL_POOL_ALLOC_MIN 0x800000 1199a2dd95SBruce Richardson 1299a2dd95SBruce Richardson /* number of pointers per alloc */ 1399a2dd95SBruce Richardson #define ACL_PTR_ALLOC 32 1499a2dd95SBruce Richardson 1545109815SKonstantin Ananyev /* account for situation when all fields are 8B long */ 1645109815SKonstantin Ananyev #define ACL_MAX_INDEXES (2 * RTE_ACL_MAX_FIELDS) 1745109815SKonstantin Ananyev 1899a2dd95SBruce Richardson /* macros for dividing rule sets heuristics */ 1999a2dd95SBruce Richardson #define NODE_MAX 0x4000 2099a2dd95SBruce Richardson #define NODE_MIN 0x800 2199a2dd95SBruce Richardson 2299a2dd95SBruce Richardson /* TALLY are statistics per field */ 2399a2dd95SBruce Richardson enum { 2499a2dd95SBruce Richardson TALLY_0 = 0, /* number of rules that are 0% or more wild. */ 2599a2dd95SBruce Richardson TALLY_25, /* number of rules that are 25% or more wild. */ 2699a2dd95SBruce Richardson TALLY_50, 2799a2dd95SBruce Richardson TALLY_75, 2899a2dd95SBruce Richardson TALLY_100, 2999a2dd95SBruce Richardson TALLY_DEACTIVATED, /* deactivated fields (100% wild in all rules). */ 3099a2dd95SBruce Richardson TALLY_DEPTH, 3199a2dd95SBruce Richardson /* number of rules that are 100% wild for this field and higher. */ 3299a2dd95SBruce Richardson TALLY_NUM 3399a2dd95SBruce Richardson }; 3499a2dd95SBruce Richardson 3599a2dd95SBruce Richardson static const uint32_t wild_limits[TALLY_DEACTIVATED] = {0, 25, 50, 75, 100}; 3699a2dd95SBruce Richardson 3799a2dd95SBruce Richardson enum { 3899a2dd95SBruce Richardson ACL_INTERSECT_NONE = 0, 3999a2dd95SBruce Richardson ACL_INTERSECT_A = 1, /* set A is a superset of A and B intersect */ 4099a2dd95SBruce Richardson ACL_INTERSECT_B = 2, /* set B is a superset of A and B intersect */ 4199a2dd95SBruce Richardson ACL_INTERSECT = 4, /* sets A and B intersect */ 4299a2dd95SBruce Richardson }; 4399a2dd95SBruce Richardson 4499a2dd95SBruce Richardson enum { 4599a2dd95SBruce Richardson ACL_PRIORITY_EQUAL = 0, 4699a2dd95SBruce Richardson ACL_PRIORITY_NODE_A = 1, 4799a2dd95SBruce Richardson ACL_PRIORITY_NODE_B = 2, 4899a2dd95SBruce Richardson ACL_PRIORITY_MIXED = 3 4999a2dd95SBruce Richardson }; 5099a2dd95SBruce Richardson 5199a2dd95SBruce Richardson 5299a2dd95SBruce Richardson struct acl_mem_block { 5399a2dd95SBruce Richardson uint32_t block_size; 5499a2dd95SBruce Richardson void *mem_ptr; 5599a2dd95SBruce Richardson }; 5699a2dd95SBruce Richardson 5799a2dd95SBruce Richardson #define MEM_BLOCK_NUM 16 5899a2dd95SBruce Richardson 5999a2dd95SBruce Richardson /* Single ACL rule, build representation.*/ 6099a2dd95SBruce Richardson struct rte_acl_build_rule { 6199a2dd95SBruce Richardson struct rte_acl_build_rule *next; 6299a2dd95SBruce Richardson struct rte_acl_config *config; 6399a2dd95SBruce Richardson /**< configuration for each field in the rule. */ 6499a2dd95SBruce Richardson const struct rte_acl_rule *f; 6599a2dd95SBruce Richardson uint32_t *wildness; 6699a2dd95SBruce Richardson }; 6799a2dd95SBruce Richardson 6899a2dd95SBruce Richardson /* Context for build phase */ 6999a2dd95SBruce Richardson struct acl_build_context { 7099a2dd95SBruce Richardson const struct rte_acl_ctx *acx; 7199a2dd95SBruce Richardson struct rte_acl_build_rule *build_rules; 7299a2dd95SBruce Richardson struct rte_acl_config cfg; 7399a2dd95SBruce Richardson int32_t node_max; 7499a2dd95SBruce Richardson int32_t cur_node_max; 7599a2dd95SBruce Richardson uint32_t node; 7699a2dd95SBruce Richardson uint32_t num_nodes; 7799a2dd95SBruce Richardson uint32_t category_mask; 7899a2dd95SBruce Richardson uint32_t num_rules; 7999a2dd95SBruce Richardson uint32_t node_id; 8099a2dd95SBruce Richardson uint32_t src_mask; 8199a2dd95SBruce Richardson uint32_t num_build_rules; 8299a2dd95SBruce Richardson uint32_t num_tries; 8399a2dd95SBruce Richardson struct tb_mem_pool pool; 8499a2dd95SBruce Richardson struct rte_acl_trie tries[RTE_ACL_MAX_TRIES]; 8599a2dd95SBruce Richardson struct rte_acl_bld_trie bld_tries[RTE_ACL_MAX_TRIES]; 8645109815SKonstantin Ananyev uint32_t data_indexes[RTE_ACL_MAX_TRIES][ACL_MAX_INDEXES]; 8799a2dd95SBruce Richardson 8899a2dd95SBruce Richardson /* memory free lists for nodes and blocks used for node ptrs */ 8999a2dd95SBruce Richardson struct acl_mem_block blocks[MEM_BLOCK_NUM]; 9099a2dd95SBruce Richardson struct rte_acl_node *node_free_list; 9199a2dd95SBruce Richardson }; 9299a2dd95SBruce Richardson 9399a2dd95SBruce Richardson static int acl_merge_trie(struct acl_build_context *context, 9499a2dd95SBruce Richardson struct rte_acl_node *node_a, struct rte_acl_node *node_b, 9599a2dd95SBruce Richardson uint32_t level, struct rte_acl_node **node_c); 9699a2dd95SBruce Richardson 9799a2dd95SBruce Richardson static void 9899a2dd95SBruce Richardson acl_deref_ptr(struct acl_build_context *context, 9999a2dd95SBruce Richardson struct rte_acl_node *node, int index); 10099a2dd95SBruce Richardson 10199a2dd95SBruce Richardson static void * 10299a2dd95SBruce Richardson acl_build_alloc(struct acl_build_context *context, size_t n, size_t s) 10399a2dd95SBruce Richardson { 10499a2dd95SBruce Richardson uint32_t m; 10599a2dd95SBruce Richardson void *p; 10699a2dd95SBruce Richardson size_t alloc_size = n * s; 10799a2dd95SBruce Richardson 10899a2dd95SBruce Richardson /* 10999a2dd95SBruce Richardson * look for memory in free lists 11099a2dd95SBruce Richardson */ 11199a2dd95SBruce Richardson for (m = 0; m < RTE_DIM(context->blocks); m++) { 11299a2dd95SBruce Richardson if (context->blocks[m].block_size == 11399a2dd95SBruce Richardson alloc_size && context->blocks[m].mem_ptr != NULL) { 11499a2dd95SBruce Richardson p = context->blocks[m].mem_ptr; 11599a2dd95SBruce Richardson context->blocks[m].mem_ptr = *((void **)p); 11699a2dd95SBruce Richardson memset(p, 0, alloc_size); 11799a2dd95SBruce Richardson return p; 11899a2dd95SBruce Richardson } 11999a2dd95SBruce Richardson } 12099a2dd95SBruce Richardson 12199a2dd95SBruce Richardson /* 12299a2dd95SBruce Richardson * return allocation from memory pool 12399a2dd95SBruce Richardson */ 12499a2dd95SBruce Richardson p = tb_alloc(&context->pool, alloc_size); 12599a2dd95SBruce Richardson return p; 12699a2dd95SBruce Richardson } 12799a2dd95SBruce Richardson 12899a2dd95SBruce Richardson /* 12999a2dd95SBruce Richardson * Free memory blocks (kept in context for reuse). 13099a2dd95SBruce Richardson */ 13199a2dd95SBruce Richardson static void 13299a2dd95SBruce Richardson acl_build_free(struct acl_build_context *context, size_t s, void *p) 13399a2dd95SBruce Richardson { 13499a2dd95SBruce Richardson uint32_t n; 13599a2dd95SBruce Richardson 13699a2dd95SBruce Richardson for (n = 0; n < RTE_DIM(context->blocks); n++) { 13799a2dd95SBruce Richardson if (context->blocks[n].block_size == s) { 13899a2dd95SBruce Richardson *((void **)p) = context->blocks[n].mem_ptr; 13999a2dd95SBruce Richardson context->blocks[n].mem_ptr = p; 14099a2dd95SBruce Richardson return; 14199a2dd95SBruce Richardson } 14299a2dd95SBruce Richardson } 14399a2dd95SBruce Richardson for (n = 0; n < RTE_DIM(context->blocks); n++) { 14499a2dd95SBruce Richardson if (context->blocks[n].block_size == 0) { 14599a2dd95SBruce Richardson context->blocks[n].block_size = s; 14699a2dd95SBruce Richardson *((void **)p) = NULL; 14799a2dd95SBruce Richardson context->blocks[n].mem_ptr = p; 14899a2dd95SBruce Richardson return; 14999a2dd95SBruce Richardson } 15099a2dd95SBruce Richardson } 15199a2dd95SBruce Richardson } 15299a2dd95SBruce Richardson 15399a2dd95SBruce Richardson /* 15499a2dd95SBruce Richardson * Allocate and initialize a new node. 15599a2dd95SBruce Richardson */ 15699a2dd95SBruce Richardson static struct rte_acl_node * 15799a2dd95SBruce Richardson acl_alloc_node(struct acl_build_context *context, int level) 15899a2dd95SBruce Richardson { 15999a2dd95SBruce Richardson struct rte_acl_node *node; 16099a2dd95SBruce Richardson 16199a2dd95SBruce Richardson if (context->node_free_list != NULL) { 16299a2dd95SBruce Richardson node = context->node_free_list; 16399a2dd95SBruce Richardson context->node_free_list = node->next; 16499a2dd95SBruce Richardson memset(node, 0, sizeof(struct rte_acl_node)); 16599a2dd95SBruce Richardson } else { 16699a2dd95SBruce Richardson node = acl_build_alloc(context, sizeof(struct rte_acl_node), 1); 16799a2dd95SBruce Richardson } 16899a2dd95SBruce Richardson 16999a2dd95SBruce Richardson if (node != NULL) { 17099a2dd95SBruce Richardson node->num_ptrs = 0; 17199a2dd95SBruce Richardson node->level = level; 17299a2dd95SBruce Richardson node->node_type = RTE_ACL_NODE_UNDEFINED; 17399a2dd95SBruce Richardson node->node_index = RTE_ACL_NODE_UNDEFINED; 17499a2dd95SBruce Richardson context->num_nodes++; 17599a2dd95SBruce Richardson node->id = context->node_id++; 17699a2dd95SBruce Richardson } 17799a2dd95SBruce Richardson return node; 17899a2dd95SBruce Richardson } 17999a2dd95SBruce Richardson 18099a2dd95SBruce Richardson /* 18199a2dd95SBruce Richardson * Dereference all nodes to which this node points 18299a2dd95SBruce Richardson */ 18399a2dd95SBruce Richardson static void 18499a2dd95SBruce Richardson acl_free_node(struct acl_build_context *context, 18599a2dd95SBruce Richardson struct rte_acl_node *node) 18699a2dd95SBruce Richardson { 18799a2dd95SBruce Richardson uint32_t n; 18899a2dd95SBruce Richardson 18999a2dd95SBruce Richardson if (node->prev != NULL) 19099a2dd95SBruce Richardson node->prev->next = NULL; 19199a2dd95SBruce Richardson for (n = 0; n < node->num_ptrs; n++) 19299a2dd95SBruce Richardson acl_deref_ptr(context, node, n); 19399a2dd95SBruce Richardson 19499a2dd95SBruce Richardson /* free mrt if this is a match node */ 19599a2dd95SBruce Richardson if (node->mrt != NULL) { 19699a2dd95SBruce Richardson acl_build_free(context, sizeof(struct rte_acl_match_results), 19799a2dd95SBruce Richardson node->mrt); 19899a2dd95SBruce Richardson node->mrt = NULL; 19999a2dd95SBruce Richardson } 20099a2dd95SBruce Richardson 20199a2dd95SBruce Richardson /* free transitions to other nodes */ 20299a2dd95SBruce Richardson if (node->ptrs != NULL) { 20399a2dd95SBruce Richardson acl_build_free(context, 20499a2dd95SBruce Richardson node->max_ptrs * sizeof(struct rte_acl_ptr_set), 20599a2dd95SBruce Richardson node->ptrs); 20699a2dd95SBruce Richardson node->ptrs = NULL; 20799a2dd95SBruce Richardson } 20899a2dd95SBruce Richardson 20999a2dd95SBruce Richardson /* put it on the free list */ 21099a2dd95SBruce Richardson context->num_nodes--; 21199a2dd95SBruce Richardson node->next = context->node_free_list; 21299a2dd95SBruce Richardson context->node_free_list = node; 21399a2dd95SBruce Richardson } 21499a2dd95SBruce Richardson 21599a2dd95SBruce Richardson 21699a2dd95SBruce Richardson /* 21799a2dd95SBruce Richardson * Include src bitset in dst bitset 21899a2dd95SBruce Richardson */ 21999a2dd95SBruce Richardson static void 22099a2dd95SBruce Richardson acl_include(struct rte_acl_bitset *dst, struct rte_acl_bitset *src, bits_t mask) 22199a2dd95SBruce Richardson { 22299a2dd95SBruce Richardson uint32_t n; 22399a2dd95SBruce Richardson 22499a2dd95SBruce Richardson for (n = 0; n < RTE_ACL_BIT_SET_SIZE; n++) 22599a2dd95SBruce Richardson dst->bits[n] = (dst->bits[n] & mask) | src->bits[n]; 22699a2dd95SBruce Richardson } 22799a2dd95SBruce Richardson 22899a2dd95SBruce Richardson /* 22999a2dd95SBruce Richardson * Set dst to bits of src1 that are not in src2 23099a2dd95SBruce Richardson */ 23199a2dd95SBruce Richardson static int 23299a2dd95SBruce Richardson acl_exclude(struct rte_acl_bitset *dst, 23399a2dd95SBruce Richardson struct rte_acl_bitset *src1, 23499a2dd95SBruce Richardson struct rte_acl_bitset *src2) 23599a2dd95SBruce Richardson { 23699a2dd95SBruce Richardson uint32_t n; 23799a2dd95SBruce Richardson bits_t all_bits = 0; 23899a2dd95SBruce Richardson 23999a2dd95SBruce Richardson for (n = 0; n < RTE_ACL_BIT_SET_SIZE; n++) { 24099a2dd95SBruce Richardson dst->bits[n] = src1->bits[n] & ~src2->bits[n]; 24199a2dd95SBruce Richardson all_bits |= dst->bits[n]; 24299a2dd95SBruce Richardson } 24399a2dd95SBruce Richardson return all_bits != 0; 24499a2dd95SBruce Richardson } 24599a2dd95SBruce Richardson 24699a2dd95SBruce Richardson /* 24799a2dd95SBruce Richardson * Add a pointer (ptr) to a node. 24899a2dd95SBruce Richardson */ 24999a2dd95SBruce Richardson static int 25099a2dd95SBruce Richardson acl_add_ptr(struct acl_build_context *context, 25199a2dd95SBruce Richardson struct rte_acl_node *node, 25299a2dd95SBruce Richardson struct rte_acl_node *ptr, 25399a2dd95SBruce Richardson struct rte_acl_bitset *bits) 25499a2dd95SBruce Richardson { 25599a2dd95SBruce Richardson uint32_t n, num_ptrs; 25699a2dd95SBruce Richardson struct rte_acl_ptr_set *ptrs = NULL; 25799a2dd95SBruce Richardson 25899a2dd95SBruce Richardson /* 25999a2dd95SBruce Richardson * If there's already a pointer to the same node, just add to the bitset 26099a2dd95SBruce Richardson */ 26199a2dd95SBruce Richardson for (n = 0; n < node->num_ptrs; n++) { 26299a2dd95SBruce Richardson if (node->ptrs[n].ptr != NULL) { 26399a2dd95SBruce Richardson if (node->ptrs[n].ptr == ptr) { 26499a2dd95SBruce Richardson acl_include(&node->ptrs[n].values, bits, -1); 26599a2dd95SBruce Richardson acl_include(&node->values, bits, -1); 26699a2dd95SBruce Richardson return 0; 26799a2dd95SBruce Richardson } 26899a2dd95SBruce Richardson } 26999a2dd95SBruce Richardson } 27099a2dd95SBruce Richardson 27199a2dd95SBruce Richardson /* if there's no room for another pointer, make room */ 27299a2dd95SBruce Richardson if (node->num_ptrs >= node->max_ptrs) { 27399a2dd95SBruce Richardson /* add room for more pointers */ 27499a2dd95SBruce Richardson num_ptrs = node->max_ptrs + ACL_PTR_ALLOC; 27599a2dd95SBruce Richardson ptrs = acl_build_alloc(context, num_ptrs, sizeof(*ptrs)); 27699a2dd95SBruce Richardson 27799a2dd95SBruce Richardson /* copy current points to new memory allocation */ 27899a2dd95SBruce Richardson if (node->ptrs != NULL) { 27999a2dd95SBruce Richardson memcpy(ptrs, node->ptrs, 28099a2dd95SBruce Richardson node->num_ptrs * sizeof(*ptrs)); 28199a2dd95SBruce Richardson acl_build_free(context, node->max_ptrs * sizeof(*ptrs), 28299a2dd95SBruce Richardson node->ptrs); 28399a2dd95SBruce Richardson } 28499a2dd95SBruce Richardson node->ptrs = ptrs; 28599a2dd95SBruce Richardson node->max_ptrs = num_ptrs; 28699a2dd95SBruce Richardson } 28799a2dd95SBruce Richardson 28899a2dd95SBruce Richardson /* Find available ptr and add a new pointer to this node */ 28999a2dd95SBruce Richardson for (n = node->min_add; n < node->max_ptrs; n++) { 29099a2dd95SBruce Richardson if (node->ptrs[n].ptr == NULL) { 29199a2dd95SBruce Richardson node->ptrs[n].ptr = ptr; 29299a2dd95SBruce Richardson acl_include(&node->ptrs[n].values, bits, 0); 29399a2dd95SBruce Richardson acl_include(&node->values, bits, -1); 29499a2dd95SBruce Richardson if (ptr != NULL) 29599a2dd95SBruce Richardson ptr->ref_count++; 29699a2dd95SBruce Richardson if (node->num_ptrs <= n) 29799a2dd95SBruce Richardson node->num_ptrs = n + 1; 29899a2dd95SBruce Richardson return 0; 29999a2dd95SBruce Richardson } 30099a2dd95SBruce Richardson } 30199a2dd95SBruce Richardson 30299a2dd95SBruce Richardson return 0; 30399a2dd95SBruce Richardson } 30499a2dd95SBruce Richardson 30599a2dd95SBruce Richardson /* 30699a2dd95SBruce Richardson * Add a pointer for a range of values 30799a2dd95SBruce Richardson */ 30899a2dd95SBruce Richardson static int 30999a2dd95SBruce Richardson acl_add_ptr_range(struct acl_build_context *context, 31099a2dd95SBruce Richardson struct rte_acl_node *root, 31199a2dd95SBruce Richardson struct rte_acl_node *node, 31299a2dd95SBruce Richardson uint8_t low, 31399a2dd95SBruce Richardson uint8_t high) 31499a2dd95SBruce Richardson { 31599a2dd95SBruce Richardson uint32_t n; 31699a2dd95SBruce Richardson struct rte_acl_bitset bitset; 31799a2dd95SBruce Richardson 31899a2dd95SBruce Richardson /* clear the bitset values */ 31999a2dd95SBruce Richardson for (n = 0; n < RTE_ACL_BIT_SET_SIZE; n++) 32099a2dd95SBruce Richardson bitset.bits[n] = 0; 32199a2dd95SBruce Richardson 32299a2dd95SBruce Richardson /* for each bit in range, add bit to set */ 32399a2dd95SBruce Richardson for (n = 0; n < UINT8_MAX + 1; n++) 32499a2dd95SBruce Richardson if (n >= low && n <= high) 32599a2dd95SBruce Richardson bitset.bits[n / (sizeof(bits_t) * 8)] |= 32699a2dd95SBruce Richardson 1U << (n % (sizeof(bits_t) * CHAR_BIT)); 32799a2dd95SBruce Richardson 32899a2dd95SBruce Richardson return acl_add_ptr(context, root, node, &bitset); 32999a2dd95SBruce Richardson } 33099a2dd95SBruce Richardson 33199a2dd95SBruce Richardson /* 33299a2dd95SBruce Richardson * Generate a bitset from a byte value and mask. 33399a2dd95SBruce Richardson */ 33499a2dd95SBruce Richardson static int 33599a2dd95SBruce Richardson acl_gen_mask(struct rte_acl_bitset *bitset, uint32_t value, uint32_t mask) 33699a2dd95SBruce Richardson { 33799a2dd95SBruce Richardson int range = 0; 33899a2dd95SBruce Richardson uint32_t n; 33999a2dd95SBruce Richardson 34099a2dd95SBruce Richardson /* clear the bitset values */ 34199a2dd95SBruce Richardson for (n = 0; n < RTE_ACL_BIT_SET_SIZE; n++) 34299a2dd95SBruce Richardson bitset->bits[n] = 0; 34399a2dd95SBruce Richardson 34499a2dd95SBruce Richardson /* for each bit in value/mask, add bit to set */ 34599a2dd95SBruce Richardson for (n = 0; n < UINT8_MAX + 1; n++) { 34699a2dd95SBruce Richardson if ((n & mask) == value) { 34799a2dd95SBruce Richardson range++; 34899a2dd95SBruce Richardson bitset->bits[n / (sizeof(bits_t) * 8)] |= 34999a2dd95SBruce Richardson 1U << (n % (sizeof(bits_t) * CHAR_BIT)); 35099a2dd95SBruce Richardson } 35199a2dd95SBruce Richardson } 35299a2dd95SBruce Richardson return range; 35399a2dd95SBruce Richardson } 35499a2dd95SBruce Richardson 35599a2dd95SBruce Richardson /* 35699a2dd95SBruce Richardson * Determine how A and B intersect. 35799a2dd95SBruce Richardson * Determine if A and/or B are supersets of the intersection. 35899a2dd95SBruce Richardson */ 35999a2dd95SBruce Richardson static int 36099a2dd95SBruce Richardson acl_intersect_type(const struct rte_acl_bitset *a_bits, 36199a2dd95SBruce Richardson const struct rte_acl_bitset *b_bits, 36299a2dd95SBruce Richardson struct rte_acl_bitset *intersect) 36399a2dd95SBruce Richardson { 36499a2dd95SBruce Richardson uint32_t n; 36599a2dd95SBruce Richardson bits_t intersect_bits = 0; 36699a2dd95SBruce Richardson bits_t a_superset = 0; 36799a2dd95SBruce Richardson bits_t b_superset = 0; 36899a2dd95SBruce Richardson 36999a2dd95SBruce Richardson /* 37099a2dd95SBruce Richardson * calculate and store intersection and check if A and/or B have 37199a2dd95SBruce Richardson * bits outside the intersection (superset) 37299a2dd95SBruce Richardson */ 37399a2dd95SBruce Richardson for (n = 0; n < RTE_ACL_BIT_SET_SIZE; n++) { 37499a2dd95SBruce Richardson intersect->bits[n] = a_bits->bits[n] & b_bits->bits[n]; 37599a2dd95SBruce Richardson a_superset |= a_bits->bits[n] ^ intersect->bits[n]; 37699a2dd95SBruce Richardson b_superset |= b_bits->bits[n] ^ intersect->bits[n]; 37799a2dd95SBruce Richardson intersect_bits |= intersect->bits[n]; 37899a2dd95SBruce Richardson } 37999a2dd95SBruce Richardson 38099a2dd95SBruce Richardson n = (intersect_bits == 0 ? ACL_INTERSECT_NONE : ACL_INTERSECT) | 38199a2dd95SBruce Richardson (b_superset == 0 ? 0 : ACL_INTERSECT_B) | 38299a2dd95SBruce Richardson (a_superset == 0 ? 0 : ACL_INTERSECT_A); 38399a2dd95SBruce Richardson 38499a2dd95SBruce Richardson return n; 38599a2dd95SBruce Richardson } 38699a2dd95SBruce Richardson 38799a2dd95SBruce Richardson /* 38899a2dd95SBruce Richardson * Duplicate a node 38999a2dd95SBruce Richardson */ 39099a2dd95SBruce Richardson static struct rte_acl_node * 39199a2dd95SBruce Richardson acl_dup_node(struct acl_build_context *context, struct rte_acl_node *node) 39299a2dd95SBruce Richardson { 39399a2dd95SBruce Richardson uint32_t n; 39499a2dd95SBruce Richardson struct rte_acl_node *next; 39599a2dd95SBruce Richardson 39699a2dd95SBruce Richardson next = acl_alloc_node(context, node->level); 39799a2dd95SBruce Richardson 39899a2dd95SBruce Richardson /* allocate the pointers */ 39999a2dd95SBruce Richardson if (node->num_ptrs > 0) { 40099a2dd95SBruce Richardson next->ptrs = acl_build_alloc(context, 40199a2dd95SBruce Richardson node->max_ptrs, 40299a2dd95SBruce Richardson sizeof(struct rte_acl_ptr_set)); 40399a2dd95SBruce Richardson next->max_ptrs = node->max_ptrs; 40499a2dd95SBruce Richardson } 40599a2dd95SBruce Richardson 40699a2dd95SBruce Richardson /* copy over the pointers */ 40799a2dd95SBruce Richardson for (n = 0; n < node->num_ptrs; n++) { 40899a2dd95SBruce Richardson if (node->ptrs[n].ptr != NULL) { 40999a2dd95SBruce Richardson next->ptrs[n].ptr = node->ptrs[n].ptr; 41099a2dd95SBruce Richardson next->ptrs[n].ptr->ref_count++; 41199a2dd95SBruce Richardson acl_include(&next->ptrs[n].values, 41299a2dd95SBruce Richardson &node->ptrs[n].values, -1); 41399a2dd95SBruce Richardson } 41499a2dd95SBruce Richardson } 41599a2dd95SBruce Richardson 41699a2dd95SBruce Richardson next->num_ptrs = node->num_ptrs; 41799a2dd95SBruce Richardson 41899a2dd95SBruce Richardson /* copy over node's match results */ 41999a2dd95SBruce Richardson if (node->match_flag == 0) 42099a2dd95SBruce Richardson next->match_flag = 0; 42199a2dd95SBruce Richardson else { 42299a2dd95SBruce Richardson next->match_flag = -1; 42399a2dd95SBruce Richardson next->mrt = acl_build_alloc(context, 1, sizeof(*next->mrt)); 42499a2dd95SBruce Richardson memcpy(next->mrt, node->mrt, sizeof(*next->mrt)); 42599a2dd95SBruce Richardson } 42699a2dd95SBruce Richardson 42799a2dd95SBruce Richardson /* copy over node's bitset */ 42899a2dd95SBruce Richardson acl_include(&next->values, &node->values, -1); 42999a2dd95SBruce Richardson 43099a2dd95SBruce Richardson node->next = next; 43199a2dd95SBruce Richardson next->prev = node; 43299a2dd95SBruce Richardson 43399a2dd95SBruce Richardson return next; 43499a2dd95SBruce Richardson } 43599a2dd95SBruce Richardson 43699a2dd95SBruce Richardson /* 43799a2dd95SBruce Richardson * Dereference a pointer from a node 43899a2dd95SBruce Richardson */ 43999a2dd95SBruce Richardson static void 44099a2dd95SBruce Richardson acl_deref_ptr(struct acl_build_context *context, 44199a2dd95SBruce Richardson struct rte_acl_node *node, int index) 44299a2dd95SBruce Richardson { 44399a2dd95SBruce Richardson struct rte_acl_node *ref_node; 44499a2dd95SBruce Richardson 44599a2dd95SBruce Richardson /* De-reference the node at the specified pointer */ 44699a2dd95SBruce Richardson if (node != NULL && node->ptrs[index].ptr != NULL) { 44799a2dd95SBruce Richardson ref_node = node->ptrs[index].ptr; 44899a2dd95SBruce Richardson ref_node->ref_count--; 44999a2dd95SBruce Richardson if (ref_node->ref_count == 0) 45099a2dd95SBruce Richardson acl_free_node(context, ref_node); 45199a2dd95SBruce Richardson } 45299a2dd95SBruce Richardson } 45399a2dd95SBruce Richardson 45499a2dd95SBruce Richardson /* 45599a2dd95SBruce Richardson * acl_exclude rte_acl_bitset from src and copy remaining pointer to dst 45699a2dd95SBruce Richardson */ 45799a2dd95SBruce Richardson static int 45899a2dd95SBruce Richardson acl_copy_ptr(struct acl_build_context *context, 45999a2dd95SBruce Richardson struct rte_acl_node *dst, 46099a2dd95SBruce Richardson struct rte_acl_node *src, 46199a2dd95SBruce Richardson int index, 46299a2dd95SBruce Richardson struct rte_acl_bitset *b_bits) 46399a2dd95SBruce Richardson { 46499a2dd95SBruce Richardson int rc; 46599a2dd95SBruce Richardson struct rte_acl_bitset bits; 46699a2dd95SBruce Richardson 46799a2dd95SBruce Richardson if (b_bits != NULL) 46899a2dd95SBruce Richardson if (!acl_exclude(&bits, &src->ptrs[index].values, b_bits)) 46999a2dd95SBruce Richardson return 0; 47099a2dd95SBruce Richardson 47199a2dd95SBruce Richardson rc = acl_add_ptr(context, dst, src->ptrs[index].ptr, &bits); 47299a2dd95SBruce Richardson if (rc < 0) 47399a2dd95SBruce Richardson return rc; 47499a2dd95SBruce Richardson return 1; 47599a2dd95SBruce Richardson } 47699a2dd95SBruce Richardson 47799a2dd95SBruce Richardson /* 47899a2dd95SBruce Richardson * Fill in gaps in ptrs list with the ptr at the end of the list 47999a2dd95SBruce Richardson */ 48099a2dd95SBruce Richardson static void 48199a2dd95SBruce Richardson acl_compact_node_ptrs(struct rte_acl_node *node_a) 48299a2dd95SBruce Richardson { 48399a2dd95SBruce Richardson uint32_t n; 48499a2dd95SBruce Richardson int min_add = node_a->min_add; 48599a2dd95SBruce Richardson 48699a2dd95SBruce Richardson while (node_a->num_ptrs > 0 && 48799a2dd95SBruce Richardson node_a->ptrs[node_a->num_ptrs - 1].ptr == NULL) 48899a2dd95SBruce Richardson node_a->num_ptrs--; 48999a2dd95SBruce Richardson 49099a2dd95SBruce Richardson for (n = min_add; n + 1 < node_a->num_ptrs; n++) { 49199a2dd95SBruce Richardson 49299a2dd95SBruce Richardson /* if this entry is empty */ 49399a2dd95SBruce Richardson if (node_a->ptrs[n].ptr == NULL) { 49499a2dd95SBruce Richardson 49599a2dd95SBruce Richardson /* move the last pointer to this entry */ 49699a2dd95SBruce Richardson acl_include(&node_a->ptrs[n].values, 49799a2dd95SBruce Richardson &node_a->ptrs[node_a->num_ptrs - 1].values, 49899a2dd95SBruce Richardson 0); 49999a2dd95SBruce Richardson node_a->ptrs[n].ptr = 50099a2dd95SBruce Richardson node_a->ptrs[node_a->num_ptrs - 1].ptr; 50199a2dd95SBruce Richardson 50299a2dd95SBruce Richardson /* 50399a2dd95SBruce Richardson * mark the end as empty and adjust the number 50499a2dd95SBruce Richardson * of used pointer enum_tries 50599a2dd95SBruce Richardson */ 50699a2dd95SBruce Richardson node_a->ptrs[node_a->num_ptrs - 1].ptr = NULL; 50799a2dd95SBruce Richardson while (node_a->num_ptrs > 0 && 50899a2dd95SBruce Richardson node_a->ptrs[node_a->num_ptrs - 1].ptr == NULL) 50999a2dd95SBruce Richardson node_a->num_ptrs--; 51099a2dd95SBruce Richardson } 51199a2dd95SBruce Richardson } 51299a2dd95SBruce Richardson } 51399a2dd95SBruce Richardson 51499a2dd95SBruce Richardson static int 51599a2dd95SBruce Richardson acl_resolve_leaf(struct acl_build_context *context, 51699a2dd95SBruce Richardson struct rte_acl_node *node_a, 51799a2dd95SBruce Richardson struct rte_acl_node *node_b, 51899a2dd95SBruce Richardson struct rte_acl_node **node_c) 51999a2dd95SBruce Richardson { 52099a2dd95SBruce Richardson uint32_t n; 52199a2dd95SBruce Richardson int combined_priority = ACL_PRIORITY_EQUAL; 52299a2dd95SBruce Richardson 52399a2dd95SBruce Richardson for (n = 0; n < context->cfg.num_categories; n++) { 52499a2dd95SBruce Richardson if (node_a->mrt->priority[n] != node_b->mrt->priority[n]) { 52599a2dd95SBruce Richardson combined_priority |= (node_a->mrt->priority[n] > 52699a2dd95SBruce Richardson node_b->mrt->priority[n]) ? 52799a2dd95SBruce Richardson ACL_PRIORITY_NODE_A : ACL_PRIORITY_NODE_B; 52899a2dd95SBruce Richardson } 52999a2dd95SBruce Richardson } 53099a2dd95SBruce Richardson 53199a2dd95SBruce Richardson /* 53299a2dd95SBruce Richardson * if node a is higher or equal priority for all categories, 53399a2dd95SBruce Richardson * then return node_a. 53499a2dd95SBruce Richardson */ 53599a2dd95SBruce Richardson if (combined_priority == ACL_PRIORITY_NODE_A || 53699a2dd95SBruce Richardson combined_priority == ACL_PRIORITY_EQUAL) { 53799a2dd95SBruce Richardson *node_c = node_a; 53899a2dd95SBruce Richardson return 0; 53999a2dd95SBruce Richardson } 54099a2dd95SBruce Richardson 54199a2dd95SBruce Richardson /* 54299a2dd95SBruce Richardson * if node b is higher or equal priority for all categories, 54399a2dd95SBruce Richardson * then return node_b. 54499a2dd95SBruce Richardson */ 54599a2dd95SBruce Richardson if (combined_priority == ACL_PRIORITY_NODE_B) { 54699a2dd95SBruce Richardson *node_c = node_b; 54799a2dd95SBruce Richardson return 0; 54899a2dd95SBruce Richardson } 54999a2dd95SBruce Richardson 55099a2dd95SBruce Richardson /* 55199a2dd95SBruce Richardson * mixed priorities - create a new node with the highest priority 55299a2dd95SBruce Richardson * for each category. 55399a2dd95SBruce Richardson */ 55499a2dd95SBruce Richardson 55599a2dd95SBruce Richardson /* force new duplication. */ 55699a2dd95SBruce Richardson node_a->next = NULL; 55799a2dd95SBruce Richardson 55899a2dd95SBruce Richardson *node_c = acl_dup_node(context, node_a); 55999a2dd95SBruce Richardson for (n = 0; n < context->cfg.num_categories; n++) { 56099a2dd95SBruce Richardson if ((*node_c)->mrt->priority[n] < node_b->mrt->priority[n]) { 56199a2dd95SBruce Richardson (*node_c)->mrt->priority[n] = node_b->mrt->priority[n]; 56299a2dd95SBruce Richardson (*node_c)->mrt->results[n] = node_b->mrt->results[n]; 56399a2dd95SBruce Richardson } 56499a2dd95SBruce Richardson } 56599a2dd95SBruce Richardson return 0; 56699a2dd95SBruce Richardson } 56799a2dd95SBruce Richardson 56899a2dd95SBruce Richardson /* 56999a2dd95SBruce Richardson * Merge nodes A and B together, 57099a2dd95SBruce Richardson * returns a node that is the path for the intersection 57199a2dd95SBruce Richardson * 57299a2dd95SBruce Richardson * If match node (leaf on trie) 57399a2dd95SBruce Richardson * For each category 57499a2dd95SBruce Richardson * return node = highest priority result 57599a2dd95SBruce Richardson * 57699a2dd95SBruce Richardson * Create C as a duplicate of A to point to child intersections 57799a2dd95SBruce Richardson * If any pointers in C intersect with any in B 57899a2dd95SBruce Richardson * For each intersection 57999a2dd95SBruce Richardson * merge children 58099a2dd95SBruce Richardson * remove intersection from C pointer 58199a2dd95SBruce Richardson * add a pointer from C to child intersection node 58299a2dd95SBruce Richardson * Compact the pointers in A and B 58399a2dd95SBruce Richardson * Copy any B pointers that are outside of the intersection to C 58499a2dd95SBruce Richardson * If C has no references to the B trie 58599a2dd95SBruce Richardson * free C and return A 58699a2dd95SBruce Richardson * Else If C has no references to the A trie 58799a2dd95SBruce Richardson * free C and return B 58899a2dd95SBruce Richardson * Else 58999a2dd95SBruce Richardson * return C 59099a2dd95SBruce Richardson */ 59199a2dd95SBruce Richardson static int 59299a2dd95SBruce Richardson acl_merge_trie(struct acl_build_context *context, 59399a2dd95SBruce Richardson struct rte_acl_node *node_a, struct rte_acl_node *node_b, 59499a2dd95SBruce Richardson uint32_t level, struct rte_acl_node **return_c) 59599a2dd95SBruce Richardson { 59699a2dd95SBruce Richardson uint32_t n, m, ptrs_c, ptrs_b; 59799a2dd95SBruce Richardson uint32_t min_add_c, min_add_b; 59899a2dd95SBruce Richardson int node_intersect_type; 59999a2dd95SBruce Richardson struct rte_acl_bitset node_intersect; 60099a2dd95SBruce Richardson struct rte_acl_node *node_c; 60199a2dd95SBruce Richardson struct rte_acl_node *node_a_next; 60299a2dd95SBruce Richardson int node_b_refs; 60399a2dd95SBruce Richardson int node_a_refs; 60499a2dd95SBruce Richardson 60599a2dd95SBruce Richardson node_c = node_a; 60699a2dd95SBruce Richardson node_a_next = node_a->next; 60799a2dd95SBruce Richardson min_add_c = 0; 60899a2dd95SBruce Richardson min_add_b = 0; 60999a2dd95SBruce Richardson node_a_refs = node_a->num_ptrs; 61099a2dd95SBruce Richardson node_b_refs = 0; 61199a2dd95SBruce Richardson node_intersect_type = 0; 61299a2dd95SBruce Richardson 61399a2dd95SBruce Richardson /* Resolve leaf nodes (matches) */ 61499a2dd95SBruce Richardson if (node_a->match_flag != 0) { 61599a2dd95SBruce Richardson acl_resolve_leaf(context, node_a, node_b, return_c); 61699a2dd95SBruce Richardson return 0; 61799a2dd95SBruce Richardson } 61899a2dd95SBruce Richardson 61999a2dd95SBruce Richardson /* 62099a2dd95SBruce Richardson * Create node C as a copy of node A, and do: C = merge(A,B); 62199a2dd95SBruce Richardson * If node A can be used instead (A==C), then later we'll 62299a2dd95SBruce Richardson * destroy C and return A. 62399a2dd95SBruce Richardson */ 62499a2dd95SBruce Richardson if (level > 0) 62599a2dd95SBruce Richardson node_c = acl_dup_node(context, node_a); 62699a2dd95SBruce Richardson 62799a2dd95SBruce Richardson /* 62899a2dd95SBruce Richardson * If the two node transitions intersect then merge the transitions. 62999a2dd95SBruce Richardson * Check intersection for entire node (all pointers) 63099a2dd95SBruce Richardson */ 63199a2dd95SBruce Richardson node_intersect_type = acl_intersect_type(&node_c->values, 63299a2dd95SBruce Richardson &node_b->values, 63399a2dd95SBruce Richardson &node_intersect); 63499a2dd95SBruce Richardson 63599a2dd95SBruce Richardson if (node_intersect_type & ACL_INTERSECT) { 63699a2dd95SBruce Richardson 63799a2dd95SBruce Richardson min_add_b = node_b->min_add; 63899a2dd95SBruce Richardson node_b->min_add = node_b->num_ptrs; 63999a2dd95SBruce Richardson ptrs_b = node_b->num_ptrs; 64099a2dd95SBruce Richardson 64199a2dd95SBruce Richardson min_add_c = node_c->min_add; 64299a2dd95SBruce Richardson node_c->min_add = node_c->num_ptrs; 64399a2dd95SBruce Richardson ptrs_c = node_c->num_ptrs; 64499a2dd95SBruce Richardson 64599a2dd95SBruce Richardson for (n = 0; n < ptrs_c; n++) { 64699a2dd95SBruce Richardson if (node_c->ptrs[n].ptr == NULL) { 64799a2dd95SBruce Richardson node_a_refs--; 64899a2dd95SBruce Richardson continue; 64999a2dd95SBruce Richardson } 65099a2dd95SBruce Richardson node_c->ptrs[n].ptr->next = NULL; 65199a2dd95SBruce Richardson for (m = 0; m < ptrs_b; m++) { 65299a2dd95SBruce Richardson 65399a2dd95SBruce Richardson struct rte_acl_bitset child_intersect; 65499a2dd95SBruce Richardson int child_intersect_type; 65599a2dd95SBruce Richardson struct rte_acl_node *child_node_c = NULL; 65699a2dd95SBruce Richardson 65799a2dd95SBruce Richardson if (node_b->ptrs[m].ptr == NULL || 65899a2dd95SBruce Richardson node_c->ptrs[n].ptr == 65999a2dd95SBruce Richardson node_b->ptrs[m].ptr) 66099a2dd95SBruce Richardson continue; 66199a2dd95SBruce Richardson 66299a2dd95SBruce Richardson child_intersect_type = acl_intersect_type( 66399a2dd95SBruce Richardson &node_c->ptrs[n].values, 66499a2dd95SBruce Richardson &node_b->ptrs[m].values, 66599a2dd95SBruce Richardson &child_intersect); 66699a2dd95SBruce Richardson 66799a2dd95SBruce Richardson if ((child_intersect_type & ACL_INTERSECT) != 66899a2dd95SBruce Richardson 0) { 66999a2dd95SBruce Richardson if (acl_merge_trie(context, 67099a2dd95SBruce Richardson node_c->ptrs[n].ptr, 67199a2dd95SBruce Richardson node_b->ptrs[m].ptr, 67299a2dd95SBruce Richardson level + 1, 67399a2dd95SBruce Richardson &child_node_c)) 67499a2dd95SBruce Richardson return 1; 67599a2dd95SBruce Richardson 67699a2dd95SBruce Richardson if (child_node_c != NULL && 67799a2dd95SBruce Richardson child_node_c != 67899a2dd95SBruce Richardson node_c->ptrs[n].ptr) { 67999a2dd95SBruce Richardson 68099a2dd95SBruce Richardson node_b_refs++; 68199a2dd95SBruce Richardson 68299a2dd95SBruce Richardson /* 68399a2dd95SBruce Richardson * Added link from C to 68499a2dd95SBruce Richardson * child_C for all transitions 68599a2dd95SBruce Richardson * in the intersection. 68699a2dd95SBruce Richardson */ 68799a2dd95SBruce Richardson acl_add_ptr(context, node_c, 68899a2dd95SBruce Richardson child_node_c, 68999a2dd95SBruce Richardson &child_intersect); 69099a2dd95SBruce Richardson 69199a2dd95SBruce Richardson /* 69299a2dd95SBruce Richardson * inc refs if pointer is not 69399a2dd95SBruce Richardson * to node b. 69499a2dd95SBruce Richardson */ 69599a2dd95SBruce Richardson node_a_refs += (child_node_c != 69699a2dd95SBruce Richardson node_b->ptrs[m].ptr); 69799a2dd95SBruce Richardson 69899a2dd95SBruce Richardson /* 69999a2dd95SBruce Richardson * Remove intersection from C 70099a2dd95SBruce Richardson * pointer. 70199a2dd95SBruce Richardson */ 70299a2dd95SBruce Richardson if (!acl_exclude( 70399a2dd95SBruce Richardson &node_c->ptrs[n].values, 70499a2dd95SBruce Richardson &node_c->ptrs[n].values, 70599a2dd95SBruce Richardson &child_intersect)) { 70699a2dd95SBruce Richardson acl_deref_ptr(context, 70799a2dd95SBruce Richardson node_c, n); 70899a2dd95SBruce Richardson node_c->ptrs[n].ptr = 70999a2dd95SBruce Richardson NULL; 71099a2dd95SBruce Richardson node_a_refs--; 71199a2dd95SBruce Richardson } 71299a2dd95SBruce Richardson } 71399a2dd95SBruce Richardson } 71499a2dd95SBruce Richardson } 71599a2dd95SBruce Richardson } 71699a2dd95SBruce Richardson 71799a2dd95SBruce Richardson /* Compact pointers */ 71899a2dd95SBruce Richardson node_c->min_add = min_add_c; 71999a2dd95SBruce Richardson acl_compact_node_ptrs(node_c); 72099a2dd95SBruce Richardson node_b->min_add = min_add_b; 72199a2dd95SBruce Richardson acl_compact_node_ptrs(node_b); 72299a2dd95SBruce Richardson } 72399a2dd95SBruce Richardson 72499a2dd95SBruce Richardson /* 72599a2dd95SBruce Richardson * Copy pointers outside of the intersection from B to C 72699a2dd95SBruce Richardson */ 72799a2dd95SBruce Richardson if ((node_intersect_type & ACL_INTERSECT_B) != 0) { 72899a2dd95SBruce Richardson node_b_refs++; 72999a2dd95SBruce Richardson for (m = 0; m < node_b->num_ptrs; m++) 73099a2dd95SBruce Richardson if (node_b->ptrs[m].ptr != NULL) 73199a2dd95SBruce Richardson acl_copy_ptr(context, node_c, 73299a2dd95SBruce Richardson node_b, m, &node_intersect); 73399a2dd95SBruce Richardson } 73499a2dd95SBruce Richardson 73599a2dd95SBruce Richardson /* 73699a2dd95SBruce Richardson * Free node C if top of trie is contained in A or B 73799a2dd95SBruce Richardson * if node C is a duplicate of node A && 73899a2dd95SBruce Richardson * node C was not an existing duplicate 73999a2dd95SBruce Richardson */ 74099a2dd95SBruce Richardson if (node_c != node_a && node_c != node_a_next) { 74199a2dd95SBruce Richardson 74299a2dd95SBruce Richardson /* 74399a2dd95SBruce Richardson * if the intersection has no references to the 74499a2dd95SBruce Richardson * B side, then it is contained in A 74599a2dd95SBruce Richardson */ 74699a2dd95SBruce Richardson if (node_b_refs == 0) { 74799a2dd95SBruce Richardson acl_free_node(context, node_c); 74899a2dd95SBruce Richardson node_c = node_a; 74999a2dd95SBruce Richardson } else { 75099a2dd95SBruce Richardson /* 75199a2dd95SBruce Richardson * if the intersection has no references to the 75299a2dd95SBruce Richardson * A side, then it is contained in B. 75399a2dd95SBruce Richardson */ 75499a2dd95SBruce Richardson if (node_a_refs == 0) { 75599a2dd95SBruce Richardson acl_free_node(context, node_c); 75699a2dd95SBruce Richardson node_c = node_b; 75799a2dd95SBruce Richardson } 75899a2dd95SBruce Richardson } 75999a2dd95SBruce Richardson } 76099a2dd95SBruce Richardson 76199a2dd95SBruce Richardson if (return_c != NULL) 76299a2dd95SBruce Richardson *return_c = node_c; 76399a2dd95SBruce Richardson 76499a2dd95SBruce Richardson if (level == 0) 76599a2dd95SBruce Richardson acl_free_node(context, node_b); 76699a2dd95SBruce Richardson 76799a2dd95SBruce Richardson return 0; 76899a2dd95SBruce Richardson } 76999a2dd95SBruce Richardson 77099a2dd95SBruce Richardson /* 77199a2dd95SBruce Richardson * Reset current runtime fields before next build: 77299a2dd95SBruce Richardson * - free allocated RT memory. 77399a2dd95SBruce Richardson * - reset all RT related fields to zero. 77499a2dd95SBruce Richardson */ 77599a2dd95SBruce Richardson static void 77699a2dd95SBruce Richardson acl_build_reset(struct rte_acl_ctx *ctx) 77799a2dd95SBruce Richardson { 77899a2dd95SBruce Richardson rte_free(ctx->mem); 77999a2dd95SBruce Richardson memset(&ctx->num_categories, 0, 78099a2dd95SBruce Richardson sizeof(*ctx) - offsetof(struct rte_acl_ctx, num_categories)); 78199a2dd95SBruce Richardson } 78299a2dd95SBruce Richardson 78399a2dd95SBruce Richardson static void 78499a2dd95SBruce Richardson acl_gen_full_range(struct acl_build_context *context, struct rte_acl_node *root, 78599a2dd95SBruce Richardson struct rte_acl_node *end, int size, int level) 78699a2dd95SBruce Richardson { 78799a2dd95SBruce Richardson struct rte_acl_node *node, *prev; 78899a2dd95SBruce Richardson uint32_t n; 78999a2dd95SBruce Richardson 79099a2dd95SBruce Richardson prev = root; 79199a2dd95SBruce Richardson for (n = size - 1; n > 0; n--) { 79299a2dd95SBruce Richardson node = acl_alloc_node(context, level++); 79399a2dd95SBruce Richardson acl_add_ptr_range(context, prev, node, 0, UINT8_MAX); 79499a2dd95SBruce Richardson prev = node; 79599a2dd95SBruce Richardson } 79699a2dd95SBruce Richardson acl_add_ptr_range(context, prev, end, 0, UINT8_MAX); 79799a2dd95SBruce Richardson } 79899a2dd95SBruce Richardson 79999a2dd95SBruce Richardson static void 80099a2dd95SBruce Richardson acl_gen_range_mdl(struct acl_build_context *context, struct rte_acl_node *root, 80199a2dd95SBruce Richardson struct rte_acl_node *end, uint8_t lo, uint8_t hi, int size, int level) 80299a2dd95SBruce Richardson { 80399a2dd95SBruce Richardson struct rte_acl_node *node; 80499a2dd95SBruce Richardson 80599a2dd95SBruce Richardson node = acl_alloc_node(context, level++); 80699a2dd95SBruce Richardson acl_add_ptr_range(context, root, node, lo, hi); 80799a2dd95SBruce Richardson acl_gen_full_range(context, node, end, size - 1, level); 80899a2dd95SBruce Richardson } 80999a2dd95SBruce Richardson 81099a2dd95SBruce Richardson static void 81199a2dd95SBruce Richardson acl_gen_range_low(struct acl_build_context *context, struct rte_acl_node *root, 81299a2dd95SBruce Richardson struct rte_acl_node *end, const uint8_t *lo, int size, int level) 81399a2dd95SBruce Richardson { 81499a2dd95SBruce Richardson struct rte_acl_node *node; 81599a2dd95SBruce Richardson uint32_t n; 81699a2dd95SBruce Richardson 81799a2dd95SBruce Richardson n = size - 1; 81899a2dd95SBruce Richardson if (n == 0) { 81999a2dd95SBruce Richardson acl_add_ptr_range(context, root, end, lo[0], UINT8_MAX); 82099a2dd95SBruce Richardson return; 82199a2dd95SBruce Richardson } 82299a2dd95SBruce Richardson 82399a2dd95SBruce Richardson node = acl_alloc_node(context, level++); 82499a2dd95SBruce Richardson acl_add_ptr_range(context, root, node, lo[n], lo[n]); 82599a2dd95SBruce Richardson 82699a2dd95SBruce Richardson /* generate lower-bound sub-trie */ 82799a2dd95SBruce Richardson acl_gen_range_low(context, node, end, lo, n, level); 82899a2dd95SBruce Richardson 82999a2dd95SBruce Richardson /* generate middle sub-trie */ 83099a2dd95SBruce Richardson if (n > 1 && lo[n - 1] != UINT8_MAX) 83199a2dd95SBruce Richardson acl_gen_range_mdl(context, node, end, lo[n - 1] + 1, UINT8_MAX, 83299a2dd95SBruce Richardson n, level); 83399a2dd95SBruce Richardson } 83499a2dd95SBruce Richardson 83599a2dd95SBruce Richardson static void 83699a2dd95SBruce Richardson acl_gen_range_high(struct acl_build_context *context, struct rte_acl_node *root, 83799a2dd95SBruce Richardson struct rte_acl_node *end, const uint8_t *hi, int size, int level) 83899a2dd95SBruce Richardson { 83999a2dd95SBruce Richardson struct rte_acl_node *node; 84099a2dd95SBruce Richardson uint32_t n; 84199a2dd95SBruce Richardson 84299a2dd95SBruce Richardson n = size - 1; 84399a2dd95SBruce Richardson if (n == 0) { 84499a2dd95SBruce Richardson acl_add_ptr_range(context, root, end, 0, hi[0]); 84599a2dd95SBruce Richardson return; 84699a2dd95SBruce Richardson } 84799a2dd95SBruce Richardson 84899a2dd95SBruce Richardson node = acl_alloc_node(context, level++); 84999a2dd95SBruce Richardson acl_add_ptr_range(context, root, node, hi[n], hi[n]); 85099a2dd95SBruce Richardson 85199a2dd95SBruce Richardson /* generate upper-bound sub-trie */ 85299a2dd95SBruce Richardson acl_gen_range_high(context, node, end, hi, n, level); 85399a2dd95SBruce Richardson 85499a2dd95SBruce Richardson /* generate middle sub-trie */ 85599a2dd95SBruce Richardson if (n > 1 && hi[n - 1] != 0) 85699a2dd95SBruce Richardson acl_gen_range_mdl(context, node, end, 0, hi[n - 1] - 1, 85799a2dd95SBruce Richardson n, level); 85899a2dd95SBruce Richardson } 85999a2dd95SBruce Richardson 86099a2dd95SBruce Richardson static struct rte_acl_node * 86199a2dd95SBruce Richardson acl_gen_range_trie(struct acl_build_context *context, 86299a2dd95SBruce Richardson const void *min, const void *max, 86399a2dd95SBruce Richardson int size, int level, struct rte_acl_node **pend) 86499a2dd95SBruce Richardson { 86599a2dd95SBruce Richardson int32_t k, n; 86699a2dd95SBruce Richardson uint8_t hi_ff, lo_00; 86799a2dd95SBruce Richardson struct rte_acl_node *node, *prev, *root; 86899a2dd95SBruce Richardson const uint8_t *lo; 86999a2dd95SBruce Richardson const uint8_t *hi; 87099a2dd95SBruce Richardson 87199a2dd95SBruce Richardson lo = min; 87299a2dd95SBruce Richardson hi = max; 87399a2dd95SBruce Richardson 87499a2dd95SBruce Richardson *pend = acl_alloc_node(context, level + size); 87599a2dd95SBruce Richardson root = acl_alloc_node(context, level++); 87699a2dd95SBruce Richardson prev = root; 87799a2dd95SBruce Richardson 87899a2dd95SBruce Richardson /* build common sub-trie till possible */ 87999a2dd95SBruce Richardson for (n = size - 1; n > 0 && lo[n] == hi[n]; n--) { 88099a2dd95SBruce Richardson node = acl_alloc_node(context, level++); 88199a2dd95SBruce Richardson acl_add_ptr_range(context, prev, node, lo[n], hi[n]); 88299a2dd95SBruce Richardson prev = node; 88399a2dd95SBruce Richardson } 88499a2dd95SBruce Richardson 88599a2dd95SBruce Richardson /* no branch needed, just one sub-trie */ 88699a2dd95SBruce Richardson if (n == 0) { 88799a2dd95SBruce Richardson acl_add_ptr_range(context, prev, *pend, lo[0], hi[0]); 88899a2dd95SBruce Richardson return root; 88999a2dd95SBruce Richardson } 89099a2dd95SBruce Richardson 8917be78d02SJosh Soref /* gather information about divergent paths */ 89299a2dd95SBruce Richardson lo_00 = 0; 89399a2dd95SBruce Richardson hi_ff = UINT8_MAX; 89499a2dd95SBruce Richardson for (k = n - 1; k >= 0; k--) { 89599a2dd95SBruce Richardson hi_ff &= hi[k]; 89699a2dd95SBruce Richardson lo_00 |= lo[k]; 89799a2dd95SBruce Richardson } 89899a2dd95SBruce Richardson 89999a2dd95SBruce Richardson /* generate left (lower-bound) sub-trie */ 90099a2dd95SBruce Richardson if (lo_00 != 0) 90199a2dd95SBruce Richardson acl_gen_range_low(context, prev, *pend, lo, n + 1, level); 90299a2dd95SBruce Richardson 90399a2dd95SBruce Richardson /* generate right (upper-bound) sub-trie */ 90499a2dd95SBruce Richardson if (hi_ff != UINT8_MAX) 90599a2dd95SBruce Richardson acl_gen_range_high(context, prev, *pend, hi, n + 1, level); 90699a2dd95SBruce Richardson 90799a2dd95SBruce Richardson /* generate sub-trie in the middle */ 90899a2dd95SBruce Richardson if (lo[n] + 1 != hi[n] || lo_00 == 0 || hi_ff == UINT8_MAX) { 90999a2dd95SBruce Richardson lo_00 = lo[n] + (lo_00 != 0); 91099a2dd95SBruce Richardson hi_ff = hi[n] - (hi_ff != UINT8_MAX); 91199a2dd95SBruce Richardson acl_gen_range_mdl(context, prev, *pend, lo_00, hi_ff, 91299a2dd95SBruce Richardson n + 1, level); 91399a2dd95SBruce Richardson } 91499a2dd95SBruce Richardson 91599a2dd95SBruce Richardson return root; 91699a2dd95SBruce Richardson } 91799a2dd95SBruce Richardson 91899a2dd95SBruce Richardson static struct rte_acl_node * 91999a2dd95SBruce Richardson acl_gen_mask_trie(struct acl_build_context *context, 92099a2dd95SBruce Richardson const void *value, const void *mask, 92199a2dd95SBruce Richardson int size, int level, struct rte_acl_node **pend) 92299a2dd95SBruce Richardson { 92399a2dd95SBruce Richardson int32_t n; 92499a2dd95SBruce Richardson struct rte_acl_node *root; 92599a2dd95SBruce Richardson struct rte_acl_node *node, *prev; 92699a2dd95SBruce Richardson struct rte_acl_bitset bits; 92799a2dd95SBruce Richardson const uint8_t *val = value; 92899a2dd95SBruce Richardson const uint8_t *msk = mask; 92999a2dd95SBruce Richardson 93099a2dd95SBruce Richardson root = acl_alloc_node(context, level++); 93199a2dd95SBruce Richardson prev = root; 93299a2dd95SBruce Richardson 93399a2dd95SBruce Richardson for (n = size - 1; n >= 0; n--) { 93499a2dd95SBruce Richardson node = acl_alloc_node(context, level++); 93599a2dd95SBruce Richardson acl_gen_mask(&bits, val[n] & msk[n], msk[n]); 93699a2dd95SBruce Richardson acl_add_ptr(context, prev, node, &bits); 93799a2dd95SBruce Richardson prev = node; 93899a2dd95SBruce Richardson } 93999a2dd95SBruce Richardson 94099a2dd95SBruce Richardson *pend = prev; 94199a2dd95SBruce Richardson return root; 94299a2dd95SBruce Richardson } 94399a2dd95SBruce Richardson 94499a2dd95SBruce Richardson static struct rte_acl_node * 94599a2dd95SBruce Richardson build_trie(struct acl_build_context *context, struct rte_acl_build_rule *head, 94699a2dd95SBruce Richardson struct rte_acl_build_rule **last, uint32_t *count) 94799a2dd95SBruce Richardson { 94899a2dd95SBruce Richardson uint32_t n, m; 94999a2dd95SBruce Richardson int field_index, node_count; 95099a2dd95SBruce Richardson struct rte_acl_node *trie; 95199a2dd95SBruce Richardson struct rte_acl_build_rule *prev, *rule; 95299a2dd95SBruce Richardson struct rte_acl_node *end, *merge, *root, *end_prev; 95399a2dd95SBruce Richardson const struct rte_acl_field *fld; 95499a2dd95SBruce Richardson 95599a2dd95SBruce Richardson prev = head; 95699a2dd95SBruce Richardson rule = head; 95799a2dd95SBruce Richardson *last = prev; 95899a2dd95SBruce Richardson 95999a2dd95SBruce Richardson trie = acl_alloc_node(context, 0); 96099a2dd95SBruce Richardson 96199a2dd95SBruce Richardson while (rule != NULL) { 96299a2dd95SBruce Richardson 96399a2dd95SBruce Richardson root = acl_alloc_node(context, 0); 96499a2dd95SBruce Richardson 96599a2dd95SBruce Richardson root->ref_count = 1; 96699a2dd95SBruce Richardson end = root; 96799a2dd95SBruce Richardson 96899a2dd95SBruce Richardson for (n = 0; n < rule->config->num_fields; n++) { 96999a2dd95SBruce Richardson 97099a2dd95SBruce Richardson field_index = rule->config->defs[n].field_index; 97199a2dd95SBruce Richardson fld = rule->f->field + field_index; 97299a2dd95SBruce Richardson end_prev = end; 97399a2dd95SBruce Richardson 97499a2dd95SBruce Richardson /* build a mini-trie for this field */ 97599a2dd95SBruce Richardson switch (rule->config->defs[n].type) { 97699a2dd95SBruce Richardson 97799a2dd95SBruce Richardson case RTE_ACL_FIELD_TYPE_BITMASK: 97899a2dd95SBruce Richardson merge = acl_gen_mask_trie(context, 97999a2dd95SBruce Richardson &fld->value, 98099a2dd95SBruce Richardson &fld->mask_range, 98199a2dd95SBruce Richardson rule->config->defs[n].size, 98299a2dd95SBruce Richardson end->level + 1, 98399a2dd95SBruce Richardson &end); 98499a2dd95SBruce Richardson break; 98599a2dd95SBruce Richardson 98699a2dd95SBruce Richardson case RTE_ACL_FIELD_TYPE_MASK: 98799a2dd95SBruce Richardson { 98899a2dd95SBruce Richardson /* 98999a2dd95SBruce Richardson * set msb for the size of the field and 99099a2dd95SBruce Richardson * all higher bits. 99199a2dd95SBruce Richardson */ 99299a2dd95SBruce Richardson uint64_t mask; 99399a2dd95SBruce Richardson mask = RTE_ACL_MASKLEN_TO_BITMASK( 99445109815SKonstantin Ananyev fld->mask_range.u64, 99599a2dd95SBruce Richardson rule->config->defs[n].size); 99699a2dd95SBruce Richardson 99799a2dd95SBruce Richardson /* gen a mini-trie for this field */ 99899a2dd95SBruce Richardson merge = acl_gen_mask_trie(context, 99999a2dd95SBruce Richardson &fld->value, 100099a2dd95SBruce Richardson (char *)&mask, 100199a2dd95SBruce Richardson rule->config->defs[n].size, 100299a2dd95SBruce Richardson end->level + 1, 100399a2dd95SBruce Richardson &end); 100499a2dd95SBruce Richardson } 100599a2dd95SBruce Richardson break; 100699a2dd95SBruce Richardson 100799a2dd95SBruce Richardson case RTE_ACL_FIELD_TYPE_RANGE: 100899a2dd95SBruce Richardson merge = acl_gen_range_trie(context, 100999a2dd95SBruce Richardson &rule->f->field[field_index].value, 101099a2dd95SBruce Richardson &rule->f->field[field_index].mask_range, 101199a2dd95SBruce Richardson rule->config->defs[n].size, 101299a2dd95SBruce Richardson end->level + 1, 101399a2dd95SBruce Richardson &end); 101499a2dd95SBruce Richardson break; 101599a2dd95SBruce Richardson 101699a2dd95SBruce Richardson default: 101799a2dd95SBruce Richardson RTE_LOG(ERR, ACL, 101899a2dd95SBruce Richardson "Error in rule[%u] type - %hhu\n", 101999a2dd95SBruce Richardson rule->f->data.userdata, 102099a2dd95SBruce Richardson rule->config->defs[n].type); 102199a2dd95SBruce Richardson return NULL; 102299a2dd95SBruce Richardson } 102399a2dd95SBruce Richardson 102499a2dd95SBruce Richardson /* merge this field on to the end of the rule */ 102599a2dd95SBruce Richardson if (acl_merge_trie(context, end_prev, merge, 0, 102699a2dd95SBruce Richardson NULL) != 0) { 102799a2dd95SBruce Richardson return NULL; 102899a2dd95SBruce Richardson } 102999a2dd95SBruce Richardson } 103099a2dd95SBruce Richardson 103199a2dd95SBruce Richardson end->match_flag = ++context->num_build_rules; 103299a2dd95SBruce Richardson 103399a2dd95SBruce Richardson /* 103499a2dd95SBruce Richardson * Setup the results for this rule. 103599a2dd95SBruce Richardson * The result and priority of each category. 103699a2dd95SBruce Richardson */ 103799a2dd95SBruce Richardson if (end->mrt == NULL) 103899a2dd95SBruce Richardson end->mrt = acl_build_alloc(context, 1, 103999a2dd95SBruce Richardson sizeof(*end->mrt)); 104099a2dd95SBruce Richardson 104199a2dd95SBruce Richardson for (m = context->cfg.num_categories; 0 != m--; ) { 104299a2dd95SBruce Richardson if (rule->f->data.category_mask & (1U << m)) { 104399a2dd95SBruce Richardson end->mrt->results[m] = rule->f->data.userdata; 104499a2dd95SBruce Richardson end->mrt->priority[m] = rule->f->data.priority; 104599a2dd95SBruce Richardson } else { 104699a2dd95SBruce Richardson end->mrt->results[m] = 0; 104799a2dd95SBruce Richardson end->mrt->priority[m] = 0; 104899a2dd95SBruce Richardson } 104999a2dd95SBruce Richardson } 105099a2dd95SBruce Richardson 105199a2dd95SBruce Richardson node_count = context->num_nodes; 105299a2dd95SBruce Richardson (*count)++; 105399a2dd95SBruce Richardson 105499a2dd95SBruce Richardson /* merge this rule into the trie */ 105599a2dd95SBruce Richardson if (acl_merge_trie(context, trie, root, 0, NULL)) 105699a2dd95SBruce Richardson return NULL; 105799a2dd95SBruce Richardson 105899a2dd95SBruce Richardson node_count = context->num_nodes - node_count; 105999a2dd95SBruce Richardson if (node_count > context->cur_node_max) { 106099a2dd95SBruce Richardson *last = prev; 106199a2dd95SBruce Richardson return trie; 106299a2dd95SBruce Richardson } 106399a2dd95SBruce Richardson 106499a2dd95SBruce Richardson prev = rule; 106599a2dd95SBruce Richardson rule = rule->next; 106699a2dd95SBruce Richardson } 106799a2dd95SBruce Richardson 106899a2dd95SBruce Richardson *last = NULL; 106999a2dd95SBruce Richardson return trie; 107099a2dd95SBruce Richardson } 107199a2dd95SBruce Richardson 107299a2dd95SBruce Richardson static void 107399a2dd95SBruce Richardson acl_calc_wildness(struct rte_acl_build_rule *head, 107499a2dd95SBruce Richardson const struct rte_acl_config *config) 107599a2dd95SBruce Richardson { 107699a2dd95SBruce Richardson uint32_t n; 107799a2dd95SBruce Richardson struct rte_acl_build_rule *rule; 107899a2dd95SBruce Richardson 107999a2dd95SBruce Richardson for (rule = head; rule != NULL; rule = rule->next) { 108099a2dd95SBruce Richardson 108199a2dd95SBruce Richardson for (n = 0; n < config->num_fields; n++) { 108299a2dd95SBruce Richardson 108399a2dd95SBruce Richardson double wild = 0; 108499a2dd95SBruce Richardson uint32_t bit_len = CHAR_BIT * config->defs[n].size; 108599a2dd95SBruce Richardson uint64_t msk_val = RTE_LEN2MASK(bit_len, 108699a2dd95SBruce Richardson typeof(msk_val)); 108799a2dd95SBruce Richardson double size = bit_len; 108899a2dd95SBruce Richardson int field_index = config->defs[n].field_index; 108999a2dd95SBruce Richardson const struct rte_acl_field *fld = rule->f->field + 109099a2dd95SBruce Richardson field_index; 109199a2dd95SBruce Richardson 109299a2dd95SBruce Richardson switch (rule->config->defs[n].type) { 109399a2dd95SBruce Richardson case RTE_ACL_FIELD_TYPE_BITMASK: 1094*3d4e27fdSDavid Marchand wild = (size - rte_popcount64( 109599a2dd95SBruce Richardson fld->mask_range.u64 & msk_val)) / 109699a2dd95SBruce Richardson size; 109799a2dd95SBruce Richardson break; 109899a2dd95SBruce Richardson 109999a2dd95SBruce Richardson case RTE_ACL_FIELD_TYPE_MASK: 110099a2dd95SBruce Richardson wild = (size - fld->mask_range.u32) / size; 110199a2dd95SBruce Richardson break; 110299a2dd95SBruce Richardson 110399a2dd95SBruce Richardson case RTE_ACL_FIELD_TYPE_RANGE: 110499a2dd95SBruce Richardson wild = (fld->mask_range.u64 & msk_val) - 110599a2dd95SBruce Richardson (fld->value.u64 & msk_val); 110699a2dd95SBruce Richardson wild = wild / msk_val; 110799a2dd95SBruce Richardson break; 110899a2dd95SBruce Richardson } 110999a2dd95SBruce Richardson 111099a2dd95SBruce Richardson rule->wildness[field_index] = (uint32_t)(wild * 100); 111199a2dd95SBruce Richardson } 111299a2dd95SBruce Richardson } 111399a2dd95SBruce Richardson } 111499a2dd95SBruce Richardson 111599a2dd95SBruce Richardson static void 111699a2dd95SBruce Richardson acl_rule_stats(struct rte_acl_build_rule *head, struct rte_acl_config *config) 111799a2dd95SBruce Richardson { 111899a2dd95SBruce Richardson struct rte_acl_build_rule *rule; 111999a2dd95SBruce Richardson uint32_t n, m, fields_deactivated = 0; 112099a2dd95SBruce Richardson uint32_t start = 0, deactivate = 0; 112199a2dd95SBruce Richardson int tally[RTE_ACL_MAX_LEVELS][TALLY_NUM]; 112299a2dd95SBruce Richardson 112399a2dd95SBruce Richardson memset(tally, 0, sizeof(tally)); 112499a2dd95SBruce Richardson 112599a2dd95SBruce Richardson for (rule = head; rule != NULL; rule = rule->next) { 112699a2dd95SBruce Richardson 112799a2dd95SBruce Richardson for (n = 0; n < config->num_fields; n++) { 112899a2dd95SBruce Richardson uint32_t field_index = config->defs[n].field_index; 112999a2dd95SBruce Richardson 113099a2dd95SBruce Richardson tally[n][TALLY_0]++; 113199a2dd95SBruce Richardson for (m = 1; m < RTE_DIM(wild_limits); m++) { 113299a2dd95SBruce Richardson if (rule->wildness[field_index] >= 113399a2dd95SBruce Richardson wild_limits[m]) 113499a2dd95SBruce Richardson tally[n][m]++; 113599a2dd95SBruce Richardson } 113699a2dd95SBruce Richardson } 113799a2dd95SBruce Richardson 113899a2dd95SBruce Richardson for (n = config->num_fields - 1; n > 0; n--) { 113999a2dd95SBruce Richardson uint32_t field_index = config->defs[n].field_index; 114099a2dd95SBruce Richardson 114199a2dd95SBruce Richardson if (rule->wildness[field_index] == 100) 114299a2dd95SBruce Richardson tally[n][TALLY_DEPTH]++; 114399a2dd95SBruce Richardson else 114499a2dd95SBruce Richardson break; 114599a2dd95SBruce Richardson } 114699a2dd95SBruce Richardson } 114799a2dd95SBruce Richardson 114899a2dd95SBruce Richardson /* 114999a2dd95SBruce Richardson * Look for any field that is always wild and drop it from the config 115099a2dd95SBruce Richardson * Only deactivate if all fields for a given input loop are deactivated. 115199a2dd95SBruce Richardson */ 115299a2dd95SBruce Richardson for (n = 1; n < config->num_fields; n++) { 115399a2dd95SBruce Richardson if (config->defs[n].input_index != 115499a2dd95SBruce Richardson config->defs[n - 1].input_index) { 115599a2dd95SBruce Richardson for (m = start; m < n; m++) 115699a2dd95SBruce Richardson tally[m][TALLY_DEACTIVATED] = deactivate; 115799a2dd95SBruce Richardson fields_deactivated += deactivate; 115899a2dd95SBruce Richardson start = n; 115999a2dd95SBruce Richardson deactivate = 1; 116099a2dd95SBruce Richardson } 116199a2dd95SBruce Richardson 116299a2dd95SBruce Richardson /* if the field is not always completely wild */ 116399a2dd95SBruce Richardson if (tally[n][TALLY_100] != tally[n][TALLY_0]) 116499a2dd95SBruce Richardson deactivate = 0; 116599a2dd95SBruce Richardson } 116699a2dd95SBruce Richardson 116799a2dd95SBruce Richardson for (m = start; m < n; m++) 116899a2dd95SBruce Richardson tally[m][TALLY_DEACTIVATED] = deactivate; 116999a2dd95SBruce Richardson 117099a2dd95SBruce Richardson fields_deactivated += deactivate; 117199a2dd95SBruce Richardson 117299a2dd95SBruce Richardson /* remove deactivated fields */ 117399a2dd95SBruce Richardson if (fields_deactivated) { 117499a2dd95SBruce Richardson uint32_t k, l = 0; 117599a2dd95SBruce Richardson 117699a2dd95SBruce Richardson for (k = 0; k < config->num_fields; k++) { 117799a2dd95SBruce Richardson if (tally[k][TALLY_DEACTIVATED] == 0) { 117899a2dd95SBruce Richardson memmove(&tally[l][0], &tally[k][0], 117999a2dd95SBruce Richardson TALLY_NUM * sizeof(tally[0][0])); 118099a2dd95SBruce Richardson memmove(&config->defs[l++], 118199a2dd95SBruce Richardson &config->defs[k], 118299a2dd95SBruce Richardson sizeof(struct rte_acl_field_def)); 118399a2dd95SBruce Richardson } 118499a2dd95SBruce Richardson } 118599a2dd95SBruce Richardson config->num_fields = l; 118699a2dd95SBruce Richardson } 118799a2dd95SBruce Richardson } 118899a2dd95SBruce Richardson 118999a2dd95SBruce Richardson static int 119099a2dd95SBruce Richardson rule_cmp_wildness(struct rte_acl_build_rule *r1, struct rte_acl_build_rule *r2) 119199a2dd95SBruce Richardson { 119299a2dd95SBruce Richardson uint32_t n; 119399a2dd95SBruce Richardson 119499a2dd95SBruce Richardson for (n = 1; n < r1->config->num_fields; n++) { 119599a2dd95SBruce Richardson int field_index = r1->config->defs[n].field_index; 119699a2dd95SBruce Richardson 119799a2dd95SBruce Richardson if (r1->wildness[field_index] != r2->wildness[field_index]) 119899a2dd95SBruce Richardson return r1->wildness[field_index] - 119999a2dd95SBruce Richardson r2->wildness[field_index]; 120099a2dd95SBruce Richardson } 120199a2dd95SBruce Richardson return 0; 120299a2dd95SBruce Richardson } 120399a2dd95SBruce Richardson 120499a2dd95SBruce Richardson /* 120599a2dd95SBruce Richardson * Split the rte_acl_build_rule list into two lists. 120699a2dd95SBruce Richardson */ 120799a2dd95SBruce Richardson static void 120899a2dd95SBruce Richardson rule_list_split(struct rte_acl_build_rule *source, 120999a2dd95SBruce Richardson struct rte_acl_build_rule **list_a, 121099a2dd95SBruce Richardson struct rte_acl_build_rule **list_b) 121199a2dd95SBruce Richardson { 121299a2dd95SBruce Richardson struct rte_acl_build_rule *fast; 121399a2dd95SBruce Richardson struct rte_acl_build_rule *slow; 121499a2dd95SBruce Richardson 121599a2dd95SBruce Richardson if (source == NULL || source->next == NULL) { 121699a2dd95SBruce Richardson /* length < 2 cases */ 121799a2dd95SBruce Richardson *list_a = source; 121899a2dd95SBruce Richardson *list_b = NULL; 121999a2dd95SBruce Richardson } else { 122099a2dd95SBruce Richardson slow = source; 122199a2dd95SBruce Richardson fast = source->next; 122299a2dd95SBruce Richardson /* Advance 'fast' two nodes, and advance 'slow' one node */ 122399a2dd95SBruce Richardson while (fast != NULL) { 122499a2dd95SBruce Richardson fast = fast->next; 122599a2dd95SBruce Richardson if (fast != NULL) { 122699a2dd95SBruce Richardson slow = slow->next; 122799a2dd95SBruce Richardson fast = fast->next; 122899a2dd95SBruce Richardson } 122999a2dd95SBruce Richardson } 123099a2dd95SBruce Richardson /* 'slow' is before the midpoint in the list, so split it in two 123199a2dd95SBruce Richardson at that point. */ 123299a2dd95SBruce Richardson *list_a = source; 123399a2dd95SBruce Richardson *list_b = slow->next; 123499a2dd95SBruce Richardson slow->next = NULL; 123599a2dd95SBruce Richardson } 123699a2dd95SBruce Richardson } 123799a2dd95SBruce Richardson 123899a2dd95SBruce Richardson /* 123999a2dd95SBruce Richardson * Merge two sorted lists. 124099a2dd95SBruce Richardson */ 124199a2dd95SBruce Richardson static struct rte_acl_build_rule * 124299a2dd95SBruce Richardson rule_list_sorted_merge(struct rte_acl_build_rule *a, 124399a2dd95SBruce Richardson struct rte_acl_build_rule *b) 124499a2dd95SBruce Richardson { 124599a2dd95SBruce Richardson struct rte_acl_build_rule *result = NULL; 124699a2dd95SBruce Richardson struct rte_acl_build_rule **last_next = &result; 124799a2dd95SBruce Richardson 124899a2dd95SBruce Richardson while (1) { 124999a2dd95SBruce Richardson if (a == NULL) { 125099a2dd95SBruce Richardson *last_next = b; 125199a2dd95SBruce Richardson break; 125299a2dd95SBruce Richardson } else if (b == NULL) { 125399a2dd95SBruce Richardson *last_next = a; 125499a2dd95SBruce Richardson break; 125599a2dd95SBruce Richardson } 125699a2dd95SBruce Richardson if (rule_cmp_wildness(a, b) >= 0) { 125799a2dd95SBruce Richardson *last_next = a; 125899a2dd95SBruce Richardson last_next = &a->next; 125999a2dd95SBruce Richardson a = a->next; 126099a2dd95SBruce Richardson } else { 126199a2dd95SBruce Richardson *last_next = b; 126299a2dd95SBruce Richardson last_next = &b->next; 126399a2dd95SBruce Richardson b = b->next; 126499a2dd95SBruce Richardson } 126599a2dd95SBruce Richardson } 126699a2dd95SBruce Richardson return result; 126799a2dd95SBruce Richardson } 126899a2dd95SBruce Richardson 126999a2dd95SBruce Richardson /* 127099a2dd95SBruce Richardson * Sort list of rules based on the rules wildness. 127199a2dd95SBruce Richardson * Use recursive mergesort algorithm. 127299a2dd95SBruce Richardson */ 127399a2dd95SBruce Richardson static struct rte_acl_build_rule * 127499a2dd95SBruce Richardson sort_rules(struct rte_acl_build_rule *head) 127599a2dd95SBruce Richardson { 127699a2dd95SBruce Richardson struct rte_acl_build_rule *a; 127799a2dd95SBruce Richardson struct rte_acl_build_rule *b; 127899a2dd95SBruce Richardson 127999a2dd95SBruce Richardson /* Base case -- length 0 or 1 */ 128099a2dd95SBruce Richardson if (head == NULL || head->next == NULL) 128199a2dd95SBruce Richardson return head; 128299a2dd95SBruce Richardson 128399a2dd95SBruce Richardson /* Split head into 'a' and 'b' sublists */ 128499a2dd95SBruce Richardson rule_list_split(head, &a, &b); 128599a2dd95SBruce Richardson 128699a2dd95SBruce Richardson /* Recursively sort the sublists */ 128799a2dd95SBruce Richardson a = sort_rules(a); 128899a2dd95SBruce Richardson b = sort_rules(b); 128999a2dd95SBruce Richardson 129099a2dd95SBruce Richardson /* answer = merge the two sorted lists together */ 129199a2dd95SBruce Richardson return rule_list_sorted_merge(a, b); 129299a2dd95SBruce Richardson } 129399a2dd95SBruce Richardson 129499a2dd95SBruce Richardson static uint32_t 129599a2dd95SBruce Richardson acl_build_index(const struct rte_acl_config *config, uint32_t *data_index) 129699a2dd95SBruce Richardson { 129799a2dd95SBruce Richardson uint32_t n, m; 129899a2dd95SBruce Richardson int32_t last_header; 129999a2dd95SBruce Richardson 130099a2dd95SBruce Richardson m = 0; 130199a2dd95SBruce Richardson last_header = -1; 130299a2dd95SBruce Richardson 130399a2dd95SBruce Richardson for (n = 0; n < config->num_fields; n++) { 130499a2dd95SBruce Richardson if (last_header != config->defs[n].input_index) { 130599a2dd95SBruce Richardson last_header = config->defs[n].input_index; 130699a2dd95SBruce Richardson data_index[m++] = config->defs[n].offset; 130745109815SKonstantin Ananyev if (config->defs[n].size > sizeof(uint32_t)) 130845109815SKonstantin Ananyev data_index[m++] = config->defs[n].offset + 130945109815SKonstantin Ananyev sizeof(uint32_t); 131099a2dd95SBruce Richardson } 131199a2dd95SBruce Richardson } 131299a2dd95SBruce Richardson 131399a2dd95SBruce Richardson return m; 131499a2dd95SBruce Richardson } 131599a2dd95SBruce Richardson 131699a2dd95SBruce Richardson static struct rte_acl_build_rule * 131799a2dd95SBruce Richardson build_one_trie(struct acl_build_context *context, 131899a2dd95SBruce Richardson struct rte_acl_build_rule *rule_sets[RTE_ACL_MAX_TRIES], 131999a2dd95SBruce Richardson uint32_t n, int32_t node_max) 132099a2dd95SBruce Richardson { 132199a2dd95SBruce Richardson struct rte_acl_build_rule *last; 132299a2dd95SBruce Richardson struct rte_acl_config *config; 132399a2dd95SBruce Richardson 132499a2dd95SBruce Richardson config = rule_sets[n]->config; 132599a2dd95SBruce Richardson 132699a2dd95SBruce Richardson acl_rule_stats(rule_sets[n], config); 132799a2dd95SBruce Richardson rule_sets[n] = sort_rules(rule_sets[n]); 132899a2dd95SBruce Richardson 132999a2dd95SBruce Richardson context->tries[n].type = RTE_ACL_FULL_TRIE; 133099a2dd95SBruce Richardson context->tries[n].count = 0; 133199a2dd95SBruce Richardson 133299a2dd95SBruce Richardson context->tries[n].num_data_indexes = acl_build_index(config, 133399a2dd95SBruce Richardson context->data_indexes[n]); 133499a2dd95SBruce Richardson context->tries[n].data_index = context->data_indexes[n]; 133599a2dd95SBruce Richardson 133699a2dd95SBruce Richardson context->cur_node_max = node_max; 133799a2dd95SBruce Richardson 133899a2dd95SBruce Richardson context->bld_tries[n].trie = build_trie(context, rule_sets[n], 133999a2dd95SBruce Richardson &last, &context->tries[n].count); 134099a2dd95SBruce Richardson 134199a2dd95SBruce Richardson return last; 134299a2dd95SBruce Richardson } 134399a2dd95SBruce Richardson 134499a2dd95SBruce Richardson static int 134599a2dd95SBruce Richardson acl_build_tries(struct acl_build_context *context, 134699a2dd95SBruce Richardson struct rte_acl_build_rule *head) 134799a2dd95SBruce Richardson { 134899a2dd95SBruce Richardson uint32_t n, num_tries; 134999a2dd95SBruce Richardson struct rte_acl_config *config; 135099a2dd95SBruce Richardson struct rte_acl_build_rule *last; 135199a2dd95SBruce Richardson struct rte_acl_build_rule *rule_sets[RTE_ACL_MAX_TRIES]; 135299a2dd95SBruce Richardson 135399a2dd95SBruce Richardson config = head->config; 135499a2dd95SBruce Richardson rule_sets[0] = head; 135599a2dd95SBruce Richardson 135699a2dd95SBruce Richardson /* initialize tries */ 135799a2dd95SBruce Richardson for (n = 0; n < RTE_DIM(context->tries); n++) { 135899a2dd95SBruce Richardson context->tries[n].type = RTE_ACL_UNUSED_TRIE; 135999a2dd95SBruce Richardson context->bld_tries[n].trie = NULL; 136099a2dd95SBruce Richardson context->tries[n].count = 0; 136199a2dd95SBruce Richardson } 136299a2dd95SBruce Richardson 136399a2dd95SBruce Richardson context->tries[0].type = RTE_ACL_FULL_TRIE; 136499a2dd95SBruce Richardson 136599a2dd95SBruce Richardson /* calc wildness of each field of each rule */ 136699a2dd95SBruce Richardson acl_calc_wildness(head, config); 136799a2dd95SBruce Richardson 136899a2dd95SBruce Richardson for (n = 0;; n = num_tries) { 136999a2dd95SBruce Richardson 137099a2dd95SBruce Richardson num_tries = n + 1; 137199a2dd95SBruce Richardson 137299a2dd95SBruce Richardson last = build_one_trie(context, rule_sets, n, context->node_max); 137399a2dd95SBruce Richardson if (context->bld_tries[n].trie == NULL) { 137499a2dd95SBruce Richardson RTE_LOG(ERR, ACL, "Build of %u-th trie failed\n", n); 137599a2dd95SBruce Richardson return -ENOMEM; 137699a2dd95SBruce Richardson } 137799a2dd95SBruce Richardson 137899a2dd95SBruce Richardson /* Build of the last trie completed. */ 137999a2dd95SBruce Richardson if (last == NULL) 138099a2dd95SBruce Richardson break; 138199a2dd95SBruce Richardson 138299a2dd95SBruce Richardson if (num_tries == RTE_DIM(context->tries)) { 138399a2dd95SBruce Richardson RTE_LOG(ERR, ACL, 138499a2dd95SBruce Richardson "Exceeded max number of tries: %u\n", 138599a2dd95SBruce Richardson num_tries); 138699a2dd95SBruce Richardson return -ENOMEM; 138799a2dd95SBruce Richardson } 138899a2dd95SBruce Richardson 138999a2dd95SBruce Richardson /* Trie is getting too big, split remaining rule set. */ 139099a2dd95SBruce Richardson rule_sets[num_tries] = last->next; 139199a2dd95SBruce Richardson last->next = NULL; 139299a2dd95SBruce Richardson acl_free_node(context, context->bld_tries[n].trie); 139399a2dd95SBruce Richardson 139499a2dd95SBruce Richardson /* Create a new copy of config for remaining rules. */ 139599a2dd95SBruce Richardson config = acl_build_alloc(context, 1, sizeof(*config)); 139699a2dd95SBruce Richardson memcpy(config, rule_sets[n]->config, sizeof(*config)); 139799a2dd95SBruce Richardson 139899a2dd95SBruce Richardson /* Make remaining rules use new config. */ 139999a2dd95SBruce Richardson for (head = rule_sets[num_tries]; head != NULL; 140099a2dd95SBruce Richardson head = head->next) 140199a2dd95SBruce Richardson head->config = config; 140299a2dd95SBruce Richardson 140399a2dd95SBruce Richardson /* 140499a2dd95SBruce Richardson * Rebuild the trie for the reduced rule-set. 140599a2dd95SBruce Richardson * Don't try to split it any further. 140699a2dd95SBruce Richardson */ 140799a2dd95SBruce Richardson last = build_one_trie(context, rule_sets, n, INT32_MAX); 140899a2dd95SBruce Richardson if (context->bld_tries[n].trie == NULL || last != NULL) { 140999a2dd95SBruce Richardson RTE_LOG(ERR, ACL, "Build of %u-th trie failed\n", n); 141099a2dd95SBruce Richardson return -ENOMEM; 141199a2dd95SBruce Richardson } 141299a2dd95SBruce Richardson 141399a2dd95SBruce Richardson } 141499a2dd95SBruce Richardson 141599a2dd95SBruce Richardson context->num_tries = num_tries; 141699a2dd95SBruce Richardson return 0; 141799a2dd95SBruce Richardson } 141899a2dd95SBruce Richardson 141999a2dd95SBruce Richardson static void 142099a2dd95SBruce Richardson acl_build_log(const struct acl_build_context *ctx) 142199a2dd95SBruce Richardson { 142299a2dd95SBruce Richardson uint32_t n; 142399a2dd95SBruce Richardson 142499a2dd95SBruce Richardson RTE_LOG(DEBUG, ACL, "Build phase for ACL \"%s\":\n" 142599a2dd95SBruce Richardson "node limit for tree split: %u\n" 142699a2dd95SBruce Richardson "nodes created: %u\n" 142799a2dd95SBruce Richardson "memory consumed: %zu\n", 142899a2dd95SBruce Richardson ctx->acx->name, 142999a2dd95SBruce Richardson ctx->node_max, 143099a2dd95SBruce Richardson ctx->num_nodes, 143199a2dd95SBruce Richardson ctx->pool.alloc); 143299a2dd95SBruce Richardson 143399a2dd95SBruce Richardson for (n = 0; n < RTE_DIM(ctx->tries); n++) { 143499a2dd95SBruce Richardson if (ctx->tries[n].count != 0) 143599a2dd95SBruce Richardson RTE_LOG(DEBUG, ACL, 143699a2dd95SBruce Richardson "trie %u: number of rules: %u, indexes: %u\n", 143799a2dd95SBruce Richardson n, ctx->tries[n].count, 143899a2dd95SBruce Richardson ctx->tries[n].num_data_indexes); 143999a2dd95SBruce Richardson } 144099a2dd95SBruce Richardson } 144199a2dd95SBruce Richardson 144299a2dd95SBruce Richardson static int 144399a2dd95SBruce Richardson acl_build_rules(struct acl_build_context *bcx) 144499a2dd95SBruce Richardson { 144599a2dd95SBruce Richardson struct rte_acl_build_rule *br, *head; 144699a2dd95SBruce Richardson const struct rte_acl_rule *rule; 144799a2dd95SBruce Richardson uint32_t *wp; 144899a2dd95SBruce Richardson uint32_t fn, i, n, num; 144999a2dd95SBruce Richardson size_t ofs, sz; 145099a2dd95SBruce Richardson 145199a2dd95SBruce Richardson fn = bcx->cfg.num_fields; 145299a2dd95SBruce Richardson n = bcx->acx->num_rules; 145399a2dd95SBruce Richardson ofs = n * sizeof(*br); 145499a2dd95SBruce Richardson sz = ofs + n * fn * sizeof(*wp); 145599a2dd95SBruce Richardson 145699a2dd95SBruce Richardson br = tb_alloc(&bcx->pool, sz); 145799a2dd95SBruce Richardson 145899a2dd95SBruce Richardson wp = (uint32_t *)((uintptr_t)br + ofs); 145999a2dd95SBruce Richardson num = 0; 146099a2dd95SBruce Richardson head = NULL; 146199a2dd95SBruce Richardson 146299a2dd95SBruce Richardson for (i = 0; i != n; i++) { 146399a2dd95SBruce Richardson rule = (const struct rte_acl_rule *) 146499a2dd95SBruce Richardson ((uintptr_t)bcx->acx->rules + bcx->acx->rule_sz * i); 146599a2dd95SBruce Richardson if ((rule->data.category_mask & bcx->category_mask) != 0) { 146699a2dd95SBruce Richardson br[num].next = head; 146799a2dd95SBruce Richardson br[num].config = &bcx->cfg; 146899a2dd95SBruce Richardson br[num].f = rule; 146999a2dd95SBruce Richardson br[num].wildness = wp; 147099a2dd95SBruce Richardson wp += fn; 147199a2dd95SBruce Richardson head = br + num; 147299a2dd95SBruce Richardson num++; 147399a2dd95SBruce Richardson } 147499a2dd95SBruce Richardson } 147599a2dd95SBruce Richardson 147699a2dd95SBruce Richardson bcx->num_rules = num; 147799a2dd95SBruce Richardson bcx->build_rules = head; 147899a2dd95SBruce Richardson 147999a2dd95SBruce Richardson return 0; 148099a2dd95SBruce Richardson } 148199a2dd95SBruce Richardson 148299a2dd95SBruce Richardson /* 148399a2dd95SBruce Richardson * Copy data_indexes for each trie into RT location. 148499a2dd95SBruce Richardson */ 148599a2dd95SBruce Richardson static void 148699a2dd95SBruce Richardson acl_set_data_indexes(struct rte_acl_ctx *ctx) 148799a2dd95SBruce Richardson { 148899a2dd95SBruce Richardson uint32_t i, n, ofs; 148999a2dd95SBruce Richardson 149099a2dd95SBruce Richardson ofs = 0; 149199a2dd95SBruce Richardson for (i = 0; i != ctx->num_tries; i++) { 149299a2dd95SBruce Richardson n = ctx->trie[i].num_data_indexes; 149399a2dd95SBruce Richardson memcpy(ctx->data_indexes + ofs, ctx->trie[i].data_index, 149499a2dd95SBruce Richardson n * sizeof(ctx->data_indexes[0])); 149599a2dd95SBruce Richardson ctx->trie[i].data_index = ctx->data_indexes + ofs; 149645109815SKonstantin Ananyev ofs += ACL_MAX_INDEXES; 149799a2dd95SBruce Richardson } 149899a2dd95SBruce Richardson } 149999a2dd95SBruce Richardson 150099a2dd95SBruce Richardson /* 150199a2dd95SBruce Richardson * Internal routine, performs 'build' phase of trie generation: 150299a2dd95SBruce Richardson * - setups build context. 15034a6672c2SStephen Hemminger * - analyzes given set of rules. 150499a2dd95SBruce Richardson * - builds internal tree(s). 150599a2dd95SBruce Richardson */ 150699a2dd95SBruce Richardson static int 150799a2dd95SBruce Richardson acl_bld(struct acl_build_context *bcx, struct rte_acl_ctx *ctx, 150899a2dd95SBruce Richardson const struct rte_acl_config *cfg, uint32_t node_max) 150999a2dd95SBruce Richardson { 151099a2dd95SBruce Richardson int32_t rc; 151199a2dd95SBruce Richardson 151299a2dd95SBruce Richardson /* setup build context. */ 151399a2dd95SBruce Richardson memset(bcx, 0, sizeof(*bcx)); 151499a2dd95SBruce Richardson bcx->acx = ctx; 151599a2dd95SBruce Richardson bcx->pool.alignment = ACL_POOL_ALIGN; 151699a2dd95SBruce Richardson bcx->pool.min_alloc = ACL_POOL_ALLOC_MIN; 151799a2dd95SBruce Richardson bcx->cfg = *cfg; 151899a2dd95SBruce Richardson bcx->category_mask = RTE_LEN2MASK(bcx->cfg.num_categories, 151999a2dd95SBruce Richardson typeof(bcx->category_mask)); 152099a2dd95SBruce Richardson bcx->node_max = node_max; 152199a2dd95SBruce Richardson 152299a2dd95SBruce Richardson rc = sigsetjmp(bcx->pool.fail, 0); 152399a2dd95SBruce Richardson 152499a2dd95SBruce Richardson /* build phase runs out of memory. */ 152599a2dd95SBruce Richardson if (rc != 0) { 152699a2dd95SBruce Richardson RTE_LOG(ERR, ACL, 152799a2dd95SBruce Richardson "ACL context: %s, %s() failed with error code: %d\n", 152899a2dd95SBruce Richardson bcx->acx->name, __func__, rc); 152999a2dd95SBruce Richardson return rc; 153099a2dd95SBruce Richardson } 153199a2dd95SBruce Richardson 153299a2dd95SBruce Richardson /* Create a build rules copy. */ 153399a2dd95SBruce Richardson rc = acl_build_rules(bcx); 153499a2dd95SBruce Richardson if (rc != 0) 153599a2dd95SBruce Richardson return rc; 153699a2dd95SBruce Richardson 153799a2dd95SBruce Richardson /* No rules to build for that context+config */ 153899a2dd95SBruce Richardson if (bcx->build_rules == NULL) { 153999a2dd95SBruce Richardson rc = -EINVAL; 154099a2dd95SBruce Richardson } else { 154199a2dd95SBruce Richardson /* build internal trie representation. */ 154299a2dd95SBruce Richardson rc = acl_build_tries(bcx, bcx->build_rules); 154399a2dd95SBruce Richardson } 154499a2dd95SBruce Richardson return rc; 154599a2dd95SBruce Richardson } 154699a2dd95SBruce Richardson 154799a2dd95SBruce Richardson /* 154899a2dd95SBruce Richardson * Check that parameters for acl_build() are valid. 154999a2dd95SBruce Richardson */ 155099a2dd95SBruce Richardson static int 155199a2dd95SBruce Richardson acl_check_bld_param(struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg) 155299a2dd95SBruce Richardson { 155399a2dd95SBruce Richardson static const size_t field_sizes[] = { 155499a2dd95SBruce Richardson sizeof(uint8_t), sizeof(uint16_t), 155599a2dd95SBruce Richardson sizeof(uint32_t), sizeof(uint64_t), 155699a2dd95SBruce Richardson }; 155799a2dd95SBruce Richardson 155899a2dd95SBruce Richardson uint32_t i, j; 155999a2dd95SBruce Richardson 156099a2dd95SBruce Richardson if (ctx == NULL || cfg == NULL || cfg->num_categories == 0 || 156199a2dd95SBruce Richardson cfg->num_categories > RTE_ACL_MAX_CATEGORIES || 156299a2dd95SBruce Richardson cfg->num_fields == 0 || 156399a2dd95SBruce Richardson cfg->num_fields > RTE_ACL_MAX_FIELDS) 156499a2dd95SBruce Richardson return -EINVAL; 156599a2dd95SBruce Richardson 156699a2dd95SBruce Richardson for (i = 0; i != cfg->num_fields; i++) { 156799a2dd95SBruce Richardson if (cfg->defs[i].type > RTE_ACL_FIELD_TYPE_BITMASK) { 156899a2dd95SBruce Richardson RTE_LOG(ERR, ACL, 156999a2dd95SBruce Richardson "ACL context: %s, invalid type: %hhu for %u-th field\n", 157099a2dd95SBruce Richardson ctx->name, cfg->defs[i].type, i); 157199a2dd95SBruce Richardson return -EINVAL; 157299a2dd95SBruce Richardson } 157399a2dd95SBruce Richardson for (j = 0; 157499a2dd95SBruce Richardson j != RTE_DIM(field_sizes) && 157599a2dd95SBruce Richardson cfg->defs[i].size != field_sizes[j]; 157699a2dd95SBruce Richardson j++) 157799a2dd95SBruce Richardson ; 157899a2dd95SBruce Richardson 157999a2dd95SBruce Richardson if (j == RTE_DIM(field_sizes)) { 158099a2dd95SBruce Richardson RTE_LOG(ERR, ACL, 158199a2dd95SBruce Richardson "ACL context: %s, invalid size: %hhu for %u-th field\n", 158299a2dd95SBruce Richardson ctx->name, cfg->defs[i].size, i); 158399a2dd95SBruce Richardson return -EINVAL; 158499a2dd95SBruce Richardson } 158599a2dd95SBruce Richardson } 158699a2dd95SBruce Richardson 158799a2dd95SBruce Richardson return 0; 158899a2dd95SBruce Richardson } 158999a2dd95SBruce Richardson 159099a2dd95SBruce Richardson /* 159199a2dd95SBruce Richardson * With current ACL implementation first field in the rule definition 159299a2dd95SBruce Richardson * has always to be one byte long. Though for optimising *classify* 159399a2dd95SBruce Richardson * implementation it might be useful to be able to use 4B reads 159499a2dd95SBruce Richardson * (as we do for rest of the fields). 159599a2dd95SBruce Richardson * This function checks input config to determine is it safe to do 4B 159699a2dd95SBruce Richardson * loads for first ACL field. For that we need to make sure that 159799a2dd95SBruce Richardson * first field in our rule definition doesn't have the biggest offset, 159899a2dd95SBruce Richardson * i.e. we still do have other fields located after the first one. 159999a2dd95SBruce Richardson * Contrary if first field has the largest offset, then it means 160099a2dd95SBruce Richardson * first field can occupy the very last byte in the input data buffer, 160199a2dd95SBruce Richardson * and we have to do single byte load for it. 160299a2dd95SBruce Richardson */ 160399a2dd95SBruce Richardson static uint32_t 160499a2dd95SBruce Richardson get_first_load_size(const struct rte_acl_config *cfg) 160599a2dd95SBruce Richardson { 160699a2dd95SBruce Richardson uint32_t i, max_ofs, ofs; 160799a2dd95SBruce Richardson 160899a2dd95SBruce Richardson ofs = 0; 160999a2dd95SBruce Richardson max_ofs = 0; 161099a2dd95SBruce Richardson 161199a2dd95SBruce Richardson for (i = 0; i != cfg->num_fields; i++) { 161299a2dd95SBruce Richardson if (cfg->defs[i].field_index == 0) 161399a2dd95SBruce Richardson ofs = cfg->defs[i].offset; 161499a2dd95SBruce Richardson else if (max_ofs < cfg->defs[i].offset) 161599a2dd95SBruce Richardson max_ofs = cfg->defs[i].offset; 161699a2dd95SBruce Richardson } 161799a2dd95SBruce Richardson 161899a2dd95SBruce Richardson return (ofs < max_ofs) ? sizeof(uint32_t) : sizeof(uint8_t); 161999a2dd95SBruce Richardson } 162099a2dd95SBruce Richardson 162199a2dd95SBruce Richardson int 162299a2dd95SBruce Richardson rte_acl_build(struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg) 162399a2dd95SBruce Richardson { 162499a2dd95SBruce Richardson int32_t rc; 162599a2dd95SBruce Richardson uint32_t n; 162699a2dd95SBruce Richardson size_t max_size; 162799a2dd95SBruce Richardson struct acl_build_context bcx; 162899a2dd95SBruce Richardson 162999a2dd95SBruce Richardson rc = acl_check_bld_param(ctx, cfg); 163099a2dd95SBruce Richardson if (rc != 0) 163199a2dd95SBruce Richardson return rc; 163299a2dd95SBruce Richardson 163399a2dd95SBruce Richardson acl_build_reset(ctx); 163499a2dd95SBruce Richardson 163599a2dd95SBruce Richardson if (cfg->max_size == 0) { 163699a2dd95SBruce Richardson n = NODE_MIN; 163799a2dd95SBruce Richardson max_size = SIZE_MAX; 163899a2dd95SBruce Richardson } else { 163999a2dd95SBruce Richardson n = NODE_MAX; 164099a2dd95SBruce Richardson max_size = cfg->max_size; 164199a2dd95SBruce Richardson } 164299a2dd95SBruce Richardson 164399a2dd95SBruce Richardson for (rc = -ERANGE; n >= NODE_MIN && rc == -ERANGE; n /= 2) { 164499a2dd95SBruce Richardson 164599a2dd95SBruce Richardson /* perform build phase. */ 164699a2dd95SBruce Richardson rc = acl_bld(&bcx, ctx, cfg, n); 164799a2dd95SBruce Richardson 164899a2dd95SBruce Richardson if (rc == 0) { 164999a2dd95SBruce Richardson /* allocate and fill run-time structures. */ 165099a2dd95SBruce Richardson rc = rte_acl_gen(ctx, bcx.tries, bcx.bld_tries, 165199a2dd95SBruce Richardson bcx.num_tries, bcx.cfg.num_categories, 165245109815SKonstantin Ananyev ACL_MAX_INDEXES * RTE_DIM(bcx.tries) * 165399a2dd95SBruce Richardson sizeof(ctx->data_indexes[0]), max_size); 165499a2dd95SBruce Richardson if (rc == 0) { 165599a2dd95SBruce Richardson /* set data indexes. */ 165699a2dd95SBruce Richardson acl_set_data_indexes(ctx); 165799a2dd95SBruce Richardson 165899a2dd95SBruce Richardson /* determine can we always do 4B load */ 165999a2dd95SBruce Richardson ctx->first_load_sz = get_first_load_size(cfg); 166099a2dd95SBruce Richardson 166199a2dd95SBruce Richardson /* copy in build config. */ 166299a2dd95SBruce Richardson ctx->config = *cfg; 166399a2dd95SBruce Richardson } 166499a2dd95SBruce Richardson } 166599a2dd95SBruce Richardson 166699a2dd95SBruce Richardson acl_build_log(&bcx); 166799a2dd95SBruce Richardson 166899a2dd95SBruce Richardson /* cleanup after build. */ 166999a2dd95SBruce Richardson tb_free_pool(&bcx.pool); 167099a2dd95SBruce Richardson } 167199a2dd95SBruce Richardson 167299a2dd95SBruce Richardson return rc; 167399a2dd95SBruce Richardson } 1674