rte_acl_build memory fragmentation concern and proposal for external memory support
Problem Statement
Hello DPDK community,
We've been using DPDK's ACL library in a long-running network application and observed a concerning issue regarding memory fragmentation. The rte_acl_build()function internally uses rte_zmalloc_socket()for dynamic memory allocation during ACL rule compilation.
While this design works well for short-lived processes, it creates challenges for long-running applications (days/weeks) where memory fragmentation becomes a critical concern. Each ACL rebuild (due to rule updates, configuration changes, etc.) allocates variable-sized memory blocks for the trie structures and transition tables, which are then freed when the ACL context is destroyed or rebuilt.
This pattern leads to:
1. Memory fragmentation over time, as the heap accumulates "holes" of varying sizes 2. Potential allocation failures even when total free memory appears sufficient 3. Difficulty in memory budgeting for deterministic systems
Current Limitation
The current API doesn't allow applications to provide pre-allocated memory for ACL construction, forcing dynamic allocations that contribute to heap fragmentation. This is particularly problematic for applications that prioritize long-term stability and predictable memory usage.
Proposed Solution
We propose extending the ACL API to support external memory buffers for the build process. This would allow applications to manage memory allocation strategies according to their specific requirements (pool-based, static allocation, etc.).
发自我的企业微信
Hi,
I suppose you mean some sort of user-provided alloc()/free() callbacks that will be used by ACL lib at generate phase?
And I presume if none provided, by default it will fall-back to rte_malloc/rte_free? If so, then yes, sounds reasonable.
Patches are welcomed :)
Konstantin
From: mannywang(王永峰) <[email protected]>
Sent: Friday, November 14, 2025 2:52 AM
To: dev <[email protected]>
Cc: konstantin.v.ananyev <[email protected]>
Subject: [RFC] rte_acl_build memory fragmentation concern and proposal for external memory support
Problem Statement
Hello DPDK community,
We've been using DPDK's ACL library in a long-running network application and observed a concerning issue regarding memory fragmentation. The rte_acl_build()function internally uses rte_zmalloc_socket()for dynamic memory allocation during ACL rule compilation.
While this design works well for short-lived processes, it creates challenges for long-running applications (days/weeks) where memory fragmentation becomes a critical concern. Each ACL rebuild (due to rule updates, configuration changes, etc.) allocates variable-sized memory blocks for the trie structures and transition tables, which are then freed when the ACL context is destroyed or rebuilt.
This pattern leads to:
1. Memory fragmentation over time, as the heap accumulates "holes" of varying sizes
2. Potential allocation failures even when total free memory appears sufficient
3. Difficulty in memory budgeting for deterministic systems
Current Limitation
The current API doesn't allow applications to provide pre-allocated memory for ACL construction, forcing dynamic allocations that contribute to heap fragmentation. This is particularly problematic for applications that prioritize long-term stability and predictable memory usage.
Proposed Solution
We propose extending the ACL API to support external memory buffers for the build process. This would allow applications to manage memory allocation strategies according to their specific requirements (pool-based, static allocation, etc.).
________________________________
发自我的企业微信<https://work.weixin.qq.com/wework_admin/user/h5/qqmail_user_card/vc879305fe5b6b5af3?from=myprofile>
PATCH 1/1 acl: support custom memory allocator
Reduce memory fragmentation caused by dynamic memory allocations by allowing users to provide custom memory allocator.
Add new members to struct rte_acl_config to allow passing custom allocator callbacks to rte_acl_build:
- running_alloc: allocator callback for run-time internal memory
- running_free: free callback for run-time internal memory
- running_ctx: user-defined context passed to running_alloc/free
- temp_alloc: allocator callback for temporary memory during ACL build
- temp_reset: reset callback for temporary allocator
- temp_ctx: user-defined context passed to temp_alloc/reset
These callbacks allow users to provide their own memory pools or allocators for both persistent runtime structures and temporary build-time data.
A typical approach is to pre-allocate a static memory region for rte_acl_ctx, and to provide a global temporary memory manager that supports multipleallocations and a single reset during ACL build.
Since tb_mem_pool handles allocation failures using siglongjmp, temp_alloc follows the same approach for failure handling.
--- app/test/test_acl.c | 181 +++++++++++++++++++++++++++++++++++++++++++- lib/acl/acl.h | 3 +- lib/acl/acl_bld.c | 14 +++- lib/acl/acl_gen.c | 8 +- lib/acl/rte_acl.c | 5 +- lib/acl/rte_acl.h | 20 +++++ lib/acl/tb_mem.c | 8 ++ lib/acl/tb_mem.h | 6 ++ 8 files changed, 236 insertions(+), 9 deletions(-)
app/test/test_acl.c +180 -1
@@ -1721,6 +1721,184 @@ test_u32_range(void)return rc; } +struct acl_ctx_wrapper_t { +struct rte_acl_ctx *ctx; +void *running_buf; +bool running_buf_using; +}; + +struct acl_temp_mem_mgr_t { +void *buf; +uint32_t buf_used; +sigjmp_buf fail; +}; + +struct acl_ctx_wrapper_t g_acl_ctx_wrapper; +struct acl_temp_mem_mgr_t g_temp_mem_mgr; + +#define ACL_RUNNING_BUF_SIZE (10 * 1024 * 1024) +#define ACL_TEMP_BUF_SIZE (10 * 1024 * 1024) + +static void *running_alloc (size_t size,unsigned int align,void *cb_data) +{ + (void )align; +struct acl_ctx_wrapper_t *gwlb_acl_ctx = (struct acl_ctx_wrapper_t *)cb_data; +if (gwlb_acl_ctx->running_buf_using) +return NULL ; +printf ("running memory alloc for acl context, size=%" PRId64", pointer=%p\n" , + size, + gwlb_acl_ctx->running_buf); + gwlb_acl_ctx->running_buf_using = true; +return gwlb_acl_ctx->running_buf; +} + +static void running_free (void *buf,void *cb_data) +{ +if (!buf) +return ; +struct acl_ctx_wrapper_t *gwlb_acl_ctx = (struct acl_ctx_wrapper_t *)cb_data; +printf ("running memory free pointer=%p\n" , buf); + gwlb_acl_ctx->running_buf_using = false; +} + +static void *temp_alloc (size_t size,sigjmp_buf fail,void *cb_data) +{ +struct acl_temp_mem_mgr_t *gwlb_acl_build = (struct acl_temp_mem_mgr_t *)cb_data; +if (ACL_TEMP_BUF_SIZE - gwlb_acl_build->buf_used < size) { +printf ("Line %i: alloc temp memory fail, size=%" PRId64", used=%d\n" , + __LINE__, + size, + gwlb_acl_build->buf_used); +siglongjmp (fail, -ENOMEM ); +return NULL ; + } +void *ret = (char *)gwlb_acl_build->buf + gwlb_acl_build->buf_used; + gwlb_acl_build->buf_used += size; +return ret; +} + +static void temp_reset (void *cb_data) +{ +struct acl_temp_mem_mgr_t *gwlb_acl_build = (struct acl_temp_mem_mgr_t *)cb_data; +memset (gwlb_acl_build->buf,0 ,ACL_TEMP_BUF_SIZE ); +printf ("temp memory reset, used total=%d\n" , gwlb_acl_build->buf_used); + gwlb_acl_build->buf_used =0 ; +} + +static int +rte_acl_ipv4vlan_build_wich_mem_cb (struct rte_acl_ctx *ctx, +const uint32_t layout[RTE_ACL_IPV4VLAN_NUM ], +uint32_t num_categories) +{ +struct rte_acl_config cfg; + +if (ctx ==NULL || layout ==NULL ) +return -EINVAL ; + +memset (&cfg,0 ,sizeof (cfg)); +acl_ipv4vlan_config (&cfg, layout, num_categories); + cfg.running_alloc = running_alloc; + cfg.running_free = running_free; + cfg.running_cb_ctx = &g_acl_ctx_wrapper; + cfg.temp_alloc = temp_alloc; + cfg.temp_reset = temp_reset; + cfg.temp_cb_ctx = &g_temp_mem_mgr; +return rte_acl_build (ctx, &cfg); +} + +static int +test_classify_buid_wich_mem_cb (struct rte_acl_ctx *acx, +const struct rte_acl_ipv4vlan_rule *rules,uint32_t num) +{ +int ret; + +/* add rules to the context */ + ret =rte_acl_ipv4vlan_add_rules (acx, rules, num); +if (ret !=0 ) { +printf ("Line %i: Adding rules to ACL context failed!\n" , + __LINE__); +return ret; + } + +/* try building the context */ + ret =rte_acl_ipv4vlan_build_wich_mem_cb (acx, ipv4_7tuple_layout, +RTE_ACL_MAX_CATEGORIES ); +if (ret !=0 ) { +printf ("Line %i: Building ACL context failed!\n" , __LINE__); +return ret; + } + +return 0 ; +} + +static int +test_mem_cb (void ) +{ +int i, ret; + g_acl_ctx_wrapper.ctx =rte_acl_create (&acl_param); +if (g_acl_ctx_wrapper.ctx ==NULL ) { +printf ("Line %i: Error creating ACL context!\n" , __LINE__); +return -1 ; + } + g_acl_ctx_wrapper.running_buf =rte_zmalloc_socket ( +"test_acl" , +ACL_RUNNING_BUF_SIZE , +RTE_CACHE_LINE_SIZE , +SOCKET_ID_ANY ); +if (!g_acl_ctx_wrapper.running_buf) { +printf ("Line %i: Error allocing running buf for acl context!\n" , __LINE__); +return 1 ; + } + g_acl_ctx_wrapper.running_buf_using = false; + + g_temp_mem_mgr.buf =malloc (ACL_TEMP_BUF_SIZE ); +if (!g_temp_mem_mgr.buf) +printf ("Line %i: Error allocing teem buf for acl build!\n" , __LINE__); +memset (g_temp_mem_mgr.buf,0 ,ACL_TEMP_BUF_SIZE ); + g_temp_mem_mgr.buf_used =0 ; + + ret =0 ; +for (i =0 ; i !=TEST_CLASSIFY_ITER ; i++) { + +if ((i &1 ) ==0 ) +rte_acl_reset (g_acl_ctx_wrapper.ctx); +else +rte_acl_reset_rules (g_acl_ctx_wrapper.ctx); + + ret =test_classify_buid_wich_mem_cb (g_acl_ctx_wrapper.ctx, acl_test_rules, +RTE_DIM (acl_test_rules)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: " +"Adding rules to ACL context failed!\n" , + __LINE__, i); +break ; + } + + ret =test_classify_run (g_acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + +/* reset rules and make sure that classify still works ok. */ +rte_acl_reset_rules (g_acl_ctx_wrapper.ctx); + ret =test_classify_run (g_acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + } + +rte_acl_free (g_acl_ctx_wrapper.ctx); +free (g_temp_mem_mgr.buf); +rte_free (g_acl_ctx_wrapper.running_buf); +return ret; +} +static int test_acl (void ) {
@@ -1742,7 +1920,8 @@ test_acl(void)return -1 ;if (test_u32_range () <0 )return -1 ; - +if (test_mem_cb () <0 ) +return -1 ;return 0 ; }
lib/acl/acl.h +2 -1
@@ -189,7 +189,8 @@ struct rte_acl_ctx {int rte_acl_gen (struct rte_acl_ctx *ctx,struct rte_acl_trie *trie,struct rte_acl_bld_trie *node_bld_trie,uint32_t num_tries, -uint32_t num_categories,uint32_t data_index_sz,size_t max_size); +uint32_t num_categories,uint32_t data_index_sz,size_t max_size, +const struct rte_acl_config *cfg);typedef int (*rte_acl_classify_t ) (const struct rte_acl_ctx *,const uint8_t **,uint32_t *,uint32_t ,uint32_t );
lib/acl/acl_bld.c +10 -4
@@ -777,9 +777,12 @@ acl_merge_trie(struct acl_build_context *context,* - reset all RT related fields to zero. */ static void -acl_build_reset (struct rte_acl_ctx *ctx) +acl_build_reset (struct rte_acl_ctx *ctx,const struct rte_acl_config *cfg) { -rte_free (ctx->mem); +if (cfg->running_free) + cfg->running_free (ctx->mem, cfg->running_cb_ctx); +else +rte_free (ctx->mem);memset (&ctx->num_categories,0 ,sizeof (*ctx) - offsetof(struct rte_acl_ctx , num_categories)); }
@@ -1518,6 +1521,9 @@ acl_bld(struct acl_build_context *bcx, struct rte_acl_ctx *ctx, bcx->acx = ctx; bcx->pool.alignment =ACL_POOL_ALIGN ; bcx->pool.min_alloc =ACL_POOL_ALLOC_MIN ; + bcx->pool.alloc_cb = cfg->temp_alloc; + bcx->pool.reset_cb = cfg->temp_reset; + bcx->pool.cb_ctx = cfg->temp_cb_ctx; bcx->cfg = *cfg; bcx->category_mask =RTE_LEN2MASK (bcx->cfg.num_categories,typeof (bcx->category_mask));
@@ -1635,7 +1641,7 @@ rte_acl_build(struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg)if (rc !=0 )return rc; -acl_build_reset (ctx); +acl_build_reset (ctx, cfg);if (cfg->max_size ==0 ) { n =NODE_MIN ;
@@ -1655,7 +1661,7 @@ rte_acl_build(struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg) rc =rte_acl_gen (ctx, bcx.tries, bcx.bld_tries, bcx.num_tries, bcx.cfg.num_categories,ACL_MAX_INDEXES *RTE_DIM (bcx.tries) * -sizeof (ctx->data_indexes[0 ]), max_size); +sizeof (ctx->data_indexes[0 ]), max_size, cfg);if (rc ==0 ) {/* set data indexes. */ acl_set_data_indexes (ctx);
lib/acl/acl_gen.c +6 -2
@@ -448,7 +448,8 @@ acl_calc_counts_indices(struct acl_node_counters *counts,int rte_acl_gen (struct rte_acl_ctx *ctx,struct rte_acl_trie *trie,struct rte_acl_bld_trie *node_bld_trie,uint32_t num_tries, -uint32_t num_categories,uint32_t data_index_sz,size_t max_size) +uint32_t num_categories,uint32_t data_index_sz,size_t max_size, +const struct rte_acl_config *cfg) {void *mem;size_t total_size;
@@ -478,7 +479,10 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie,return -ERANGE ; } - mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , +if (cfg->running_alloc) + mem = cfg->running_alloc (total_size,RTE_CACHE_LINE_SIZE , cfg->running_cb_ctx); +else + mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , ctx->socket_id);if (mem ==NULL ) { ACL_LOG(ERR,
lib/acl/rte_acl.c +4 -1
@@ -362,7 +362,10 @@ rte_acl_free(struct rte_acl_ctx *ctx)rte_mcfg_tailq_write_unlock (); -rte_free (ctx->mem); +if (ctx->config.running_free) + ctx->config.running_free (ctx->mem, ctx->config.running_cb_ctx); +else +rte_free (ctx->mem);rte_free (ctx);rte_free (te); }
lib/acl/rte_acl.h +20 -0
@@ -13,6 +13,7 @@#include <rte_common.h> #include <rte_acl_osdep.h> +#include <setjmp.h> #ifdef __cplusplusextern "C" {
@@ -61,6 +62,11 @@ struct rte_acl_field_def {* ACL build configuration. * Defines the fields of an ACL trie and number of categories to build with. */ +typedef void *(*rte_acl_running_alloc_t )(size_t ,unsigned int ,void *); +typedef void (*rte_acl_running_free_t )(void *,void *); +typedef void *(*rte_acl_temp_alloc_t )(size_t ,sigjmp_buf ,void *); +typedef void (*rte_acl_temp_reset_t )(void *); +struct rte_acl_config {uint32_t num_categories;/**< Number of categories to build with. */ uint32_t num_fields;/**< Number of field definitions. */
@@ -68,6 +74,20 @@ struct rte_acl_config {/**< array of field definitions. */ size_t max_size;/**< max memory limit for internal run-time structures. */ + +/**< Allocator callback for run-time internal memory. */ +rte_acl_running_alloc_t running_alloc; +/**< Free callback for run-time internal memory. */ +rte_acl_running_free_t running_free; +/**< User context passed to running_alloc/free. */ +void *running_cb_ctx; + +/**< Allocator callback for temporary memory used during build. */ +rte_acl_temp_alloc_t temp_alloc; +/**< Reset callback for temporary allocator. */ +rte_acl_temp_reset_t temp_reset; +/**< User context passed to temp_alloc/reset. */ +void *temp_cb_ctx; };/**
lib/acl/tb_mem.c +8 -0
@@ -55,6 +55,9 @@ tb_alloc(struct tb_mem_pool *pool, size_t size) size =RTE_ALIGN_CEIL (size, pool->alignment); +if (pool->alloc_cb) +return pool->alloc_cb (size, pool->fail, pool->cb_ctx); + block = pool->block;if (block ==NULL || block->size < size) { new_sz = (size > pool->min_alloc) ? size : pool->min_alloc;
@@ -71,6 +74,11 @@ tb_free_pool(struct tb_mem_pool *pool) {struct tb_mem_block *next, *block; +if (pool->reset_cb) { + pool->reset_cb (pool->cb_ctx); +return ; + } +for (block = pool->block; block !=NULL ; block = next) { next = block->next;free (block);
lib/acl/tb_mem.h +6 -0
@@ -24,11 +24,17 @@ struct tb_mem_block {uint8_t *mem; }; +typedef void *(*rte_tb_alloc_t )(size_t ,sigjmp_buf ,void *); +typedef void (*rte_tb_reset_t )(void *); +struct tb_mem_pool {struct tb_mem_block *block;size_t alignment;size_t min_alloc;size_t alloc; +rte_tb_alloc_t alloc_cb; +rte_tb_reset_t reset_cb; +void *cb_ctx;/* jump target in case of memory allocation failure. */ sigjmp_buf fail; };
Signature · 1 line
2.43.0
PATCH v2 1/1 acl: support custom memory allocator
In reply to Konstantin AnanyevReduce memory fragmentation caused by dynamic memory allocations by allowing users to provide custom memory allocator.
Add new members to struct rte_acl_config to allow passing custom allocator callbacks to rte_acl_build:
- running_alloc: allocator callback for run-time internal memory
- running_free: free callback for run-time internal memory
- running_ctx: user-defined context passed to running_alloc/free
- temp_alloc: allocator callback for temporary memory during ACL build
- temp_reset: reset callback for temporary allocator
- temp_ctx: user-defined context passed to temp_alloc/reset
These callbacks allow users to provide their own memory pools or allocators for both persistent runtime structures and temporary build-time data.
A typical approach is to pre-allocate a static memory region for rte_acl_ctx, and to provide a global temporary memory manager that supports multipleallocations and a single reset during ACL build.
Since tb_mem_pool handles allocation failures using siglongjmp, temp_alloc follows the same approach for failure handling.
v2:
Fix build warning of test code on 32-bit systems
--- ...-acl-support-custom-memory-allocator.patch | 440 ++++++++++++++++++ app/test/test_acl.c | 181 ++++++- lib/acl/acl.h | 3 +- lib/acl/acl_bld.c | 14 +- lib/acl/acl_gen.c | 8 +- lib/acl/rte_acl.c | 5 +- lib/acl/rte_acl.h | 20 + lib/acl/tb_mem.c | 8 + lib/acl/tb_mem.h | 6 + 9 files changed, 676 insertions(+), 9 deletions(-) create mode 100644 0001-acl-support-custom-memory-allocator.patch
0001-acl-support-custom-memory-allocator.patch new file +440 -0
@@ -0,0 +1,440 @@ +From 248773f961d66ebf1a0812822e9251cf4b0f9d5e Mon Sep 17 00:00:00 2001 +From: YongFeng Wang <[email protected]> +Date: Mon, 24 Nov 2025 19:10:38 +0800 +Subject: [PATCH] acl: support custom memory allocator + +Reduce memory fragmentation caused by dynamic memory allocations +by allowing users to provide custom memory allocator. + +Add new members to struct rte_acl_config to allow passing custom +allocator callbacks to rte_acl_build: + +- running_alloc: allocator callback for run-time internal memory +- running_free: free callback for run-time internal memory +- running_ctx: user-defined context passed to running_alloc/free + +- temp_alloc: allocator callback for temporary memory during ACL build +- temp_reset: reset callback for temporary allocator +- temp_ctx: user-defined context passed to temp_alloc/reset + +These callbacks allow users to provide their own memory pools or +allocators for both persistent runtime structures and temporary +build-time data. + +A typical approach is to pre-allocate a static memory region +for rte_acl_ctx, and to provide a global temporary memory manager +that supports multipleallocations and a single reset during ACL build. + +Since tb_mem_pool handles allocation failures using siglongjmp, +temp_alloc follows the same approach for failure handling. + +Signed-off-by: YongFeng Wang <[email protected]> +--- + app/test/test_acl.c | 181 +++++++++++++++++++++++++++++++++++++++++++- + lib/acl/acl.h | 3 +- + lib/acl/acl_bld.c | 14 +++- + lib/acl/acl_gen.c | 8 +- + lib/acl/rte_acl.c | 5 +- + lib/acl/rte_acl.h | 20 +++++ + lib/acl/tb_mem.c | 8 ++ + lib/acl/tb_mem.h | 6 ++ + 8 files changed, 236 insertions(+), 9 deletions(-) + +diff --git a/app/test/test_acl.c b/app/test/test_acl.c +index 43d13b5b0f..0f20967df2 100644 +--- a/app/test/test_acl.c ++++ b/app/test/test_acl.c +@@ -1721,6 +1721,184 @@ test_u32_range(void) + return rc; + } + ++struct acl_ctx_wrapper_t { ++ struct rte_acl_ctx *ctx; ++ void *running_buf; ++ bool running_buf_using; ++}; ++ ++struct acl_temp_mem_mgr_t { ++ void *buf; ++ uint32_t buf_used; ++ sigjmp_buf fail; ++}; ++ ++struct acl_ctx_wrapper_t g_acl_ctx_wrapper; ++struct acl_temp_mem_mgr_t g_temp_mem_mgr; ++ ++#define ACL_RUNNING_BUF_SIZE (10 * 1024 * 1024) ++#define ACL_TEMP_BUF_SIZE (10 * 1024 * 1024) ++ ++static void *running_alloc(size_t size, unsigned int align, void *cb_data) ++{ ++ (void)align; ++ struct acl_ctx_wrapper_t *gwlb_acl_ctx = (struct acl_ctx_wrapper_t *)cb_data; ++ if (gwlb_acl_ctx->running_buf_using) ++ return NULL; ++ printf("running memory alloc for acl context, size=%" PRId64 ", pointer=%p\n", ++ size, ++ gwlb_acl_ctx->running_buf); ++ gwlb_acl_ctx->running_buf_using = true; ++ return gwlb_acl_ctx->running_buf; ++} ++ ++static void running_free(void *buf, void *cb_data) ++{ ++ if (!buf) ++ return; ++ struct acl_ctx_wrapper_t *gwlb_acl_ctx = (struct acl_ctx_wrapper_t *)cb_data; ++ printf("running memory free pointer=%p\n", buf); ++ gwlb_acl_ctx->running_buf_using = false; ++} ++ ++static void *temp_alloc(size_t size, sigjmp_buf fail, void *cb_data) ++{ ++ struct acl_temp_mem_mgr_t *gwlb_acl_build = (struct acl_temp_mem_mgr_t *)cb_data; ++ if (ACL_TEMP_BUF_SIZE - gwlb_acl_build->buf_used < size) { ++ printf("Line %i: alloc temp memory fail, size=%" PRId64 ", used=%d\n", ++ __LINE__, ++ size, ++ gwlb_acl_build->buf_used); ++ siglongjmp(fail, -ENOMEM); ++ return NULL; ++ } ++ void *ret = (char *)gwlb_acl_build->buf + gwlb_acl_build->buf_used; ++ gwlb_acl_build->buf_used += size; ++ return ret; ++} ++ ++static void temp_reset(void *cb_data) ++{ ++ struct acl_temp_mem_mgr_t *gwlb_acl_build = (struct acl_temp_mem_mgr_t *)cb_data; ++ memset(gwlb_acl_build->buf, 0, ACL_TEMP_BUF_SIZE); ++ printf("temp memory reset, used total=%d\n", gwlb_acl_build->buf_used); ++ gwlb_acl_build->buf_used = 0; ++} ++ ++static int ++rte_acl_ipv4vlan_build_wich_mem_cb(struct rte_acl_ctx *ctx, ++ const uint32_t layout[RTE_ACL_IPV4VLAN_NUM], ++ uint32_t num_categories) ++{ ++ struct rte_acl_config cfg; ++ ++ if (ctx == NULL || layout == NULL) ++ return -EINVAL; ++ ++ memset(&cfg, 0, sizeof(cfg)); ++ acl_ipv4vlan_config(&cfg, layout, num_categories); ++ cfg.running_alloc = running_alloc; ++ cfg.running_free = running_free; ++ cfg.running_cb_ctx = &g_acl_ctx_wrapper; ++ cfg.temp_alloc = temp_alloc; ++ cfg.temp_reset = temp_reset; ++ cfg.temp_cb_ctx = &g_temp_mem_mgr; ++ return rte_acl_build(ctx, &cfg); ++} ++ ++static int ++test_classify_buid_wich_mem_cb(struct rte_acl_ctx *acx, ++ const struct rte_acl_ipv4vlan_rule *rules, uint32_t num) ++{ ++ int ret; ++ ++ /* add rules to the context */ ++ ret = rte_acl_ipv4vlan_add_rules(acx, rules, num); ++ if (ret != 0) { ++ printf("Line %i: Adding rules to ACL context failed!\n", ++ __LINE__); ++ return ret; ++ } ++ ++ /* try building the context */ ++ ret = rte_acl_ipv4vlan_build_wich_mem_cb(acx, ipv4_7tuple_layout, ++ RTE_ACL_MAX_CATEGORIES); ++ if (ret != 0) { ++ printf("Line %i: Building ACL context failed!\n", __LINE__); ++ return ret; ++ } ++ ++ return 0; ++} ++ ++static int ++test_mem_cb(void) ++{ ++ int i, ret; ++ g_acl_ctx_wrapper.ctx = rte_acl_create(&acl_param); ++ if (g_acl_ctx_wrapper.ctx == NULL) { ++ printf("Line %i: Error creating ACL context!\n", __LINE__); ++ return -1; ++ } ++ g_acl_ctx_wrapper.running_buf = rte_zmalloc_socket( ++ "test_acl", ++ ACL_RUNNING_BUF_SIZE, ++ RTE_CACHE_LINE_SIZE, ++ SOCKET_ID_ANY); ++ if (!g_acl_ctx_wrapper.running_buf) { ++ printf("Line %i: Error allocing running buf for acl context!\n", __LINE__); ++ return 1; ++ } ++ g_acl_ctx_wrapper.running_buf_using = false; ++ ++ g_temp_mem_mgr.buf = malloc(ACL_TEMP_BUF_SIZE); ++ if (!g_temp_mem_mgr.buf) ++ printf("Line %i: Error allocing teem buf for acl build!\n", __LINE__); ++ memset(g_temp_mem_mgr.buf, 0, ACL_TEMP_BUF_SIZE); ++ g_temp_mem_mgr.buf_used = 0; ++ ++ ret = 0; ++ for (i = 0; i != TEST_CLASSIFY_ITER; i++) { ++ ++ if ((i & 1) == 0) ++ rte_acl_reset(g_acl_ctx_wrapper.ctx); ++ else ++ rte_acl_reset_rules(g_acl_ctx_wrapper.ctx); ++ ++ ret = test_classify_buid_wich_mem_cb(g_acl_ctx_wrapper.ctx, acl_test_rules, ++ RTE_DIM(acl_test_rules)); ++ if (ret != 0) { ++ printf("Line %i, iter: %d: " ++ "Adding rules to ACL context failed!\n", ++ __LINE__, i); ++ break; ++ } ++ ++ ret = test_classify_run(g_acl_ctx_wrapper.ctx, acl_test_data, ++ RTE_DIM(acl_test_data)); ++ if (ret != 0) { ++ printf("Line %i, iter: %d: %s failed!\n", ++ __LINE__, i, __func__); ++ break; ++ } ++ ++ /* reset rules and make sure that classify still works ok. */ ++ rte_acl_reset_rules(g_acl_ctx_wrapper.ctx); ++ ret = test_classify_run(g_acl_ctx_wrapper.ctx, acl_test_data, ++ RTE_DIM(acl_test_data)); ++ if (ret != 0) { ++ printf("Line %i, iter: %d: %s failed!\n", ++ __LINE__, i, __func__); ++ break; ++ } ++ } ++ ++ rte_acl_free(g_acl_ctx_wrapper.ctx); ++ free(g_temp_mem_mgr.buf); ++ rte_free(g_acl_ctx_wrapper.running_buf); ++ return ret; ++} ++ + static int + test_acl(void) + { +@@ -1742,7 +1920,8 @@ test_acl(void) + return -1; + if (test_u32_range() < 0) + return -1; +- ++ if (test_mem_cb() < 0) ++ return -1; + return 0; + } + +diff --git a/lib/acl/acl.h b/lib/acl/acl.h +index c8e4e72fab..7080fff64d 100644 +--- a/lib/acl/acl.h ++++ b/lib/acl/acl.h +@@ -189,7 +189,8 @@ struct rte_acl_ctx { + + int rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie, + struct rte_acl_bld_trie *node_bld_trie, uint32_t num_tries, +- uint32_t num_categories, uint32_t data_index_sz, size_t max_size); ++ uint32_t num_categories, uint32_t data_index_sz, size_t max_size, ++ const struct rte_acl_config *cfg); + + typedef int (*rte_acl_classify_t) + (const struct rte_acl_ctx *, const uint8_t **, uint32_t *, uint32_t, uint32_t); +diff --git a/lib/acl/acl_bld.c b/lib/acl/acl_bld.c +index 7056b1c117..1fd0ee3aa5 100644 +--- a/lib/acl/acl_bld.c ++++ b/lib/acl/acl_bld.c +@@ -777,9 +777,12 @@ acl_merge_trie(struct acl_build_context *context, + * - reset all RT related fields to zero. + */ + static void +-acl_build_reset(struct rte_acl_ctx *ctx) ++acl_build_reset(struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg) + { +- rte_free(ctx->mem); ++ if (cfg->running_free) ++ cfg->running_free(ctx->mem, cfg->running_cb_ctx); ++ else ++ rte_free(ctx->mem); + memset(&ctx->num_categories, 0, + sizeof(*ctx) - offsetof(struct rte_acl_ctx, num_categories)); + } +@@ -1518,6 +1521,9 @@ acl_bld(struct acl_build_context *bcx, struct rte_acl_ctx *ctx, + bcx->acx = ctx; + bcx->pool.alignment = ACL_POOL_ALIGN; + bcx->pool.min_alloc = ACL_POOL_ALLOC_MIN; ++ bcx->pool.alloc_cb = cfg->temp_alloc; ++ bcx->pool.reset_cb = cfg->temp_reset; ++ bcx->pool.cb_ctx = cfg->temp_cb_ctx; + bcx->cfg = *cfg; + bcx->category_mask = RTE_LEN2MASK(bcx->cfg.num_categories, + typeof(bcx->category_mask)); +@@ -1635,7 +1641,7 @@ rte_acl_build(struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg) + if (rc != 0) + return rc; + +- acl_build_reset(ctx); ++ acl_build_reset(ctx, cfg); + + if (cfg->max_size == 0) { + n = NODE_MIN; +@@ -1655,7 +1661,7 @@ rte_acl_build(struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg) + rc = rte_acl_gen(ctx, bcx.tries, bcx.bld_tries, + bcx.num_tries, bcx.cfg.num_categories, + ACL_MAX_INDEXES * RTE_DIM(bcx.tries) * +- sizeof(ctx->data_indexes[0]), max_size); ++ sizeof(ctx->data_indexes[0]), max_size, cfg); + if (rc == 0) { + /* set data indexes. */ + acl_set_data_indexes(ctx); +diff --git a/lib/acl/acl_gen.c b/lib/acl/acl_gen.c +index 3c53d24056..6aa7d74635 100644 +--- a/lib/acl/acl_gen.c ++++ b/lib/acl/acl_gen.c +@@ -448,7 +448,8 @@ acl_calc_counts_indices(struct acl_node_counters *counts, + int + rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie, + struct rte_acl_bld_trie *node_bld_trie, uint32_t num_tries, +- uint32_t num_categories, uint32_t data_index_sz, size_t max_size) ++ uint32_t num_categories, uint32_t data_index_sz, size_t max_size, ++ const struct rte_acl_config *cfg) + { + void *mem; + size_t total_size; +@@ -478,7 +479,10 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie, + return -ERANGE; + } + +- mem = rte_zmalloc_socket(ctx->name, total_size, RTE_CACHE_LINE_SIZE, ++ if (cfg->running_alloc) ++ mem = cfg->running_alloc(total_size, RTE_CACHE_LINE_SIZE, cfg->running_cb_ctx); ++ else ++ mem = rte_zmalloc_socket(ctx->name, total_size, RTE_CACHE_LINE_SIZE, + ctx->socket_id); + if (mem == NULL) { + ACL_LOG(ERR, +diff --git a/lib/acl/rte_acl.c b/lib/acl/rte_acl.c +index 8c0ca29618..e765c40f4f 100644 +--- a/lib/acl/rte_acl.c ++++ b/lib/acl/rte_acl.c +@@ -362,7 +362,10 @@ rte_acl_free(struct rte_acl_ctx *ctx) + + rte_mcfg_tailq_write_unlock(); + +- rte_free(ctx->mem); ++ if (ctx->config.running_free) ++ ctx->config.running_free(ctx->mem, ctx->config.running_cb_ctx); ++ else ++ rte_free(ctx->mem); + rte_free(ctx); + rte_free(te); + } +diff --git a/lib/acl/rte_acl.h b/lib/acl/rte_acl.h +index 95354cabb8..c675c9ff81 100644 +--- a/lib/acl/rte_acl.h ++++ b/lib/acl/rte_acl.h +@@ -13,6 +13,7 @@ + + #include <rte_common.h> + #include <rte_acl_osdep.h> ++#include <setjmp.h> + + #ifdef __cplusplus + extern "C" { +@@ -61,6 +62,11 @@ struct rte_acl_field_def { + * ACL build configuration. + * Defines the fields of an ACL trie and number of categories to build with. + */ ++typedef void *(*rte_acl_running_alloc_t)(size_t, unsigned int, void *); ++typedef void (*rte_acl_running_free_t)(void *, void *); ++typedef void *(*rte_acl_temp_alloc_t)(size_t, sigjmp_buf, void *); ++typedef void (*rte_acl_temp_reset_t)(void *); ++ + struct rte_acl_config { + uint32_t num_categories; /**< Number of categories to build with. */ + uint32_t num_fields; /**< Number of field definitions. */ +@@ -68,6 +74,20 @@ struct rte_acl_config { + /**< array of field definitions. */ + size_t max_size; + /**< max memory limit for internal run-time structures. */ ++ ++ /**< Allocator callback for run-time internal memory. */ ++ rte_acl_running_alloc_t running_alloc; ++ /**< Free callback for run-time internal memory. */ ++ rte_acl_running_free_t running_free; ++ /**< User context passed to running_alloc/free. */ ++ void *running_cb_ctx; ++ ++ /**< Allocator callback for temporary memory used during build. */ ++ rte_acl_temp_alloc_t temp_alloc; ++ /**< Reset callback for temporary allocator. */ ++ rte_acl_temp_reset_t temp_reset; ++ /**< User context passed to temp_alloc/reset. */ ++ void *temp_cb_ctx; + }; + + /** +diff --git a/lib/acl/tb_mem.c b/lib/acl/tb_mem.c +index 9264433422..b9c69b563e 100644 +--- a/lib/acl/tb_mem.c ++++ b/lib/acl/tb_mem.c +@@ -55,6 +55,9 @@ tb_alloc(struct tb_mem_pool *pool, size_t size) + + size = RTE_ALIGN_CEIL(size, pool->alignment); + ++ if (pool->alloc_cb) ++ return pool->alloc_cb(size, pool->fail, pool->cb_ctx); ++ + block = pool->block; + if (block == NULL || block->size < size) { + new_sz = (size > pool->min_alloc) ? size : pool->min_alloc; +@@ -71,6 +74,11 @@ tb_free_pool(struct tb_mem_pool *pool) + { + struct tb_mem_block *next, *block; + ++ if (pool->reset_cb) { ++ pool->reset_cb(pool->cb_ctx); ++ return; ++ } ++ + for (block = pool->block; block != NULL; block = next) { + next = block->next; + free(block); +diff --git a/lib/acl/tb_mem.h b/lib/acl/tb_mem.h +index 2093744a6d..2fdebefc31 100644 +--- a/lib/acl/tb_mem.h ++++ b/lib/acl/tb_mem.h +@@ -24,11 +24,17 @@ struct tb_mem_block { + uint8_t *mem; + }; + ++typedef void *(*rte_tb_alloc_t)(size_t, sigjmp_buf, void *); ++typedef void (*rte_tb_reset_t)(void *); ++ + struct tb_mem_pool { + struct tb_mem_block *block; + size_t alignment; + size_t min_alloc; + size_t alloc; ++ rte_tb_alloc_t alloc_cb; ++ rte_tb_reset_t reset_cb; ++ void *cb_ctx; + /* jump target in case of memory allocation failure. */ + sigjmp_buf fail; + }; +-- +2.43.0 +
app/test/test_acl.c +180 -1
193 lines not on this page · raw message
lib/acl/acl.h +2 -1
@@ -189,7 +189,8 @@ struct rte_acl_ctx {int rte_acl_gen (struct rte_acl_ctx *ctx,struct rte_acl_trie *trie,struct rte_acl_bld_trie *node_bld_trie,uint32_t num_tries, -uint32_t num_categories,uint32_t data_index_sz,size_t max_size); +uint32_t num_categories,uint32_t data_index_sz,size_t max_size, +const struct rte_acl_config *cfg);typedef int (*rte_acl_classify_t ) (const struct rte_acl_ctx *,const uint8_t **,uint32_t *,uint32_t ,uint32_t );
lib/acl/acl_bld.c +10 -4
@@ -777,9 +777,12 @@ acl_merge_trie(struct acl_build_context *context,* - reset all RT related fields to zero. */ static void -acl_build_reset (struct rte_acl_ctx *ctx) +acl_build_reset (struct rte_acl_ctx *ctx,const struct rte_acl_config *cfg) { -rte_free (ctx->mem); +if (cfg->running_free) + cfg->running_free (ctx->mem, cfg->running_cb_ctx); +else +rte_free (ctx->mem);memset (&ctx->num_categories,0 ,sizeof (*ctx) - offsetof(struct rte_acl_ctx , num_categories)); }
@@ -1518,6 +1521,9 @@ acl_bld(struct acl_build_context *bcx, struct rte_acl_ctx *ctx, bcx->acx = ctx; bcx->pool.alignment =ACL_POOL_ALIGN ; bcx->pool.min_alloc =ACL_POOL_ALLOC_MIN ; + bcx->pool.alloc_cb = cfg->temp_alloc; + bcx->pool.reset_cb = cfg->temp_reset; + bcx->pool.cb_ctx = cfg->temp_cb_ctx; bcx->cfg = *cfg; bcx->category_mask =RTE_LEN2MASK (bcx->cfg.num_categories,typeof (bcx->category_mask));
@@ -1635,7 +1641,7 @@ rte_acl_build(struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg)if (rc !=0 )return rc; -acl_build_reset (ctx); +acl_build_reset (ctx, cfg);if (cfg->max_size ==0 ) { n =NODE_MIN ;
@@ -1655,7 +1661,7 @@ rte_acl_build(struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg) rc =rte_acl_gen (ctx, bcx.tries, bcx.bld_tries, bcx.num_tries, bcx.cfg.num_categories,ACL_MAX_INDEXES *RTE_DIM (bcx.tries) * -sizeof (ctx->data_indexes[0 ]), max_size); +sizeof (ctx->data_indexes[0 ]), max_size, cfg);if (rc ==0 ) {/* set data indexes. */ acl_set_data_indexes (ctx);
lib/acl/acl_gen.c +6 -2
@@ -448,7 +448,8 @@ acl_calc_counts_indices(struct acl_node_counters *counts,int rte_acl_gen (struct rte_acl_ctx *ctx,struct rte_acl_trie *trie,struct rte_acl_bld_trie *node_bld_trie,uint32_t num_tries, -uint32_t num_categories,uint32_t data_index_sz,size_t max_size) +uint32_t num_categories,uint32_t data_index_sz,size_t max_size, +const struct rte_acl_config *cfg) {void *mem;size_t total_size;
@@ -478,7 +479,10 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie,return -ERANGE ; } - mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , +if (cfg->running_alloc) + mem = cfg->running_alloc (total_size,RTE_CACHE_LINE_SIZE , cfg->running_cb_ctx); +else + mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , ctx->socket_id);if (mem ==NULL ) { ACL_LOG(ERR,
lib/acl/rte_acl.c +4 -1
@@ -362,7 +362,10 @@ rte_acl_free(struct rte_acl_ctx *ctx)rte_mcfg_tailq_write_unlock (); -rte_free (ctx->mem); +if (ctx->config.running_free) + ctx->config.running_free (ctx->mem, ctx->config.running_cb_ctx); +else +rte_free (ctx->mem);rte_free (ctx);rte_free (te); }
lib/acl/rte_acl.h +20 -0
@@ -13,6 +13,7 @@#include <rte_common.h> #include <rte_acl_osdep.h> +#include <setjmp.h> #ifdef __cplusplusextern "C" {
@@ -61,6 +62,11 @@ struct rte_acl_field_def {* ACL build configuration. * Defines the fields of an ACL trie and number of categories to build with. */ +typedef void *(*rte_acl_running_alloc_t )(size_t ,unsigned int ,void *); +typedef void (*rte_acl_running_free_t )(void *,void *); +typedef void *(*rte_acl_temp_alloc_t )(size_t ,sigjmp_buf ,void *); +typedef void (*rte_acl_temp_reset_t )(void *); +struct rte_acl_config {uint32_t num_categories;/**< Number of categories to build with. */ uint32_t num_fields;/**< Number of field definitions. */
@@ -68,6 +74,20 @@ struct rte_acl_config {/**< array of field definitions. */ size_t max_size;/**< max memory limit for internal run-time structures. */ + +/**< Allocator callback for run-time internal memory. */ +rte_acl_running_alloc_t running_alloc; +/**< Free callback for run-time internal memory. */ +rte_acl_running_free_t running_free; +/**< User context passed to running_alloc/free. */ +void *running_cb_ctx; + +/**< Allocator callback for temporary memory used during build. */ +rte_acl_temp_alloc_t temp_alloc; +/**< Reset callback for temporary allocator. */ +rte_acl_temp_reset_t temp_reset; +/**< User context passed to temp_alloc/reset. */ +void *temp_cb_ctx; };/**
lib/acl/tb_mem.c +8 -0
@@ -55,6 +55,9 @@ tb_alloc(struct tb_mem_pool *pool, size_t size) size =RTE_ALIGN_CEIL (size, pool->alignment); +if (pool->alloc_cb) +return pool->alloc_cb (size, pool->fail, pool->cb_ctx); + block = pool->block;if (block ==NULL || block->size < size) { new_sz = (size > pool->min_alloc) ? size : pool->min_alloc;
@@ -71,6 +74,11 @@ tb_free_pool(struct tb_mem_pool *pool) {struct tb_mem_block *next, *block; +if (pool->reset_cb) { + pool->reset_cb (pool->cb_ctx); +return ; + } +for (block = pool->block; block !=NULL ; block = next) { next = block->next;free (block);
lib/acl/tb_mem.h +6 -0
@@ -24,11 +24,17 @@ struct tb_mem_block {uint8_t *mem; }; +typedef void *(*rte_tb_alloc_t )(size_t ,sigjmp_buf ,void *); +typedef void (*rte_tb_reset_t )(void *); +struct tb_mem_pool {struct tb_mem_block *block;size_t alignment;size_t min_alloc;size_t alloc; +rte_tb_alloc_t alloc_cb; +rte_tb_reset_t reset_cb; +void *cb_ctx;/* jump target in case of memory allocation failure. */ sigjmp_buf fail; };
Signature · 1 line
2.43.0
PATCH v3 1/1 acl: support custom memory allocator
In reply to Konstantin AnanyevReduce memory fragmentation caused by dynamic memory allocations by allowing users to provide custom memory allocator.
Add new members to struct rte_acl_config to allow passing custom allocator callbacks to rte_acl_build:
- running_alloc: allocator callback for run-time internal memory
- running_free: free callback for run-time internal memory
- running_ctx: user-defined context passed to running_alloc/free
- temp_alloc: allocator callback for temporary memory during ACL build
- temp_reset: reset callback for temporary allocator
- temp_ctx: user-defined context passed to temp_alloc/reset
These callbacks allow users to provide their own memory pools or allocators for both persistent runtime structures and temporary build-time data.
A typical approach is to pre-allocate a static memory region for rte_acl_ctx, and to provide a global temporary memory manager that supports multipleallocations and a single reset during ACL build.
Since tb_mem_pool handles allocation failures using siglongjmp, temp_alloc follows the same approach for failure handling.
--- app/test/test_acl.c | 181 +++++++++++++++++++++++++++++++++++++++++++- lib/acl/acl.h | 3 +- lib/acl/acl_bld.c | 14 +++- lib/acl/acl_gen.c | 8 +- lib/acl/rte_acl.c | 5 +- lib/acl/rte_acl.h | 20 +++++ lib/acl/tb_mem.c | 8 ++ lib/acl/tb_mem.h | 6 ++ 8 files changed, 236 insertions(+), 9 deletions(-)
app/test/test_acl.c +180 -1
@@ -1721,6 +1721,184 @@ test_u32_range(void)return rc; } +struct acl_ctx_wrapper_t { +struct rte_acl_ctx *ctx; +void *running_buf; +bool running_buf_using; +}; + +struct acl_temp_mem_mgr_t { +void *buf; +uint32_t buf_used; +sigjmp_buf fail; +}; + +struct acl_ctx_wrapper_t g_acl_ctx_wrapper; +struct acl_temp_mem_mgr_t g_temp_mem_mgr; + +#define ACL_RUNNING_BUF_SIZE (10 * 1024 * 1024) +#define ACL_TEMP_BUF_SIZE (10 * 1024 * 1024) + +static void *running_alloc (size_t size,unsigned int align,void *cb_data) +{ + (void )align; +struct acl_ctx_wrapper_t *gwlb_acl_ctx = (struct acl_ctx_wrapper_t *)cb_data; +if (gwlb_acl_ctx->running_buf_using) +return NULL ; +printf ("running memory alloc for acl context, size=%zu, pointer=%p\n" , + size, + gwlb_acl_ctx->running_buf); + gwlb_acl_ctx->running_buf_using = true; +return gwlb_acl_ctx->running_buf; +} + +static void running_free (void *buf,void *cb_data) +{ +if (!buf) +return ; +struct acl_ctx_wrapper_t *gwlb_acl_ctx = (struct acl_ctx_wrapper_t *)cb_data; +printf ("running memory free pointer=%p\n" , buf); + gwlb_acl_ctx->running_buf_using = false; +} + +static void *temp_alloc (size_t size,sigjmp_buf fail,void *cb_data) +{ +struct acl_temp_mem_mgr_t *gwlb_acl_build = (struct acl_temp_mem_mgr_t *)cb_data; +if (ACL_TEMP_BUF_SIZE - gwlb_acl_build->buf_used < size) { +printf ("Line %i: alloc temp memory fail, size=%zu, used=%d\n" , + __LINE__, + size, + gwlb_acl_build->buf_used); +siglongjmp (fail, -ENOMEM ); +return NULL ; + } +void *ret = (char *)gwlb_acl_build->buf + gwlb_acl_build->buf_used; + gwlb_acl_build->buf_used += size; +return ret; +} + +static void temp_reset (void *cb_data) +{ +struct acl_temp_mem_mgr_t *gwlb_acl_build = (struct acl_temp_mem_mgr_t *)cb_data; +memset (gwlb_acl_build->buf,0 ,ACL_TEMP_BUF_SIZE ); +printf ("temp memory reset, used total=%u\n" , gwlb_acl_build->buf_used); + gwlb_acl_build->buf_used =0 ; +} + +static int +rte_acl_ipv4vlan_build_wich_mem_cb (struct rte_acl_ctx *ctx, +const uint32_t layout[RTE_ACL_IPV4VLAN_NUM ], +uint32_t num_categories) +{ +struct rte_acl_config cfg; + +if (ctx ==NULL || layout ==NULL ) +return -EINVAL ; + +memset (&cfg,0 ,sizeof (cfg)); +acl_ipv4vlan_config (&cfg, layout, num_categories); + cfg.running_alloc = running_alloc; + cfg.running_free = running_free; + cfg.running_cb_ctx = &g_acl_ctx_wrapper; + cfg.temp_alloc = temp_alloc; + cfg.temp_reset = temp_reset; + cfg.temp_cb_ctx = &g_temp_mem_mgr; +return rte_acl_build (ctx, &cfg); +} + +static int +test_classify_buid_wich_mem_cb (struct rte_acl_ctx *acx, +const struct rte_acl_ipv4vlan_rule *rules,uint32_t num) +{ +int ret; + +/* add rules to the context */ + ret =rte_acl_ipv4vlan_add_rules (acx, rules, num); +if (ret !=0 ) { +printf ("Line %i: Adding rules to ACL context failed!\n" , + __LINE__); +return ret; + } + +/* try building the context */ + ret =rte_acl_ipv4vlan_build_wich_mem_cb (acx, ipv4_7tuple_layout, +RTE_ACL_MAX_CATEGORIES ); +if (ret !=0 ) { +printf ("Line %i: Building ACL context failed!\n" , __LINE__); +return ret; + } + +return 0 ; +} + +static int +test_mem_cb (void ) +{ +int i, ret; + g_acl_ctx_wrapper.ctx =rte_acl_create (&acl_param); +if (g_acl_ctx_wrapper.ctx ==NULL ) { +printf ("Line %i: Error creating ACL context!\n" , __LINE__); +return -1 ; + } + g_acl_ctx_wrapper.running_buf =rte_zmalloc_socket ( +"test_acl" , +ACL_RUNNING_BUF_SIZE , +RTE_CACHE_LINE_SIZE , +SOCKET_ID_ANY ); +if (!g_acl_ctx_wrapper.running_buf) { +printf ("Line %i: Error allocing running buf for acl context!\n" , __LINE__); +return 1 ; + } + g_acl_ctx_wrapper.running_buf_using = false; + + g_temp_mem_mgr.buf =malloc (ACL_TEMP_BUF_SIZE ); +if (!g_temp_mem_mgr.buf) +printf ("Line %i: Error allocing teem buf for acl build!\n" , __LINE__); +memset (g_temp_mem_mgr.buf,0 ,ACL_TEMP_BUF_SIZE ); + g_temp_mem_mgr.buf_used =0 ; + + ret =0 ; +for (i =0 ; i !=TEST_CLASSIFY_ITER ; i++) { + +if ((i &1 ) ==0 ) +rte_acl_reset (g_acl_ctx_wrapper.ctx); +else +rte_acl_reset_rules (g_acl_ctx_wrapper.ctx); + + ret =test_classify_buid_wich_mem_cb (g_acl_ctx_wrapper.ctx, acl_test_rules, +RTE_DIM (acl_test_rules)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: " +"Adding rules to ACL context failed!\n" , + __LINE__, i); +break ; + } + + ret =test_classify_run (g_acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + +/* reset rules and make sure that classify still works ok. */ +rte_acl_reset_rules (g_acl_ctx_wrapper.ctx); + ret =test_classify_run (g_acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + } + +rte_acl_free (g_acl_ctx_wrapper.ctx); +free (g_temp_mem_mgr.buf); +rte_free (g_acl_ctx_wrapper.running_buf); +return ret; +} +static int test_acl (void ) {
@@ -1742,7 +1920,8 @@ test_acl(void)return -1 ;if (test_u32_range () <0 )return -1 ; - +if (test_mem_cb () <0 ) +return -1 ;return 0 ; }
lib/acl/acl.h +2 -1
@@ -189,7 +189,8 @@ struct rte_acl_ctx {int rte_acl_gen (struct rte_acl_ctx *ctx,struct rte_acl_trie *trie,struct rte_acl_bld_trie *node_bld_trie,uint32_t num_tries, -uint32_t num_categories,uint32_t data_index_sz,size_t max_size); +uint32_t num_categories,uint32_t data_index_sz,size_t max_size, +const struct rte_acl_config *cfg);typedef int (*rte_acl_classify_t ) (const struct rte_acl_ctx *,const uint8_t **,uint32_t *,uint32_t ,uint32_t );
lib/acl/acl_bld.c +10 -4
@@ -777,9 +777,12 @@ acl_merge_trie(struct acl_build_context *context,* - reset all RT related fields to zero. */ static void -acl_build_reset (struct rte_acl_ctx *ctx) +acl_build_reset (struct rte_acl_ctx *ctx,const struct rte_acl_config *cfg) { -rte_free (ctx->mem); +if (cfg->running_free) + cfg->running_free (ctx->mem, cfg->running_cb_ctx); +else +rte_free (ctx->mem);memset (&ctx->num_categories,0 ,sizeof (*ctx) - offsetof(struct rte_acl_ctx , num_categories)); }
@@ -1518,6 +1521,9 @@ acl_bld(struct acl_build_context *bcx, struct rte_acl_ctx *ctx, bcx->acx = ctx; bcx->pool.alignment =ACL_POOL_ALIGN ; bcx->pool.min_alloc =ACL_POOL_ALLOC_MIN ; + bcx->pool.alloc_cb = cfg->temp_alloc; + bcx->pool.reset_cb = cfg->temp_reset; + bcx->pool.cb_ctx = cfg->temp_cb_ctx; bcx->cfg = *cfg; bcx->category_mask =RTE_LEN2MASK (bcx->cfg.num_categories,typeof (bcx->category_mask));
@@ -1635,7 +1641,7 @@ rte_acl_build(struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg)if (rc !=0 )return rc; -acl_build_reset (ctx); +acl_build_reset (ctx, cfg);if (cfg->max_size ==0 ) { n =NODE_MIN ;
@@ -1655,7 +1661,7 @@ rte_acl_build(struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg) rc =rte_acl_gen (ctx, bcx.tries, bcx.bld_tries, bcx.num_tries, bcx.cfg.num_categories,ACL_MAX_INDEXES *RTE_DIM (bcx.tries) * -sizeof (ctx->data_indexes[0 ]), max_size); +sizeof (ctx->data_indexes[0 ]), max_size, cfg);if (rc ==0 ) {/* set data indexes. */ acl_set_data_indexes (ctx);
lib/acl/acl_gen.c +6 -2
@@ -448,7 +448,8 @@ acl_calc_counts_indices(struct acl_node_counters *counts,int rte_acl_gen (struct rte_acl_ctx *ctx,struct rte_acl_trie *trie,struct rte_acl_bld_trie *node_bld_trie,uint32_t num_tries, -uint32_t num_categories,uint32_t data_index_sz,size_t max_size) +uint32_t num_categories,uint32_t data_index_sz,size_t max_size, +const struct rte_acl_config *cfg) {void *mem;size_t total_size;
@@ -478,7 +479,10 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie,return -ERANGE ; } - mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , +if (cfg->running_alloc) + mem = cfg->running_alloc (total_size,RTE_CACHE_LINE_SIZE , cfg->running_cb_ctx); +else + mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , ctx->socket_id);if (mem ==NULL ) { ACL_LOG(ERR,
lib/acl/rte_acl.c +4 -1
@@ -362,7 +362,10 @@ rte_acl_free(struct rte_acl_ctx *ctx)rte_mcfg_tailq_write_unlock (); -rte_free (ctx->mem); +if (ctx->config.running_free) + ctx->config.running_free (ctx->mem, ctx->config.running_cb_ctx); +else +rte_free (ctx->mem);rte_free (ctx);rte_free (te); }
lib/acl/rte_acl.h +20 -0
@@ -13,6 +13,7 @@#include <rte_common.h> #include <rte_acl_osdep.h> +#include <setjmp.h> #ifdef __cplusplusextern "C" {
@@ -61,6 +62,11 @@ struct rte_acl_field_def {* ACL build configuration. * Defines the fields of an ACL trie and number of categories to build with. */ +typedef void *(*rte_acl_running_alloc_t )(size_t ,unsigned int ,void *); +typedef void (*rte_acl_running_free_t )(void *,void *); +typedef void *(*rte_acl_temp_alloc_t )(size_t ,sigjmp_buf ,void *); +typedef void (*rte_acl_temp_reset_t )(void *); +struct rte_acl_config {uint32_t num_categories;/**< Number of categories to build with. */ uint32_t num_fields;/**< Number of field definitions. */
@@ -68,6 +74,20 @@ struct rte_acl_config {/**< array of field definitions. */ size_t max_size;/**< max memory limit for internal run-time structures. */ + +/**< Allocator callback for run-time internal memory. */ +rte_acl_running_alloc_t running_alloc; +/**< Free callback for run-time internal memory. */ +rte_acl_running_free_t running_free; +/**< User context passed to running_alloc/free. */ +void *running_cb_ctx; + +/**< Allocator callback for temporary memory used during build. */ +rte_acl_temp_alloc_t temp_alloc; +/**< Reset callback for temporary allocator. */ +rte_acl_temp_reset_t temp_reset; +/**< User context passed to temp_alloc/reset. */ +void *temp_cb_ctx; };/**
lib/acl/tb_mem.c +8 -0
@@ -55,6 +55,9 @@ tb_alloc(struct tb_mem_pool *pool, size_t size) size =RTE_ALIGN_CEIL (size, pool->alignment); +if (pool->alloc_cb) +return pool->alloc_cb (size, pool->fail, pool->cb_ctx); + block = pool->block;if (block ==NULL || block->size < size) { new_sz = (size > pool->min_alloc) ? size : pool->min_alloc;
@@ -71,6 +74,11 @@ tb_free_pool(struct tb_mem_pool *pool) {struct tb_mem_block *next, *block; +if (pool->reset_cb) { + pool->reset_cb (pool->cb_ctx); +return ; + } +for (block = pool->block; block !=NULL ; block = next) { next = block->next;free (block);
lib/acl/tb_mem.h +6 -0
@@ -24,11 +24,17 @@ struct tb_mem_block {uint8_t *mem; }; +typedef void *(*rte_tb_alloc_t )(size_t ,sigjmp_buf ,void *); +typedef void (*rte_tb_reset_t )(void *); +struct tb_mem_pool {struct tb_mem_block *block;size_t alignment;size_t min_alloc;size_t alloc; +rte_tb_alloc_t alloc_cb; +rte_tb_reset_t reset_cb; +void *cb_ctx;/* jump target in case of memory allocation failure. */ sigjmp_buf fail; };
Signature · 1 line
2.43.0
Re: [PATCH v3] acl: support custom memory allocator
Quoted 18 lines · On Tue, 25 Nov 2025 12:14:46 +0000 "mannywang(王永峰)" <[email protected]>…
Reduce memory fragmentation caused by dynamic memory allocations by allowing users to provide custom memory allocator.
Add new members to struct
rte_acl_configto allow passing custom allocator callbacks torte_acl_build:-
running_alloc: allocator callback for run-time internal memory
-running_free: free callback for run-time internal memory
-running_ctx: user-defined context passed to running_alloc/free-
temp_alloc: allocator callback for temporary memory during ACL build
-temp_reset: reset callback for temporary allocator
-temp_ctx: user-defined context passed to temp_alloc/resetThese callbacks allow users to provide their own memory pools or allocators for both persistent runtime structures and temporary build-time data.
Quoted 8 lines
A typical approach is to pre-allocate a static memory region for
rte_acl_ctx, and to provide a global temporary memory manager that supports multipleallocations and a single reset during ACL build.Since
tb_mem_poolhandles allocation failures using siglongjmp,temp_allocfollows the same approach for failure handling.Signed-off-by YongFeng Wang <[email protected]>
Rather than introduce an API change which can have impacts in many places; would it be better to fix the underlying rte_malloc implementation. The allocator in rte_malloc() is simplistic compared to glibc and other malloc libraries. The other libraries provide better density, statistics and performance.
Improving rte_malloc() would help all use cases not just the special case of busy ACL usage.
The other question is does ACL library really need to be storing this data in huge pages at all? If all it needed was an allocator for single process usage, than just using regular malloc would avoid the whole mess.
Re: [Internet]Re: [PATCH v3] acl: support custom memory allocator
Thanks for the review and questions.
In our deployment scenario, long-running services must avoid runtime allocation/deallocation to ensure stability. We have observed memory fragmentation in practice when frequent small allocations happen on the data path. Even with optimized allocators, this behavior accumulates over time and can lead to unexpected latency spikes.
To address this, our project adopts a pre-allocation model: each acl_ctx is associated with a sufficiently large memory block during initialization, and no allocations occur afterwards. This approach has been effective in eliminating runtime uncertainty in our use case.
The proposed patch enables applications with similar requirements to plug their memory management strategy into the ACL layer without changing the core logic. The default behavior remains unchanged.
Quoted 44 lines · Stephen Hemminger
Quoted 18 lines · On Tue, 25 Nov 2025 12:14:46 +0000 "mannywang(王永峰)" <[email protected]>…
Reduce memory fragmentation caused by dynamic memory allocations by allowing users to provide custom memory allocator.
Add new members to struct
rte_acl_configto allow passing custom allocator callbacks torte_acl_build:-
running_alloc: allocator callback for run-time internal memory
-running_free: free callback for run-time internal memory
-running_ctx: user-defined context passed to running_alloc/free-
temp_alloc: allocator callback for temporary memory during ACL build
-temp_reset: reset callback for temporary allocator
-temp_ctx: user-defined context passed to temp_alloc/resetThese callbacks allow users to provide their own memory pools or allocators for both persistent runtime structures and temporary build-time data.
Quoted 8 lines
A typical approach is to pre-allocate a static memory region for
rte_acl_ctx, and to provide a global temporary memory manager that supports multipleallocations and a single reset during ACL build.Since
tb_mem_poolhandles allocation failures using siglongjmp,temp_allocfollows the same approach for failure handling.Signed-off-by YongFeng Wang <[email protected]>Rather than introduce an API change which can have impacts in many places; would it be better to fix the underlying
rte_mallocimplementation. The allocator inrte_malloc()is simplistic compared to glibc and other malloc libraries. The other libraries provide better density, statistics and performance.Improving
rte_malloc()would help all use cases not just the special case of busy ACL usage.The other question is does ACL library really need to be storing this data in huge pages at all? If all it needed was an allocator for single process usage, than just using regular malloc would avoid the whole mess.
Re: [PATCH v3] acl: support custom memory allocator
In reply to mannywang(王永峰)Quoted 18 lines · mannywang(王永峰)
Reduce memory fragmentation caused by dynamic memory allocations by allowing users to provide custom memory allocator.
Add new members to struct
rte_acl_configto allow passing custom allocator callbacks torte_acl_build:-
running_alloc: allocator callback for run-time internal memory
-running_free: free callback for run-time internal memory
-running_ctx: user-defined context passed to running_alloc/free-
temp_alloc: allocator callback for temporary memory during ACL build
-temp_reset: reset callback for temporary allocator
-temp_ctx: user-defined context passed to temp_alloc/resetThese callbacks allow users to provide their own memory pools or allocators for both persistent runtime structures and temporary build-time data.
Quoted 6 lines
A typical approach is to pre-allocate a static memory region for
rte_acl_ctx, and to provide a global temporary memory manager that supports multipleallocations and a single reset during ACL build.Since
tb_mem_poolhandles allocation failures using siglongjmp,temp_allocfollows the same approach for failure handling.
If a static memory region would suffice for runtime memory, could you have solved the issue using existing API as follows?
1. Allocate memory in any way, may even use rte_malloc_*().
2. Create a new heap using rte_malloc_heap_create().
3. Attach the memory to the heap using rte_malloc_heap_memory_add().
4. Get the heap "socket ID" using rte_malloc_heap_get_socket().
5. Pass the heap "socket ID" to rte_acl_create().
In https://inbox.dpdk.org/dev/[email protected]/
you said that the issue is runtime memory fragmentation, but also did "propose extending the ACL API to support external memory buffers for the build process".
What is the issue with build-time allocations?
Re: [Internet]Re: [PATCH v3] acl: support custom memory allocator
Thanks for sharing this suggestion.
We actually evaluated the heap-based approach before implementing this patch. It can help in some scenarios, but unfortunately it does not fully solve our use cases. Specifically:
1. **Heap count / scalability**
Our application maintains at least ~200 rte_acl_ctx instances (due
to the
total rule count and multi-tenant isolation). Allowing a dedicated
heap per
context would exceed the practical limits of the current rte_malloc heap model. The number of heaps that can be created is not unlimited, and maintaining hundreds of separate heaps would introduce considerable management overhead.
2. **Temporary allocations in build stage**
During rte_acl_build, a significant portion of memory is allocated
through
calloc() for internal temporary structures. These allocations are
freed
right after the build completes. Even if runtime memory could come
from a
custom heap, these temporary allocations would still need an independent allocator or callback mechanism to avoid fragmentation and repeated malloc/free cycles.
The goal of this patch is to provide allocator callbacks so that applications can apply their own memory model consistently — static region for runtime, and resettable pool for build — without relying on uncontrolled internal allocations.
Quoted 41 lines · Dmitry Kozlyuk
Quoted 18 lines · mannywang(王永峰)
Reduce memory fragmentation caused by dynamic memory allocations by allowing users to provide custom memory allocator.
Add new members to struct
rte_acl_configto allow passing custom allocator callbacks torte_acl_build:-
running_alloc: allocator callback for run-time internal memory
-running_free: free callback for run-time internal memory
-running_ctx: user-defined context passed to running_alloc/free-
temp_alloc: allocator callback for temporary memory during ACL build
-temp_reset: reset callback for temporary allocator
-temp_ctx: user-defined context passed to temp_alloc/resetThese callbacks allow users to provide their own memory pools or allocators for both persistent runtime structures and temporary build-time data.
Quoted 6 lines
A typical approach is to pre-allocate a static memory region for
rte_acl_ctx, and to provide a global temporary memory manager that supports multipleallocations and a single reset during ACL build.Since
tb_mem_poolhandles allocation failures using siglongjmp,temp_allocfollows the same approach for failure handling.If a static memory region would suffice for runtime memory, could you have solved the issue using existing API as follows?
1. Allocate memory in any way, may even userte_malloc_*().
2. Create a new heap usingrte_malloc_heap_create().
3. Attach the memory to the heap usingrte_malloc_heap_memory_add().
4. Get the heap "socket ID" usingrte_malloc_heap_get_socket().
5. Pass the heap "socket ID" torte_acl_create().In https://inbox.dpdk.org/dev/[email protected]/
you said that the issue is runtime memory fragmentation, but also did "propose extending the ACL API to support external memory buffers for the build process".
What is the issue with build-time allocations?
Re: [Internet]Re: [PATCH v3] acl: support custom memory allocator
Quoted 6 lines · mannywang(王永峰)
Thanks for sharing this suggestion.
We actually evaluated the heap-based approach before implementing this patch. It can help in some scenarios, but unfortunately it does not fully solve our use cases. Specifically:
Quoted 7 lines
1. **Heap count / scalability**
Our application maintains at least ~200rte_acl_ctxinstances (due to the total rule count and multi-tenant isolation). Allowing a dedicated heap per context would exceed the practical limits of the currentrte_mallocheap model. The number of heaps that can be created is not unlimited, and maintaining hundreds of separate heaps would introduce considerable management overhead.
This is a valid point against heaps, thanks.
Quoted 7 lines
2. **Temporary allocations in build stage**
Duringrte_acl_build, a significant portion of memory is allocated throughcalloc()for internal temporary structures. These allocations are freed right after the build completes. Even if runtime memory could come from a custom heap, these temporary allocations would still need an independent allocator or callback mechanism to avoid fragmentation and repeated malloc/free cycles.
I don't understand the build stage issue and why it needs a custom allocator. What exactly gets fragmented?
It is the entire process address space which is practically unlimited? How does is malloc/free overhead compare to the overall ACL build time?
Re: [PATCH v3] acl: support custom memory allocator
Thanks for the follow-up question.
Quoted 1 line
I don't understand the build stage issue and why it needs a custom allocator.
The fragmentation concern does not come from the amount of address space, but from how the underlying heap allocator manages **large / mid-sized temporary buffers** that are repeatedly allocated and freed during ACL build.
ACL build allocates many temporary arrays, tables and sorted structures. Some of them are several MB in size. When these allocations are done via malloc/calloc, they typically end up in the general heap. Every build iteration produces a different allocation pattern and size distribution. Even if the allocations are freed at the end, the internal heap layout is not restored to a “flat” state. Small holes remain, and future allocation of large contiguous blocks may fail even if the total free memory is sufficient.
This becomes a real operational issue in long-running processes.
Quoted 1 line
What exactly gets fragmented? It is the entire process address space which is practically unlimited?
It is not the address space that is the limiting factor. It is the **allocator's internal arena**.
Most allocators (glibc malloc, jemalloc, tcmalloc, etc) retain internal metadata, bins, and split blocks. Their fragmentation behavior accumulates over time. The process may still have hundreds of MB of “free memory”, but not in **contiguous regions** that satisfy the next large request.
Quoted 1 line
How does malloc/free overhead compare to overall ACL build time?
The cost of malloc/free calls themselves is not the core problem. The overhead is small relative to the total build time.
The risk is that allocator fragmentation increases unpredictably over a long deployment, until a large block allocation fails in the data plane.
Our team has seen this exact behavior in production environments. Because we cannot fully control the allocator state, we prefer a model with zero dynamic allocation after init:
* persistent runtime structures → pre-allocated static region
* temporary build data → resettable memory pool
This avoids failure modes caused by allocator history and guarantees stable latency regardless of system uptime or build frequency.
Quoted 27 lines · Dmitry Kozlyuk
Quoted 6 lines · mannywang(王永峰)
Thanks for sharing this suggestion.
We actually evaluated the heap-based approach before implementing this patch. It can help in some scenarios, but unfortunately it does not fully solve our use cases. Specifically:
Quoted 7 lines
1. **Heap count / scalability**
Our application maintains at least ~200rte_acl_ctxinstances (due to the total rule count and multi-tenant isolation). Allowing a dedicated heap per context would exceed the practical limits of the currentrte_mallocheap model. The number of heaps that can be created is not unlimited, and maintaining hundreds of separate heaps would introduce considerable management overhead.This is a valid point against heaps, thanks.
Quoted 7 lines
2. **Temporary allocations in build stage**
Duringrte_acl_build, a significant portion of memory is allocated throughcalloc()for internal temporary structures. These allocations are freed right after the build completes. Even if runtime memory could come from a custom heap, these temporary allocations would still need an independent allocator or callback mechanism to avoid fragmentation and repeated malloc/free cycles.I don't understand the build stage issue and why it needs a custom allocator. What exactly gets fragmented?
It is the entire process address space which is practically unlimited? How does is malloc/free overhead compare to the overall ACL build time?
Re: [PATCH v3] acl: support custom memory allocator
Quoted 44 lines · On Wed, 26 Nov 2025 16:09:20 +0800 "mannywang(王永峰)" <[email protected]>…
Thanks for the follow-up question.
Quoted 1 line
I don't understand the build stage issue and why it needs a custom
allocator.
The fragmentation concern does not come from the amount of address space, but from how the underlying heap allocator manages **large / mid-sized temporary buffers** that are repeatedly allocated and freed during ACL build.
ACL build allocates many temporary arrays, tables and sorted structures. Some of them are several MB in size. When these allocations are done via malloc/calloc, they typically end up in the general heap. Every build iteration produces a different allocation pattern and size distribution. Even if the allocations are freed at the end, the internal heap layout is not restored to a “flat” state. Small holes remain, and future allocation of large contiguous blocks may fail even if the total free memory is sufficient.
This becomes a real operational issue in long-running processes.
Quoted 1 line
What exactly gets fragmented? It is the entire process address space
which is practically unlimited?
It is not the address space that is the limiting factor. It is the **allocator's internal arena**.
Most allocators (glibc malloc, jemalloc, tcmalloc, etc) retain internal metadata, bins, and split blocks. Their fragmentation behavior accumulates over time. The process may still have hundreds of MB of “free memory”, but not in **contiguous regions** that satisfy the next large request.
Quoted 1 line
How does malloc/free overhead compare to overall ACL build time?
The cost of malloc/free calls themselves is not the core problem. The overhead is small relative to the total build time.
The risk is that allocator fragmentation increases unpredictably over a long deployment, until a large block allocation fails in the data plane.
Our team has seen this exact behavior in production environments. Because we cannot fully control the allocator state, we prefer a model with zero dynamic allocation after init:
Quoted 44 lines
* persistent runtime structures → pre-allocated static region
* temporary build data → resettable memory poolThis avoids failure modes caused by allocator history and guarantees stable latency regardless of system uptime or build frequency.
Quoted 37 lines · Dmitry Kozlyuk
Quoted 8 lines · mannywang(王永峰)
Thanks for sharing this suggestion.
We actually evaluated the heap-based approach before implementing this patch.
It can help in some scenarios, but unfortunately it does not fully solve our
use cases. Specifically:Quoted 10 lines
1. **Heap count / scalability**
Our application maintains at least ~200rte_acl_ctxinstances (due to the
total rule count and multi-tenant isolation). Allowing a dedicated heap per
context would exceed the practical limits of the currentrte_mallocheap
model. The number of heaps that can be created is not unlimited, and maintaining hundreds of separate heaps would introduce considerable management overhead.This is a valid point against heaps, thanks.
Quoted 11 lines
2. **Temporary allocations in build stage**
Duringrte_acl_build, a significant portion of memory is allocated through
calloc()for internal temporary structures. These allocations are freed
right after the build completes. Even if runtime memory could come from a
custom heap, these temporary allocations would still need an independent
allocator or callback mechanism to avoid fragmentation and repeated malloc/free cycles.I don't understand the build stage issue and why it needs a custom allocator.
What exactly gets fragmented?
It is the entire process address space which is practically unlimited? How does is malloc/free overhead compare to the overall ACL build time?
I have seen similar issues in other networking software, mostly it is because glibc wants to avoid expensive compaction. See https://sourceware.org/glibc/wiki/MallocInternals
The solution was to call malloc_trim() at the end of control transaction. If ACL library is doing lots of small allocations, then adding it there would help.
The effect can also be mitigated by using mallopt to adjust MALLOC_TRIM_THRESHOLD. There is lots of documentation on the Internet on this.
Another option for some workloads is using an alternative library for malloc. There are lots of benchmarks on glibc vs tcmalloc vs jemalloc.
Re: [Internet]Re: [PATCH v3] acl: support custom memory allocator
Thank you very much for your suggestions.Yes, we could indeed do better on the dynamic memory management side, and some other team members also share similar views.
On the other hand, this patch gives users an option: to completely avoid dynamic memory management, or, to put it more directly, to trade a sufficiently large (and possibly wasteful) amount of memory for higher determinism.
Quoted 108 lines · Stephen Hemminger
Quoted 44 lines · On Wed, 26 Nov 2025 16:09:20 +0800 "mannywang(王永峰)" <[email protected]>…
Thanks for the follow-up question.
Quoted 1 line
I don't understand the build stage issue and why it needs a custom
allocator.
The fragmentation concern does not come from the amount of address space, but from how the underlying heap allocator manages **large / mid-sized temporary buffers** that are repeatedly allocated and freed during ACL build.
ACL build allocates many temporary arrays, tables and sorted structures. Some of them are several MB in size. When these allocations are done via malloc/calloc, they typically end up in the general heap. Every build iteration produces a different allocation pattern and size distribution. Even if the allocations are freed at the end, the internal heap layout is not restored to a “flat” state. Small holes remain, and future allocation of large contiguous blocks may fail even if the total free memory is sufficient.
This becomes a real operational issue in long-running processes.
Quoted 1 line
What exactly gets fragmented? It is the entire process address space
which is practically unlimited?
It is not the address space that is the limiting factor. It is the **allocator's internal arena**.
Most allocators (glibc malloc, jemalloc, tcmalloc, etc) retain internal metadata, bins, and split blocks. Their fragmentation behavior accumulates over time. The process may still have hundreds of MB of “free memory”, but not in **contiguous regions** that satisfy the next large request.
Quoted 1 line
How does malloc/free overhead compare to overall ACL build time?
The cost of malloc/free calls themselves is not the core problem. The overhead is small relative to the total build time.
The risk is that allocator fragmentation increases unpredictably over a long deployment, until a large block allocation fails in the data plane.
Our team has seen this exact behavior in production environments. Because we cannot fully control the allocator state, we prefer a model with zero dynamic allocation after init:
Quoted 44 lines
* persistent runtime structures → pre-allocated static region
* temporary build data → resettable memory poolThis avoids failure modes caused by allocator history and guarantees stable latency regardless of system uptime or build frequency.
Quoted 37 lines · Dmitry Kozlyuk
Quoted 8 lines · mannywang(王永峰)
Thanks for sharing this suggestion.
We actually evaluated the heap-based approach before implementing this patch.
It can help in some scenarios, but unfortunately it does not fully solve our
use cases. Specifically:Quoted 10 lines
1. **Heap count / scalability**
Our application maintains at least ~200rte_acl_ctxinstances (due
to the
total rule count and multi-tenant isolation). Allowing a dedicated
heap per
context would exceed the practical limits of the currentrte_malloc
heap
model. The number of heaps that can be created is not unlimited, and maintaining hundreds of separate heaps would introduce considerable management overhead.This is a valid point against heaps, thanks.
Quoted 11 lines
2. **Temporary allocations in build stage**
Duringrte_acl_build, a significant portion of memory is
allocated through
calloc()for internal temporary structures. These allocations are
freed
right after the build completes. Even if runtime memory could come
from a
custom heap, these temporary allocations would still need an
independent
allocator or callback mechanism to avoid fragmentation and repeated malloc/free cycles.I don't understand the build stage issue and why it needs a custom allocator.
What exactly gets fragmented?
It is the entire process address space which is practically unlimited? How does is malloc/free overhead compare to the overall ACL build time?I have seen similar issues in other networking software, mostly it is because glibc wants to avoid expensive compaction. See https://sourceware.org/glibc/wiki/MallocInternals
The solution was to call
malloc_trim()at the end of control transaction. If ACL library is doing lots of small allocations, then adding it there would help.The effect can also be mitigated by using mallopt to adjust
MALLOC_TRIM_THRESHOLD. There is lots of documentation on the Internet on this.Another option for some workloads is using an alternative library for malloc. There are lots of benchmarks on glibc vs tcmalloc vs jemalloc.
RE: [PATCH v3] acl: support custom memory allocator
In reply to mannywang(王永峰)Quoted 20 lines
Reduce memory fragmentation caused by dynamic memory allocations by allowing users to provide custom memory allocator.
Add new members to struct
rte_acl_configto allow passing custom allocator callbacks torte_acl_build:-
running_alloc: allocator callback for run-time internal memory
-running_free: free callback for run-time internal memory
-running_ctx: user-defined context passed to running_alloc/free-
temp_alloc: allocator callback for temporary memory during ACL build
-temp_reset: reset callback for temporary allocator
-temp_ctx: user-defined context passed to temp_alloc/resetThese callbacks allow users to provide their own memory pools or allocators for both persistent runtime structures and temporary build-time data.
Quoted 6 lines
A typical approach is to pre-allocate a static memory region for
rte_acl_ctx, and to provide a global temporary memory manager that supports multipleallocations and a single reset during ACL build.Since
tb_mem_poolhandles allocation failures using siglongjmp,temp_allocfollows the same approach for failure handling.
Thank you for the patch, though overall approach looks
a bit overcomplicated to me: in particular I am still not convinced that we do need a special allocator for temporary build buffers. Another concern, is that 'struct rte_acl_config' is part of public API and can't be changed at will: only at next API/ABI breakage point. Can I suggest something more simpler:
1. Add new pubic API:
struct rte_acl_mem_cb {
void (*zalloc)(void *udata, size_t size, size_t align, int32_t numa_socket); void (*free)( void *udata, void *ptr2free);
void *udata;
};
int rte_acl_set_mem_cb(struct rte_acl_ctx *acl, const struct struct rte_acl_mem_ctx *mcb); int rte_acl_get_mem_cb(const struct rte_acl_ctx *acl, struct struct rte_acl_mem_ctx *mcb);
and add ' struct rte_acl_mem_cb' instance into struct rte_acl_ctx.
At rte_acl_create() initialize them into some default functions that will be just a stubs around calling rte_zmallo_socket()/rte_free().
At acl_gen.c we will have:
- mem = rte_zmalloc_socket(ctx->name, total_size, RTE_CACHE_LINE_SIZE,
+ mem = ctx->mcb.zmalloc(ctx->mcb.udata, total_size, RTE_CACHE_LINE_SIZE,
ctx->socket_id);
Does it make sense to you?
Quoted 427 lines
Signed-off-by YongFeng Wang <[email protected]>--- app/test/test_acl.c | 181 +++++++++++++++++++++++++++++++++++++++++++- lib/acl/acl.h | 3 +- lib/acl/acl_bld.c | 14 +++- lib/acl/acl_gen.c | 8 +- lib/acl/rte_acl.c | 5 +- lib/acl/rte_acl.h | 20 +++++ lib/acl/tb_mem.c | 8 ++ lib/acl/tb_mem.h | 6 ++ 8 files changed, 236 insertions(+), 9 deletions(-)app/test/test_acl.c +59
-0@@ -1721,6 +1721,184 @@ test_u32_range(void)return rc; } +struct acl_ctx_wrapper_t { +struct rte_acl_ctx *ctx; +void *running_buf; +bool running_buf_using; +}; + +struct acl_temp_mem_mgr_t { +void *buf; +uint32_t buf_used; +sigjmp_buf fail; +}; + +struct acl_ctx_wrapper_t g_acl_ctx_wrapper; +struct acl_temp_mem_mgr_t g_temp_mem_mgr; + +#define ACL_RUNNING_BUF_SIZE (10 * 1024 * 1024) +#define ACL_TEMP_BUF_SIZE (10 * 1024 * 1024) + +static void *running_alloc (size_t size,unsigned int align,void *cb_data) +{ + (void )align; +struct acl_ctx_wrapper_t *gwlb_acl_ctx = (struct acl_ctx_wrapper_t *)cb_data; +if (gwlb_acl_ctx->running_buf_using) +return NULL ; +printf ("running memory alloc for acl context, size=%zu, pointer=%p\n" , + size, + gwlb_acl_ctx->running_buf); + gwlb_acl_ctx->running_buf_using = true; +return gwlb_acl_ctx->running_buf; +} + +static void running_free (void *buf,void *cb_data) +{ +if (!buf) +return ; +struct acl_ctx_wrapper_t *gwlb_acl_ctx = (struct acl_ctx_wrapper_t *)cb_data; +printf ("running memory free pointer=%p\n" , buf); + gwlb_acl_ctx->running_buf_using = false; +} + +static void *temp_alloc (size_t size,sigjmp_buf fail,void *cb_data) +{ +struct acl_temp_mem_mgr_t *gwlb_acl_build = (struct acl_temp_mem_mgr_t *)cb_data; +if (ACL_TEMP_BUF_SIZE - gwlb_acl_build->buf_used < size) { +printf ("Line %i: alloc temp memory fail, size=%zu, used=%d\n" , + __LINE__, + size, + gwlb_acl_build->buf_used); +siglongjmp (fail, -ENOMEM ); +return NULL ; + } +void *ret = (char *)gwlb_acl_build->buf + gwlb_acl_build->buf_used; + gwlb_acl_build->buf_used += size; +return ret; +} + +static void temp_reset (void *cb_data) +{ + struct acl_temp_mem_mgr_t *gwlb_acl_build = (struct
acl_temp_mem_mgr_t*)cb_data;app/test/test_acl.c +2
-0+memset (gwlb_acl_build->buf,0 ,ACL_TEMP_BUF_SIZE ); + printf("temp memory reset, used total=%u\n", gwlb_acl_build-Quoted 1 line
buf_used);app/test/test_acl.c +65
-0+ gwlb_acl_build->buf_used =0 ; +} + +static int +rte_acl_ipv4vlan_build_wich_mem_cb (struct rte_acl_ctx *ctx, +const uint32_t layout[RTE_ACL_IPV4VLAN_NUM ], +uint32_t num_categories) +{ +struct rte_acl_config cfg; + +if (ctx ==NULL || layout ==NULL ) +return -EINVAL ; + +memset (&cfg,0 ,sizeof (cfg)); +acl_ipv4vlan_config (&cfg, layout, num_categories); + cfg.running_alloc = running_alloc; + cfg.running_free = running_free; + cfg.running_cb_ctx = &g_acl_ctx_wrapper; + cfg.temp_alloc = temp_alloc; + cfg.temp_reset = temp_reset; + cfg.temp_cb_ctx = &g_temp_mem_mgr; +return rte_acl_build (ctx, &cfg); +} + +static int +test_classify_buid_wich_mem_cb (struct rte_acl_ctx *acx, +const struct rte_acl_ipv4vlan_rule *rules,uint32_t num) +{ +int ret; + +/* add rules to the context */ + ret =rte_acl_ipv4vlan_add_rules (acx, rules, num); +if (ret !=0 ) { +printf ("Line %i: Adding rules to ACL context failed!\n" , + __LINE__); +return ret; + } + +/* try building the context */ + ret =rte_acl_ipv4vlan_build_wich_mem_cb (acx, ipv4_7tuple_layout, +RTE_ACL_MAX_CATEGORIES ); +if (ret !=0 ) { +printf ("Line %i: Building ACL context failed!\n" , __LINE__); +return ret; + } + +return 0 ; +} + +static int +test_mem_cb (void ) +{ +int i, ret; + g_acl_ctx_wrapper.ctx =rte_acl_create (&acl_param); +if (g_acl_ctx_wrapper.ctx ==NULL ) { +printf ("Line %i: Error creating ACL context!\n" , __LINE__); +return -1 ; + } + g_acl_ctx_wrapper.running_buf =rte_zmalloc_socket ( +"test_acl" , +ACL_RUNNING_BUF_SIZE , +RTE_CACHE_LINE_SIZE , +SOCKET_ID_ANY ); +if (!g_acl_ctx_wrapper.running_buf) { + printf("Line %i: Error allocing running buf for acl context!\n",
__LINE__);app/test/test_acl.c +7
-0+return 1 ; + } + g_acl_ctx_wrapper.running_buf_using = false; + + g_temp_mem_mgr.buf =malloc (ACL_TEMP_BUF_SIZE ); +if (!g_temp_mem_mgr.buf) + printf("Line %i: Error allocing teem buf for acl build!\n",
__LINE__);app/test/test_acl.c +12
-0+memset (g_temp_mem_mgr.buf,0 ,ACL_TEMP_BUF_SIZE ); + g_temp_mem_mgr.buf_used =0 ; + + ret =0 ; +for (i =0 ; i !=TEST_CLASSIFY_ITER ; i++) { + +if ((i &1 ) ==0 ) +rte_acl_reset (g_acl_ctx_wrapper.ctx); +else +rte_acl_reset_rules (g_acl_ctx_wrapper.ctx); + + ret = test_classify_buid_wich_mem_cb(g_acl_ctx_wrapper.ctx,
acl_test_rules,app/test/test_acl.c +35
-1+ RTE_DIM(acl_test_rules)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: " +"Adding rules to ACL context failed!\n" , + __LINE__, i); +break ; + } + + ret =test_classify_run (g_acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + +/* reset rules and make sure that classify still works ok. */ +rte_acl_reset_rules (g_acl_ctx_wrapper.ctx); + ret =test_classify_run (g_acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + } + +rte_acl_free (g_acl_ctx_wrapper.ctx); +free (g_temp_mem_mgr.buf); +rte_free (g_acl_ctx_wrapper.running_buf); +return ret; +} +static int test_acl (void ) {@@ -1742,7 +1920,8 @@ test_acl(void)return -1 ;if (test_u32_range () <0 )return -1 ; - +if (test_mem_cb () <0 ) +return -1 ;return 0 ; }lib/acl/acl.h +2
-1@@ -189,7 +189,8 @@ struct rte_acl_ctx {int rte_acl_gen (struct rte_acl_ctx *ctx,struct rte_acl_trie *trie,struct rte_acl_bld_trie *node_bld_trie,uint32_t num_tries, -uint32_t num_categories,uint32_t data_index_sz,size_t max_size); +uint32_t num_categories,uint32_t data_index_sz,size_t max_size, +const struct rte_acl_config *cfg);typedef int (*rte_acl_classify_t ) (const struct rte_acl_ctx *,const uint8_t **,uint32_t *,uint32_t ,uint32_t );lib/acl/acl_bld.c +8
-2@@ -777,9 +777,12 @@ acl_merge_trie(struct acl_build_context *context,* - reset all RT related fields to zero. */ static void -acl_build_reset (struct rte_acl_ctx *ctx) +acl_build_reset (struct rte_acl_ctx *ctx,const struct rte_acl_config *cfg) { -rte_free (ctx->mem); +if (cfg->running_free) + cfg->running_free (ctx->mem, cfg->running_cb_ctx); +else +rte_free (ctx->mem);memset (&ctx->num_categories,0 ,sizeof (*ctx) - offsetof(struct rte_acl_ctx , num_categories)); }@@ -1518,6 +1521,9 @@ acl_bld(struct acl_build_context *bcx, struct rte_acl_ctx *ctx, bcx->acx = ctx; bcx->pool.alignment =ACL_POOL_ALIGN ; bcx->pool.min_alloc =ACL_POOL_ALLOC_MIN ; + bcx->pool.alloc_cb = cfg->temp_alloc; + bcx->pool.reset_cb = cfg->temp_reset; + bcx->pool.cb_ctx = cfg->temp_cb_ctx; bcx->cfg = *cfg; bcx->category_mask = RTE_LEN2MASK(bcx->cfg.num_categories,lib/acl/acl_bld.c +1
-1typeof(bcx->category_mask));@@ -1635,7 +1641,7 @@ rte_acl_build(struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg)if (rc !=0 )return rc; -acl_build_reset (ctx); +acl_build_reset (ctx, cfg);if (cfg->max_size ==0 ) {lib/acl/acl_bld.c +1
-1n =NODE_MIN ;@@ -1655,7 +1661,7 @@ rte_acl_build(struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg) rc =rte_acl_gen (ctx, bcx.tries, bcx.bld_tries, bcx.num_tries, bcx.cfg.num_categories,ACL_MAX_INDEXES *RTE_DIM (bcx.tries) * -sizeof (ctx->data_indexes[0 ]), max_size); +sizeof (ctx->data_indexes[0 ]), max_size, cfg);if (rc ==0 ) {/* set data indexes. */ lib/acl/acl_bld.c +0
-0acl_set_data_indexes (ctx);lib/acl/acl_gen.c +2
-1@@ -448,7 +448,8 @@ acl_calc_counts_indices(struct acl_node_counters *counts,int rte_acl_gen (struct rte_acl_ctx *ctx,struct rte_acl_trie *trie,struct rte_acl_bld_trie *node_bld_trie,uint32_t num_tries, -uint32_t num_categories,uint32_t data_index_sz,size_t max_size) +uint32_t num_categories,uint32_t data_index_sz,size_t max_size, +const struct rte_acl_config *cfg) {void *mem;lib/acl/acl_gen.c +4
-1size_t total_size;@@ -478,7 +479,10 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie, return -ERANGE ; } - mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , + if (cfg->running_alloc) + mem = cfg->running_alloc(total_size, RTE_CACHE_LINE_SIZE, cfg- >running_cb_ctx); + else + mem = rte_zmalloc_socket(ctx->name, total_size,
RTE_CACHE_LINE_SIZE,lib/acl/acl_gen.c +0
-0ctx->socket_id);if (mem ==NULL ) { ACL_LOG(ERR,lib/acl/rte_acl.c +4
-1@@ -362,7 +362,10 @@ rte_acl_free(struct rte_acl_ctx *ctx)rte_mcfg_tailq_write_unlock (); -rte_free (ctx->mem); +if (ctx->config.running_free) + ctx->config.running_free (ctx->mem, ctx- >config.running_cb_ctx); +else +rte_free (ctx->mem);rte_free (ctx);rte_free (te);}
lib/acl/rte_acl.h +20
-0@@ -13,6 +13,7 @@#include <rte_common.h> #include <rte_acl_osdep.h> +#include <setjmp.h> #ifdef __cplusplusextern "C" {@@ -61,6 +62,11 @@ struct rte_acl_field_def {* ACL build configuration. * Defines the fields of an ACL trie and number of categories to build with. */ +typedef void *(*rte_acl_running_alloc_t )(size_t ,unsigned int ,void *); +typedef void (*rte_acl_running_free_t )(void *,void *); +typedef void *(*rte_acl_temp_alloc_t )(size_t ,sigjmp_buf ,void *); +typedef void (*rte_acl_temp_reset_t )(void *); +struct rte_acl_config {uint32_t num_categories;/**< Number of categories to build with. */ uint32_t num_fields;/**< Number of field definitions. */ @@ -68,6 +74,20 @@ struct rte_acl_config {/**< array of field definitions. */ size_t max_size;/**< max memory limit for internal run-time structures. */ + +/**< Allocator callback for run-time internal memory. */ +rte_acl_running_alloc_t running_alloc; +/**< Free callback for run-time internal memory. */ +rte_acl_running_free_t running_free; +/**< User context passed to running_alloc/free. */ +void *running_cb_ctx; + +/**< Allocator callback for temporary memory used during build. */ +rte_acl_temp_alloc_t temp_alloc; +/**< Reset callback for temporary allocator. */ +rte_acl_temp_reset_t temp_reset; +/**< User context passed to temp_alloc/reset. */ +void *temp_cb_ctx; };/** lib/acl/tb_mem.c +8
-0@@ -55,6 +55,9 @@ tb_alloc(struct tb_mem_pool *pool, size_t size) size =RTE_ALIGN_CEIL (size, pool->alignment); +if (pool->alloc_cb) +return pool->alloc_cb (size, pool->fail, pool->cb_ctx); + block = pool->block;if (block ==NULL || block->size < size) { new_sz = (size > pool->min_alloc) ? size : pool->min_alloc;@@ -71,6 +74,11 @@ tb_free_pool(struct tb_mem_pool *pool) {struct tb_mem_block *next, *block; +if (pool->reset_cb) { + pool->reset_cb (pool->cb_ctx); +return ; + } +for (block = pool->block; block !=NULL ; block = next) { next = block->next;free (block);lib/acl/tb_mem.h +6
-1@@ -24,11 +24,17 @@ struct tb_mem_block {uint8_t *mem; }; +typedef void *(*rte_tb_alloc_t )(size_t ,sigjmp_buf ,void *); +typedef void (*rte_tb_reset_t )(void *); +struct tb_mem_pool {struct tb_mem_block *block;size_t alignment;size_t min_alloc;size_t alloc; +rte_tb_alloc_t alloc_cb; +rte_tb_reset_t reset_cb; +void *cb_ctx;/* jump target in case of memory allocation failure. */ sigjmp_buf fail; }; --2.43.0
RE: [PATCH v3] acl: support custom memory allocator
Yes, I will update the patch accordingly and send next version.
Quoted 483 lines · Konstantin Ananyev
Quoted 20 lines
Reduce memory fragmentation caused by dynamic memory allocations by allowing users to provide custom memory allocator.
Add new members to struct
rte_acl_configto allow passing custom allocator callbacks torte_acl_build:-
running_alloc: allocator callback for run-time internal memory
-running_free: free callback for run-time internal memory
-running_ctx: user-defined context passed to running_alloc/free-
temp_alloc: allocator callback for temporary memory during ACL build
-temp_reset: reset callback for temporary allocator
-temp_ctx: user-defined context passed to temp_alloc/resetThese callbacks allow users to provide their own memory pools or allocators for both persistent runtime structures and temporary build-time data.
Quoted 6 lines
A typical approach is to pre-allocate a static memory region for
rte_acl_ctx, and to provide a global temporary memory manager that supports multipleallocations and a single reset during ACL build.Since
tb_mem_poolhandles allocation failures using siglongjmp,temp_allocfollows the same approach for failure handling.Thank you for the patch, though overall approach looks
a bit overcomplicated to me: in particular I am still not convinced that we do need a special allocator for temporary build buffers. Another concern, is that 'structrte_acl_config' is part of public API and can't be changed at will: only at next API/ABI breakage point. Can I suggest something more simpler:1. Add new pubic API:
struct rte_acl_mem_cb { void (*zalloc)(void *udata, size_t size, size_t align, int32_t numa_socket); void (*free)( void *udata, void *ptr2free); void *udata; };int
rte_acl_set_mem_cb(struct rte_acl_ctx *acl, const struct struct rte_acl_mem_ctx *mcb); intrte_acl_get_mem_cb(const struct rte_acl_ctx *acl, struct struct rte_acl_mem_ctx *mcb);and add ' struct
rte_acl_mem_cb' instance into structrte_acl_ctx.
Atrte_acl_create()initialize them into some default functions that will be just a stubs around calling rte_zmallo_socket()/rte_free().
Atacl_gen.cwe will have:lib/acl/acl_gen.c +1
-1- mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , + mem = ctx->mcb.zmalloc (ctx->mcb.udata, total_size,RTE_CACHE_LINE_SIZE , ctx->socket_id);Does it make sense to you?
Quoted 427 lines
Signed-off-by YongFeng Wang <[email protected]>--- app/test/test_acl.c | 181 +++++++++++++++++++++++++++++++++++++++++++- lib/acl/acl.h | 3 +- lib/acl/acl_bld.c | 14 +++- lib/acl/acl_gen.c | 8 +- lib/acl/rte_acl.c | 5 +- lib/acl/rte_acl.h | 20 +++++ lib/acl/tb_mem.c | 8 ++ lib/acl/tb_mem.h | 6 ++ 8 files changed, 236 insertions(+), 9 deletions(-)app/test/test_acl.c +59
-0@@ -1721,6 +1721,184 @@ test_u32_range(void)return rc; } +struct acl_ctx_wrapper_t { +struct rte_acl_ctx *ctx; +void *running_buf; +bool running_buf_using; +}; + +struct acl_temp_mem_mgr_t { +void *buf; +uint32_t buf_used; +sigjmp_buf fail; +}; + +struct acl_ctx_wrapper_t g_acl_ctx_wrapper; +struct acl_temp_mem_mgr_t g_temp_mem_mgr; + +#define ACL_RUNNING_BUF_SIZE (10 * 1024 * 1024) +#define ACL_TEMP_BUF_SIZE (10 * 1024 * 1024) + +static void *running_alloc (size_t size,unsigned int align,void *cb_data) +{ + (void )align; +struct acl_ctx_wrapper_t *gwlb_acl_ctx = (struct acl_ctx_wrapper_t *)cb_data; +if (gwlb_acl_ctx->running_buf_using) +return NULL ; +printf ("running memory alloc for acl context, size=%zu, pointer=%p\n" , + size, + gwlb_acl_ctx->running_buf); + gwlb_acl_ctx->running_buf_using = true; +return gwlb_acl_ctx->running_buf; +} + +static void running_free (void *buf,void *cb_data) +{ +if (!buf) +return ; +struct acl_ctx_wrapper_t *gwlb_acl_ctx = (struct acl_ctx_wrapper_t *)cb_data; +printf ("running memory free pointer=%p\n" , buf); + gwlb_acl_ctx->running_buf_using = false; +} + +static void *temp_alloc (size_t size,sigjmp_buf fail,void *cb_data) +{ +struct acl_temp_mem_mgr_t *gwlb_acl_build = (struct acl_temp_mem_mgr_t *)cb_data; +if (ACL_TEMP_BUF_SIZE - gwlb_acl_build->buf_used < size) { +printf ("Line %i: alloc temp memory fail, size=%zu, used=%d\n" , + __LINE__, + size, + gwlb_acl_build->buf_used); +siglongjmp (fail, -ENOMEM ); +return NULL ; + } +void *ret = (char *)gwlb_acl_build->buf + gwlb_acl_build->buf_used; + gwlb_acl_build->buf_used += size; +return ret; +} + +static void temp_reset (void *cb_data) +{ + struct acl_temp_mem_mgr_t *gwlb_acl_build = (struct
acl_temp_mem_mgr_t*)cb_data;app/test/test_acl.c +2
-0+memset (gwlb_acl_build->buf,0 ,ACL_TEMP_BUF_SIZE ); + printf("temp memory reset, used total=%u\n", gwlb_acl_build-Quoted 1 line
buf_used);app/test/test_acl.c +65
-0+ gwlb_acl_build->buf_used =0 ; +} + +static int +rte_acl_ipv4vlan_build_wich_mem_cb (struct rte_acl_ctx *ctx, +const uint32_t layout[RTE_ACL_IPV4VLAN_NUM ], +uint32_t num_categories) +{ +struct rte_acl_config cfg; + +if (ctx ==NULL || layout ==NULL ) +return -EINVAL ; + +memset (&cfg,0 ,sizeof (cfg)); +acl_ipv4vlan_config (&cfg, layout, num_categories); + cfg.running_alloc = running_alloc; + cfg.running_free = running_free; + cfg.running_cb_ctx = &g_acl_ctx_wrapper; + cfg.temp_alloc = temp_alloc; + cfg.temp_reset = temp_reset; + cfg.temp_cb_ctx = &g_temp_mem_mgr; +return rte_acl_build (ctx, &cfg); +} + +static int +test_classify_buid_wich_mem_cb (struct rte_acl_ctx *acx, +const struct rte_acl_ipv4vlan_rule *rules,uint32_t num) +{ +int ret; + +/* add rules to the context */ + ret =rte_acl_ipv4vlan_add_rules (acx, rules, num); +if (ret !=0 ) { +printf ("Line %i: Adding rules to ACL context failed!\n" , + __LINE__); +return ret; + } + +/* try building the context */ + ret =rte_acl_ipv4vlan_build_wich_mem_cb (acx, ipv4_7tuple_layout, +RTE_ACL_MAX_CATEGORIES ); +if (ret !=0 ) { +printf ("Line %i: Building ACL context failed!\n" , __LINE__); +return ret; + } + +return 0 ; +} + +static int +test_mem_cb (void ) +{ +int i, ret; + g_acl_ctx_wrapper.ctx =rte_acl_create (&acl_param); +if (g_acl_ctx_wrapper.ctx ==NULL ) { +printf ("Line %i: Error creating ACL context!\n" , __LINE__); +return -1 ; + } + g_acl_ctx_wrapper.running_buf =rte_zmalloc_socket ( +"test_acl" , +ACL_RUNNING_BUF_SIZE , +RTE_CACHE_LINE_SIZE , +SOCKET_ID_ANY ); +if (!g_acl_ctx_wrapper.running_buf) { + printf("Line %i: Error allocing running buf for acl context!\n",
__LINE__);app/test/test_acl.c +7
-0+return 1 ; + } + g_acl_ctx_wrapper.running_buf_using = false; + + g_temp_mem_mgr.buf =malloc (ACL_TEMP_BUF_SIZE ); +if (!g_temp_mem_mgr.buf) + printf("Line %i: Error allocing teem buf for acl build!\n",
__LINE__);app/test/test_acl.c +12
-0+memset (g_temp_mem_mgr.buf,0 ,ACL_TEMP_BUF_SIZE ); + g_temp_mem_mgr.buf_used =0 ; + + ret =0 ; +for (i =0 ; i !=TEST_CLASSIFY_ITER ; i++) { + +if ((i &1 ) ==0 ) +rte_acl_reset (g_acl_ctx_wrapper.ctx); +else +rte_acl_reset_rules (g_acl_ctx_wrapper.ctx); + + ret = test_classify_buid_wich_mem_cb(g_acl_ctx_wrapper.ctx,
acl_test_rules,app/test/test_acl.c +35
-1+ RTE_DIM(acl_test_rules)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: " +"Adding rules to ACL context failed!\n" , + __LINE__, i); +break ; + } + + ret =test_classify_run (g_acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + +/* reset rules and make sure that classify still works ok. */ +rte_acl_reset_rules (g_acl_ctx_wrapper.ctx); + ret =test_classify_run (g_acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + } + +rte_acl_free (g_acl_ctx_wrapper.ctx); +free (g_temp_mem_mgr.buf); +rte_free (g_acl_ctx_wrapper.running_buf); +return ret; +} +static int test_acl (void ) {@@ -1742,7 +1920,8 @@ test_acl(void)return -1 ;if (test_u32_range () <0 )return -1 ; - +if (test_mem_cb () <0 ) +return -1 ;return 0 ; }lib/acl/acl.h +2
-1@@ -189,7 +189,8 @@ struct rte_acl_ctx {int rte_acl_gen (struct rte_acl_ctx *ctx,struct rte_acl_trie *trie,struct rte_acl_bld_trie *node_bld_trie,uint32_t num_tries, -uint32_t num_categories,uint32_t data_index_sz,size_t max_size); +uint32_t num_categories,uint32_t data_index_sz,size_t max_size, +const struct rte_acl_config *cfg);typedef int (*rte_acl_classify_t ) (const struct rte_acl_ctx *,const uint8_t **,uint32_t *,uint32_t ,uint32_t );lib/acl/acl_bld.c +8
-2@@ -777,9 +777,12 @@ acl_merge_trie(struct acl_build_context *context,* - reset all RT related fields to zero. */ static void -acl_build_reset (struct rte_acl_ctx *ctx) +acl_build_reset (struct rte_acl_ctx *ctx,const struct rte_acl_config *cfg) { -rte_free (ctx->mem); +if (cfg->running_free) + cfg->running_free (ctx->mem, cfg->running_cb_ctx); +else +rte_free (ctx->mem);memset (&ctx->num_categories,0 ,sizeof (*ctx) - offsetof(struct rte_acl_ctx , num_categories)); }@@ -1518,6 +1521,9 @@ acl_bld(struct acl_build_context *bcx, struct rte_acl_ctx *ctx, bcx->acx = ctx; bcx->pool.alignment =ACL_POOL_ALIGN ; bcx->pool.min_alloc =ACL_POOL_ALLOC_MIN ; + bcx->pool.alloc_cb = cfg->temp_alloc; + bcx->pool.reset_cb = cfg->temp_reset; + bcx->pool.cb_ctx = cfg->temp_cb_ctx; bcx->cfg = *cfg; bcx->category_mask = RTE_LEN2MASK(bcx->cfg.num_categories,typeof(bcx->category_mask));lib/acl/acl_bld.c +1
-1@@ -1635,7 +1641,7 @@ rte_acl_build(struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg)if (rc !=0 )return rc; -acl_build_reset (ctx); +acl_build_reset (ctx, cfg);if (cfg->max_size ==0 ) {n = NODE_MIN;lib/acl/acl_bld.c +1
-1@@ -1655,7 +1661,7 @@ rte_acl_build(struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg) rc =rte_acl_gen (ctx, bcx.tries, bcx.bld_tries, bcx.num_tries, bcx.cfg.num_categories,ACL_MAX_INDEXES *RTE_DIM (bcx.tries) * -sizeof (ctx->data_indexes[0 ]), max_size); +sizeof (ctx->data_indexes[0 ]), max_size, cfg);if (rc ==0 ) {/* set data indexes. */ acl_set_data_indexes(ctx);lib/acl/acl_gen.c +2
-1@@ -448,7 +448,8 @@ acl_calc_counts_indices(struct acl_node_counters *counts,int rte_acl_gen (struct rte_acl_ctx *ctx,struct rte_acl_trie *trie,struct rte_acl_bld_trie *node_bld_trie,uint32_t num_tries, -uint32_t num_categories,uint32_t data_index_sz,size_t max_size) +uint32_t num_categories,uint32_t data_index_sz,size_t max_size, +const struct rte_acl_config *cfg) {void *mem;
size_ttotal_size;lib/acl/acl_gen.c +4
-1@@ -478,7 +479,10 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie, return -ERANGE ; } - mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , + if (cfg->running_alloc) + mem = cfg->running_alloc(total_size, RTE_CACHE_LINE_SIZE, cfg- > running_cb_ctx); + else + mem = rte_zmalloc_socket(ctx->name, total_size,
RTE_CACHE_LINE_SIZE,
ctx->socket_id);
if (mem == NULL) {
ACL_LOG(ERR,lib/acl/rte_acl.c +4
-1@@ -362,7 +362,10 @@ rte_acl_free(struct rte_acl_ctx *ctx)rte_mcfg_tailq_write_unlock (); -rte_free (ctx->mem); +if (ctx->config.running_free) + ctx->config.running_free (ctx->mem, ctx- > config.running_cb_ctx); +else +rte_free (ctx->mem);rte_free (ctx);rte_free (te);}lib/acl/rte_acl.h +20
-0@@ -13,6 +13,7 @@#include <rte_common.h> #include <rte_acl_osdep.h> +#include <setjmp.h> #ifdef __cplusplusextern "C" {@@ -61,6 +62,11 @@ struct rte_acl_field_def {* ACL build configuration. * Defines the fields of an ACL trie and number of categories to build with. */ +typedef void *(*rte_acl_running_alloc_t )(size_t ,unsigned int ,void *); +typedef void (*rte_acl_running_free_t )(void *,void *); +typedef void *(*rte_acl_temp_alloc_t )(size_t ,sigjmp_buf ,void *); +typedef void (*rte_acl_temp_reset_t )(void *); +struct rte_acl_config {uint32_t num_categories;/**< Number of categories to build with. */ uint32_t num_fields;/**< Number of field definitions. */ @@ -68,6 +74,20 @@ struct rte_acl_config {/**< array of field definitions. */ size_t max_size;/**< max memory limit for internal run-time structures. */ + +/**< Allocator callback for run-time internal memory. */ +rte_acl_running_alloc_t running_alloc; +/**< Free callback for run-time internal memory. */ +rte_acl_running_free_t running_free; +/**< User context passed to running_alloc/free. */ +void *running_cb_ctx; + +/**< Allocator callback for temporary memory used during build. */ +rte_acl_temp_alloc_t temp_alloc; +/**< Reset callback for temporary allocator. */ +rte_acl_temp_reset_t temp_reset; +/**< User context passed to temp_alloc/reset. */ +void *temp_cb_ctx; };/** lib/acl/tb_mem.c +8
-0@@ -55,6 +55,9 @@ tb_alloc(struct tb_mem_pool *pool, size_t size) size =RTE_ALIGN_CEIL (size, pool->alignment); +if (pool->alloc_cb) +return pool->alloc_cb (size, pool->fail, pool->cb_ctx); + block = pool->block;if (block ==NULL || block->size < size) { new_sz = (size > pool->min_alloc) ? size : pool->min_alloc;@@ -71,6 +74,11 @@ tb_free_pool(struct tb_mem_pool *pool) {struct tb_mem_block *next, *block; +if (pool->reset_cb) { + pool->reset_cb (pool->cb_ctx); +return ; + } +for (block = pool->block; block !=NULL ; block = next) { next = block->next;free (block);lib/acl/tb_mem.h +6
-1@@ -24,11 +24,17 @@ struct tb_mem_block {uint8_t *mem; }; +typedef void *(*rte_tb_alloc_t )(size_t ,sigjmp_buf ,void *); +typedef void (*rte_tb_reset_t )(void *); +struct tb_mem_pool {struct tb_mem_block *block;size_t alignment;size_t min_alloc;size_t alloc; +rte_tb_alloc_t alloc_cb; +rte_tb_reset_t reset_cb; +void *cb_ctx;/* jump target in case of memory allocation failure. */ sigjmp_buf fail; }; --2.43.0
PATCH v4 1/1 acl: support custom memory allocators in rte_acl_build
In reply to Konstantin AnanyevAllow users to provide custom
memory allocation callbacks for runtime memory in rte_acl_ctx, via struct rte_acl_mem_cb.
Key changes:
- Added struct rte_acl_mem_cb with zalloc, free, and udata.
- Added rte_acl_set_mem_cb() / rte_acl_get_mem_cb() to set/get callbacks.
- Default allocation uses existing rte_zmalloc_socket/rte_free.
- Modified ACL code to call callbacks for runtime allocations instead of rte_zmalloc_socket/rte_free directly.
--- app/test/test_acl.c | 121 ++++++++++++++++++++++++++++++++++++++++++++ lib/acl/acl.h | 1 + lib/acl/acl_bld.c | 2 +- lib/acl/acl_gen.c | 4 +- lib/acl/rte_acl.c | 35 ++++++++++++- lib/acl/rte_acl.h | 42 +++++++++++++++ 6 files changed, 201 insertions(+), 4 deletions(-)
app/test/test_acl.c +121 -0
@@ -1721,6 +1721,125 @@ test_u32_range(void)return rc; } +struct acl_ctx_wrapper_t { +struct rte_acl_ctx *ctx; +void *running_buf; +bool running_buf_using; +}; + +#define ACL_RUNNING_BUF_SIZE (10 * 1024 * 1024) + +static void *running_alloc (void *udata,char *name,size_t size, +size_t align,int32_t socket_id) +{ + (void )align; + (void )name; + (void )socket_id; +if (size >ACL_RUNNING_BUF_SIZE ) +return NULL ; +struct acl_ctx_wrapper_t *gwlb_acl_ctx = (struct acl_ctx_wrapper_t *)udata; +if (gwlb_acl_ctx->running_buf_using) +return NULL ; +printf ("running memory alloc for acl context, size=%zu, pointer=%p\n" , + size, + gwlb_acl_ctx->running_buf); +memset (gwlb_acl_ctx->running_buf,0 , size); + gwlb_acl_ctx->running_buf_using = true; +return gwlb_acl_ctx->running_buf; +} + +static void running_free (void *udata,void *ptr) +{ +if (!ptr) +return ; +struct acl_ctx_wrapper_t *gwlb_acl_ctx = (struct acl_ctx_wrapper_t *)udata; +printf ("running memory free, pointer=%p\n" , ptr); + gwlb_acl_ctx->running_buf_using = false; +} + +static int +test_mem_cb (void ) +{ +int i, ret; +struct acl_ctx_wrapper_t g_acl_ctx_wrapper; + g_acl_ctx_wrapper.ctx =rte_acl_create (&acl_param); +if (g_acl_ctx_wrapper.ctx ==NULL ) { +printf ("Line %i: Error creating ACL context!\n" , __LINE__); +return -1 ; + } + g_acl_ctx_wrapper.running_buf =rte_zmalloc_socket ( +"test_acl" , +ACL_RUNNING_BUF_SIZE , +RTE_CACHE_LINE_SIZE , +SOCKET_ID_ANY ); +if (!g_acl_ctx_wrapper.running_buf) { +printf ("Line %i: Error allocing running buf for acl context!\n" , __LINE__); +return 1 ; + } + g_acl_ctx_wrapper.running_buf_using = false; + +struct rte_acl_mem_cb mcb = { + .zalloc = running_alloc, + .free = running_free, + .udata = &g_acl_ctx_wrapper + }; + ret =rte_acl_set_mem_cb (g_acl_ctx_wrapper.ctx, &mcb); +if (ret) { +printf ("Line %i: Error set mem cb for acl context!\n" , __LINE__); +return 1 ; + } +struct rte_acl_mem_cb new_mcb; +memset (&new_mcb,0 ,sizeof (struct rte_acl_mem_cb )); + ret =rte_acl_get_mem_cb (g_acl_ctx_wrapper.ctx, &new_mcb); +if (ret) { +printf ("Line %i: Error get mem cb for acl context!\n" , __LINE__); +return 1 ; + } +if (memcmp (&mcb, &new_mcb,sizeof (struct rte_acl_mem_cb )) !=0 ) { +printf ("Line %i: Error get mem cb for acl context!\n" , __LINE__); +return 1 ; + } + ret =0 ; +for (i =0 ; i !=TEST_CLASSIFY_ITER ; i++) { + +if ((i &1 ) ==0 ) +rte_acl_reset (g_acl_ctx_wrapper.ctx); +else +rte_acl_reset_rules (g_acl_ctx_wrapper.ctx); + + ret =test_classify_buid (g_acl_ctx_wrapper.ctx, acl_test_rules, +RTE_DIM (acl_test_rules)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: " +"Adding rules to ACL context failed!\n" , + __LINE__, i); +break ; + } + + ret =test_classify_run (g_acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + +/* reset rules and make sure that classify still works ok. */ +rte_acl_reset_rules (g_acl_ctx_wrapper.ctx); + ret =test_classify_run (g_acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + } + +rte_acl_free (g_acl_ctx_wrapper.ctx); +rte_free (g_acl_ctx_wrapper.running_buf); +return ret; +} +static int test_acl (void ) {
@@ -1742,6 +1861,8 @@ test_acl(void)return -1 ;if (test_u32_range () <0 )return -1 ; +if (test_mem_cb () <0 ) +return -1 ;return 0 ; }
lib/acl/acl.h +1 -0
@@ -174,6 +174,7 @@ struct rte_acl_ctx {uint32_t max_rules;uint32_t rule_sz;uint32_t num_rules; +struct rte_acl_mem_cb mem_cb;uint32_t num_categories;uint32_t num_tries;uint32_t match_index;
lib/acl/acl_bld.c +1 -1
@@ -779,7 +779,7 @@ acl_merge_trie(struct acl_build_context *context,static void acl_build_reset (struct rte_acl_ctx *ctx) { -rte_free (ctx->mem); + ctx->mem_cb.free (ctx->mem_cb.udata, ctx->mem);memset (&ctx->num_categories,0 ,sizeof (*ctx) - offsetof(struct rte_acl_ctx , num_categories)); }
lib/acl/acl_gen.c +2 -2
@@ -478,8 +478,8 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie,return -ERANGE ; } - mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , - ctx->socket_id); + mem = ctx->mem_cb.zalloc (ctx->mem_cb.udata, ctx->name, total_size, +RTE_CACHE_LINE_SIZE , ctx->socket_id);if (mem ==NULL ) { ACL_LOG(ERR, "allocation of %zu bytes on socket %d for %s failed",
lib/acl/rte_acl.c +34 -1
@@ -264,6 +264,20 @@ acl_get_best_alg(void)return alg[i]; } +static void * +zalloc_dft (void *udata,char *name,size_t size,size_t align,int32_t socket_id) +{ + (void )udata; +return rte_zmalloc_socket (name, size, align, socket_id); +} + +static void +free_dft (void *udata,void *ptr) +{ + (void )udata; +rte_free (ptr); +} +RTE_EXPORT_SYMBOL (rte_acl_set_ctx_classify) extern intrte_acl_set_ctx_classify (struct rte_acl_ctx *ctx,enum rte_acl_classify_alg alg)
@@ -362,7 +376,7 @@ rte_acl_free(struct rte_acl_ctx *ctx)rte_mcfg_tailq_write_unlock (); -rte_free (ctx->mem); + ctx->mem_cb.free (ctx->mem_cb.udata, ctx->mem);rte_free (ctx);rte_free (te); }
@@ -425,6 +439,9 @@ rte_acl_create(const struct rte_acl_param *param) ctx->rule_sz = param->rule_size; ctx->socket_id = param->socket_id; ctx->alg =acl_get_best_alg (); + ctx->mem_cb.zalloc = zalloc_dft; + ctx->mem_cb.free = free_dft; + ctx->mem_cb.udata =NULL ;strlcpy (ctx->name, param->name,sizeof (ctx->name)); te->data = (void *) ctx;
@@ -555,3 +572,19 @@ rte_acl_list_dump(void) }rte_mcfg_tailq_read_unlock (); } + +int rte_acl_set_mem_cb (struct rte_acl_ctx *acl,const struct rte_acl_mem_cb *mcb) +{ +if (!acl || !mcb || !mcb->zalloc || !mcb->free) +return -EINVAL ; +memcpy (&acl->mem_cb, mcb,sizeof (struct rte_acl_mem_cb )); +return 0 ; +} + +int rte_acl_get_mem_cb (const struct rte_acl_ctx *acl,struct rte_acl_mem_cb *mcb) +{ +if (!acl || !mcb) +return -EINVAL ; +memcpy (mcb, &acl->mem_cb,sizeof (struct rte_acl_mem_cb )); +return 0 ; +}
lib/acl/rte_acl.h +42 -0
48 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v4] acl: support custom memory allocators in rte_acl_build
Recheck-request: iol-intel-Performance
Putting in a retest due to an infra fail at UNH. No action on your end is needed.
Re: [PATCH v4] acl: support custom memory allocators in rte_acl_build
In reply to mannywang(王永峰)app/test/test_acl.c test_u32_range(void) · line 1737
Quoted 6 lines · On Mon, 1 Dec 2025 12:05:07 +0000 "mannywang(王永峰)" <[email protected]>…
app/test/test_acl.c +6
-0+static void *running_alloc (void *udata,char *name,size_t size, +size_t align,int32_t socket_id) +{ + (void )align; + (void )name; + (void )socket_id;
Please use __rte_unused attribute or RTE_SET_USED() macro for this.
Re: [Internet]Re: [PATCH v4] acl: support custom memory allocators in rte_acl_build
Thanks a lot for your review and feedback.
The comments have been addressed in this version.
Quoted 12 lines · Stephen Hemminger
app/test/test_acl.c test_u32_range(void) · line 1737
Quoted 6 lines · On Mon, 1 Dec 2025 12:05:07 +0000 "mannywang(王永峰)" <[email protected]>…
app/test/test_acl.c +6
-0+static void *running_alloc (void *udata,char *name,size_t size, +size_t align,int32_t socket_id) +{ + (void )align; + (void )name; + (void )socket_id;Please use
__rte_unusedattribute orRTE_SET_USED()macro for this.
PATCH v5 1/1 acl: support custom memory allocators in rte_acl_build
In reply to Konstantin AnanyevAllow users to provide custom
memory allocation callbacks for runtime memory in rte_acl_ctx, via struct rte_acl_mem_cb.
Key changes:
- Added struct rte_acl_mem_cb with zalloc, free, and udata.
- Added rte_acl_set_mem_cb() / rte_acl_get_mem_cb() to set/get callbacks.
- Default allocation uses existing rte_zmalloc_socket/rte_free.
- Modified ACL code to call callbacks for runtime allocations instead of rte_zmalloc_socket/rte_free directly.
v5:
- Remove temporary memory allocation callback for build stage.
- Introduce new API (rte_acl_set_mem_cb / rte_acl_get_mem_cb) instead of modifying existing rte_acl_config to preserve ABI compatibility.
--- app/test/test_acl.c | 121 ++++++++++++++++++++++++++++++++++++++++++++ lib/acl/acl.h | 1 + lib/acl/acl_bld.c | 2 +- lib/acl/acl_gen.c | 4 +- lib/acl/rte_acl.c | 45 +++++++++++++++- lib/acl/rte_acl.h | 42 +++++++++++++++ 6 files changed, 211 insertions(+), 4 deletions(-)
app/test/test_acl.c +121 -0
@@ -1721,6 +1721,125 @@ test_u32_range(void)return rc; } +struct acl_ctx_wrapper_t { +struct rte_acl_ctx *ctx; +void *running_buf; +bool running_buf_using; +}; + +#define ACL_RUNNING_BUF_SIZE (10 * 1024 * 1024) + +static void *running_alloc (void *udata,char *name,size_t size, +size_t align,int32_t socket_id) +{ + (void )align; + (void )name; + (void )socket_id; +if (size >ACL_RUNNING_BUF_SIZE ) +return NULL ; +struct acl_ctx_wrapper_t *gwlb_acl_ctx = (struct acl_ctx_wrapper_t *)udata; +if (gwlb_acl_ctx->running_buf_using) +return NULL ; +printf ("running memory alloc for acl context, size=%zu, pointer=%p\n" , + size, + gwlb_acl_ctx->running_buf); +memset (gwlb_acl_ctx->running_buf,0 , size); + gwlb_acl_ctx->running_buf_using = true; +return gwlb_acl_ctx->running_buf; +} + +static void running_free (void *udata,void *ptr) +{ +if (!ptr) +return ; +struct acl_ctx_wrapper_t *gwlb_acl_ctx = (struct acl_ctx_wrapper_t *)udata; +printf ("running memory free, pointer=%p\n" , ptr); + gwlb_acl_ctx->running_buf_using = false; +} + +static int +test_mem_cb (void ) +{ +int i, ret; +struct acl_ctx_wrapper_t g_acl_ctx_wrapper; + g_acl_ctx_wrapper.ctx =rte_acl_create (&acl_param); +if (g_acl_ctx_wrapper.ctx ==NULL ) { +printf ("Line %i: Error creating ACL context!\n" , __LINE__); +return -1 ; + } + g_acl_ctx_wrapper.running_buf =rte_zmalloc_socket ( +"test_acl" , +ACL_RUNNING_BUF_SIZE , +RTE_CACHE_LINE_SIZE , +SOCKET_ID_ANY ); +if (!g_acl_ctx_wrapper.running_buf) { +printf ("Line %i: Error allocing running buf for acl context!\n" , __LINE__); +return 1 ; + } + g_acl_ctx_wrapper.running_buf_using = false; + +struct rte_acl_mem_cb mcb = { + .zalloc = running_alloc, + .free = running_free, + .udata = &g_acl_ctx_wrapper + }; + ret =rte_acl_set_mem_cb (g_acl_ctx_wrapper.ctx, &mcb); +if (ret) { +printf ("Line %i: Error set mem cb for acl context!\n" , __LINE__); +return 1 ; + } +struct rte_acl_mem_cb new_mcb; +memset (&new_mcb,0 ,sizeof (struct rte_acl_mem_cb )); + ret =rte_acl_get_mem_cb (g_acl_ctx_wrapper.ctx, &new_mcb); +if (ret) { +printf ("Line %i: Error get mem cb for acl context!\n" , __LINE__); +return 1 ; + } +if (memcmp (&mcb, &new_mcb,sizeof (struct rte_acl_mem_cb )) !=0 ) { +printf ("Line %i: Error get mem cb for acl context!\n" , __LINE__); +return 1 ; + } + ret =0 ; +for (i =0 ; i !=TEST_CLASSIFY_ITER ; i++) { + +if ((i &1 ) ==0 ) +rte_acl_reset (g_acl_ctx_wrapper.ctx); +else +rte_acl_reset_rules (g_acl_ctx_wrapper.ctx); + + ret =test_classify_buid (g_acl_ctx_wrapper.ctx, acl_test_rules, +RTE_DIM (acl_test_rules)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: " +"Adding rules to ACL context failed!\n" , + __LINE__, i); +break ; + } + + ret =test_classify_run (g_acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + +/* reset rules and make sure that classify still works ok. */ +rte_acl_reset_rules (g_acl_ctx_wrapper.ctx); + ret =test_classify_run (g_acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + } + +rte_acl_free (g_acl_ctx_wrapper.ctx); +rte_free (g_acl_ctx_wrapper.running_buf); +return ret; +} +static int test_acl (void ) {
@@ -1742,6 +1861,8 @@ test_acl(void)return -1 ;if (test_u32_range () <0 )return -1 ; +if (test_mem_cb () <0 ) +return -1 ;return 0 ; }
lib/acl/acl.h +1 -0
@@ -174,6 +174,7 @@ struct rte_acl_ctx {uint32_t max_rules;uint32_t rule_sz;uint32_t num_rules; +struct rte_acl_mem_cb mem_cb;uint32_t num_categories;uint32_t num_tries;uint32_t match_index;
lib/acl/acl_bld.c +1 -1
@@ -779,7 +779,7 @@ acl_merge_trie(struct acl_build_context *context,static void acl_build_reset (struct rte_acl_ctx *ctx) { -rte_free (ctx->mem); + ctx->mem_cb.free (ctx->mem_cb.udata, ctx->mem);memset (&ctx->num_categories,0 ,sizeof (*ctx) - offsetof(struct rte_acl_ctx , num_categories)); }
lib/acl/acl_gen.c +2 -2
@@ -478,8 +478,8 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie,return -ERANGE ; } - mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , - ctx->socket_id); + mem = ctx->mem_cb.zalloc (ctx->mem_cb.udata, ctx->name, total_size, +RTE_CACHE_LINE_SIZE , ctx->socket_id);if (mem ==NULL ) { ACL_LOG(ERR, "allocation of %zu bytes on socket %d for %s failed",
lib/acl/rte_acl.c +44 -1
66 lines not on this page · raw message
lib/acl/rte_acl.h +42 -0
48 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v5] acl: support custom memory allocators in rte_acl_build
commit header could simply as: acl: support custom memory allocator
Quoted 31 lines · mannywang(王永峰)
Allow users to provide custom
memory allocation callbacks for runtime memory inrte_acl_ctx, via structrte_acl_mem_cb.Key changes:
- Added structrte_acl_mem_cbwith zalloc, free, and udata.
- Addedrte_acl_set_mem_cb()/rte_acl_get_mem_cb()to set/get callbacks.
- Default allocation uses existing rte_zmalloc_socket/rte_free.
- Modified ACL code to call callbacks for runtime allocations instead of rte_zmalloc_socket/rte_free directly.v5:
- Remove temporary memory allocation callback for build stage.
- Introduce new API (rte_acl_set_mem_cb/rte_acl_get_mem_cb) instead of modifying existingrte_acl_configto preserve ABI compatibility.Signed-off-by YongFeng Wang <[email protected]>--- app/test/test_acl.c | 121 ++++++++++++++++++++++++++++++++++++++++++++ lib/acl/acl.h | 1 + lib/acl/acl_bld.c | 2 +- lib/acl/acl_gen.c | 4 +- lib/acl/rte_acl.c | 45 +++++++++++++++- lib/acl/rte_acl.h | 42 +++++++++++++++ 6 files changed, 211 insertions(+), 4 deletions(-)app/test/test_acl.c +0
-0
app/test/test_acl.c test_u32_range(void) · line 1724
Quoted 4 lines
app/test/test_acl.c +1
-0@@ -1721,6 +1721,125 @@ test_u32_range(void)return rc; } +struct acl_ctx_wrapper_t {
no need add post-fix _t
Quoted 6 lines
app/test/test_acl.c +6
-0+struct rte_acl_ctx *ctx; +void *running_buf; +bool running_buf_using; +}; + +#define ACL_RUNNING_BUF_SIZE (10 * 1024 * 1024)
app/test/test_acl.c test_u32_range(void) · line 1740
Quoted 10 lines
app/test/test_acl.c +10
-0+ +static void *running_alloc (void *udata,char *name,size_t size, +size_t align,int32_t socket_id) +{ + (void )align; + (void )name; + (void )socket_id; +if (size >ACL_RUNNING_BUF_SIZE ) +return NULL ; +struct acl_ctx_wrapper_t *gwlb_acl_ctx = (struct acl_ctx_wrapper_t *)udata;
gwlb - what the meaning? suggest simple acl_ctx or just ctx.
app/test/test_acl.c test_u32_range(void) · line 1747
Quoted 7 lines
app/test/test_acl.c +7
-0+if (gwlb_acl_ctx->running_buf_using) +return NULL ; +printf ("running memory alloc for acl context, size=%zu, pointer=%p\n" , + size, + gwlb_acl_ctx->running_buf); +memset (gwlb_acl_ctx->running_buf,0 , size); + gwlb_acl_ctx->running_buf_using = true;
this allocator don't support conti alloc, it will has dependent for acl-library, how about use Dmitry Kozlyuk's comment for [PATCH v3], that is use rte_malloc_heap_create
Quoted 7 lines
app/test/test_acl.c +7
-0+return gwlb_acl_ctx->running_buf; +} + +static void running_free (void *udata,void *ptr) +{ +if (!ptr) +return ;
app/test/test_acl.c test_u32_range(void) · line 1764
Quoted 10 lines
app/test/test_acl.c +10
-0+struct acl_ctx_wrapper_t *gwlb_acl_ctx = (struct acl_ctx_wrapper_t *)udata; +printf ("running memory free, pointer=%p\n" , ptr); + gwlb_acl_ctx->running_buf_using = false; +} + +static int +test_mem_cb (void ) +{ +int i, ret; +struct acl_ctx_wrapper_t g_acl_ctx_wrapper;
no need add g_acl_ prefix, and suggest zero default (struct acl_ctx_wrapper ctx_wrap = {0})
app/test/test_acl.c test_u32_range(void) · line 1776
Quoted 12 lines
app/test/test_acl.c +12
-0+ g_acl_ctx_wrapper.ctx =rte_acl_create (&acl_param); +if (g_acl_ctx_wrapper.ctx ==NULL ) { +printf ("Line %i: Error creating ACL context!\n" , __LINE__); +return -1 ; + } + g_acl_ctx_wrapper.running_buf =rte_zmalloc_socket ( +"test_acl" , +ACL_RUNNING_BUF_SIZE , +RTE_CACHE_LINE_SIZE , +SOCKET_ID_ANY ); +if (!g_acl_ctx_wrapper.running_buf) { +printf ("Line %i: Error allocing running buf for acl context!\n" , __LINE__);
please add resource rollback, the same as following.
app/test/test_acl.c test_u32_range(void) · line 1787
Quoted 11 lines
app/test/test_acl.c +11
-0+return 1 ; + } + g_acl_ctx_wrapper.running_buf_using = false; + +struct rte_acl_mem_cb mcb = { + .zalloc = running_alloc, + .free = running_free, + .udata = &g_acl_ctx_wrapper + }; + ret =rte_acl_set_mem_cb (g_acl_ctx_wrapper.ctx, &mcb); +if (ret) {
please use explicit exp, e.g. if (ret != 0), the same as other place.
Quoted 6 lines
app/test/test_acl.c +6
-0+printf ("Line %i: Error set mem cb for acl context!\n" , __LINE__); +return 1 ; + } +struct rte_acl_mem_cb new_mcb; +memset (&new_mcb,0 ,sizeof (struct rte_acl_mem_cb )); + ret =rte_acl_get_mem_cb (g_acl_ctx_wrapper.ctx, &new_mcb);
app/test/test_acl.c test_u32_range(void) · line 1803
Quoted 10 lines
app/test/test_acl.c +10
-0+if (ret) { +printf ("Line %i: Error get mem cb for acl context!\n" , __LINE__); +return 1 ; + } +if (memcmp (&mcb, &new_mcb,sizeof (struct rte_acl_mem_cb )) !=0 ) { +printf ("Line %i: Error get mem cb for acl context!\n" , __LINE__); +return 1 ; + } + ret =0 ; +for (i =0 ; i !=TEST_CLASSIFY_ITER ; i++) {
why not i < TEST_CLASSIFY_ITER ?
app/test/test_acl.c test_u32_range(void) · line 1814
Quoted 11 lines
app/test/test_acl.c +11
-0+ +if ((i &1 ) ==0 ) +rte_acl_reset (g_acl_ctx_wrapper.ctx); +else +rte_acl_reset_rules (g_acl_ctx_wrapper.ctx); + + ret =test_classify_buid (g_acl_ctx_wrapper.ctx, acl_test_rules, +RTE_DIM (acl_test_rules)); +if (ret !=0 ) { + printf("Line %i, iter: %d: " + "Adding rules to ACL context failed!\n",
no need extra line for log-string.
Quoted 45 lines
app/test/test_acl.c +30
-0+ __LINE__, i); +break ; + } + + ret =test_classify_run (g_acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + +/* reset rules and make sure that classify still works ok. */ +rte_acl_reset_rules (g_acl_ctx_wrapper.ctx); + ret =test_classify_run (g_acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + } + +rte_acl_free (g_acl_ctx_wrapper.ctx); +rte_free (g_acl_ctx_wrapper.running_buf); +return ret; +} +static int test_acl (void ) {@@ -1742,6 +1861,8 @@ test_acl(void)return -1 ;if (test_u32_range () <0 )return -1 ; +if (test_mem_cb () <0 ) +return -1 ;return 0 ; }lib/acl/acl.h +0
-0
lib/acl/acl.h struct rte_acl_ctx { · line 177
Quoted 4 lines
lib/acl/acl.h +1
-0@@ -174,6 +174,7 @@ struct rte_acl_ctx {uint32_t max_rules;uint32_t rule_sz;uint32_t num_rules; +struct rte_acl_mem_cb mem_cb;
why not place near *mem/mem_sz field ?
Quoted 36 lines
lib/acl/acl.h +0
-0uint32_t num_categories;uint32_t num_tries;uint32_t match_index;lib/acl/acl_bld.c +1
-1@@ -779,7 +779,7 @@ acl_merge_trie(struct acl_build_context *context,static void acl_build_reset (struct rte_acl_ctx *ctx) { -rte_free (ctx->mem); + ctx->mem_cb.free (ctx->mem_cb.udata, ctx->mem);memset (&ctx->num_categories,0 ,sizeof (*ctx) - offsetof(struct rte_acl_ctx , num_categories)); }lib/acl/acl_gen.c +2
-2@@ -478,8 +478,8 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie,return -ERANGE ; } - mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , - ctx->socket_id); + mem = ctx->mem_cb.zalloc (ctx->mem_cb.udata, ctx->name, total_size, +RTE_CACHE_LINE_SIZE , ctx->socket_id);if (mem ==NULL ) { ACL_LOG(ERR, "allocation of %zu bytes on socket %d for %s failed",lib/acl/rte_acl.c +0
-0
lib/acl/rte_acl.c acl_get_best_alg(void) · line 268
Quoted 5 lines
lib/acl/rte_acl.c +2
-0@@ -264,6 +264,20 @@ acl_get_best_alg(void)return alg[i]; } +static void * +zalloc_dft (void *udata,char *name,size_t size,size_t align,int32_t socket_id)
how about acl_mem_default_zalloc?
lib/acl/rte_acl.c acl_get_best_alg(void) · line 275
Quoted 7 lines
lib/acl/rte_acl.c +7
-0+{ + (void )udata; +return rte_zmalloc_socket (name, size, align, socket_id); +} + +static void +free_dft (void *udata,void *ptr)
how about acl_mem_default_free?
Quoted 28 lines
lib/acl/rte_acl.c +9
-1+{ + (void )udata; +rte_free (ptr); +} +RTE_EXPORT_SYMBOL (rte_acl_set_ctx_classify) extern intrte_acl_set_ctx_classify (struct rte_acl_ctx *ctx,enum rte_acl_classify_alg alg)@@ -362,7 +376,7 @@ rte_acl_free(struct rte_acl_ctx *ctx)rte_mcfg_tailq_write_unlock (); -rte_free (ctx->mem); + ctx->mem_cb.free (ctx->mem_cb.udata, ctx->mem);rte_free (ctx);rte_free (te); }@@ -425,6 +439,9 @@ rte_acl_create(const struct rte_acl_param *param) ctx->rule_sz = param->rule_size; ctx->socket_id = param->socket_id; ctx->alg =acl_get_best_alg (); + ctx->mem_cb.zalloc = zalloc_dft; + ctx->mem_cb.free = free_dft; + ctx->mem_cb.udata =NULL ;strlcpy (ctx->name, param->name,sizeof (ctx->name)); te->data = (void *) ctx;
lib/acl/rte_acl.c rte_acl_list_dump(void) · line 579
Quoted 8 lines
lib/acl/rte_acl.c +5
-0@@ -555,3 +572,29 @@ rte_acl_list_dump(void) }rte_mcfg_tailq_read_unlock (); } + +/* +* Set memory allocation callbacks for a given ACL context. +*/ +RTE_EXPORT_SYMBOL (rte_acl_set_mem_cb)
Should be RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_acl_set_mem_cb, 26.03)
lib/acl/rte_acl.c rte_acl_list_dump(void) · line 583
Quoted 4 lines
lib/acl/rte_acl.c +4
-0+int +rte_acl_set_mem_cb (struct rte_acl_ctx *acl,const struct rte_acl_mem_cb *mcb) +{ +if (!acl || !mcb || !mcb->zalloc || !mcb->free)
suggest acl == NULL || mcb == NULL and ...
lib/acl/rte_acl.c rte_acl_list_dump(void) · line 592
Quoted 9 lines
lib/acl/rte_acl.c +9
-0+return -EINVAL ; +memcpy (&acl->mem_cb, mcb,sizeof (struct rte_acl_mem_cb )); +return 0 ; +} + +/* +* Retrieve the memory allocation callbacks assigned to the ACL context. +*/ +RTE_EXPORT_SYMBOL (rte_acl_get_mem_cb)
Should be RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_acl_get_mem_cb, 26.03)
lib/acl/rte_acl.c rte_acl_list_dump(void) · line 596
Quoted 4 lines
lib/acl/rte_acl.c +4
-0+int +rte_acl_get_mem_cb (const struct rte_acl_ctx *acl,struct rte_acl_mem_cb *mcb) +{ +if (!acl || !mcb)
suggest acl == NULL || mcb == NULL
Quoted 9 lines
lib/acl/rte_acl.c +4
-0+return -EINVAL ; +memcpy (mcb, &acl->mem_cb,sizeof (struct rte_acl_mem_cb )); +return 0 ; +}lib/acl/rte_acl.h +0
-0
lib/acl/rte_acl.h struct rte_acl_param { · line 142
Quoted 7 lines
lib/acl/rte_acl.h +4
-0@@ -136,6 +136,48 @@ struct rte_acl_param {/** @internal opaque ACL handle */ struct rte_acl_ctx ; +/** +* Memory allocation callbacks for ACL runtime. +*/ +struct rte_acl_mem_cb {
How about rte_acl_mem_hook ?
I think hook is better suited to this semantics.
lib/acl/rte_acl.h struct rte_acl_param { · line 144
Quoted 2 lines
lib/acl/rte_acl.h +2
-0+/** Allocate zero-initialized memory used during runtime. */ +void *(*zalloc)(void *udata,char *name,size_t size,size_t align,int32_t socket_id);
Suggest move udata as the last one parameter.
lib/acl/rte_acl.h struct rte_acl_param { · line 147
Quoted 3 lines
lib/acl/rte_acl.h +3
-0+ +/** Free memory previously allocated by zalloc(). */ +void (*free)(void *udata,void *ptr);
Suggest move udata as the last one parameter.
lib/acl/rte_acl.h struct rte_acl_param { · line 159
Quoted 12 lines
lib/acl/rte_acl.h +12
-0+ +/** User-provided context passed to allocation/free callbacks. */ +void *udata; +}; + +/** +* Set memory allocation callbacks for a given ACL context. +* +* @param acl +* The ACL context. +* @param mcb +* Pointer to the memory callback structure
Suggest add one note about when could invoke this API.
lib/acl/rte_acl.h struct rte_acl_param { · line 164
Quoted 5 lines
lib/acl/rte_acl.h +5
-0+* +* @return +* 0 on success. +* -EINVAL if parameters are invalid. +*/
should add __rte_experimental
Quoted 4 lines
lib/acl/rte_acl.h +4
-0+int rte_acl_set_mem_cb (struct rte_acl_ctx *acl,const struct rte_acl_mem_cb *mcb); + +/** +* Retrieve the memory allocation callbacks assigned to the ACL context.
lib/acl/rte_acl.h struct rte_acl_param { · line 178
Quoted 10 lines
lib/acl/rte_acl.h +10
-0+* +* @param acl +* The ACL context. +* @param mcb +* Output location for the current memory callback structure +* +* @return +* 0 on success. +* -EINVAL if parameters are invalid. +*/
should add __rte_experimental
lib/acl/rte_acl.h struct rte_acl_param { · line 183
Quoted 5 lines
lib/acl/rte_acl.h +2
-0+int rte_acl_get_mem_cb (const struct rte_acl_ctx *acl,struct rte_acl_mem_cb *mcb); +/** * De-allocate all memory used by ACL context. *
please add descriptor section for packet_classif_access_ctrl.rst and release note.
PATCH v6 1/1 acl: support custom memory allocators
Allow users to provide custom
memory allocation hooks for runtime memory in rte_acl_ctx, via struct rte_acl_mem_hook.
Key changes:
- Added struct rte_acl_mem_hook with zalloc, free, and udata.
- Added rte_acl_set_mem_hook / rte_acl_get_mem_hook to set/get callbacks.
- Default allocation uses existing rte_zmalloc_socket/rte_free.
- Modified ACL code to call callbacks for runtime allocations instead of rte_zmalloc_socket/rte_free directly.
v5:
- Remove temporary memory allocation callback for build stage.
- Introduce new API (rte_acl_set_mem_hook / rte_acl_get_mem_hook) instead of modifying existing rte_acl_config to preserve ABI compatibility.
v6:
- Reworked API to meet consistency and naming conventions.
- Adjusted parameter order for better readability and alignment.
- Renamed internal variables for clarity and code consistency.
--- app/test/test_acl.c | 121 ++++++++++++++++++ .../prog_guide/packet_classif_access_ctrl.rst | 31 +++++ lib/acl/acl.h | 1 + lib/acl/acl_bld.c | 2 +- lib/acl/acl_gen.c | 4 +- lib/acl/rte_acl.c | 45 ++++++- lib/acl/rte_acl.h | 47 +++++++ 7 files changed, 247 insertions(+), 4 deletions(-)
app/test/test_acl.c +121 -0
@@ -1721,6 +1721,125 @@ test_u32_range(void)return rc; } +struct acl_ctx_wrapper { +struct rte_acl_ctx *ctx; +void *running_buf; +bool running_buf_using; +}; + +#define ACL_RUNNING_BUF_SIZE (10 * 1024 * 1024) + +static void *running_alloc (char *name,size_t size, +size_t align,int32_t socket_id,void *udata) +{ +RTE_SET_USED (align); +RTE_SET_USED (name); +RTE_SET_USED (socket_id); +if (size >ACL_RUNNING_BUF_SIZE ) +return NULL ; +struct acl_ctx_wrapper *acl_ctx = (struct acl_ctx_wrapper *)udata; +if (acl_ctx->running_buf_using) +return NULL ; +printf ("running memory alloc for acl context, size=%zu, pointer=%p\n" , + size, + acl_ctx->running_buf); +memset (acl_ctx->running_buf,0 , size); + acl_ctx->running_buf_using = true; +return acl_ctx->running_buf; +} + +static void running_free (void *ptr,void *udata) +{ +if (!ptr) +return ; +struct acl_ctx_wrapper *acl_ctx = (struct acl_ctx_wrapper *)udata; +printf ("running memory free, pointer=%p\n" , ptr); + acl_ctx->running_buf_using = false; +} + +static int +test_mem_hook (void ) +{ +int i, ret; +struct acl_ctx_wrapper acl_ctx_wrapper = {0 }; + acl_ctx_wrapper.ctx =rte_acl_create (&acl_param); +if (acl_ctx_wrapper.ctx ==NULL ) { +printf ("Line %i: Error creating ACL context!\n" , __LINE__); +return -1 ; + } + acl_ctx_wrapper.running_buf =rte_zmalloc_socket ( +"test_acl" , +ACL_RUNNING_BUF_SIZE , +RTE_CACHE_LINE_SIZE , +SOCKET_ID_ANY ); +if (!acl_ctx_wrapper.running_buf) { +rte_acl_free (acl_ctx_wrapper.ctx); +printf ("Line %i: Error allocing running buf for acl context!\n" , __LINE__); +return 1 ; + } + acl_ctx_wrapper.running_buf_using = false; + +struct rte_acl_mem_hook mhook = { + .zalloc = running_alloc, + .free = running_free, + .udata = &acl_ctx_wrapper + }; + ret =rte_acl_set_mem_hook (acl_ctx_wrapper.ctx, &mhook); +if (ret !=0 ) { +printf ("Line %i: Error set mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx_wrapper.ctx); +rte_free (acl_ctx_wrapper.running_buf); +return 1 ; + } +struct rte_acl_mem_hook new_hook; +memset (&new_hook,0 ,sizeof (struct rte_acl_mem_hook )); +if (rte_acl_get_mem_hook (acl_ctx_wrapper.ctx, &new_hook) !=0 + ||memcmp (&mhook, &new_hook,sizeof (struct rte_acl_mem_hook )) !=0 ) { +printf ("Line %i: Error get mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx_wrapper.ctx); +rte_free (acl_ctx_wrapper.running_buf); +return 1 ; + } + ret =0 ; +for (i =0 ; i <TEST_CLASSIFY_ITER ; i++) { + +if ((i &1 ) ==0 ) +rte_acl_reset (acl_ctx_wrapper.ctx); +else +rte_acl_reset_rules (acl_ctx_wrapper.ctx); + + ret =test_classify_buid (acl_ctx_wrapper.ctx, acl_test_rules, +RTE_DIM (acl_test_rules)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: Adding rules to ACL context failed!\n" , + __LINE__, i); +break ; + } + + ret =test_classify_run (acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + +/* reset rules and make sure that classify still works ok. */ +rte_acl_reset_rules (acl_ctx_wrapper.ctx); + ret =test_classify_run (acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + } + +rte_acl_free (acl_ctx_wrapper.ctx); +rte_free (acl_ctx_wrapper.running_buf); +return ret; +} +static int test_acl (void ) {
@@ -1742,6 +1861,8 @@ test_acl(void)return -1 ;if (test_u32_range () <0 )return -1 ; +if (test_mem_hook () <0 ) +return -1 ;return 0 ; }
doc/guides/prog_guide/packet_classif_access_ctrl.rst +31 -0
@@ -359,7 +359,38 @@ For example: ret = rte_acl_build(acx, &cfg); } +Custom Memory Hooks +~~~~~~~~~~~~~~~~~~~~ + +The ACL library supports custom memory allocation for runtime structures. +Applications can supply their own memory hooks through: + +.. code-block:: c + + int rte_acl_set_mem_hook(struct rte_acl_ctx *ctx, + const struct rte_acl_mem_hook *mhook); + + int rte_acl_get_mem_hook(const struct rte_acl_ctx *ctx, + struct rte_acl_mem_hook *mhook); + +The ``rte_acl_mem_hook`` structure defines memory hooks: + +.. code-block:: c + + struct rte_acl_mem_hook { + /** Allocate zero-initialized memory used during runtime. */ + void *(*zalloc)(char *name, size_t size, size_t align, int32_t socket_id, void *udata); + + /** Free memory previously allocated by zalloc(). */ + void (*free)(void *ptr, void *udata); + + /** User-provided context passed to allocation/free hooks. */ + void *udata; + }; + +Applications may use these hooks to allocate memory from custom pools or pre-allocated buffers. +If no memory hook is provided, the ACL library uses rte_zmalloc_socket() internally. Classification methods ~~~~~~~~~~~~~~~~~~~~~~
lib/acl/acl.h +1 -0
@@ -174,6 +174,7 @@ struct rte_acl_ctx {uint32_t max_rules;uint32_t rule_sz;uint32_t num_rules; +struct rte_acl_mem_hook mem_hook;uint32_t num_categories;uint32_t num_tries;uint32_t match_index;
lib/acl/acl_bld.c +1 -1
@@ -779,7 +779,7 @@ acl_merge_trie(struct acl_build_context *context,static void acl_build_reset (struct rte_acl_ctx *ctx) { -rte_free (ctx->mem); + ctx->mem_hook.free (ctx->mem, ctx->mem_hook.udata);memset (&ctx->num_categories,0 ,sizeof (*ctx) - offsetof(struct rte_acl_ctx , num_categories)); }
lib/acl/acl_gen.c +2 -2
@@ -478,8 +478,8 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie,return -ERANGE ; } - mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , - ctx->socket_id); + mem = ctx->mem_hook.zalloc (ctx->name, total_size, +RTE_CACHE_LINE_SIZE , ctx->socket_id, ctx->mem_hook.udata);if (mem ==NULL ) { ACL_LOG(ERR, "allocation of %zu bytes on socket %d for %s failed",
lib/acl/rte_acl.c +44 -1
66 lines not on this page · raw message
lib/acl/rte_acl.h +47 -0
53 lines not on this page · raw message
Signature · 1 line
2.43.0
RE: [PATCH v6] acl: support custom memory allocators
Quoted 5 lines
Allow users to provide custom
memory allocation hooks for runtime memory inrte_acl_ctx, via structrte_acl_mem_hook.
LGTM in general, few extra comments below.
Quoted 53 lines
Key changes:
- Added structrte_acl_mem_hookwith zalloc, free, and udata.
- Addedrte_acl_set_mem_hook/rte_acl_get_mem_hookto set/get callbacks.
- Default allocation uses existing rte_zmalloc_socket/rte_free.
- Modified ACL code to call callbacks for runtime allocations instead of rte_zmalloc_socket/rte_free directly.v5:
- Remove temporary memory allocation callback for build stage.
- Introduce new API (rte_acl_set_mem_hook/rte_acl_get_mem_hook) instead of modifying existingrte_acl_configto preserve ABI compatibility.v6:
- Reworked API to meet consistency and naming conventions.
- Adjusted parameter order for better readability and alignment.
- Renamed internal variables for clarity and code consistency.Signed-off-by YongFeng Wang <[email protected]>--- app/test/test_acl.c | 121 ++++++++++++++++++ .../prog_guide/packet_classif_access_ctrl.rst | 31 +++++ lib/acl/acl.h | 1 + lib/acl/acl_bld.c | 2 +- lib/acl/acl_gen.c | 4 +- lib/acl/rte_acl.c | 45 ++++++- lib/acl/rte_acl.h | 47 +++++++ 7 files changed, 247 insertions(+), 4 deletions(-)app/test/test_acl.c +16
-0@@ -1721,6 +1721,125 @@ test_u32_range(void)return rc; } +struct acl_ctx_wrapper { +struct rte_acl_ctx *ctx; +void *running_buf; +bool running_buf_using; +}; + +#define ACL_RUNNING_BUF_SIZE (10 * 1024 * 1024) + +static void *running_alloc (char *name,size_t size, +size_t align,int32_t socket_id,void *udata) +{ +RTE_SET_USED (align); +RTE_SET_USED (name); +RTE_SET_USED (socket_id); +if (size >ACL_RUNNING_BUF_SIZE ) +return NULL ;
app/test/test_acl.c test_u32_range(void) · line 1749
Quoted 10 lines
app/test/test_acl.c +10
-0+struct acl_ctx_wrapper *acl_ctx = (struct acl_ctx_wrapper *)udata; +if (acl_ctx->running_buf_using) +return NULL ; +printf ("running memory alloc for acl context, size=%zu, pointer=%p\n" , + size, + acl_ctx->running_buf); +memset (acl_ctx->running_buf,0 , size); + acl_ctx->running_buf_using = true; +return acl_ctx->running_buf; +}
Is there any point to have such memhook in our UT?
From one side: it doesn't test anything new, as memory is still allocsted via rte_zmalloc(). From other side it is error prone, as you don't check that pre-allocated buffer will really satisfy requested size and alignment parameters.
Might be just use libc malloc/free here?
Quoted 305 lines
app/test/test_acl.c +49
-0+ +static void running_free (void *ptr,void *udata) +{ +if (!ptr) +return ; +struct acl_ctx_wrapper *acl_ctx = (struct acl_ctx_wrapper *)udata; +printf ("running memory free, pointer=%p\n" , ptr); + acl_ctx->running_buf_using = false; +} + +static int +test_mem_hook (void ) +{ +int i, ret; +struct acl_ctx_wrapper acl_ctx_wrapper = {0 }; + acl_ctx_wrapper.ctx =rte_acl_create (&acl_param); +if (acl_ctx_wrapper.ctx ==NULL ) { +printf ("Line %i: Error creating ACL context!\n" , __LINE__); +return -1 ; + } + acl_ctx_wrapper.running_buf =rte_zmalloc_socket ( +"test_acl" , +ACL_RUNNING_BUF_SIZE , +RTE_CACHE_LINE_SIZE , +SOCKET_ID_ANY ); +if (!acl_ctx_wrapper.running_buf) { +rte_acl_free (acl_ctx_wrapper.ctx); +printf ("Line %i: Error allocing running buf for acl context!\n" , __LINE__); +return 1 ; + } + acl_ctx_wrapper.running_buf_using = false; + +struct rte_acl_mem_hook mhook = { + .zalloc = running_alloc, + .free = running_free, + .udata = &acl_ctx_wrapper + }; + ret =rte_acl_set_mem_hook (acl_ctx_wrapper.ctx, &mhook); +if (ret !=0 ) { +printf ("Line %i: Error set mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx_wrapper.ctx); +rte_free (acl_ctx_wrapper.running_buf); +return 1 ; + } +struct rte_acl_mem_hook new_hook; +memset (&new_hook,0 ,sizeof (struct rte_acl_mem_hook )); +if (rte_acl_get_mem_hook (acl_ctx_wrapper.ctx, &new_hook) !=0 + ||memcmp (&mhook, &new_hook,sizeof (struct rte_acl_mem_hook )) !=0 ) { + printf("Line %i: Error get mem hook for acl context!\n",
__LINE__);app/test/test_acl.c +16
-0+rte_acl_free (acl_ctx_wrapper.ctx); +rte_free (acl_ctx_wrapper.running_buf); +return 1 ; + } + ret =0 ; +for (i =0 ; i <TEST_CLASSIFY_ITER ; i++) { + +if ((i &1 ) ==0 ) +rte_acl_reset (acl_ctx_wrapper.ctx); +else +rte_acl_reset_rules (acl_ctx_wrapper.ctx); + + ret =test_classify_buid (acl_ctx_wrapper.ctx, acl_test_rules, +RTE_DIM (acl_test_rules)); +if (ret !=0 ) { + printf("Line %i, iter: %d: Adding rules to ACL contextfailed!\n",
app/test/test_acl.c +30
-0+ __LINE__, i); +break ; + } + + ret =test_classify_run (acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + +/* reset rules and make sure that classify still works ok. */ +rte_acl_reset_rules (acl_ctx_wrapper.ctx); + ret =test_classify_run (acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + } + +rte_acl_free (acl_ctx_wrapper.ctx); +rte_free (acl_ctx_wrapper.running_buf); +return ret; +} +static int test_acl (void ) {@@ -1742,6 +1861,8 @@ test_acl(void)return -1 ;if (test_u32_range () <0 )return -1 ; +if (test_mem_hook () <0 ) +return -1 ;return 0 ; }doc/guides/prog_guide/packet_classif_access_ctrl.rst +30
-0@@ -359,7 +359,38 @@ For example: ret = rte_acl_build(acx, &cfg); } +Custom Memory Hooks +~~~~~~~~~~~~~~~~~~~~ + +The ACL library supports custom memory allocation for runtime structures. +Applications can supply their own memory hooks through: + +.. code-block:: c + + int rte_acl_set_mem_hook(struct rte_acl_ctx *ctx, + const struct rte_acl_mem_hook *mhook); + + int rte_acl_get_mem_hook(const struct rte_acl_ctx *ctx, + struct rte_acl_mem_hook *mhook); + +The ``rte_acl_mem_hook`` structure defines memory hooks: + +.. code-block:: c + + struct rte_acl_mem_hook { + /** Allocate zero-initialized memory used during runtime. */ + void *(*zalloc)(char *name, size_t size, size_t align, int32_t socket_id, void *udata); + + /** Free memory previously allocated by zalloc(). */ + void (*free)(void *ptr, void *udata); + + /** User-provided context passed to allocation/free hooks. */ + void *udata; + }; + +Applications may use these hooks to allocate memory from custom pools orpre-allocated buffers.
+If no memory hook is provided, the ACL library uses
rte_zmalloc_socket()internally.Classification methods
~~~~~~~~~~~~~~~~~~~~~~lib/acl/acl.h +1
-0@@ -174,6 +174,7 @@ struct rte_acl_ctx {uint32_t max_rules;uint32_t rule_sz;uint32_t num_rules; +struct rte_acl_mem_hook mem_hook;uint32_t num_categories;uint32_t num_tries;uint32_t match_index;lib/acl/acl_bld.c +1
-1@@ -779,7 +779,7 @@ acl_merge_trie(struct acl_build_context *context,static void acl_build_reset (struct rte_acl_ctx *ctx) { -rte_free (ctx->mem); + ctx->mem_hook.free (ctx->mem, ctx->mem_hook.udata);memset (&ctx->num_categories,0 ,sizeof (*ctx) - offsetof(struct rte_acl_ctx , num_categories)); }lib/acl/acl_gen.c +2
-2@@ -478,8 +478,8 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie, return -ERANGE ; } - mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , - ctx->socket_id); + mem = ctx->mem_hook.zalloc (ctx->name, total_size, +RTE_CACHE_LINE_SIZE , ctx->socket_id, ctx- >mem_hook.udata);lib/acl/acl_gen.c +0
-0if (mem ==NULL ) { ACL_LOG(ERR, "allocation of %zu bytes on socket %d for %s failed",lib/acl/rte_acl.c +14
-0@@ -264,6 +264,20 @@ acl_get_best_alg(void)return alg[i]; } +static void * +acl_mem_default_zalloc (char *name,size_t size,size_t align,int32_t socket_id,void *udata) +{ +RTE_SET_USED (udata); +return rte_zmalloc_socket (name, size, align, socket_id); +} + +static void +acl_mem_default_free (void *ptr,void *udata) +{ +RTE_SET_USED (udata); +rte_free (ptr); +} +RTE_EXPORT_SYMBOL (rte_acl_set_ctx_classify)extern int
rte_acl_set_ctx_classify(struct rte_acl_ctx *ctx, enum rte_acl_classify_alg alg)lib/acl/rte_acl.c +11
-1@@ -362,7 +376,7 @@ rte_acl_free(struct rte_acl_ctx *ctx)rte_mcfg_tailq_write_unlock (); -rte_free (ctx->mem); + ctx->mem_hook.free (ctx->mem, ctx->mem_hook.udata);rte_free (ctx);rte_free (te); }@@ -425,6 +439,9 @@ rte_acl_create(const struct rte_acl_param *param) ctx->rule_sz = param->rule_size; ctx->socket_id = param->socket_id; ctx->alg =acl_get_best_alg (); + ctx->mem_hook.zalloc = acl_mem_default_zalloc; + ctx->mem_hook.free = acl_mem_default_free; + ctx->mem_hook.udata =NULL ;strlcpy (ctx->name, param->name,sizeof (ctx->name)); te->data = (void *) ctx;@@ -555,3 +572,29 @@ rte_acl_list_dump(void) }rte_mcfg_tailq_read_unlock (); } + +/* +* Set memory allocation hooks for a given ACL context. +*/ +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_acl_set_mem_hook, 26.03) +int +rte_acl_set_mem_hook(struct rte_acl_ctx *acl, const struct rte_acl_mem_hook*mhook)
lib/acl/rte_acl.c +2
-0+{ + if (acl == NULL || mhook == NULL || mhook->zalloc == NULL || mhook-Quoted 1 line
free == NULL)
lib/acl/rte_acl.c +11
-0+return -EINVAL ; +memcpy (&acl->mem_hook, mhook,sizeof (struct rte_acl_mem_hook )); +return 0 ; +} + +/* +* Retrieve the memory allocation hooks assigned to the ACL context. +*/ +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_acl_get_mem_hook, 26.03) +int +rte_acl_get_mem_hook(const struct rte_acl_ctx *acl, struct
rte_acl_mem_hook*mhook)lib/acl/rte_acl.c +6
-0+{ +if (acl ==NULL || mhook ==NULL ) +return -EINVAL ; +memcpy (mhook, &acl->mem_hook,sizeof (struct rte_acl_mem_hook )); +return 0 ; +}lib/acl/rte_acl.h +18
-0@@ -136,6 +136,53 @@ struct rte_acl_param {/** @internal opaque ACL handle */ struct rte_acl_ctx ; +/** +* Memory allocation hooks for ACL runtime. +*/ +struct rte_acl_mem_hook { +/** Allocate zero-initialized memory used during runtime. */ +void *(*zalloc)(char *name,size_t size,size_t align,int32_t socket_id,void *udata); + +/** Free memory previously allocated by zalloc(). */ +void (*free)(void *ptr,void *udata); + +/** User-provided context passed to allocation/free hooks. */ +void *udata; +}; + +/** +* Set memory allocation hooks for a given ACL context. +* +* Applications may use these hooks to allocate memory from custom pools or
Quoted 8 lines
pre-allocated buffers.
+ * If no memory hook is provided, the ACL library usesrte_zmalloc_socket()internally.
+ *
+ * @param acl
+ * The ACL context.
+ * @param mhook
+ * Pointer to the memory hook structure
We probably need to mention that it has to be called before acl_build(). And if acl_build() already was invoked (user wants to change mhook after it), then user has to do the following sequence:rte_acl_reset(); re_acl_set_mem_hook(); rte_acl_build();
Quoted 30 lines
+ *
+ * @return
+ * 0 on success.
+ *-EINVALif parameters are invalid.
+ */lib/acl/rte_acl.h +2
-0+__rte_experimental +int rte_acl_set_mem_hook(struct rte_acl_ctx *acl, const struct
rte_acl_mem_hook*mhook);lib/acl/rte_acl.h +15
-0+ +/** +* Retrieve the memory allocation hooks assigned to the ACL context. +* +* @param acl +* The ACL context. +* @param mhook +* Output location for the current memory hook structure +* +* @return +* 0 on success. +* -EINVAL if parameters are invalid. +*/ +__rte_experimental +int rte_acl_get_mem_hook(const struct rte_acl_ctx *acl, struct
rte_acl_mem_hook*mhook);
+
/**
* De-allocate all memory used by ACL context.
*
--
2.43.0
RE: [PATCH v6] acl: support custom memory allocators
Regarding the memhook usage in UT:
The test was originally added only to validate the control path (rte_acl_set_mem_hook / rte_acl_get_mem_hook) rather than memory semantics. Since this brings little value and may be error-prone, I will switch the UT to use malloc/free as suggested.
About the API usage notes:
I agree with the recommendation. and will update the documentation.
Quoted 429 lines · Konstantin Ananyev
Quoted 5 lines
Allow users to provide custom
memory allocation hooks for runtime memory inrte_acl_ctx, via structrte_acl_mem_hook.LGTM in general, few extra comments below.
Quoted 53 lines
Key changes:
- Added structrte_acl_mem_hookwith zalloc, free, and udata.
- Addedrte_acl_set_mem_hook/rte_acl_get_mem_hookto set/get callbacks.
- Default allocation uses existing rte_zmalloc_socket/rte_free.
- Modified ACL code to call callbacks for runtime allocations instead of rte_zmalloc_socket/rte_free directly.v5:
- Remove temporary memory allocation callback for build stage.
- Introduce new API (rte_acl_set_mem_hook/rte_acl_get_mem_hook) instead of modifying existingrte_acl_configto preserve ABI compatibility.v6:
- Reworked API to meet consistency and naming conventions.
- Adjusted parameter order for better readability and alignment.
- Renamed internal variables for clarity and code consistency.Signed-off-by YongFeng Wang <[email protected]>--- app/test/test_acl.c | 121 ++++++++++++++++++ .../prog_guide/packet_classif_access_ctrl.rst | 31 +++++ lib/acl/acl.h | 1 + lib/acl/acl_bld.c | 2 +- lib/acl/acl_gen.c | 4 +- lib/acl/rte_acl.c | 45 ++++++- lib/acl/rte_acl.h | 47 +++++++ 7 files changed, 247 insertions(+), 4 deletions(-)app/test/test_acl.c +16
-0@@ -1721,6 +1721,125 @@ test_u32_range(void)return rc; } +struct acl_ctx_wrapper { +struct rte_acl_ctx *ctx; +void *running_buf; +bool running_buf_using; +}; + +#define ACL_RUNNING_BUF_SIZE (10 * 1024 * 1024) + +static void *running_alloc (char *name,size_t size, +size_t align,int32_t socket_id,void *udata) +{ +RTE_SET_USED (align); +RTE_SET_USED (name); +RTE_SET_USED (socket_id); +if (size >ACL_RUNNING_BUF_SIZE ) +return NULL ;app/test/test_acl.c test_u32_range(void) · line 1749
Quoted 10 lines
app/test/test_acl.c +10
-0+struct acl_ctx_wrapper *acl_ctx = (struct acl_ctx_wrapper *)udata; +if (acl_ctx->running_buf_using) +return NULL ; +printf ("running memory alloc for acl context, size=%zu, pointer=%p\n" , + size, + acl_ctx->running_buf); +memset (acl_ctx->running_buf,0 , size); + acl_ctx->running_buf_using = true; +return acl_ctx->running_buf; +}Is there any point to have such memhook in our UT?
From one side: it doesn't test anything new, as memory is still allocsted viarte_zmalloc(). From other side it is error prone, as you don't check that pre-allocated buffer
will really satisfy requested size and alignment parameters.
Might be just use libc malloc/free here?Quoted 305 lines
app/test/test_acl.c +49
-0+ +static void running_free (void *ptr,void *udata) +{ +if (!ptr) +return ; +struct acl_ctx_wrapper *acl_ctx = (struct acl_ctx_wrapper *)udata; +printf ("running memory free, pointer=%p\n" , ptr); + acl_ctx->running_buf_using = false; +} + +static int +test_mem_hook (void ) +{ +int i, ret; +struct acl_ctx_wrapper acl_ctx_wrapper = {0 }; + acl_ctx_wrapper.ctx =rte_acl_create (&acl_param); +if (acl_ctx_wrapper.ctx ==NULL ) { +printf ("Line %i: Error creating ACL context!\n" , __LINE__); +return -1 ; + } + acl_ctx_wrapper.running_buf =rte_zmalloc_socket ( +"test_acl" , +ACL_RUNNING_BUF_SIZE , +RTE_CACHE_LINE_SIZE , +SOCKET_ID_ANY ); +if (!acl_ctx_wrapper.running_buf) { +rte_acl_free (acl_ctx_wrapper.ctx); +printf ("Line %i: Error allocing running buf for acl context!\n" , __LINE__); +return 1 ; + } + acl_ctx_wrapper.running_buf_using = false; + +struct rte_acl_mem_hook mhook = { + .zalloc = running_alloc, + .free = running_free, + .udata = &acl_ctx_wrapper + }; + ret =rte_acl_set_mem_hook (acl_ctx_wrapper.ctx, &mhook); +if (ret !=0 ) { +printf ("Line %i: Error set mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx_wrapper.ctx); +rte_free (acl_ctx_wrapper.running_buf); +return 1 ; + } +struct rte_acl_mem_hook new_hook; +memset (&new_hook,0 ,sizeof (struct rte_acl_mem_hook )); +if (rte_acl_get_mem_hook (acl_ctx_wrapper.ctx, &new_hook) !=0 + ||memcmp (&mhook, &new_hook,sizeof (struct rte_acl_mem_hook )) !=0 ) { + printf("Line %i: Error get mem hook for acl context!\n",
__LINE__);app/test/test_acl.c +16
-0+rte_acl_free (acl_ctx_wrapper.ctx); +rte_free (acl_ctx_wrapper.running_buf); +return 1 ; + } + ret =0 ; +for (i =0 ; i <TEST_CLASSIFY_ITER ; i++) { + +if ((i &1 ) ==0 ) +rte_acl_reset (acl_ctx_wrapper.ctx); +else +rte_acl_reset_rules (acl_ctx_wrapper.ctx); + + ret =test_classify_buid (acl_ctx_wrapper.ctx, acl_test_rules, +RTE_DIM (acl_test_rules)); +if (ret !=0 ) { + printf("Line %i, iter: %d: Adding rules to ACL contextfailed!\n",
app/test/test_acl.c +30
-0+ __LINE__, i); +break ; + } + + ret =test_classify_run (acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + +/* reset rules and make sure that classify still works ok. */ +rte_acl_reset_rules (acl_ctx_wrapper.ctx); + ret =test_classify_run (acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + } + +rte_acl_free (acl_ctx_wrapper.ctx); +rte_free (acl_ctx_wrapper.running_buf); +return ret; +} +static int test_acl (void ) {@@ -1742,6 +1861,8 @@ test_acl(void)return -1 ;if (test_u32_range () <0 )return -1 ; +if (test_mem_hook () <0 ) +return -1 ;return 0 ; }doc/guides/prog_guide/packet_classif_access_ctrl.rst +30
-0@@ -359,7 +359,38 @@ For example: ret = rte_acl_build(acx, &cfg); } +Custom Memory Hooks +~~~~~~~~~~~~~~~~~~~~ + +The ACL library supports custom memory allocation for runtime structures. +Applications can supply their own memory hooks through: + +.. code-block:: c + + int rte_acl_set_mem_hook(struct rte_acl_ctx *ctx, + const struct rte_acl_mem_hook *mhook); + + int rte_acl_get_mem_hook(const struct rte_acl_ctx *ctx, + struct rte_acl_mem_hook *mhook); + +The ``rte_acl_mem_hook`` structure defines memory hooks: + +.. code-block:: c + + struct rte_acl_mem_hook { + /** Allocate zero-initialized memory used during runtime. */ + void *(*zalloc)(char *name, size_t size, size_t align, int32_t socket_id, void *udata); + + /** Free memory previously allocated by zalloc(). */ + void (*free)(void *ptr, void *udata); + + /** User-provided context passed to allocation/free hooks. */ + void *udata; + }; + +Applications may use these hooks to allocate memory from custom pools orpre-allocated buffers.
+If no memory hook is provided, the ACL library uses
rte_zmalloc_socket()internally.Classification methods
~~~~~~~~~~~~~~~~~~~~~~lib/acl/acl.h +1
-0@@ -174,6 +174,7 @@ struct rte_acl_ctx {uint32_t max_rules;uint32_t rule_sz;uint32_t num_rules; +struct rte_acl_mem_hook mem_hook;uint32_t num_categories;uint32_t num_tries;uint32_t match_index;lib/acl/acl_bld.c +1
-1@@ -779,7 +779,7 @@ acl_merge_trie(struct acl_build_context *context,static void acl_build_reset (struct rte_acl_ctx *ctx) { -rte_free (ctx->mem); + ctx->mem_hook.free (ctx->mem, ctx->mem_hook.udata);memset (&ctx->num_categories,0 ,sizeof (*ctx) - offsetof(struct rte_acl_ctx , num_categories)); }lib/acl/acl_gen.c +2
-2@@ -478,8 +478,8 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie, return -ERANGE ; } - mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , - ctx->socket_id); + mem = ctx->mem_hook.zalloc (ctx->name, total_size, +RTE_CACHE_LINE_SIZE , ctx->socket_id, ctx- > mem_hook.udata);if (mem == NULL) {
ACL_LOG(ERR,
"allocation of %zu bytes on socket %d for %s failed",lib/acl/rte_acl.c +14
-0@@ -264,6 +264,20 @@ acl_get_best_alg(void)return alg[i]; } +static void * +acl_mem_default_zalloc (char *name,size_t size,size_t align,int32_t socket_id,void *udata) +{ +RTE_SET_USED (udata); +return rte_zmalloc_socket (name, size, align, socket_id); +} + +static void +acl_mem_default_free (void *ptr,void *udata) +{ +RTE_SET_USED (udata); +rte_free (ptr); +} +RTE_EXPORT_SYMBOL (rte_acl_set_ctx_classify)extern int
rte_acl_set_ctx_classify(struct rte_acl_ctx *ctx, enum rte_acl_classify_alg alg)lib/acl/rte_acl.c +11
-1@@ -362,7 +376,7 @@ rte_acl_free(struct rte_acl_ctx *ctx)rte_mcfg_tailq_write_unlock (); -rte_free (ctx->mem); + ctx->mem_hook.free (ctx->mem, ctx->mem_hook.udata);rte_free (ctx);rte_free (te); }@@ -425,6 +439,9 @@ rte_acl_create(const struct rte_acl_param *param) ctx->rule_sz = param->rule_size; ctx->socket_id = param->socket_id; ctx->alg =acl_get_best_alg (); + ctx->mem_hook.zalloc = acl_mem_default_zalloc; + ctx->mem_hook.free = acl_mem_default_free; + ctx->mem_hook.udata =NULL ;strlcpy (ctx->name, param->name,sizeof (ctx->name)); te->data = (void *) ctx;@@ -555,3 +572,29 @@ rte_acl_list_dump(void) }rte_mcfg_tailq_read_unlock (); } + +/* +* Set memory allocation hooks for a given ACL context. +*/ +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_acl_set_mem_hook, 26.03) +int +rte_acl_set_mem_hook(struct rte_acl_ctx *acl, const struct rte_acl_mem_hook*mhook)
lib/acl/rte_acl.c +2
-0+{ + if (acl == NULL || mhook == NULL || mhook->zalloc == NULL || mhook-Quoted 1 line
free == NULL)
lib/acl/rte_acl.c +11
-0+return -EINVAL ; +memcpy (&acl->mem_hook, mhook,sizeof (struct rte_acl_mem_hook )); +return 0 ; +} + +/* +* Retrieve the memory allocation hooks assigned to the ACL context. +*/ +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_acl_get_mem_hook, 26.03) +int +rte_acl_get_mem_hook(const struct rte_acl_ctx *acl, struct
rte_acl_mem_hook*mhook)lib/acl/rte_acl.c +6
-0+{ +if (acl ==NULL || mhook ==NULL ) +return -EINVAL ; +memcpy (mhook, &acl->mem_hook,sizeof (struct rte_acl_mem_hook )); +return 0 ; +}lib/acl/rte_acl.h +18
-0@@ -136,6 +136,53 @@ struct rte_acl_param {/** @internal opaque ACL handle */ struct rte_acl_ctx ; +/** +* Memory allocation hooks for ACL runtime. +*/ +struct rte_acl_mem_hook { +/** Allocate zero-initialized memory used during runtime. */ +void *(*zalloc)(char *name,size_t size,size_t align,int32_t socket_id,void *udata); + +/** Free memory previously allocated by zalloc(). */ +void (*free)(void *ptr,void *udata); + +/** User-provided context passed to allocation/free hooks. */ +void *udata; +}; + +/** +* Set memory allocation hooks for a given ACL context. +* +* Applications may use these hooks to allocate memory from custom pools or Quoted 8 lines
pre-allocated buffers.
+ * If no memory hook is provided, the ACL library usesrte_zmalloc_socket()internally.
+ *
+ * @param acl
+ * The ACL context.
+ * @param mhook
+ * Pointer to the memory hook structureWe probably need to mention that it has to be called before
acl_build(). And ifacl_build()already was invoked (user wants to change mhook after it), then user has to do the following sequence:rte_acl_reset();re_acl_set_mem_hook();rte_acl_build();Quoted 30 lines
+ *
+ * @return
+ * 0 on success.
+ *-EINVALif parameters are invalid.
+ */lib/acl/rte_acl.h +2
-0+__rte_experimental +int rte_acl_set_mem_hook(struct rte_acl_ctx *acl, const struct
rte_acl_mem_hook*mhook);lib/acl/rte_acl.h +15
-0+ +/** +* Retrieve the memory allocation hooks assigned to the ACL context. +* +* @param acl +* The ACL context. +* @param mhook +* Output location for the current memory hook structure +* +* @return +* 0 on success. +* -EINVAL if parameters are invalid. +*/ +__rte_experimental +int rte_acl_get_mem_hook(const struct rte_acl_ctx *acl, struct
rte_acl_mem_hook*mhook);
+
/**
* De-allocate all memory used by ACL context.
*
--
2.43.0
Re: [PATCH v6] acl: support custom memory allocators
In reply to Konstantin AnanyevQuoted 67 lines · On Mon, 8 Dec 2025 09:43:01 +0000 Konstantin Ananyev…
Quoted 3 lines
Allow users to provide custom
memory allocation hooks for runtime memory inrte_acl_ctx, via structrte_acl_mem_hook.LGTM in general, few extra comments below.
Quoted 61 lines
Key changes:
- Added structrte_acl_mem_hookwith zalloc, free, and udata.
- Addedrte_acl_set_mem_hook/rte_acl_get_mem_hookto set/get callbacks.
- Default allocation uses existing rte_zmalloc_socket/rte_free.
- Modified ACL code to call callbacks for runtime allocations instead of rte_zmalloc_socket/rte_free directly.v5:
- Remove temporary memory allocation callback for build stage.
- Introduce new API (rte_acl_set_mem_hook/rte_acl_get_mem_hook) instead of modifying existingrte_acl_configto preserve ABI compatibility.v6:
- Reworked API to meet consistency and naming conventions.
- Adjusted parameter order for better readability and alignment.
- Renamed internal variables for clarity and code consistency.Signed-off-by YongFeng Wang <[email protected]>--- app/test/test_acl.c | 121 ++++++++++++++++++ .../prog_guide/packet_classif_access_ctrl.rst | 31 +++++ lib/acl/acl.h | 1 + lib/acl/acl_bld.c | 2 +- lib/acl/acl_gen.c | 4 +- lib/acl/rte_acl.c | 45 ++++++- lib/acl/rte_acl.h | 47 +++++++ 7 files changed, 247 insertions(+), 4 deletions(-)app/test/test_acl.c +24
-0@@ -1721,6 +1721,125 @@ test_u32_range(void)return rc; } +struct acl_ctx_wrapper { +struct rte_acl_ctx *ctx; +void *running_buf; +bool running_buf_using; +}; + +#define ACL_RUNNING_BUF_SIZE (10 * 1024 * 1024) + +static void *running_alloc (char *name,size_t size, +size_t align,int32_t socket_id,void *udata) +{ +RTE_SET_USED (align); +RTE_SET_USED (name); +RTE_SET_USED (socket_id); +if (size >ACL_RUNNING_BUF_SIZE ) +return NULL ; +struct acl_ctx_wrapper *acl_ctx = (struct acl_ctx_wrapper *)udata; +if (acl_ctx->running_buf_using) +return NULL ; +printf ("running memory alloc for acl context, size=%zu, pointer=%p\n" , + size, + acl_ctx->running_buf); +memset (acl_ctx->running_buf,0 , size); + acl_ctx->running_buf_using = true;
Quoted 8 lines
app/test/test_acl.c test_u32_range(void) · line 1749
Quoted 2 lines
app/test/test_acl.c +2
-0+return acl_ctx->running_buf; +}Is there any point to have such memhook in our UT?
From one side: it doesn't test anything new, as memory is still allocsted viarte_zmalloc(). From other side it is error prone, as you don't check that pre-allocated buffer will really satisfy requested size and alignment parameters.
Might be just use libc malloc/free here?
A lot of the problems would go away if ACL just used regular malloc/free more, and rte_malloc/rte_free less. The existing rte_malloc is slow and fragments badly
Re: [Internet]Re: [PATCH v6] acl: support custom memory allocators
Just to add some background on how ACL currently allocates memory:
ACL has two types of allocations:
1. The memory used for the ACL match trie is NUMA-aware. This part relies on specific NUMA placement, so replacing rte_malloc
with
regular malloc/free is not feasible.
2. The temporary memory used during the build phase already uses malloc/free, since NUMA locality is not required there.
Quoted 2 lines · Stephen Hemminger
A lot of the problems would go away if ACL just used regular malloc/free more, and rte_malloc/rte_free less. The existing
rte_mallocis slow and fragments badly
RE: [PATCH v6] acl: support custom memory allocators
In reply to Stephen HemmingerQuoted 78 lines
Quoted 9 lines · On Mon, 8 Dec 2025 09:43:01 +0000 Konstantin Ananyev…
Quoted 3 lines
Allow users to provide custom
memory allocation hooks for runtime memory inrte_acl_ctx, via structrte_acl_mem_hook.LGTM in general, few extra comments below.
Quoted 3 lines
Key changes:
- Added structrte_acl_mem_hookwith zalloc, free, and udata.
- Addedrte_acl_set_mem_hook/rte_acl_get_mem_hookto set/getcallbacks.
Quoted 55 lines
Quoted 55 lines
- Default allocation uses existing rte_zmalloc_socket/rte_free.
- Modified ACL code to call callbacks for runtime allocations instead of rte_zmalloc_socket/rte_free directly.v5:
- Remove temporary memory allocation callback for build stage.
- Introduce new API (rte_acl_set_mem_hook/rte_acl_get_mem_hook) instead of modifying existingrte_acl_configto preserve ABI compatibility.v6:
- Reworked API to meet consistency and naming conventions.
- Adjusted parameter order for better readability and alignment.
- Renamed internal variables for clarity and code consistency.Signed-off-by YongFeng Wang <[email protected]>--- app/test/test_acl.c | 121 ++++++++++++++++++ .../prog_guide/packet_classif_access_ctrl.rst | 31 +++++ lib/acl/acl.h | 1 + lib/acl/acl_bld.c | 2 +- lib/acl/acl_gen.c | 4 +- lib/acl/rte_acl.c | 45 ++++++- lib/acl/rte_acl.h | 47 +++++++ 7 files changed, 247 insertions(+), 4 deletions(-)app/test/test_acl.c +21
-0@@ -1721,6 +1721,125 @@ test_u32_range(void)return rc; } +struct acl_ctx_wrapper { +struct rte_acl_ctx *ctx; +void *running_buf; +bool running_buf_using; +}; + +#define ACL_RUNNING_BUF_SIZE (10 * 1024 * 1024) + +static void *running_alloc (char *name,size_t size, +size_t align,int32_t socket_id,void *udata) +{ +RTE_SET_USED (align); +RTE_SET_USED (name); +RTE_SET_USED (socket_id); +if (size >ACL_RUNNING_BUF_SIZE ) +return NULL ; +struct acl_ctx_wrapper *acl_ctx = (struct acl_ctx_wrapper *)udata; +if (acl_ctx->running_buf_using) +return NULL ; + printf("running memory alloc for acl context, size=%zu, pointer=%p\n", + size,Quoted 8 lines
app/test/test_acl.c test_u32_range(void) · line 1749
Quoted 5 lines
app/test/test_acl.c +5
-0+ acl_ctx->running_buf); +memset (acl_ctx->running_buf,0 , size); + acl_ctx->running_buf_using = true; +return acl_ctx->running_buf; +}Is there any point to have such memhook in our UT?
From one side: it doesn't test anything new, as memory is still allocsted via
Quoted 7 lines
rte_zmalloc().Quoted 3 lines
From other side it is error prone, as you don't check that pre-allocated buffer will really satisfy requested size and alignment parameters. Might be just use libc malloc/free here?
A lot of the problems would go away if ACL just used regular malloc/free more, and rte_malloc/rte_free less.
It uses rte_malloc in just two places - to allocate ctx itself and for actual Run-Time table. All temporary allocations are done with normal malloc.
There are obvious reasons why people prefer to use rte_malloc-ed memory
in their data-path functions: rte-malloc-ed memory uses hugepages and is MP shared. So I suppose providing users a choice where they want their ACL tables to be located is a good option.
Quoted 1 line
The existing
rte_mallocis slow and fragments badly.
Then we probably need to improve it, don't we?
Re: [PATCH v6] acl: support custom memory allocators
Quoted 86 lines · Konstantin Ananyev
Quoted 78 lines
Quoted 9 lines · On Mon, 8 Dec 2025 09:43:01 +0000 Konstantin Ananyev…
Quoted 3 lines
Allow users to provide custom
memory allocation hooks for runtime memory inrte_acl_ctx, via structrte_acl_mem_hook.LGTM in general, few extra comments below.
Quoted 3 lines
Key changes:
- Added structrte_acl_mem_hookwith zalloc, free, and udata.
- Addedrte_acl_set_mem_hook/rte_acl_get_mem_hookto set/getcallbacks.
Quoted 55 lines
Quoted 55 lines
- Default allocation uses existing rte_zmalloc_socket/rte_free.
- Modified ACL code to call callbacks for runtime allocations instead of rte_zmalloc_socket/rte_free directly.v5:
- Remove temporary memory allocation callback for build stage.
- Introduce new API (rte_acl_set_mem_hook/rte_acl_get_mem_hook) instead of modifying existingrte_acl_configto preserve ABI compatibility.v6:
- Reworked API to meet consistency and naming conventions.
- Adjusted parameter order for better readability and alignment.
- Renamed internal variables for clarity and code consistency.Signed-off-by YongFeng Wang <[email protected]>--- app/test/test_acl.c | 121 ++++++++++++++++++ .../prog_guide/packet_classif_access_ctrl.rst | 31 +++++ lib/acl/acl.h | 1 + lib/acl/acl_bld.c | 2 +- lib/acl/acl_gen.c | 4 +- lib/acl/rte_acl.c | 45 ++++++- lib/acl/rte_acl.h | 47 +++++++ 7 files changed, 247 insertions(+), 4 deletions(-)app/test/test_acl.c +21
-0@@ -1721,6 +1721,125 @@ test_u32_range(void)return rc; } +struct acl_ctx_wrapper { +struct rte_acl_ctx *ctx; +void *running_buf; +bool running_buf_using; +}; + +#define ACL_RUNNING_BUF_SIZE (10 * 1024 * 1024) + +static void *running_alloc (char *name,size_t size, +size_t align,int32_t socket_id,void *udata) +{ +RTE_SET_USED (align); +RTE_SET_USED (name); +RTE_SET_USED (socket_id); +if (size >ACL_RUNNING_BUF_SIZE ) +return NULL ; +struct acl_ctx_wrapper *acl_ctx = (struct acl_ctx_wrapper *)udata; +if (acl_ctx->running_buf_using) +return NULL ; + printf("running memory alloc for acl context, size=%zu, pointer=%p\n", + size,Quoted 8 lines
app/test/test_acl.c test_u32_range(void) · line 1749
Quoted 5 lines
app/test/test_acl.c +5
-0+ acl_ctx->running_buf); +memset (acl_ctx->running_buf,0 , size); + acl_ctx->running_buf_using = true; +return acl_ctx->running_buf; +}Is there any point to have such memhook in our UT?
From one side: it doesn't test anything new, as memory is still allocsted viaQuoted 7 lines
rte_zmalloc().Quoted 3 lines
From other side it is error prone, as you don't check that pre-allocated buffer will really satisfy requested size and alignment parameters. Might be just use libc malloc/free here?
A lot of the problems would go away if ACL just used regular malloc/free more, and rte_malloc/rte_free less.
Quoted 6 lines
It uses
rte_mallocin just two places - to allocate ctx itself and for actual Run-Time table. All temporary allocations are done with normal malloc.
There are obvious reasons why people prefer to use rte_malloc-ed memory
in their data-path functions: rte-malloc-ed memory uses hugepages and is MP shared. So I suppose providing users a choice where they want their ACL tables to be located is a good option.
There is a global acl list (rte_acl_tailq) which could across multi-process, so that main process create one acl, then secondary process could get the same acl by rte_acl_create() with same name. This based on the acl library use rte_malloc. Now the base is broken when introduce this commit.
Quoted 5 lines
Quoted 2 lines
The existing
rte_mallocis slow and fragments badly.Then we probably need to improve it, don't we?
PATCH v7 1/1 acl: support custom memory allocators
In reply to mannywang(王永峰)Allow users to provide custom
memory allocation hooks for runtime memory in rte_acl_ctx, via struct rte_acl_mem_hook.
Key changes:
- Added struct rte_acl_mem_hook with zalloc, free, and udata.
- Added rte_acl_set_mem_hook / rte_acl_get_mem_hook to set/get callbacks.
- Default allocation uses existing rte_zmalloc_socket/rte_free.
- Modified ACL code to call callbacks for runtime allocations instead of rte_zmalloc_socket/rte_free directly.
v5:
- Remove temporary memory allocation callback for build stage.
- Introduce new API (rte_acl_set_mem_hook / rte_acl_get_mem_hook) instead of modifying existing rte_acl_config to preserve ABI compatibility.
v6:
- Reworked API to meet consistency and naming conventions.
- Adjusted parameter order for better readability and alignment.
- Renamed internal variables for clarity and code consistency.
v7:
- Switch the UT to use malloc/free.
- Update the documentation to clarify that rte_acl_set_mem_hook must be called before rte_acl_build.
--- app/test/test_acl.c | 117 ++++++++++++++++++ .../prog_guide/packet_classif_access_ctrl.rst | 31 +++++ lib/acl/acl.h | 1 + lib/acl/acl_bld.c | 2 +- lib/acl/acl_gen.c | 4 +- lib/acl/rte_acl.c | 45 ++++++- lib/acl/rte_acl.h | 53 ++++++++ 7 files changed, 249 insertions(+), 4 deletions(-)
app/test/test_acl.c +117 -0
@@ -1721,6 +1721,121 @@ test_u32_range(void)return rc; } +struct acl_ctx_wrapper { +struct rte_acl_ctx *ctx; +void *running_buf; +bool running_buf_using; +}; + +#define ACL_RUNNING_BUF_SIZE (10 * 1024 * 1024) + +static void *running_alloc (char *name,size_t size, +size_t align,int32_t socket_id,void *udata) +{ +RTE_SET_USED (align); +RTE_SET_USED (name); +RTE_SET_USED (socket_id); +if (size >ACL_RUNNING_BUF_SIZE ) +return NULL ; +struct acl_ctx_wrapper *acl_ctx = (struct acl_ctx_wrapper *)udata; +if (acl_ctx->running_buf_using) +return NULL ; +printf ("running memory alloc for acl context, size=%zu, pointer=%p\n" , + size, + acl_ctx->running_buf); +memset (acl_ctx->running_buf,0 , size); + acl_ctx->running_buf_using = true; +return acl_ctx->running_buf; +} + +static void running_free (void *ptr,void *udata) +{ +if (!ptr) +return ; +struct acl_ctx_wrapper *acl_ctx = (struct acl_ctx_wrapper *)udata; +printf ("running memory free, pointer=%p\n" , ptr); + acl_ctx->running_buf_using = false; +} + +static int +test_mem_hook (void ) +{ +int i, ret; +struct acl_ctx_wrapper acl_ctx_wrapper = {0 }; + acl_ctx_wrapper.ctx =rte_acl_create (&acl_param); +if (acl_ctx_wrapper.ctx ==NULL ) { +printf ("Line %i: Error creating ACL context!\n" , __LINE__); +return -1 ; + } + acl_ctx_wrapper.running_buf =malloc (ACL_RUNNING_BUF_SIZE ); +if (!acl_ctx_wrapper.running_buf) { +rte_acl_free (acl_ctx_wrapper.ctx); +printf ("Line %i: Error allocing running buf for acl context!\n" , __LINE__); +return 1 ; + } + acl_ctx_wrapper.running_buf_using = false; + +struct rte_acl_mem_hook mhook = { + .zalloc = running_alloc, + .free = running_free, + .udata = &acl_ctx_wrapper + }; + ret =rte_acl_set_mem_hook (acl_ctx_wrapper.ctx, &mhook); +if (ret !=0 ) { +printf ("Line %i: Error set mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx_wrapper.ctx); +free (acl_ctx_wrapper.running_buf); +return 1 ; + } +struct rte_acl_mem_hook new_hook; +memset (&new_hook,0 ,sizeof (struct rte_acl_mem_hook )); +if (rte_acl_get_mem_hook (acl_ctx_wrapper.ctx, &new_hook) !=0 + ||memcmp (&mhook, &new_hook,sizeof (struct rte_acl_mem_hook )) !=0 ) { +printf ("Line %i: Error get mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx_wrapper.ctx); +free (acl_ctx_wrapper.running_buf); +return 1 ; + } + ret =0 ; +for (i =0 ; i <TEST_CLASSIFY_ITER ; i++) { + +if ((i &1 ) ==0 ) +rte_acl_reset (acl_ctx_wrapper.ctx); +else +rte_acl_reset_rules (acl_ctx_wrapper.ctx); + + ret =test_classify_buid (acl_ctx_wrapper.ctx, acl_test_rules, +RTE_DIM (acl_test_rules)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: Adding rules to ACL context failed!\n" , + __LINE__, i); +break ; + } + + ret =test_classify_run (acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + +/* reset rules and make sure that classify still works ok. */ +rte_acl_reset_rules (acl_ctx_wrapper.ctx); + ret =test_classify_run (acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + } + +rte_acl_free (acl_ctx_wrapper.ctx); +free (acl_ctx_wrapper.running_buf); +return ret; +} +static int test_acl (void ) {
@@ -1742,6 +1857,8 @@ test_acl(void)return -1 ;if (test_u32_range () <0 )return -1 ; +if (test_mem_hook () <0 ) +return -1 ;return 0 ; }
doc/guides/prog_guide/packet_classif_access_ctrl.rst +31 -0
@@ -359,7 +359,38 @@ For example: ret = rte_acl_build(acx, &cfg); } +Custom Memory Hooks +~~~~~~~~~~~~~~~~~~~~ + +The ACL library supports custom memory allocation for runtime structures. +Applications can supply their own memory hooks through: + +.. code-block:: c + + int rte_acl_set_mem_hook(struct rte_acl_ctx *ctx, + const struct rte_acl_mem_hook *mhook); + + int rte_acl_get_mem_hook(const struct rte_acl_ctx *ctx, + struct rte_acl_mem_hook *mhook); + +The ``rte_acl_mem_hook`` structure defines memory hooks: + +.. code-block:: c + + struct rte_acl_mem_hook { + /** Allocate zero-initialized memory used during runtime. */ + void *(*zalloc)(char *name, size_t size, size_t align, int32_t socket_id, void *udata); + + /** Free memory previously allocated by zalloc(). */ + void (*free)(void *ptr, void *udata); + + /** User-provided context passed to allocation/free hooks. */ + void *udata; + }; + +Applications may use these hooks to allocate memory from custom pools or pre-allocated buffers. +If no memory hook is provided, the ACL library uses rte_zmalloc_socket() internally. Classification methods ~~~~~~~~~~~~~~~~~~~~~~
lib/acl/acl.h +1 -0
@@ -174,6 +174,7 @@ struct rte_acl_ctx {uint32_t max_rules;uint32_t rule_sz;uint32_t num_rules; +struct rte_acl_mem_hook mem_hook;uint32_t num_categories;uint32_t num_tries;uint32_t match_index;
lib/acl/acl_bld.c +1 -1
@@ -779,7 +779,7 @@ acl_merge_trie(struct acl_build_context *context,static void acl_build_reset (struct rte_acl_ctx *ctx) { -rte_free (ctx->mem); + ctx->mem_hook.free (ctx->mem, ctx->mem_hook.udata);memset (&ctx->num_categories,0 ,sizeof (*ctx) - offsetof(struct rte_acl_ctx , num_categories)); }
lib/acl/acl_gen.c +2 -2
@@ -478,8 +478,8 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie,return -ERANGE ; } - mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , - ctx->socket_id); + mem = ctx->mem_hook.zalloc (ctx->name, total_size, +RTE_CACHE_LINE_SIZE , ctx->socket_id, ctx->mem_hook.udata);if (mem ==NULL ) { ACL_LOG(ERR, "allocation of %zu bytes on socket %d for %s failed",
lib/acl/rte_acl.c +44 -1
66 lines not on this page · raw message
lib/acl/rte_acl.h +53 -0
59 lines not on this page · raw message
Signature · 1 line
2.43.0
RE: [PATCH v7] acl: support custom memory allocators
Quoted 64 lines
Allow users to provide custom
memory allocation hooks for runtime memory inrte_acl_ctx, via structrte_acl_mem_hook.Key changes:
- Added structrte_acl_mem_hookwith zalloc, free, and udata.
- Addedrte_acl_set_mem_hook/rte_acl_get_mem_hookto set/get callbacks.
- Default allocation uses existing rte_zmalloc_socket/rte_free.
- Modified ACL code to call callbacks for runtime allocations instead of rte_zmalloc_socket/rte_free directly.v5:
- Remove temporary memory allocation callback for build stage.
- Introduce new API (rte_acl_set_mem_hook/rte_acl_get_mem_hook) instead of modifying existingrte_acl_configto preserve ABI compatibility.v6:
- Reworked API to meet consistency and naming conventions.
- Adjusted parameter order for better readability and alignment.
- Renamed internal variables for clarity and code consistency.v7:
- Switch the UT to use malloc/free.
- Update the documentation to clarify thatrte_acl_set_mem_hookmust be called beforerte_acl_build.Signed-off-by YongFeng Wang <[email protected]>--- app/test/test_acl.c | 117 ++++++++++++++++++ .../prog_guide/packet_classif_access_ctrl.rst | 31 +++++ lib/acl/acl.h | 1 + lib/acl/acl_bld.c | 2 +- lib/acl/acl_gen.c | 4 +- lib/acl/rte_acl.c | 45 ++++++- lib/acl/rte_acl.h | 53 ++++++++ 7 files changed, 249 insertions(+), 4 deletions(-)app/test/test_acl.c +16
-0@@ -1721,6 +1721,121 @@ test_u32_range(void)return rc; } +struct acl_ctx_wrapper { +struct rte_acl_ctx *ctx; +void *running_buf; +bool running_buf_using; +}; + +#define ACL_RUNNING_BUF_SIZE (10 * 1024 * 1024) + +static void *running_alloc (char *name,size_t size, +size_t align,int32_t socket_id,void *udata) +{ +RTE_SET_USED (align); +RTE_SET_USED (name); +RTE_SET_USED (socket_id); +if (size >ACL_RUNNING_BUF_SIZE ) +return NULL ;
app/test/test_acl.c test_u32_range(void) · line 1749
Quoted 10 lines
app/test/test_acl.c +10
-0+struct acl_ctx_wrapper *acl_ctx = (struct acl_ctx_wrapper *)udata; +if (acl_ctx->running_buf_using) +return NULL ; +printf ("running memory alloc for acl context, size=%zu, pointer=%p\n" , + size, + acl_ctx->running_buf); +memset (acl_ctx->running_buf,0 , size); + acl_ctx->running_buf_using = true; +return acl_ctx->running_buf; +}
Sorry, I didn't express myself clear enough.
My thought was to use malloc/aligned_alloc() directly in running_alloc() function. To be more specific here:
static void *running_alloc(char *name, size_t size, size_t align, int32_t socket_id, void *udata) {
...
/* just for the API correctness check */
If (udata != <expected_value>) {
/* report error */
return NULL; }
addr = aligned_alloc(align, size);
if (addr == NULL) {
/* report error*/ }
return addr;
}
Quoted 338 lines
app/test/test_acl.c +90
-0+static void running_free (void *ptr,void *udata) +{ +if (!ptr) +return ; +struct acl_ctx_wrapper *acl_ctx = (struct acl_ctx_wrapper *)udata; +printf ("running memory free, pointer=%p\n" , ptr); + acl_ctx->running_buf_using = false; +} + +static int +test_mem_hook (void ) +{ +int i, ret; +struct acl_ctx_wrapper acl_ctx_wrapper = {0 }; + acl_ctx_wrapper.ctx =rte_acl_create (&acl_param); +if (acl_ctx_wrapper.ctx ==NULL ) { +printf ("Line %i: Error creating ACL context!\n" , __LINE__); +return -1 ; + } + acl_ctx_wrapper.running_buf =malloc (ACL_RUNNING_BUF_SIZE ); +if (!acl_ctx_wrapper.running_buf) { +rte_acl_free (acl_ctx_wrapper.ctx); +printf ("Line %i: Error allocing running buf for acl context!\n" , __LINE__); +return 1 ; + } + acl_ctx_wrapper.running_buf_using = false; + +struct rte_acl_mem_hook mhook = { + .zalloc = running_alloc, + .free = running_free, + .udata = &acl_ctx_wrapper + }; + ret =rte_acl_set_mem_hook (acl_ctx_wrapper.ctx, &mhook); +if (ret !=0 ) { +printf ("Line %i: Error set mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx_wrapper.ctx); +free (acl_ctx_wrapper.running_buf); +return 1 ; + } +struct rte_acl_mem_hook new_hook; +memset (&new_hook,0 ,sizeof (struct rte_acl_mem_hook )); +if (rte_acl_get_mem_hook (acl_ctx_wrapper.ctx, &new_hook) !=0 + ||memcmp (&mhook, &new_hook,sizeof (struct rte_acl_mem_hook )) !=0 ) { +printf ("Line %i: Error get mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx_wrapper.ctx); +free (acl_ctx_wrapper.running_buf); +return 1 ; + } + ret =0 ; +for (i =0 ; i <TEST_CLASSIFY_ITER ; i++) { + +if ((i &1 ) ==0 ) +rte_acl_reset (acl_ctx_wrapper.ctx); +else +rte_acl_reset_rules (acl_ctx_wrapper.ctx); + + ret =test_classify_buid (acl_ctx_wrapper.ctx, acl_test_rules, +RTE_DIM (acl_test_rules)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: Adding rules to ACL context failed!\n", + __LINE__, i); +break ; + } + + ret =test_classify_run (acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + +/* reset rules and make sure that classify still works ok. */ +rte_acl_reset_rules (acl_ctx_wrapper.ctx); + ret =test_classify_run (acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + } + +rte_acl_free (acl_ctx_wrapper.ctx); +free (acl_ctx_wrapper.running_buf); +return ret; +} +static int test_acl (void ) {@@ -1742,6 +1857,8 @@ test_acl(void)return -1 ;if (test_u32_range () <0 )return -1 ; +if (test_mem_hook () <0 ) +return -1 ;return 0 ; }doc/guides/prog_guide/packet_classif_access_ctrl.rst +30
-0@@ -359,7 +359,38 @@ For example: ret = rte_acl_build(acx, &cfg); } +Custom Memory Hooks +~~~~~~~~~~~~~~~~~~~~ + +The ACL library supports custom memory allocation for runtime structures. +Applications can supply their own memory hooks through: + +.. code-block:: c + + int rte_acl_set_mem_hook(struct rte_acl_ctx *ctx, + const struct rte_acl_mem_hook *mhook); + + int rte_acl_get_mem_hook(const struct rte_acl_ctx *ctx, + struct rte_acl_mem_hook *mhook); + +The ``rte_acl_mem_hook`` structure defines memory hooks: + +.. code-block:: c + + struct rte_acl_mem_hook { + /** Allocate zero-initialized memory used during runtime. */ + void *(*zalloc)(char *name, size_t size, size_t align, int32_t socket_id, void *udata); + + /** Free memory previously allocated by zalloc(). */ + void (*free)(void *ptr, void *udata); + + /** User-provided context passed to allocation/free hooks. */ + void *udata; + }; + +Applications may use these hooks to allocate memory from custom pools or pre-allocated buffers.
doc/guides/prog_guide/packet_classif_access_ctrl.rst +1
-0+If no memory hook is provided, the ACL library uses rte_zmalloc_socket() internally. Classification methods ~~~~~~~~~~~~~~~~~~~~~~lib/acl/acl.h +1
-0@@ -174,6 +174,7 @@ struct rte_acl_ctx {uint32_t max_rules;uint32_t rule_sz;uint32_t num_rules; +struct rte_acl_mem_hook mem_hook;uint32_t num_categories;uint32_t num_tries;uint32_t match_index;lib/acl/acl_bld.c +1
-1@@ -779,7 +779,7 @@ acl_merge_trie(struct acl_build_context *context,static void acl_build_reset (struct rte_acl_ctx *ctx) { -rte_free (ctx->mem); + ctx->mem_hook.free (ctx->mem, ctx->mem_hook.udata);memset (&ctx->num_categories,0 ,sizeof (*ctx) - offsetof(struct rte_acl_ctx , num_categories)); }lib/acl/acl_gen.c +2
-2@@ -478,8 +478,8 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie,return -ERANGE ; } - mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , - ctx->socket_id); + mem = ctx->mem_hook.zalloc (ctx->name, total_size, +RTE_CACHE_LINE_SIZE , ctx->socket_id, ctx- >mem_hook.udata);if (mem ==NULL ) { ACL_LOG(ERR,lib/acl/acl_gen.c +0
-0"allocation of %zu bytes on socket %d for %s failed",lib/acl/rte_acl.c +14
-0@@ -264,6 +264,20 @@ acl_get_best_alg(void)return alg[i]; } +static void * +acl_mem_default_zalloc (char *name,size_t size,size_t align,int32_t socket_id,void *udata) +{ +RTE_SET_USED (udata); +return rte_zmalloc_socket (name, size, align, socket_id); +} + +static void +acl_mem_default_free (void *ptr,void *udata) +{ +RTE_SET_USED (udata); +rte_free (ptr); +} +RTE_EXPORT_SYMBOL (rte_acl_set_ctx_classify)extern int
rte_acl_set_ctx_classify(struct rte_acl_ctx *ctx, enum rte_acl_classify_alg alg)lib/acl/rte_acl.c +11
-1@@ -362,7 +376,7 @@ rte_acl_free(struct rte_acl_ctx *ctx)rte_mcfg_tailq_write_unlock (); -rte_free (ctx->mem); + ctx->mem_hook.free (ctx->mem, ctx->mem_hook.udata);rte_free (ctx);rte_free (te); }@@ -425,6 +439,9 @@ rte_acl_create(const struct rte_acl_param *param) ctx->rule_sz = param->rule_size; ctx->socket_id = param->socket_id; ctx->alg =acl_get_best_alg (); + ctx->mem_hook.zalloc = acl_mem_default_zalloc; + ctx->mem_hook.free = acl_mem_default_free; + ctx->mem_hook.udata =NULL ;strlcpy (ctx->name, param->name,sizeof (ctx->name)); te->data = (void *) ctx;@@ -555,3 +572,29 @@ rte_acl_list_dump(void) }rte_mcfg_tailq_read_unlock (); } + +/* +* Set memory allocation hooks for a given ACL context. +*/ +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_acl_set_mem_hook, 26.03) +int +rte_acl_set_mem_hook(struct rte_acl_ctx *acl, const struct rte_acl_mem_hook*mhook)
lib/acl/rte_acl.c +2
-0+{ + if (acl == NULL || mhook == NULL || mhook->zalloc == NULL || mhook-Quoted 1 line
free == NULL)
lib/acl/rte_acl.c +11
-0+return -EINVAL ; +memcpy (&acl->mem_hook, mhook,sizeof (struct rte_acl_mem_hook )); +return 0 ; +} + +/* +* Retrieve the memory allocation hooks assigned to the ACL context. +*/ +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_acl_get_mem_hook, 26.03) +int +rte_acl_get_mem_hook(const struct rte_acl_ctx *acl, struct rte_acl_mem_hook*mhook)
lib/acl/rte_acl.c +6
-0+{ +if (acl ==NULL || mhook ==NULL ) +return -EINVAL ; +memcpy (mhook, &acl->mem_hook,sizeof (struct rte_acl_mem_hook )); +return 0 ; +}lib/acl/rte_acl.h +53
-0@@ -136,6 +136,59 @@ struct rte_acl_param {/** @internal opaque ACL handle */ struct rte_acl_ctx ; +/** +* Memory allocation hooks for ACL runtime. +*/ +struct rte_acl_mem_hook { +/** Allocate zero-initialized memory used during runtime. */ +void *(*zalloc)(char *name,size_t size,size_t align,int32_t socket_id,void *udata); + +/** Free memory previously allocated by zalloc(). */ +void (*free)(void *ptr,void *udata); + +/** User-provided context passed to allocation/free hooks. */ +void *udata; +}; + +/** +* Set memory allocation hooks for a given ACL context. +* +* Applications may use these hooks to allocate memory from custom pools or +* pre-allocated buffers. If no memory hook is provided, the ACL library uses +* rte_zmalloc_socket() internally. +* +* This function must be called **before** rte_acl_build(). +* If the hook needs to be changed after a build, the ACL context must be reset +* first by invoking rte_acl_reset(), and only then can the memory hook be +* updated followed by another call to rte_acl_build(). +* +* @param acl +* The ACL context. +* @param mhook +* Pointer to the memory hook structure +* +* @return +* 0 on success. +* -EINVAL if parameters are invalid. +*/ +__rte_experimental +int rte_acl_set_mem_hook(struct rte_acl_ctx *acl, const struct rte_acl_mem_hook *mhook); + +/** +* Retrieve the memory allocation hooks assigned to the ACL context. +* +* @param acl +* The ACL context. +* @param mhook +* Output location for the current memory hook structure +* +* @return +* 0 on success. +* -EINVAL if parameters are invalid. +*/ +__rte_experimental +int rte_acl_get_mem_hook(const struct rte_acl_ctx *acl, struct rte_acl_mem_hook *mhook); +/**
* De-allocate all memory used by ACL context.
*
--
2.43.0
Re: [Internet]RE: [PATCH v7] acl: support custom memory allocators
Thanks for the clarification, and sorry for misunderstanding your intention earlier. I’ve updated the implementation to use malloc/aligned_alloc() directly in running_alloc() as you suggested. Please check whether the new version matches your expectation.
Quoted 18 lines · Konstantin Ananyev
Sorry, I didn't express myself clear enough.
My thought was to use malloc/aligned_alloc() directly inrunning_alloc()function. To be more specific here:
static void *running_alloc(char *name,size_tsize,size_talign,int32_tsocket_id, void *udata){ ... /* just for the API correctness check */ If (udata != <expected_value>) { /* report error */ return NULL; } addr = aligned_alloc(align, size); if (addr == NULL) { /* report error*/ } return addr; }
PATCH v8 1/1 acl: support custom memory allocators
In reply to mannywang(王永峰)Allow users to provide custom
memory allocation hooks for runtime memory in rte_acl_ctx, via struct rte_acl_mem_hook.
Key changes:
- Added struct rte_acl_mem_hook with zalloc, free, and udata.
- Added rte_acl_set_mem_hook / rte_acl_get_mem_hook to set/get callbacks.
- Default allocation uses existing rte_zmalloc_socket/rte_free.
- Modified ACL code to call callbacks for runtime allocations instead of rte_zmalloc_socket/rte_free directly.
v5:
- Remove temporary memory allocation callback for build stage.
- Introduce new API (rte_acl_set_mem_hook / rte_acl_get_mem_hook) instead of modifying existing rte_acl_config to preserve ABI compatibility.
v6:
- Reworked API to meet consistency and naming conventions.
- Adjusted parameter order for better readability and alignment.
- Renamed internal variables for clarity and code consistency.
v7:
- Switch the UT to use malloc/free.
- Update the documentation to clarify that rte_acl_set_mem_hook must be called before rte_acl_build.
v8:
- Fix UT code per review.
--- app/test/test_acl.c | 107 ++++++++++++++++++ .../prog_guide/packet_classif_access_ctrl.rst | 31 +++++ lib/acl/acl.h | 1 + lib/acl/acl_bld.c | 2 +- lib/acl/acl_gen.c | 4 +- lib/acl/rte_acl.c | 45 +++++++- lib/acl/rte_acl.h | 53 +++++++++ 7 files changed, 239 insertions(+), 4 deletions(-)
app/test/test_acl.c +107 -0
@@ -1721,6 +1721,111 @@ test_u32_range(void)return rc; } +static struct rte_acl_ctx *acl_ctx; + +static void *running_alloc (char *name,size_t size, +size_t align,int32_t socket_id,void *udata) +{ +RTE_SET_USED (name); +RTE_SET_USED (socket_id); + +if (udata != acl_ctx) { +printf ("%s#%i udata mismatch!\n" , + __func__, __LINE__); +return NULL ; + } +void *addr =aligned_alloc (align, size); +if (addr ==NULL ) { +printf ("%s#%i alloc memory failed!\n" , + __func__, __LINE__); + } +return addr; +} + +static void running_free (void *ptr,void *udata) +{ +if (udata != acl_ctx) { +printf ("%s#%i udata mismatch!\n" , + __func__, __LINE__); +return ; + } +if (!ptr) +return ; +free (ptr); +} + +static int +test_mem_hook (void ) +{ +int i, ret; + + acl_ctx =rte_acl_create (&acl_param); +if (acl_ctx ==NULL ) { +printf ("Line %i: Error creating ACL context!\n" , __LINE__); +return -1 ; + } + +struct rte_acl_mem_hook mhook = { + .zalloc = running_alloc, + .free = running_free, + .udata = acl_ctx + }; + + ret =rte_acl_set_mem_hook (acl_ctx, &mhook); +if (ret !=0 ) { +printf ("Line %i: Error set mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx); +return 1 ; + } + +struct rte_acl_mem_hook new_hook; +memset (&new_hook,0 ,sizeof (struct rte_acl_mem_hook )); +if (rte_acl_get_mem_hook (acl_ctx, &new_hook) !=0 + ||memcmp (&mhook, &new_hook,sizeof (struct rte_acl_mem_hook )) !=0 ) { +printf ("Line %i: Error get mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx); +return 1 ; + } + + ret =0 ; +for (i =0 ; i <TEST_CLASSIFY_ITER ; i++) { + +if ((i &1 ) ==0 ) +rte_acl_reset (acl_ctx); +else +rte_acl_reset_rules (acl_ctx); + + ret =test_classify_buid (acl_ctx, acl_test_rules, +RTE_DIM (acl_test_rules)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: Adding rules to ACL context failed!\n" , + __LINE__, i); +break ; + } + + ret =test_classify_run (acl_ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + +/* reset rules and make sure that classify still works ok. */ +rte_acl_reset_rules (acl_ctx); + ret =test_classify_run (acl_ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + } + +rte_acl_free (acl_ctx); +return ret; +} +static int test_acl (void ) {
@@ -1742,6 +1847,8 @@ test_acl(void)return -1 ;if (test_u32_range () <0 )return -1 ; +if (test_mem_hook () <0 ) +return -1 ;return 0 ; }
doc/guides/prog_guide/packet_classif_access_ctrl.rst +31 -0
@@ -359,7 +359,38 @@ For example: ret = rte_acl_build(acx, &cfg); } +Custom Memory Hooks +~~~~~~~~~~~~~~~~~~~~ + +The ACL library supports custom memory allocation for runtime structures. +Applications can supply their own memory hooks through: + +.. code-block:: c + + int rte_acl_set_mem_hook(struct rte_acl_ctx *ctx, + const struct rte_acl_mem_hook *mhook); + + int rte_acl_get_mem_hook(const struct rte_acl_ctx *ctx, + struct rte_acl_mem_hook *mhook); + +The ``rte_acl_mem_hook`` structure defines memory hooks: + +.. code-block:: c + + struct rte_acl_mem_hook { + /** Allocate zero-initialized memory used during runtime. */ + void *(*zalloc)(char *name, size_t size, size_t align, int32_t socket_id, void *udata); + + /** Free memory previously allocated by zalloc(). */ + void (*free)(void *ptr, void *udata); + + /** User-provided context passed to allocation/free hooks. */ + void *udata; + }; + +Applications may use these hooks to allocate memory from custom pools or pre-allocated buffers. +If no memory hook is provided, the ACL library uses rte_zmalloc_socket() internally. Classification methods ~~~~~~~~~~~~~~~~~~~~~~
lib/acl/acl.h +1 -0
@@ -174,6 +174,7 @@ struct rte_acl_ctx {uint32_t max_rules;uint32_t rule_sz;uint32_t num_rules; +struct rte_acl_mem_hook mem_hook;uint32_t num_categories;uint32_t num_tries;uint32_t match_index;
lib/acl/acl_bld.c +1 -1
@@ -779,7 +779,7 @@ acl_merge_trie(struct acl_build_context *context,static void acl_build_reset (struct rte_acl_ctx *ctx) { -rte_free (ctx->mem); + ctx->mem_hook.free (ctx->mem, ctx->mem_hook.udata);memset (&ctx->num_categories,0 ,sizeof (*ctx) - offsetof(struct rte_acl_ctx , num_categories)); }
lib/acl/acl_gen.c +2 -2
@@ -478,8 +478,8 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie,return -ERANGE ; } - mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , - ctx->socket_id); + mem = ctx->mem_hook.zalloc (ctx->name, total_size, +RTE_CACHE_LINE_SIZE , ctx->socket_id, ctx->mem_hook.udata);if (mem ==NULL ) { ACL_LOG(ERR, "allocation of %zu bytes on socket %d for %s failed",
lib/acl/rte_acl.c +44 -1
66 lines not on this page · raw message
lib/acl/rte_acl.h +53 -0
59 lines not on this page · raw message
Signature · 1 line
2.43.0
RE: [PATCH v8] acl: support custom memory allocators
Quoted 6 lines
Allow users to provide custom
memory allocation hooks for runtime memory inrte_acl_ctx, via structrte_acl_mem_hook.
Almost there, just few extra nits spotted.
Pls see below.
Quoted 65 lines
Key changes:
- Added structrte_acl_mem_hookwith zalloc, free, and udata.
- Addedrte_acl_set_mem_hook/rte_acl_get_mem_hookto set/get callbacks.
- Default allocation uses existing rte_zmalloc_socket/rte_free.
- Modified ACL code to call callbacks for runtime allocations instead of rte_zmalloc_socket/rte_free directly.v5:
- Remove temporary memory allocation callback for build stage.
- Introduce new API (rte_acl_set_mem_hook/rte_acl_get_mem_hook) instead of modifying existingrte_acl_configto preserve ABI compatibility.v6:
- Reworked API to meet consistency and naming conventions.
- Adjusted parameter order for better readability and alignment.
- Renamed internal variables for clarity and code consistency.v7:
- Switch the UT to use malloc/free.
- Update the documentation to clarify thatrte_acl_set_mem_hookmust be called beforerte_acl_build.v8:
- Fix UT code per review.Signed-off-by YongFeng Wang <[email protected]>--- app/test/test_acl.c | 107 ++++++++++++++++++ .../prog_guide/packet_classif_access_ctrl.rst | 31 +++++ lib/acl/acl.h | 1 + lib/acl/acl_bld.c | 2 +- lib/acl/acl_gen.c | 4 +- lib/acl/rte_acl.c | 45 +++++++- lib/acl/rte_acl.h | 53 +++++++++ 7 files changed, 239 insertions(+), 4 deletions(-)app/test/test_acl.c +20
-0@@ -1721,6 +1721,111 @@ test_u32_range(void)return rc; } +static struct rte_acl_ctx *acl_ctx; + +static void *running_alloc (char *name,size_t size, +size_t align,int32_t socket_id,void *udata) +{ +RTE_SET_USED (name); +RTE_SET_USED (socket_id); + +if (udata != acl_ctx) { +printf ("%s#%i udata mismatch!\n" , + __func__, __LINE__); +return NULL ; + } +void *addr =aligned_alloc (align, size); +if (addr ==NULL ) { +printf ("%s#%i alloc memory failed!\n" , + __func__, __LINE__); + } +return addr; +}
app/test/test_acl.c test_u32_range(void) · line 1753
Quoted 10 lines
app/test/test_acl.c +10
-0+ +static void running_free (void *ptr,void *udata) +{ +if (udata != acl_ctx) { +printf ("%s#%i udata mismatch!\n" , + __func__, __LINE__); +return ; + } +if (!ptr) +return ;
That's unnecessary, free() works perfect well with NULL pointers.
Quoted 15 lines
app/test/test_acl.c +15
-0+free (ptr); +} + +static int +test_mem_hook (void ) +{ +int i, ret; + + acl_ctx =rte_acl_create (&acl_param); +if (acl_ctx ==NULL ) { +printf ("Line %i: Error creating ACL context!\n" , __LINE__); +return -1 ; + } + +struct rte_acl_mem_hook mhook = {
app/test/test_acl.c test_u32_range(void) · line 1778
Quoted 10 lines
app/test/test_acl.c +10
-0+ .zalloc = running_alloc, + .free = running_free, + .udata = acl_ctx + }; + + ret =rte_acl_set_mem_hook (acl_ctx, &mhook); +if (ret !=0 ) { +printf ("Line %i: Error set mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx); +return 1 ;
Here and below, I suppose you either have to return negative value, or change the if condition below:
+ if (test_mem_hook() < 0)
app/test/test_acl.c test_u32_range(void) · line 1781
Quoted 3 lines
app/test/test_acl.c +3
-0+ } + +struct rte_acl_mem_hook new_hook;
My preference would be to have all local vars when possible to be defined at the start of the function/compound statement,
even though it is not mandatory any more.
Quoted 36 lines
app/test/test_acl.c +34
-0+memset (&new_hook,0 ,sizeof (struct rte_acl_mem_hook )); +if (rte_acl_get_mem_hook (acl_ctx, &new_hook) !=0 + ||memcmp (&mhook, &new_hook,sizeof (struct rte_acl_mem_hook )) !=0 ) { +printf ("Line %i: Error get mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx); +return 1 ; + } + + ret =0 ; +for (i =0 ; i <TEST_CLASSIFY_ITER ; i++) { + +if ((i &1 ) ==0 ) +rte_acl_reset (acl_ctx); +else +rte_acl_reset_rules (acl_ctx); + + ret =test_classify_buid (acl_ctx, acl_test_rules, +RTE_DIM (acl_test_rules)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: Adding rules to ACL context failed!\n", + __LINE__, i); +break ; + } + + ret =test_classify_run (acl_ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + +/* reset rules and make sure that classify still works ok. */ +rte_acl_reset_rules (acl_ctx);
app/test/test_acl.c test_u32_range(void) · line 1825
Quoted 10 lines
app/test/test_acl.c +10
-0+ ret =test_classify_run (acl_ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + } + +rte_acl_free (acl_ctx);
It would be safer to reset acl_ct to NULL
Quoted 249 lines
app/test/test_acl.c +3
-0+return ret; +} + static int
test_acl(void)
{app/test/test_acl.c +2
-0@@ -1742,6 +1847,8 @@ test_acl(void)return -1 ;if (test_u32_range () <0 )return -1 ; +if (test_mem_hook () <0 ) +return -1 ;return 0 ; }doc/guides/prog_guide/packet_classif_access_ctrl.rst +30
-0@@ -359,7 +359,38 @@ For example: ret = rte_acl_build(acx, &cfg); } +Custom Memory Hooks +~~~~~~~~~~~~~~~~~~~~ + +The ACL library supports custom memory allocation for runtime structures. +Applications can supply their own memory hooks through: + +.. code-block:: c + + int rte_acl_set_mem_hook(struct rte_acl_ctx *ctx, + const struct rte_acl_mem_hook *mhook); + + int rte_acl_get_mem_hook(const struct rte_acl_ctx *ctx, + struct rte_acl_mem_hook *mhook); + +The ``rte_acl_mem_hook`` structure defines memory hooks: + +.. code-block:: c + + struct rte_acl_mem_hook { + /** Allocate zero-initialized memory used during runtime. */ + void *(*zalloc)(char *name, size_t size, size_t align, int32_t socket_id, void *udata); + + /** Free memory previously allocated by zalloc(). */ + void (*free)(void *ptr, void *udata); + + /** User-provided context passed to allocation/free hooks. */ + void *udata; + }; + +Applications may use these hooks to allocate memory from custom pools or pre-allocated buffers.
doc/guides/prog_guide/packet_classif_access_ctrl.rst +1
-0+If no memory hook is provided, the ACL library uses rte_zmalloc_socket() internally. Classification methods ~~~~~~~~~~~~~~~~~~~~~~lib/acl/acl.h +1
-0@@ -174,6 +174,7 @@ struct rte_acl_ctx {uint32_t max_rules;uint32_t rule_sz;uint32_t num_rules; +struct rte_acl_mem_hook mem_hook;uint32_t num_categories;uint32_t num_tries;uint32_t match_index;lib/acl/acl_bld.c +1
-1@@ -779,7 +779,7 @@ acl_merge_trie(struct acl_build_context *context,static void acl_build_reset (struct rte_acl_ctx *ctx) { -rte_free (ctx->mem); + ctx->mem_hook.free (ctx->mem, ctx->mem_hook.udata);memset (&ctx->num_categories,0 ,sizeof (*ctx) - offsetof(struct rte_acl_ctx , num_categories)); }lib/acl/acl_gen.c +2
-2@@ -478,8 +478,8 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie,return -ERANGE ; } - mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , - ctx->socket_id); + mem = ctx->mem_hook.zalloc (ctx->name, total_size, +RTE_CACHE_LINE_SIZE , ctx->socket_id, ctx- >mem_hook.udata);if (mem ==NULL ) { ACL_LOG(ERR,lib/acl/acl_gen.c +0
-0"allocation of %zu bytes on socket %d for %s failed",lib/acl/rte_acl.c +14
-0@@ -264,6 +264,20 @@ acl_get_best_alg(void)return alg[i]; } +static void * +acl_mem_default_zalloc (char *name,size_t size,size_t align,int32_t socket_id,void *udata) +{ +RTE_SET_USED (udata); +return rte_zmalloc_socket (name, size, align, socket_id); +} + +static void +acl_mem_default_free (void *ptr,void *udata) +{ +RTE_SET_USED (udata); +rte_free (ptr); +} +RTE_EXPORT_SYMBOL (rte_acl_set_ctx_classify)extern int
rte_acl_set_ctx_classify(struct rte_acl_ctx *ctx, enum rte_acl_classify_alg alg)lib/acl/rte_acl.c +11
-1@@ -362,7 +376,7 @@ rte_acl_free(struct rte_acl_ctx *ctx)rte_mcfg_tailq_write_unlock (); -rte_free (ctx->mem); + ctx->mem_hook.free (ctx->mem, ctx->mem_hook.udata);rte_free (ctx);rte_free (te); }@@ -425,6 +439,9 @@ rte_acl_create(const struct rte_acl_param *param) ctx->rule_sz = param->rule_size; ctx->socket_id = param->socket_id; ctx->alg =acl_get_best_alg (); + ctx->mem_hook.zalloc = acl_mem_default_zalloc; + ctx->mem_hook.free = acl_mem_default_free; + ctx->mem_hook.udata =NULL ;strlcpy (ctx->name, param->name,sizeof (ctx->name)); te->data = (void *) ctx;@@ -555,3 +572,29 @@ rte_acl_list_dump(void) }rte_mcfg_tailq_read_unlock (); } + +/* +* Set memory allocation hooks for a given ACL context. +*/ +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_acl_set_mem_hook, 26.03) +int +rte_acl_set_mem_hook(struct rte_acl_ctx *acl, const struct rte_acl_mem_hook*mhook)
lib/acl/rte_acl.c +2
-0+{ + if (acl == NULL || mhook == NULL || mhook->zalloc == NULL || mhook-Quoted 1 line
free == NULL)
lib/acl/rte_acl.c +11
-0+return -EINVAL ; +memcpy (&acl->mem_hook, mhook,sizeof (struct rte_acl_mem_hook )); +return 0 ; +} + +/* +* Retrieve the memory allocation hooks assigned to the ACL context. +*/ +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_acl_get_mem_hook, 26.03) +int +rte_acl_get_mem_hook(const struct rte_acl_ctx *acl, struct rte_acl_mem_hook*mhook)
lib/acl/rte_acl.c +6
-0+{ +if (acl ==NULL || mhook ==NULL ) +return -EINVAL ; +memcpy (mhook, &acl->mem_hook,sizeof (struct rte_acl_mem_hook )); +return 0 ; +}lib/acl/rte_acl.h +53
-0@@ -136,6 +136,59 @@ struct rte_acl_param {/** @internal opaque ACL handle */ struct rte_acl_ctx ; +/** +* Memory allocation hooks for ACL runtime. +*/ +struct rte_acl_mem_hook { +/** Allocate zero-initialized memory used during runtime. */ +void *(*zalloc)(char *name,size_t size,size_t align,int32_t socket_id,void *udata); + +/** Free memory previously allocated by zalloc(). */ +void (*free)(void *ptr,void *udata); + +/** User-provided context passed to allocation/free hooks. */ +void *udata; +}; + +/** +* Set memory allocation hooks for a given ACL context. +* +* Applications may use these hooks to allocate memory from custom pools or +* pre-allocated buffers. If no memory hook is provided, the ACL library uses +* rte_zmalloc_socket() internally. +* +* This function must be called **before** rte_acl_build(). +* If the hook needs to be changed after a build, the ACL context must be reset +* first by invoking rte_acl_reset(), and only then can the memory hook be +* updated followed by another call to rte_acl_build(). +* +* @param acl +* The ACL context. +* @param mhook +* Pointer to the memory hook structure +* +* @return +* 0 on success. +* -EINVAL if parameters are invalid. +*/ +__rte_experimental +int rte_acl_set_mem_hook(struct rte_acl_ctx *acl, const struct rte_acl_mem_hook *mhook); + +/** +* Retrieve the memory allocation hooks assigned to the ACL context. +* +* @param acl +* The ACL context. +* @param mhook +* Output location for the current memory hook structure +* +* @return +* 0 on success. +* -EINVAL if parameters are invalid. +*/ +__rte_experimental +int rte_acl_get_mem_hook(const struct rte_acl_ctx *acl, struct rte_acl_mem_hook *mhook); +/**
* De-allocate all memory used by ACL context.
*
--
2.43.0
RE: [PATCH v8] acl: support custom memory allocators
Thanks for the comments.
These issues have been fixed in v9.
Quoted 1 line · Konstantin Ananyev
memset(&new_hook, 0, sizeof(struct rte_acl_mem_hook));
PATCH v9 1/1 acl: support custom memory allocators
In reply to mannywang(王永峰)Allow users to provide custom
memory allocation hooks for runtime memory in rte_acl_ctx, via struct rte_acl_mem_hook.
Key changes:
- Added struct rte_acl_mem_hook with zalloc, free, and udata.
- Added rte_acl_set_mem_hook / rte_acl_get_mem_hook to set/get callbacks.
- Default allocation uses existing rte_zmalloc_socket/rte_free.
- Modified ACL code to call callbacks for runtime allocations instead of rte_zmalloc_socket/rte_free directly.
v5:
- Remove temporary memory allocation callback for build stage.
- Introduce new API (rte_acl_set_mem_hook / rte_acl_get_mem_hook) instead of modifying existing rte_acl_config to preserve ABI compatibility.
v6:
- Reworked API to meet consistency and naming conventions.
- Adjusted parameter order for better readability and alignment.
- Renamed internal variables for clarity and code consistency.
v7:
- Switch the UT to use malloc/free.
- Update the documentation to clarify that rte_acl_set_mem_hook must be called before rte_acl_build.
v8:
- Fix UT code per review.
v9:
- Fix UT code per review.
--- app/test/test_acl.c | 105 ++++++++++++++++++ .../prog_guide/packet_classif_access_ctrl.rst | 31 ++++++ lib/acl/acl.h | 1 + lib/acl/acl_bld.c | 2 +- lib/acl/acl_gen.c | 4 +- lib/acl/rte_acl.c | 45 +++++++- lib/acl/rte_acl.h | 53 +++++++++ 7 files changed, 237 insertions(+), 4 deletions(-)
app/test/test_acl.c +105 -0
@@ -1721,6 +1721,109 @@ test_u32_range(void)return rc; } +static struct rte_acl_ctx *acl_ctx; + +static void *running_alloc (char *name,size_t size, +size_t align,int32_t socket_id,void *udata) +{ +RTE_SET_USED (name); +RTE_SET_USED (socket_id); + +if (udata != acl_ctx) { +printf ("%s#%i udata mismatch!\n" , + __func__, __LINE__); +return NULL ; + } +void *addr =aligned_alloc (align, size); +if (addr ==NULL ) { +printf ("%s#%i alloc memory failed!\n" , + __func__, __LINE__); + } +return addr; +} + +static void running_free (void *ptr,void *udata) +{ +if (udata != acl_ctx) { +printf ("%s#%i udata mismatch!\n" , + __func__, __LINE__); +return ; + } +free (ptr); +} + +static int +test_mem_hook (void ) +{ +int i, ret; +struct rte_acl_mem_hook new_hook; + + acl_ctx =rte_acl_create (&acl_param); +if (acl_ctx ==NULL ) { +printf ("Line %i: Error creating ACL context!\n" , __LINE__); +return -1 ; + } + +struct rte_acl_mem_hook mhook = { + .zalloc = running_alloc, + .free = running_free, + .udata = acl_ctx + }; + + ret =rte_acl_set_mem_hook (acl_ctx, &mhook); +if (ret !=0 ) { +printf ("Line %i: Error set mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx); +return -1 ; + } + +memset (&new_hook,0 ,sizeof (struct rte_acl_mem_hook )); +if (rte_acl_get_mem_hook (acl_ctx, &new_hook) !=0 + ||memcmp (&mhook, &new_hook,sizeof (struct rte_acl_mem_hook )) !=0 ) { +printf ("Line %i: Error get mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx); +return -1 ; + } + + ret =0 ; +for (i =0 ; i <TEST_CLASSIFY_ITER ; i++) { +if ((i &1 ) ==0 ) +rte_acl_reset (acl_ctx); +else +rte_acl_reset_rules (acl_ctx); + + ret =test_classify_buid (acl_ctx, acl_test_rules, +RTE_DIM (acl_test_rules)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: Adding rules to ACL context failed!\n" , + __LINE__, i); +break ; + } + + ret =test_classify_run (acl_ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + +/* reset rules and make sure that classify still works ok. */ +rte_acl_reset_rules (acl_ctx); + ret =test_classify_run (acl_ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + } + +rte_acl_free (acl_ctx); + acl_ctx =NULL ; +return ret; +} +static int test_acl (void ) {
@@ -1742,6 +1845,8 @@ test_acl(void)return -1 ;if (test_u32_range () <0 )return -1 ; +if (test_mem_hook () <0 ) +return -1 ;return 0 ; }
doc/guides/prog_guide/packet_classif_access_ctrl.rst +31 -0
@@ -359,7 +359,38 @@ For example: ret = rte_acl_build(acx, &cfg); } +Custom Memory Hooks +~~~~~~~~~~~~~~~~~~~~ + +The ACL library supports custom memory allocation for runtime structures. +Applications can supply their own memory hooks through: + +.. code-block:: c + + int rte_acl_set_mem_hook(struct rte_acl_ctx *ctx, + const struct rte_acl_mem_hook *mhook); + + int rte_acl_get_mem_hook(const struct rte_acl_ctx *ctx, + struct rte_acl_mem_hook *mhook); + +The ``rte_acl_mem_hook`` structure defines memory hooks: + +.. code-block:: c + + struct rte_acl_mem_hook { + /** Allocate zero-initialized memory used during runtime. */ + void *(*zalloc)(char *name, size_t size, size_t align, int32_t socket_id, void *udata); + + /** Free memory previously allocated by zalloc(). */ + void (*free)(void *ptr, void *udata); + + /** User-provided context passed to allocation/free hooks. */ + void *udata; + }; + +Applications may use these hooks to allocate memory from custom pools or pre-allocated buffers. +If no memory hook is provided, the ACL library uses rte_zmalloc_socket() internally. Classification methods ~~~~~~~~~~~~~~~~~~~~~~
lib/acl/acl.h +1 -0
@@ -174,6 +174,7 @@ struct rte_acl_ctx {uint32_t max_rules;uint32_t rule_sz;uint32_t num_rules; +struct rte_acl_mem_hook mem_hook;uint32_t num_categories;uint32_t num_tries;uint32_t match_index;
lib/acl/acl_bld.c +1 -1
@@ -779,7 +779,7 @@ acl_merge_trie(struct acl_build_context *context,static void acl_build_reset (struct rte_acl_ctx *ctx) { -rte_free (ctx->mem); + ctx->mem_hook.free (ctx->mem, ctx->mem_hook.udata);memset (&ctx->num_categories,0 ,sizeof (*ctx) - offsetof(struct rte_acl_ctx , num_categories)); }
lib/acl/acl_gen.c +2 -2
@@ -478,8 +478,8 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie,return -ERANGE ; } - mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , - ctx->socket_id); + mem = ctx->mem_hook.zalloc (ctx->name, total_size, +RTE_CACHE_LINE_SIZE , ctx->socket_id, ctx->mem_hook.udata);if (mem ==NULL ) { ACL_LOG(ERR, "allocation of %zu bytes on socket %d for %s failed",
lib/acl/rte_acl.c +44 -1
66 lines not on this page · raw message
lib/acl/rte_acl.h +53 -0
59 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v10 1/1 acl: support custom memory allocators
In reply to mannywang(王永峰)Allow users to provide custom
memory allocation hooks for runtime memory in rte_acl_ctx, via struct rte_acl_mem_hook.
Key changes:
- Added struct rte_acl_mem_hook with zalloc, free, and udata.
- Added rte_acl_set_mem_hook / rte_acl_get_mem_hook to set/get callbacks.
- Default allocation uses existing rte_zmalloc_socket/rte_free.
- Modified ACL code to call callbacks for runtime allocations instead of rte_zmalloc_socket/rte_free directly.
v5:
- Remove temporary memory allocation callback for build stage.
- Introduce new API (rte_acl_set_mem_hook / rte_acl_get_mem_hook) instead of modifying existing rte_acl_config to preserve ABI compatibility.
v6:
- Reworked API to meet consistency and naming conventions.
- Adjusted parameter order for better readability and alignment.
- Renamed internal variables for clarity and code consistency.
v7:
- Switch the UT to use malloc/free.
- Update the documentation to clarify that rte_acl_set_mem_hook must be called before rte_acl_build.
v8:
- Fix UT code per review.
v9:
- Fix UT code per review.
v10:
- Fix memory allocation alignment issue in UT code
--- app/test/test_acl.c | 106 ++++++++++++++++++ .../prog_guide/packet_classif_access_ctrl.rst | 31 +++++ lib/acl/acl.h | 1 + lib/acl/acl_bld.c | 2 +- lib/acl/acl_gen.c | 4 +- lib/acl/rte_acl.c | 45 +++++++- lib/acl/rte_acl.h | 53 +++++++++ 7 files changed, 238 insertions(+), 4 deletions(-)
app/test/test_acl.c +106 -0
@@ -1721,6 +1721,110 @@ test_u32_range(void)return rc; } +static struct rte_acl_ctx *acl_ctx; + +static void *running_alloc (char *name,size_t size, +size_t align,int32_t socket_id,void *udata) +{ +size_t aligned_size = (size + align -1 ) & ~(align -1 ); +RTE_SET_USED (name); +RTE_SET_USED (socket_id); + +if (udata != acl_ctx) { +printf ("%s#%i udata mismatch!\n" , + __func__, __LINE__); +return NULL ; + } +void *addr =aligned_alloc (align, aligned_size); +if (addr ==NULL ) { +printf ("%s#%i alloc memory failed!\n" , + __func__, __LINE__); + } +return addr; +} + +static void running_free (void *ptr,void *udata) +{ +if (udata != acl_ctx) { +printf ("%s#%i udata mismatch!\n" , + __func__, __LINE__); +return ; + } +free (ptr); +} + +static int +test_mem_hook (void ) +{ +int i, ret; +struct rte_acl_mem_hook new_hook; + + acl_ctx =rte_acl_create (&acl_param); +if (acl_ctx ==NULL ) { +printf ("Line %i: Error creating ACL context!\n" , __LINE__); +return -1 ; + } + +struct rte_acl_mem_hook mhook = { + .zalloc = running_alloc, + .free = running_free, + .udata = acl_ctx + }; + + ret =rte_acl_set_mem_hook (acl_ctx, &mhook); +if (ret !=0 ) { +printf ("Line %i: Error set mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx); +return -1 ; + } + +memset (&new_hook,0 ,sizeof (struct rte_acl_mem_hook )); +if (rte_acl_get_mem_hook (acl_ctx, &new_hook) !=0 + ||memcmp (&mhook, &new_hook,sizeof (struct rte_acl_mem_hook )) !=0 ) { +printf ("Line %i: Error get mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx); +return -1 ; + } + + ret =0 ; +for (i =0 ; i <TEST_CLASSIFY_ITER ; i++) { +if ((i &1 ) ==0 ) +rte_acl_reset (acl_ctx); +else +rte_acl_reset_rules (acl_ctx); + + ret =test_classify_buid (acl_ctx, acl_test_rules, +RTE_DIM (acl_test_rules)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: Adding rules to ACL context failed!\n" , + __LINE__, i); +break ; + } + + ret =test_classify_run (acl_ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + +/* reset rules and make sure that classify still works ok. */ +rte_acl_reset_rules (acl_ctx); + ret =test_classify_run (acl_ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + } + +rte_acl_free (acl_ctx); + acl_ctx =NULL ; +return ret; +} +static int test_acl (void ) {
@@ -1742,6 +1846,8 @@ test_acl(void)return -1 ;if (test_u32_range () <0 )return -1 ; +if (test_mem_hook () <0 ) +return -1 ;return 0 ; }
doc/guides/prog_guide/packet_classif_access_ctrl.rst +31 -0
@@ -359,7 +359,38 @@ For example: ret = rte_acl_build(acx, &cfg); } +Custom Memory Hooks +~~~~~~~~~~~~~~~~~~~~ + +The ACL library supports custom memory allocation for runtime structures. +Applications can supply their own memory hooks through: + +.. code-block:: c + + int rte_acl_set_mem_hook(struct rte_acl_ctx *ctx, + const struct rte_acl_mem_hook *mhook); + + int rte_acl_get_mem_hook(const struct rte_acl_ctx *ctx, + struct rte_acl_mem_hook *mhook); + +The ``rte_acl_mem_hook`` structure defines memory hooks: + +.. code-block:: c + + struct rte_acl_mem_hook { + /** Allocate zero-initialized memory used during runtime. */ + void *(*zalloc)(char *name, size_t size, size_t align, int32_t socket_id, void *udata); + + /** Free memory previously allocated by zalloc(). */ + void (*free)(void *ptr, void *udata); + + /** User-provided context passed to allocation/free hooks. */ + void *udata; + }; + +Applications may use these hooks to allocate memory from custom pools or pre-allocated buffers. +If no memory hook is provided, the ACL library uses rte_zmalloc_socket() internally. Classification methods ~~~~~~~~~~~~~~~~~~~~~~
lib/acl/acl.h +1 -0
@@ -174,6 +174,7 @@ struct rte_acl_ctx {uint32_t max_rules;uint32_t rule_sz;uint32_t num_rules; +struct rte_acl_mem_hook mem_hook;uint32_t num_categories;uint32_t num_tries;uint32_t match_index;
lib/acl/acl_bld.c +1 -1
@@ -779,7 +779,7 @@ acl_merge_trie(struct acl_build_context *context,static void acl_build_reset (struct rte_acl_ctx *ctx) { -rte_free (ctx->mem); + ctx->mem_hook.free (ctx->mem, ctx->mem_hook.udata);memset (&ctx->num_categories,0 ,sizeof (*ctx) - offsetof(struct rte_acl_ctx , num_categories)); }
lib/acl/acl_gen.c +2 -2
@@ -478,8 +478,8 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie,return -ERANGE ; } - mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , - ctx->socket_id); + mem = ctx->mem_hook.zalloc (ctx->name, total_size, +RTE_CACHE_LINE_SIZE , ctx->socket_id, ctx->mem_hook.udata);if (mem ==NULL ) { ACL_LOG(ERR, "allocation of %zu bytes on socket %d for %s failed",
lib/acl/rte_acl.c +44 -1
66 lines not on this page · raw message
lib/acl/rte_acl.h +53 -0
59 lines not on this page · raw message
Signature · 1 line
2.43.0
RE: [PATCH v10] acl: support custom memory allocators
Quoted 53 lines
Allow users to provide custom
memory allocation hooks for runtime memory inrte_acl_ctx, via structrte_acl_mem_hook.Key changes:
- Added structrte_acl_mem_hookwith zalloc, free, and udata.
- Addedrte_acl_set_mem_hook/rte_acl_get_mem_hookto set/get callbacks.
- Default allocation uses existing rte_zmalloc_socket/rte_free.
- Modified ACL code to call callbacks for runtime allocations instead of rte_zmalloc_socket/rte_free directly.v5:
- Remove temporary memory allocation callback for build stage.
- Introduce new API (rte_acl_set_mem_hook/rte_acl_get_mem_hook) instead of modifying existingrte_acl_configto preserve ABI compatibility.v6:
- Reworked API to meet consistency and naming conventions.
- Adjusted parameter order for better readability and alignment.
- Renamed internal variables for clarity and code consistency.v7:
- Switch the UT to use malloc/free.
- Update the documentation to clarify thatrte_acl_set_mem_hookmust be called beforerte_acl_build.v8:
- Fix UT code per review.v9:
- Fix UT code per review.v10:
- Fix memory allocation alignment issue in UT codeSigned-off-by YongFeng Wang <[email protected]>--- app/test/test_acl.c | 106 ++++++++++++++++++ .../prog_guide/packet_classif_access_ctrl.rst | 31 +++++ lib/acl/acl.h | 1 + lib/acl/acl_bld.c | 2 +- lib/acl/acl_gen.c | 4 +- lib/acl/rte_acl.c | 45 +++++++- lib/acl/rte_acl.h | 53 +++++++++ 7 files changed, 238 insertions(+), 4 deletions(-)app/test/test_acl.c +0
-0
app/test/test_acl.c test_u32_range(void) · line 1729
Quoted 9 lines
app/test/test_acl.c +6
-0@@ -1721,6 +1721,110 @@ test_u32_range(void)return rc; } +static struct rte_acl_ctx *acl_ctx; + +static void *running_alloc (char *name,size_t size, +size_t align,int32_t socket_id,void *udata) +{ +size_t aligned_size = (size + align -1 ) & ~(align -1 );
LGTM, just as a nit: there is a macro RTE_ALIGN_CEIL() in rte_common, that does exactly the same.
Quoted 346 lines
app/test/test_acl.c +98
-0+RTE_SET_USED (name); +RTE_SET_USED (socket_id); + +if (udata != acl_ctx) { +printf ("%s#%i udata mismatch!\n" , + __func__, __LINE__); +return NULL ; + } +void *addr =aligned_alloc (align, aligned_size); +if (addr ==NULL ) { +printf ("%s#%i alloc memory failed!\n" , + __func__, __LINE__); + } +return addr; +} + +static void running_free (void *ptr,void *udata) +{ +if (udata != acl_ctx) { +printf ("%s#%i udata mismatch!\n" , + __func__, __LINE__); +return ; + } +free (ptr); +} + +static int +test_mem_hook (void ) +{ +int i, ret; +struct rte_acl_mem_hook new_hook; + + acl_ctx =rte_acl_create (&acl_param); +if (acl_ctx ==NULL ) { +printf ("Line %i: Error creating ACL context!\n" , __LINE__); +return -1 ; + } + +struct rte_acl_mem_hook mhook = { + .zalloc = running_alloc, + .free = running_free, + .udata = acl_ctx + }; + + ret =rte_acl_set_mem_hook (acl_ctx, &mhook); +if (ret !=0 ) { +printf ("Line %i: Error set mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx); +return -1 ; + } + +memset (&new_hook,0 ,sizeof (struct rte_acl_mem_hook )); +if (rte_acl_get_mem_hook (acl_ctx, &new_hook) !=0 + ||memcmp (&mhook, &new_hook,sizeof (struct rte_acl_mem_hook )) !=0 ) { +printf ("Line %i: Error get mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx); +return -1 ; + } + + ret =0 ; +for (i =0 ; i <TEST_CLASSIFY_ITER ; i++) { +if ((i &1 ) ==0 ) +rte_acl_reset (acl_ctx); +else +rte_acl_reset_rules (acl_ctx); + + ret =test_classify_buid (acl_ctx, acl_test_rules, +RTE_DIM (acl_test_rules)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: Adding rules to ACL context failed!\n", + __LINE__, i); +break ; + } + + ret =test_classify_run (acl_ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + +/* reset rules and make sure that classify still works ok. */ +rte_acl_reset_rules (acl_ctx); + ret =test_classify_run (acl_ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + } + +rte_acl_free (acl_ctx); + acl_ctx =NULL ; +return ret; +} + static int
test_acl(void)
{app/test/test_acl.c +2
-0@@ -1742,6 +1846,8 @@ test_acl(void)return -1 ;if (test_u32_range () <0 )return -1 ; +if (test_mem_hook () <0 ) +return -1 ;return 0 ; }doc/guides/prog_guide/packet_classif_access_ctrl.rst +30
-0@@ -359,7 +359,38 @@ For example: ret = rte_acl_build(acx, &cfg); } +Custom Memory Hooks +~~~~~~~~~~~~~~~~~~~~ + +The ACL library supports custom memory allocation for runtime structures. +Applications can supply their own memory hooks through: + +.. code-block:: c + + int rte_acl_set_mem_hook(struct rte_acl_ctx *ctx, + const struct rte_acl_mem_hook *mhook); + + int rte_acl_get_mem_hook(const struct rte_acl_ctx *ctx, + struct rte_acl_mem_hook *mhook); + +The ``rte_acl_mem_hook`` structure defines memory hooks: + +.. code-block:: c + + struct rte_acl_mem_hook { + /** Allocate zero-initialized memory used during runtime. */ + void *(*zalloc)(char *name, size_t size, size_t align, int32_t socket_id, void *udata); + + /** Free memory previously allocated by zalloc(). */ + void (*free)(void *ptr, void *udata); + + /** User-provided context passed to allocation/free hooks. */ + void *udata; + }; + +Applications may use these hooks to allocate memory from custom pools or pre-allocated buffers.
doc/guides/prog_guide/packet_classif_access_ctrl.rst +1
-0+If no memory hook is provided, the ACL library uses rte_zmalloc_socket() internally. Classification methods ~~~~~~~~~~~~~~~~~~~~~~lib/acl/acl.h +1
-0@@ -174,6 +174,7 @@ struct rte_acl_ctx {uint32_t max_rules;uint32_t rule_sz;uint32_t num_rules; +struct rte_acl_mem_hook mem_hook;uint32_t num_categories;uint32_t num_tries;uint32_t match_index;lib/acl/acl_bld.c +1
-1@@ -779,7 +779,7 @@ acl_merge_trie(struct acl_build_context *context,static void acl_build_reset (struct rte_acl_ctx *ctx) { -rte_free (ctx->mem); + ctx->mem_hook.free (ctx->mem, ctx->mem_hook.udata);memset (&ctx->num_categories,0 ,sizeof (*ctx) - offsetof(struct rte_acl_ctx , num_categories)); }lib/acl/acl_gen.c +2
-2@@ -478,8 +478,8 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie,return -ERANGE ; } - mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , - ctx->socket_id); + mem = ctx->mem_hook.zalloc (ctx->name, total_size, +RTE_CACHE_LINE_SIZE , ctx->socket_id, ctx- >mem_hook.udata);if (mem ==NULL ) { ACL_LOG(ERR,lib/acl/acl_gen.c +0
-0"allocation of %zu bytes on socket %d for %s failed",lib/acl/rte_acl.c +14
-0@@ -264,6 +264,20 @@ acl_get_best_alg(void)return alg[i]; } +static void * +acl_mem_default_zalloc (char *name,size_t size,size_t align,int32_t socket_id,void *udata) +{ +RTE_SET_USED (udata); +return rte_zmalloc_socket (name, size, align, socket_id); +} + +static void +acl_mem_default_free (void *ptr,void *udata) +{ +RTE_SET_USED (udata); +rte_free (ptr); +} +RTE_EXPORT_SYMBOL (rte_acl_set_ctx_classify)extern int
rte_acl_set_ctx_classify(struct rte_acl_ctx *ctx, enum rte_acl_classify_alg alg)lib/acl/rte_acl.c +11
-1@@ -362,7 +376,7 @@ rte_acl_free(struct rte_acl_ctx *ctx)rte_mcfg_tailq_write_unlock (); -rte_free (ctx->mem); + ctx->mem_hook.free (ctx->mem, ctx->mem_hook.udata);rte_free (ctx);rte_free (te); }@@ -425,6 +439,9 @@ rte_acl_create(const struct rte_acl_param *param) ctx->rule_sz = param->rule_size; ctx->socket_id = param->socket_id; ctx->alg =acl_get_best_alg (); + ctx->mem_hook.zalloc = acl_mem_default_zalloc; + ctx->mem_hook.free = acl_mem_default_free; + ctx->mem_hook.udata =NULL ;strlcpy (ctx->name, param->name,sizeof (ctx->name)); te->data = (void *) ctx;@@ -555,3 +572,29 @@ rte_acl_list_dump(void) }rte_mcfg_tailq_read_unlock (); } + +/* +* Set memory allocation hooks for a given ACL context. +*/ +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_acl_set_mem_hook, 26.03) +int +rte_acl_set_mem_hook(struct rte_acl_ctx *acl, const struct rte_acl_mem_hook*mhook)
lib/acl/rte_acl.c +2
-0+{ + if (acl == NULL || mhook == NULL || mhook->zalloc == NULL || mhook-Quoted 1 line
free == NULL)
lib/acl/rte_acl.c +11
-0+return -EINVAL ; +memcpy (&acl->mem_hook, mhook,sizeof (struct rte_acl_mem_hook )); +return 0 ; +} + +/* +* Retrieve the memory allocation hooks assigned to the ACL context. +*/ +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_acl_get_mem_hook, 26.03) +int +rte_acl_get_mem_hook(const struct rte_acl_ctx *acl, struct rte_acl_mem_hook*mhook)
lib/acl/rte_acl.c +6
-0+{ +if (acl ==NULL || mhook ==NULL ) +return -EINVAL ; +memcpy (mhook, &acl->mem_hook,sizeof (struct rte_acl_mem_hook )); +return 0 ; +}lib/acl/rte_acl.h +53
-0@@ -136,6 +136,59 @@ struct rte_acl_param {/** @internal opaque ACL handle */ struct rte_acl_ctx ; +/** +* Memory allocation hooks for ACL runtime. +*/ +struct rte_acl_mem_hook { +/** Allocate zero-initialized memory used during runtime. */ +void *(*zalloc)(char *name,size_t size,size_t align,int32_t socket_id,void *udata); + +/** Free memory previously allocated by zalloc(). */ +void (*free)(void *ptr,void *udata); + +/** User-provided context passed to allocation/free hooks. */ +void *udata; +}; + +/** +* Set memory allocation hooks for a given ACL context. +* +* Applications may use these hooks to allocate memory from custom pools or +* pre-allocated buffers. If no memory hook is provided, the ACL library uses +* rte_zmalloc_socket() internally. +* +* This function must be called **before** rte_acl_build(). +* If the hook needs to be changed after a build, the ACL context must be reset +* first by invoking rte_acl_reset(), and only then can the memory hook be +* updated followed by another call to rte_acl_build(). +* +* @param acl +* The ACL context. +* @param mhook +* Pointer to the memory hook structure +* +* @return +* 0 on success. +* -EINVAL if parameters are invalid. +*/ +__rte_experimental +int rte_acl_set_mem_hook(struct rte_acl_ctx *acl, const struct rte_acl_mem_hook *mhook); + +/** +* Retrieve the memory allocation hooks assigned to the ACL context. +* +* @param acl +* The ACL context. +* @param mhook +* Output location for the current memory hook structure +* +* @return +* 0 on success. +* -EINVAL if parameters are invalid. +*/ +__rte_experimental +int rte_acl_get_mem_hook(const struct rte_acl_ctx *acl, struct rte_acl_mem_hook *mhook); +/**
* De-allocate all memory used by ACL context.
*
--
2.43.0
RE: [PATCH v10] acl: support custom memory allocators
Thanks for the review, v11 sent addressing the comments.
Quoted 1 line · Konstantin Ananyev
PATCH v11 1/1 acl: support custom memory allocators
In reply to mannywang(王永峰)Allow users to provide custom
memory allocation hooks for runtime memory in rte_acl_ctx, via struct rte_acl_mem_hook.
Key changes:
- Added struct rte_acl_mem_hook with zalloc, free, and udata.
- Added rte_acl_set_mem_hook / rte_acl_get_mem_hook to set/get callbacks.
- Default allocation uses existing rte_zmalloc_socket/rte_free.
- Modified ACL code to call callbacks for runtime allocations instead of rte_zmalloc_socket/rte_free directly.
v5:
- Remove temporary memory allocation callback for build stage.
- Introduce new API (rte_acl_set_mem_hook / rte_acl_get_mem_hook) instead of modifying existing rte_acl_config to preserve ABI compatibility.
v6:
- Reworked API to meet consistency and naming conventions.
- Adjusted parameter order for better readability and alignment.
- Renamed internal variables for clarity and code consistency.
v7:
- Switch the UT to use malloc/free.
- Update the documentation to clarify that rte_acl_set_mem_hook must be called before rte_acl_build.
v8:
- Fix UT code per review.
v9:
- Fix UT code per review.
v10:
- Fix memory allocation alignment issue in UT code
v11:
- Use RTE_ALIGN_CEIL() instead of open-coded alignment
--- app/test/test_acl.c | 106 ++++++++++++++++++ .../prog_guide/packet_classif_access_ctrl.rst | 31 +++++ lib/acl/acl.h | 1 + lib/acl/acl_bld.c | 2 +- lib/acl/acl_gen.c | 4 +- lib/acl/rte_acl.c | 45 +++++++- lib/acl/rte_acl.h | 53 +++++++++ 7 files changed, 238 insertions(+), 4 deletions(-)
app/test/test_acl.c +106 -0
@@ -1721,6 +1721,110 @@ test_u32_range(void)return rc; } +static struct rte_acl_ctx *acl_ctx; + +static void *running_alloc (char *name,size_t size, +size_t align,int32_t socket_id,void *udata) +{ +size_t aligned_size =RTE_ALIGN_CEIL (size, align); +RTE_SET_USED (name); +RTE_SET_USED (socket_id); + +if (udata != acl_ctx) { +printf ("%s#%i udata mismatch!\n" , + __func__, __LINE__); +return NULL ; + } +void *addr =aligned_alloc (align, aligned_size); +if (addr ==NULL ) { +printf ("%s#%i alloc memory failed!\n" , + __func__, __LINE__); + } +return addr; +} + +static void running_free (void *ptr,void *udata) +{ +if (udata != acl_ctx) { +printf ("%s#%i udata mismatch!\n" , + __func__, __LINE__); +return ; + } +free (ptr); +} + +static int +test_mem_hook (void ) +{ +int i, ret; +struct rte_acl_mem_hook new_hook; + + acl_ctx =rte_acl_create (&acl_param); +if (acl_ctx ==NULL ) { +printf ("Line %i: Error creating ACL context!\n" , __LINE__); +return -1 ; + } + +struct rte_acl_mem_hook mhook = { + .zalloc = running_alloc, + .free = running_free, + .udata = acl_ctx + }; + + ret =rte_acl_set_mem_hook (acl_ctx, &mhook); +if (ret !=0 ) { +printf ("Line %i: Error set mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx); +return -1 ; + } + +memset (&new_hook,0 ,sizeof (struct rte_acl_mem_hook )); +if (rte_acl_get_mem_hook (acl_ctx, &new_hook) !=0 + ||memcmp (&mhook, &new_hook,sizeof (struct rte_acl_mem_hook )) !=0 ) { +printf ("Line %i: Error get mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx); +return -1 ; + } + + ret =0 ; +for (i =0 ; i <TEST_CLASSIFY_ITER ; i++) { +if ((i &1 ) ==0 ) +rte_acl_reset (acl_ctx); +else +rte_acl_reset_rules (acl_ctx); + + ret =test_classify_buid (acl_ctx, acl_test_rules, +RTE_DIM (acl_test_rules)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: Adding rules to ACL context failed!\n" , + __LINE__, i); +break ; + } + + ret =test_classify_run (acl_ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + +/* reset rules and make sure that classify still works ok. */ +rte_acl_reset_rules (acl_ctx); + ret =test_classify_run (acl_ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + } + +rte_acl_free (acl_ctx); + acl_ctx =NULL ; +return ret; +} +static int test_acl (void ) {
@@ -1742,6 +1846,8 @@ test_acl(void)return -1 ;if (test_u32_range () <0 )return -1 ; +if (test_mem_hook () <0 ) +return -1 ;return 0 ; }
doc/guides/prog_guide/packet_classif_access_ctrl.rst +31 -0
@@ -359,7 +359,38 @@ For example: ret = rte_acl_build(acx, &cfg); } +Custom Memory Hooks +~~~~~~~~~~~~~~~~~~~~ + +The ACL library supports custom memory allocation for runtime structures. +Applications can supply their own memory hooks through: + +.. code-block:: c + + int rte_acl_set_mem_hook(struct rte_acl_ctx *ctx, + const struct rte_acl_mem_hook *mhook); + + int rte_acl_get_mem_hook(const struct rte_acl_ctx *ctx, + struct rte_acl_mem_hook *mhook); + +The ``rte_acl_mem_hook`` structure defines memory hooks: + +.. code-block:: c + + struct rte_acl_mem_hook { + /** Allocate zero-initialized memory used during runtime. */ + void *(*zalloc)(char *name, size_t size, size_t align, int32_t socket_id, void *udata); + + /** Free memory previously allocated by zalloc(). */ + void (*free)(void *ptr, void *udata); + + /** User-provided context passed to allocation/free hooks. */ + void *udata; + }; + +Applications may use these hooks to allocate memory from custom pools or pre-allocated buffers. +If no memory hook is provided, the ACL library uses rte_zmalloc_socket() internally. Classification methods ~~~~~~~~~~~~~~~~~~~~~~
lib/acl/acl.h +1 -0
@@ -174,6 +174,7 @@ struct rte_acl_ctx {uint32_t max_rules;uint32_t rule_sz;uint32_t num_rules; +struct rte_acl_mem_hook mem_hook;uint32_t num_categories;uint32_t num_tries;uint32_t match_index;
lib/acl/acl_bld.c +1 -1
@@ -779,7 +779,7 @@ acl_merge_trie(struct acl_build_context *context,static void acl_build_reset (struct rte_acl_ctx *ctx) { -rte_free (ctx->mem); + ctx->mem_hook.free (ctx->mem, ctx->mem_hook.udata);memset (&ctx->num_categories,0 ,sizeof (*ctx) - offsetof(struct rte_acl_ctx , num_categories)); }
lib/acl/acl_gen.c +2 -2
@@ -478,8 +478,8 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie,return -ERANGE ; } - mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , - ctx->socket_id); + mem = ctx->mem_hook.zalloc (ctx->name, total_size, +RTE_CACHE_LINE_SIZE , ctx->socket_id, ctx->mem_hook.udata);if (mem ==NULL ) { ACL_LOG(ERR, "allocation of %zu bytes on socket %d for %s failed",
lib/acl/rte_acl.c +44 -1
66 lines not on this page · raw message
lib/acl/rte_acl.h +53 -0
59 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v12 1/1 acl: support custom memory allocators
Allow users to provide custom
memory allocation hooks for runtime memory in rte_acl_ctx, via struct rte_acl_mem_hook.
Key changes:
- Added struct rte_acl_mem_hook with zalloc, free, and udata.
- Added rte_acl_set_mem_hook / rte_acl_get_mem_hook to set/get callbacks.
- Default allocation uses existing rte_zmalloc_socket/rte_free.
- Modified ACL code to call callbacks for runtime allocations instead of rte_zmalloc_socket/rte_free directly.
v5:
- Remove temporary memory allocation callback for build stage.
- Introduce new API (rte_acl_set_mem_hook / rte_acl_get_mem_hook) instead of modifying existing rte_acl_config to preserve ABI compatibility.
v6:
- Reworked API to meet consistency and naming conventions.
- Adjusted parameter order for better readability and alignment.
- Renamed internal variables for clarity and code consistency.
v7:
- Switch the UT to use malloc/free.
- Update the documentation to clarify that rte_acl_set_mem_hook must be called before rte_acl_build.
v8:
- Fix UT code per review.
v9:
- Fix UT code per review.
v10:
- Fix memory allocation alignment issue in UT code.
v11:
- Use RTE_ALIGN_CEIL() instead of open-coded alignment.
v12:
- Add release notes.
--- app/test/test_acl.c | 106 ++++++++++++++++++ .../prog_guide/packet_classif_access_ctrl.rst | 31 +++++ doc/guides/rel_notes/release_26_03.rst | 1 + lib/acl/acl.h | 1 + lib/acl/acl_bld.c | 2 +- lib/acl/acl_gen.c | 4 +- lib/acl/rte_acl.c | 45 +++++++- lib/acl/rte_acl.h | 53 +++++++++ 8 files changed, 239 insertions(+), 4 deletions(-)
app/test/test_acl.c +106 -0
@@ -1721,6 +1721,110 @@ test_u32_range(void)return rc; } +static struct rte_acl_ctx *acl_ctx; + +static void *running_alloc (char *name,size_t size, +size_t align,int32_t socket_id,void *udata) +{ +size_t aligned_size =RTE_ALIGN_CEIL (size, align); +RTE_SET_USED (name); +RTE_SET_USED (socket_id); + +if (udata != acl_ctx) { +printf ("%s#%i udata mismatch!\n" , + __func__, __LINE__); +return NULL ; + } +void *addr =aligned_alloc (align, aligned_size); +if (addr ==NULL ) { +printf ("%s#%i alloc memory failed!\n" , + __func__, __LINE__); + } +return addr; +} + +static void running_free (void *ptr,void *udata) +{ +if (udata != acl_ctx) { +printf ("%s#%i udata mismatch!\n" , + __func__, __LINE__); +return ; + } +free (ptr); +} + +static int +test_mem_hook (void ) +{ +int i, ret; +struct rte_acl_mem_hook new_hook; + + acl_ctx =rte_acl_create (&acl_param); +if (acl_ctx ==NULL ) { +printf ("Line %i: Error creating ACL context!\n" , __LINE__); +return -1 ; + } + +struct rte_acl_mem_hook mhook = { + .zalloc = running_alloc, + .free = running_free, + .udata = acl_ctx + }; + + ret =rte_acl_set_mem_hook (acl_ctx, &mhook); +if (ret !=0 ) { +printf ("Line %i: Error set mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx); +return -1 ; + } + +memset (&new_hook,0 ,sizeof (struct rte_acl_mem_hook )); +if (rte_acl_get_mem_hook (acl_ctx, &new_hook) !=0 + ||memcmp (&mhook, &new_hook,sizeof (struct rte_acl_mem_hook )) !=0 ) { +printf ("Line %i: Error get mem hook for acl context!\n" , __LINE__); +rte_acl_free (acl_ctx); +return -1 ; + } + + ret =0 ; +for (i =0 ; i <TEST_CLASSIFY_ITER ; i++) { +if ((i &1 ) ==0 ) +rte_acl_reset (acl_ctx); +else +rte_acl_reset_rules (acl_ctx); + + ret =test_classify_buid (acl_ctx, acl_test_rules, +RTE_DIM (acl_test_rules)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: Adding rules to ACL context failed!\n" , + __LINE__, i); +break ; + } + + ret =test_classify_run (acl_ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + +/* reset rules and make sure that classify still works ok. */ +rte_acl_reset_rules (acl_ctx); + ret =test_classify_run (acl_ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + } + +rte_acl_free (acl_ctx); + acl_ctx =NULL ; +return ret; +} +static int test_acl (void ) {
@@ -1742,6 +1846,8 @@ test_acl(void)return -1 ;if (test_u32_range () <0 )return -1 ; +if (test_mem_hook () <0 ) +return -1 ;return 0 ; }
doc/guides/prog_guide/packet_classif_access_ctrl.rst +31 -0
@@ -359,7 +359,38 @@ For example: ret = rte_acl_build(acx, &cfg); } +Custom Memory Hooks +~~~~~~~~~~~~~~~~~~~~ + +The ACL library supports custom memory allocation for runtime structures. +Applications can supply their own memory hooks through: + +.. code-block:: c + + int rte_acl_set_mem_hook(struct rte_acl_ctx *ctx, + const struct rte_acl_mem_hook *mhook); + + int rte_acl_get_mem_hook(const struct rte_acl_ctx *ctx, + struct rte_acl_mem_hook *mhook); + +The ``rte_acl_mem_hook`` structure defines memory hooks: + +.. code-block:: c + + struct rte_acl_mem_hook { + /** Allocate zero-initialized memory used during runtime. */ + void *(*zalloc)(char *name, size_t size, size_t align, int32_t socket_id, void *udata); + + /** Free memory previously allocated by zalloc(). */ + void (*free)(void *ptr, void *udata); + + /** User-provided context passed to allocation/free hooks. */ + void *udata; + }; + +Applications may use these hooks to allocate memory from custom pools or pre-allocated buffers. +If no memory hook is provided, the ACL library uses rte_zmalloc_socket() internally. Classification methods ~~~~~~~~~~~~~~~~~~~~~~
doc/guides/rel_notes/release_26_03.rst +1 -0
@@ -55,6 +55,7 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* **ACL library**: Added support for custom memory allocation hooks (`rte_acl_mem_hook`) allowing applications to provide their own allocation and free functions for ACL runtime memory. Removed Items -------------
lib/acl/acl.h +1 -0
@@ -174,6 +174,7 @@ struct rte_acl_ctx {uint32_t max_rules;uint32_t rule_sz;uint32_t num_rules; +struct rte_acl_mem_hook mem_hook;uint32_t num_categories;uint32_t num_tries;uint32_t match_index;
lib/acl/acl_bld.c +1 -1
@@ -779,7 +779,7 @@ acl_merge_trie(struct acl_build_context *context,static void acl_build_reset (struct rte_acl_ctx *ctx) { -rte_free (ctx->mem); + ctx->mem_hook.free (ctx->mem, ctx->mem_hook.udata);memset (&ctx->num_categories,0 ,sizeof (*ctx) - offsetof(struct rte_acl_ctx , num_categories)); }
lib/acl/acl_gen.c +2 -2
@@ -478,8 +478,8 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie,return -ERANGE ; } - mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , - ctx->socket_id); + mem = ctx->mem_hook.zalloc (ctx->name, total_size, +RTE_CACHE_LINE_SIZE , ctx->socket_id, ctx->mem_hook.udata);if (mem ==NULL ) { ACL_LOG(ERR, "allocation of %zu bytes on socket %d for %s failed",
lib/acl/rte_acl.c +44 -1
66 lines not on this page · raw message
lib/acl/rte_acl.h +53 -0
59 lines not on this page · raw message
Signature · 1 line
2.43.0
Thomas Monjalon
AppliedRe: [PATCH v12] acl: support custom memory allocators
17/12/2025 14:06, mannywang(王永峰):
Quoted 10 lines
Allow users to provide custom
memory allocation hooks for runtime memory inrte_acl_ctx, via structrte_acl_mem_hook.Key changes:
- Added structrte_acl_mem_hookwith zalloc, free, and udata.
- Addedrte_acl_set_mem_hook/rte_acl_get_mem_hookto set/get callbacks.
- Default allocation uses existing rte_zmalloc_socket/rte_free.
- Modified ACL code to call callbacks for runtime allocations instead of rte_zmalloc_socket/rte_free directly.
I've simplified the doc to avoid copying what is already in doxygen and may become outdated.
Applied, thanks.
Re: [Internet]Re: [PATCH v5] acl: support custom memory allocators in rte_acl_build
In reply to fengchengwenThanks for the review and comments.
Regarding the placement of mem_hook (or mem_cb):
I didn’t adopt the suggestion to move this field after num_categories, because the memory region after num_categories is cleared during rte_acl_reset(). To preserve the callback data across resets, the field must be located before num_categories.
All the other comments have been addressed in this version.
Quoted 405 lines · fengchengwen
commit header could simply as: acl: support custom memory allocator
Quoted 31 lines · mannywang(王永峰)
Allow users to provide custom
memory allocation callbacks for runtime memory inrte_acl_ctx, via structrte_acl_mem_cb.Key changes:
- Added structrte_acl_mem_cbwith zalloc, free, and udata.
- Addedrte_acl_set_mem_cb()/rte_acl_get_mem_cb()to set/get callbacks.
- Default allocation uses existing rte_zmalloc_socket/rte_free.
- Modified ACL code to call callbacks for runtime allocations instead of rte_zmalloc_socket/rte_free directly.v5:
- Remove temporary memory allocation callback for build stage.
- Introduce new API (rte_acl_set_mem_cb/rte_acl_get_mem_cb) instead of modifying existingrte_acl_configto preserve ABI compatibility.Signed-off-by YongFeng Wang <[email protected]>--- app/test/test_acl.c | 121 ++++++++++++++++++++++++++++++++++++++++++++ lib/acl/acl.h | 1 + lib/acl/acl_bld.c | 2 +- lib/acl/acl_gen.c | 4 +- lib/acl/rte_acl.c | 45 +++++++++++++++- lib/acl/rte_acl.h | 42 +++++++++++++++ 6 files changed, 211 insertions(+), 4 deletions(-)app/test/test_acl.c +0
-0app/test/test_acl.c test_u32_range(void) · line 1724
Quoted 4 lines
app/test/test_acl.c +1
-0@@ -1721,6 +1721,125 @@ test_u32_range(void)return rc; } +struct acl_ctx_wrapper_t {no need add post-fix _t
Quoted 6 lines
app/test/test_acl.c +6
-0+struct rte_acl_ctx *ctx; +void *running_buf; +bool running_buf_using; +}; + +#define ACL_RUNNING_BUF_SIZE (10 * 1024 * 1024)app/test/test_acl.c test_u32_range(void) · line 1740
Quoted 10 lines
app/test/test_acl.c +10
-0+ +static void *running_alloc (void *udata,char *name,size_t size, +size_t align,int32_t socket_id) +{ + (void )align; + (void )name; + (void )socket_id; +if (size >ACL_RUNNING_BUF_SIZE ) +return NULL ; +struct acl_ctx_wrapper_t *gwlb_acl_ctx = (struct acl_ctx_wrapper_t *)udata;gwlb - what the meaning? suggest simple
acl_ctxor just ctx.app/test/test_acl.c test_u32_range(void) · line 1747
Quoted 7 lines
app/test/test_acl.c +7
-0+if (gwlb_acl_ctx->running_buf_using) +return NULL ; +printf ("running memory alloc for acl context, size=%zu, pointer=%p\n" , + size, + gwlb_acl_ctx->running_buf); +memset (gwlb_acl_ctx->running_buf,0 , size); + gwlb_acl_ctx->running_buf_using = true;this allocator don't support conti alloc, it will has dependent for acl-library, how about use Dmitry Kozlyuk's comment for [PATCH v3], that is use
rte_malloc_heap_createQuoted 7 lines
app/test/test_acl.c +7
-0+return gwlb_acl_ctx->running_buf; +} + +static void running_free (void *udata,void *ptr) +{ +if (!ptr) +return ;app/test/test_acl.c test_u32_range(void) · line 1764
Quoted 10 lines
app/test/test_acl.c +10
-0+struct acl_ctx_wrapper_t *gwlb_acl_ctx = (struct acl_ctx_wrapper_t *)udata; +printf ("running memory free, pointer=%p\n" , ptr); + gwlb_acl_ctx->running_buf_using = false; +} + +static int +test_mem_cb (void ) +{ +int i, ret; +struct acl_ctx_wrapper_t g_acl_ctx_wrapper;no need add
g_acl_prefix, and suggest zero default (structacl_ctx_wrapperctx_wrap= {0})app/test/test_acl.c test_u32_range(void) · line 1776
Quoted 12 lines
app/test/test_acl.c +12
-0+ g_acl_ctx_wrapper.ctx =rte_acl_create (&acl_param); +if (g_acl_ctx_wrapper.ctx ==NULL ) { +printf ("Line %i: Error creating ACL context!\n" , __LINE__); +return -1 ; + } + g_acl_ctx_wrapper.running_buf =rte_zmalloc_socket ( +"test_acl" , +ACL_RUNNING_BUF_SIZE , +RTE_CACHE_LINE_SIZE , +SOCKET_ID_ANY ); +if (!g_acl_ctx_wrapper.running_buf) { +printf ("Line %i: Error allocing running buf for acl context!\n" , __LINE__);please add resource rollback, the same as following.
app/test/test_acl.c test_u32_range(void) · line 1787
Quoted 11 lines
app/test/test_acl.c +11
-0+return 1 ; + } + g_acl_ctx_wrapper.running_buf_using = false; + +struct rte_acl_mem_cb mcb = { + .zalloc = running_alloc, + .free = running_free, + .udata = &g_acl_ctx_wrapper + }; + ret =rte_acl_set_mem_cb (g_acl_ctx_wrapper.ctx, &mcb); +if (ret) {please use explicit exp, e.g. if (ret != 0), the same as other place.
Quoted 6 lines
app/test/test_acl.c +6
-0+printf ("Line %i: Error set mem cb for acl context!\n" , __LINE__); +return 1 ; + } +struct rte_acl_mem_cb new_mcb; +memset (&new_mcb,0 ,sizeof (struct rte_acl_mem_cb )); + ret =rte_acl_get_mem_cb (g_acl_ctx_wrapper.ctx, &new_mcb);app/test/test_acl.c test_u32_range(void) · line 1803
Quoted 10 lines
app/test/test_acl.c +10
-0+if (ret) { +printf ("Line %i: Error get mem cb for acl context!\n" , __LINE__); +return 1 ; + } +if (memcmp (&mcb, &new_mcb,sizeof (struct rte_acl_mem_cb )) !=0 ) { +printf ("Line %i: Error get mem cb for acl context!\n" , __LINE__); +return 1 ; + } + ret =0 ; +for (i =0 ; i !=TEST_CLASSIFY_ITER ; i++) {why not i <
TEST_CLASSIFY_ITER?app/test/test_acl.c test_u32_range(void) · line 1814
Quoted 11 lines
app/test/test_acl.c +11
-0+ +if ((i &1 ) ==0 ) +rte_acl_reset (g_acl_ctx_wrapper.ctx); +else +rte_acl_reset_rules (g_acl_ctx_wrapper.ctx); + + ret =test_classify_buid (g_acl_ctx_wrapper.ctx, acl_test_rules, +RTE_DIM (acl_test_rules)); +if (ret !=0 ) { + printf("Line %i, iter: %d: " + "Adding rules to ACL context failed!\n",no need extra line for log-string.
Quoted 45 lines
app/test/test_acl.c +30
-0+ __LINE__, i); +break ; + } + + ret =test_classify_run (g_acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + +/* reset rules and make sure that classify still works ok. */ +rte_acl_reset_rules (g_acl_ctx_wrapper.ctx); + ret =test_classify_run (g_acl_ctx_wrapper.ctx, acl_test_data, +RTE_DIM (acl_test_data)); +if (ret !=0 ) { +printf ("Line %i, iter: %d: %s failed!\n" , + __LINE__, i, __func__); +break ; + } + } + +rte_acl_free (g_acl_ctx_wrapper.ctx); +rte_free (g_acl_ctx_wrapper.running_buf); +return ret; +} +static int test_acl (void ) {@@ -1742,6 +1861,8 @@ test_acl(void)return -1 ;if (test_u32_range () <0 )return -1 ; +if (test_mem_cb () <0 ) +return -1 ;return 0 ; }lib/acl/acl.h +0
-0lib/acl/acl.h struct rte_acl_ctx { · line 177
Quoted 4 lines
lib/acl/acl.h +1
-0@@ -174,6 +174,7 @@ struct rte_acl_ctx {uint32_t max_rules;uint32_t rule_sz;uint32_t num_rules; +struct rte_acl_mem_cb mem_cb;why not place near *mem/mem_sz field ?
Quoted 36 lines
lib/acl/acl.h +0
-0uint32_t num_categories;uint32_t num_tries;uint32_t match_index;lib/acl/acl_bld.c +1
-1@@ -779,7 +779,7 @@ acl_merge_trie(struct acl_build_context *context,static void acl_build_reset (struct rte_acl_ctx *ctx) { -rte_free (ctx->mem); + ctx->mem_cb.free (ctx->mem_cb.udata, ctx->mem);memset (&ctx->num_categories,0 ,sizeof (*ctx) - offsetof(struct rte_acl_ctx , num_categories)); }lib/acl/acl_gen.c +2
-2@@ -478,8 +478,8 @@ rte_acl_gen(struct rte_acl_ctx *ctx, struct rte_acl_trie *trie,return -ERANGE ; } - mem =rte_zmalloc_socket (ctx->name, total_size,RTE_CACHE_LINE_SIZE , - ctx->socket_id); + mem = ctx->mem_cb.zalloc (ctx->mem_cb.udata, ctx->name, total_size, +RTE_CACHE_LINE_SIZE , ctx->socket_id);if (mem ==NULL ) { ACL_LOG(ERR, "allocation of %zu bytes on socket %d for %s failed",lib/acl/rte_acl.c +0
-0lib/acl/rte_acl.c acl_get_best_alg(void) · line 268
Quoted 5 lines
lib/acl/rte_acl.c +2
-0@@ -264,6 +264,20 @@ acl_get_best_alg(void)return alg[i]; } +static void * +zalloc_dft (void *udata,char *name,size_t size,size_t align,int32_t socket_id)how about
acl_mem_default_zalloc?lib/acl/rte_acl.c acl_get_best_alg(void) · line 275
Quoted 7 lines
lib/acl/rte_acl.c +7
-0+{ + (void )udata; +return rte_zmalloc_socket (name, size, align, socket_id); +} + +static void +free_dft (void *udata,void *ptr)how about
acl_mem_default_free?Quoted 28 lines
lib/acl/rte_acl.c +9
-1+{ + (void )udata; +rte_free (ptr); +} +RTE_EXPORT_SYMBOL (rte_acl_set_ctx_classify) extern intrte_acl_set_ctx_classify (struct rte_acl_ctx *ctx,enum rte_acl_classify_alg alg)@@ -362,7 +376,7 @@ rte_acl_free(struct rte_acl_ctx *ctx)rte_mcfg_tailq_write_unlock (); -rte_free (ctx->mem); + ctx->mem_cb.free (ctx->mem_cb.udata, ctx->mem);rte_free (ctx);rte_free (te); }@@ -425,6 +439,9 @@ rte_acl_create(const struct rte_acl_param *param) ctx->rule_sz = param->rule_size; ctx->socket_id = param->socket_id; ctx->alg =acl_get_best_alg (); + ctx->mem_cb.zalloc = zalloc_dft; + ctx->mem_cb.free = free_dft; + ctx->mem_cb.udata =NULL ;strlcpy (ctx->name, param->name,sizeof (ctx->name)); te->data = (void *) ctx;lib/acl/rte_acl.c rte_acl_list_dump(void) · line 579
Quoted 8 lines
lib/acl/rte_acl.c +5
-0@@ -555,3 +572,29 @@ rte_acl_list_dump(void) }rte_mcfg_tailq_read_unlock (); } + +/* +* Set memory allocation callbacks for a given ACL context. +*/ +RTE_EXPORT_SYMBOL (rte_acl_set_mem_cb)Should be
RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_acl_set_mem_cb, 26.03)lib/acl/rte_acl.c rte_acl_list_dump(void) · line 583
Quoted 4 lines
lib/acl/rte_acl.c +4
-0+int +rte_acl_set_mem_cb (struct rte_acl_ctx *acl,const struct rte_acl_mem_cb *mcb) +{ +if (!acl || !mcb || !mcb->zalloc || !mcb->free)suggest acl == NULL || mcb == NULL and ...
lib/acl/rte_acl.c rte_acl_list_dump(void) · line 592
Quoted 9 lines
lib/acl/rte_acl.c +9
-0+return -EINVAL ; +memcpy (&acl->mem_cb, mcb,sizeof (struct rte_acl_mem_cb )); +return 0 ; +} + +/* +* Retrieve the memory allocation callbacks assigned to the ACL context. +*/ +RTE_EXPORT_SYMBOL (rte_acl_get_mem_cb)Should be
RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_acl_get_mem_cb, 26.03)lib/acl/rte_acl.c rte_acl_list_dump(void) · line 596
Quoted 4 lines
lib/acl/rte_acl.c +4
-0+int +rte_acl_get_mem_cb (const struct rte_acl_ctx *acl,struct rte_acl_mem_cb *mcb) +{ +if (!acl || !mcb)suggest acl == NULL || mcb == NULL
Quoted 9 lines
lib/acl/rte_acl.c +4
-0+return -EINVAL ; +memcpy (mcb, &acl->mem_cb,sizeof (struct rte_acl_mem_cb )); +return 0 ; +}lib/acl/rte_acl.h +0
-0lib/acl/rte_acl.h struct rte_acl_param { · line 142
Quoted 7 lines
lib/acl/rte_acl.h +4
-0@@ -136,6 +136,48 @@ struct rte_acl_param {/** @internal opaque ACL handle */ struct rte_acl_ctx ; +/** +* Memory allocation callbacks for ACL runtime. +*/ +struct rte_acl_mem_cb {How about
rte_acl_mem_hook?
I think hook is better suited to this semantics.lib/acl/rte_acl.h struct rte_acl_param { · line 144
Quoted 2 lines
lib/acl/rte_acl.h +2
-0+/** Allocate zero-initialized memory used during runtime. */ +void *(*zalloc)(void *udata,char *name,size_t size,size_t align,int32_t socket_id);Suggest move udata as the last one parameter.
lib/acl/rte_acl.h struct rte_acl_param { · line 147
Quoted 3 lines
lib/acl/rte_acl.h +3
-0+ +/** Free memory previously allocated by zalloc(). */ +void (*free)(void *udata,void *ptr);Suggest move udata as the last one parameter.
lib/acl/rte_acl.h struct rte_acl_param { · line 159
Quoted 12 lines
lib/acl/rte_acl.h +12
-0+ +/** User-provided context passed to allocation/free callbacks. */ +void *udata; +}; + +/** +* Set memory allocation callbacks for a given ACL context. +* +* @param acl +* The ACL context. +* @param mcb +* Pointer to the memory callback structure Suggest add one note about when could invoke this API.
lib/acl/rte_acl.h struct rte_acl_param { · line 164
Quoted 5 lines
lib/acl/rte_acl.h +5
-0+* +* @return +* 0 on success. +* -EINVAL if parameters are invalid. +*/ should add
__rte_experimentalQuoted 4 lines
lib/acl/rte_acl.h +4
-0+int rte_acl_set_mem_cb (struct rte_acl_ctx *acl,const struct rte_acl_mem_cb *mcb); + +/** +* Retrieve the memory allocation callbacks assigned to the ACL context. lib/acl/rte_acl.h struct rte_acl_param { · line 178
Quoted 10 lines
lib/acl/rte_acl.h +10
-0+* +* @param acl +* The ACL context. +* @param mcb +* Output location for the current memory callback structure +* +* @return +* 0 on success. +* -EINVAL if parameters are invalid. +*/ should add
__rte_experimentallib/acl/rte_acl.h struct rte_acl_param { · line 183
Quoted 5 lines
lib/acl/rte_acl.h +2
-0+int rte_acl_get_mem_cb (const struct rte_acl_ctx *acl,struct rte_acl_mem_cb *mcb); +/** * De-allocate all memory used by ACL context. * please add descriptor section for
packet_classif_access_ctrl.rstand release note.
Re: [PATCH v3] acl: support custom memory allocator
In reply to mannywang(王永峰)Quoted 22 lines · On Tue, 25 Nov 2025 12:14:46 +0000 "mannywang(王永峰)" <[email protected]>…
Reduce memory fragmentation caused by dynamic memory allocations by allowing users to provide custom memory allocator.
Add new members to struct
rte_acl_configto allow passing custom allocator callbacks torte_acl_build:-
running_alloc: allocator callback for run-time internal memory
-running_free: free callback for run-time internal memory
-running_ctx: user-defined context passed to running_alloc/free-
temp_alloc: allocator callback for temporary memory during ACL build
-temp_reset: reset callback for temporary allocator
-temp_ctx: user-defined context passed to temp_alloc/resetThese callbacks allow users to provide their own memory pools or allocators for both persistent runtime structures and temporary build-time data.
A typical approach is to pre-allocate a static memory region for
rte_acl_ctx, and to provide a global temporary memory manager that supports multipleallocations and a single reset during ACL build.
Quoted 5 lines
Since
tb_mem_poolhandles allocation failures using siglongjmp,temp_allocfollows the same approach for failure handling.Signed-off-by YongFeng Wang <[email protected]>---
Rather than custom allocators, I did a couple of quick AI queries about alternatives. It looks like there are some big global gains possible here:
Summary of Recommendations
Improvement Benefit Complexity Priority
ACL Object Pooling Eliminates ACL-specific fragmentation Medium High
Size-Class Segregation Reduces general fragmentation High High
Slab Allocator for Build Better hugepage utilization Medium Medium
Deferred Coalescing Reduces fragmentation from churn Medium Medium
Thread-Local Caching Reduces contention, improves locality Medium Medium
Also adding some malloc_trim() would help.
https://claude.ai/share/75fcf73c-17e3-4f41-8590-f2ab640f9512
Re: [Internet]Re: [PATCH v3] acl: support custom memory allocator
In fact, my original goal was simply to bind a pre-allocated memory block to the acl_ctx so it could use that memory persistently, without reallocating and freeing it on each build. I wasn’t trying to specify any particular memory or object management strategy beyond that.
Quoted 3 lines · Stephen Hemminger
Rather than custom allocators, I did a couple of quick AI queries about alternatives. It looks like there are some big global gains possible here:
Re: [Internet]Re: [PATCH v3] acl: support custom memory allocator
In reply to Stephen HemmingerSupplement: The two types of memory in ACL make a management strategy unnecessary: (1) the run-time memory for the match tree, which is allocated as a single block during each build and freed when the ACL is reset; and (2) the temporary memory used during the build process, which is allocated multiple times within a single build and then released all at once after the build is finished.
Quoted 48 lines · Stephen Hemminger
Quoted 22 lines · On Tue, 25 Nov 2025 12:14:46 +0000 "mannywang(王永峰)" <[email protected]>…
Reduce memory fragmentation caused by dynamic memory allocations by allowing users to provide custom memory allocator.
Add new members to struct
rte_acl_configto allow passing custom allocator callbacks torte_acl_build:-
running_alloc: allocator callback for run-time internal memory
-running_free: free callback for run-time internal memory
-running_ctx: user-defined context passed to running_alloc/free-
temp_alloc: allocator callback for temporary memory during ACL build
-temp_reset: reset callback for temporary allocator
-temp_ctx: user-defined context passed to temp_alloc/resetThese callbacks allow users to provide their own memory pools or allocators for both persistent runtime structures and temporary build-time data.
A typical approach is to pre-allocate a static memory region for
rte_acl_ctx, and to provide a global temporary memory manager that supports multipleallocations and a single reset during ACL build.Quoted 5 lines
Since
tb_mem_poolhandles allocation failures using siglongjmp,temp_allocfollows the same approach for failure handling.Signed-off-by YongFeng Wang <[email protected]>---
Rather than custom allocators, I did a couple of quick AI queries about alternatives. It looks like there are some big global gains possible here:
Summary of Recommendations
Improvement Benefit Complexity Priority
ACL Object Pooling Eliminates ACL-specific fragmentation Medium High
Size-Class Segregation Reduces general fragmentation High High
Slab Allocator for Build Better hugepage utilization Medium Medium
Deferred Coalescing Reduces fragmentation from churn Medium Medium
Thread-Local Caching Reduces contention, improves locality Medium MediumAlso adding some
malloc_trim()would help.https://claude.ai/share/75fcf73c-17e3-4f41-8590-f2ab640f9512
Re: [Internet]Re: [PATCH v3] acl: support custom memory allocator
In reply to Stephen HemmingerPlease forgive me for repeating myself, because I am worried that I did not express it clearly:
ACL does not need a memory manager, because for each build it only allocates one block of memory, and before the next build (during reset) the memory is freed. The temporary memory is slightly more complicated since it is allocated multiple times, but it is still freed all at once. Therefore, for ACL, we do need to allocate memory, but we do not need a memory manager.
However, the memory used in the current build process is indeed dynamically allocated, which will cause fragmentation in the corresponding memory manager when multiple builds are performed.
Based on the above situation, I think the most reasonable approach is to bind a pre-allocated static memory block to the ACL context.
Quoted 48 lines · Stephen Hemminger
Quoted 22 lines · On Tue, 25 Nov 2025 12:14:46 +0000 "mannywang(王永峰)" <[email protected]>…
Reduce memory fragmentation caused by dynamic memory allocations by allowing users to provide custom memory allocator.
Add new members to struct
rte_acl_configto allow passing custom allocator callbacks torte_acl_build:-
running_alloc: allocator callback for run-time internal memory
-running_free: free callback for run-time internal memory
-running_ctx: user-defined context passed to running_alloc/free-
temp_alloc: allocator callback for temporary memory during ACL build
-temp_reset: reset callback for temporary allocator
-temp_ctx: user-defined context passed to temp_alloc/resetThese callbacks allow users to provide their own memory pools or allocators for both persistent runtime structures and temporary build-time data.
A typical approach is to pre-allocate a static memory region for
rte_acl_ctx, and to provide a global temporary memory manager that supports multipleallocations and a single reset during ACL build.Quoted 5 lines
Since
tb_mem_poolhandles allocation failures using siglongjmp,temp_allocfollows the same approach for failure handling.Signed-off-by YongFeng Wang <[email protected]>---
Rather than custom allocators, I did a couple of quick AI queries about alternatives. It looks like there are some big global gains possible here:
Summary of Recommendations
Improvement Benefit Complexity Priority
ACL Object Pooling Eliminates ACL-specific fragmentation Medium High
Size-Class Segregation Reduces general fragmentation High High
Slab Allocator for Build Better hugepage utilization Medium Medium
Deferred Coalescing Reduces fragmentation from churn Medium Medium
Thread-Local Caching Reduces contention, improves locality Medium MediumAlso adding some
malloc_trim()would help.https://claude.ai/share/75fcf73c-17e3-4f41-8590-f2ab640f9512