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 A4141A0032 for ; Wed, 11 May 2022 18:47:00 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 8576440042; Wed, 11 May 2022 18:47:00 +0200 (CEST) Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by mails.dpdk.org (Postfix) with ESMTP id 79BCA40042 for ; Wed, 11 May 2022 18:46:59 +0200 (CEST) Received: by mail-pf1-f179.google.com with SMTP id v11so2474970pff.6 for ; Wed, 11 May 2022 09:46:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=vUP0s9A9EglTN7qJ3YWaOKG8hw8cIB1I3jKoYln/Lb4=; b=NtjL9R6vtRUMCRdMgd3P4MkY1ZpTsv6zqLvztROtQxiNS9Kfm5JtBKSNLeX70WiqBd GIA9xykiMtzLcOI4J76kg6LmYtLwJlUDxR2Eq9cFv/ng9RPvBztEojjaByIDIM4ISCEK lbt06+DJzsIZtE2TxNhdjCThb52QTX3VvHlaOGrH9qKiN4EJtJm3Go6DwQE8xg4wPNAo +8uXw7R68qk7a83yklKCceJzyhkpSySu1sWI4xeytS9OYQn5uclcB/2QFdXHeVKfYIxk OQJuBsNC57ZLdRVL77eqZSZAXULmwFwIH0kecKIPbkbtOgb7+L+bP90F57rhixnLGZxs n/XA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=vUP0s9A9EglTN7qJ3YWaOKG8hw8cIB1I3jKoYln/Lb4=; b=ejyL9aFhdH35Ig3YwlClh8OxU8GtKHOw/EfNfNPA2G8MRAFwUJONeJKz07O4dAc63i Taj/XKPmgmtlYf87iMGGnJsrWf+QXZa2sWfyKKEmBbdq6SJQAbzK6gBCvx4hoKgZHES6 z+mpeOBSyI5+33bi5Z8RNQf2QBsqdeepiTISEhL8DT9KzP+v1ZgL+nuBrYqC9NkyDStV OLo7Mh5tHhHMFQCrh5qEMkRlW07u9Jo0OLDInso/GfaJrEUiZ2M/ThB1mD5dHR5W4Ezx al83g1djPmsBjG6mAbgykHs05oYCnz2iNWi8MowflCeWAwaG8F56igP5kGOiDRPCWn0b QAUA== X-Gm-Message-State: AOAM532cDCgTUS9fFmUrOuV2f+gVhQAX+PrIUv+c80VO/pSJtt2v52eu 5fhadO4TA+ddkV53CfnpjZ7DTg== X-Google-Smtp-Source: ABdhPJz3iftFKRBNB+2IkbZ4jqh4T8bcjZdiOFz2NumOMltprkqIcYK5tIpEcOTrwVAJiQ1BfxbHtA== X-Received: by 2002:a05:6a00:1903:b0:4fa:fa9e:42e6 with SMTP id y3-20020a056a00190300b004fafa9e42e6mr26104189pfi.1.1652287618734; Wed, 11 May 2022 09:46:58 -0700 (PDT) Received: from hermes.local (204-195-112-199.wavecable.com. [204.195.112.199]) by smtp.gmail.com with ESMTPSA id l62-20020a639141000000b003c14af50625sm86016pge.61.2022.05.11.09.46.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 09:46:58 -0700 (PDT) Date: Wed, 11 May 2022 09:46:55 -0700 From: Stephen Hemminger To: Quentin Armitage Cc: Reshma Pattan , Ray Kinsella , dev@dpdk.org, stable@dpdk.org Subject: Re: [PATCH] libpcapng: fix timestamp wrapping in output files Message-ID: <20220511094655.4f885c84@hermes.local> In-Reply-To: <20220507161237.207805-1-quentin@armitage.org.uk> References: <20220507161237.207805-1-quentin@armitage.org.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit 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 On Sat, 7 May 2022 17:12:36 +0100 Quentin Armitage wrote: > 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 What about something like this instead. diff --git a/lib/pcapng/rte_pcapng.c b/lib/pcapng/rte_pcapng.c index 90b2f5bc6905..c5534301bf2c 100644 --- a/lib/pcapng/rte_pcapng.c +++ b/lib/pcapng/rte_pcapng.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include "pcapng_proto.h" @@ -34,27 +35,39 @@ struct rte_pcapng { }; /* For converting TSC cycles to PCAPNG ns format */ -struct pcapng_time { +#define TICK_SCALE 16u +static struct { uint64_t ns; uint64_t cycles; + struct rte_reciprocal_u64 inverse; } pcapng_time; RTE_INIT(pcapng_init) { struct timespec ts; + uint64_t scale_tick_per_ns; pcapng_time.cycles = rte_get_tsc_cycles(); clock_gettime(CLOCK_REALTIME, &ts); pcapng_time.ns = rte_timespec_to_ns(&ts); + + scale_tick_per_ns = (rte_get_tsc_hz() * TICK_SCALE) / NSEC_PER_SEC; + pcapng_time.inverse = rte_reciprocal_value_u64(scale_tick_per_ns); } /* PCAPNG timestamps are in nanoseconds */ static uint64_t pcapng_tsc_to_ns(uint64_t cycles) { - uint64_t delta; + uint64_t delta, elapsed; delta = cycles - pcapng_time.cycles; - return pcapng_time.ns + (delta * NSEC_PER_SEC) / rte_get_tsc_hz(); + + /* Compute elapsed time in nanoseconds scaled by TICK_SCALE + * since the start of the capture. + * With scale of 4 this will roll over in 36 years. + */ + elapsed = rte_reciprocal_divide_u64(delta, &pcapng_time.inverse); + return pcapng_time.ns + elapsed / TICK_SCALE; } /* length of option including padding */