Add a sample application to measure the precision of the send-on-timestamp offload (Tx packet pacing, also known as Earliest TxTime First).
A generator paces packets on a Tx port. Each packet gets a launch time that tracks the device clock, read with rte_eth_read_clock() and extrapolated with the TSC, and is handed to the driver a fixed lead time before it. The clock is read on its own lcore when one is available, as a read may be slow, for example a request to the PF on a VF. Packets are either evenly spaced or sent in groups of closely spaced packets, and may be spread over several Tx queues, as a Tx queue fetches a packet only when its launch time expires. The receiver either runs on a second port, comparing the hardware Rx timestamp of every packet with the launch time carried in the payload, or on another host, identifying the frames by their EtherType. The generator reports late, held back and stalled transmissions. Signed-off-by: Soumyadeep Hore <[email protected]> --- MAINTAINERS | 4 + examples/meson.build | 1 + examples/txpp/main.c | 800 ++++++++++++++++++++++++++++++++++++++ examples/txpp/meson.build | 16 + 4 files changed, 821 insertions(+) create mode 100644 examples/txpp/main.c create mode 100644 examples/txpp/meson.build diff --git a/MAINTAINERS b/MAINTAINERS index 096232e5d3..d8c4353e6a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2119,6 +2119,10 @@ M: John McNamara <[email protected]> F: examples/skeleton/ F: doc/guides/sample_app_ug/skeleton.rst +Tx packet pacing example +M: Soumyadeep Hore <[email protected]> +F: examples/txpp/ + VMDq examples F: examples/vmdq/ F: doc/guides/sample_app_ug/vmdq_forwarding.rst diff --git a/examples/meson.build b/examples/meson.build index 40c92cbd1a..fa2e2e8218 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -50,6 +50,7 @@ all_examples = [ 'service_cores', 'skeleton', 'timer', + 'txpp', 'vdpa', 'vhost', 'vhost_blk', diff --git a/examples/txpp/main.c b/examples/txpp/main.c new file mode 100644 index 0000000000..68a66f05df --- /dev/null +++ b/examples/txpp/main.c @@ -0,0 +1,800 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2026 Intel Corporation + */ + +/* + * Tx Packet Pacing (Earliest TxTime First / SO_TXTIME) precision tester. + * + * A generator paces packets on a Tx port using the send-on-timestamp offload: + * each packet is given a hardware launch time that tracks the device PHC, and + * is handed to hardware only a fixed lead time before that launch time so the + * schedule never races ahead of real time. + * + * A receiver on a separate Rx port reads the hardware Rx timestamp of every + * packet and compares it with the scheduled launch time carried in the + * payload, reporting the launch error (bias and jitter) and the inter-arrival + * accuracy. + * + * When both ports are on the same adapter (shared PHC) the error is absolute; + * on different adapters it carries a constant PHC offset, so the jitter and the + * inter-arrival interval are the meaningful precision metrics. + */ + +#include <inttypes.h> +#include <getopt.h> +#include <signal.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <math.h> + +#include <rte_eal.h> +#include <rte_ethdev.h> +#include <rte_ether.h> +#include <rte_cycles.h> +#include <rte_lcore.h> +#include <rte_mbuf.h> +#include <rte_mbuf_dyn.h> +#include <rte_seqlock.h> + +#define RX_RING_SIZE 1024 +#define TX_RING_SIZE 1024 +#define NUM_MBUFS 16383 +#define MBUF_CACHE_SIZE 256 +#define BURST_SIZE 32 +#define TX_BURST_MAX 256 +#define TX_QUEUES_MAX 16 +#define NS_PER_SEC 1000000000ULL +#define LINK_WAIT_MS 10000 +#define LINK_SETTLE_MS 1000 +#define CLOCK_SYNC_MS 100 +#define CLOCK_READ_SLACK_US 10 + +#define TXPP_MAGIC 0x54585050u /* "TXPP" */ +#define TXPP_ETHERTYPE 0x88B5 /* local experimental EtherType 1 */ + +/* Hardware launch-time granularity (128 ns) used only for the histogram. */ +#define TXPP_RES_NS 128 +#define HIST_BINS 16 + +struct txpp_payload { + uint32_t magic; + uint32_t seq; + uint64_t sched_ns; /* scheduled launch time, device PHC nanoseconds */ +}; + +struct txpp_cfg { + uint16_t tx_port; + uint16_t rx_port; + uint64_t interval_ns; /* requested inter-packet spacing */ + uint64_t period_ns; /* start-to-start spacing of packet groups */ + uint32_t group; /* packets per group, sent interval_ns apart */ + uint64_t lead_ns; /* how early a packet is given to hardware */ + uint64_t offset_ns; /* initial lead of the first launch time */ + uint64_t count; /* number of packets, 0 means run until stopped */ + uint64_t cal_ns; /* fixed latency subtracted from the error */ + uint16_t pkt_size; + uint16_t tx_desc; /* Tx ring size, bounds lead / interval */ + uint16_t tx_rs; /* Tx RS and free threshold, 0 = PMD default */ + uint16_t min_burst; /* packets handed to the driver at once */ + uint16_t tx_queues; /* consecutive packets use consecutive queues */ + bool tx_only; /* receiver runs on another host */ +}; + +struct rx_stats { + uint64_t rx_pkts; + uint64_t matched; + uint64_t no_ts; + uint64_t seq_gaps; + /* launch error (ns) running stats via Welford */ + uint64_t n; + double mean; + double m2; + int64_t err_min; + int64_t err_max; + /* inter-arrival interval error (ns) */ + uint64_t iv_n; + double iv_mean; + double iv_m2; + uint64_t hist[HIST_BINS]; /* |error| in TXPP_RES_NS buckets */ +}; + +static struct txpp_cfg cfg = { + .tx_port = 0, + .rx_port = 1, + .interval_ns = 10000, /* 10 us */ + .group = 1, + .lead_ns = 50000, /* 50 us */ + .offset_ns = 1000000, /* 1 ms */ + .count = 0, + .cal_ns = 0, + .pkt_size = 64, + .tx_desc = TX_RING_SIZE, + .min_burst = 1, + .tx_queues = 1, +}; + +static volatile bool force_quit; +static int tx_ts_offset = -1; +static uint64_t tx_ts_flag; +static int rx_ts_offset = -1; +static uint64_t rx_ts_flag; +static uint64_t tx_sent; +static uint64_t tx_late; /* handed to the driver after its launch time */ +static uint64_t tx_ring_full; /* bursts not fully accepted at once */ +static uint64_t tx_stalls; /* Tx loop iterations further apart than 100 us */ +static uint64_t tx_max_stall; /* longest gap between Tx loop iterations, TSC */ +static struct rx_stats stats; +static struct rte_ether_addr tx_mac; +static struct rte_ether_addr rx_mac = { + .addr_bytes = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, +}; +static bool rx_mac_set; + +static void +signal_handler(int signum) +{ + if (signum == SIGINT || signum == SIGTERM) + force_quit = true; +} + +static int +port_init(uint16_t port, struct rte_mempool *mbuf_pool, bool is_tx) +{ + struct rte_eth_conf port_conf; + struct rte_eth_dev_info dev_info; + struct rte_eth_txconf txconf; + uint16_t nb_rxd = RX_RING_SIZE; + uint16_t nb_txd = cfg.tx_desc; + uint16_t nb_txq = is_tx ? cfg.tx_queues : 1; + uint16_t q; + int retval; + + if (!rte_eth_dev_is_valid_port(port)) + return -1; + + memset(&port_conf, 0, sizeof(port_conf)); + retval = rte_eth_dev_info_get(port, &dev_info); + if (retval != 0) + return retval; + + if (nb_txq > dev_info.max_tx_queues) { + printf("Port %u: only %u Tx queues\n", port, + dev_info.max_tx_queues); + return -1; + } + if (is_tx) { + if (!(dev_info.tx_offload_capa & + RTE_ETH_TX_OFFLOAD_SEND_ON_TIMESTAMP)) { + printf("Port %u: no SEND_ON_TIMESTAMP offload\n", port); + return -1; + } + port_conf.txmode.offloads |= + RTE_ETH_TX_OFFLOAD_SEND_ON_TIMESTAMP; + } else { + if (!(dev_info.rx_offload_capa & RTE_ETH_RX_OFFLOAD_TIMESTAMP)) { + printf("Port %u: no Rx TIMESTAMP offload\n", port); + return -1; + } + port_conf.rxmode.offloads |= RTE_ETH_RX_OFFLOAD_TIMESTAMP; + } + + retval = rte_eth_dev_configure(port, 1, nb_txq, &port_conf); + if (retval != 0) + return retval; + + retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd); + if (retval != 0) + return retval; + + retval = rte_eth_rx_queue_setup(port, 0, nb_rxd, + rte_eth_dev_socket_id(port), NULL, mbuf_pool); + if (retval < 0) + return retval; + + txconf = dev_info.default_txconf; + txconf.offloads = port_conf.txmode.offloads; + if (cfg.tx_rs != 0) { + txconf.tx_rs_thresh = cfg.tx_rs; + txconf.tx_free_thresh = cfg.tx_rs; + } + for (q = 0; q < nb_txq; q++) { + retval = rte_eth_tx_queue_setup(port, q, nb_txd, + rte_eth_dev_socket_id(port), &txconf); + if (retval < 0) + return retval; + } + + retval = rte_eth_dev_start(port); + if (retval < 0) + return retval; + + if (!is_tx) + rte_eth_promiscuous_enable(port); + + return 0; +} + +/* Packets sent before the link is up on both ends are lost on the wire. */ +static int +wait_link_up(uint16_t port) +{ + struct rte_eth_link link; + int i; + + for (i = 0; i < LINK_WAIT_MS / 100 && !force_quit; i++) { + if (rte_eth_link_get_nowait(port, &link) == 0 && + link.link_status == RTE_ETH_LINK_UP) { + printf("Port %u link up, %u Mbps\n", port, link.link_speed); + /* give the link partner time to finish its own link up */ + rte_delay_ms(LINK_SETTLE_MS); + return 0; + } + rte_delay_ms(100); + } + printf("Port %u link is down\n", port); + return -1; +} + +/* Device PHC paired with the TSC at the middle of the clock read. */ +static struct { + rte_seqlock_t lock; + uint64_t phc; + uint64_t tsc; +} clk; +static uint64_t clk_reads; +static uint64_t clk_rejected; +static uint64_t clk_min_read; /* fastest clock read, TSC cycles */ +static bool clk_lcore; /* the clock is refreshed by its own lcore */ + +/* + * Read the device PHC and anchor it to the TSC for cheap extrapolation. + * A clock read may be a slow request (e.g. to the PF on a VF), so the + * anchor is the middle of the read, and reads much slower than the + * fastest one are too far from their middle to be used. + */ +static int +clock_sync(void) +{ + uint64_t phc, t0, rd; + int ret; + + /* ordered TSC reads, so the device read is inside [t0, t0 + rd] */ + t0 = rte_rdtsc_precise(); + ret = rte_eth_read_clock(cfg.tx_port, &phc); + rd = rte_rdtsc_precise() - t0; + if (ret != 0) + return ret; + + clk_reads++; + if (clk_min_read == 0 || rd < clk_min_read) + clk_min_read = rd; + if (clk.tsc != 0 && + rd > 2 * clk_min_read + rte_get_tsc_hz() / US_PER_S * CLOCK_READ_SLACK_US) { + clk_rejected++; + return 0; + } + + rte_seqlock_write_lock(&clk.lock); + clk.phc = phc; + clk.tsc = t0 + rd / 2; + rte_seqlock_write_unlock(&clk.lock); + return 0; +} + +static inline uint64_t +phc_now(uint64_t tsc_hz) +{ + uint64_t phc, tsc; + uint32_t sn; + + do { + sn = rte_seqlock_read_begin(&clk.lock); + phc = clk.phc; + tsc = clk.tsc; + } while (rte_seqlock_read_retry(&clk.lock, sn)); + + return phc + (rte_rdtsc() - tsc) * NS_PER_SEC / tsc_hz; +} + +/* Refresh the clock anchor so the Tx loop never waits for a clock read. */ +static void +clock_loop(void) +{ + while (!force_quit) { + clock_sync(); + rte_delay_us_sleep(CLOCK_SYNC_MS * 1000); + } +} + +static void +build_packet(struct rte_mbuf *m, uint32_t seq, uint64_t launch) +{ + struct rte_ether_hdr *eth; + struct txpp_payload *pl; + char *data; + + m->data_len = cfg.pkt_size; + m->pkt_len = cfg.pkt_size; + + data = rte_pktmbuf_mtod(m, char *); + memset(data, 0, cfg.pkt_size); + + eth = (struct rte_ether_hdr *)data; + rte_ether_addr_copy(&rx_mac, ð->dst_addr); + rte_ether_addr_copy(&tx_mac, ð->src_addr); + eth->ether_type = rte_cpu_to_be_16(TXPP_ETHERTYPE); + + pl = (struct txpp_payload *)(data + sizeof(*eth)); + pl->magic = TXPP_MAGIC; + pl->seq = seq; + pl->sched_ns = launch; + + /* program the hardware launch time */ + *RTE_MBUF_DYNFIELD(m, tx_ts_offset, uint64_t *) = launch; + m->ol_flags |= tx_ts_flag; +} + +static inline uint64_t +launch_time(uint64_t start, uint64_t seq) +{ + return start + seq / cfg.group * cfg.period_ns + + seq % cfg.group * cfg.interval_ns; +} + +static uint16_t +tx_queue_burst(uint16_t queue, struct rte_mbuf **pkts, uint16_t nb) +{ + uint16_t sent; + + sent = rte_eth_tx_burst(cfg.tx_port, queue, pkts, nb); + if (sent < nb) + tx_ring_full++; + /* the rest was never given to hardware: finish the hand-off */ + while (sent < nb) { + sent += rte_eth_tx_burst(cfg.tx_port, queue, pkts + sent, + nb - sent); + if (force_quit && sent < nb) { + rte_pktmbuf_free_bulk(pkts + sent, nb - sent); + break; + } + } + return sent; +} + +static int +tx_loop(void *arg) +{ + struct rte_mempool *pool = arg; + uint64_t tsc_hz = rte_get_tsc_hz(); + uint64_t resync = rte_rdtsc(); + uint64_t last = resync; + uint64_t start; + uint32_t seq = 0; + + start = phc_now(tsc_hz) + cfg.offset_ns; + + while (!force_quit && (cfg.count == 0 || seq < cfg.count)) { + struct rte_mbuf *pkts[TX_BURST_MAX]; + uint16_t nb, sent; + uint64_t now, tsc; + + /* a stalled loop hands packets over late */ + tsc = rte_rdtsc(); + if (tsc - last > tx_max_stall) + tx_max_stall = tsc - last; + if (tsc - last > tsc_hz / 10000) + tx_stalls++; + last = tsc; + + /* without a clock lcore, keep the anchor fresh from here */ + if (!clk_lcore && rte_rdtsc() - resync > + tsc_hz / 1000 * CLOCK_SYNC_MS) { + clock_sync(); + resync = rte_rdtsc(); + } + + now = phc_now(tsc_hz); + + /* hand over every packet due within the lead time in one burst */ + for (nb = 0; nb < RTE_MAX(cfg.min_burst, BURST_SIZE) && + (cfg.count == 0 || seq + nb < cfg.count); nb++) { + uint64_t t = launch_time(start, seq + nb); + + if ((int64_t)(t - cfg.lead_ns - now) > 0) + break; + } + /* fewer, larger bursts mean fewer doorbells and descriptor fetches */ + if (nb < cfg.min_burst && + (cfg.count == 0 || seq + nb < cfg.count)) + nb = 0; + if (nb == 0) { + rte_pause(); + continue; + } + if (rte_pktmbuf_alloc_bulk(pool, pkts, nb) != 0) + continue; + + for (sent = 0; sent < nb; sent++) { + uint64_t t = launch_time(start, seq + sent); + + if ((int64_t)(t - now) <= 0) + tx_late++; + build_packet(pkts[sent], seq + sent, t); + } + + if (cfg.tx_queues == 1) { + tx_sent += tx_queue_burst(0, pkts, nb); + } else { + static struct rte_mbuf *qpkts[TX_QUEUES_MAX][TX_BURST_MAX]; + uint16_t qn[TX_QUEUES_MAX] = { 0 }; + uint16_t q; + + for (sent = 0; sent < nb; sent++) { + q = (seq + sent) % cfg.tx_queues; + qpkts[q][qn[q]++] = pkts[sent]; + } + for (q = 0; q < cfg.tx_queues; q++) + if (qn[q] != 0) + tx_sent += tx_queue_burst(q, qpkts[q], + qn[q]); + } + seq += nb; + } + force_quit = true; + return 0; +} + +static void +account(int64_t err, uint64_t rx_ts, uint32_t seq, uint64_t sched) +{ + static uint64_t prev_rx; + static uint64_t prev_sched; + static uint32_t prev_seq; + static bool have_prev; + double d; + uint64_t bin; + + /* Welford update of the launch error */ + stats.n++; + d = (double)err - stats.mean; + stats.mean += d / stats.n; + stats.m2 += d * ((double)err - stats.mean); + if (stats.n == 1 || err < stats.err_min) + stats.err_min = err; + if (stats.n == 1 || err > stats.err_max) + stats.err_max = err; + + bin = (uint64_t)llabs(err) / TXPP_RES_NS; + if (bin >= HIST_BINS) + bin = HIST_BINS - 1; + stats.hist[bin]++; + + if (have_prev) { + int64_t iv = (int64_t)(rx_ts - prev_rx) - + (int64_t)(sched - prev_sched); + + stats.iv_n++; + d = (double)iv - stats.iv_mean; + stats.iv_mean += d / stats.iv_n; + stats.iv_m2 += d * ((double)iv - stats.iv_mean); + + if (seq != prev_seq + 1) + stats.seq_gaps++; + } + prev_rx = rx_ts; + prev_sched = sched; + prev_seq = seq; + have_prev = true; +} + +static int +rx_loop(void *arg __rte_unused) +{ + while (!force_quit) { + struct rte_mbuf *pkts[BURST_SIZE]; + uint16_t nb, i; + + nb = rte_eth_rx_burst(cfg.rx_port, 0, pkts, BURST_SIZE); + for (i = 0; i < nb; i++) { + struct rte_mbuf *m = pkts[i]; + struct rte_ether_hdr *eth; + struct txpp_payload *pl; + uint64_t rx_ts; + int64_t err; + + stats.rx_pkts++; + eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *); + pl = (struct txpp_payload *)(eth + 1); + + if (rte_pktmbuf_data_len(m) < sizeof(*eth) + sizeof(*pl) || + pl->magic != TXPP_MAGIC) { + rte_pktmbuf_free(m); + continue; + } + stats.matched++; + + if (!(m->ol_flags & rx_ts_flag)) { + stats.no_ts++; + rte_pktmbuf_free(m); + continue; + } + + rx_ts = *RTE_MBUF_DYNFIELD(m, rx_ts_offset, uint64_t *); + err = (int64_t)(rx_ts - pl->sched_ns) - + (int64_t)cfg.cal_ns; + account(err, rx_ts, pl->seq, pl->sched_ns); + rte_pktmbuf_free(m); + } + } + return 0; +} + +static void +print_stats(void) +{ + double jitter = stats.n > 1 ? sqrt(stats.m2 / (stats.n - 1)) : 0; + double iv_jit = stats.iv_n > 1 ? + sqrt(stats.iv_m2 / (stats.iv_n - 1)) : 0; + struct rte_eth_stats st; + int i; + + printf("\n==== Tx Packet Pacing results ====\n"); + printf("Tx sent : %" PRIu64 "\n", tx_sent); + if (rte_eth_stats_get(cfg.tx_port, &st) == 0) + printf("NIC Tx packets : %" PRIu64 " (errors %" PRIu64 ")\n", + st.opackets, st.oerrors); + printf("Tx late : %" PRIu64 + " (handed to the driver after the launch time)\n", tx_late); + printf("Tx ring full : %" PRIu64 + " (bursts that waited for Tx ring room)\n", tx_ring_full); + printf("Tx loop stalls : %" PRIu64 " over 100 us, longest %.1f us\n", + tx_stalls, (double)tx_max_stall * 1e6 / rte_get_tsc_hz()); + printf("Clock reads : %" PRIu64 " (%" PRIu64 + " slow ones ignored, fastest %.1f us, %s)\n", clk_reads, + clk_rejected, (double)clk_min_read * 1e6 / rte_get_tsc_hz(), + clk_lcore ? "own lcore" : "Tx lcore"); + if (cfg.tx_only) + return; + printf("Rx received : %" PRIu64 " (matched %" PRIu64 + ", no-timestamp %" PRIu64 ")\n", + stats.rx_pkts, stats.matched, stats.no_ts); + printf("Lost (sent-match) : %" PRId64 "\n", + (int64_t)tx_sent - (int64_t)stats.matched); + if (stats.n == 0) { + printf("No timestamped samples collected.\n"); + return; + } + printf("Launch error (ns) : bias %.1f jitter %.1f min %" PRId64 + " max %" PRId64 "\n", + stats.mean, jitter, stats.err_min, stats.err_max); + printf("Interval error(ns): bias %.1f jitter %.1f\n", + stats.iv_mean, iv_jit); + printf("Sequence gaps : %" PRIu64 "\n", stats.seq_gaps); + printf("|error| histogram (%d ns buckets):\n", TXPP_RES_NS); + for (i = 0; i < HIST_BINS; i++) { + if (stats.hist[i] == 0) + continue; + printf(" [%4d-%4d) ns : %" PRIu64 "\n", + i * TXPP_RES_NS, (i + 1) * TXPP_RES_NS, stats.hist[i]); + } + if (cfg.tx_port != cfg.rx_port) + printf("Note: Tx and Rx are different ports; if they are on " + "different adapters the bias includes a constant PHC " + "offset - use jitter and interval error as the precision " + "metric.\n"); +} + +static void +usage(const char *prog) +{ + printf("Usage: %s [EAL args] -- [options]\n" + " -t PORT Tx port id (default 0)\n" + " -r PORT Rx port id (default 1)\n" + " -i NS inter-packet interval in ns (default 10000)\n" + " -g N packets per group, sent -i apart (default 1)\n" + " -p NS start-to-start group period in ns " + "(default N * interval)\n" + " -l NS lead time packet is given to HW before launch " + "(default 50000)\n" + " -o NS initial lead of first launch time (default 1000000)\n" + " -n COUNT packets to send, 0 = until stopped (default 0)\n" + " -s BYTES packet size (default 64)\n" + " -q DESC Tx ring size, must hold lead / interval packets " + "(default 1024)\n" + " -R N Tx RS and free threshold (default PMD default)\n" + " -b N minimum packets per burst, up to 256 (default 1)\n" + " -Q N Tx queues, packet n uses queue n %% N, up to 16 " + "(default 1)\n" + " -c NS fixed latency subtracted from error (default 0)\n" + " -T Tx only, the receiver runs on another host\n" + " -d MAC destination MAC in Tx only mode (default broadcast)\n" + " -h show this help\n", prog); +} + +static int +parse_args(int argc, char **argv) +{ + int opt; + + while ((opt = getopt(argc, argv, "t:r:i:g:p:l:o:n:s:q:R:b:Q:c:Td:h")) != -1) { + switch (opt) { + case 't': + cfg.tx_port = (uint16_t)strtoul(optarg, NULL, 0); + break; + case 'r': + cfg.rx_port = (uint16_t)strtoul(optarg, NULL, 0); + break; + case 'i': + cfg.interval_ns = strtoull(optarg, NULL, 0); + break; + case 'g': + cfg.group = (uint32_t)strtoul(optarg, NULL, 0); + break; + case 'p': + cfg.period_ns = strtoull(optarg, NULL, 0); + break; + case 'l': + cfg.lead_ns = strtoull(optarg, NULL, 0); + break; + case 'o': + cfg.offset_ns = strtoull(optarg, NULL, 0); + break; + case 'n': + cfg.count = strtoull(optarg, NULL, 0); + break; + case 's': + cfg.pkt_size = (uint16_t)strtoul(optarg, NULL, 0); + break; + case 'q': + cfg.tx_desc = (uint16_t)strtoul(optarg, NULL, 0); + break; + case 'R': + cfg.tx_rs = (uint16_t)strtoul(optarg, NULL, 0); + break; + case 'b': + cfg.min_burst = RTE_MIN((uint16_t)strtoul(optarg, NULL, 0), + TX_BURST_MAX); + break; + case 'Q': + cfg.tx_queues = (uint16_t)strtoul(optarg, NULL, 0); + if (cfg.tx_queues == 0 || cfg.tx_queues > TX_QUEUES_MAX) { + printf("Tx queues must be 1-%d\n", TX_QUEUES_MAX); + return -1; + } + break; + case 'c': + cfg.cal_ns = strtoull(optarg, NULL, 0); + break; + case 'T': + cfg.tx_only = true; + break; + case 'd': + if (rte_ether_unformat_addr(optarg, &rx_mac) != 0) { + printf("invalid MAC address %s\n", optarg); + return -1; + } + rx_mac_set = true; + break; + case 'h': + default: + usage(argv[0]); + return -1; + } + } + + if (cfg.pkt_size < sizeof(struct rte_ether_hdr) + + sizeof(struct txpp_payload)) + cfg.pkt_size = sizeof(struct rte_ether_hdr) + + sizeof(struct txpp_payload); + if (cfg.group == 0) + cfg.group = 1; + if (cfg.period_ns == 0) + cfg.period_ns = cfg.group * cfg.interval_ns; + if (cfg.period_ns < cfg.group * cfg.interval_ns) { + printf("group period must be at least packets * interval\n"); + return -1; + } + return 0; +} + +int +main(int argc, char **argv) +{ + struct rte_mempool *pool; + unsigned int lcore; + int ret; + + ret = rte_eal_init(argc, argv); + if (ret < 0) + rte_exit(EXIT_FAILURE, "EAL init failed\n"); + argc -= ret; + argv += ret; + + if (parse_args(argc, argv) != 0) + return 1; + + if (rte_eth_dev_count_avail() < 1) + rte_exit(EXIT_FAILURE, "No Ethernet ports available\n"); + if (!cfg.tx_only && rte_lcore_count() < 2) + rte_exit(EXIT_FAILURE, "Need at least 2 lcores (Tx and Rx)\n"); + + force_quit = false; + signal(SIGINT, signal_handler); + signal(SIGTERM, signal_handler); + + /* every Tx ring may hold a full ring of mbufs */ + pool = rte_pktmbuf_pool_create("TXPP_POOL", + RTE_MAX(NUM_MBUFS, cfg.tx_queues * cfg.tx_desc + + RX_RING_SIZE + 4 * TX_BURST_MAX), + MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, + rte_socket_id()); + if (pool == NULL) + rte_exit(EXIT_FAILURE, "mbuf pool create failed\n"); + + /* The send-on-timestamp and Rx timestamp share the same mbuf field. */ + if (rte_mbuf_dyn_tx_timestamp_register(&tx_ts_offset, &tx_ts_flag) != 0) + rte_exit(EXIT_FAILURE, "Tx timestamp dynfield register failed\n"); + if (rte_mbuf_dyn_rx_timestamp_register(&rx_ts_offset, &rx_ts_flag) != 0) + rte_exit(EXIT_FAILURE, "Rx timestamp dynfield register failed\n"); + + if (port_init(cfg.tx_port, pool, true) != 0) + rte_exit(EXIT_FAILURE, "Tx port %u init failed\n", cfg.tx_port); + rte_eth_macaddr_get(cfg.tx_port, &tx_mac); + + if (cfg.tx_only) { + if (wait_link_up(cfg.tx_port) != 0) + rte_exit(EXIT_FAILURE, "Tx port %u link down\n", + cfg.tx_port); + printf("Tx port %u -> " RTE_ETHER_ADDR_PRT_FMT + ", interval %" PRIu64 " ns, group %u every %" PRIu64 + " ns, lead %" PRIu64 " ns, %u Tx queues\n", + cfg.tx_port, RTE_ETHER_ADDR_BYTES(&rx_mac), + cfg.interval_ns, cfg.group, cfg.period_ns, cfg.lead_ns, + cfg.tx_queues); + lcore = rte_get_next_lcore(-1, 1, 0); + } else { + if (port_init(cfg.rx_port, pool, false) != 0) + rte_exit(EXIT_FAILURE, "Rx port %u init failed\n", + cfg.rx_port); + if (!rx_mac_set) + rte_eth_macaddr_get(cfg.rx_port, &rx_mac); + if (wait_link_up(cfg.tx_port) != 0 || + wait_link_up(cfg.rx_port) != 0) + rte_exit(EXIT_FAILURE, "link down\n"); + + printf("Tx port %u -> Rx port %u, interval %" PRIu64 + " ns, lead %" PRIu64 " ns\n", + cfg.tx_port, cfg.rx_port, cfg.interval_ns, cfg.lead_ns); + + lcore = rte_get_next_lcore(-1, 1, 0); + rte_eal_remote_launch(rx_loop, NULL, lcore); + lcore = rte_get_next_lcore(lcore, 1, 0); + } + + rte_seqlock_init(&clk.lock); + if (clock_sync() != 0) + rte_exit(EXIT_FAILURE, "failed to read the device clock\n"); + + if (lcore < RTE_MAX_LCORE) { + clk_lcore = true; + rte_eal_remote_launch(tx_loop, pool, lcore); + clock_loop(); + } else { + tx_loop(pool); + } + + rte_eal_mp_wait_lcore(); + + print_stats(); + + rte_eth_dev_stop(cfg.tx_port); + rte_eth_dev_close(cfg.tx_port); + if (!cfg.tx_only) { + rte_eth_dev_stop(cfg.rx_port); + rte_eth_dev_close(cfg.rx_port); + } + rte_eal_cleanup(); + return 0; +} diff --git a/examples/txpp/meson.build b/examples/txpp/meson.build new file mode 100644 index 0000000000..dc453539cb --- /dev/null +++ b/examples/txpp/meson.build @@ -0,0 +1,16 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2026 Intel Corporation + +# meson file, for building this example as part of a main DPDK build. +# +# To build this example as a standalone application with an already-installed +# DPDK instance, use 'make' + +# rte_eth_read_clock is experimental +allow_experimental_apis = true + +deps += ['ethdev'] + +sources = files( + 'main.c', +) -- 2.43.0

