1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2015 Intel Corporation
9 #include <rte_ethdev.h>
10 #include <rte_cycles.h>
11 #include <rte_lcore.h>
13 #include <rte_mbuf_dyn.h>
15 #define RX_RING_SIZE 1024
16 #define TX_RING_SIZE 1024
18 #define NUM_MBUFS 8191
19 #define MBUF_CACHE_SIZE 250
22 static int hwts_dynfield_offset = -1;
24 static inline rte_mbuf_timestamp_t *
25 hwts_field(struct rte_mbuf *mbuf)
27 return RTE_MBUF_DYNFIELD(mbuf,
28 hwts_dynfield_offset, rte_mbuf_timestamp_t *);
31 typedef uint64_t tsc_t;
32 static int tsc_dynfield_offset = -1;
35 tsc_field(struct rte_mbuf *mbuf)
37 return RTE_MBUF_DYNFIELD(mbuf, tsc_dynfield_offset, tsc_t *);
40 static const char usage[] =
41 "%s EAL_ARGS -- [-t]\n";
43 static const struct rte_eth_conf port_conf_default = {
45 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
50 uint64_t total_cycles;
51 uint64_t total_queue_cycles;
57 #define TICKS_PER_CYCLE_SHIFT 16
58 static uint64_t ticks_per_cycle_mult;
61 add_timestamps(uint16_t port __rte_unused, uint16_t qidx __rte_unused,
62 struct rte_mbuf **pkts, uint16_t nb_pkts,
63 uint16_t max_pkts __rte_unused, void *_ __rte_unused)
66 uint64_t now = rte_rdtsc();
68 for (i = 0; i < nb_pkts; i++)
69 *tsc_field(pkts[i]) = now;
74 calc_latency(uint16_t port, uint16_t qidx __rte_unused,
75 struct rte_mbuf **pkts, uint16_t nb_pkts, void *_ __rte_unused)
78 uint64_t queue_ticks = 0;
79 uint64_t now = rte_rdtsc();
84 rte_eth_read_clock(port, &ticks);
86 for (i = 0; i < nb_pkts; i++) {
87 cycles += now - *tsc_field(pkts[i]);
89 queue_ticks += ticks - *hwts_field(pkts[i]);
92 latency_numbers.total_cycles += cycles;
94 latency_numbers.total_queue_cycles += (queue_ticks
95 * ticks_per_cycle_mult) >> TICKS_PER_CYCLE_SHIFT;
97 latency_numbers.total_pkts += nb_pkts;
99 if (latency_numbers.total_pkts > (100 * 1000 * 1000ULL)) {
100 printf("Latency = %"PRIu64" cycles\n",
101 latency_numbers.total_cycles / latency_numbers.total_pkts);
102 if (hw_timestamping) {
103 printf("Latency from HW = %"PRIu64" cycles\n",
104 latency_numbers.total_queue_cycles
105 / latency_numbers.total_pkts);
107 latency_numbers.total_cycles = 0;
108 latency_numbers.total_queue_cycles = 0;
109 latency_numbers.total_pkts = 0;
115 * Initialises a given port using global settings and with the rx buffers
116 * coming from the mbuf_pool passed as parameter
119 port_init(uint16_t port, struct rte_mempool *mbuf_pool)
121 struct rte_eth_conf port_conf = port_conf_default;
122 const uint16_t rx_rings = 1, tx_rings = 1;
123 uint16_t nb_rxd = RX_RING_SIZE;
124 uint16_t nb_txd = TX_RING_SIZE;
127 struct rte_eth_dev_info dev_info;
128 struct rte_eth_rxconf rxconf;
129 struct rte_eth_txconf txconf;
131 if (!rte_eth_dev_is_valid_port(port))
134 retval = rte_eth_dev_info_get(port, &dev_info);
136 printf("Error during getting device (port %u) info: %s\n",
137 port, strerror(-retval));
142 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
143 port_conf.txmode.offloads |=
144 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
146 if (hw_timestamping) {
147 if (!(dev_info.rx_offload_capa & DEV_RX_OFFLOAD_TIMESTAMP)) {
148 printf("\nERROR: Port %u does not support hardware timestamping\n"
152 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_TIMESTAMP;
153 rte_mbuf_dyn_rx_timestamp_register(&hwts_dynfield_offset, NULL);
154 if (hwts_dynfield_offset < 0) {
155 printf("ERROR: Failed to register timestamp field\n");
160 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
164 retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
168 rxconf = dev_info.default_rxconf;
170 for (q = 0; q < rx_rings; q++) {
171 retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
172 rte_eth_dev_socket_id(port), &rxconf, mbuf_pool);
177 txconf = dev_info.default_txconf;
178 txconf.offloads = port_conf.txmode.offloads;
179 for (q = 0; q < tx_rings; q++) {
180 retval = rte_eth_tx_queue_setup(port, q, nb_txd,
181 rte_eth_dev_socket_id(port), &txconf);
186 retval = rte_eth_dev_start(port);
190 if (hw_timestamping && ticks_per_cycle_mult == 0) {
191 uint64_t cycles_base = rte_rdtsc();
193 retval = rte_eth_read_clock(port, &ticks_base);
197 uint64_t cycles = rte_rdtsc();
199 rte_eth_read_clock(port, &ticks);
200 uint64_t c_freq = cycles - cycles_base;
201 uint64_t t_freq = ticks - ticks_base;
202 double freq_mult = (double)c_freq / t_freq;
203 printf("TSC Freq ~= %" PRIu64
204 "\nHW Freq ~= %" PRIu64
206 c_freq * 10, t_freq * 10, freq_mult);
207 /* TSC will be faster than internal ticks so freq_mult is > 0
208 * We convert the multiplication to an integer shift & mult
210 ticks_per_cycle_mult = (1 << TICKS_PER_CYCLE_SHIFT) / freq_mult;
213 struct rte_ether_addr addr;
215 retval = rte_eth_macaddr_get(port, &addr);
217 printf("Failed to get MAC address on port %u: %s\n",
218 port, rte_strerror(-retval));
221 printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
222 " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
224 addr.addr_bytes[0], addr.addr_bytes[1],
225 addr.addr_bytes[2], addr.addr_bytes[3],
226 addr.addr_bytes[4], addr.addr_bytes[5]);
228 retval = rte_eth_promiscuous_enable(port);
232 rte_eth_add_rx_callback(port, 0, add_timestamps, NULL);
233 rte_eth_add_tx_callback(port, 0, calc_latency, NULL);
239 * Main thread that does the work, reading from INPUT_PORT
240 * and writing to OUTPUT_PORT
242 static __rte_noreturn void
247 RTE_ETH_FOREACH_DEV(port)
248 if (rte_eth_dev_socket_id(port) > 0 &&
249 rte_eth_dev_socket_id(port) !=
250 (int)rte_socket_id())
251 printf("WARNING, port %u is on remote NUMA node to "
252 "polling thread.\n\tPerformance will "
253 "not be optimal.\n", port);
255 printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
258 RTE_ETH_FOREACH_DEV(port) {
259 struct rte_mbuf *bufs[BURST_SIZE];
260 const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
262 if (unlikely(nb_rx == 0))
264 const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
266 if (unlikely(nb_tx < nb_rx)) {
269 for (buf = nb_tx; buf < nb_rx; buf++)
270 rte_pktmbuf_free(bufs[buf]);
276 /* Main function, does initialisation and calls the per-lcore functions */
278 main(int argc, char *argv[])
280 struct rte_mempool *mbuf_pool;
283 struct option lgopts[] = {
286 int opt, option_index;
288 static const struct rte_mbuf_dynfield tsc_dynfield_desc = {
289 .name = "example_bbdev_dynfield_tsc",
290 .size = sizeof(tsc_t),
291 .align = __alignof__(tsc_t),
295 int ret = rte_eal_init(argc, argv);
298 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
302 while ((opt = getopt_long(argc, argv, "t", lgopts, &option_index))
309 printf(usage, argv[0]);
312 optind = 1; /* reset getopt lib */
314 nb_ports = rte_eth_dev_count_avail();
315 if (nb_ports < 2 || (nb_ports & 1))
316 rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
318 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL",
319 NUM_MBUFS * nb_ports, MBUF_CACHE_SIZE, 0,
320 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
321 if (mbuf_pool == NULL)
322 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
324 tsc_dynfield_offset =
325 rte_mbuf_dynfield_register(&tsc_dynfield_desc);
326 if (tsc_dynfield_offset < 0)
327 rte_exit(EXIT_FAILURE, "Cannot register mbuf field\n");
329 /* initialize all ports */
330 RTE_ETH_FOREACH_DEV(portid)
331 if (port_init(portid, mbuf_pool) != 0)
332 rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8"\n",
335 if (rte_lcore_count() > 1)
336 printf("\nWARNING: Too much enabled lcores - "
337 "App uses only 1 lcore\n");
339 /* call lcore_main on main core only */