DPDK patches and discussions
 help / color / mirror / Atom feed
From: Quentin Armitage <quentin@armitage.org.uk>
To: Reshma Pattan <reshma.pattan@intel.com>,
	Stephen Hemminger <stephen@networkplumber.org>,
	Ray Kinsella <mdr@ashroe.eu>
Cc: dev@dpdk.org, Quentin Armitage <quentin@armitage.org.uk>,
	stable@dpdk.org
Subject: [PATCH] libpcapng: fix timestamp wrapping in output files
Date: Sat,  7 May 2022 17:12:36 +0100	[thread overview]
Message-ID: <20220507161237.207805-1-quentin@armitage.org.uk> (raw)

In pcap_tsc_to_ns(), delta * NSEC_PER_SEC will overflow approx 8
seconds after pcap_init is called when using a TSC with a frequency
of 2.5GHz.

To avoid the overflow, reread the time and TSC once
delta * NSEC_PER_SEC > (1 << 63). In order to ensure that there
is no overflow if there is a several second gap between calls to
pcapng_tsc_to_ns() the actual check to reread the clock is:
  delta > ((1ULL << 63) / NSEC_PER_SEC)

Fixes: 8d23ce8f5ee ("pcapng: add new library for writing pcapng files")
Cc: stable@dpdk.org

Signed-off-by: Quentin Armitage <quentin@armitage.org.uk>
---
 lib/pcapng/rte_pcapng.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/lib/pcapng/rte_pcapng.c b/lib/pcapng/rte_pcapng.c
index 90b2f5bc69..7770be725f 100644
--- a/lib/pcapng/rte_pcapng.c
+++ b/lib/pcapng/rte_pcapng.c
@@ -34,7 +34,7 @@ struct rte_pcapng {
 };
 
 /* For converting TSC cycles to PCAPNG ns format */
-struct pcapng_time {
+static struct pcapng_time {
 	uint64_t ns;
 	uint64_t cycles;
 } pcapng_time;
@@ -53,7 +53,21 @@ static uint64_t pcapng_tsc_to_ns(uint64_t cycles)
 {
 	uint64_t delta;
 
+	/* With a TSC frequency of 2.5GHz, delta * NSEC_PER_SEC will
+	 * wrap in under 8 seconds. Once half that time has elapsed
+	 * reread the system clock and TSC to ensure wrapping does not
+	 * occur.
+	 */
 	delta = cycles - pcapng_time.cycles;
+	if (delta > ((1ULL << 63) / NSEC_PER_SEC)) {
+		pcapng_init();
+		if (cycles > pcapng_time.cycles)
+			delta = cycles - pcapng_time.cycles;
+		else {
+			delta = pcapng_time.cycles - cycles;
+			return pcapng_time.ns - (delta * NSEC_PER_SEC) / rte_get_tsc_hz();
+		}
+	}
 	return pcapng_time.ns + (delta * NSEC_PER_SEC) / rte_get_tsc_hz();
 }
 
-- 
2.34.1


             reply	other threads:[~2022-05-07 16:13 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-07 16:12 Quentin Armitage [this message]
2022-05-11 16:08 ` Stephen Hemminger
2022-05-11 16:46 ` Stephen Hemminger
2022-05-14 17:14   ` Quentin Armitage
2022-05-16 13:26     ` Stephen Hemminger
2022-05-17 10:01   ` [PATCH v2] " Quentin Armitage
2022-05-17 15:15     ` Stephen Hemminger
2022-06-01 14:38       ` Thomas Monjalon
2022-05-17 21:04     ` 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=20220507161237.207805-1-quentin@armitage.org.uk \
    --to=quentin@armitage.org.uk \
    --cc=dev@dpdk.org \
    --cc=mdr@ashroe.eu \
    --cc=reshma.pattan@intel.com \
    --cc=stable@dpdk.org \
    --cc=stephen@networkplumber.org \
    /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;
as well as URLs for NNTP newsgroup(s).