From: Dawid Wesierski <[email protected]> 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 <[email protected]> Signed-off-by: Dawid Wesierski <[email protected]> Signed-off-by: Stephen Hemminger <[email protected]> --- 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 <rte_bus_vdev.h> +#include <rte_errno.h> #include <rte_ethdev.h> #include <rte_ether.h> #include <rte_ip.h> @@ -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

