From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 8BE9FA050C for ; Sat, 7 May 2022 18:13:14 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 7D3B24114E; Sat, 7 May 2022 18:13:14 +0200 (CEST) Received: from nabal.armitage.org.uk (host-92-27-6-192.static.as13285.net [92.27.6.192]) by mails.dpdk.org (Postfix) with ESMTP id 8859540395; Sat, 7 May 2022 18:13:12 +0200 (CEST) Received: from localhost (nabal.armitage.org.uk [127.0.0.1]) by nabal.armitage.org.uk (Postfix) with ESMTP id DB45C2E42EE; Sat, 7 May 2022 17:13:11 +0100 (BST) Authentication-Results: nabal.armitage.org.uk (amavisd-new); dkim=pass (1024-bit key) reason="pass (just generated, assumed good)" header.d=armitage.org.uk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=armitage.org.uk; h=content-transfer-encoding:mime-version:x-mailer:message-id :date:date:subject:subject:from:from:received; s=20200110; t= 1651939974; x=1652803975; bh=op2GSy3aeE/zs/MeCvgl9Kh+2BoWPDz5qjb KKkuNjK4=; b=hW48MvTHhKtCI2mUInLJYoBQj+gvswDUlbWW7FelipA2Q4zThZc 8Ufj162xlmR9tu1vvnuQ/WQ3gqFuBWiqCobqvXKWOHndVuVgEkL65KOzhseS+VGT vfsGgNLWb69aTSOrUVFHzWrgPc2m3v1zzl6UDdVKTT3qZaf7YMM3fCZQ= X-Virus-Scanned: amavisd-new at armitage.org.uk Received: from samson.armitage.org.uk (samson.armitage.org.uk [IPv6:2001:470:69dd:35::210]) by nabal.armitage.org.uk (Postfix) with ESMTPSA id 0F72A2E42EB; Sat, 7 May 2022 17:12:53 +0100 (BST) From: Quentin Armitage To: Reshma Pattan , Stephen Hemminger , Ray Kinsella Cc: dev@dpdk.org, Quentin Armitage , stable@dpdk.org Subject: [PATCH] libpcapng: fix timestamp wrapping in output files Date: Sat, 7 May 2022 17:12:36 +0100 Message-Id: <20220507161237.207805-1-quentin@armitage.org.uk> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: stable@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: patches for DPDK stable branches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: stable-bounces@dpdk.org 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 --- 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