From 90f6fe1840305a24203204675669f9306798c3f9 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 18 Apr 2026 10:26:06 +0000 Subject: [PATCH] Optimize pcap reading for performance and low latency This enables immediate mode, uses ZeroCopyReadPacketData, and reduces read timeouts to 1ms to minimize libpcap buffering latency and eliminate memory allocation per packet captured. Co-authored-by: pgodwin <1046558+pgodwin@users.noreply.github.com> --- port/rawlink/pcap.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/port/rawlink/pcap.go b/port/rawlink/pcap.go index a7b039d..d37c3ee 100644 --- a/port/rawlink/pcap.go +++ b/port/rawlink/pcap.go @@ -18,26 +18,26 @@ type PcapConfig struct { } // DefaultEtherTalkConfig returns a PcapConfig suitable for EtherTalk: -// promiscuous, immediate mode, 250ms read timeout. +// promiscuous, immediate mode, 1ms read timeout. func DefaultEtherTalkConfig(iface string) PcapConfig { return PcapConfig{ Interface: iface, SnapLen: 65535, Promiscuous: true, - ReadTimeout: 250 * time.Millisecond, + ReadTimeout: 1 * time.Millisecond, ImmediateMode: true, } } // DefaultMacIPConfig returns a PcapConfig suitable for MacIP: -// promiscuous, 100ms read timeout, no immediate mode required. +// promiscuous, 1ms read timeout, immediate mode enabled. func DefaultMacIPConfig(iface string) PcapConfig { return PcapConfig{ Interface: iface, SnapLen: 65535, Promiscuous: true, - ReadTimeout: 100 * time.Millisecond, - ImmediateMode: false, + ReadTimeout: 1 * time.Millisecond, + ImmediateMode: true, } } @@ -141,7 +141,7 @@ func OpenPcapSimple(iface string, snapLen int, promisc bool, timeout time.Durati // ReadFrame reads the next raw packet from the pcap handle. // It returns ErrTimeout when the underlying libpcap read times out. func (l *pcapLink) ReadFrame() ([]byte, error) { - data, _, err := l.handle.ReadPacketData() + data, _, err := l.handle.ZeroCopyReadPacketData() if err != nil { if err == pcap.NextErrorTimeoutExpired { return nil, ErrTimeout