Bug #9454
opencapture-bypass: flows forcibly evicted at flow memcap skip the bypass update
Description
Affected versions: main; 8.0.x has the same code in FlowGetUsedFlow() / StillAlive() (not verified by a run on 8.0.x)
Description¶
When the flow memcap is reached and no spare flow is available, a worker thread takes a used flow from the flow hash (FlowGetUsedFlow()), skipping flows that were seen recently (StillAlive(): lastts within 1 to 30 seconds depending on the flow state).
For a capture-bypassed flow (FLOW_STATE_CAPTURE_BYPASSED) lastts is not a measure of activity: Suricata does not see its packets, and lastts is only refreshed when the flow manager polls the capture method through the BypassUpdate() callback, about once per bypassed flow timeout. A flow that is active in the capture method therefore looks idle to StillAlive() about 30 seconds after each poll and is evicted.
The worker clears the flow without running BypassUpdate():
- the packets and bytes the capture method counted since the last poll are not added to
flow_bypassed.pkts/flow_bypassed.bytes, and the flow record misses them; - the capture side entries are not removed. With eBPF/XDP bypass both half-flow keys stay in
flow_table_v4/flow_table_v6; no flow references them any more, so nothing removes them until Suricata exits (or, with pinned maps, until the next start reads them back). The kernel keeps bypassing that 5-tuple; - the per-interface bypassed flow count (
iface-bypassed-stats) is not decremented.
Ticket #8442 is the same effect through the worker timeout check.
Steps to reproduce¶
AF_PACKET with XDP bypass on a veth, 1 worker, flow.memcap: 256kb, flow.prealloc: 50, flow-timeouts.udp.bypassed: 100, rule alert udp any any -> any 9999 (content:"BYPASSME"; bypass; sid:1;).
- Send one UDP flow to port 9999 continuously (5 packets/s) for 50 seconds. It is bypassed on its first packet; the kernel map has 2 entries.
- After 36 seconds (more than 30 seconds after the bypass) replay a burst of new UDP flows that exceeds the flow memcap.
- Stop Suricata after the traffic ended.
Script: repro2/run_gu.sh <build> <outdir>.
Observed¶
flow.get_used is non-zero and a flow record for the bypassed flow is written during the flood with "reason":"forced", "state":"bypassed", "bypass":"capture" while the flow is still sending. flow_bypassed.pkts and flow_bypassed.bytes stay at 0. The two map entries remain until Suricata exits.
Expected¶
The bypassed flow is left to the flow manager: it stays in the flow table while the capture method reports activity, its counters reach flow_bypassed.pkts / flow_bypassed.bytes, and its map entries are removed when it times out.
LS Updated by Lukas Sismis about 3 hours ago
- Affected Versions 8.0.8, git main added
LS Updated by Lukas Sismis about 3 hours ago
- Related to Bug #8033: BPF map (flow_table_v4) is not cleaned well even after network traffic stop being replayed added