[PATCH v3 1/1] pcapng: add user-supplied timestamp support

Morten Brørup mb at smartsharesystems.com
Thu Jun 18 18:45:19 CEST 2026


> From: Stephen Hemminger [mailto:stephen at networkplumber.org]
> Sent: Thursday, 18 June 2026 17.23
> 
> On Thu, 18 Jun 2026 10:38:15 -0400
> Dawid Wesierski <dawid.wesierski at intel.com> wrote:
> 
> > + * @param ts
> > + *   Packet timestamp in nanoseconds since the Unix epoch. If zero,
> the
> > + *   current TSC is captured and converted to epoch ns by
> > + *   rte_pcapng_write_packets() when the packet is written.
> >   *
> 
> It might help users if a helper rte_tsc_to_epoch() was exposed.

+1

Please note that such a helper would need to compensate for rte_rdtsc() drift.

Simplified:

int64_t rte_tsc_to_ns(tsc)
{
  struct timespec ts = clock_gettime(CLOCK_REALTIME);
  int64_t now_ns = ts.tv_sec * NS_PER_S + ts.tv_nsec;
  int64_t now_tsc = rte_rdtsc();
  int64_t diff_tsc = tsc - tsc_now;
  return now_ns + diff_tsc * NS_PER_S / rte_get_tsc_hz();
}

A performance optimized version would take "now_ns" and "now_tsc" as parameters.
And with "now_ns" passed as a parameter, the function also works with other clocks, such as CLOCK_MONOTONIC.

Also see this discussion on the Grout mailing list:
https://inbox.dpdk.org/grout/98CBD80474FA8B44BF855DF32C47DC35F6590E@smartserver.smartshare.dk/T/#m465a04ca2e8219612dd9c3efb4198d23d5813422




More information about the dev mailing list