nfqws,tpws: optimize empty hostlists check in dp_match()

This commit is contained in:
bol-van 2024-11-03 21:43:22 +03:00
parent 9e84bf7a42
commit f2ea38cf93
2 changed files with 10 additions and 4 deletions

View File

@ -151,6 +151,8 @@ static bool dp_match(
uint8_t l3proto, const struct sockaddr *dest, const char *hostname, t_l7proto l7proto, uint8_t l3proto, const struct sockaddr *dest, const char *hostname, t_l7proto l7proto,
bool *bCheckDone, bool *bCheckResult, bool *bExcluded) bool *bCheckDone, bool *bCheckResult, bool *bExcluded)
{ {
bool bHostlistsEmpty;
if (bCheckDone) *bCheckDone = false; if (bCheckDone) *bCheckDone = false;
if (!HostlistsReloadCheckForProfile(dp)) return false; if (!HostlistsReloadCheckForProfile(dp)) return false;
@ -164,7 +166,8 @@ static bool dp_match(
if (dp->filter_l7 && !l7_proto_match(l7proto, dp->filter_l7)) if (dp->filter_l7 && !l7_proto_match(l7proto, dp->filter_l7))
// L7 filter does not match // L7 filter does not match
return false; return false;
if (!dp->hostlist_auto && !hostname && !PROFILE_HOSTLISTS_EMPTY(dp)) bHostlistsEmpty = PROFILE_HOSTLISTS_EMPTY(dp);
if (!dp->hostlist_auto && !hostname && !bHostlistsEmpty)
// avoid cpu consuming ipset check. profile cannot win if regular hostlists are present without auto hostlist and hostname is unknown. // avoid cpu consuming ipset check. profile cannot win if regular hostlists are present without auto hostlist and hostname is unknown.
return false; return false;
if (!IpsetCheck(dp, dest->sa_family==AF_INET ? &((struct sockaddr_in*)dest)->sin_addr : NULL, dest->sa_family==AF_INET6 ? &((struct sockaddr_in6*)dest)->sin6_addr : NULL)) if (!IpsetCheck(dp, dest->sa_family==AF_INET ? &((struct sockaddr_in*)dest)->sin_addr : NULL, dest->sa_family==AF_INET6 ? &((struct sockaddr_in6*)dest)->sin6_addr : NULL))
@ -174,7 +177,7 @@ static bool dp_match(
// autohostlist profile matching l3/l4/l7 filter always win // autohostlist profile matching l3/l4/l7 filter always win
if (dp->hostlist_auto) return true; if (dp->hostlist_auto) return true;
if (PROFILE_HOSTLISTS_EMPTY(dp)) if (bHostlistsEmpty)
// profile without hostlist filter wins // profile without hostlist filter wins
return true; return true;
else else

View File

@ -26,6 +26,8 @@ static bool l7_proto_match(t_l7proto l7proto, uint32_t filter_l7)
static bool dp_match(struct desync_profile *dp, const struct sockaddr *dest, const char *hostname, t_l7proto l7proto) static bool dp_match(struct desync_profile *dp, const struct sockaddr *dest, const char *hostname, t_l7proto l7proto)
{ {
bool bHostlistsEmpty;
if (!HostlistsReloadCheckForProfile(dp)) return false; if (!HostlistsReloadCheckForProfile(dp)) return false;
if ((dest->sa_family==AF_INET && !dp->filter_ipv4) || (dest->sa_family==AF_INET6 && !dp->filter_ipv6)) if ((dest->sa_family==AF_INET && !dp->filter_ipv4) || (dest->sa_family==AF_INET6 && !dp->filter_ipv6))
@ -37,7 +39,8 @@ static bool dp_match(struct desync_profile *dp, const struct sockaddr *dest, con
if (dp->filter_l7 && !l7_proto_match(l7proto, dp->filter_l7)) if (dp->filter_l7 && !l7_proto_match(l7proto, dp->filter_l7))
// L7 filter does not match // L7 filter does not match
return false; return false;
if (!dp->hostlist_auto && !hostname && !PROFILE_HOSTLISTS_EMPTY(dp)) bHostlistsEmpty = PROFILE_HOSTLISTS_EMPTY(dp);
if (!dp->hostlist_auto && !hostname && !bHostlistsEmpty)
// avoid cpu consuming ipset check. profile cannot win if regular hostlists are present without auto hostlist and hostname is unknown. // avoid cpu consuming ipset check. profile cannot win if regular hostlists are present without auto hostlist and hostname is unknown.
return false; return false;
if (!IpsetCheck(dp, dest->sa_family==AF_INET ? &((struct sockaddr_in*)dest)->sin_addr : NULL, dest->sa_family==AF_INET6 ? &((struct sockaddr_in6*)dest)->sin6_addr : NULL)) if (!IpsetCheck(dp, dest->sa_family==AF_INET ? &((struct sockaddr_in*)dest)->sin_addr : NULL, dest->sa_family==AF_INET6 ? &((struct sockaddr_in6*)dest)->sin6_addr : NULL))
@ -47,7 +50,7 @@ static bool dp_match(struct desync_profile *dp, const struct sockaddr *dest, con
// autohostlist profile matching l3/l4/l7 filter always win // autohostlist profile matching l3/l4/l7 filter always win
if (dp->hostlist_auto) return true; if (dp->hostlist_auto) return true;
if (PROFILE_HOSTLISTS_EMPTY(dp)) if (bHostlistsEmpty)
// profile without hostlist filter wins // profile without hostlist filter wins
return true; return true;
else if (hostname) else if (hostname)