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;
60 /* Callback added to the RX port and applied to packets. 8< */
62 add_timestamps(uint16_t port __rte_unused, uint16_t qidx __rte_unused,
63 struct rte_mbuf **pkts, uint16_t nb_pkts,
64 uint16_t max_pkts __rte_unused, void *_ __rte_unused)
67 uint64_t now = rte_rdtsc();
69 for (i = 0; i < nb_pkts; i++)
70 *tsc_field(pkts[i]) = now;
73 /* >8 End of callback addition and application. */
75 /* Callback is added to the TX port. 8< */
77 calc_latency(uint16_t port, uint16_t qidx __rte_unused,
78 struct rte_mbuf **pkts, uint16_t nb_pkts, void *_ __rte_unused)
81 uint64_t queue_ticks = 0;
82 uint64_t now = rte_rdtsc();
87 rte_eth_read_clock(port, &ticks);
89 for (i = 0; i < nb_pkts; i++) {
90 cycles += now - *tsc_field(pkts[i]);
92 queue_ticks += ticks - *hwts_field(pkts[i]);
95 latency_numbers.total_cycles += cycles;
97 latency_numbers.total_queue_cycles += (queue_ticks
98 * ticks_per_cycle_mult) >> TICKS_PER_CYCLE_SHIFT;
100 latency_numbers.total_pkts += nb_pkts;
102 if (latency_numbers.total_pkts > (100 * 1000 * 1000ULL)) {
103 printf("Latency = %"PRIu64" cycles\n",
104 latency_numbers.total_cycles / latency_numbers.total_pkts);
105 if (hw_timestamping) {
106 printf("Latency from HW = %"PRIu64" cycles\n",
107 latency_numbers.total_queue_cycles
108 / latency_numbers.total_pkts);
110 latency_numbers.total_cycles = 0;
111 latency_numbers.total_queue_cycles = 0;
112 latency_numbers.total_pkts = 0;
116 /* >8 End of callback addition. */
119 * Initialises a given port using global settings and with the rx buffers
120 * coming from the mbuf_pool passed as parameter
123 /* Port initialization. 8< */
125 port_init(uint16_t port, struct rte_mempool *mbuf_pool)
127 struct rte_eth_conf port_conf = port_conf_default;
128 const uint16_t rx_rings = 1, tx_rings = 1;
129 uint16_t nb_rxd = RX_RING_SIZE;
130 uint16_t nb_txd = TX_RING_SIZE;
133 struct rte_eth_dev_info dev_info;
134 struct rte_eth_rxconf rxconf;
135 struct rte_eth_txconf txconf;
137 if (!rte_eth_dev_is_valid_port(port))
140 retval = rte_eth_dev_info_get(port, &dev_info);
142 printf("Error during getting device (port %u) info: %s\n",
143 port, strerror(-retval));
148 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
149 port_conf.txmode.offloads |=
150 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
152 if (hw_timestamping) {
153 if (!(dev_info.rx_offload_capa & DEV_RX_OFFLOAD_TIMESTAMP)) {
154 printf("\nERROR: Port %u does not support hardware timestamping\n"
158 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_TIMESTAMP;
159 rte_mbuf_dyn_rx_timestamp_register(&hwts_dynfield_offset, NULL);
160 if (hwts_dynfield_offset < 0) {
161 printf("ERROR: Failed to register timestamp field\n");
166 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
170 retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
174 rxconf = dev_info.default_rxconf;
176 for (q = 0; q < rx_rings; q++) {
177 retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
178 rte_eth_dev_socket_id(port), &rxconf, mbuf_pool);
183 txconf = dev_info.default_txconf;
184 txconf.offloads = port_conf.txmode.offloads;
185 for (q = 0; q < tx_rings; q++) {
186 retval = rte_eth_tx_queue_setup(port, q, nb_txd,
187 rte_eth_dev_socket_id(port), &txconf);
192 retval = rte_eth_dev_start(port);
196 if (hw_timestamping && ticks_per_cycle_mult == 0) {
197 uint64_t cycles_base = rte_rdtsc();
199 retval = rte_eth_read_clock(port, &ticks_base);
203 uint64_t cycles = rte_rdtsc();
205 rte_eth_read_clock(port, &ticks);
206 uint64_t c_freq = cycles - cycles_base;
207 uint64_t t_freq = ticks - ticks_base;
208 double freq_mult = (double)c_freq / t_freq;
209 printf("TSC Freq ~= %" PRIu64
210 "\nHW Freq ~= %" PRIu64
212 c_freq * 10, t_freq * 10, freq_mult);
213 /* TSC will be faster than internal ticks so freq_mult is > 0
214 * We convert the multiplication to an integer shift & mult
216 ticks_per_cycle_mult = (1 << TICKS_PER_CYCLE_SHIFT) / freq_mult;
219 struct rte_ether_addr addr;
221 retval = rte_eth_macaddr_get(port, &addr);
223 printf("Failed to get MAC address on port %u: %s\n",
224 port, rte_strerror(-retval));
227 printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
228 " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
230 RTE_ETHER_ADDR_BYTES(&addr));
232 retval = rte_eth_promiscuous_enable(port);
236 /* RX and TX callbacks are added to the ports. 8< */
237 rte_eth_add_rx_callback(port, 0, add_timestamps, NULL);
238 rte_eth_add_tx_callback(port, 0, calc_latency, NULL);
239 /* >8 End of RX and TX callbacks. */
243 /* >8 End of port initialization. */
246 * Main thread that does the work, reading from INPUT_PORT
247 * and writing to OUTPUT_PORT
249 static __rte_noreturn void
254 RTE_ETH_FOREACH_DEV(port)
255 if (rte_eth_dev_socket_id(port) > 0 &&
256 rte_eth_dev_socket_id(port) !=
257 (int)rte_socket_id())
258 printf("WARNING, port %u is on remote NUMA node to "
259 "polling thread.\n\tPerformance will "
260 "not be optimal.\n", port);
262 printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
265 RTE_ETH_FOREACH_DEV(port) {
266 struct rte_mbuf *bufs[BURST_SIZE];
267 const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
269 if (unlikely(nb_rx == 0))
271 const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
273 if (unlikely(nb_tx < nb_rx)) {
276 for (buf = nb_tx; buf < nb_rx; buf++)
277 rte_pktmbuf_free(bufs[buf]);
283 /* Main function, does initialisation and calls the per-lcore functions */
285 main(int argc, char *argv[])
287 struct rte_mempool *mbuf_pool;
290 struct option lgopts[] = {
293 int opt, option_index;
295 static const struct rte_mbuf_dynfield tsc_dynfield_desc = {
296 .name = "example_bbdev_dynfield_tsc",
297 .size = sizeof(tsc_t),
298 .align = __alignof__(tsc_t),
302 int ret = rte_eal_init(argc, argv);
305 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
309 while ((opt = getopt_long(argc, argv, "t", lgopts, &option_index))
316 printf(usage, argv[0]);
319 optind = 1; /* reset getopt lib */
321 nb_ports = rte_eth_dev_count_avail();
322 if (nb_ports < 2 || (nb_ports & 1))
323 rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
325 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL",
326 NUM_MBUFS * nb_ports, MBUF_CACHE_SIZE, 0,
327 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
328 if (mbuf_pool == NULL)
329 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
331 tsc_dynfield_offset =
332 rte_mbuf_dynfield_register(&tsc_dynfield_desc);
333 if (tsc_dynfield_offset < 0)
334 rte_exit(EXIT_FAILURE, "Cannot register mbuf field\n");
336 /* initialize all ports */
337 RTE_ETH_FOREACH_DEV(portid)
338 if (port_init(portid, mbuf_pool) != 0)
339 rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu16"\n",
342 if (rte_lcore_count() > 1)
343 printf("\nWARNING: Too much enabled lcores - "
344 "App uses only 1 lcore\n");
346 /* call lcore_main on main core only */
349 /* clean up the EAL */