From 3b826fff6840b24e54a086606a14185745b6d3a3 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Wed, 15 Nov 2023 19:18:08 +0100 Subject: [PATCH] detect/tag: reuse result of previous host lookup Minor optimization that could lead to a reduction in host table lookups if more than one host feature is in use. --- src/detect-engine-tag.c | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/detect-engine-tag.c b/src/detect-engine-tag.c index aceb5f10c6..e6a4134048 100644 --- a/src/detect-engine-tag.c +++ b/src/detect-engine-tag.c @@ -493,6 +493,26 @@ static void TagHandlePacketHost(Host *host, Packet *p) } } +static Host *GetLockedSrcHost(Packet *p) +{ + if (p->host_src == NULL) { + p->host_src = HostLookupHostFromHash(&p->src); + } else { + HostLock(p->host_src); + } + return p->host_src; +} + +static Host *GetLockedDstHost(Packet *p) +{ + if (p->host_dst == NULL) { + p->host_dst = HostLookupHostFromHash(&p->dst); + } else { + HostLock(p->host_dst); + } + return p->host_dst; +} + /** * \brief Search tags for src and dst. Update entries of the tag, remove if necessary * @@ -516,20 +536,22 @@ void TagHandlePacket(DetectEngineCtx *de_ctx, TagHandlePacketFlow(p->flow, p); } - Host *src = HostLookupHostFromHash(&p->src); - if (src) { + Host *src = GetLockedSrcHost(p); + if (src != NULL) { if (TagHostHasTag(src)) { - TagHandlePacketHost(src,p); + TagHandlePacketHost(src, p); } - HostRelease(src); + HostUnlock(src); } - Host *dst = HostLookupHostFromHash(&p->dst); - if (dst) { + + Host *dst = GetLockedDstHost(p); + if (dst != NULL) { if (TagHostHasTag(dst)) { - TagHandlePacketHost(dst,p); + TagHandlePacketHost(dst, p); } - HostRelease(dst); + HostUnlock(dst); } + SCReturn; }