Sizes of LRU caches are now configurable

0 as size value disable the cache.

The diffs in unit tests are due to the fact that some lookups are
performed before the first insert: before this change these lookups
weren't counted because the cache was not yet initialized, now they are.
This commit is contained in:
Nardi Ivan
2022-09-23 18:33:48 +02:00
committed by Toni
parent 83de3e4716
commit 1f345b311f
133 changed files with 345 additions and 232 deletions
+120 -6
View File
@@ -2779,6 +2779,14 @@ struct ndpi_detection_module_struct *ndpi_init_detection_module(ndpi_init_prefs
return(NULL);
}
ndpi_str->ookla_cache_num_entries = 1024;
ndpi_str->bittorrent_cache_num_entries = 32768;
ndpi_str->zoom_cache_num_entries = 512;
ndpi_str->stun_cache_num_entries = 1024;
ndpi_str->tls_cert_cache_num_entries = 1024;
ndpi_str->mining_cache_num_entries = 1024;
ndpi_str->msteams_cache_num_entries = 1024;
ndpi_str->opportunistic_tls_smtp_enabled = 1;
ndpi_str->opportunistic_tls_imap_enabled = 1;
ndpi_str->opportunistic_tls_pop_enabled = 1;
@@ -2851,6 +2859,56 @@ void ndpi_finalize_initialization(struct ndpi_detection_module_struct *ndpi_str)
ndpi_add_domain_risk_exceptions(ndpi_str);
if(ndpi_str->ookla_cache_num_entries > 0) {
ndpi_str->ookla_cache = ndpi_lru_cache_init(ndpi_str->ookla_cache_num_entries);
if(!ndpi_str->ookla_cache) {
NDPI_LOG_ERR(ndpi_str, "Error allocating lru cache (num_entries %u)\n",
ndpi_str->ookla_cache_num_entries);
}
}
if(ndpi_str->bittorrent_cache_num_entries > 0) {
ndpi_str->bittorrent_cache = ndpi_lru_cache_init(ndpi_str->bittorrent_cache_num_entries);
if(!ndpi_str->bittorrent_cache) {
NDPI_LOG_ERR(ndpi_str, "Error allocating lru cache (num_entries %u)\n",
ndpi_str->bittorrent_cache_num_entries);
}
}
if(ndpi_str->zoom_cache_num_entries > 0) {
ndpi_str->zoom_cache = ndpi_lru_cache_init(ndpi_str->zoom_cache_num_entries);
if(!ndpi_str->zoom_cache) {
NDPI_LOG_ERR(ndpi_str, "Error allocating lru cache (num_entries %u)\n",
ndpi_str->zoom_cache_num_entries);
}
}
if(ndpi_str->stun_cache_num_entries > 0) {
ndpi_str->stun_cache = ndpi_lru_cache_init(ndpi_str->stun_cache_num_entries);
if(!ndpi_str->stun_cache) {
NDPI_LOG_ERR(ndpi_str, "Error allocating lru cache (num_entries %u)\n",
ndpi_str->stun_cache_num_entries);
}
}
if(ndpi_str->tls_cert_cache_num_entries > 0) {
ndpi_str->tls_cert_cache = ndpi_lru_cache_init(ndpi_str->tls_cert_cache_num_entries);
if(!ndpi_str->tls_cert_cache) {
NDPI_LOG_ERR(ndpi_str, "Error allocating lru cache (num_entries %u)\n",
ndpi_str->tls_cert_cache_num_entries);
}
}
if(ndpi_str->mining_cache_num_entries > 0) {
ndpi_str->mining_cache = ndpi_lru_cache_init(ndpi_str->mining_cache_num_entries);
if(!ndpi_str->mining_cache) {
NDPI_LOG_ERR(ndpi_str, "Error allocating lru cache (num_entries %u)\n",
ndpi_str->mining_cache_num_entries);
}
}
if(ndpi_str->msteams_cache_num_entries > 0) {
ndpi_str->msteams_cache = ndpi_lru_cache_init(ndpi_str->msteams_cache_num_entries);
if(!ndpi_str->msteams_cache) {
NDPI_LOG_ERR(ndpi_str, "Error allocating lru cache (num_entries %u)\n",
ndpi_str->msteams_cache_num_entries);
}
}
if(ndpi_str->ac_automa_finalized) return;
ndpi_automa * const automa[] = { &ndpi_str->host_automa,
@@ -5625,9 +5683,6 @@ static void ndpi_reconcile_protocols(struct ndpi_detection_module_struct *ndpi_s
if(flow->is_ipv6 == 0 && flow->l4_proto == IPPROTO_TCP) {
// printf("====>> NDPI_PROTOCOL_MSTEAMS\n");
if(ndpi_str->msteams_cache == NULL)
ndpi_str->msteams_cache = ndpi_lru_cache_init(1024);
if(ndpi_str->msteams_cache)
ndpi_lru_add_to_cache(ndpi_str->msteams_cache,
ntohl(flow->c_address.v4),
@@ -5769,9 +5824,6 @@ static u_int8_t ndpi_search_into_zoom_cache(struct ndpi_detection_module_struct
static void ndpi_add_connection_as_zoom(struct ndpi_detection_module_struct *ndpi_struct,
u_int32_t daddr /* Network byte order */) {
if(ndpi_struct->zoom_cache == NULL)
ndpi_struct->zoom_cache = ndpi_lru_cache_init(512);
if(ndpi_struct->zoom_cache)
ndpi_lru_add_to_cache(ndpi_struct->zoom_cache, daddr, NDPI_PROTOCOL_ZOOM);
}
@@ -8291,6 +8343,68 @@ int ndpi_get_lru_cache_stats(struct ndpi_detection_module_struct *ndpi_struct,
}
}
int ndpi_set_lru_cache_size(struct ndpi_detection_module_struct *ndpi_struct,
lru_cache_type cache_type,
u_int32_t num_entries)
{
switch(cache_type) {
case NDPI_LRUCACHE_OOKLA:
ndpi_struct->ookla_cache_num_entries = num_entries;
return 0;
case NDPI_LRUCACHE_BITTORRENT:
ndpi_struct->bittorrent_cache_num_entries = num_entries;
return 0;
case NDPI_LRUCACHE_ZOOM:
ndpi_struct->zoom_cache_num_entries = num_entries;
return 0;
case NDPI_LRUCACHE_STUN:
ndpi_struct->stun_cache_num_entries = num_entries;
return 0;
case NDPI_LRUCACHE_TLS_CERT:
ndpi_struct->tls_cert_cache_num_entries = num_entries;
return 0;
case NDPI_LRUCACHE_MINING:
ndpi_struct->mining_cache_num_entries = num_entries;
return 0;
case NDPI_LRUCACHE_MSTEAMS:
ndpi_struct->msteams_cache_num_entries = num_entries;
return 0;
default:
return -1;
}
}
int ndpi_get_lru_cache_size(struct ndpi_detection_module_struct *ndpi_struct,
lru_cache_type cache_type,
u_int32_t *num_entries)
{
switch(cache_type) {
case NDPI_LRUCACHE_OOKLA:
*num_entries = ndpi_struct->ookla_cache_num_entries;
return 0;
case NDPI_LRUCACHE_BITTORRENT:
*num_entries = ndpi_struct->bittorrent_cache_num_entries;
return 0;
case NDPI_LRUCACHE_ZOOM:
*num_entries = ndpi_struct->zoom_cache_num_entries;
return 0;
case NDPI_LRUCACHE_STUN:
*num_entries = ndpi_struct->stun_cache_num_entries;
return 0;
case NDPI_LRUCACHE_TLS_CERT:
*num_entries = ndpi_struct->tls_cert_cache_num_entries;
return 0;
case NDPI_LRUCACHE_MINING:
*num_entries = ndpi_struct->mining_cache_num_entries;
return 0;
case NDPI_LRUCACHE_MSTEAMS:
*num_entries = ndpi_struct->msteams_cache_num_entries;
return 0;
default:
return -1;
}
}
/* ******************************************************************** */
/*