When to Use
Use this skill when a co-located feed handler records tick arrival times that must
reflect when the packet hit the wire, not when Python got around to reading it.
An application-level time.time() reading includes NIC-to-kernel DMA, softirq
scheduling, socket-buffer queueing and the interpreter's own context switches.
Linux exposes earlier capture points through SO_TIMESTAMPING, and this skill
covers decoding them correctly.
The one thing this skill exists to get right is which capture point you actually
received. Three different SOL_SOCKET control messages carry a timestamp, two of
them are byte-identical in size on a 64-bit host, and only one of them can ever
contain a hardware timestamp:
cmsg_type |
Enabled by |
Payload |
Size (LP64 / ILP32) |
Capture point |
SCM_TIMESTAMP (29 / 63) |
SO_TIMESTAMP |
struct __kernel_old_timeval |
16 / 8 |
Kernel receive path, microseconds |
SCM_TIMESTAMPNS (35 / 64) |
SO_TIMESTAMPNS |
struct timespec |
16 / 8 |
Kernel receive path, nanoseconds |
SCM_TIMESTAMPING (37 / 65) |
SO_TIMESTAMPING |
struct scm_timestamping (timespec[3]) |
48 / 24 |
ts[0] software, ts[1] deprecated, ts[2] hardware |
Classifying these by buffer length instead of cmsg_type is the classic failure:
a 16-byte payload is a software timestamp either way, and a 48-byte payload decoded
from offset 0 yields ts[0] — the kernel software timestamp — which then gets
recorded and reported as a wire timestamp.
When NOT to Use
- On any non-Linux host.
SO_TIMESTAMPING is a Linux socket option. There is no
portable equivalent, and the engine returns False from enable_nic_timestamping
rather than pretending otherwise.
- To subtract a hardware timestamp from a system-clock reading, without a
disciplined PHC. The kernel does not convert hardware timestamps to system time;
ts[2] is read from the adapter's PTP hardware clock, an independent clock. Until
phc2sys/sfptpd ties it to CLOCK_REALTIME, T_app − T_hw is a clock offset
plus a capture delay, and routinely goes negative. See
clock-synchronization-ptp-for-trading-hosts.
- As evidence of MiFID II RTS 25 clock compliance. Decoding a timestamp is not
auditing one. Use
hardware-timestamping-vs-software-timestamping-accuracy for the
divergence/granularity audit and the documented timestamping point.
- As a substitute for a latency budget. Wire-to-decision decomposition across the
whole path is
tick-to-trade-latency-measurement and
strategy-latency-budget-decomposition.
- With a kernel-bypass stack that never touches a Linux socket. Onload's
onload_timestamping and equivalent bypass APIs deliver timestamps through the
vendor library, not through recvmsg ancillary data; the layout facts here still
apply, the socket plumbing does not.
Prerequisites
- Linux host with an adapter whose driver supports receive hardware timestamping.
Verify before trusting any
HARDWARE_NIC label: ethtool -T <iface> must list
hardware-receive / SOF_TIMESTAMPING_RX_HARDWARE in its capabilities. A driver
without it still returns a populated SCM_TIMESTAMPING message — the hardware slot
is simply zero.
ptp4l disciplining the PHC to a grandmaster, and phc2sys (or sfptpd) relating
the PHC to the system clock, if any cross-layer subtraction is to mean anything.
socket.recvmsg with an ancillary buffer sized via
socket.CMSG_SPACE(48) — struct scm_timestamping is 48 bytes on LP64, and an
undersized ancbufsize truncates the control message rather than failing loudly.
- CPython's
socket module exports none of SO_TIMESTAMPING, SCM_TIMESTAMPING or
the SOF_TIMESTAMPING_* flags. The numeric constants must be supplied by the
caller; hasattr(socket, "SO_TIMESTAMPNS") is False on every platform and must
never be used to gate activation.
Workflow
Confirm the adapter can do it before enabling anything. Run
ethtool -T <iface>. Decision point: if hardware-receive is absent, do not ship
a config that claims hardware timestamps — either fix the driver/NIC or set
use_hardware_timestamping=False and record kernel software timestamps under
their real name.
Enable SO_TIMESTAMPING, not SO_TIMESTAMPNS.
engine = NICHardwareTimestamperEngine(use_hardware_timestamping=True)
if not engine.enable_nic_timestamping(sock):
raise RuntimeError("hardware timestamping unavailable — do not start the feed")
The engine sets SOF_TIMESTAMPING_RX_HARDWARE | SOF_TIMESTAMPING_RAW_HARDWARE
(0x44). Decision point: never additionally enable SO_TIMESTAMP or
SO_TIMESTAMPNS on the same socket — with SOF_TIMESTAMPING_SOFTWARE set, the
kernel fabricates a substitute software timestamp into ts[0] when a real one is
missing, and it is indistinguishable from a genuine capture.
Receive with an adequately sized control buffer.
payload, ancdata, flags, addr = sock.recvmsg(65535, socket.CMSG_SPACE(48))
Decode by cmsg_type, and take the hardware timestamp from ts[2].
pkt = engine.process_packet_with_nic_timestamp(payload, ancdata, time.time_ns())
Precedence is hardware (ts[2]) → kernel nanosecond → kernel microsecond →
application clock. All control messages are scanned before one is chosen;
recvmsg does not guarantee the hardware message arrives first. An all-zero
ts[2] means the adapter did not stamp this packet, not that it stamped it at
the epoch.
Treat degradation as an incident, not a log line. Decision point: if
pkt.degraded is set, hardware timestamping was required and did not arrive —
the tick stream's stated accuracy is no longer true. Alert; do not silently
continue writing ticks that claim wire accuracy.
Read capture_delay_ns as signed, and know which timebase it crosses. When
pkt.cross_timebase_comparison is True the figure mixes the PHC and
CLOCK_REALTIME. A negative value is not a fast packet — it is an undisciplined
PHC. Fix the clock before reading anything else from that batch.
Full procedure: see references/workflows.md.
Standards reference: see references/standards.md.
Printable pre-flight checklist: see assets/checklist.md.
Common Pitfalls
- Classifying the timestamp by control-buffer length. On LP64 both
SCM_TIMESTAMPNS (timespec) and SCM_TIMESTAMP (timeval) are exactly 16
bytes. Decoding a timeval as a timespec reads tv_usec as nanoseconds and
under-reports the sub-second component by a factor of 1000 — half a second becomes
half a millisecond — while a size test also labels both as "hardware".
- Reading
ts[0] out of struct scm_timestamping and calling it hardware.
ts[0] is the software timestamp; the hardware one is ts[2]. ts[1] held
hardware-converted-to-system-time and is deprecated — do not resurrect it.
- Gating on
hasattr(socket, "SO_TIMESTAMPNS"). CPython never defines it, so the
guard is always false and timestamping is silently never enabled. The failure looks
exactly like a working system that simply never sees hardware timestamps.
- Enabling
SO_TIMESTAMP/SO_TIMESTAMPNS alongside SO_TIMESTAMPING. With
SOF_TIMESTAMPING_SOFTWARE requested, the kernel generates a false software
timestamp in ts[0] during recvmsg() when a real one is missing.
- Clamping the capture delay at zero.
max(0.0, T_app − T_hw) turns the single
clearest symptom of an undisciplined PHC into a healthy-looking 0.0 and hides the
PTP fault the rest of this skill is trying to surface.
- Reading only the negative case as a clock fault. A PHC that was never set counts
from zero at boot, so
T_app − T_hw comes back as a positive delay of roughly the
current epoch — tens of years — rather than a negative one. Both directions are the
same fault. Sanity-check the magnitude of capture_delay_ns whenever
cross_timebase_comparison is True, using a bound derived from your own measured
capture path rather than a number copied from a datasheet.
- Carrying a nanosecond epoch in a float.
int(time.time() * 1e9) at a ~1.78e18
ns epoch has 256 ns of binary64 spacing — coarser than the effect being measured.
Use time.time_ns() and integers end to end.
- Trusting a populated
SCM_TIMESTAMPING message as proof of hardware capture.
The message is delivered whether or not the adapter stamped the packet. Confirm
with ethtool -T and check that ts[2] is non-zero.
- Sizing
ancbufsize for a 16-byte timespec. struct scm_timestamping needs
CMSG_SPACE(48) on LP64; a smaller buffer truncates the control message and the
hardware slot is the part that gets cut.
- Assuming the constants are portable across architectures.
SO_TIMESTAMPING_OLD
is 37 under asm-generic (x86-64, arm64, mips, alpha) but 0x0023 on sparc and
0x4020 on parisc. Pass the overrides rather than hard-coding.
Verification
- Build a
struct scm_timestamping with ts[0] at +900,000 ns and ts[2] at
+400,000 ns and assert the selected timestamp_ns is the +400,000 value with
source == HARDWARE_NIC. A decoder reading offset 0 returns +900,000 here.
- Feed a 16-byte
SCM_TIMESTAMPNS payload and assert source == KERNEL_SOFTWARE,
not HARDWARE_NIC.
- Feed a 16-byte
SCM_TIMESTAMP payload with tv_usec = 500_000 and assert the
decoded value is base + 500_000_000 ns, not base + 500_000 ns.
- Present
SCM_TIMESTAMPNS ahead of SCM_TIMESTAMPING in the ancillary list and
assert the hardware timestamp still wins.
- Set
ts[2] 250 µs ahead of the application timestamp and assert
capture_delay_ns == -250_000 and cross_timebase_comparison is True — the value
must be signed, not clamped.
- Assert an all-zero
ts[2] yields KERNEL_SOFTWARE with degraded is True, and a
32-byte (truncated) SCM_TIMESTAMPING payload yields APPLICATION_FALLBACK
rather than a misdecoded number.
- Assert
tv_nsec == 1_000_000_000 is rejected, a float epoch raises TypeError,
and a negative epoch raises ValueError.
- Assert
timestamping_flags() is 68 (1<<2 | 1<<6) when hardware is required and
92 when software fallback is allowed, and that the default option number is 37,
not 35.
- Run
python -m unittest discover -s skills/network-interface-level-tick-timestamping/scripts.
Related Skills
clock-synchronization-ptp-for-trading-hosts
hardware-timestamping-vs-software-timestamping-accuracy
clock-skew-correction-for-tick-timestamps
tick-to-trade-latency-measurement
binary-protocol-parsing-for-low-latency-feeds
feed-handler-cpu-pinning-and-numa-awareness
1---2name: network-interface-level-tick-timestamping3description: >-4 Use when a Linux feed handler must stamp ticks at wire arrival rather than in the5 application. Decodes SO_TIMESTAMPING ancillary messages and reads the NIC hardware6 stamp without mistaking a kernel software timestamp for it.7license: Apache-2.08metadata:9 source: https://skillmd.com/skills/himanshuj16/network-interface-level-tick-timestamping10 domain: algorithmic-trading11 subdomain: real-time-architecture12 tags: real-time-architecture, nic-timestamping, so-timestamping, scm-timestamping, ptp-hardware-clock, solarflare, low-latency13 brokers_frameworks: "Linux SO_TIMESTAMPING (SCM_TIMESTAMPING); PTP hardware clock (ptp4l / phc2sys); Solarflare/AMD OpenOnload & sfptpd; Python socket.recvmsg"14 version: "2.0.0"15 author: algo-trading-skills-contributors16---1718## When to Use1920Use this skill when a co-located feed handler records tick arrival times that must21reflect when the packet hit the wire, not when Python got around to reading it.22An application-level `time.time()` reading includes NIC-to-kernel DMA, softirq23scheduling, socket-buffer queueing and the interpreter's own context switches.24Linux exposes earlier capture points through `SO_TIMESTAMPING`, and this skill25covers decoding them correctly.2627The one thing this skill exists to get right is **which capture point you actually28received**. Three different `SOL_SOCKET` control messages carry a timestamp, two of29them are byte-identical in size on a 64-bit host, and only one of them can ever30contain a hardware timestamp:3132| `cmsg_type` | Enabled by | Payload | Size (LP64 / ILP32) | Capture point |33|---|---|---|---|---|34| `SCM_TIMESTAMP` (29 / 63) | `SO_TIMESTAMP` | `struct __kernel_old_timeval` | 16 / 8 | Kernel receive path, **microseconds** |35| `SCM_TIMESTAMPNS` (35 / 64) | `SO_TIMESTAMPNS` | `struct timespec` | 16 / 8 | Kernel receive path, nanoseconds |36| `SCM_TIMESTAMPING` (37 / 65) | `SO_TIMESTAMPING` | `struct scm_timestamping` (`timespec[3]`) | 48 / 24 | `ts[0]` software, `ts[1]` deprecated, **`ts[2]` hardware** |3738Classifying these by buffer length instead of `cmsg_type` is the classic failure:39a 16-byte payload is a software timestamp either way, and a 48-byte payload decoded40from offset 0 yields `ts[0]` — the kernel software timestamp — which then gets41recorded and reported as a wire timestamp.4243## When NOT to Use4445- **On any non-Linux host.** `SO_TIMESTAMPING` is a Linux socket option. There is no46 portable equivalent, and the engine returns `False` from `enable_nic_timestamping`47 rather than pretending otherwise.48- **To subtract a hardware timestamp from a system-clock reading, without a49 disciplined PHC.** The kernel does not convert hardware timestamps to system time;50 `ts[2]` is read from the adapter's PTP hardware clock, an independent clock. Until51 `phc2sys`/`sfptpd` ties it to `CLOCK_REALTIME`, `T_app − T_hw` is a clock offset52 plus a capture delay, and routinely goes negative. See53 `clock-synchronization-ptp-for-trading-hosts`.54- **As evidence of MiFID II RTS 25 clock compliance.** Decoding a timestamp is not55 auditing one. Use `hardware-timestamping-vs-software-timestamping-accuracy` for the56 divergence/granularity audit and the documented timestamping point.57- **As a substitute for a latency budget.** Wire-to-decision decomposition across the58 whole path is `tick-to-trade-latency-measurement` and59 `strategy-latency-budget-decomposition`.60- **With a kernel-bypass stack that never touches a Linux socket.** Onload's61 `onload_timestamping` and equivalent bypass APIs deliver timestamps through the62 vendor library, not through `recvmsg` ancillary data; the layout facts here still63 apply, the socket plumbing does not.6465## Prerequisites6667- Linux host with an adapter whose driver supports receive hardware timestamping.68 **Verify before trusting any `HARDWARE_NIC` label**: `ethtool -T <iface>` must list69 `hardware-receive` / `SOF_TIMESTAMPING_RX_HARDWARE` in its capabilities. A driver70 without it still returns a populated `SCM_TIMESTAMPING` message — the hardware slot71 is simply zero.72- `ptp4l` disciplining the PHC to a grandmaster, and `phc2sys` (or `sfptpd`) relating73 the PHC to the system clock, if any cross-layer subtraction is to mean anything.74- `socket.recvmsg` with an ancillary buffer sized via75 `socket.CMSG_SPACE(48)` — `struct scm_timestamping` is 48 bytes on LP64, and an76 undersized `ancbufsize` truncates the control message rather than failing loudly.77- CPython's `socket` module exports none of `SO_TIMESTAMPING`, `SCM_TIMESTAMPING` or78 the `SOF_TIMESTAMPING_*` flags. The numeric constants must be supplied by the79 caller; `hasattr(socket, "SO_TIMESTAMPNS")` is `False` on every platform and must80 never be used to gate activation.8182## Workflow83841. **Confirm the adapter can do it before enabling anything.** Run85 `ethtool -T <iface>`. Decision point: if `hardware-receive` is absent, do not ship86 a config that claims hardware timestamps — either fix the driver/NIC or set87 `use_hardware_timestamping=False` and record kernel software timestamps under88 their real name.89902. **Enable `SO_TIMESTAMPING`, not `SO_TIMESTAMPNS`.**91 ```python92 engine = NICHardwareTimestamperEngine(use_hardware_timestamping=True)93 if not engine.enable_nic_timestamping(sock):94 raise RuntimeError("hardware timestamping unavailable — do not start the feed")95 ```96 The engine sets `SOF_TIMESTAMPING_RX_HARDWARE | SOF_TIMESTAMPING_RAW_HARDWARE`97 (`0x44`). Decision point: never additionally enable `SO_TIMESTAMP` or98 `SO_TIMESTAMPNS` on the same socket — with `SOF_TIMESTAMPING_SOFTWARE` set, the99 kernel fabricates a substitute software timestamp into `ts[0]` when a real one is100 missing, and it is indistinguishable from a genuine capture.1011023. **Receive with an adequately sized control buffer.**103 ```python104 payload, ancdata, flags, addr = sock.recvmsg(65535, socket.CMSG_SPACE(48))105 ```1061074. **Decode by `cmsg_type`, and take the hardware timestamp from `ts[2]`.**108 ```python109 pkt = engine.process_packet_with_nic_timestamp(payload, ancdata, time.time_ns())110 ```111 Precedence is hardware (`ts[2]`) → kernel nanosecond → kernel microsecond →112 application clock. All control messages are scanned before one is chosen;113 `recvmsg` does not guarantee the hardware message arrives first. An all-zero114 `ts[2]` means the adapter did not stamp this packet, not that it stamped it at115 the epoch.1161175. **Treat degradation as an incident, not a log line.** Decision point: if118 `pkt.degraded` is set, hardware timestamping was required and did not arrive —119 the tick stream's stated accuracy is no longer true. Alert; do not silently120 continue writing ticks that claim wire accuracy.1211226. **Read `capture_delay_ns` as signed, and know which timebase it crosses.** When123 `pkt.cross_timebase_comparison` is `True` the figure mixes the PHC and124 `CLOCK_REALTIME`. A negative value is not a fast packet — it is an undisciplined125 PHC. Fix the clock before reading anything else from that batch.126127> Full procedure: see `references/workflows.md`.128> Standards reference: see `references/standards.md`.129> Printable pre-flight checklist: see `assets/checklist.md`.130131## Common Pitfalls132133- **Classifying the timestamp by control-buffer length.** On LP64 both134 `SCM_TIMESTAMPNS` (`timespec`) and `SCM_TIMESTAMP` (`timeval`) are exactly 16135 bytes. Decoding a `timeval` as a `timespec` reads `tv_usec` as nanoseconds and136 under-reports the sub-second component by a factor of 1000 — half a second becomes137 half a millisecond — while a size test also labels both as "hardware".138- **Reading `ts[0]` out of `struct scm_timestamping` and calling it hardware.**139 `ts[0]` is the software timestamp; the hardware one is `ts[2]`. `ts[1]` held140 hardware-converted-to-system-time and is deprecated — do not resurrect it.141- **Gating on `hasattr(socket, "SO_TIMESTAMPNS")`.** CPython never defines it, so the142 guard is always false and timestamping is silently never enabled. The failure looks143 exactly like a working system that simply never sees hardware timestamps.144- **Enabling `SO_TIMESTAMP`/`SO_TIMESTAMPNS` alongside `SO_TIMESTAMPING`.** With145 `SOF_TIMESTAMPING_SOFTWARE` requested, the kernel generates a false software146 timestamp in `ts[0]` during `recvmsg()` when a real one is missing.147- **Clamping the capture delay at zero.** `max(0.0, T_app − T_hw)` turns the single148 clearest symptom of an undisciplined PHC into a healthy-looking `0.0` and hides the149 PTP fault the rest of this skill is trying to surface.150- **Reading only the negative case as a clock fault.** A PHC that was never set counts151 from zero at boot, so `T_app − T_hw` comes back as a *positive* delay of roughly the152 current epoch — tens of years — rather than a negative one. Both directions are the153 same fault. Sanity-check the magnitude of `capture_delay_ns` whenever154 `cross_timebase_comparison` is `True`, using a bound derived from your own measured155 capture path rather than a number copied from a datasheet.156- **Carrying a nanosecond epoch in a float.** `int(time.time() * 1e9)` at a ~1.78e18157 ns epoch has 256 ns of binary64 spacing — coarser than the effect being measured.158 Use `time.time_ns()` and integers end to end.159- **Trusting a populated `SCM_TIMESTAMPING` message as proof of hardware capture.**160 The message is delivered whether or not the adapter stamped the packet. Confirm161 with `ethtool -T` and check that `ts[2]` is non-zero.162- **Sizing `ancbufsize` for a 16-byte timespec.** `struct scm_timestamping` needs163 `CMSG_SPACE(48)` on LP64; a smaller buffer truncates the control message and the164 hardware slot is the part that gets cut.165- **Assuming the constants are portable across architectures.** `SO_TIMESTAMPING_OLD`166 is 37 under `asm-generic` (x86-64, arm64, mips, alpha) but `0x0023` on sparc and167 `0x4020` on parisc. Pass the overrides rather than hard-coding.168169## Verification170171- Build a `struct scm_timestamping` with `ts[0]` at +900,000 ns and `ts[2]` at172 +400,000 ns and assert the selected `timestamp_ns` is the **+400,000** value with173 `source == HARDWARE_NIC`. A decoder reading offset 0 returns +900,000 here.174- Feed a 16-byte `SCM_TIMESTAMPNS` payload and assert `source == KERNEL_SOFTWARE`,175 not `HARDWARE_NIC`.176- Feed a 16-byte `SCM_TIMESTAMP` payload with `tv_usec = 500_000` and assert the177 decoded value is `base + 500_000_000` ns, not `base + 500_000` ns.178- Present `SCM_TIMESTAMPNS` ahead of `SCM_TIMESTAMPING` in the ancillary list and179 assert the hardware timestamp still wins.180- Set `ts[2]` 250 µs ahead of the application timestamp and assert181 `capture_delay_ns == -250_000` and `cross_timebase_comparison is True` — the value182 must be signed, not clamped.183- Assert an all-zero `ts[2]` yields `KERNEL_SOFTWARE` with `degraded is True`, and a184 32-byte (truncated) `SCM_TIMESTAMPING` payload yields `APPLICATION_FALLBACK`185 rather than a misdecoded number.186- Assert `tv_nsec == 1_000_000_000` is rejected, a float epoch raises `TypeError`,187 and a negative epoch raises `ValueError`.188- Assert `timestamping_flags()` is `68` (`1<<2 | 1<<6`) when hardware is required and189 `92` when software fallback is allowed, and that the default option number is 37,190 not 35.191- Run `python -m unittest discover -s skills/network-interface-level-tick-timestamping/scripts`.192193## Related Skills194195- `clock-synchronization-ptp-for-trading-hosts`196- `hardware-timestamping-vs-software-timestamping-accuracy`197- `clock-skew-correction-for-tick-timestamps`198- `tick-to-trade-latency-measurement`199- `binary-protocol-parsing-for-low-latency-feeds`200- `feed-handler-cpu-pinning-and-numa-awareness`