Linux Packet Timestamping Playbook (SO_TIMESTAMPING + PTP, Practical)

2026-03-19 · software

Linux Packet Timestamping Playbook (SO_TIMESTAMPING + PTP, Practical)

Date: 2026-03-19
Category: knowledge

Why this matters

If you care about microsecond-level latency (execution gateways, market-data collectors, telemetry ingest), you need to separate:

Without accurate packet timestamps, these collapse into one noisy number and you can’t debug what really got slower.


1) Clock model first (before socket code)

Treat timestamps as a clock-discipline problem first, API problem second.

You usually have:

Practical rule:

  1. Discipline PHC and system clock (e.g., ptp4l + phc2sys).
  2. Prefer CLOCK_TAI for monotonic-ish wall timeline without leap-second surprise.
  3. Tag every metric with clock domain so you never mix apples and oranges.

2) Capability check: confirm NIC/driver supports what you need

Before implementation, verify hardware capabilities:

Many outages start with: “code expects HW TX timestamps, NIC silently gives SW timestamps.”


3) Pick timestamping mode by use case

Mode A: software timestamps only (baseline)

Mode B: RX hardware timestamps

Mode C: TX hardware timestamps

Use software timestamps as fallback telemetry, not as primary truth for low-latency claims.


4) Socket API essentials (SO_TIMESTAMPING)

Enable timestamping with SO_TIMESTAMPING and relevant SOF_TIMESTAMPING_* flags (hardware/software, RX/TX, raw hardware).

Operational essentials:

  1. TX timestamps arrive on error queue → use recvmsg(..., MSG_ERRQUEUE) path.
  2. Parse control messages (SCM_TIMESTAMPING) safely.
  3. Preserve packet correlation (e.g., ID/sequence) so timestamp can be mapped to order/request.

If your send path is async/batched, correlation IDs are mandatory or your timing data becomes unusable.


5) PTP discipline: keep clocks trustworthy

Typical production pattern:

Track continuously:

Any serious timestamping dashboard should show sync health next to latency metrics.


6) Common failure modes

  1. Clock-domain mixing

    • comparing hardware timestamps with app CLOCK_REALTIME without normalization.
  2. Silent fallback to software timestamps

    • capability drift after driver/firmware/kernel update.
  3. Ignoring TX error queue consumption

    • timestamps drop under load, leading to biased samples.
  4. Offload interactions misunderstood

    • GRO/LRO/TSO/GSO can distort packet-level assumptions.
  5. Queue/CPU affinity drift

    • packet path migrates across cores, injecting jitter unrelated to network.
  6. No leap-second policy

    • CLOCK_REALTIME adjustments create phantom spikes if not handled carefully.

7) Observability schema (minimum)

Per packet/order (sampled if needed):

Aggregate metrics:

Define SLOs separately for:


8) Rollout plan (safe)

  1. Lab validation
    • verify capability matrix per NIC model + driver version.
  2. Shadow mode
    • collect timestamp telemetry without changing routing/execution decisions.
  3. Drift alarms
    • alert on PHC/system offset and timestamp fallback rate.
  4. Guarded activation
    • enable decision logic that depends on timestamps only after data quality gates pass.
  5. Regression gate
    • block kernel/driver upgrades unless timestamp integrity tests pass.

9) Practical checklist


References