From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 40D1B606A322 for ; Wed, 2 Dec 2026 17:51:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=networkplumber-org.20260707.gappssmtp.com header.i=@networkplumber-org.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=POoPS+nd Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 606C240689; Wed, 2 Dec 2026 18:50:37 +0100 (CET) Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by mails.dpdk.org (Postfix) with ESMTPS id C36A04064F for ; Wed, 2 Dec 2026 18:50:34 +0100 (CET) Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-3b532f8a51eso2777875a91.1 for ; Wed, 02 Dec 2026 09:50:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20260707.gappssmtp.com; s=20260707; t=1796233833; x=1796838633; darn=dpdk.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=JDw/JZcn1dC8r53f/CWT9ueKRFcwkA8Ra9gTWaC18Pc=; b=POoPS+ndVFwClADtz81PlFjykoipzA0NwoJJora228GPYoZLpzdzQvdjDuReAGLq/S rCRXfziQBil2DdCrRabwMUO4lHwkY5NK0IvwmIckXJ136obmhuUJe7rsnVBz944QglSt oTJYVGgVgdUzYH3oRFzM1hhZY9kxwoM09XA2njf/6eMmyD+RL0M248O5cFM8KqXd7fYX z3lrJv+/bnmjgxCBezL4YQoar79Rh1KwdrBG8WNSf+rwaN+OGAWLkAlB8uAh83lOyNz2 wpTpJDSjrRY5PdVpH3FHxONa5+Jh/c1xRpzjDGG54Y1kDavUDv8s8gr76rQok1nSBpTs 4XUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796233833; x=1796838633; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=JDw/JZcn1dC8r53f/CWT9ueKRFcwkA8Ra9gTWaC18Pc=; b=k1UyLHnSASAkVCEq233T2leQZF819l74mYfW4Vu5lOfNZgct3/1Fi4Oyw9KqajOdIo 6zMKnQHe6M0d6uQxRKhwtaR5iFIVO4BjGxTa9F7VBBkUZm+Cj7RXGtZKXTHkYceqVtHR Svb9W+YlefOn7DXJEJjkswv5EfT4mItUhXP5j59rDpHuxYqIM0lR+yReLGYDJD+pI/ge elhIYPpc4PcIJdJDpQjOYq+wVbYc6rFnPmDx6bxjZJJhmcoxuHVp2Q4cigw4J5Iv08B8 0i1LM4u7cYC+UYvsrKfT1Rn1uIYyzho0Il40/bHJ8BNcjswNhkC+7FTVirRyuYCKXn20 8QEQ== X-Gm-Message-State: APuU4Z/q3rAzZRDkJDVCjxp55RGNb5LedsXQ/a8qEwKMMvgNcZZFhF/9 G70tm6O0lT0ETvCdoPnoT6CcXlJ+Cbbw7W2ef1maR0gQLONgOubXvjZgDW3TijtN0fVThf6rW4h F0pBa8nE= X-Gm-Gg: AZFdTZfJnBdA5oL8b2GQXJenuoNnvkfWHshkBl18OYn1hX4dZc7WWWNlLimSCVmsSqw V0actcyQ1Vw2O9fRBwBI+5MYesB1jL4KIbYSNcVezJTEa2yLB+NAW5IAyXYnJtsPmo9eNkKkSx2 8NG1UrxdOXG3/5rGIKXT6wONpiIjjT2rAp2f0rgjdLbLoJqYHXzW7sbKfGOyZXi+yBgcepPINGG Gk5iPOep01lJPvGrakOoJGrxAnVTJo4WEbaXIjmpFGr+dv50xPp57go51aduAebiDPPfvesLfmZ gF853ht48jV01dd8CbnyRL1pV3/UaRVnO7w76gdd3I31ot6DIHYx8Bp9br9Vp4UPPgltBo65I4S EZi1wSF6ieRfSoo5rsrlhsfQmRl08MDlDM+wgsZjSHbvrcAlm4tStxwMt9mwDYuZXU5EMtN7bgU EbPqXCtmHDTQEWYV/nEGJVZSO2plz4Y/eIJaMHYzEQGd8c+5M6YZJsD5+kWSJj6uOhhnEvWh/L8 4ZrRgef2BqRPDexduncB2C9sK2xj7ctRt6ztcf2WSyylOY8nAXtJP24Ea7uUT5CfQD+gxyhnVc9 uW9CMrOsbMeGmrqI0clCJQiDIsss2N7A+QpL4Vm73X1BZfR8wSCqEBm7RIGVfRNj4Ro5 X-Received: by 2002:a17:90b:5346:b0:3b0:9976:c1c6 with SMTP id 98e67ed59e1d1-3b41c9bbe00mr38898498a91.66.1796233833107; Wed, 02 Dec 2026 09:50:33 -0800 (PST) Received: from phoenix.lan (204-195-112-43.wavecable.com. [204.195.112.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b5c784e74esm211314a91.6.2026.12.02.09.50.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 09:50:32 -0800 (PST) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Subject: [PATCH v2 4/7] pcapng: use EAL TSC to epoch conversion Date: Wed, 2 Dec 2026 09:49:42 -0800 Message-ID: <20261202175028.4071813-5-stephen@networkplumber.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261202175028.4071813-1-stephen@networkplumber.org> References: <20261201224258.1962108-1-stephen@networkplumber.org> <20261202175028.4071813-1-stephen@networkplumber.org> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Archive: List-Post: List-Help: List-Subscribe: , X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 The pcapng library carried its own TSC to nanoseconds conversion along with the reference point it needs: the TSC and wall clock sampled when the capture file was opened. EAL now records an equivalent reference point at init and exposes rte_tsc_to_ns(), so the local copy is redundant. Convert timestamps with rte_tsc_to_ns() and drop the per-handle reference point. The interface statistics block still reports when the file was opened rather than when EAL started. Timestamps are now converted when the packet is copied rather than when it is written, so test_long_timestamp can no longer reach large deltas by rewriting the cycle count in the block header. Drop it; the overflow it covered is tested directly on rte_tsc_to_ns(). Signed-off-by: Stephen Hemminger --- app/test/test_pcapng.c | 145 ++-------------------------------------- lib/pcapng/rte_pcapng.c | 63 +++-------------- 2 files changed, 12 insertions(+), 196 deletions(-) diff --git a/app/test/test_pcapng.c b/app/test/test_pcapng.c index 688b58794e..3e190888b6 100644 --- a/app/test/test_pcapng.c +++ b/app/test/test_pcapng.c @@ -467,47 +467,6 @@ valid_pcapng_file(const char *file_name, uint64_t started, unsigned int expected return ret; } -/* Record the timestamp of the first packet in the file. */ -static void -first_timestamp_cb(u_char *user, const struct pcap_pkthdr *h, - const u_char *bytes __rte_unused) -{ - uint64_t *ts_ns = (uint64_t *)user; - - /* File has nanosecond precision, so tv_usec holds ns. */ - if (*ts_ns == 0) - *ts_ns = (uint64_t)h->ts.tv_sec * NS_PER_S + h->ts.tv_usec; -} - -static int -read_one_timestamp(const char *file_name, uint64_t *ts_ns) -{ - char errbuf[PCAP_ERRBUF_SIZE]; - pcap_t *pcap; - int ret; - - *ts_ns = 0; - pcap = pcap_open_offline_with_tstamp_precision(file_name, - PCAP_TSTAMP_PRECISION_NANO, - errbuf); - if (pcap == NULL) { - printf("pcap_open_offline('%s') failed: %s\n", - file_name, errbuf); - return -1; - } - - ret = pcap_loop(pcap, 0, first_timestamp_cb, (u_char *)ts_ns); - if (ret != 0) - printf("pcap_loop: failed: %s\n", pcap_geterr(pcap)); - pcap_close(pcap); - - if (ret == 0 && *ts_ns == 0) { - printf("no packet found in %s\n", file_name); - return -1; - } - return ret; -} - static int test_add_interface(void) { @@ -648,8 +607,8 @@ test_write_before_open(void) mbuf1_prepare(&mbfs); mbuf1_resize(&mbfs, rte_rand_max(MAX_DATA_SIZE)); - /* Copy packets BEFORE opening the pcapng file. - * This exercises the negative TSC delta path. + /* Copy packets BEFORE opening the pcapng file, so their + * timestamps precede the capture start time. */ for (i = 0; i < (int)count; i++) { clones[i] = rte_pcapng_copy(port_id, 0, &mbfs.mb[0], mp, @@ -662,7 +621,7 @@ test_write_before_open(void) } } - /* Small delay so fdopen's tsc_base is measurably after the copies */ + /* Small delay so the capture start is measurably after the copies */ rte_delay_us_block(100); now = current_timestamp(); @@ -689,7 +648,7 @@ test_write_before_open(void) goto fail; } - /* Write the pre-captured packets — timestamps precede tsc_base */ + /* Write the pre-captured packets */ len = rte_pcapng_write_packets(pcapng, clones, count); rte_pktmbuf_free_bulk(clones, count); if (len <= 0) { @@ -721,101 +680,6 @@ test_cleanup(void) rte_vdev_uninit(null_dev); } -/* - * Converting a cycle count to nanoseconds overflows past - * UINT64_MAX / NS_PER_S cycles, which is only a few seconds of real - * capture. Forge the timestamp in the block header to reach large - * deltas without waiting. - */ -static int -test_long_timestamp(void) -{ - /* seconds into the future */ - static const unsigned int offsets[] = { 1, 8, 3600, 10 * 86400 }; - struct pcapng_test_hdr { - uint32_t block_type; - uint32_t block_length; - uint32_t interface_id; - uint32_t timestamp_hi; - uint32_t timestamp_lo; - } *epb; - struct dummy_mbuf mbfs; - uint64_t hz = rte_get_tsc_hz(); - unsigned int i; - - TEST_ASSERT(hz != 0, "TSC frequency is zero"); - - mbuf1_prepare(&mbfs); - mbuf1_resize(&mbfs, 512); - - for (i = 0; i < RTE_DIM(offsets); i++) { - char file_name[PATH_MAX] = "/tmp/pcapng_test_XXXXXX.pcapng"; - uint64_t cycles, base_ns, want_ns, got_ns, diff; - struct rte_mbuf *mc; - rte_pcapng_t *pcapng; - int ret, tmp_fd; - ssize_t len; - - mc = rte_pcapng_copy(port_id, 0, &mbfs.mb[0], mp, - rte_pktmbuf_pkt_len(&mbfs.mb[0]), - RTE_PCAPNG_DIRECTION_IN, NULL); - TEST_ASSERT(mc != NULL, "rte_pcapng_copy failed"); - - tmp_fd = mkstemps(file_name, strlen(".pcapng")); - if (tmp_fd == -1) { - rte_pktmbuf_free(mc); - TEST_ASSERT(false, "mkstemps() failed"); - } - - base_ns = current_timestamp(); - pcapng = rte_pcapng_fdopen(tmp_fd, NULL, NULL, "longts", NULL); - if (pcapng == NULL) { - close(tmp_fd); - rte_pktmbuf_free(mc); - TEST_ASSERT(false, "rte_pcapng_fdopen failed"); - } - - ret = rte_pcapng_add_interface(pcapng, port_id, DLT_EN10MB, - NULL, NULL, NULL); - if (ret < 0) { - rte_pcapng_close(pcapng); - rte_pktmbuf_free(mc); - TEST_ASSERT(false, "can not add port %u", port_id); - } - - /* Conversion happens on write, so move the capture time. */ - epb = rte_pktmbuf_mtod(mc, struct pcapng_test_hdr *); - cycles = (uint64_t)epb->timestamp_hi << 32; - cycles += epb->timestamp_lo; - cycles += (uint64_t)offsets[i] * hz; - epb->timestamp_hi = cycles >> 32; - epb->timestamp_lo = (uint32_t)cycles; - - len = rte_pcapng_write_packets(pcapng, &mc, 1); - rte_pktmbuf_free(mc); - rte_pcapng_close(pcapng); - TEST_ASSERT(len > 0, "write failed at +%u s", offsets[i]); - - ret = read_one_timestamp(file_name, &got_ns); - TEST_ASSERT(ret == 0, "can not read back +%u s", offsets[i]); - - /* An overflow wraps and misses by the whole offset. */ - want_ns = base_ns + (uint64_t)offsets[i] * NS_PER_S; - diff = (got_ns > want_ns) ? got_ns - want_ns : want_ns - got_ns; - if (diff > 2 * NS_PER_S) - printf("at +%u s: got %"PRIu64" want %"PRIu64"\n", - offsets[i], got_ns, want_ns); - else - remove(file_name); - - TEST_ASSERT(diff <= 2 * NS_PER_S, - "timestamp off by %"PRIu64" ns at +%u s", - diff, offsets[i]); - } - - return 0; -} - static struct unit_test_suite test_pcapng_suite = { .setup = test_setup, @@ -825,7 +689,6 @@ unit_test_suite test_pcapng_suite = { TEST_CASE(test_add_interface), TEST_CASE(test_write_packets), TEST_CASE(test_write_before_open), - TEST_CASE(test_long_timestamp), TEST_CASES_END() } }; diff --git a/lib/pcapng/rte_pcapng.c b/lib/pcapng/rte_pcapng.c index ce71b07d93..f09253ab94 100644 --- a/lib/pcapng/rte_pcapng.c +++ b/lib/pcapng/rte_pcapng.c @@ -40,8 +40,7 @@ struct rte_pcapng { int outfd; /* output file */ unsigned int ports; /* number of interfaces added */ - uint64_t offset_ns; /* ns since 1/1/1970 when initialized */ - uint64_t tsc_base; /* TSC when started */ + uint64_t start_time; /* ns since 1/1/1970 when opened */ /* DPDK port id to interface index in file */ uint32_t port_index[RTE_MAX_ETHPORTS]; @@ -97,39 +96,6 @@ static ssize_t writev(int fd, const struct iovec *iov, int iovcnt) #define if_indextoname(ifindex, ifname) NULL #endif -/* - * Convert a count of cycles to nanoseconds. - * - * Compute the whole seconds first, so that the remainder is always - * less than the frequency and scaling it by NS_PER_S cannot wrap. - */ -static uint64_t -pcapng_cycles_to_ns(uint64_t delta) -{ - const uint64_t hz = rte_get_tsc_hz(); - uint64_t secs = delta / hz; - uint64_t rem = delta % hz; - - return secs * NS_PER_S + (rem * NS_PER_S) / hz; -} - -/* Convert from TSC (CPU cycles) to nanoseconds */ -static uint64_t -pcapng_timestamp(const rte_pcapng_t *self, uint64_t cycles) -{ - /* - * A packet may be copied before the file was opened, so the TSC - * can be behind the reference point. Handle both directions on - * an unsigned magnitude. - */ - if (unlikely(cycles < self->tsc_base)) - return self->offset_ns - - pcapng_cycles_to_ns(self->tsc_base - cycles); - - return self->offset_ns + - pcapng_cycles_to_ns(cycles - self->tsc_base); -} - /* length of option including padding */ static uint16_t pcapng_optlen(uint16_t len) { @@ -361,7 +327,7 @@ rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id, { struct pcapng_statistics *hdr; struct pcapng_option *opt; - uint64_t start_time = self->offset_ns; + uint64_t start_time = self->start_time; uint64_t sample_time; uint32_t optlen, len; uint32_t *buf; @@ -414,7 +380,7 @@ rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id, hdr->block_length = len; hdr->interface_id = self->port_index[port_id]; - sample_time = pcapng_timestamp(self, rte_get_tsc_cycles()); + sample_time = rte_tsc_to_ns(rte_get_tsc_cycles()); hdr->timestamp_hi = sample_time >> 32; hdr->timestamp_lo = (uint32_t)sample_time; @@ -652,10 +618,11 @@ rte_pcapng_copy(uint16_t port_id, uint32_t queue, /* Interface index is filled in later during write */ mc->port = port_id; - /* Put timestamp in cycles here - adjust in packet write */ - timestamp = rte_get_tsc_cycles(); + /* Put timestamp in nanoseconds */ + timestamp = rte_tsc_to_ns(rte_get_tsc_cycles()); epb->timestamp_hi = timestamp >> 32; epb->timestamp_lo = (uint32_t)timestamp; + epb->capture_length = pkt_len; epb->original_length = orig_len; @@ -682,7 +649,6 @@ rte_pcapng_write_packets(rte_pcapng_t *self, for (i = 0; i < nb_pkts; i++) { struct rte_mbuf *m = pkts[i]; struct pcapng_enhance_packet_block *epb; - uint64_t cycles, timestamp; /* sanity check that is really a pcapng mbuf */ epb = rte_pktmbuf_mtod(m, struct pcapng_enhance_packet_block *); @@ -699,13 +665,6 @@ rte_pcapng_write_packets(rte_pcapng_t *self, return -1; } - /* adjust timestamp recorded in packet */ - cycles = (uint64_t)epb->timestamp_hi << 32; - cycles += epb->timestamp_lo; - timestamp = pcapng_timestamp(self, cycles); - epb->timestamp_hi = timestamp >> 32; - epb->timestamp_lo = (uint32_t)timestamp; - /* * The DPDK port is recorded during pcapng_copy. * Map that to PCAPNG interface in file. @@ -747,8 +706,6 @@ rte_pcapng_fdopen(int fd, { unsigned int i; rte_pcapng_t *self; - struct timespec ts; - uint64_t cycles; int ret; if ((osname && strlen(osname) > PCAPNG_STR_MAX) || @@ -768,17 +725,13 @@ rte_pcapng_fdopen(int fd, self->outfd = fd; self->ports = 0; - /* If Hz is zero, something is seriously broken. */ + /* Timestamps come from the TSC; if Hz is zero it is unusable. */ if (rte_get_tsc_hz() == 0) { rte_errno = ENODEV; goto fail; } - /* record start time in ns since 1/1/1970 */ - cycles = rte_get_tsc_cycles(); - clock_gettime(CLOCK_REALTIME, &ts); - self->tsc_base = (cycles + rte_get_tsc_cycles()) / 2; - self->offset_ns = rte_timespec_to_ns(&ts); + self->start_time = rte_tsc_to_ns(rte_get_tsc_cycles()); for (i = 0; i < RTE_MAX_ETHPORTS; i++) self->port_index[i] = UINT32_MAX; -- 2.53.0