Performance optimization to avoid un-necessary calls and thus increase the overall performance

This commit is contained in:
Luca Deri
2021-02-11 17:54:55 +01:00
parent d99d636696
commit 813df77d07
+40 -8
View File
@@ -1639,11 +1639,38 @@ static int fill_prefix_v6(prefix_t *prefix, const struct in6_addr *addr, int bit
/* ******************************************* */ /* ******************************************* */
u_int8_t ndpi_is_public_ipv4(u_int32_t a /* host byte order */) {
if( ((a & 0xFF000000) == 0x0A000000 /* 10.0.0.0/8 */)
|| ((a & 0xFFF00000) == 0xAC100000 /* 172.16.0.0/12 */)
|| ((a & 0xFFFF0000) == 0xC0A80000 /* 192.168.0.0/16 */)
|| ((a & 0xFF000000) == 0x7F000000 /* 127.0.0.0/8 */)
|| ((a & 0xF0000000) == 0xE0000000 /* 224.0.0.0/4 */)
)
return(0);
else
return(1);
}
/* ******************************************* */
u_int16_t ndpi_network_ptree_match(struct ndpi_detection_module_struct *ndpi_str, u_int16_t ndpi_network_ptree_match(struct ndpi_detection_module_struct *ndpi_str,
struct in_addr *pin /* network byte order */) { struct in_addr *pin /* network byte order */) {
prefix_t prefix; prefix_t prefix;
patricia_node_t *node; patricia_node_t *node;
if(ndpi_str->ndpi_num_custom_protocols == 0) {
/*
In case we don't have defined any custom protocol we check the ptree
only in case of public IP addresses as in ndpi_content_match.c.inc
we only have public IP addresses. Instead with custom protocols, users
might have defined private protocols hence we should not skip
the checks below
*/
if(ndpi_is_public_ipv4(ntohl(pin->s_addr)) == 0)
return(NDPI_PROTOCOL_UNKNOWN); /* Non public IP */
}
/* Make sure all in network byte order otherwise compares wont work */ /* Make sure all in network byte order otherwise compares wont work */
fill_prefix_v4(&prefix, pin, 32, ((patricia_tree_t *) ndpi_str->protocols_ptree)->maxbits); fill_prefix_v4(&prefix, pin, 32, ((patricia_tree_t *) ndpi_str->protocols_ptree)->maxbits);
node = ndpi_patricia_search_best(ndpi_str->protocols_ptree, &prefix); node = ndpi_patricia_search_best(ndpi_str->protocols_ptree, &prefix);
@@ -2602,7 +2629,8 @@ int ndpi_handle_rule(struct ndpi_detection_module_struct *ndpi_str, char *rule,
} }
for (i = 0, def = NULL; i < (int) ndpi_str->ndpi_num_supported_protocols; i++) { for (i = 0, def = NULL; i < (int) ndpi_str->ndpi_num_supported_protocols; i++) {
if(ndpi_str->proto_defaults[i].protoName && strcasecmp(ndpi_str->proto_defaults[i].protoName, proto) == 0) { if(ndpi_str->proto_defaults[i].protoName
&& strcasecmp(ndpi_str->proto_defaults[i].protoName, proto) == 0) {
def = &ndpi_str->proto_defaults[i]; def = &ndpi_str->proto_defaults[i];
subprotocol_id = i; subprotocol_id = i;
break; break;
@@ -4510,6 +4538,9 @@ uint8_t ndpi_connection_tracking(struct ndpi_detection_module_struct *ndpi_str,
void ndpi_fill_protocol_category(struct ndpi_detection_module_struct *ndpi_str, struct ndpi_flow_struct *flow, void ndpi_fill_protocol_category(struct ndpi_detection_module_struct *ndpi_str, struct ndpi_flow_struct *flow,
ndpi_protocol *ret) { ndpi_protocol *ret) {
if((ret->master_protocol == NDPI_PROTOCOL_UNKNOWN) && (ret->app_protocol == NDPI_PROTOCOL_UNKNOWN))
return;
if(ndpi_str->custom_categories.categories_loaded) { if(ndpi_str->custom_categories.categories_loaded) {
if(flow->guessed_header_category != NDPI_PROTOCOL_CATEGORY_UNSPECIFIED) { if(flow->guessed_header_category != NDPI_PROTOCOL_CATEGORY_UNSPECIFIED) {
flow->category = ret->category = flow->guessed_header_category; flow->category = ret->category = flow->guessed_header_category;
@@ -4695,7 +4726,8 @@ uint8_t ndpi_connection_tracking(struct ndpi_detection_module_struct *ndpi_str,
flow->guessed_host_protocol_id = ndpi_guess_host_protocol_id(ndpi_str, flow); flow->guessed_host_protocol_id = ndpi_guess_host_protocol_id(ndpi_str, flow);
if(ndpi_str->custom_categories.categories_loaded && flow->packet.iph) { if(ndpi_str->custom_categories.categories_loaded && flow->packet.iph) {
ndpi_fill_ip_protocol_category(ndpi_str, flow->packet.iph->saddr, flow->packet.iph->daddr, ret); if(ndpi_str->ndpi_num_custom_protocols != 0)
ndpi_fill_ip_protocol_category(ndpi_str, flow->packet.iph->saddr, flow->packet.iph->daddr, ret);
flow->guessed_header_category = ret->category; flow->guessed_header_category = ret->category;
} else } else
flow->guessed_header_category = NDPI_PROTOCOL_CATEGORY_UNSPECIFIED; flow->guessed_header_category = NDPI_PROTOCOL_CATEGORY_UNSPECIFIED;
@@ -4705,8 +4737,8 @@ uint8_t ndpi_connection_tracking(struct ndpi_detection_module_struct *ndpi_str,
ret->master_protocol = NDPI_PROTOCOL_UNKNOWN, ret->master_protocol = NDPI_PROTOCOL_UNKNOWN,
ret->app_protocol = flow->guessed_protocol_id ? flow->guessed_protocol_id : flow->guessed_host_protocol_id; ret->app_protocol = flow->guessed_protocol_id ? flow->guessed_protocol_id : flow->guessed_host_protocol_id;
/* The call below is useful but not necessary, so let's skip for the moment... */ // if(ndpi_str->ndpi_num_custom_protocols != 0)
// ndpi_fill_protocol_category(ndpi_str, flow, ret); ndpi_fill_protocol_category(ndpi_str, flow, ret);
return(-1); return(-1);
} }
@@ -4719,8 +4751,8 @@ uint8_t ndpi_connection_tracking(struct ndpi_detection_module_struct *ndpi_str,
*ret = ndpi_detection_giveup(ndpi_str, flow, 0, &protocol_was_guessed); *ret = ndpi_detection_giveup(ndpi_str, flow, 0, &protocol_was_guessed);
} }
/* The call below is useful but not necessary, so let's skip for the moment... */ // if(ndpi_str->ndpi_num_custom_protocols != 0)
// ndpi_fill_protocol_category(ndpi_str, flow, ret); ndpi_fill_protocol_category(ndpi_str, flow, ret);
return(-1); return(-1);
} }
} else { } else {
@@ -4759,8 +4791,8 @@ uint8_t ndpi_connection_tracking(struct ndpi_detection_module_struct *ndpi_str,
num_calls = ndpi_check_flow_func(ndpi_str, flow, &ndpi_selection_packet); num_calls = ndpi_check_flow_func(ndpi_str, flow, &ndpi_selection_packet);
/* The call below is useful but not necessary, so let's skip for the moment... */ //if(ndpi_str->ndpi_num_custom_protocols != 0)
// ndpi_fill_protocol_category(ndpi_str, flow, ret); ndpi_fill_protocol_category(ndpi_str, flow, ret);
return(-1); return(-1);
} }