Either through misuse of the API, or races it might happen
that rte_pcapng_copy() is called before rte_pcapng_fdopen().

Simplest solution to handle this is to modify the logic for
TSC to nanosecond epoch conversion to handle the case where
the difference is negative.

Bugzilla ID: 1291
Fixes: 166591931b72 ("pcapng: modify timestamp calculation")
Cc: [email protected]

Signed-off-by: Stephen Hemminger <[email protected]>
---
 lib/pcapng/rte_pcapng.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/lib/pcapng/rte_pcapng.c b/lib/pcapng/rte_pcapng.c
index b93af418aa..1a1935e876 100644
--- a/lib/pcapng/rte_pcapng.c
+++ b/lib/pcapng/rte_pcapng.c
@@ -155,13 +155,16 @@ tsc_clock_init(struct tsc_clock *clk)
 static inline uint64_t
 tsc_to_ns_epoch(const struct tsc_clock *clk, uint64_t tsc)
 {
-       uint64_t delta, ns;
-
-       delta = tsc - clk->tsc_base;
-       ns = (delta >> clk->shift) * NSEC_PER_SEC;
-       ns = rte_reciprocal_divide_u64(ns, &clk->tsc_hz_inv);
-
-       return clk->ns_base + ns;
+       int64_t delta = tsc - clk->tsc_base;
+       uint64_t ns;
+
+       if (unlikely(delta < 0)) {
+               ns = (-delta >> clk->shift) * NSEC_PER_SEC;
+               return clk->ns_base - rte_reciprocal_divide_u64(ns, 
&clk->tsc_hz_inv);
+       } else {
+               ns = (delta >> clk->shift) * NSEC_PER_SEC;
+               return clk->ns_base + rte_reciprocal_divide_u64(ns, 
&clk->tsc_hz_inv);
+       }
 }
 
 /* length of option including padding */
-- 
2.51.0

Reply via email to