Skip to content

Add context-managed realtime packet session with RT-kernel mode#39

Draft
polybassa with Copilot wants to merge 2 commits into
masterfrom
copilot/propose-real-time-mode-for-scapy
Draft

Add context-managed realtime packet session with RT-kernel mode#39
polybassa with Copilot wants to merge 2 commits into
masterfrom
copilot/propose-real-time-mode-for-scapy

Conversation

Copilot AI commented May 27, 2026

Copy link
Copy Markdown

This introduces a reusable realtime send/receive session API for Scapy and adds an explicit rt_kernel=True mode for lower-jitter, deterministic I/O behavior on real-time kernels. The goal is to support interactive packet workflows with clear socket lifecycle, configurable latency controls, and RT-safe integration points.

  • API: context-managed realtime session

    • Added RealtimeSession and realtime_session(...) in scapy/sendrecv.py.
    • Exposes session-scoped send(), recv(timeout=...), and sendrecv(...).
    • Keeps one socket open across operations and guarantees cleanup on context exit.
  • Execution model split by mode

    • Default mode: background receive thread + bounded queue for asynchronous consumption.
    • rt_kernel=True: single-threaded blocking receive path on the same socket to reduce scheduler jitter.
  • RT tuning + integration hooks

    • Added optional knobs: socket_rcvbuf, socket_sndbuf, poll_interval, queue_size, strict_drop.
    • Added setup_callback / teardown_callback hooks so callers can apply external process-level RT setup (e.g., scheduler/affinity) outside Scapy core.
    • Preserves kernel-derived packet timestamps when provided by socket paths; only falls back when absent.
  • Docs: realtime-kernel caveats

    • Added usage section in doc/scapy/usage.rst covering API usage, mode behavior, tuning options, and RT operational caveats (privileges, starvation risk, latency-vs-completeness tradeoff).
  • Regression coverage

    • Added test/regression.uts scenarios for:
      • timeout behavior in RT mode,
      • timestamp ordering,
      • background-thread shutdown semantics,
      • strict-drop freshness policy,
      • session lifecycle (send() requires entered context) and sendrecv() flow.
from scapy.sendrecv import realtime_session

with realtime_session(iface="eth0", rt_kernel=True, poll_interval=0.001) as rt:
    rt.send(pkt)
    ans = rt.recv(timeout=0.05)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants