From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH 05/12] net/pcap: support nanosecond timestamp precision
Date: Tue, 6 Jan 2026 10:26:44 -0800 [thread overview]
Message-ID: <20260106182823.192350-6-stephen@networkplumber.org> (raw)
In-Reply-To: <20260106182823.192350-1-stephen@networkplumber.org>
Consistently support nanosecond timestamps across all the
variations of pcap PMD receive.
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
drivers/net/pcap/pcap_ethdev.c | 103 +++++++++++++++++++++++++--------
1 file changed, 79 insertions(+), 24 deletions(-)
diff --git a/drivers/net/pcap/pcap_ethdev.c b/drivers/net/pcap/pcap_ethdev.c
index 41c5bc638f..905ca62bd4 100644
--- a/drivers/net/pcap/pcap_ethdev.c
+++ b/drivers/net/pcap/pcap_ethdev.c
@@ -19,13 +19,12 @@
#include <rte_mbuf_dyn.h>
#include <bus_vdev_driver.h>
#include <rte_os_shim.h>
+#include <rte_time.h>
#include "pcap_osdep.h"
#define RTE_ETH_PCAP_SNAPSHOT_LEN 65535
#define RTE_ETH_PCAP_SNAPLEN (RTE_ETHER_MAX_JUMBO_FRAME_LEN - RTE_ETHER_CRC_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"
@@ -68,6 +67,7 @@ struct queue_missed_stat {
struct pcap_rx_queue {
uint16_t port_id;
uint16_t queue_id;
+ bool timestamp_offloading;
struct rte_mempool *mb_pool;
struct queue_stat rx_stat;
struct queue_missed_stat missed_stat;
@@ -95,6 +95,7 @@ struct pmd_internals {
bool single_iface;
bool phy_mac;
bool infinite_rx;
+ bool timestamp_offloading;
};
struct pmd_process_private {
@@ -299,7 +300,6 @@ eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
if (ret != 1) {
if (ret == PCAP_ERROR)
pcap_q->rx_stat.err_pkts++;
-
break;
}
@@ -325,10 +325,20 @@ eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
}
mbuf->pkt_len = len;
- uint64_t us = (uint64_t)header->ts.tv_sec * US_PER_S + header->ts.tv_usec;
- *RTE_MBUF_DYNFIELD(mbuf, timestamp_dynfield_offset, rte_mbuf_timestamp_t *) = us;
- mbuf->ol_flags |= timestamp_rx_dynflag;
+ if (pcap_q->timestamp_offloading) {
+ /*
+ * Although time stamp in struct pcap_pkthdr is defined as struct timeval,
+ * it really is a timespec with nanosecond resolution.
+ */
+ const struct timespec *ts = (struct timespec *)&header->ts;
+
+ *RTE_MBUF_DYNFIELD(mbuf, timestamp_dynfield_offset,
+ rte_mbuf_timestamp_t *) = rte_timespec_to_ns(ts);
+
+ mbuf->ol_flags |= timestamp_rx_dynflag;
+ }
+
mbuf->port = pcap_q->port_id;
bufs[num_rx] = mbuf;
num_rx++;
@@ -514,22 +524,57 @@ 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 = pcap_create(iface, errbuf);
+ if (pc == NULL) {
+ PMD_LOG(ERR, "Couldn't create %s: %s", iface, errbuf);
+ goto error;
+ }
- if (*pcap == NULL) {
- PMD_LOG(ERR, "Couldn't open %s: %s", iface, errbuf);
- return -1;
+ int status = pcap_set_tstamp_precision(pc, PCAP_TSTAMP_PRECISION_NANO);
+ if (status != 0)
+ PMD_LOG(WARNING, "%s: Could not set to ns precision: %s",
+ iface, pcap_statustostr(status));
+
+ 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 promiscious: %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(*pcap, 1, errbuf)) {
+ 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)
+ pcap_close(pc);
+ return -1;
}
static int
@@ -576,7 +621,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);
@@ -613,6 +659,15 @@ eth_dev_start(struct rte_eth_dev *dev)
struct pcap_tx_queue *tx;
struct pcap_rx_queue *rx;
+ if (internals->timestamp_offloading) {
+ 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;
+ }
+ }
+
/* Special iface case. Single pcap is open and shared between tx/rx. */
if (internals->single_iface) {
tx = &internals->tx_queue[0];
@@ -728,8 +783,13 @@ eth_dev_stop(struct rte_eth_dev *dev)
}
static int
-eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
+eth_dev_configure(struct rte_eth_dev *dev)
{
+ struct pmd_internals *internals = dev->data->dev_private;
+ struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
+ const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode;
+
+ internals->timestamp_offloading = !!(rxmode->offloads & RTE_ETH_RX_OFFLOAD_TIMESTAMP);
return 0;
}
@@ -749,6 +809,7 @@ eth_dev_info(struct rte_eth_dev *dev,
dev_info->max_mtu = RTE_ETH_PCAP_SNAPLEN;
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_TIMESTAMP;
return 0;
}
@@ -896,6 +957,7 @@ eth_rx_queue_setup(struct rte_eth_dev *dev,
pcap_q->port_id = dev->data->port_id;
pcap_q->queue_id = rx_queue_id;
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;
@@ -1446,13 +1508,6 @@ pmd_pcap_probe(struct rte_vdev_device *dev)
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;
- }
-
if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
eth_dev = rte_eth_dev_attach_secondary(name);
if (!eth_dev) {
--
2.51.0
next prev parent reply other threads:[~2026-01-06 18:33 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-06 18:26 [PATCH 00/12] net/pcap: cleanups and test Stephen Hemminger
2026-01-06 18:26 ` [PATCH 01/12] net/pcap: avoid using rte_malloc and rte_memcpy Stephen Hemminger
2026-01-06 18:26 ` [PATCH 02/12] net/pcap: support MTU set Stephen Hemminger
2026-01-06 18:26 ` [PATCH 03/12] net/pcap: use bool for flags Stephen Hemminger
2026-01-07 10:28 ` Marat Khalili
2026-01-09 0:23 ` Stephen Hemminger
2026-01-06 18:26 ` [PATCH 04/12] net/pcap: support Tx offloads Stephen Hemminger
2026-01-06 18:26 ` Stephen Hemminger [this message]
2026-01-06 18:26 ` [PATCH 06/12] net/pcap: remove global variables Stephen Hemminger
2026-01-07 9:48 ` Marat Khalili
2026-01-06 18:26 ` [PATCH 07/12] net/pcap: avoid use of volatile Stephen Hemminger
2026-01-07 10:31 ` Marat Khalili
2026-01-06 18:26 ` [PATCH 08/12] net/pcap: optimize calculation of receive timestamp Stephen Hemminger
2026-01-07 10:58 ` Marat Khalili
2026-01-06 18:26 ` [PATCH 09/12] net/pcap: report receive clock Stephen Hemminger
2026-01-06 18:26 ` [PATCH 10/12] net/pcap: cleanup MAC address handling Stephen Hemminger
2026-01-06 18:26 ` [PATCH 11/12] net/pcap: support MAC address set Stephen Hemminger
2026-01-06 18:26 ` [PATCH 12/12] test: add test for pcap PMD Stephen Hemminger
2026-01-09 1:16 ` [PATCH v2 0/9] pcap: cleanup pcap PMD and add test Stephen Hemminger
2026-01-09 1:16 ` [PATCH v2 1/9] net/pcap: avoid using rte_malloc and rte_memcpy Stephen Hemminger
2026-01-09 1:16 ` [PATCH v2 2/9] net/pcap: support MTU set Stephen Hemminger
2026-01-09 1:16 ` [PATCH v2 3/9] net/pcap: use bool for flags Stephen Hemminger
2026-01-09 1:16 ` [PATCH v2 4/9] net/pcap: support Tx offloads Stephen Hemminger
2026-01-09 1:16 ` [PATCH v2 5/9] net/pcap: support nanosecond timestamp precision Stephen Hemminger
2026-01-09 1:16 ` [PATCH v2 6/9] net/pcap: remove global variables Stephen Hemminger
2026-01-09 1:16 ` [PATCH v2 7/9] net/pcap: avoid use of volatile Stephen Hemminger
2026-01-09 1:16 ` [PATCH v2 8/9] net/pcap: support MAC address set Stephen Hemminger
2026-01-09 1:16 ` [PATCH v2 9/9] test: add test for pcap PMD Stephen Hemminger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260106182823.192350-6-stephen@networkplumber.org \
--to=stephen@networkplumber.org \
--cc=dev@dpdk.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).