Add DeviceConfiguration::disableImmediateMode to allow TPACKET_V3 on Linux - #2210
Add DeviceConfiguration::disableImmediateMode to allow TPACKET_V3 on Linux#2210MerinoSheep wants to merge 12 commits into
DeviceConfiguration::disableImmediateMode to allow TPACKET_V3 on Linux#2210Conversation
…Linux doOpen() unconditionally enables libpcap's immediate mode whenever the platform supports it, with no way to opt out. On Linux this forces libpcap to fall back from TPACKET_V3 to TPACKET_V2 and forces a non-blocking poll_timeout=0, turning the capture loop into a continuous busy-poll spin instead of an efficient blocking wakeup. Add a disableImmediateMode field (default false, preserving existing behavior) so callers can opt out of immediate mode and let libpcap use TPACKET_V3 with blocking, batched reads bounded by packetBufferTimeoutMs. A loopback benchmark comparing both modes under synthetic UDP flood showed ~14x lower capture-thread CPU usage and ~1.7x higher throughput with immediate mode disabled. Co-authored-by: Cursor <cursoragent@cursor.com>
Trim redundant restating of behavior in favor of the why: TPACKET_V2 vs TPACKET_V3 and busy-poll vs blocking wait tradeoffs. Co-authored-by: Cursor <cursoragent@cursor.com>
Lead with what the flag does, add a manpage reference, and match the style used by other DeviceConfiguration fields in this file. Co-authored-by: Cursor <cursoragent@cursor.com>
With immediate mode disabled, the packet buffer timeout controls delivery latency, but the platform default passed to pcap_set_timeout was -1 on Linux/Windows, which libpcap documents as unpredictable and which can delay delivery until a ring block fills. Fall back to 100 ms when packetBufferTimeoutMs is not set, drop the manpage URL from the doc comment, and fix a continuation-line alignment nit. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The disableImmediateMode timeout fallback was unconditionally substituting 100ms whenever packetBufferTimeoutMs was unset, clobbering the small positive LIBPCAP_OPEN_LIVE_TIMEOUT already used on FreeBSD/macOS to work around a pcap_breakloop() bug. Only fall back to 100ms when the platform default is itself non-positive (Linux/Windows), leaving FreeBSD/macOS behavior unchanged regardless of disableImmediateMode. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## dev #2210 +/- ##
==========================================
+ Coverage 81.30% 83.01% +1.71%
==========================================
Files 319 328 +9
Lines 52180 59679 +7499
Branches 12124 12455 +331
==========================================
+ Hits 42423 49544 +7121
- Misses 7519 9143 +1624
+ Partials 2238 992 -1246
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| /// Disable libpcap's "immediate mode", which PcapPlusPlus enables by default (where supported) for | ||
| /// minimal packet delivery latency. On Linux, immediate mode also forces a less efficient capture | ||
| /// path (TPACKET_V2 with non-blocking polling, instead of TPACKET_V3 with blocking, batched reads), | ||
| /// which can hurt throughput and CPU usage at high packet rates. Setting this to true trades some | ||
| /// added latency (up to packetBufferTimeoutMs, or 100 ms on Linux/Windows if not set) for those | ||
| /// throughput and CPU benefits. | ||
| /// Default value is false. | ||
| bool disableImmediateMode; |
There was a problem hiding this comment.
IMO, positive flags are easier to reason about than negative flags since the reader does not have to negate the condition. useImmediateMode defaulted to true might be easier to reason about when looking through the docs.
Also, I am wondering, do you think it would be better to have the flag be an enum instead of a bool?
Something like:
enum class BufferingMode
{
Default, // let the device decide (current behavior)
Immediate, // use immediate mode
Buffered // use buffered mode
}Default mode can also possibly have a graceful fallback to buffered, if immediate mode fails to enable for some reason.
There was a problem hiding this comment.
yeah good idea, Ill change
Use an enum class (Default/Immediate/Buffered) instead of a boolean flag, per review discussion.
Reformat throw to a single line per clang-format, and suppress a cppcheck knownConditionTrueFalse false positive on platforms where LIBPCAP_OPEN_LIVE_TIMEOUT is a positive constant.
Summary
PCAPPP_ENABLE_PCAP_IMMEDIATE_MODE, defaultOFF).DeviceConfiguration::disableImmediateMode(defaultfalse, backward compatible) to opt out and let Linux use TPACKET_V3.Changes
PcapLiveDevice.h: newdisableImmediateModefield + constructor param (defaultfalse).PcapLiveDevice.cpp:doOpen()skipspcap_set_immediate_mode()when set.packetBufferTimeoutMsis given (their default-1is unpredictable without immediate mode). FreeBSD/macOS keep their existingpcap_breakloop()workaround timeouts untouched.LiveDeviceTests.cpp:TestPcapLiveDeviceBlockingModenow also runs against adisableImmediateMode = trueconfig.