Skip to content

Commit 5c13964

Browse files
committed
config: move LRU cache configurations to the new API
1 parent b9fca3e commit 5c13964

File tree

8 files changed

+164
-333
lines changed

8 files changed

+164
-333
lines changed

doc/configuration_parameters.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ TODO
88
| 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) |
99
| 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 |
1010
| "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 |
11+
| 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 |
12+
| 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 |

example/config.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
,packets_limit_per_flow,32
88
tls,metadata.sha1_fingerprint.enable,1
99

10+
,lru.bittorrent.ttl,0
11+
1012

example/ndpiReader.c

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,6 @@ int enable_malloc_bins = 0;
143143
int max_malloc_bins = 14;
144144
int malloc_size_stats = 0;
145145

146-
static int lru_cache_sizes[NDPI_LRUCACHE_MAX];
147-
static int lru_cache_ttls[NDPI_LRUCACHE_MAX];
148-
149146
struct flow_info {
150147
struct ndpi_flow_info *flow;
151148
u_int16_t thread_id;
@@ -607,16 +604,12 @@ static void help(u_int long_help) {
607604
" -A | Dump internal statistics (LRU caches / Patricia trees / Ahocarasick automas / ...\n"
608605
" -M | Memory allocation stats on data-path (only by the library). It works only on single-thread configuration\n"
609606
" -Z proto:value | Set this value of aggressiveness for this protocol (0 to disable it). This flag can be used multiple times\n"
610-
" --lru-cache-size=NAME:size | Specify the size for this LRU cache (0 to disable it). This flag can be used multiple times\n"
611-
" --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"
612607
" --cfg=proto,param,value | Configure the specific attribute of this protocol\n"
613608
,
614609
human_readeable_string_len,
615610
min_pattern_len, max_pattern_len, max_num_packets_per_flow, max_packet_payload_dissection,
616611
max_num_reported_top_payloads, max_num_tcp_dissected_pkts, max_num_udp_dissected_pkts);
617612

618-
printf("\nLRU Cache names: ookla, bittorrent, zoom, stun, tls_cert, mining, msteams, stun_zoom\n");
619-
620613
NDPI_PROTOCOL_BITMASK all;
621614
struct ndpi_detection_module_struct *ndpi_info_mod = ndpi_init_detection_module(init_prefs);
622615
NDPI_BITMASK_SET_ALL(all);
@@ -663,9 +656,6 @@ static void help(u_int long_help) {
663656
}
664657

665658

666-
#define OPTLONG_VALUE_LRU_CACHE_SIZE 1000
667-
#define OPTLONG_VALUE_LRU_CACHE_TTL 1001
668-
669659
#define OPTLONG_VALUE_CFG 3000
670660

671661
static struct option longopts[] = {
@@ -709,9 +699,6 @@ static struct option longopts[] = {
709699
{ "result-path", required_argument, NULL, 'w'},
710700
{ "quiet", no_argument, NULL, 'q'},
711701

712-
{ "lru-cache-size", required_argument, NULL, OPTLONG_VALUE_LRU_CACHE_SIZE},
713-
{ "lru-cache-ttl", required_argument, NULL, OPTLONG_VALUE_LRU_CACHE_TTL},
714-
715702
{ "cfg", required_argument, NULL, OPTLONG_VALUE_CFG},
716703

717704
{0, 0, 0, 0}
@@ -903,52 +890,6 @@ void printCSVHeader() {
903890
fprintf(csv_fp, "\n");
904891
}
905892

906-
static int cache_idx_from_name(const char *name)
907-
{
908-
if(strcmp(name, "ookla") == 0)
909-
return NDPI_LRUCACHE_OOKLA;
910-
if(strcmp(name, "bittorrent") == 0)
911-
return NDPI_LRUCACHE_BITTORRENT;
912-
if(strcmp(name, "zoom") == 0)
913-
return NDPI_LRUCACHE_ZOOM;
914-
if(strcmp(name, "stun") == 0)
915-
return NDPI_LRUCACHE_STUN;
916-
if(strcmp(name, "tls_cert") == 0)
917-
return NDPI_LRUCACHE_TLS_CERT;
918-
if(strcmp(name, "mining") == 0)
919-
return NDPI_LRUCACHE_MINING;
920-
if(strcmp(name, "msteams") == 0)
921-
return NDPI_LRUCACHE_MSTEAMS;
922-
if(strcmp(name, "stun_zoom") == 0)
923-
return NDPI_LRUCACHE_STUN_ZOOM;
924-
return -1;
925-
}
926-
927-
static int parse_cache_param(char *param, int *cache_idx, int *param_value)
928-
{
929-
char *saveptr, *tmp_str, *cache_str, *param_str;
930-
int idx;
931-
932-
tmp_str = ndpi_strdup(param);
933-
if(tmp_str) {
934-
cache_str = strtok_r(tmp_str, ":", &saveptr);
935-
if(cache_str) {
936-
param_str = strtok_r(NULL, ":", &saveptr);
937-
if(param_str) {
938-
idx = cache_idx_from_name(cache_str);
939-
if(idx >= 0) {
940-
*cache_idx = idx;
941-
*param_value = atoi(param_str);
942-
ndpi_free(tmp_str);
943-
return 0;
944-
}
945-
}
946-
}
947-
}
948-
ndpi_free(tmp_str);
949-
return -1;
950-
}
951-
952893
static int parse_two_unsigned_integer(char *param, u_int32_t *num1, u_int32_t *num2)
953894
{
954895
char *saveptr, *tmp_str, *num1_str, *num2_str;
@@ -1035,7 +976,6 @@ static void parseOptions(int argc, char **argv) {
1035976
u_int num_cores = sysconf(_SC_NPROCESSORS_ONLN);
1036977
#endif
1037978
#endif
1038-
int cache_idx, cache_size, cache_ttl;
1039979
char *s1, *s2, *s3;
1040980

1041981
#ifdef USE_DPDK
@@ -1052,11 +992,6 @@ static void parseOptions(int argc, char **argv) {
1052992
for(i = 0; i < NDPI_MAX_SUPPORTED_PROTOCOLS; i++)
1053993
aggressiveness[i] = -1; /* Use the default value */
1054994

1055-
for(i = 0; i < NDPI_LRUCACHE_MAX; i++) {
1056-
lru_cache_sizes[i] = -1; /* Use the default value */
1057-
lru_cache_ttls[i] = -1; /* Use the default value */
1058-
}
1059-
1060995
while((opt = getopt_long(argc, argv,
1061996
"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:",
1062997
longopts, &option_idx)) != EOF) {
@@ -1364,22 +1299,6 @@ static void parseOptions(int argc, char **argv) {
13641299
if(max_num_udp_dissected_pkts < 3) max_num_udp_dissected_pkts = 3;
13651300
break;
13661301

1367-
case OPTLONG_VALUE_LRU_CACHE_SIZE:
1368-
if(parse_cache_param(optarg, &cache_idx, &cache_size) == -1) {
1369-
printf("Invalid parameter [%s]\n", optarg);
1370-
exit(1);
1371-
}
1372-
lru_cache_sizes[cache_idx] = cache_size;
1373-
break;
1374-
1375-
case OPTLONG_VALUE_LRU_CACHE_TTL:
1376-
if(parse_cache_param(optarg, &cache_idx, &cache_ttl) == -1) {
1377-
printf("Invalid parameter [%s]\n", optarg);
1378-
exit(1);
1379-
}
1380-
lru_cache_ttls[cache_idx] = cache_ttl;
1381-
break;
1382-
13831302
case OPTLONG_VALUE_CFG:
13841303
if(parse_three_strings(optarg, &s1, &s2, &s3) == -1 ||
13851304
__add_cfg(s1, s2, s3, 0) == -1) {
@@ -2889,20 +2808,6 @@ static void setupDetection(u_int16_t thread_id, pcap_t * pcap_handle) {
28892808
if(_protoFilePath != NULL)
28902809
ndpi_load_protocols_file(ndpi_thread_info[thread_id].workflow->ndpi_struct, _protoFilePath);
28912810

2892-
/* Enable/disable/configure LRU caches size here */
2893-
for(i = 0; i < NDPI_LRUCACHE_MAX; i++) {
2894-
if(lru_cache_sizes[i] != -1)
2895-
ndpi_set_lru_cache_size(ndpi_thread_info[thread_id].workflow->ndpi_struct,
2896-
i, lru_cache_sizes[i]);
2897-
}
2898-
2899-
/* Enable/disable LRU caches TTL here */
2900-
for(i = 0; i < NDPI_LRUCACHE_MAX; i++) {
2901-
if(lru_cache_ttls[i] != -1)
2902-
ndpi_set_lru_cache_ttl(ndpi_thread_info[thread_id].workflow->ndpi_struct,
2903-
i, lru_cache_ttls[i]);
2904-
}
2905-
29062811
/* Set aggressiveness here */
29072812
for(i = 0; i < NDPI_MAX_SUPPORTED_PROTOCOLS; i++) {
29082813
if(aggressiveness[i] != -1)

fuzz/fuzz_config.cpp

Lines changed: 81 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
1313
struct ndpi_detection_module_struct *ndpi_info_mod;
1414
struct ndpi_flow_struct flow;
1515
u_int8_t protocol_was_guessed;
16-
u_int32_t i, num;
16+
u_int32_t i;
1717
u_int16_t random_proto, bool_value;
1818
int random_value;
1919
NDPI_PROTOCOL_BITMASK enabled_bitmask;
@@ -73,16 +73,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
7373
if(fuzzed_data.ConsumeBool())
7474
ndpi_load_ipv4_ptree(ndpi_info_mod, "ipv4_addresses.txt", NDPI_PROTOCOL_TLS);
7575

76-
for(i = 0; i < NDPI_LRUCACHE_MAX + 1; i++) { /* + 1 to test invalid type */
77-
ndpi_set_lru_cache_size(ndpi_info_mod, static_cast<lru_cache_type>(i),
78-
fuzzed_data.ConsumeIntegralInRange(0, (1 << 16) - 1));
79-
ndpi_get_lru_cache_size(ndpi_info_mod, static_cast<lru_cache_type>(i), &num);
80-
81-
ndpi_set_lru_cache_ttl(ndpi_info_mod, static_cast<lru_cache_type>(i),
82-
fuzzed_data.ConsumeIntegralInRange(0, (1 << 24) - 1));
83-
ndpi_get_lru_cache_ttl(ndpi_info_mod, static_cast<lru_cache_type>(i), &num);
84-
}
85-
8676
/* TODO: stub for geo stuff */
8777
ndpi_load_geoip(ndpi_info_mod, NULL, NULL);
8878

@@ -124,6 +114,86 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
124114
sprintf(cfg_value, "%d", value);
125115
ndpi_set_config(ndpi_info_mod, NULL, "packets_limit_per_flow", cfg_value);
126116
}
117+
if(fuzzed_data.ConsumeBool()) {
118+
value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 / 2); /* max / 2 instead of max + 1 to avoid oom on oss-fuzzer */
119+
sprintf(cfg_value, "%d", value);
120+
ndpi_set_config(ndpi_info_mod, NULL, "lru.ookla.size", cfg_value);
121+
}
122+
if(fuzzed_data.ConsumeBool()) {
123+
value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 + 1);
124+
sprintf(cfg_value, "%d", value);
125+
ndpi_set_config(ndpi_info_mod, NULL, "lru.ookla.ttl", cfg_value);
126+
}
127+
if(fuzzed_data.ConsumeBool()) {
128+
value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 / 2); /* max / 2 instead of max + 1 to avoid oom on oss-fuzzer */
129+
sprintf(cfg_value, "%d", value);
130+
ndpi_set_config(ndpi_info_mod, NULL, "lru.bittorrent.size", cfg_value);
131+
}
132+
if(fuzzed_data.ConsumeBool()) {
133+
value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 + 1);
134+
sprintf(cfg_value, "%d", value);
135+
ndpi_set_config(ndpi_info_mod, NULL, "lru.bittorrent.ttl", cfg_value);
136+
}
137+
if(fuzzed_data.ConsumeBool()) {
138+
value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 / 2); /* max / 2 instead of max + 1 to avoid oom on oss-fuzzer */
139+
sprintf(cfg_value, "%d", value);
140+
ndpi_set_config(ndpi_info_mod, NULL, "lru.zoom.size", cfg_value);
141+
}
142+
if(fuzzed_data.ConsumeBool()) {
143+
value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 + 1);
144+
sprintf(cfg_value, "%d", value);
145+
ndpi_set_config(ndpi_info_mod, NULL, "lru.zoom.ttl", cfg_value);
146+
}
147+
if(fuzzed_data.ConsumeBool()) {
148+
value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 / 2); /* max / 2 instead of max + 1 to avoid oom on oss-fuzzer */
149+
sprintf(cfg_value, "%d", value);
150+
ndpi_set_config(ndpi_info_mod, NULL, "lru.stun.size", cfg_value);
151+
}
152+
if(fuzzed_data.ConsumeBool()) {
153+
value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 + 1);
154+
sprintf(cfg_value, "%d", value);
155+
ndpi_set_config(ndpi_info_mod, NULL, "lru.stun.ttl", cfg_value);
156+
}
157+
if(fuzzed_data.ConsumeBool()) {
158+
value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 / 2); /* max / 2 instead of max + 1 to avoid oom on oss-fuzzer */
159+
sprintf(cfg_value, "%d", value);
160+
ndpi_set_config(ndpi_info_mod, NULL, "lru.tls_cert.size", cfg_value);
161+
}
162+
if(fuzzed_data.ConsumeBool()) {
163+
value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 + 1);
164+
sprintf(cfg_value, "%d", value);
165+
ndpi_set_config(ndpi_info_mod, NULL, "lru.tls_cert.ttl", cfg_value);
166+
}
167+
if(fuzzed_data.ConsumeBool()) {
168+
value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 / 2); /* max / 2 instead of max + 1 to avoid oom on oss-fuzzer */
169+
sprintf(cfg_value, "%d", value);
170+
ndpi_set_config(ndpi_info_mod, NULL, "lru.mining.size", cfg_value);
171+
}
172+
if(fuzzed_data.ConsumeBool()) {
173+
value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 + 1);
174+
sprintf(cfg_value, "%d", value);
175+
ndpi_set_config(ndpi_info_mod, NULL, "lru.mining.ttl", cfg_value);
176+
}
177+
if(fuzzed_data.ConsumeBool()) {
178+
value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 / 2); /* max / 2 instead of max + 1 to avoid oom on oss-fuzzer */
179+
sprintf(cfg_value, "%d", value);
180+
ndpi_set_config(ndpi_info_mod, NULL, "lru.msteams.size", cfg_value);
181+
}
182+
if(fuzzed_data.ConsumeBool()) {
183+
value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 + 1);
184+
sprintf(cfg_value, "%d", value);
185+
ndpi_set_config(ndpi_info_mod, NULL, "lru.msteams.ttl", cfg_value);
186+
}
187+
if(fuzzed_data.ConsumeBool()) {
188+
value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 / 2); /* max / 2 instead of max + 1 to avoid oom on oss-fuzzer */
189+
sprintf(cfg_value, "%d", value);
190+
ndpi_set_config(ndpi_info_mod, NULL, "lru.stun_zoom.size", cfg_value);
191+
}
192+
if(fuzzed_data.ConsumeBool()) {
193+
value = fuzzed_data.ConsumeIntegralInRange(0, 16777215 + 1);
194+
sprintf(cfg_value, "%d", value);
195+
ndpi_set_config(ndpi_info_mod, NULL, "lru.stun_zoom.ttl", cfg_value);
196+
}
127197

128198
ndpi_finalize_initialization(ndpi_info_mod);
129199

src/include/ndpi_api.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,20 +1074,6 @@ extern "C" {
10741074
lru_cache_type cache_type,
10751075
struct ndpi_lru_cache_stats *stats);
10761076

1077-
int ndpi_get_lru_cache_size(struct ndpi_detection_module_struct *ndpi_struct,
1078-
lru_cache_type cache_type,
1079-
u_int32_t *num_entries);
1080-
int ndpi_set_lru_cache_size(struct ndpi_detection_module_struct *ndpi_struct,
1081-
lru_cache_type cache_type,
1082-
u_int32_t num_entries);
1083-
1084-
int ndpi_set_lru_cache_ttl(struct ndpi_detection_module_struct *ndpi_struct,
1085-
lru_cache_type cache_type,
1086-
u_int32_t ttl);
1087-
int ndpi_get_lru_cache_ttl(struct ndpi_detection_module_struct *ndpi_struct,
1088-
lru_cache_type cache_type,
1089-
u_int32_t *ttl);
1090-
10911077
int ndpi_set_opportunistic_tls(struct ndpi_detection_module_struct *ndpi_struct,
10921078
u_int16_t proto, int value);
10931079
int ndpi_get_opportunistic_tls(struct ndpi_detection_module_struct *ndpi_struct,

0 commit comments

Comments
 (0)