Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor optimizations/v1 #12398

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/app-layer-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,11 @@ uint64_t AppLayerParserGetTransactionInspectId(AppLayerParserState *pstate, uint
{
SCEnter();

if (pstate == NULL)
SCReturnCT(0ULL, "uint64_t");
if (pstate != NULL)
SCReturnCT(pstate->inspect_id[(direction & STREAM_TOSERVER) ? 0 : 1], "uint64_t");

SCReturnCT(pstate->inspect_id[(direction & STREAM_TOSERVER) ? 0 : 1], "uint64_t");
DEBUG_VALIDATE_BUG_ON(1);
SCReturnCT(0ULL, "uint64_t");
}

inline uint64_t AppLayerParserGetTxDetectFlags(AppLayerTxData *txd, const uint8_t dir)
Expand Down
4 changes: 4 additions & 0 deletions src/detect-engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -1955,6 +1955,10 @@ static int DetectEngineInspectRulePayloadMatches(
SCLogDebug("SIG_FLAG_REQUIRE_STREAM_ONLY, so no match");
return DETECT_ENGINE_INSPECT_SIG_NO_MATCH;
}
if (s->flags & SIG_FLAG_REQUIRE_STREAM_ONLY) {
SCLogDebug("SIG_FLAG_REQUIRE_STREAM_ONLY, so no match");
return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return DETECT_ENGINE_INSPECT_SIG_NO_MATCH for consistency?
as

src/detect-engine-state.h:#define DETECT_ENGINE_INSPECT_SIG_NO_MATCH 0

}
Comment on lines +1958 to +1961
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference b/w this and the branch right above this?

if (DetectEngineInspectPacketPayload(de_ctx, det_ctx, s, p->flow, p) != 1) {
return DETECT_ENGINE_INSPECT_SIG_NO_MATCH;
}
Expand Down
7 changes: 7 additions & 0 deletions src/detect-flow.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,13 @@ int DetectFlowSetup (DetectEngineCtx *de_ctx, Signature *s, const char *flowstr)
DetectFlowFree(de_ctx, fd);
}

if (parse_flags & DETECT_FLOW_FLAG_ONLYSTREAM) {
s->flags |= (SIG_FLAG_REQUIRE_STREAM | SIG_FLAG_REQUIRE_STREAM_ONLY);
}
if (parse_flags & DETECT_FLOW_FLAG_NOSTREAM) {
s->flags |= SIG_FLAG_REQUIRE_PACKET;
}

if (parse_flags & DETECT_FLOW_FLAG_ONLYSTREAM) {
s->flags |= (SIG_FLAG_REQUIRE_STREAM | SIG_FLAG_REQUIRE_STREAM_ONLY);
}
Expand Down
4 changes: 2 additions & 2 deletions src/detect.c
Original file line number Diff line number Diff line change
Expand Up @@ -1340,14 +1340,14 @@ static DetectTransaction GetDetectTx(const uint8_t ipproto, const AppProto alpro
}
uint64_t detect_flags =
(flow_flags & STREAM_TOSERVER) ? txd->detect_flags_ts : txd->detect_flags_tc;
if (detect_flags & APP_LAYER_TX_INSPECTED_FLAG) {
if (unlikely(detect_flags & APP_LAYER_TX_INSPECTED_FLAG)) {
SCLogDebug("%"PRIu64" tx already fully inspected for %s. Flags %016"PRIx64,
tx_id, flow_flags & STREAM_TOSERVER ? "toserver" : "toclient",
detect_flags);
DetectTransaction no_tx = NO_TX;
return no_tx;
}
if (detect_flags & APP_LAYER_TX_SKIP_INSPECT_FLAG) {
if (unlikely(detect_flags & APP_LAYER_TX_SKIP_INSPECT_FLAG)) {
SCLogDebug("%" PRIu64 " tx should not be inspected in direction %s. Flags %016" PRIx64,
tx_id, flow_flags & STREAM_TOSERVER ? "toserver" : "toclient", detect_flags);
DetectTransaction no_tx = NO_TX;
Expand Down
36 changes: 18 additions & 18 deletions src/tm-threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -2345,23 +2345,23 @@ uint16_t TmThreadsGetWorkerThreadMax(void)
*/
void TmThreadsInjectFlowById(Flow *f, const int id)
{
BUG_ON(id <= 0 || id > (int)thread_store.threads_size);

int idx = id - 1;

Thread *t = &thread_store.threads[idx];
ThreadVars *tv = t->tv;

BUG_ON(tv == NULL || tv->flow_queue == NULL);

FlowEnqueue(tv->flow_queue, f);

/* wake up listening thread(s) if necessary */
if (tv->inq != NULL) {
SCMutexLock(&tv->inq->pq->mutex_q);
SCCondSignal(&tv->inq->pq->cond_q);
SCMutexUnlock(&tv->inq->pq->mutex_q);
} else if (tv->break_loop) {
TmThreadsCaptureBreakLoop(tv);
if (id > 0 && id <= (int)thread_store.threads_size) {
int idx = id - 1;
Thread *t = &thread_store.threads[idx];
ThreadVars *tv = t->tv;
if (tv != NULL && tv->flow_queue != NULL) {
FlowEnqueue(tv->flow_queue, f);

/* wake up listening thread(s) if necessary */
if (tv->inq != NULL) {
SCMutexLock(&tv->inq->pq->mutex_q);
SCCondSignal(&tv->inq->pq->cond_q);
SCMutexUnlock(&tv->inq->pq->mutex_q);
} else if (tv->break_loop) {
TmThreadsCaptureBreakLoop(tv);
}
return;
}
}
BUG_ON(1);
}
33 changes: 16 additions & 17 deletions src/util-prefilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,24 @@ int PrefilterAddSidsResize(PrefilterRuleStore *pmq, uint32_t new_size);
static inline void PrefilterAddSids(
PrefilterRuleStore *pmq, const SigIntId *sids, uint32_t sids_size)
{
if (sids_size == 0)
return;

uint32_t new_size = pmq->rule_id_array_cnt + sids_size;
if (new_size > pmq->rule_id_array_size) {
if (PrefilterAddSidsResize(pmq, new_size) == 0) {
// Failed to allocate larger memory for all the SIDS, but
// keep as many as we can.
sids_size = pmq->rule_id_array_size - pmq->rule_id_array_cnt;
if (sids_size > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not following why is an extra indentation preferable over return if base condition doesn't match.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic I think is that it assumes the first branch (so sids_size == 0 leading to return) to be the most likely.

uint32_t new_size = pmq->rule_id_array_cnt + sids_size;
if (new_size > pmq->rule_id_array_size) {
if (PrefilterAddSidsResize(pmq, new_size) == 0) {
// Failed to allocate larger memory for all the SIDS, but
// keep as many as we can.
sids_size = pmq->rule_id_array_size - pmq->rule_id_array_cnt;
}
}
SCLogDebug("Adding %u sids", sids_size);
// Add SIDs for this pattern to the end of the array
SigIntId *ptr = pmq->rule_id_array + pmq->rule_id_array_cnt;
SigIntId *end = ptr + sids_size;
do {
*ptr++ = *sids++;
} while (ptr != end);
pmq->rule_id_array_cnt += sids_size;
}
SCLogDebug("Adding %u sids", sids_size);
// Add SIDs for this pattern to the end of the array
SigIntId *ptr = pmq->rule_id_array + pmq->rule_id_array_cnt;
SigIntId *end = ptr + sids_size;
do {
*ptr++ = *sids++;
} while (ptr != end);
pmq->rule_id_array_cnt += sids_size;
}

int PmqSetup(PrefilterRuleStore *);
Expand Down
Loading