Skip to content

Commit

Permalink
Added MAC Address cache duration preference (#8684)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoBiscosi committed Sep 3, 2024
1 parent 4d80bb1 commit a9ae442
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 4 deletions.
4 changes: 1 addition & 3 deletions include/Mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,7 @@ class Mac : public GenericHashEntry {
stats->incSentStats(t, num_pkts, num_bytes), last_seen = t;
}

bool is_hash_entry_state_idle_transition_ready() {
return ((getUses() == 0) && is_active_entry_now_idle(MAX_MAC_IDLE));
}
bool is_hash_entry_state_idle_transition_ready();

inline void incnDPIStats(time_t when, ndpi_protocol_category_t ndpi_category,
u_int64_t sent_packets, u_int64_t sent_bytes,
Expand Down
3 changes: 2 additions & 1 deletion include/Prefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Prefs {
tls_quic_hostnaming;
u_int32_t behaviour_analysis_learning_period;
u_int32_t iec60870_learning_period, modbus_learning_period,
devices_learning_period;
devices_learning_period, mac_address_cache_duration;
u_int32_t host_port_learning_period;
#ifdef NTOPNG_PRO
ndpi_bitmap* modbus_allowed_function_codes;
Expand Down Expand Up @@ -697,6 +697,7 @@ class Prefs {
inline u_int getModbusTooManyExceptionsThreshold() { return(modbus_too_many_exceptions); }
#endif
inline u_int32_t devicesLearingPeriod() { return (devices_learning_period); };
inline u_int32_t macAddressCacheDuration() { return (mac_address_cache_duration); };
inline u_int32_t hostPortLearningPeriod() { return (host_port_learning_period); };
inline bool are_alerts_disabled() { return (disable_alerts); };
inline bool dontEmitFlowAlerts() {
Expand Down
2 changes: 2 additions & 0 deletions include/ntop_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,8 @@
NTOPNG_PREFS_PREFIX ".is_active_local_host_cache_enabled"
#define CONST_RUNTIME_ACTIVE_LOCAL_HOSTS_CACHE_INTERVAL \
NTOPNG_PREFS_PREFIX ".active_local_host_cache_interval"
#define CONST_RUNTIME_MAC_ADDRESS_CACHE_DURATION \
NTOPNG_PREFS_PREFIX ".mac_address_cache_duration"
#define CONST_RUNTIME_PREFS_LOG_TO_FILE NTOPNG_PREFS_PREFIX ".log_to_file"
#define CONST_RUNTIME_PREFS_HOUSEKEEPING_FREQ \
NTOPNG_PREFS_PREFIX ".housekeeping_freq"
Expand Down
2 changes: 2 additions & 0 deletions scripts/locales/en.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6800,6 +6800,8 @@ local lang = {
["local_auth"] = "Local Authentication",
["local_host_cache_duration_description"] = "Time after which a cached local host is deleted from the cache. Default: 1 hour.",
["local_host_cache_duration_title"] = "Local Hosts Cache Duration",
["mac_address_cache_duration_description"] = "Time after which an inactive cached mac address (not doing traffic) is deleted from the cache. Default: 5 minutes.",
["mac_address_cache_duration_title"] = "Mac Address Cache Duration",
["local_host_mask"] = "Mask Local Hosts",
["local_host_max_idle_description"] = "Inactivity time after which a local host is considered idle (sec). Idle local hosts are dumped to a cache so their counters can be restored in case they become active again. Counters include, but are not limited to, packets and bytes total and per Layer-7 application. Default: 5 min.",
["local_host_max_idle_title"] = "Local Host Idle Timeout",
Expand Down
10 changes: 10 additions & 0 deletions scripts/lua/admin/prefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ local influxdb = require("influxdb")
local script_manager = require("script_manager")
local info = ntop.getInfo()
local auth = require "auth"
local prefs = ntop.getPrefs()

local email_peer_pattern = [[^([a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*)$]]

Expand Down Expand Up @@ -1348,6 +1349,15 @@ if auth.has_capability(auth.capabilities.preferences) then
min = 60,
tformat = "mhd"
})


prefsInputFieldPrefs(subpage_active.entries["mac_address_cache_duration"].title,
subpage_active.entries["mac_address_cache_duration"].description, "ntopng.prefs.",
"mac_address_cache_duration", prefs.mac_address_cache_duration or 300, "number", true, nil,
nil, {
min = 5,
tformat = "mhd"
})

print('</table>')

Expand Down
1 change: 1 addition & 0 deletions scripts/lua/modules/http_lint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2357,6 +2357,7 @@ local known_parameters = {
["non_local_host_max_idle"] = validateNumber,
["flow_max_idle"] = validateNumber,
["active_local_host_cache_interval"] = validateNumber,
["mac_address_cache_duration"] = validateNumber,
["auth_session_duration"] = validateNumber,
["local_host_cache_duration"] = validateNumber,
["intf_rrd_raw_days"] = validateNumber,
Expand Down
4 changes: 4 additions & 0 deletions scripts/lua/modules/prefs_menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ local menu_subpages = {{
local_host_cache_duration = {
title = i18n("prefs.local_host_cache_duration_title"),
description = i18n("prefs.local_host_cache_duration_description")
},
mac_address_cache_duration = {
title = i18n("prefs.mac_address_cache_duration_title"),
description = i18n("prefs.mac_address_cache_duration_description")
}
}
}, {
Expand Down
7 changes: 7 additions & 0 deletions src/Mac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,10 @@ void Mac::dumpAssetInfo() {
ndpi_term_serializer(&device_json);
}
#endif

/* *************************************** */

bool Mac::is_hash_entry_state_idle_transition_ready() {
//ntop->getTrace()->traceEvent(TRACE_NORMAL, "Configured Expiration Time: %d", ntop->getPrefs()->macAddressCacheDuration());
return ((getUses() == 0) && is_active_entry_now_idle(ntop->getPrefs()->macAddressCacheDuration()));
}
5 changes: 5 additions & 0 deletions src/Prefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Prefs::Prefs(Ntop *_ntop) {
max_extracted_pcap_bytes = CONST_DEFAULT_MAX_EXTR_PCAP_BYTES;
behaviour_analysis_learning_period =
CONST_DEFAULT_BEHAVIOUR_ANALYSIS_LEARNING_PERIOD;
mac_address_cache_duration = MAX_MAC_IDLE;
behaviour_analysis_learning_status_during_learning = service_allowed;
behaviour_analysis_learning_status_post_learning = service_allowed;
iec60870_learning_period = CONST_IEC104_LEARNING_TIME;
Expand Down Expand Up @@ -915,6 +916,8 @@ void Prefs::reloadPrefsFromRedis() {
pkt_ifaces_flow_max_idle = getDefaultPrefsValue(CONST_FLOW_MAX_IDLE_PREFS, MAX_FLOW_IDLE),
active_local_hosts_cache_interval = getDefaultPrefsValue(CONST_RUNTIME_ACTIVE_LOCAL_HOSTS_CACHE_INTERVAL,
CONST_DEFAULT_ACTIVE_LOCAL_HOSTS_CACHE_INTERVAL),
mac_address_cache_duration = getDefaultPrefsValue(CONST_RUNTIME_MAC_ADDRESS_CACHE_DURATION,
MAX_MAC_IDLE),

log_to_file = getDefaultBoolPrefsValue(CONST_RUNTIME_PREFS_LOG_TO_FILE, false);
intf_rrd_raw_days = getDefaultPrefsValue(CONST_INTF_RRD_RAW_DAYS, INTF_RRD_RAW_DAYS),
Expand Down Expand Up @@ -2775,6 +2778,8 @@ void Prefs::lua(lua_State *vm) {
iec60870_learning_period);
lua_push_uint64_table_entry(vm, "modbus_learning_period",
modbus_learning_period);
lua_push_uint64_table_entry(vm, "mac_address_cache_duration",
mac_address_cache_duration);
lua_push_uint64_table_entry(vm, "devices_learning_period",
devices_learning_period);
lua_push_uint64_table_entry(vm, "host_port_learning_period",
Expand Down

0 comments on commit a9ae442

Please sign in to comment.