patches for DPDK stable branches
 help / color / mirror / Atom feed
* [PATCH] libpcapng: fix timestamp wrapping in output files
@ 2022-05-07 16:12 Quentin Armitage
  2022-05-11 16:08 ` Stephen Hemminger
  2022-05-11 16:46 ` Stephen Hemminger
  0 siblings, 2 replies; 9+ messages in thread
From: Quentin Armitage @ 2022-05-07 16:12 UTC (permalink / raw)
  To: Reshma Pattan, Stephen Hemminger, Ray Kinsella
  Cc: dev, Quentin Armitage, stable

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


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-06-01 14:38 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-07 16:12 [PATCH] libpcapng: fix timestamp wrapping in output files Quentin Armitage
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

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).