199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause 299a2dd95SBruce Richardson * Copyright(c) 2010-2014 Intel Corporation 399a2dd95SBruce Richardson */ 499a2dd95SBruce Richardson 599a2dd95SBruce Richardson #ifndef _RTE_ACL_H_ 699a2dd95SBruce Richardson #define _RTE_ACL_H_ 799a2dd95SBruce Richardson 899a2dd95SBruce Richardson /** 999a2dd95SBruce Richardson * @file 1099a2dd95SBruce Richardson * 1199a2dd95SBruce Richardson * RTE Classifier. 1299a2dd95SBruce Richardson */ 1399a2dd95SBruce Richardson 1499a2dd95SBruce Richardson #include <rte_acl_osdep.h> 1599a2dd95SBruce Richardson 1699a2dd95SBruce Richardson #ifdef __cplusplus 1799a2dd95SBruce Richardson extern "C" { 1899a2dd95SBruce Richardson #endif 1999a2dd95SBruce Richardson 2099a2dd95SBruce Richardson #define RTE_ACL_MAX_CATEGORIES 16 2199a2dd95SBruce Richardson 2299a2dd95SBruce Richardson #define RTE_ACL_RESULTS_MULTIPLIER (XMM_SIZE / sizeof(uint32_t)) 2399a2dd95SBruce Richardson 2499a2dd95SBruce Richardson #define RTE_ACL_MAX_LEVELS 64 2599a2dd95SBruce Richardson #define RTE_ACL_MAX_FIELDS 64 2699a2dd95SBruce Richardson 2799a2dd95SBruce Richardson union rte_acl_field_types { 2899a2dd95SBruce Richardson uint8_t u8; 2999a2dd95SBruce Richardson uint16_t u16; 3099a2dd95SBruce Richardson uint32_t u32; 3199a2dd95SBruce Richardson uint64_t u64; 3299a2dd95SBruce Richardson }; 3399a2dd95SBruce Richardson 3499a2dd95SBruce Richardson enum { 3599a2dd95SBruce Richardson RTE_ACL_FIELD_TYPE_MASK = 0, 3699a2dd95SBruce Richardson RTE_ACL_FIELD_TYPE_RANGE, 3799a2dd95SBruce Richardson RTE_ACL_FIELD_TYPE_BITMASK 3899a2dd95SBruce Richardson }; 3999a2dd95SBruce Richardson 4099a2dd95SBruce Richardson /** 4199a2dd95SBruce Richardson * ACL Field definition. 4299a2dd95SBruce Richardson * Each field in the ACL rule has an associate definition. 4399a2dd95SBruce Richardson * It defines the type of field, its size, its offset in the input buffer, 4499a2dd95SBruce Richardson * the field index, and the input index. 4599a2dd95SBruce Richardson * For performance reasons, the inner loop of the search function is unrolled 4699a2dd95SBruce Richardson * to process four input bytes at a time. This requires the input to be grouped 4799a2dd95SBruce Richardson * into sets of 4 consecutive bytes. The loop processes the first input byte as 4899a2dd95SBruce Richardson * part of the setup and then subsequent bytes must be in groups of 4 4999a2dd95SBruce Richardson * consecutive bytes. 5099a2dd95SBruce Richardson */ 5199a2dd95SBruce Richardson struct rte_acl_field_def { 5299a2dd95SBruce Richardson uint8_t type; /**< type - RTE_ACL_FIELD_TYPE_*. */ 5399a2dd95SBruce Richardson uint8_t size; /**< size of field 1,2,4, or 8. */ 5499a2dd95SBruce Richardson uint8_t field_index; /**< index of field inside the rule. */ 5599a2dd95SBruce Richardson uint8_t input_index; /**< 0-N input index. */ 5699a2dd95SBruce Richardson uint32_t offset; /**< offset to start of field. */ 5799a2dd95SBruce Richardson }; 5899a2dd95SBruce Richardson 5999a2dd95SBruce Richardson /** 6099a2dd95SBruce Richardson * ACL build configuration. 6199a2dd95SBruce Richardson * Defines the fields of an ACL trie and number of categories to build with. 6299a2dd95SBruce Richardson */ 6399a2dd95SBruce Richardson struct rte_acl_config { 6499a2dd95SBruce Richardson uint32_t num_categories; /**< Number of categories to build with. */ 6599a2dd95SBruce Richardson uint32_t num_fields; /**< Number of field definitions. */ 6699a2dd95SBruce Richardson struct rte_acl_field_def defs[RTE_ACL_MAX_FIELDS]; 6799a2dd95SBruce Richardson /**< array of field definitions. */ 6899a2dd95SBruce Richardson size_t max_size; 6999a2dd95SBruce Richardson /**< max memory limit for internal run-time structures. */ 7099a2dd95SBruce Richardson }; 7199a2dd95SBruce Richardson 7299a2dd95SBruce Richardson /** 7399a2dd95SBruce Richardson * Defines the value of a field for a rule. 7499a2dd95SBruce Richardson */ 7599a2dd95SBruce Richardson struct rte_acl_field { 7699a2dd95SBruce Richardson union rte_acl_field_types value; 7799a2dd95SBruce Richardson /**< a 1,2,4, or 8 byte value of the field. */ 7899a2dd95SBruce Richardson union rte_acl_field_types mask_range; 7999a2dd95SBruce Richardson /**< 8099a2dd95SBruce Richardson * depending on field type: 8199a2dd95SBruce Richardson * mask -> 1.2.3.4/32 value=0x1020304, mask_range=32, 8299a2dd95SBruce Richardson * range -> 0 : 65535 value=0, mask_range=65535, 8399a2dd95SBruce Richardson * bitmask -> 0x06/0xff value=6, mask_range=0xff. 8499a2dd95SBruce Richardson */ 8599a2dd95SBruce Richardson }; 8699a2dd95SBruce Richardson 8799a2dd95SBruce Richardson enum { 8899a2dd95SBruce Richardson RTE_ACL_TYPE_SHIFT = 29, 8999a2dd95SBruce Richardson RTE_ACL_MAX_INDEX = RTE_LEN2MASK(RTE_ACL_TYPE_SHIFT, uint32_t), 9099a2dd95SBruce Richardson RTE_ACL_MAX_PRIORITY = RTE_ACL_MAX_INDEX, 9199a2dd95SBruce Richardson RTE_ACL_MIN_PRIORITY = 1, 9299a2dd95SBruce Richardson }; 9399a2dd95SBruce Richardson 9499a2dd95SBruce Richardson #define RTE_ACL_MASKLEN_TO_BITMASK(v, s) \ 9599a2dd95SBruce Richardson ((v) == 0 ? (v) : (typeof(v))((uint64_t)-1 << ((s) * CHAR_BIT - (v)))) 9699a2dd95SBruce Richardson 9799a2dd95SBruce Richardson /** 9899a2dd95SBruce Richardson * Miscellaneous data for ACL rule. 9999a2dd95SBruce Richardson */ 10099a2dd95SBruce Richardson struct rte_acl_rule_data { 10199a2dd95SBruce Richardson uint32_t category_mask; /**< Mask of categories for that rule. */ 10299a2dd95SBruce Richardson int32_t priority; /**< Priority for that rule. */ 10399a2dd95SBruce Richardson uint32_t userdata; /**< Associated with the rule user data. */ 10499a2dd95SBruce Richardson }; 10599a2dd95SBruce Richardson 10699a2dd95SBruce Richardson /** 10799a2dd95SBruce Richardson * Defines single ACL rule. 10899a2dd95SBruce Richardson * data - miscellaneous data for the rule. 10999a2dd95SBruce Richardson * field[] - value and mask or range for each field. 11099a2dd95SBruce Richardson */ 11199a2dd95SBruce Richardson #define RTE_ACL_RULE_DEF(name, fld_num) struct name {\ 11299a2dd95SBruce Richardson struct rte_acl_rule_data data; \ 11399a2dd95SBruce Richardson struct rte_acl_field field[fld_num]; \ 11499a2dd95SBruce Richardson } 11599a2dd95SBruce Richardson 11699a2dd95SBruce Richardson RTE_ACL_RULE_DEF(rte_acl_rule,); 11799a2dd95SBruce Richardson 11899a2dd95SBruce Richardson #define RTE_ACL_RULE_SZ(fld_num) \ 11999a2dd95SBruce Richardson (sizeof(struct rte_acl_rule) + sizeof(struct rte_acl_field) * (fld_num)) 12099a2dd95SBruce Richardson 12199a2dd95SBruce Richardson 12299a2dd95SBruce Richardson /** Max number of characters in name.*/ 12399a2dd95SBruce Richardson #define RTE_ACL_NAMESIZE 32 12499a2dd95SBruce Richardson 12599a2dd95SBruce Richardson /** 12699a2dd95SBruce Richardson * Parameters used when creating the ACL context. 12799a2dd95SBruce Richardson */ 12899a2dd95SBruce Richardson struct rte_acl_param { 12999a2dd95SBruce Richardson const char *name; /**< Name of the ACL context. */ 13099a2dd95SBruce Richardson int socket_id; /**< Socket ID to allocate memory for. */ 13199a2dd95SBruce Richardson uint32_t rule_size; /**< Size of each rule. */ 13299a2dd95SBruce Richardson uint32_t max_rule_num; /**< Maximum number of rules. */ 13399a2dd95SBruce Richardson }; 13499a2dd95SBruce Richardson 13599a2dd95SBruce Richardson 13699a2dd95SBruce Richardson /** 13799a2dd95SBruce Richardson * Create a new ACL context. 13899a2dd95SBruce Richardson * 13999a2dd95SBruce Richardson * @param param 14099a2dd95SBruce Richardson * Parameters used to create and initialise the ACL context. 14199a2dd95SBruce Richardson * @return 14299a2dd95SBruce Richardson * Pointer to ACL context structure that is used in future ACL 14399a2dd95SBruce Richardson * operations, or NULL on error, with error code set in rte_errno. 14499a2dd95SBruce Richardson * Possible rte_errno errors include: 14599a2dd95SBruce Richardson * - EINVAL - invalid parameter passed to function 14699a2dd95SBruce Richardson */ 14799a2dd95SBruce Richardson struct rte_acl_ctx * 14899a2dd95SBruce Richardson rte_acl_create(const struct rte_acl_param *param); 14999a2dd95SBruce Richardson 15099a2dd95SBruce Richardson /** 15199a2dd95SBruce Richardson * Find an existing ACL context object and return a pointer to it. 15299a2dd95SBruce Richardson * 15399a2dd95SBruce Richardson * @param name 15499a2dd95SBruce Richardson * Name of the ACL context as passed to rte_acl_create() 15599a2dd95SBruce Richardson * @return 15699a2dd95SBruce Richardson * Pointer to ACL context or NULL if object not found 15799a2dd95SBruce Richardson * with rte_errno set appropriately. Possible rte_errno values include: 15899a2dd95SBruce Richardson * - ENOENT - value not available for return 15999a2dd95SBruce Richardson */ 16099a2dd95SBruce Richardson struct rte_acl_ctx * 16199a2dd95SBruce Richardson rte_acl_find_existing(const char *name); 16299a2dd95SBruce Richardson 16399a2dd95SBruce Richardson /** 16499a2dd95SBruce Richardson * De-allocate all memory used by ACL context. 16599a2dd95SBruce Richardson * 16699a2dd95SBruce Richardson * @param ctx 16799a2dd95SBruce Richardson * ACL context to free 168*448e01f1SStephen Hemminger * If ctx is NULL, no operation is performed. 16999a2dd95SBruce Richardson */ 17099a2dd95SBruce Richardson void 17199a2dd95SBruce Richardson rte_acl_free(struct rte_acl_ctx *ctx); 17299a2dd95SBruce Richardson 17399a2dd95SBruce Richardson /** 17499a2dd95SBruce Richardson * Add rules to an existing ACL context. 17599a2dd95SBruce Richardson * This function is not multi-thread safe. 17699a2dd95SBruce Richardson * 17799a2dd95SBruce Richardson * @param ctx 17899a2dd95SBruce Richardson * ACL context to add patterns to. 17999a2dd95SBruce Richardson * @param rules 18099a2dd95SBruce Richardson * Array of rules to add to the ACL context. 18199a2dd95SBruce Richardson * Note that all fields in rte_acl_rule structures are expected 18299a2dd95SBruce Richardson * to be in host byte order. 18399a2dd95SBruce Richardson * Each rule expected to be in the same format and not exceed size 18499a2dd95SBruce Richardson * specified at ACL context creation time. 18599a2dd95SBruce Richardson * @param num 18699a2dd95SBruce Richardson * Number of elements in the input array of rules. 18799a2dd95SBruce Richardson * @return 18899a2dd95SBruce Richardson * - -ENOMEM if there is no space in the ACL context for these rules. 18999a2dd95SBruce Richardson * - -EINVAL if the parameters are invalid. 19099a2dd95SBruce Richardson * - Zero if operation completed successfully. 19199a2dd95SBruce Richardson */ 19299a2dd95SBruce Richardson int 19399a2dd95SBruce Richardson rte_acl_add_rules(struct rte_acl_ctx *ctx, const struct rte_acl_rule *rules, 19499a2dd95SBruce Richardson uint32_t num); 19599a2dd95SBruce Richardson 19699a2dd95SBruce Richardson /** 19799a2dd95SBruce Richardson * Delete all rules from the ACL context. 19899a2dd95SBruce Richardson * This function is not multi-thread safe. 19999a2dd95SBruce Richardson * Note that internal run-time structures are not affected. 20099a2dd95SBruce Richardson * 20199a2dd95SBruce Richardson * @param ctx 20299a2dd95SBruce Richardson * ACL context to delete rules from. 20399a2dd95SBruce Richardson */ 20499a2dd95SBruce Richardson void 20599a2dd95SBruce Richardson rte_acl_reset_rules(struct rte_acl_ctx *ctx); 20699a2dd95SBruce Richardson 20799a2dd95SBruce Richardson /** 20899a2dd95SBruce Richardson * Analyze set of rules and build required internal run-time structures. 20999a2dd95SBruce Richardson * This function is not multi-thread safe. 21099a2dd95SBruce Richardson * 21199a2dd95SBruce Richardson * @param ctx 21299a2dd95SBruce Richardson * ACL context to build. 21399a2dd95SBruce Richardson * @param cfg 21499a2dd95SBruce Richardson * Pointer to struct rte_acl_config - defines build parameters. 21599a2dd95SBruce Richardson * @return 21699a2dd95SBruce Richardson * - -ENOMEM if couldn't allocate enough memory. 21799a2dd95SBruce Richardson * - -EINVAL if the parameters are invalid. 21899a2dd95SBruce Richardson * - Negative error code if operation failed. 21999a2dd95SBruce Richardson * - Zero if operation completed successfully. 22099a2dd95SBruce Richardson */ 22199a2dd95SBruce Richardson int 22299a2dd95SBruce Richardson rte_acl_build(struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg); 22399a2dd95SBruce Richardson 22499a2dd95SBruce Richardson /** 22599a2dd95SBruce Richardson * Delete all rules from the ACL context and 22699a2dd95SBruce Richardson * destroy all internal run-time structures. 22799a2dd95SBruce Richardson * This function is not multi-thread safe. 22899a2dd95SBruce Richardson * 22999a2dd95SBruce Richardson * @param ctx 23099a2dd95SBruce Richardson * ACL context to reset. 23199a2dd95SBruce Richardson */ 23299a2dd95SBruce Richardson void 23399a2dd95SBruce Richardson rte_acl_reset(struct rte_acl_ctx *ctx); 23499a2dd95SBruce Richardson 23599a2dd95SBruce Richardson /** 23699a2dd95SBruce Richardson * Available implementations of ACL classify. 23799a2dd95SBruce Richardson */ 23899a2dd95SBruce Richardson enum rte_acl_classify_alg { 23999a2dd95SBruce Richardson RTE_ACL_CLASSIFY_DEFAULT = 0, 24099a2dd95SBruce Richardson RTE_ACL_CLASSIFY_SCALAR = 1, /**< generic implementation. */ 24199a2dd95SBruce Richardson RTE_ACL_CLASSIFY_SSE = 2, /**< requires SSE4.1 support. */ 24299a2dd95SBruce Richardson RTE_ACL_CLASSIFY_AVX2 = 3, /**< requires AVX2 support. */ 24399a2dd95SBruce Richardson RTE_ACL_CLASSIFY_NEON = 4, /**< requires NEON support. */ 24499a2dd95SBruce Richardson RTE_ACL_CLASSIFY_ALTIVEC = 5, /**< requires ALTIVEC support. */ 24599a2dd95SBruce Richardson RTE_ACL_CLASSIFY_AVX512X16 = 6, /**< requires AVX512 support. */ 24699a2dd95SBruce Richardson RTE_ACL_CLASSIFY_AVX512X32 = 7, /**< requires AVX512 support. */ 24799a2dd95SBruce Richardson }; 24899a2dd95SBruce Richardson 24999a2dd95SBruce Richardson /** 25099a2dd95SBruce Richardson * Perform search for a matching ACL rule for each input data buffer. 25199a2dd95SBruce Richardson * Each input data buffer can have up to *categories* matches. 25299a2dd95SBruce Richardson * That implies that results array should be big enough to hold 25399a2dd95SBruce Richardson * (categories * num) elements. 25499a2dd95SBruce Richardson * Also categories parameter should be either one or multiple of 25599a2dd95SBruce Richardson * RTE_ACL_RESULTS_MULTIPLIER and can't be bigger than RTE_ACL_MAX_CATEGORIES. 25699a2dd95SBruce Richardson * If more than one rule is applicable for given input buffer and 25799a2dd95SBruce Richardson * given category, then rule with highest priority will be returned as a match. 25899a2dd95SBruce Richardson * Note, that it is a caller's responsibility to ensure that input parameters 25999a2dd95SBruce Richardson * are valid and point to correct memory locations. 26099a2dd95SBruce Richardson * 26199a2dd95SBruce Richardson * @param ctx 26299a2dd95SBruce Richardson * ACL context to search with. 26399a2dd95SBruce Richardson * @param data 26499a2dd95SBruce Richardson * Array of pointers to input data buffers to perform search. 26599a2dd95SBruce Richardson * Note that all fields in input data buffers supposed to be in network 26699a2dd95SBruce Richardson * byte order (MSB). 26799a2dd95SBruce Richardson * @param results 26899a2dd95SBruce Richardson * Array of search results, *categories* results per each input data buffer. 26999a2dd95SBruce Richardson * @param num 27099a2dd95SBruce Richardson * Number of elements in the input data buffers array. 27199a2dd95SBruce Richardson * @param categories 27299a2dd95SBruce Richardson * Number of maximum possible matches for each input buffer, one possible 27399a2dd95SBruce Richardson * match per category. 27499a2dd95SBruce Richardson * @return 27599a2dd95SBruce Richardson * zero on successful completion. 27699a2dd95SBruce Richardson * -EINVAL for incorrect arguments. 27799a2dd95SBruce Richardson */ 27899a2dd95SBruce Richardson extern int 27999a2dd95SBruce Richardson rte_acl_classify(const struct rte_acl_ctx *ctx, 28099a2dd95SBruce Richardson const uint8_t **data, 28199a2dd95SBruce Richardson uint32_t *results, uint32_t num, 28299a2dd95SBruce Richardson uint32_t categories); 28399a2dd95SBruce Richardson 28499a2dd95SBruce Richardson /** 28599a2dd95SBruce Richardson * Perform search using specified algorithm for a matching ACL rule for 28699a2dd95SBruce Richardson * each input data buffer. 28799a2dd95SBruce Richardson * Each input data buffer can have up to *categories* matches. 28899a2dd95SBruce Richardson * That implies that results array should be big enough to hold 28999a2dd95SBruce Richardson * (categories * num) elements. 29099a2dd95SBruce Richardson * Also categories parameter should be either one or multiple of 29199a2dd95SBruce Richardson * RTE_ACL_RESULTS_MULTIPLIER and can't be bigger than RTE_ACL_MAX_CATEGORIES. 29299a2dd95SBruce Richardson * If more than one rule is applicable for given input buffer and 29399a2dd95SBruce Richardson * given category, then rule with highest priority will be returned as a match. 29499a2dd95SBruce Richardson * Note, that it is a caller's responsibility to ensure that input parameters 29599a2dd95SBruce Richardson * are valid and point to correct memory locations. 29699a2dd95SBruce Richardson * 29799a2dd95SBruce Richardson * @param ctx 29899a2dd95SBruce Richardson * ACL context to search with. 29999a2dd95SBruce Richardson * @param data 30099a2dd95SBruce Richardson * Array of pointers to input data buffers to perform search. 30199a2dd95SBruce Richardson * Note that all fields in input data buffers supposed to be in network 30299a2dd95SBruce Richardson * byte order (MSB). 30399a2dd95SBruce Richardson * @param results 30499a2dd95SBruce Richardson * Array of search results, *categories* results per each input data buffer. 30599a2dd95SBruce Richardson * @param num 30699a2dd95SBruce Richardson * Number of elements in the input data buffers array. 30799a2dd95SBruce Richardson * @param categories 30899a2dd95SBruce Richardson * Number of maximum possible matches for each input buffer, one possible 30999a2dd95SBruce Richardson * match per category. 31099a2dd95SBruce Richardson * @param alg 31199a2dd95SBruce Richardson * Algorithm to be used for the search. 31299a2dd95SBruce Richardson * It is the caller responsibility to ensure that the value refers to the 31399a2dd95SBruce Richardson * existing algorithm, and that it could be run on the given CPU. 31499a2dd95SBruce Richardson * @return 31599a2dd95SBruce Richardson * zero on successful completion. 31699a2dd95SBruce Richardson * -EINVAL for incorrect arguments. 31799a2dd95SBruce Richardson */ 31899a2dd95SBruce Richardson extern int 31999a2dd95SBruce Richardson rte_acl_classify_alg(const struct rte_acl_ctx *ctx, 32099a2dd95SBruce Richardson const uint8_t **data, 32199a2dd95SBruce Richardson uint32_t *results, uint32_t num, 32299a2dd95SBruce Richardson uint32_t categories, 32399a2dd95SBruce Richardson enum rte_acl_classify_alg alg); 32499a2dd95SBruce Richardson 32599a2dd95SBruce Richardson /* 32699a2dd95SBruce Richardson * Override the default classifier function for a given ACL context. 32799a2dd95SBruce Richardson * @param ctx 32899a2dd95SBruce Richardson * ACL context to change classify function for. 32999a2dd95SBruce Richardson * @param alg 33099a2dd95SBruce Richardson * New default classify algorithm for given ACL context. 33199a2dd95SBruce Richardson * It is the caller responsibility to ensure that the value refers to the 33299a2dd95SBruce Richardson * existing algorithm, and that it could be run on the given CPU. 33399a2dd95SBruce Richardson * The max SIMD bitwidth value in EAL must also allow for the chosen algorithm. 33499a2dd95SBruce Richardson * @return 33599a2dd95SBruce Richardson * - -EINVAL if the parameters are invalid. 33699a2dd95SBruce Richardson * - -ENOTSUP requested algorithm is not supported by given platform. 33799a2dd95SBruce Richardson * - Zero if operation completed successfully. 33899a2dd95SBruce Richardson */ 33999a2dd95SBruce Richardson extern int 34099a2dd95SBruce Richardson rte_acl_set_ctx_classify(struct rte_acl_ctx *ctx, 34199a2dd95SBruce Richardson enum rte_acl_classify_alg alg); 34299a2dd95SBruce Richardson 34399a2dd95SBruce Richardson /** 34499a2dd95SBruce Richardson * Dump an ACL context structure to the console. 34599a2dd95SBruce Richardson * 34699a2dd95SBruce Richardson * @param ctx 34799a2dd95SBruce Richardson * ACL context to dump. 34899a2dd95SBruce Richardson */ 34999a2dd95SBruce Richardson void 35099a2dd95SBruce Richardson rte_acl_dump(const struct rte_acl_ctx *ctx); 35199a2dd95SBruce Richardson 35299a2dd95SBruce Richardson /** 35399a2dd95SBruce Richardson * Dump all ACL context structures to the console. 35499a2dd95SBruce Richardson */ 35599a2dd95SBruce Richardson void 35699a2dd95SBruce Richardson rte_acl_list_dump(void); 35799a2dd95SBruce Richardson 35899a2dd95SBruce Richardson #ifdef __cplusplus 35999a2dd95SBruce Richardson } 36099a2dd95SBruce Richardson #endif 36199a2dd95SBruce Richardson 36299a2dd95SBruce Richardson #endif /* _RTE_ACL_H_ */ 363