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 8B555606A320 for ; Tue, 1 Dec 2026 22:43:40 +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=CMNwZkhh Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 5610E4068E; Tue, 1 Dec 2026 23:43:08 +0100 (CET) Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) by mails.dpdk.org (Postfix) with ESMTPS id 0E3DA40664 for ; Tue, 1 Dec 2026 23:43:07 +0100 (CET) Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-3b51a9606e8so3983323a91.3 for ; Tue, 01 Dec 2026 14:43:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20260707.gappssmtp.com; s=20260707; t=1796164985; x=1796769785; 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:content-type; bh=+CmjF249SkS/vb3YR6c6C9T9DxP5Oli6KKpVnMVT2us=; b=CMNwZkhhl5UlQ845v4J+nW0h0wvZHoHKNOEP+fXmuWe5TQo49g1yHItjELGcfiP9Tp g57XCWOBVlV0kIOmnZGwJa3EjtyIdJBgEobJ7HLyLF9Ei80B2zn2JjqY5uzVphzRvhkr y1cjvuZ3j/klN8pdHvuxfcTp+mwmLo/SS9iBwzgR35NrrTroCwtWhRy1tCIoxcnNxsAu flJazKcMLYVcRGMIYKbWSon2h9iXPpq05/eK6mqpvHEVxLC0Yp5jD0Vezdi8ZcBGgd4o KTCRJ9140UqcZ4h9ZX9JOE2J4fZpeLMssJSJfOBC2tCLToFqAWMOUFflRPAuY8IPxmuj pfMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796164985; x=1796769785; 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:content-type; bh=+CmjF249SkS/vb3YR6c6C9T9DxP5Oli6KKpVnMVT2us=; b=Zy7ZN6faD+CS80hWyxC8oCiwMHCin38oRljqL9bwsojHy5Y4Lt3r2fzo5rGjqVAdx6 Yp18MZSEZe/9gOCchVOtjB8V2U2Vr67UzaZgfz79MfDvE9mEN0FMFIUsfWXsyxZ2to3G Gk9IwOYP39NPdKSMrYmXlgh553ns1mqp2F15I+qCXqtFdWjsByXvfesP0ZSnkGBD1PAI Q916iCVAtgwlZiDwqssl2ACJ7A1KSnXfSxOYAFuav7s8lOcFXGemvDw83OfBiDvt/A8y RbW/TkQ7WkCceocsOd6CHgMtz3DUzL3w2euhpFMVm6863Q7YRPxIIrha5KPcReyiuRpx SYhQ== X-Gm-Message-State: APuU4Z+5qEUuLwTLeAjY3IBlGNAlumO6q9ebkj+9gonURg1DUpJeO9A0 PsFuVh3UNeD1K+S8M+lWJ5P8Aix17F4dwMbpZ7/GTsamQrD+BqCTcnk5XRroJ3oS3O7Cm5t/iN/ LikiY X-Gm-Gg: AZFdTZch3RXQug68dLru8oE7a2Hj8A6n5/ISjRUv+RybIEVhMo50oBlR3hzOQr+qjS4 UEkxb5+AFht3BY29NC9qe6kAiufm3tFF9CjsfhH+vJgLZDVTWRXp2eAnYbbskXmbP1tffO4mJKn LbkNopWsQlsZLdYC3pIKtWv2LqG2uJQM9Yxrn7mtTwLDgSSb8ZZdPpppsxUTTY//K0GWi3dk/Ko SxwGcQz4tvzeN0udp7xiPhOS0lxMsiov+FwlQuJBZIKJHtI+D+eq6XdQi4I74qvqdmc7ZRbpLJm KqPBjNezIcffWaYLMNT+7qH5Glq4R2efbO9c/czMdx4j2mGWCOVAV2NA9L0nUXD71bYxXyAjuWx hJa5Hz8AMhy0foLCJAeat2u6ZlerohSLokfqSPLqcxHy9Xgd+hC7IFqCJQ3SLWfQt1GFhf/Tb1E f9++lBPwwO9e9qr7xUZx2n2HSksxcz3E2dNhnrlUXGxZvWDMoBDtuOQwno9neAYAYkRlcQryzJi RRY2vw7d4+2DMcv3w9FRxgz7omf2xtIOuYZlm6zfigMEgyzphvqBTCrQsiAd3BRcgglZiep0K+k TKE6rHDu61tW3EHMO+5wOPvbTk+gTxpox7Bjxsnt6tv2FomvvgmllRENXXIbiM8d0uu6 X-Received: by 2002:a17:90b:5546:b0:3b3:c44b:b779 with SMTP id 98e67ed59e1d1-3b41c935639mr41823604a91.46.1796164985388; Tue, 01 Dec 2026 14:43:05 -0800 (PST) Received: from phoenix.lan (204-195-112-43.wavecable.com. [204.195.112.43]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cde6cfebdbasm20739684a12.13.2026.12.01.14.43.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 14:43:05 -0800 (PST) From: Stephen Hemminger To: dev@dpdk.org Cc: Dawid Wesierski , Marek Kasiewicz , Stephen Hemminger , Reshma Pattan Subject: [PATCH 5/7] pcapng: add user-supplied timestamp support Date: Tue, 1 Dec 2026 14:41:38 -0800 Message-ID: <20261201224258.1962108-6-stephen@networkplumber.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261201224258.1962108-1-stephen@networkplumber.org> References: <20261201224258.1962108-1-stephen@networkplumber.org> X-Archived-By: 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-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 From: Dawid Wesierski An application may have a better time for a packet than the TSC at copy time, for example a hardware Rx timestamp, but rte_pcapng_copy() always timestamps with the TSC. Add rte_pcapng_copy_ts(), which takes the time of the packet in nanoseconds since the Unix epoch and stores it as given. rte_pcapng_copy() becomes a wrapper that converts the TSC with rte_tsc_to_ns() and passes the result, so both entry points share one implementation. rte_pcapng_copy() keeps its signature, so no API or ABI is broken and no deprecation notice is needed. The new function is experimental. Signed-off-by: Marek Kasiewicz Signed-off-by: Dawid Wesierski Signed-off-by: Stephen Hemminger --- app/test/test_pcapng.c | 89 ++++++++++++++++++++++++++ doc/guides/prog_guide/pcapng_lib.rst | 4 ++ doc/guides/rel_notes/release_27_03.rst | 7 ++ lib/pcapng/rte_pcapng.c | 39 +++++++---- lib/pcapng/rte_pcapng.h | 50 ++++++++++++++- 5 files changed, 177 insertions(+), 12 deletions(-) diff --git a/app/test/test_pcapng.c b/app/test/test_pcapng.c index 3e190888b6..7e19aa4096 100644 --- a/app/test/test_pcapng.c +++ b/app/test/test_pcapng.c @@ -18,6 +18,7 @@ #endif #include +#include #include #include #include @@ -673,6 +674,93 @@ test_write_before_open(void) return -1; } +static int +test_pcapng_timestamp(void) +{ + char file_name[PATH_MAX] = "/tmp/pcapng_test_XXXXXX.pcapng"; + char errbuf[PCAP_ERRBUF_SIZE]; + rte_pcapng_t *pcapng = NULL; + struct dummy_mbuf mbfs; + struct pcap_pkthdr h; + struct rte_mbuf *mc; + uint64_t pcap_ts, ns; + pcap_t *pcap; + int ret, tmp_fd; + + tmp_fd = mkstemps(file_name, strlen(".pcapng")); + if (tmp_fd == -1) { + perror("mkstemps() failure"); + return -1; + } + + pcapng = rte_pcapng_fdopen(tmp_fd, NULL, NULL, "pcapng_ts_test", NULL); + if (pcapng == NULL) { + printf("rte_pcapng_fdopen failed\n"); + close(tmp_fd); + goto fail; + } + + ret = rte_pcapng_add_interface(pcapng, port_id, DLT_EN10MB, NULL, NULL, NULL); + if (ret < 0) { + printf("can not add port %u\n", port_id); + goto fail; + } + + mbuf1_prepare(&mbfs); + + /* One second ahead, so that it cannot be the TSC of the copy */ + pcap_ts = current_timestamp() + NS_PER_S; + mc = rte_pcapng_copy_ts(port_id, 0, &mbfs.mb[0], mp, + rte_pktmbuf_pkt_len(&mbfs.mb[0]), + RTE_PCAPNG_DIRECTION_IN, "custom_ts", pcap_ts); + if (mc == NULL) { + printf("rte_pcapng_copy_ts failed\n"); + goto fail; + } + + ret = rte_pcapng_write_packets(pcapng, &mc, 1); + rte_pktmbuf_free(mc); + if (ret <= 0) { + printf("Write of custom timestamp packet failed\n"); + goto fail; + } + + rte_pcapng_close(pcapng); + pcapng = NULL; + + pcap = pcap_open_offline_with_tstamp_precision(file_name, + PCAP_TSTAMP_PRECISION_NANO, + errbuf); + if (pcap == NULL) { + printf("pcap_open_offline failed: %s\n", errbuf); + goto fail; + } + + if (pcap_next(pcap, &h) == NULL) { + printf("No packets in file\n"); + pcap_close(pcap); + goto fail; + } + pcap_close(pcap); + + /* With nanosecond precision, tv_usec holds nanoseconds. */ + ns = (uint64_t)h.ts.tv_sec * NS_PER_S + h.ts.tv_usec; + if (ns != pcap_ts) { + printf("Timestamp mismatch: expected %"PRIu64" got %"PRIu64"\n", + pcap_ts, ns); + goto fail; + } + + remove(file_name); + return 0; + +fail: + if (pcapng) + rte_pcapng_close(pcapng); + remove(file_name); + return -1; +} + static void test_cleanup(void) { @@ -689,6 +777,7 @@ 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_pcapng_timestamp), TEST_CASES_END() } }; diff --git a/doc/guides/prog_guide/pcapng_lib.rst b/doc/guides/prog_guide/pcapng_lib.rst index d18ebdbd1f..598cc6c93b 100644 --- a/doc/guides/prog_guide/pcapng_lib.rst +++ b/doc/guides/prog_guide/pcapng_lib.rst @@ -32,6 +32,10 @@ to determine the lower bound based on MTU. Collecting packets is done in two parts. The function ``rte_pcapng_copy`` is used to format and copy mbuf data and ``rte_pcapng_write_packets`` writes a burst of packets to the output file. +The timestamp of each packet is the TSC at the time of ``rte_pcapng_copy``. +An application that has a better time for the packet, +for example a hardware timestamp, +can give it in nanoseconds to ``rte_pcapng_copy_ts`` instead. The function ``rte_pcapng_write_stats`` can be used to write statistics information into the output file. diff --git a/doc/guides/rel_notes/release_27_03.rst b/doc/guides/rel_notes/release_27_03.rst index 63bb5ad42d..726c5b1e3f 100644 --- a/doc/guides/rel_notes/release_27_03.rst +++ b/doc/guides/rel_notes/release_27_03.rst @@ -61,6 +61,13 @@ New Features to convert a TSC value into nanoseconds since 1/1/1970, using a reference point recorded during EAL initialization. +* **Added pcapng support for user-supplied timestamps.** + + Added the experimental ``rte_pcapng_copy_ts()`` function, + which takes the time of the packet in nanoseconds since the Unix epoch, + for example a hardware Rx timestamp. + ``rte_pcapng_copy()`` is unchanged and still uses the TSC at copy time. + Removed Items ------------- diff --git a/lib/pcapng/rte_pcapng.c b/lib/pcapng/rte_pcapng.c index f09253ab94..d6f50c2fc0 100644 --- a/lib/pcapng/rte_pcapng.c +++ b/lib/pcapng/rte_pcapng.c @@ -473,20 +473,25 @@ pcapng_vlan_insert(struct rte_mbuf *m, uint16_t ether_type, uint16_t tci) * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */ -/* Make a copy of original mbuf with pcapng header and options */ -RTE_EXPORT_SYMBOL(rte_pcapng_copy) +/* + * Make a copy of original mbuf with pcapng header and options. + * + * The timestamp is the caller's time of the packet, in nanoseconds + * since the Unix epoch, and is stored as given. + */ +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pcapng_copy_ts, 27.03) struct rte_mbuf * -rte_pcapng_copy(uint16_t port_id, uint32_t queue, - const struct rte_mbuf *md, - struct rte_mempool *mp, - uint32_t length, - enum rte_pcapng_direction direction, - const char *comment) +rte_pcapng_copy_ts(uint16_t port_id, uint32_t queue, + const struct rte_mbuf *md, + struct rte_mempool *mp, + uint32_t length, + enum rte_pcapng_direction direction, + const char *comment, + uint64_t timestamp) { struct pcapng_enhance_packet_block *epb; uint32_t orig_len, pkt_len, padding, flags; struct pcapng_option *opt; - uint64_t timestamp; uint16_t optlen; struct rte_mbuf *mc; bool rss_hash; @@ -618,8 +623,6 @@ 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 nanoseconds */ - timestamp = rte_tsc_to_ns(rte_get_tsc_cycles()); epb->timestamp_hi = timestamp >> 32; epb->timestamp_lo = (uint32_t)timestamp; @@ -636,6 +639,20 @@ rte_pcapng_copy(uint16_t port_id, uint32_t queue, return NULL; } +RTE_EXPORT_SYMBOL(rte_pcapng_copy) +struct rte_mbuf * +rte_pcapng_copy(uint16_t port_id, uint32_t queue, + const struct rte_mbuf *md, + struct rte_mempool *mp, + uint32_t length, + enum rte_pcapng_direction direction, + const char *comment) +{ + return rte_pcapng_copy_ts(port_id, queue, md, mp, length, + direction, comment, + rte_tsc_to_ns(rte_get_tsc_cycles())); +} + /* Write pre-formatted packets to file. */ RTE_EXPORT_SYMBOL(rte_pcapng_write_packets) ssize_t diff --git a/lib/pcapng/rte_pcapng.h b/lib/pcapng/rte_pcapng.h index d8d328f710..647c9d6c2b 100644 --- a/lib/pcapng/rte_pcapng.h +++ b/lib/pcapng/rte_pcapng.h @@ -133,12 +133,60 @@ enum rte_pcapng_direction { * @return * - The pointer to the new mbuf formatted for pcapng_write * - NULL on error such as invalid port or out of memory. + * + * The packet is timestamped with the TSC at the time of the copy, + * converted to epoch time. + * Use rte_pcapng_copy_ts() to give the time of the packet directly. */ struct rte_mbuf * rte_pcapng_copy(uint16_t port_id, uint32_t queue, const struct rte_mbuf *m, struct rte_mempool *mp, uint32_t length, - enum rte_pcapng_direction direction, const char *comment); + enum rte_pcapng_direction direction, + const char *comment); + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Format an mbuf for writing to file, with the time of the packet. + * + * Same as rte_pcapng_copy(), except that the caller gives the time of + * the packet instead of it being taken from the TSC at copy time. + * + * @param port_id + * The Ethernet port on which packet was received + * or is going to be transmitted. + * @param queue + * The queue on the Ethernet port where packet was received + * or is going to be transmitted. + * @param mp + * The mempool from which the "clone" mbufs are allocated. + * @param m + * The mbuf to copy + * @param length + * The upper limit on bytes to copy. Passing UINT32_MAX + * means all data (after offset). + * @param direction + * The direction of the packet: receive, transmit or unknown. + * @param comment + * Optional per packet comment. + * Truncated to UINT16_MAX characters. + * @param timestamp + * Time of the packet in nanoseconds since the Unix epoch, + * for example a hardware timestamp. + * + * @return + * - The pointer to the new mbuf formatted for pcapng_write + * - NULL on error such as invalid port or out of memory. + */ +__rte_experimental +struct rte_mbuf * +rte_pcapng_copy_ts(uint16_t port_id, uint32_t queue, + const struct rte_mbuf *m, struct rte_mempool *mp, + uint32_t length, + enum rte_pcapng_direction direction, + const char *comment, uint64_t timestamp); /** -- 2.53.0