From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0065CF9D0E2 for ; Tue, 14 Apr 2026 16:11:55 +0000 (UTC) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 1F25040B92; Tue, 14 Apr 2026 18:10:35 +0200 (CEST) Received: from mail-oi1-f176.google.com (mail-oi1-f176.google.com [209.85.167.176]) by mails.dpdk.org (Postfix) with ESMTP id 5DEFA4067A for ; Tue, 14 Apr 2026 18:10:32 +0200 (CEST) Received: by mail-oi1-f176.google.com with SMTP id 5614622812f47-46aa216a65aso3087422b6e.1 for ; Tue, 14 Apr 2026 09:10:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20251104.gappssmtp.com; s=20251104; t=1776183031; x=1776787831; darn=dpdk.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nBMbO+jneA31WFXIB1pNcYFWUan20lATibLRWRL1yPc=; b=hk2OvfQ7zX1CzyShsR0ddce8bcKxv7Ry7CJBfcp68838okbTCPYKH5QVps5dGvhsBn zX5TJLcFlOP/cN04TORS2/cgyoLXPT10UqGHFgSEJTgr5KJ21xC1O4eP1OHPzv91lWQK 4KgVHgS/UCUyumZ5Dsf/OXcBhk2TjlPhR/7LhRozJm2wH3Oz2EJb+qjDhN0BSKKGK2HG Q8ra9RNB7/0Zmr8d47W0ugetQjx6tf9+LqEcOsZP32Z5wsECdqn/Qmq8kCaiKrM7F3Y7 srbkfUj0nWILCeXoZmqFqp9mhykQTefDzUeV4ZPvuJ1riE1/oPY0wpV+Pjn3cJOStSt5 KNvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776183031; x=1776787831; h=content-transfer-encoding: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; bh=nBMbO+jneA31WFXIB1pNcYFWUan20lATibLRWRL1yPc=; b=FMY0/nEMCpP7+4Jwk0LmFHYQTvuclG+fHaFzMfmygQ9yOqHsXbD5/9O3/fpfzbZIbX V9OyfKwwQ6qWnidxOY2Bfftd0W7JJnZGBXY0hua7e2E449E5bEi9+H2X9eKtDbKVLYC2 jn6t/CYJlWHjdx2Wm/G5+y+2nAefmg2qW3VQu0UyvmuDG3VTXFMVsmxOejgk/2h/zyYi IXbMOlnLgx+gceumvNxYPljNOvRBoPiZnSKKD4j4fENdrQdFNSHoAvDvBQfD55g/d8Fu ++awQ1r82rBQem43jN3SNIG5zrFe4hDG3ypJJ5tkgk6LM3Xe8PaImdoGEVuhXBaR3eyp Ay+w== X-Gm-Message-State: AOJu0YyYsRYWB3VjRupaHQ4YSNNLHVw7iysvkkvPMrUIeu9Bhfn3/GQc gdqi+wTLK/9aQJamxP/3VHfhd4c0HgpUaX7yMvRA19VfOLtkNIssLj4RIfZqdSY5c2tJ9Tx54/w jPx6K X-Gm-Gg: AeBDieu71a33UMFP1ky1MbfbyRWlr98b4wpYoAPUbOxJxRtZMATowL6hBlNFjI5clqT /DjeO/ixFDTQvRiQpWNs1fXA4kZFwXls3RAp7QiqZKwzzqj/jBR+L1SByduPzEh2vfZM4/naogd /5k0wnfX7LmV7nrS/LVl/4rJCCCfm1+mhBjqGBmkcJBAyf9iCVO2z5O2RNmXsyiFvFP4cFPvKl+ oNyy2dYMEqEIdshpS0WYkmF0M1fIRNsmOww1K5TD7v2mBAtqBy2lh1ghC7i47+szyOoulfTs8H3 WfslLaosmZLHC/4b2Lm61EaeQyj+a1IptB3FQCKeN6okSYVECZ23FeYTwnevveq0Fk6Y1Ykx22c nOpySG2sGc+Ocnr0k8nAEB8x9+ecOmKOSKyubXLDT24Pw8bPzszXWUscym4euZ/PtMMi3U63OYV UMWwjFrGaIhlJTq31b+LU44edp65rONDXQtLA/JVLD0/I= X-Received: by 2002:a05:6808:1824:b0:468:982:cc11 with SMTP id 5614622812f47-4789e329389mr9929388b6e.13.1776183031534; Tue, 14 Apr 2026 09:10:31 -0700 (PDT) Received: from phoenix.lan ([104.202.41.210]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-423dd396960sm11809554fac.2.2026.04.14.09.10.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Apr 2026 09:10:31 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Subject: [PATCH v22 15/24] net/pcap: support nanosecond timestamp precision Date: Tue, 14 Apr 2026 09:08:06 -0700 Message-ID: <20260414161011.756101-16-stephen@networkplumber.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260414161011.756101-1-stephen@networkplumber.org> References: <20260106182823.192350-1-stephen@networkplumber.org> <20260414161011.756101-1-stephen@networkplumber.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Enable nanosecond-precision timestamps for both live capture and pcap file reading. Replace pcap_open_live() with the pcap_create()/pcap_activate() API, which allows setting PCAP_TSTAMP_PRECISION_NANO before activation. Similarly, use pcap_open_offline_with_tstamp_precision() for reading pcap files. The pcap_pkthdr timestamp field, despite being declared as struct timeval, actually contains nanoseconds (not microseconds) when nanosecond precision is requested. Make receive timestamp offloading conditional: timestamps are now only written to the mbuf dynamic field when RTE_ETH_RX_OFFLOAD_TIMESTAMP is enabled. Previously, timestamps were unconditionally added to every received packet. Other related changes: * Add read_clock dev_op returning current UTC time for timestamp correlation. * Move per-burst timestamp calculation outside the packet loop in tx_dumper. * Enable immediate mode and improve error reporting in live capture setup. Signed-off-by: Stephen Hemminger --- doc/guides/nics/pcap.rst | 3 + doc/guides/rel_notes/release_26_07.rst | 1 + drivers/net/pcap/pcap_ethdev.c | 161 +++++++++++++++++++------ 3 files changed, 130 insertions(+), 35 deletions(-) diff --git a/doc/guides/nics/pcap.rst b/doc/guides/nics/pcap.rst index 2c5fc0b500..65209c1db4 100644 --- a/doc/guides/nics/pcap.rst +++ b/doc/guides/nics/pcap.rst @@ -257,3 +257,6 @@ Features and Limitations * The PMD will transparently insert a VLAN tag to transmitted packets if the mbuf has ``RTE_MBUF_F_TX_VLAN`` set. + +* If ``RTE_ETH_RX_OFFLOAD_TIMESTAMP`` is enabled then the timestamp + from the pcap file is converted to DPDK format. diff --git a/doc/guides/rel_notes/release_26_07.rst b/doc/guides/rel_notes/release_26_07.rst index 3e97046267..4205d90be1 100644 --- a/doc/guides/rel_notes/release_26_07.rst +++ b/doc/guides/rel_notes/release_26_07.rst @@ -59,6 +59,7 @@ New Features * Added support for VLAN insertion and stripping. * Added support for reporting link state in ``iface`` mode. + * Receive timestamps support nanosecond precision. Removed Items diff --git a/drivers/net/pcap/pcap_ethdev.c b/drivers/net/pcap/pcap_ethdev.c index daee7f1d97..fbd45c18b3 100644 --- a/drivers/net/pcap/pcap_ethdev.c +++ b/drivers/net/pcap/pcap_ethdev.c @@ -27,13 +27,11 @@ #include #include #include +#include #include "pcap_osdep.h" #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535 -#define RTE_ETH_PCAP_SNAPLEN RTE_ETHER_MAX_JUMBO_FRAME_LEN -#define RTE_ETH_PCAP_PROMISC 1 -#define RTE_ETH_PCAP_TIMEOUT -1 #define ETH_PCAP_RX_PCAP_ARG "rx_pcap" #define ETH_PCAP_TX_PCAP_ARG "tx_pcap" @@ -77,6 +75,7 @@ struct pcap_rx_queue { uint16_t port_id; uint16_t queue_id; bool vlan_strip; + bool timestamp_offloading; struct rte_mempool *mb_pool; struct queue_stat rx_stat; struct queue_missed_stat missed_stat; @@ -108,6 +107,7 @@ struct pmd_internals { bool phy_mac; bool infinite_rx; bool vlan_strip; + bool timestamp_offloading; }; struct pmd_process_private { @@ -269,6 +269,15 @@ eth_pcap_rx_infinite(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) if (pcap_q->vlan_strip) rte_vlan_strip(bufs[i]); + if (pcap_q->timestamp_offloading) { + struct timespec ts; + + timespec_get(&ts, TIME_UTC); + *RTE_MBUF_DYNFIELD(bufs[i], timestamp_dynfield_offset, + rte_mbuf_timestamp_t *) = rte_timespec_to_ns(&ts); + bufs[i]->ol_flags |= timestamp_rx_dynflag; + } + rx_bytes += bufs[i]->data_len; /* Enqueue packet back on ring to allow infinite rx. */ @@ -339,10 +348,21 @@ eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) if (pcap_q->vlan_strip) rte_vlan_strip(mbuf); - uint64_t us = (uint64_t)header->ts.tv_sec * US_PER_S + header->ts.tv_usec; + if (pcap_q->timestamp_offloading) { + /* + * The use of tv_usec as nanoseconds is not a bug here. + * Interface is always created with nanosecond precision, and + * that is how pcap API bodged in nanoseconds support. + */ + uint64_t ns = (uint64_t)header->ts.tv_sec * NSEC_PER_SEC + + header->ts.tv_usec; + + *RTE_MBUF_DYNFIELD(mbuf, timestamp_dynfield_offset, + rte_mbuf_timestamp_t *) = ns; + + mbuf->ol_flags |= timestamp_rx_dynflag; + } - *RTE_MBUF_DYNFIELD(mbuf, timestamp_dynfield_offset, rte_mbuf_timestamp_t *) = us; - mbuf->ol_flags |= timestamp_rx_dynflag; mbuf->port = pcap_q->port_id; bufs[num_rx] = mbuf; num_rx++; @@ -362,14 +382,19 @@ eth_null_rx(void *queue __rte_unused, return 0; } -#define NSEC_PER_SEC 1000000000L - /* - * This function stores nanoseconds in `tv_usec` field of `struct timeval`, - * because `ts` goes directly to nanosecond-precision dump. + * Calculate current timestamp in nanoseconds by computing + * offset from starting time value. + * + * Note: it is not a bug that this code is putting nanosecond + * value into microsecond timeval field. The pcap API is old + * and nanoseconds were bodged on as an after thought. + * As long as the pcap stream is set to nanosecond precision + * it expects nanoseconds here. */ static inline void -calculate_timestamp(struct timeval *ts) { +calculate_timestamp(struct timeval *ts) +{ uint64_t cycles; struct timespec cur_time; @@ -446,8 +471,10 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) if (dumper == NULL || nb_pkts == 0) return 0; - /* writes the nb_pkts packets to the previously opened pcap file - * dumper */ + /* all packets in burst have same timestamp */ + calculate_timestamp(&header.ts); + + /* writes the nb_pkts packets to the previously opened pcap file dumper */ for (i = 0; i < nb_pkts; i++) { struct rte_mbuf *mbuf = bufs[i]; uint32_t len, caplen; @@ -465,8 +492,6 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) len = rte_pktmbuf_pkt_len(mbuf); caplen = RTE_MIN(len, RTE_ETH_PCAP_SNAPSHOT_LEN); - calculate_timestamp(&header.ts); - header.len = len; header.caplen = caplen; @@ -595,22 +620,62 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) * pcap_open_live wrapper function */ static inline int -open_iface_live(const char *iface, pcap_t **pcap) { - *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN, - RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf); +open_iface_live(const char *iface, pcap_t **pcap) +{ + pcap_t *pc; + int status; - if (*pcap == NULL) { - PMD_LOG(ERR, "Couldn't open %s: %s", iface, errbuf); - return -1; + pc = pcap_create(iface, errbuf); + if (pc == NULL) { + PMD_LOG(ERR, "Couldn't create %s: %s", iface, errbuf); + goto error; } - if (pcap_setnonblock(*pcap, 1, errbuf)) { + status = pcap_set_tstamp_precision(pc, PCAP_TSTAMP_PRECISION_NANO); + if (status != 0) { + PMD_LOG(ERR, "%s: Could not set to ns precision: %s", + iface, pcap_statustostr(status)); + goto error; + } + + status = pcap_set_immediate_mode(pc, 1); + if (status != 0) + PMD_LOG(WARNING, "%s: Could not set to immediate mode: %s", + iface, pcap_statustostr(status)); + + status = pcap_set_promisc(pc, 1); + if (status != 0) + PMD_LOG(WARNING, "%s: Could not set to promiscuous: %s", + iface, pcap_statustostr(status)); + + status = pcap_set_snaplen(pc, RTE_ETH_PCAP_SNAPSHOT_LEN); + if (status != 0) + PMD_LOG(WARNING, "%s: Could not set snapshot length: %s", + iface, pcap_statustostr(status)); + + status = pcap_activate(pc); + if (status < 0) { + char *cp = pcap_geterr(pc); + + if (status == PCAP_ERROR) + PMD_LOG(ERR, "%s: could not activate: %s", iface, cp); + else + PMD_LOG(ERR, "%s: %s (%s)", iface, pcap_statustostr(status), cp); + goto error; + } + + if (pcap_setnonblock(pc, 1, errbuf)) { PMD_LOG(ERR, "Couldn't set non-blocking on %s: %s", iface, errbuf); - pcap_close(*pcap); - return -1; + goto error; } + *pcap = pc; return 0; + +error: + if (pc != NULL) + pcap_close(pc); + return -1; } static int @@ -657,7 +722,8 @@ open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper) static int open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap) { - *pcap = pcap_open_offline(pcap_filename, errbuf); + *pcap = pcap_open_offline_with_tstamp_precision(pcap_filename, + PCAP_TSTAMP_PRECISION_NANO, errbuf); if (*pcap == NULL) { PMD_LOG(ERR, "Couldn't open %s: %s", pcap_filename, errbuf); @@ -816,6 +882,17 @@ eth_dev_configure(struct rte_eth_dev *dev) const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode; internals->vlan_strip = !!(rxmode->offloads & RTE_ETH_RX_OFFLOAD_VLAN_STRIP); + internals->timestamp_offloading = !!(rxmode->offloads & RTE_ETH_RX_OFFLOAD_TIMESTAMP); + + if (internals->timestamp_offloading && timestamp_rx_dynflag == 0) { + int ret = rte_mbuf_dyn_rx_timestamp_register(×tamp_dynfield_offset, + ×tamp_rx_dynflag); + if (ret != 0) { + PMD_LOG(ERR, "Failed to register Rx timestamp field/flag"); + return ret; + } + } + return 0; } @@ -833,7 +910,8 @@ eth_dev_info(struct rte_eth_dev *dev, dev_info->min_rx_bufsize = 0; dev_info->tx_offload_capa = RTE_ETH_TX_OFFLOAD_MULTI_SEGS | RTE_ETH_TX_OFFLOAD_VLAN_INSERT; - dev_info->rx_offload_capa = RTE_ETH_RX_OFFLOAD_VLAN_STRIP; + dev_info->rx_offload_capa = RTE_ETH_RX_OFFLOAD_VLAN_STRIP | + RTE_ETH_RX_OFFLOAD_TIMESTAMP; return 0; } @@ -1000,6 +1078,7 @@ eth_rx_queue_setup(struct rte_eth_dev *dev, pcap_q->queue_id = rx_queue_id; pcap_q->vlan_strip = internals->vlan_strip; dev->data->rx_queues[rx_queue_id] = pcap_q; + pcap_q->timestamp_offloading = internals->timestamp_offloading; if (internals->infinite_rx) { struct pmd_process_private *pp; @@ -1138,6 +1217,17 @@ eth_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id) return 0; } +/* Timestamp values in receive packets from libpcap are in nanoseconds */ +static int +eth_dev_read_clock(struct rte_eth_dev *dev __rte_unused, uint64_t *timestamp) +{ + struct timespec cur_time; + + timespec_get(&cur_time, TIME_UTC); + *timestamp = rte_timespec_to_ns(&cur_time); + return 0; +} + static int eth_vlan_offload_set(struct rte_eth_dev *dev, int mask) { @@ -1164,6 +1254,7 @@ static const struct eth_dev_ops ops = { .dev_close = eth_dev_close, .dev_configure = eth_dev_configure, .dev_infos_get = eth_dev_info, + .read_clock = eth_dev_read_clock, .rx_queue_setup = eth_rx_queue_setup, .tx_queue_setup = eth_tx_queue_setup, .tx_queue_release = eth_tx_queue_release, @@ -1578,15 +1669,15 @@ pmd_pcap_probe(struct rte_vdev_device *dev) name = rte_vdev_device_name(dev); PMD_LOG(INFO, "Initializing pmd_pcap for %s", name); - timespec_get(&start_time, TIME_UTC); - start_cycles = rte_get_timer_cycles(); - hz = rte_get_timer_hz(); - - ret = rte_mbuf_dyn_rx_timestamp_register(×tamp_dynfield_offset, - ×tamp_rx_dynflag); - if (ret != 0) { - PMD_LOG(ERR, "Failed to register Rx timestamp field/flag"); - return -1; + /* Record info for timestamps on first probe */ + if (hz == 0) { + hz = rte_get_timer_hz(); + if (hz == 0) { + PMD_LOG(ERR, "Reported hz is zero!"); + return -1; + } + timespec_get(&start_time, TIME_UTC); + start_cycles = rte_get_timer_cycles(); } if (rte_eal_process_type() == RTE_PROC_SECONDARY) { -- 2.53.0