diff --git a/lib/ff_api.h b/lib/ff_api.h index 175dc47ca..57f882c1b 100644 --- a/lib/ff_api.h +++ b/lib/ff_api.h @@ -64,6 +64,16 @@ void ff_run(loop_func_t loop, void *arg); void ff_stop_run(void); +/* + * Control whether ff_run() automatically tears down config, DPDK EAL, and log + * before returning (default: 1). Set to 0 when the application needs to close + * BSD sockets or epoll fds after ff_run() returns, then call ff_cleanup(). + */ +void ff_set_auto_cleanup(int enable); + +/* Idempotent teardown of config, DPDK EAL, and F-Stack log. */ +int ff_cleanup(void); + /* * Resident slim primary helpers (graceful_reload=1). * ff_primary_conf_digest: digest of the config items the primary applies diff --git a/lib/ff_dpdk_if.c b/lib/ff_dpdk_if.c index c6c43b6e3..9cc780005 100644 --- a/lib/ff_dpdk_if.c +++ b/lib/ff_dpdk_if.c @@ -4153,6 +4153,8 @@ ff_dpdk_if_up(void) { return 0; } +static int ff_auto_cleanup = 1; + void ff_dpdk_run(loop_func_t loop, void *arg) { struct loop_routine *lr = rte_malloc(NULL, @@ -4165,6 +4167,27 @@ ff_dpdk_run(loop_func_t loop, void *arg) { stop_clock(); rte_free(lr); + if (ff_auto_cleanup) { + ff_dpdk_cleanup(); + } +} + +void +ff_dpdk_set_auto_cleanup(int enable) +{ + ff_auto_cleanup = enable ? 1 : 0; +} + +int +ff_dpdk_cleanup(void) +{ + static int cleaned = 0; + if (cleaned) { + return 0; + } + cleaned = 1; + + int ret = 0; /* FU-S2-2-CFG-UNLOAD: free ff_global_cfg before EAL cleanup so any * cfg.* allocations made via DPDK helpers (none today, but defensive) * are released while the EAL is still up. Mirrors the comment block @@ -4172,9 +4195,10 @@ ff_dpdk_run(loop_func_t loop, void *arg) { if (!(ff_global_cfg.dpdk.primary_slim && rte_eal_process_type() == RTE_PROC_PRIMARY)) { ff_unload_config(); - rte_eal_cleanup(); + ret = rte_eal_cleanup(); } ff_log_close(); + return ret; } void diff --git a/lib/ff_dpdk_if.h b/lib/ff_dpdk_if.h index 1d5c36da0..f8804bfd8 100644 --- a/lib/ff_dpdk_if.h +++ b/lib/ff_dpdk_if.h @@ -58,6 +58,8 @@ int ff_dpdk_init(int argc, char **argv); int ff_dpdk_if_up(void); void ff_dpdk_run(loop_func_t loop, void *arg); void ff_dpdk_stop(void); +void ff_dpdk_set_auto_cleanup(int enable); +int ff_dpdk_cleanup(void); struct ff_dpdk_if_context; struct ff_port_cfg; diff --git a/lib/ff_init.c b/lib/ff_init.c index d32c6beec..3fcb33a71 100644 --- a/lib/ff_init.c +++ b/lib/ff_init.c @@ -69,6 +69,18 @@ ff_stop_run(void) ff_dpdk_stop(); } +void +ff_set_auto_cleanup(int enable) +{ + ff_dpdk_set_auto_cleanup(enable); +} + +int +ff_cleanup(void) +{ + return ff_dpdk_cleanup(); +} + const char * ff_eal_runtime_dir(void) {