From 0fb24ef64e1a68b3984252c67fb2b7d70bc75511 Mon Sep 17 00:00:00 2001 From: Simone Mainardi Date: Thu, 20 Dec 2018 18:57:48 +0100 Subject: [PATCH] Fixes missing purgeIdle for inactive dynamic interfaces --- include/Redis.h | 6 +++--- include/ntop_defines.h | 1 + src/LuaEngine.cpp | 5 +++-- src/NetworkInterface.cpp | 15 ++++++++++++--- src/Redis.cpp | 6 +++--- 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/include/Redis.h b/include/Redis.h index 1016d1370be5..4c7512a82c3d 100644 --- a/include/Redis.h +++ b/include/Redis.h @@ -51,8 +51,8 @@ class Redis { int pushHost(const char* ns_cache, const char* ns_list, char *hostname, bool dont_check_for_existence, bool localHost); int popHost(const char* ns_list, char *hostname, u_int hostname_len); - void addToCache(char *key, char *value, u_int expire_secs); - bool isCacheable(char *key); + void addToCache(const char * const key, const char * const value, u_int expire_secs); + bool isCacheable(const char * const key); bool expireCache(char *key, u_int expire_sec); void checkDumpable(const char * const key); @@ -75,7 +75,7 @@ class Redis { int hashDel(const char * const key, const char * const field); int hashSet(const char * const key, const char * const field, const char * const value); int delHash(char *key, char *member); - int set(char *key, char *value, u_int expire_secs=0); + int set(const char * const key, const char * const value, u_int expire_secs=0); int keys(const char *pattern, char ***keys_p); int hashKeys(const char *pattern, char ***keys_p); int hashGetAll(const char *key, char ***keys_p, char ***values_p); diff --git a/include/ntop_defines.h b/include/ntop_defines.h index a3866ada00e4..80722555bd23 100644 --- a/include/ntop_defines.h +++ b/include/ntop_defines.h @@ -214,6 +214,7 @@ #define MAX_FAILED_LOGIN_ATTEMPTS 5 #define FAILED_LOGIN_ATTEMPTS_INTERVAL 300 /* seconds */ #define CONST_STR_FAILED_LOGIN_KEY "ntopng.cache.failed_logins.%s" +#define CONST_STR_RELOAD_LISTS "ntopng.cache.reload_lists_utils" /* sync with lists_utils.lua */ #define NTOP_NOLOGIN_USER "nologin" #define NTOP_DEFAULT_USER_LANG "en" #define MAX_OPTIONS 24 diff --git a/src/LuaEngine.cpp b/src/LuaEngine.cpp index 149ddfd6c21b..898d53ab59eb 100644 --- a/src/LuaEngine.cpp +++ b/src/LuaEngine.cpp @@ -1703,13 +1703,14 @@ static int ntop_reloadCustomCategories(lua_State* vm) { if(((iface = ntop->getInterfaceAtId(vm, i)) != NULL) && iface->isPacketInterface()) { iface->requestReloadCustomCategories(); + _usleep(5e4); for(j = 0; j < max_wait && iface->customCategoriesReloadRequested(); j++) { /* Make sure the interface has reloaded the categories */ - _usleep(500000); + _usleep(5e5); } if(j == max_wait) { - ntop->getTrace()->traceEvent(TRACE_ERROR, "Interface didn't reload configugration on time"); + ntop->getTrace()->traceEvent(TRACE_ERROR, "Interface didn't reload configugration on time [iface: %s]", iface->get_name()); } } } diff --git a/src/NetworkInterface.cpp b/src/NetworkInterface.cpp index cfbab5731a5c..fb054a928327 100644 --- a/src/NetworkInterface.cpp +++ b/src/NetworkInterface.cpp @@ -1043,6 +1043,7 @@ NetworkInterface* NetworkInterface::getSubInterface(u_int32_t criteria, bool par h->iface->setDynamicInterface(); HASH_ADD_INT(flowHashing, criteria, h); numVirtualInterfaces++; + ntop->getRedis()->set(CONST_STR_RELOAD_LISTS, (const char * const)"1"); } } else ntop->getTrace()->traceEvent(TRACE_WARNING, "Not enough memory"); @@ -1375,7 +1376,6 @@ bool NetworkInterface::processPacket(u_int32_t bridge_iface_idx, bool ret; vIface->setTimeLastPktRcvd(h->ts.tv_sec); - vIface->purgeIdle(h->ts.tv_sec); ret = vIface->processPacket(bridge_iface_idx, ingressPacket, when, packet_time, eth, vlan_id, @@ -1904,6 +1904,15 @@ void NetworkInterface::purgeIdle(time_t when) { ntop->getTrace()->traceEvent(TRACE_DEBUG, "Purged %u/%u idle hosts/macs on %s", m, getNumHosts()+getNumMacs(), ifname); } + + if(flowHashing) { + FlowHashing *current, *tmp; + + HASH_ITER(hh, flowHashing, current, tmp) { + if(current->iface) + current->iface->purgeIdle(when); + } + } } /* ***************************************************** */ @@ -2545,7 +2554,7 @@ void NetworkInterface::pollQueuedeBPFEvents() { void NetworkInterface::reloadCustomCategories() { if(customCategoriesReloadRequested()) { - ntop->getTrace()->traceEvent(TRACE_DEBUG, "Going to reload categories.."); + ntop->getTrace()->traceEvent(TRACE_DEBUG, "Going to reload categories [iface: %s]", get_name()); ndpi_enable_loaded_categories(ndpi_struct); reload_custom_categories = false; } @@ -4946,7 +4955,7 @@ u_int NetworkInterface::purgeIdleFlows() { pollQueuedeBPFEvents(); reloadCustomCategories(); - + if(!purge_idle_flows_hosts) return(0); if(next_idle_flow_purge == 0) { diff --git a/src/Redis.cpp b/src/Redis.cpp index 8e1a618aaab0..da2746c24128 100644 --- a/src/Redis.cpp +++ b/src/Redis.cpp @@ -165,7 +165,7 @@ int Redis::expire(char *key, u_int expire_secs) { /* **************************************** */ -bool Redis::isCacheable(char *key) { +bool Redis::isCacheable(const char * const key) { if((strstr(key, "ntopng.cache.")) || (strstr(key, "ntopng.prefs.")) || (strstr(key, "ntopng.user.") && (!strstr(key, ".password")))) @@ -212,7 +212,7 @@ void Redis::checkDumpable(const char * const key) { /* **************************************** */ /* NOTE: We assume that the addToCache() caller locks this instance */ -void Redis::addToCache(char *key, char *value, u_int expire_secs) { +void Redis::addToCache(const char * const key, const char * const value, u_int expire_secs) { StringCache_t *cached = NULL; if(!initializationCompleted) return; @@ -429,7 +429,7 @@ int Redis::hashDel(const char * const key, const char * const field) { /* **************************************** */ -int Redis::set(char *key, char *value, u_int expire_secs) { +int Redis::set(const char * const key, const char * const value, u_int expire_secs) { int rc; redisReply *reply;