Flows are preallocated at startup and stored in protocol specific caches.
FlowKey is used for quick look up in the cache hash table.

Each flow may have associated inspectors:

* clouseau is the Wizard bound to the flow to help determine the
  appropriate service inspector

* gadget is the service inspector

* data is a passive service inspector such as a client config.

FlowData is used by various inspectors to store specific data on the flow
for later use.  Any inspector may store data on the flow, not just clouseau
gadget.

FlowData reference counts the associated inspector so that the inspector
can be freed (via garbage collection) after a reload.

There are many flags that may be set on a flow to indicate session tracking
state, disposition, etc.

==== High Availability

HighAvailability (ha.cc, ha.h) serves to synchronize session state between high
availability partners.  The primary purpose is to exchange session state with the
goal of keeping the session caches in sync.  Other clients may also register
with the HA service to exchange additional session data by implementing one or
more FlowHAClient classes.

HA uses Side Channel Connectors and/or DAQ module IOCTLs to transmit and receive
HA state messages.  A full duplex Side Channel is required for Side Channel
communications.  A DAQ module that supports the IOCTLs for setting and getting
HA state is required for a functional DAQ-backed setup.  Modules that do not
support these IOCTLs will silently fail and present no HA state on future
packets in the flow.  HA state will be queried from the DAQ module prior to new
flow creation when the appropriate DAQ packet message flag has been set on the
initiating packet.

The HA subsystem exchanges two high level message types:
  - DELETE: Indicate to the partner that a session has been removed.  No
additional HA client status will be exchanged.  (Not used for DAQ-backed
storage.)
  - UPDATE: Indicate all other state changes.  The message always includes
the session state and optionally may include state from other HA clients.
By default, the update messages are incremental.  In the case of DAQ-backed
storage, the update messages are always fully formed.

The HA subsystem implements these classes:
  - HighAvailabilityManager - A collection of static elements providing the
    top-most interface to HA capabilities.
  - HAMessage - A wrapper around the actual message and includes a cursor and
    convenience functions for producer/consumer activity.  Passed around
    among all message handing classes/methods.
  - HighAvailability - If HA is enabled, instantiated in each packet thread and
    provides all primary HA functionality for the thread.  Referenced via a
    THREAD_LOCAL object pointer.
  - FlowHAState - One per flow and referenced via a pointer in the Flow object.
    Contains all dynamic state information.  A set of get'ers and set'ers are
    implemented to help manage the flags, client pending flags, and timing
    information.
  - FlowHAClient - All HA coordination is managed via a set of FlowHAClient's.
    Every function that desires to use HA capabilities defines one FlowHAClient
    object within the packet processing thread.  Each client is provided a
    handle bit-mask as part of the pending logic.  The client may set the
    pending bit to indicate the desire to add content to the HA message.
    A maximum of 17 clients may existing (numbers 1-16, corresponding to the
    16 bits of the pending flags.  Client 0 is the 'session', is always present,
    and is handled as a special case.  Client 0 is the fundamental session HA
    state sync functionality.  Other clients are optional.


09/25/2023
In response to the need for more nuanced management of different protocol
types within the `flow_cache`, we've leveraged the newly added feature in
xhash. This enhancement includes the introduction of Protocol-Based
Least Recently Used (LRU) caches, which substantially improves the handling
of multiple protocol types in the cache.
With this implementation, during various pruning sessions (idle,
excess, and timeout etc), we've adopted a round-robin method to cycle through
the LRU lists designated for each protocol type. This methodology ensures
that every protocol, irrespective of its inherent timeout characteristics,
is given an equal opportunity for pruning and retirement from the cache.
This adjustment to `flow_cache` not only addresses the previously observed
bias during pruning sessions, especially concerning UDP flows,
but lays down a framework for more advanced and controlled data management
within the cache moving forward.

10/25/2024
Allowlist LRU
To address the need for preserving flows that have been allowlisted and are
at risk of timing out, we've introduced a configurable Allowlist LRU cache
within the flow_cache. This enhancement enables the retention of flows marked
with a whitelist verdict, preventing them from being prematurely pruned due
to inactivity timeouts. This is particularly beneficial in scenarios where
Snort ceases to observe traffic for a flow after the whitelist decision,
especially if that flow is long-lived. Without this adjustment,
such flows may be pruned by the cache upon timeout, potentially impacting
event logging at the flow’s end-of-life (EOF) due to missing
pruned flow information.

The Allowlist LRU cache is disabled by default but can be enabled by adding
allowlist_cache = { enable = true } in the stream configuration.
Like the protocol-based LRUs, this allowlist functionality is an
additional LRU rather than a new hash_table, thereby maintaining
consistent performance with previous configurations.

04/07/2025
Changed the flow data hash to start with 7 hash buckets. This is trying to balance
between memory usage and speed.
The flow data hash load factor controls the when a hash table rebalance happens. We
don't ever want a hash rebalance to happen for flow data as that would mean allocating
a new bigger bucket array and rehashing all of the elements in the hash. So, a max
load factor of 5.0 was chosen. This means that the average depth for all buckets in
the hash would need to be 5 or more before a rebalance would happen. This is very
unlikely to happen.

05/06/2026
==== HA Reload

HighAvailabilityManager supports config reload. Two pieces cooperate:

  - HAReloadTuner (a ReloadSwapper registered from snort.cc::get_reload_config())
    runs first at tswap on each packet thread.  Its job is to delete and recreate
    the THREAD_LOCAL HighAvailability object so it matches the new config.

  - Each HA-using inspector (stream + stream_tcp/udp/icmp/ip and appid)
    registers its own ReloadSwapper from its configure().  These run after
    HAReloadTuner in FIFO order; each one calls its plugin's tterm()+tinit() to
    rebuild the plugin's FlowHAClient instances against the freshly created
    HighAvailability object.

During reload, InspectorManager::thread_reinit() calls each inspector's
tinit() before any ReloadSwapper::tswap() runs (see Analyzer::reinit() and
Swapper::apply()).  The plugin's per-thread HA client pointer therefore still
references the prior cycle's instance at the time tinit() fires, and the new
HighAvailability object hasn't been built yet.  Plugin tinit() must guard
against re-entry (typically: skip the new() when the pointer is non-null);
the per-plugin ReloadSwapper will tterm()+tinit() once HAReloadTuner::tswap()
has installed the new HighAvailability.  Without this guard, the prior
FlowHAClient leaks and FlowHAClient's ctor logs a spurious
"already-occupied index" error.

HighAvailabilityManager::configure() runs on the main thread and is called
unconditionally on every reload so the client-name registry is reset every
cycle. Data-path entrypoints gate only on the per-thread ha pointer, so a
reload aborted after configure() but before tswap is harmless.

FlowHAClient indices are assigned by sorting the registered names in
finalize_ha_client_indices(), called once per Snort::init() and once per
get_reload_config() (only when HA is configured).  Both peers in an HA pair
must register the same set of names so the sorted order, and therefore the
on-wire client indices, are identical.  Names are constructed by the API
(register_ha_client / FlowHAClient ctor) from the inspector's module name
plus an optional suffix; this guarantees uniqueness across plugins without
relying on convention.  Every client (including the session/stream client)
gets a regular index and handle = 1 << bit; the session client is identified
by HAClientInfo::is_session, not by any reserved handle value.  The
pending-update mask is 16 bits, so a maximum of 16 clients total are allowed.

When peers register divergent name sets the standby will hit unknown_client_idx
or client_consume_errors counters.


SideChannel reload limitation
-----------------------------
SideChannelManager is configured once at process startup (snort.cc calls
SideChannelManager::pre_config_init() and the per-thread set is built by
SideChannelManager::thread_init() during Analyzer startup); it does not
participate in reload.  HighAvailability::HighAvailability() looks up its
SideChannel via SideChannelManager::get_side_channel(port) against that
startup-time set.

Consequence: enabling HA on a reload, disabling it, or changing the HA
ports/min_session_lifetime/min_sync_interval/daq_channel settings is fully
supported, but adding, removing, or repointing a side_channel{} configuration
is not honored at reload time -- the new SideChannel definition will be
ignored and HA will continue to use (or fail to find) the startup-time
channel.  Changes to side_channel{} require a process restart.

06/03/2026
==== Per-Flow Memory Tracking

Flow::get_mem_usage() returns a running byte count of heap allocations
relevant to the flow. get_peak_mem_usage() returns the high water mark for the current
logical session. Flow::restart() resets the peak to the current retained footprint so a
reused Flow object does not carry the previous session's peak forward. Both accessors are
intended to be O(1) so they can be queried often (e.g on pruning).

The choice to use a push model here, rather than summing the flow's allocations on demand
is deliberate. It allows us to query the flow's memory utilization quickly (and frequently,
in the case of pruning) with limited perf hit, at the cost of accuracy.

The count is kept in two ways:
1) Fixed footprints which are accounted for automatically. Every FlowData object
that's attached via set_flow_data() has its size_of() added by FlowDataStore on attach,
and is subtracted on detach (set/erase/clear). Because the same value is being added
and removed, this will ensure we don't drift over time and "leak" allocations.

2) Dynamic footprints, which are pushed at the site of allocation. Inspectors that grow
heap allocations can call flow->add_mem(n) at allocation, and sub_mem(n) at deallocation.
This is for objects and buffers not covered by a FlowData size_of(). Currently pushed:
  - the per-protocol Session object itself (sizeof(*this)), in the ctor/dtor of
    tcp/udp/icmp/ip/file/user session (stream/*).
  - queued TCP reassembly segments (stream/tcp/tcp_reassembly_segments.cc).
  - HTTP decompression state (http_inspect, via the zlib/brotli allocator hooks in
    http_compress_stream.cc).
  - FlowStash items: the StashItem wrapper plus each item's key/string capacity, on the
    stash write path (flow_stash.cc). A stashed generic object's own heap is the object's
    responsibility to push.
  - appid_ha.cc consume path repeats the sizeof(AppIdSessionApi) add that
    allocate_session() would otherwise do, since it bypasses it.
HTTP/HTTP2 reassembly buffers are not yet tracked.

sub_mem's under-subtraction handling is deliberately two-tier. In debug builds
assert(bytes + sizeof(Flow) <= mem_bytes) is a tripwire: any subtraction that would push
mem_bytes below its sizeof(Flow) baseline is an alloc/dealloc mismatch and aborts loudly.
In release builds (assert compiled out) the subtraction clamps at zero rather than back up
to sizeof(Flow). This is intentional: it prevents an unsigned wrap-around that would blow
up the MAX/SUM pegs with a ~2^64 value, and it leaves an out-of-range value (below
sizeof(Flow), possibly zero) that visibly flags the mismatch in production -- clamping up
to a plausible sizeof(Flow) would hide the bug instead.

=== Adding a new FlowData subclass

Put one macro (from flow_data.h) in a public/protected section:

  FLOW_DATA_SIZE_OF(MyFlowData)                  // sizeof(*this)
  FLOW_DATA_SIZE_OF_EXTRA(MyFlowData, EXTRA)     // + always-present heap stuff

The EXTRA macro covers objects that this class owns for its whole lifetime, e.g
members that are new'd at construction and freed in the destructor. 

Skipping the macro isn't a build error, we let size_of() default to just sizeof(FlowData), so
the class still works and is counted, but we undercount in this scenario.

For transient buffers that are grown during packet processing, use the add_mem/sub_mem calls from
the above section.

=== Observability:
  * dump_flow_memory_summary control (live view, stream module): walks the cache
    reading get_mem_usage()/get_peak_mem_usage() and prints a per-protocol and ALL
    summary (count/total/avg/cur_max/peak_max), a current-bytes histogram, and the
    top-N flows by peak (optional arg: N, default 10).

  * dump_flows / dump_flows_binary controls (stream module): per-flow dump. The
    text form prints "flow_mem N bytes"; the binary form serializes flow_mem
    (get_mem_usage) and peak_flow_mem (get_peak_mem_usage) in the
    DumpFlowsBinaryDescriptor wire record. That record is a raw byte image with no
    version framing, so its layout is an external contract shared by tools/show_flows
    and the out-of-tree correlate_conns consumer; the static_assert on its size is
    the tripwire for keeping those consumers in sync.

  * stream.flow_mem trace option (trace framework): logs each flow's current and
    peak memory as it is released, retired, or reload-deleted (flow_cache.cc).
    Emitted at debug
    verbosity like the stream_tcp traces, so enable it with
    trace = { modules = { stream = { flow_mem = 7 } } } -- a low-level
    "enable all" won't trip it.

  * Stream base peg counters, sampled at release/retire and reload-driven
    delete (flow_cache.cc):
    - released_max_flow_mem (MAX): largest peak_mem_usage seen at release on
      this thread during the interval.
    - released_total_flow_mem (SUM): cumulative get_mem_usage() released
      across all flows.

08/28/2026
Blocked flows and prune candidacy
The uni list holds uni-prune candidates, not every unidirectional flow.
prune_unis() cannot reclaim a blocked flow, so a flow leaves the list when it
blocks; Flow::set_block_flags() is the single idempotent funnel that sets the
SSNFLAG_DROP_* bits and drops candidacy.  Only uni pruning reads the list, so the
flow stays in the hash table and its protocol LRU, found by find() and counted by
current_flows.  uni_flows and uni_ip_flows count candidates.

prune_unis() unlinks any blocked flow it meets, since HA import ordering or a direct
session_flags write can block one without the funnel; when that unlink alone
satisfies the exit condition the reclaim is recorded EXCESS, not UNI.
max_blocked_unlink_per_prune caps the unlinking, and cleanup_flows is 1, so a call
may unlink up to the cap while reclaiming one flow; a deeper backlog drains over
several allocations through prune_excess() until the list falls to max_uni.

prune_excess() is bounded by max_excess_examined_per_prune, which fixes what one
allocation pays to walk past flows it cannot take.  It round robins the allowlist and
per-protocol LRUs, releasing an oldest flow free to take and rotating past one that
is not, so a run of blocked or offloaded heads costs a walk rather than a reclaim.
Only a head actually read spends budget, so empty LRUs cost a pointer read and a
single-protocol deployment keeps its budget.  A lap finding every reachable LRU empty
ends the walk, which is what terminates it when the rest sit in an LRU the rotation
never visits.

Reclaiming nothing falls back to prune_multiple(), bounded by config.prune_flows.
prune_one() checks neither block state nor save_me, making the fallback both where
excess pruning can take a blocked flow and what holds the cache at max_flows.
prune_flows is "1:max32" default 10, so it is zero only when the stream module never
configures -- a unit test, or no -c -- and only then can excess pruning return zero
while over max_flows.  Zero disables every prune_multiple() caller, not just this one:
the flow memcap prunes through the same function, Stream::prune_flows() registered as
the MemoryCap prune handler, so a blocked flow is never exempt from memory reclaim and
block state cannot pin the cache above the cap.

Reclaiming nothing includes exhausting the budget with reclaimable flows deeper than
the bound, so above the bound the preference inverts: a blocked run longer than the
bound ahead of an eligible flow costs prune_flows blocked flows and spares the
eligible one.  That is the price of a fixed per-allocation cost, paid to hold
max_flows.  The inversion is transient, because a declined head is rotated to MRU and
the next call resumes past it, so the walk re-reads nothing and crosses a run of N
blocked flows in N reads: with 300 blocked ahead of one eligible and prune_flows 1,
the calls read 256 then 44 and the second takes the eligible flow.  The cost is per
over-cap episode and bounded by the backlog, not by the packet rate, and an episode
ends as soon as the walk reaches an eligible flow.

A reclaim of prune_flows leaves the cache prune_flows below max_flows, and allocate()
prunes only at max_flows, so the allocations that fill that gap back in never reach
prune_excess() at all.  Anything sampling the walk has to allow for that gap; the
larger prune_flows is, the more allocations pass between walks.

Leaving the list is not permanent.  Flow::update_uni_candidacy() compares the
session_flags from before a write and moves the flow off or back on if the write
changed its block state; set_block_flags() and restart() go through it, and
relink_uni() is a no-op for a listed flow.  Relinking undoes only what the block
took, so the flow must have been a candidate apart from the block -- not
bidirectional before the write -- and still be one after; without the first,
restart() would relink a flow unlinked for two way traffic, and without the second an
HA import turning a blocked unidirectional flow bidirectional would relink it.  A
write bypassing both reconciles itself, since StreamHAClient::consume() assigns
ssn_state wholesale and the drop bits are HA critical, so a standby learns both block
and unblock.

restart() is also reached from Flow::reset() during teardown, so the flow must leave
the list after its reset, not before, or the list keeps a node about to be freed;
release() and retire() get that from remove(), and the reload delete path unlinks
explicitly.

LRU round robin bookkeeping
The loops walking every per-protocol LRU carry a bitmask of the LRUs they are done
with and stop when all are set.  Two rules keep them terminating.

* A branch declining to reclaim the head must retire that LRU or advance its cursor;
  doing neither faces the same head with the same mask forever.  timeout() advances
  with lru_next(), retires the LRU when that runs out, and tracks the protocols in
  play as a mask rather than an index, so nothing depends on where the previous pass
  stopped.

* The exit test must cover exactly the LRUs the loop visits, so there are two.
  prune_idle() and delete_active_flows() walk first_proto through max_protocols and
  never touch PktType::NONE, so they use all_proto_lrus_checked() against
  proto_lru_mask.  prune_multiple() starts at zero and wraps through it, so it uses
  all_lrus_checked() and the full mask; the protocol-only test would let it stop
  before draining PktType::NONE.

LRU zero is nearly a one way door, which is why the excess walk carries an empty lap
terminator no real run needs.  allocate() indexes by the key's pkt_type and will put
a flow there, but purge(), prune_idle(), timeout() and delete_active_flows() start at
first_proto, and prune_multiple() reaches zero only through prune_one(), which
refuses the table's last node -- so such a flow prunes down to one and no further.
Production never gets there, since a key's pkt_type comes from a decoded packet; that
also makes the terminator untestable, as draining the LRU to prove the walk exits
leaves a flow the cache cannot free.  It stays so termination does not rest on LRU
zero being empty.
