DPDK-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>,
	Reshma Pattan <reshma.pattan@intel.com>
Subject: [PATCH 6/7] test/pcapng: compare timestamps in one clock domain
Date: Tue,  1 Dec 2026 14:41:39 -0800	[thread overview]
Message-ID: <20261201224258.1962108-7-stephen@networkplumber.org> (raw)
In-Reply-To: <20261201224258.1962108-1-stephen@networkplumber.org>

The pcapng test bounded the packet timestamps it read back using
clock_gettime(CLOCK_REALTIME) sampled around the capture, but the
timestamps in the file come from the TSC converted with the reference
point EAL records at init. Comparing the two measures the drift
between the calibrated TSC rate and NTP's slewing of the wall clock,
which grows with process uptime. Packets were reported a few tens of
microseconds before the start of the window.

Bound the window with rte_tsc_to_ns() so both sides of the comparison
come from the same reference point.

What matters for analysis is the time between packets rather than the
absolute offset, so also check that the timestamps never go backwards.
A reference point that moved during the capture would show up as a
negative delta, which no test covered before.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test/test_pcapng.c | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

diff --git a/app/test/test_pcapng.c b/app/test/test_pcapng.c
index 7e19aa4096..0cf5d469e5 100644
--- a/app/test/test_pcapng.c
+++ b/app/test/test_pcapng.c
@@ -350,6 +350,7 @@ struct pkt_print_ctx {
 	unsigned int count;
 	uint64_t start_ns;
 	uint64_t end_ns;
+	uint64_t prev_ns;
 };
 
 static void
@@ -398,6 +399,19 @@ parse_pcap_packet(u_char *user, const struct pcap_pkthdr *h,
 		goto error;
 	}
 
+	/*
+	 * Packets are stamped from the TSC and written in order, so the
+	 * time between them must never go backwards. A reference point
+	 * that moved mid-capture, for example by following a wall clock
+	 * step, would show up here as a negative delta.
+	 */
+	if (ns < ctx->prev_ns) {
+		printf("Timestamp went backwards by %"PRIu64" ns\n",
+		       ctx->prev_ns - ns);
+		goto error;
+	}
+	ctx->prev_ns = ns;
+
 	if (!rte_is_broadcast_ether_addr(&eh->dst_addr)) {
 		printf("Destination is not broadcast\n");
 		goto error;
@@ -441,8 +455,16 @@ valid_pcapng_file(const char *file_name, uint64_t started, unsigned int expected
 	struct pkt_print_ctx ctx = { };
 	int ret;
 
+	/*
+	 * Packet timestamps come from the TSC converted with the reference
+	 * point EAL took at init. Comparing them against CLOCK_REALTIME
+	 * read now would measure the drift between the calibrated TSC rate
+	 * and NTP's slewing of the wall clock, so bound the window using
+	 * the same conversion the library uses.
+	 */
 	ctx.start_ns = started;
-	ctx.end_ns = current_timestamp() + NS_PER_S;
+	ctx.end_ns = rte_tsc_to_ns(rte_get_tsc_cycles()) + NS_PER_S;
+	ctx.prev_ns = started;
 
 	ctx.pcap = pcap_open_offline_with_tstamp_precision(file_name,
 							   PCAP_TSTAMP_PRECISION_NANO,
@@ -474,7 +496,7 @@ test_add_interface(void)
 	char file_name[PATH_MAX] = "/tmp/pcapng_test_XXXXXX.pcapng";
 	static rte_pcapng_t *pcapng;
 	int ret, tmp_fd;
-	uint64_t now = current_timestamp();
+	uint64_t now = rte_tsc_to_ns(rte_get_tsc_cycles());
 
 	tmp_fd = mkstemps(file_name, strlen(".pcapng"));
 	if (tmp_fd == -1) {
@@ -535,7 +557,7 @@ test_write_packets(void)
 	char file_name[PATH_MAX] = "/tmp/pcapng_test_XXXXXX.pcapng";
 	rte_pcapng_t *pcapng = NULL;
 	int ret, tmp_fd, count;
-	uint64_t now = current_timestamp();
+	uint64_t now = rte_tsc_to_ns(rte_get_tsc_cycles());
 
 	tmp_fd = mkstemps(file_name, strlen(".pcapng"));
 	if (tmp_fd == -1) {
@@ -625,7 +647,7 @@ test_write_before_open(void)
 	/* Small delay so the capture start is measurably after the copies */
 	rte_delay_us_block(100);
 
-	now = current_timestamp();
+	now = rte_tsc_to_ns(rte_get_tsc_cycles());
 
 	tmp_fd = mkstemps(file_name, strlen(".pcapng"));
 	if (tmp_fd == -1) {
-- 
2.53.0


  parent reply	other threads:[~2026-12-01 22:43 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 22:41 [PATCH 0/7] pcapng: fixes, timestamps and stdio output Stephen Hemminger
2026-12-01 22:41 ` [PATCH 1/7] graph: fix double close of pcapng file Stephen Hemminger
2026-12-01 22:41 ` [PATCH 2/7] pcapng: call writev as needed on highly segmented mbuf Stephen Hemminger
2026-12-01 22:41 ` [PATCH 3/7] eal: add function to convert TSC to epoch time Stephen Hemminger
2026-12-01 22:41 ` [PATCH 4/7] pcapng: use EAL TSC to epoch conversion Stephen Hemminger
2026-12-01 22:41 ` [PATCH 5/7] pcapng: add user-supplied timestamp support Stephen Hemminger
2026-12-01 22:41 ` Stephen Hemminger [this message]
2026-12-01 22:41 ` [PATCH 7/7] pcapng: use stdio for output Stephen Hemminger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201224258.1962108-7-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=dev@dpdk.org \
    --cc=reshma.pattan@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox