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

Thresholds/v17 #11324

Closed
wants to merge 25 commits into from
Closed

Thresholds/v17 #11324

wants to merge 25 commits into from

Conversation

victorjulien
Copy link
Member

SV_BRANCH=OISF/suricata-verify#1923

#11254 rebased

Add support for 'by_flow' track option. This allows using the various
threshold options in the context of a single flow.

Example:

    alert tcp ... stream-event:pkt_broken_ack; \
        threshold:type limit, track by_flow, count 1, seconds 3600;

The example would limit the number of alerts to once per hour for
packets triggering the 'pkt_broken_ack' stream event.

Implemented as a special "flowvar" holding the threshold entries. This
means no synchronization is required, making this a cheaper option
compared to the other trackers.

Ticket: OISF#6822.
Allow rate_filter and thresholds from the global config to specify
tracking "by_flow".
Traffic variables (flowvars, flowbits, xbits, etc) use a smaller int for
their type than detection types. As a workaround make sure the values fit
in a uint8_t.
Limits propegation checked for DETECT_DEPTH as a content flag,
which appears to have worked by chance. After reshuffling the
keyword id's it no longer worked. This patch uses the proper
flag DETECT_CONTENT_DEPTH.
Thresholding often has 2 stages:

1. recording matches
2. appling an action, like suppress

E.g. with something like:
threshold:type limit, count 10, seconds 3600, track by_src;
the recording state is about counting 10 first hits for an IP,
then followed by the "suppress" state that might last an hour.

By_src/by_dst are expensive, as they do a host table lookup and lock
the host. If many threads require this access, lock contention becomes
a serious problem.

This patch adds a thread local cache to avoid the synchronization
overhead. When the threshold for a host enters the "apply" stage,
a thread local hash entry is added. This entry knows the expiry
time and the action to apply. This way the action can be applied
w/o the synchronization overhead.

A rbtree is used to handle expiration.
Packet pointer is not used during allocation.
Add a callback and helper function to handle data expiration.

Update datasets to explicitly not use expiration.
Instead of a Host and IPPair table thresholding layer, use a dedicated
THash to store both. This allows hashing on host+sid+tracker or
ippair+sid+tracker, to create more unique hash keys.

This allows for fewer hash collisions.

The per rule tracking also uses this, so that the single big lock is no
longer a single point of contention.

Reimplement storage for flow thresholds to reuse as much logic as
possible from the host/ippair/rule thresholds.

Ticket: OISF#426.
Use the same hash key as for the regular threshold storage,
so include gid, rev, tentant id.
@victorjulien victorjulien requested review from jufajardini and a team as code owners June 18, 2024 20:37
@victorjulien victorjulien mentioned this pull request Jun 18, 2024
Copy link

codecov bot commented Jun 18, 2024

Codecov Report

Attention: Patch coverage is 92.42144% with 41 lines in your changes missing coverage. Please review.

Project coverage is 82.48%. Comparing base (49ecf37) to head (8dee1b5).
Report is 43 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #11324      +/-   ##
==========================================
+ Coverage   82.47%   82.48%   +0.01%     
==========================================
  Files         934      934              
  Lines      252270   252346      +76     
==========================================
+ Hits       208055   208146      +91     
+ Misses      44215    44200      -15     
Flag Coverage Δ
fuzzcorpus 60.20% <24.78%> (-0.06%) ⬇️
livemode 18.74% <13.59%> (-0.03%) ⬇️
pcap 43.63% <67.10%> (-0.14%) ⬇️
suricata-verify 61.30% <61.40%> (-0.02%) ⬇️
unittests 59.98% <72.27%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

@suricata-qa
Copy link

Information: QA ran without warnings.

Pipeline 21131

@victorjulien victorjulien added the needs rebase Needs rebase to master label Jun 24, 2024
@victorjulien victorjulien mentioned this pull request Jun 24, 2024
@victorjulien
Copy link
Member Author

replaced by #11358

@victorjulien victorjulien deleted the thresholds/v17 branch July 2, 2024 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs rebase Needs rebase to master
Development

Successfully merging this pull request may close these issues.

2 participants