From 5c13964dad112206e98c8a574bd45b56fc55b77e Mon Sep 17 00:00:00 2001 From: Nardi Ivan Date: Tue, 9 Jan 2024 11:12:27 +0100 Subject: [PATCH] config: move LRU cache configurations to the new API --- doc/configuration_parameters.md | 2 + example/config.txt | 2 + example/ndpiReader.c | 95 ------------ fuzz/fuzz_config.cpp | 92 +++++++++-- src/include/ndpi_api.h | 14 -- src/lib/ndpi_main.c | 257 +++++++------------------------ src/lib/ndpi_private.h | 33 ++-- tests/cfgs/caches_cfg/config.txt | 2 +- 8 files changed, 164 insertions(+), 333 deletions(-) diff --git a/doc/configuration_parameters.md b/doc/configuration_parameters.md index 77ce00236fd3..00a87e11788e 100644 --- a/doc/configuration_parameters.md +++ b/doc/configuration_parameters.md @@ -8,3 +8,5 @@ TODO | NULL | "packets_limit_per_flow" | 32 | 0 | 255 | The upper limit on the number of packets per flow that will be subject to DPI, after which classification will be considered complete (0 = no limit) | | NULL | "filename.config" | NULL | NULL | NULL | Name of the file containing a list of configuration knobs itself (one per line)!. Useful to configure nDPI via text file instead of via API | | "tls" | "metadata.sha1_fingerprint.enable" | 1 | NULL | NULL | Enable/disable computation and export of SHA1 fingerprint for TLS flows. Note that if it is disable, the flow risk `NDPI_MALICIOUS_SHA1_CERTIFICATE` is not checked | +| NULL | "lru.$CACHE_NAME.size" | See description | 0 | 16777215 | Set the size (in number of elements) of the specified LRU cache (0 = the cache is disabled). The keyword "$CACHE_NAME" is a placeholder for the cache name and the possible values are: ookla, bittorrent, zoom, stun, tls_cert, mining, msteams, stun_zoom. The default value is "32768" for the bittorrent cache, "512" for the zoom cache and "1024" for all the other caches | +| NULL | "lru.$CACHE_NAME.ttl" | See description | 0 | 16777215 | Set the TTL (in seconds) for the elements of the specified LRU cache (0 = the elements never explicitly expire). The keyword "$CACHE_NAME" is a placeholder for the cache name and the possible values are: ookla, bittorrent, zoom, stun, tls_cert, mining, msteams, stun_zoom. The default value is "120" for the ookla cache, "60" for the msteams and stun_zoom caches and "0" for all the other caches | diff --git a/example/config.txt b/example/config.txt index 966a917ce74b..1b59d22dd34a 100644 --- a/example/config.txt +++ b/example/config.txt @@ -7,4 +7,6 @@ ,packets_limit_per_flow,32 tls,metadata.sha1_fingerprint.enable,1 +,lru.bittorrent.ttl,0 + diff --git a/example/ndpiReader.c b/example/ndpiReader.c index 06097d5806b8..8e39f169497f 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -143,9 +143,6 @@ int enable_malloc_bins = 0; int max_malloc_bins = 14; int malloc_size_stats = 0; -static int lru_cache_sizes[NDPI_LRUCACHE_MAX]; -static int lru_cache_ttls[NDPI_LRUCACHE_MAX]; - struct flow_info { struct ndpi_flow_info *flow; u_int16_t thread_id; @@ -607,16 +604,12 @@ static void help(u_int long_help) { " -A | Dump internal statistics (LRU caches / Patricia trees / Ahocarasick automas / ...\n" " -M | Memory allocation stats on data-path (only by the library). It works only on single-thread configuration\n" " -Z proto:value | Set this value of aggressiveness for this protocol (0 to disable it). This flag can be used multiple times\n" - " --lru-cache-size=NAME:size | Specify the size for this LRU cache (0 to disable it). This flag can be used multiple times\n" - " --lru-cache-ttl=NAME:size | Specify the TTL [in seconds] for this LRU cache (0 to disable it). This flag can be used multiple times\n" " --cfg=proto,param,value | Configure the specific attribute of this protocol\n" , human_readeable_string_len, min_pattern_len, max_pattern_len, max_num_packets_per_flow, max_packet_payload_dissection, max_num_reported_top_payloads, max_num_tcp_dissected_pkts, max_num_udp_dissected_pkts); - printf("\nLRU Cache names: ookla, bittorrent, zoom, stun, tls_cert, mining, msteams, stun_zoom\n"); - NDPI_PROTOCOL_BITMASK all; struct ndpi_detection_module_struct *ndpi_info_mod = ndpi_init_detection_module(init_prefs); NDPI_BITMASK_SET_ALL(all); @@ -663,9 +656,6 @@ static void help(u_int long_help) { } -#define OPTLONG_VALUE_LRU_CACHE_SIZE 1000 -#define OPTLONG_VALUE_LRU_CACHE_TTL 1001 - #define OPTLONG_VALUE_CFG 3000 static struct option longopts[] = { @@ -709,9 +699,6 @@ static struct option longopts[] = { { "result-path", required_argument, NULL, 'w'}, { "quiet", no_argument, NULL, 'q'}, - { "lru-cache-size", required_argument, NULL, OPTLONG_VALUE_LRU_CACHE_SIZE}, - { "lru-cache-ttl", required_argument, NULL, OPTLONG_VALUE_LRU_CACHE_TTL}, - { "cfg", required_argument, NULL, OPTLONG_VALUE_CFG}, {0, 0, 0, 0} @@ -903,52 +890,6 @@ void printCSVHeader() { fprintf(csv_fp, "\n"); } -static int cache_idx_from_name(const char *name) -{ - if(strcmp(name, "ookla") == 0) - return NDPI_LRUCACHE_OOKLA; - if(strcmp(name, "bittorrent") == 0) - return NDPI_LRUCACHE_BITTORRENT; - if(strcmp(name, "zoom") == 0) - return NDPI_LRUCACHE_ZOOM; - if(strcmp(name, "stun") == 0) - return NDPI_LRUCACHE_STUN; - if(strcmp(name, "tls_cert") == 0) - return NDPI_LRUCACHE_TLS_CERT; - if(strcmp(name, "mining") == 0) - return NDPI_LRUCACHE_MINING; - if(strcmp(name, "msteams") == 0) - return NDPI_LRUCACHE_MSTEAMS; - if(strcmp(name, "stun_zoom") == 0) - return NDPI_LRUCACHE_STUN_ZOOM; - return -1; -} - -static int parse_cache_param(char *param, int *cache_idx, int *param_value) -{ - char *saveptr, *tmp_str, *cache_str, *param_str; - int idx; - - tmp_str = ndpi_strdup(param); - if(tmp_str) { - cache_str = strtok_r(tmp_str, ":", &saveptr); - if(cache_str) { - param_str = strtok_r(NULL, ":", &saveptr); - if(param_str) { - idx = cache_idx_from_name(cache_str); - if(idx >= 0) { - *cache_idx = idx; - *param_value = atoi(param_str); - ndpi_free(tmp_str); - return 0; - } - } - } - } - ndpi_free(tmp_str); - return -1; -} - static int parse_two_unsigned_integer(char *param, u_int32_t *num1, u_int32_t *num2) { char *saveptr, *tmp_str, *num1_str, *num2_str; @@ -1035,7 +976,6 @@ static void parseOptions(int argc, char **argv) { u_int num_cores = sysconf(_SC_NPROCESSORS_ONLN); #endif #endif - int cache_idx, cache_size, cache_ttl; char *s1, *s2, *s3; #ifdef USE_DPDK @@ -1052,11 +992,6 @@ static void parseOptions(int argc, char **argv) { for(i = 0; i < NDPI_MAX_SUPPORTED_PROTOCOLS; i++) aggressiveness[i] = -1; /* Use the default value */ - for(i = 0; i < NDPI_LRUCACHE_MAX; i++) { - lru_cache_sizes[i] = -1; /* Use the default value */ - lru_cache_ttls[i] = -1; /* Use the default value */ - } - while((opt = getopt_long(argc, argv, "a:Ab:B:e:Ec:C:dDFf:g:G:i:Ij:k:K:S:hHp:pP:l:r:Rs:tu:v:V:n:rp:x:X:w:Z:q0123:456:7:89:m:MT:U:", longopts, &option_idx)) != EOF) { @@ -1364,22 +1299,6 @@ static void parseOptions(int argc, char **argv) { if(max_num_udp_dissected_pkts < 3) max_num_udp_dissected_pkts = 3; break; - case OPTLONG_VALUE_LRU_CACHE_SIZE: - if(parse_cache_param(optarg, &cache_idx, &cache_size) == -1) { - printf("Invalid parameter [%s]\n", optarg); - exit(1); - } - lru_cache_sizes[cache_idx] = cache_size; - break; - - case OPTLONG_VALUE_LRU_CACHE_TTL: - if(parse_cache_param(optarg, &cache_idx, &cache_ttl) == -1) { - printf("Invalid parameter [%s]\n", optarg); - exit(1); - } - lru_cache_ttls[cache_idx] = cache_ttl; - break; - case OPTLONG_VALUE_CFG: if(parse_three_strings(optarg, &s1, &s2, &s3) == -1 || __add_cfg(s1, s2, s3, 0) == -1) { @@ -2889,20 +2808,6 @@ static void setupDetection(u_int16_t thread_id, pcap_t * pcap_handle) { if(_protoFilePath != NULL) ndpi_load_protocols_file(ndpi_thread_info[thread_id].workflow->ndpi_struct, _protoFilePath); - /* Enable/disable/configure LRU caches size here */ - for(i = 0; i < NDPI_LRUCACHE_MAX; i++) { - if(lru_cache_sizes[i] != -1) - ndpi_set_lru_cache_size(ndpi_thread_info[thread_id].workflow->ndpi_struct, - i, lru_cache_sizes[i]); - } - - /* Enable/disable LRU caches TTL here */ - for(i = 0; i < NDPI_LRUCACHE_MAX; i++) { - if(lru_cache_ttls[i] != -1) - ndpi_set_lru_cache_ttl(ndpi_thread_info[thread_id].workflow->ndpi_struct, - i, lru_cache_ttls[i]); - } - /* Set aggressiveness here */ for(i = 0; i < NDPI_MAX_SUPPORTED_PROTOCOLS; i++) { if(aggressiveness[i] != -1) diff --git a/fuzz/fuzz_config.cpp b/fuzz/fuzz_config.cpp index 573f6ecbe87e..e84c9036edaa 100644 --- a/fuzz/fuzz_config.cpp +++ b/fuzz/fuzz_config.cpp @@ -13,7 +13,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { struct ndpi_detection_module_struct *ndpi_info_mod; struct ndpi_flow_struct flow; u_int8_t protocol_was_guessed; - u_int32_t i, num; + u_int32_t i; u_int16_t random_proto, bool_value; int random_value; NDPI_PROTOCOL_BITMASK enabled_bitmask; @@ -73,16 +73,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { if(fuzzed_data.ConsumeBool()) ndpi_load_ipv4_ptree(ndpi_info_mod, "ipv4_addresses.txt", NDPI_PROTOCOL_TLS); - for(i = 0; i < NDPI_LRUCACHE_MAX + 1; i++) { /* + 1 to test invalid type */ - ndpi_set_lru_cache_size(ndpi_info_mod, static_cast(i), - fuzzed_data.ConsumeIntegralInRange(0, (1 << 16) - 1)); - ndpi_get_lru_cache_size(ndpi_info_mod, static_cast(i), &num); - - ndpi_set_lru_cache_ttl(ndpi_info_mod, static_cast(i), - fuzzed_data.ConsumeIntegralInRange(0, (1 << 24) - 1)); - ndpi_get_lru_cache_ttl(ndpi_info_mod, static_cast(i), &num); - } - /* TODO: stub for geo stuff */ ndpi_load_geoip(ndpi_info_mod, NULL, NULL); @@ -124,6 +114,86 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { sprintf(cfg_value, "%d", value); ndpi_set_config(ndpi_info_mod, NULL, "packets_limit_per_flow", cfg_value); } + if(fuzzed_data.ConsumeBool()) { + value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 / 2); /* max / 2 instead of max + 1 to avoid oom on oss-fuzzer */ + sprintf(cfg_value, "%d", value); + ndpi_set_config(ndpi_info_mod, NULL, "lru.ookla.size", cfg_value); + } + if(fuzzed_data.ConsumeBool()) { + value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 + 1); + sprintf(cfg_value, "%d", value); + ndpi_set_config(ndpi_info_mod, NULL, "lru.ookla.ttl", cfg_value); + } + if(fuzzed_data.ConsumeBool()) { + value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 / 2); /* max / 2 instead of max + 1 to avoid oom on oss-fuzzer */ + sprintf(cfg_value, "%d", value); + ndpi_set_config(ndpi_info_mod, NULL, "lru.bittorrent.size", cfg_value); + } + if(fuzzed_data.ConsumeBool()) { + value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 + 1); + sprintf(cfg_value, "%d", value); + ndpi_set_config(ndpi_info_mod, NULL, "lru.bittorrent.ttl", cfg_value); + } + if(fuzzed_data.ConsumeBool()) { + value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 / 2); /* max / 2 instead of max + 1 to avoid oom on oss-fuzzer */ + sprintf(cfg_value, "%d", value); + ndpi_set_config(ndpi_info_mod, NULL, "lru.zoom.size", cfg_value); + } + if(fuzzed_data.ConsumeBool()) { + value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 + 1); + sprintf(cfg_value, "%d", value); + ndpi_set_config(ndpi_info_mod, NULL, "lru.zoom.ttl", cfg_value); + } + if(fuzzed_data.ConsumeBool()) { + value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 / 2); /* max / 2 instead of max + 1 to avoid oom on oss-fuzzer */ + sprintf(cfg_value, "%d", value); + ndpi_set_config(ndpi_info_mod, NULL, "lru.stun.size", cfg_value); + } + if(fuzzed_data.ConsumeBool()) { + value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 + 1); + sprintf(cfg_value, "%d", value); + ndpi_set_config(ndpi_info_mod, NULL, "lru.stun.ttl", cfg_value); + } + if(fuzzed_data.ConsumeBool()) { + value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 / 2); /* max / 2 instead of max + 1 to avoid oom on oss-fuzzer */ + sprintf(cfg_value, "%d", value); + ndpi_set_config(ndpi_info_mod, NULL, "lru.tls_cert.size", cfg_value); + } + if(fuzzed_data.ConsumeBool()) { + value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 + 1); + sprintf(cfg_value, "%d", value); + ndpi_set_config(ndpi_info_mod, NULL, "lru.tls_cert.ttl", cfg_value); + } + if(fuzzed_data.ConsumeBool()) { + value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 / 2); /* max / 2 instead of max + 1 to avoid oom on oss-fuzzer */ + sprintf(cfg_value, "%d", value); + ndpi_set_config(ndpi_info_mod, NULL, "lru.mining.size", cfg_value); + } + if(fuzzed_data.ConsumeBool()) { + value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 + 1); + sprintf(cfg_value, "%d", value); + ndpi_set_config(ndpi_info_mod, NULL, "lru.mining.ttl", cfg_value); + } + if(fuzzed_data.ConsumeBool()) { + value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 / 2); /* max / 2 instead of max + 1 to avoid oom on oss-fuzzer */ + sprintf(cfg_value, "%d", value); + ndpi_set_config(ndpi_info_mod, NULL, "lru.msteams.size", cfg_value); + } + if(fuzzed_data.ConsumeBool()) { + value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 + 1); + sprintf(cfg_value, "%d", value); + ndpi_set_config(ndpi_info_mod, NULL, "lru.msteams.ttl", cfg_value); + } + if(fuzzed_data.ConsumeBool()) { + value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 / 2); /* max / 2 instead of max + 1 to avoid oom on oss-fuzzer */ + sprintf(cfg_value, "%d", value); + ndpi_set_config(ndpi_info_mod, NULL, "lru.stun_zoom.size", cfg_value); + } + if(fuzzed_data.ConsumeBool()) { + value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 + 1); + sprintf(cfg_value, "%d", value); + ndpi_set_config(ndpi_info_mod, NULL, "lru.stun_zoom.ttl", cfg_value); + } ndpi_finalize_initialization(ndpi_info_mod); diff --git a/src/include/ndpi_api.h b/src/include/ndpi_api.h index 8c44cec0ac4f..7c4d6a7c1388 100644 --- a/src/include/ndpi_api.h +++ b/src/include/ndpi_api.h @@ -1074,20 +1074,6 @@ extern "C" { lru_cache_type cache_type, struct ndpi_lru_cache_stats *stats); - int ndpi_get_lru_cache_size(struct ndpi_detection_module_struct *ndpi_struct, - lru_cache_type cache_type, - u_int32_t *num_entries); - int ndpi_set_lru_cache_size(struct ndpi_detection_module_struct *ndpi_struct, - lru_cache_type cache_type, - u_int32_t num_entries); - - int ndpi_set_lru_cache_ttl(struct ndpi_detection_module_struct *ndpi_struct, - lru_cache_type cache_type, - u_int32_t ttl); - int ndpi_get_lru_cache_ttl(struct ndpi_detection_module_struct *ndpi_struct, - lru_cache_type cache_type, - u_int32_t *ttl); - int ndpi_set_opportunistic_tls(struct ndpi_detection_module_struct *ndpi_struct, u_int16_t proto, int value); int ndpi_get_opportunistic_tls(struct ndpi_detection_module_struct *ndpi_struct, diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index a5bdb66d14d6..80b999ba3e32 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -3422,24 +3422,6 @@ 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->stun_zoom_cache_num_entries = 1024; - - ndpi_str->ookla_cache_ttl = 120; /* sec */ - ndpi_str->bittorrent_cache_ttl = 0; - ndpi_str->zoom_cache_ttl = 0; - ndpi_str->stun_cache_ttl = 0; - ndpi_str->tls_cert_cache_ttl = 0; - ndpi_str->mining_cache_ttl = 0; - ndpi_str->msteams_cache_ttl = 60; /* sec */ - ndpi_str->stun_zoom_cache_ttl = 60; /* sec */ - ndpi_str->opportunistic_tls_smtp_enabled = 1; ndpi_str->opportunistic_tls_imap_enabled = 1; ndpi_str->opportunistic_tls_pop_enabled = 1; @@ -3520,68 +3502,68 @@ int 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, - ndpi_str->ookla_cache_ttl); + if(ndpi_str->cfg.ookla_cache_num_entries > 0) { + ndpi_str->ookla_cache = ndpi_lru_cache_init(ndpi_str->cfg.ookla_cache_num_entries, + ndpi_str->cfg.ookla_cache_ttl); if(!ndpi_str->ookla_cache) { NDPI_LOG_ERR(ndpi_str, "Error allocating lru cache (num_entries %u)\n", - ndpi_str->ookla_cache_num_entries); + ndpi_str->cfg.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, - ndpi_str->bittorrent_cache_ttl); + if(ndpi_str->cfg.bittorrent_cache_num_entries > 0) { + ndpi_str->bittorrent_cache = ndpi_lru_cache_init(ndpi_str->cfg.bittorrent_cache_num_entries, + ndpi_str->cfg.bittorrent_cache_ttl); if(!ndpi_str->bittorrent_cache) { NDPI_LOG_ERR(ndpi_str, "Error allocating lru cache (num_entries %u)\n", - ndpi_str->bittorrent_cache_num_entries); + ndpi_str->cfg.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, - ndpi_str->zoom_cache_ttl); + if(ndpi_str->cfg.zoom_cache_num_entries > 0) { + ndpi_str->zoom_cache = ndpi_lru_cache_init(ndpi_str->cfg.zoom_cache_num_entries, + ndpi_str->cfg.zoom_cache_ttl); if(!ndpi_str->zoom_cache) { NDPI_LOG_ERR(ndpi_str, "Error allocating lru cache (num_entries %u)\n", - ndpi_str->zoom_cache_num_entries); + ndpi_str->cfg.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, - ndpi_str->stun_cache_ttl); + if(ndpi_str->cfg.stun_cache_num_entries > 0) { + ndpi_str->stun_cache = ndpi_lru_cache_init(ndpi_str->cfg.stun_cache_num_entries, + ndpi_str->cfg.stun_cache_ttl); if(!ndpi_str->stun_cache) { NDPI_LOG_ERR(ndpi_str, "Error allocating lru cache (num_entries %u)\n", - ndpi_str->stun_cache_num_entries); + ndpi_str->cfg.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, - ndpi_str->tls_cert_cache_ttl); + if(ndpi_str->cfg.tls_cert_cache_num_entries > 0) { + ndpi_str->tls_cert_cache = ndpi_lru_cache_init(ndpi_str->cfg.tls_cert_cache_num_entries, + ndpi_str->cfg.tls_cert_cache_ttl); 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); + ndpi_str->cfg.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, - ndpi_str->mining_cache_ttl); + if(ndpi_str->cfg.mining_cache_num_entries > 0) { + ndpi_str->mining_cache = ndpi_lru_cache_init(ndpi_str->cfg.mining_cache_num_entries, + ndpi_str->cfg.mining_cache_ttl); if(!ndpi_str->mining_cache) { NDPI_LOG_ERR(ndpi_str, "Error allocating lru cache (num_entries %u)\n", - ndpi_str->mining_cache_num_entries); + ndpi_str->cfg.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, - ndpi_str->msteams_cache_ttl); + if(ndpi_str->cfg.msteams_cache_num_entries > 0) { + ndpi_str->msteams_cache = ndpi_lru_cache_init(ndpi_str->cfg.msteams_cache_num_entries, + ndpi_str->cfg.msteams_cache_ttl); if(!ndpi_str->msteams_cache) { NDPI_LOG_ERR(ndpi_str, "Error allocating lru cache (num_entries %u)\n", - ndpi_str->msteams_cache_num_entries); + ndpi_str->cfg.msteams_cache_num_entries); } } - if(ndpi_str->stun_zoom_cache_num_entries > 0) { - ndpi_str->stun_zoom_cache = ndpi_lru_cache_init(ndpi_str->stun_zoom_cache_num_entries, - ndpi_str->stun_zoom_cache_ttl); + if(ndpi_str->cfg.stun_zoom_cache_num_entries > 0) { + ndpi_str->stun_zoom_cache = ndpi_lru_cache_init(ndpi_str->cfg.stun_zoom_cache_num_entries, + ndpi_str->cfg.stun_zoom_cache_ttl); if(!ndpi_str->stun_zoom_cache) { NDPI_LOG_ERR(ndpi_str, "Error allocating lru cache (num_entries %u)\n", - ndpi_str->stun_zoom_cache_num_entries); + ndpi_str->cfg.stun_zoom_cache_num_entries); } } @@ -9868,154 +9850,6 @@ 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) -{ - if(!ndpi_struct) - return -1; - - 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; - case NDPI_LRUCACHE_STUN_ZOOM: - ndpi_struct->stun_zoom_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) -{ - if(!ndpi_struct) - return -1; - - 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; - case NDPI_LRUCACHE_STUN_ZOOM: - *num_entries = ndpi_struct->stun_zoom_cache_num_entries; - return 0; - default: - return -1; - } -} - -int ndpi_set_lru_cache_ttl(struct ndpi_detection_module_struct *ndpi_struct, - lru_cache_type cache_type, - u_int32_t ttl) -{ - if(!ndpi_struct) - return -1; - - switch(cache_type) { - case NDPI_LRUCACHE_OOKLA: - ndpi_struct->ookla_cache_ttl = ttl; - return 0; - case NDPI_LRUCACHE_BITTORRENT: - ndpi_struct->bittorrent_cache_ttl = ttl; - return 0; - case NDPI_LRUCACHE_ZOOM: - ndpi_struct->zoom_cache_ttl = ttl; - return 0; - case NDPI_LRUCACHE_STUN: - ndpi_struct->stun_cache_ttl = ttl; - return 0; - case NDPI_LRUCACHE_TLS_CERT: - ndpi_struct->tls_cert_cache_ttl = ttl; - return 0; - case NDPI_LRUCACHE_MINING: - ndpi_struct->mining_cache_ttl = ttl; - return 0; - case NDPI_LRUCACHE_MSTEAMS: - ndpi_struct->msteams_cache_ttl = ttl; - return 0; - case NDPI_LRUCACHE_STUN_ZOOM: - ndpi_struct->stun_zoom_cache_ttl = ttl; - return 0; - default: - return -1; - } -} - -int ndpi_get_lru_cache_ttl(struct ndpi_detection_module_struct *ndpi_struct, - lru_cache_type cache_type, - u_int32_t *ttl) -{ - if(!ndpi_struct || !ttl) - return -1; - - switch(cache_type) { - case NDPI_LRUCACHE_OOKLA: - *ttl = ndpi_struct->ookla_cache_ttl; - return 0; - case NDPI_LRUCACHE_BITTORRENT: - *ttl = ndpi_struct->bittorrent_cache_ttl; - return 0; - case NDPI_LRUCACHE_ZOOM: - *ttl = ndpi_struct->zoom_cache_ttl; - return 0; - case NDPI_LRUCACHE_STUN: - *ttl = ndpi_struct->stun_cache_ttl; - return 0; - case NDPI_LRUCACHE_TLS_CERT: - *ttl = ndpi_struct->tls_cert_cache_ttl; - return 0; - case NDPI_LRUCACHE_MINING: - *ttl = ndpi_struct->mining_cache_ttl; - return 0; - case NDPI_LRUCACHE_MSTEAMS: - *ttl = ndpi_struct->msteams_cache_ttl; - return 0; - case NDPI_LRUCACHE_STUN_ZOOM: - *ttl = ndpi_struct->stun_zoom_cache_ttl; - return 0; - default: - return -1; - } -} - /* ******************************************************************** */ /* @@ -10981,6 +10815,33 @@ static const struct cfg_param { { NULL, "filename.config", NULL, NULL, NULL, CFG_PARAM_FILENAME_CONFIG, __OFF(filename_config) }, + /* LRU caches */ + + { NULL, "lru.ookla.size", "1024", "0", "16777215", CFG_PARAM_INT, __OFF(ookla_cache_num_entries)}, + { NULL, "lru.ookla.ttl", "120", "0", "16777215", CFG_PARAM_INT, __OFF(ookla_cache_ttl)}, + + { NULL, "lru.bittorrent.size", "32768", "0", "16777215", CFG_PARAM_INT, __OFF(bittorrent_cache_num_entries)}, + { NULL, "lru.bittorrent.ttl", "0", "0", "16777215", CFG_PARAM_INT, __OFF(bittorrent_cache_ttl)}, + + { NULL, "lru.zoom.size", "512", "0", "16777215", CFG_PARAM_INT, __OFF(zoom_cache_num_entries)}, + { NULL, "lru.zoom.ttl", "0", "0", "16777215", CFG_PARAM_INT, __OFF(zoom_cache_ttl)}, + + { NULL, "lru.stun.size", "1024", "0", "16777215", CFG_PARAM_INT, __OFF(stun_cache_num_entries)}, + { NULL, "lru.stun.ttl", "0", "0", "16777215", CFG_PARAM_INT, __OFF(stun_cache_ttl)}, + + { NULL, "lru.tls_cert.size", "1024", "0", "16777215", CFG_PARAM_INT, __OFF(tls_cert_cache_num_entries)}, + { NULL, "lru.tls_cert.ttl", "0", "0", "16777215", CFG_PARAM_INT, __OFF(tls_cert_cache_ttl)}, + + { NULL, "lru.mining.size", "1024", "0", "16777215", CFG_PARAM_INT, __OFF(mining_cache_num_entries)}, + { NULL, "lru.mining.ttl", "0", "0", "16777215", CFG_PARAM_INT, __OFF(mining_cache_ttl)}, + + { NULL, "lru.msteams.size", "1024", "0", "16777215", CFG_PARAM_INT, __OFF(msteams_cache_num_entries)}, + { NULL, "lru.msteams.ttl", "60", "0", "16777215", CFG_PARAM_INT, __OFF(msteams_cache_ttl)}, + + { NULL, "lru.stun_zoom.size", "1024", "0", "16777215", CFG_PARAM_INT, __OFF(stun_zoom_cache_num_entries)}, + { NULL, "lru.stun_zoom.ttl", "60", "0", "16777215", CFG_PARAM_INT, __OFF(stun_zoom_cache_ttl)}, + + { NULL, NULL, NULL, NULL, NULL, 0, -1 }, }; diff --git a/src/lib/ndpi_private.h b/src/lib/ndpi_private.h index 4d8d52f208e4..ceb397dabe25 100644 --- a/src/lib/ndpi_private.h +++ b/src/lib/ndpi_private.h @@ -129,6 +129,25 @@ struct ndpi_detection_module_config_struct { char filename_config[CFG_MAX_LEN]; + /* LRU caches */ + + int ookla_cache_num_entries; + int ookla_cache_ttl; + int bittorrent_cache_num_entries; + int bittorrent_cache_ttl; + int zoom_cache_num_entries; + int zoom_cache_ttl; + int stun_cache_num_entries; + int stun_cache_ttl; + int tls_cert_cache_num_entries; + int tls_cert_cache_ttl; + int mining_cache_num_entries; + int mining_cache_ttl; + int msteams_cache_num_entries; + int msteams_cache_ttl; + int stun_zoom_cache_num_entries; + int stun_zoom_cache_ttl; + /* Protocols */ int tls_sha1_fingerprint_enabled; @@ -222,13 +241,9 @@ struct ndpi_detection_module_struct { /* NDPI_PROTOCOL_OOKLA */ struct ndpi_lru_cache *ookla_cache; - u_int32_t ookla_cache_num_entries; - u_int32_t ookla_cache_ttl; /* NDPI_PROTOCOL_BITTORRENT */ struct ndpi_lru_cache *bittorrent_cache; - u_int32_t bittorrent_cache_num_entries; - u_int32_t bittorrent_cache_ttl; /* NDPI_PROTOCOL_ZOOM */ struct ndpi_lru_cache *zoom_cache; @@ -237,26 +252,16 @@ struct ndpi_detection_module_struct { /* NDPI_PROTOCOL_STUN and subprotocols */ struct ndpi_lru_cache *stun_cache; - u_int32_t stun_cache_num_entries; - u_int32_t stun_cache_ttl; struct ndpi_lru_cache *stun_zoom_cache; - u_int32_t stun_zoom_cache_num_entries; - u_int32_t stun_zoom_cache_ttl; /* NDPI_PROTOCOL_TLS and subprotocols */ struct ndpi_lru_cache *tls_cert_cache; - u_int32_t tls_cert_cache_num_entries; - int32_t tls_cert_cache_ttl; /* NDPI_PROTOCOL_MINING and subprotocols */ struct ndpi_lru_cache *mining_cache; - u_int32_t mining_cache_num_entries; - u_int32_t mining_cache_ttl; /* NDPI_PROTOCOL_MSTEAMS */ struct ndpi_lru_cache *msteams_cache; - u_int32_t msteams_cache_num_entries; - u_int32_t msteams_cache_ttl; /* *** If you add a new LRU cache, please update lru_cache_type above! *** */ diff --git a/tests/cfgs/caches_cfg/config.txt b/tests/cfgs/caches_cfg/config.txt index a700bb270c95..82ede54272bf 100644 --- a/tests/cfgs/caches_cfg/config.txt +++ b/tests/cfgs/caches_cfg/config.txt @@ -1 +1 @@ ---lru-cache-size=ookla:0 --lru-cache-ttl=msteams:1 +--cfg=,lru.ookla.size,0 --cfg=,lru.msteams.ttl,1