From 7a08d92004a9ec68297cd2d3eb9a66e2b58ae22b Mon Sep 17 00:00:00 2001 From: Alexander Maltsev Date: Sat, 19 Jul 2025 21:50:45 +0500 Subject: [PATCH] session: set maximum memory for fifos Add APP_OPTIONS_MAX_FIFO_MEMORY to set upper limit on how much memory could be used by fifos. It is rounded upwards to nearest segment size. If not specified, no limit is set. Type: feature Change-Id: I7fb87808973e761b7c40b734b18d55b8a8d8296e Signed-off-by: Alexander Maltsev --- src/vnet/session/application.c | 15 ++++++++++++--- src/vnet/session/application_interface.h | 2 +- src/vnet/session/segment_manager.c | 7 +++++++ src/vnet/session/segment_manager.h | 1 + 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/vnet/session/application.c b/src/vnet/session/application.c index 1ba4604a971..2d1b58dbe63 100644 --- a/src/vnet/session/application.c +++ b/src/vnet/session/application.c @@ -855,6 +855,13 @@ application_alloc_and_init (app_init_args_t *a) props->low_watermark = opts[APP_OPTIONS_LOW_WATERMARK]; if (opts[APP_OPTIONS_PCT_FIRST_ALLOC]) props->pct_first_alloc = opts[APP_OPTIONS_PCT_FIRST_ALLOC]; + if (opts[APP_OPTIONS_MAX_FIFO_MEMORY]) + { + /* Round upwards to nearest segment_size */ + props->max_segments = + (opts[APP_OPTIONS_MAX_FIFO_MEMORY] + props->segment_size - 1) / + props->segment_size; + } props->segment_type = seg_type; if (opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_COLLECTOR) @@ -1825,9 +1832,11 @@ format_application (u8 * s, va_list * args) s = format (s, "app-name %v app-index %u ns-index %u seg-size %U\n", app_name, app->app_index, app->ns_index, format_memory_size, props->add_segment_size); - s = format (s, "rx-fifo-size %U tx-fifo-size %U workers:\n", - format_memory_size, props->rx_fifo_size, - format_memory_size, props->tx_fifo_size); + s = + format (s, "rx-fifo-size %U tx-fifo-size %U max-fifo-memory %U workers:\n", + format_memory_size, props->rx_fifo_size, format_memory_size, + props->tx_fifo_size, format_memory_size, + props->max_segments * props->segment_size); pool_foreach (wrk_map, app->worker_maps) { app_wrk = app_worker_get (wrk_map->wrk_index); diff --git a/src/vnet/session/application_interface.h b/src/vnet/session/application_interface.h index 342d9cfb684..b1569f076f1 100644 --- a/src/vnet/session/application_interface.h +++ b/src/vnet/session/application_interface.h @@ -181,7 +181,7 @@ typedef enum APP_OPTIONS_EVT_QUEUE_SIZE, APP_OPTIONS_SEGMENT_SIZE, APP_OPTIONS_ADD_SEGMENT_SIZE, - APP_OPTIONS_UNUSED1, + APP_OPTIONS_MAX_FIFO_MEMORY, APP_OPTIONS_RX_FIFO_SIZE, APP_OPTIONS_TX_FIFO_SIZE, APP_OPTIONS_PREALLOC_FIFO_PAIRS, diff --git a/src/vnet/session/segment_manager.c b/src/vnet/session/segment_manager.c index 341b70086d1..f7f88cb1401 100644 --- a/src/vnet/session/segment_manager.c +++ b/src/vnet/session/segment_manager.c @@ -115,6 +115,13 @@ segment_manager_add_segment_inline (segment_manager_t *sm, uword segment_size, if (need_lock) clib_rwlock_writer_lock (&sm->segments_rwlock); + if (props->max_segments && pool_elts (sm->segments) >= props->max_segments) + { + SESSION_DBG ( + "max number of segments allocated, can't allocate new segment"); + goto done; + } + pool_get_zero (sm->segments, fs); /* diff --git a/src/vnet/session/segment_manager.h b/src/vnet/session/segment_manager.h index 0fb957a0912..7f73b7964b0 100644 --- a/src/vnet/session/segment_manager.h +++ b/src/vnet/session/segment_manager.h @@ -41,6 +41,7 @@ typedef struct _segment_manager_props u8 low_watermark; /**< memory usage low watermark % */ u8 pct_first_alloc; /**< pct of fifo size to alloc */ u8 huge_page; /**< use hugepage */ + u32 max_segments; /**< max number of segments, 0 for unlimited */ } segment_manager_props_t; #define foreach_seg_manager_flag \ -- 2.16.6