Skip to content

Commit

Permalink
flow: Add cfg for optional flow reuse during low memory
Browse files Browse the repository at this point in the history
By default, force flow reuse to reuse an existing flows no matter
the state of the flow.

Add a configuration option flow.force-reuse, enabled by default, that
can turn off the above behaviour.

Ticket: #6293
  • Loading branch information
coledishington committed Jan 28, 2024
1 parent 3cb7112 commit 337ba14
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
8 changes: 8 additions & 0 deletions doc/userguide/configuration/suricata-yaml.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,14 @@ percent of the 10000 flows is completed).
emergency_recovery: 30 #Percentage of 1000 prealloc'd flows.
prune_flows: 5 #Amount of flows being terminated during the emergency mode.

If aggressive flow pruning in emergency-mode is not desired, it can be disabled by
configuring flow.force_reuse.

::

flow:
force_reuse: false

Flow Time-Outs
~~~~~~~~~~~~~~

Expand Down
7 changes: 6 additions & 1 deletion src/flow-hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -1157,9 +1157,14 @@ static inline bool StillAlive(const Flow *f, const SCTime_t ts)
*/
static Flow *FlowGetUsedFlow(ThreadVars *tv, DecodeThreadVars *dtv, const SCTime_t ts)
{
uint32_t idx = GetUsedAtomicUpdate(FLOW_GET_NEW_TRIES) % flow_config.hash_size;
uint32_t tried = 0;
uint32_t idx;

if (!flow_config.force_reuse) {
return NULL;
}

idx = GetUsedAtomicUpdate(FLOW_GET_NEW_TRIES) % flow_config.hash_size;
while (1) {
if (tried++ > FLOW_GET_NEW_TRIES) {
STATSADDUI64(counter_flow_get_used_eval, tried);
Expand Down
14 changes: 11 additions & 3 deletions src/flow.c
Original file line number Diff line number Diff line change
Expand Up @@ -623,11 +623,19 @@ void FlowInitConfig(bool quiet)
}
}

int confint_val;
if (ConfGetBool("flow.force-reuse", &confint_val) != 1) {
flow_config.force_reuse = true;
} else {
flow_config.force_reuse = !!confint_val;
}

flow_config.memcap_policy = ExceptionPolicyParse("flow.memcap-policy", false);

SCLogDebug("Flow config from suricata.yaml: memcap: %"PRIu64", hash-size: "
"%"PRIu32", prealloc: %"PRIu32, SC_ATOMIC_GET(flow_config.memcap),
flow_config.hash_size, flow_config.prealloc);
SCLogDebug("Flow config from suricata.yaml: memcap: %" PRIu64 ", hash-size: "
"%" PRIu32 ", prealloc: %" PRIu32 ", reuse: %s",
SC_ATOMIC_GET(flow_config.memcap), flow_config.hash_size, flow_config.prealloc,
flow_config.force_reuse ? "force" : "disabled");

/* alloc hash memory */
uint64_t hash_size = flow_config.hash_size * sizeof(FlowBucket);
Expand Down
3 changes: 3 additions & 0 deletions src/flow.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ typedef struct FlowCnf_
uint32_t hash_size;
uint32_t prealloc;

/* Controls if non-expired flows are re-used in low memory conditions. */
bool force_reuse;

uint32_t timeout_new;
uint32_t timeout_est;

Expand Down
1 change: 1 addition & 0 deletions suricata.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,7 @@ flow:
emergency-recovery: 30
#managers: 1 # default to one flow manager
#recyclers: 1 # default to one flow recycler thread
#force-reuse: true # Default to forcing flow reuse in low memory conditions

# This option controls the use of VLAN ids in the flow (and defrag)
# hashing. Normally this should be enabled, but in some (broken)
Expand Down

0 comments on commit 337ba14

Please sign in to comment.