1d30ea906Sjfb8856606 /* SPDX-License-Identifier: BSD-3-Clause
2d30ea906Sjfb8856606 * Copyright(c) 2010-2015 Intel Corporation
3a9643ea8Slogwang */
4a9643ea8Slogwang
5a9643ea8Slogwang #include <stdint.h>
6a9643ea8Slogwang #include <inttypes.h>
74418919fSjohnjiang #include <getopt.h>
8a9643ea8Slogwang #include <rte_eal.h>
9a9643ea8Slogwang #include <rte_ethdev.h>
10a9643ea8Slogwang #include <rte_cycles.h>
11a9643ea8Slogwang #include <rte_lcore.h>
12a9643ea8Slogwang #include <rte_mbuf.h>
13*2d9fd380Sjfb8856606 #include <rte_mbuf_dyn.h>
14a9643ea8Slogwang
15d30ea906Sjfb8856606 #define RX_RING_SIZE 1024
16d30ea906Sjfb8856606 #define TX_RING_SIZE 1024
17a9643ea8Slogwang
18a9643ea8Slogwang #define NUM_MBUFS 8191
19a9643ea8Slogwang #define MBUF_CACHE_SIZE 250
20a9643ea8Slogwang #define BURST_SIZE 32
21a9643ea8Slogwang
22*2d9fd380Sjfb8856606 static int hwts_dynfield_offset = -1;
23*2d9fd380Sjfb8856606
24*2d9fd380Sjfb8856606 static inline rte_mbuf_timestamp_t *
hwts_field(struct rte_mbuf * mbuf)25*2d9fd380Sjfb8856606 hwts_field(struct rte_mbuf *mbuf)
26*2d9fd380Sjfb8856606 {
27*2d9fd380Sjfb8856606 return RTE_MBUF_DYNFIELD(mbuf,
28*2d9fd380Sjfb8856606 hwts_dynfield_offset, rte_mbuf_timestamp_t *);
29*2d9fd380Sjfb8856606 }
30*2d9fd380Sjfb8856606
31*2d9fd380Sjfb8856606 typedef uint64_t tsc_t;
32*2d9fd380Sjfb8856606 static int tsc_dynfield_offset = -1;
33*2d9fd380Sjfb8856606
34*2d9fd380Sjfb8856606 static inline tsc_t *
tsc_field(struct rte_mbuf * mbuf)35*2d9fd380Sjfb8856606 tsc_field(struct rte_mbuf *mbuf)
36*2d9fd380Sjfb8856606 {
37*2d9fd380Sjfb8856606 return RTE_MBUF_DYNFIELD(mbuf, tsc_dynfield_offset, tsc_t *);
38*2d9fd380Sjfb8856606 }
39*2d9fd380Sjfb8856606
404418919fSjohnjiang static const char usage[] =
414418919fSjohnjiang "%s EAL_ARGS -- [-t]\n";
424418919fSjohnjiang
43a9643ea8Slogwang static const struct rte_eth_conf port_conf_default = {
44d30ea906Sjfb8856606 .rxmode = {
454418919fSjohnjiang .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
46d30ea906Sjfb8856606 },
47a9643ea8Slogwang };
48a9643ea8Slogwang
49a9643ea8Slogwang static struct {
50a9643ea8Slogwang uint64_t total_cycles;
514418919fSjohnjiang uint64_t total_queue_cycles;
52a9643ea8Slogwang uint64_t total_pkts;
53a9643ea8Slogwang } latency_numbers;
54a9643ea8Slogwang
554418919fSjohnjiang int hw_timestamping;
564418919fSjohnjiang
574418919fSjohnjiang #define TICKS_PER_CYCLE_SHIFT 16
584418919fSjohnjiang static uint64_t ticks_per_cycle_mult;
59a9643ea8Slogwang
60a9643ea8Slogwang static uint16_t
add_timestamps(uint16_t port __rte_unused,uint16_t qidx __rte_unused,struct rte_mbuf ** pkts,uint16_t nb_pkts,uint16_t max_pkts __rte_unused,void * _ __rte_unused)612bfe3f2eSlogwang add_timestamps(uint16_t port __rte_unused, uint16_t qidx __rte_unused,
62a9643ea8Slogwang struct rte_mbuf **pkts, uint16_t nb_pkts,
63a9643ea8Slogwang uint16_t max_pkts __rte_unused, void *_ __rte_unused)
64a9643ea8Slogwang {
65a9643ea8Slogwang unsigned i;
66a9643ea8Slogwang uint64_t now = rte_rdtsc();
67a9643ea8Slogwang
68a9643ea8Slogwang for (i = 0; i < nb_pkts; i++)
69*2d9fd380Sjfb8856606 *tsc_field(pkts[i]) = now;
70a9643ea8Slogwang return nb_pkts;
71a9643ea8Slogwang }
72a9643ea8Slogwang
73a9643ea8Slogwang static uint16_t
calc_latency(uint16_t port,uint16_t qidx __rte_unused,struct rte_mbuf ** pkts,uint16_t nb_pkts,void * _ __rte_unused)744418919fSjohnjiang calc_latency(uint16_t port, uint16_t qidx __rte_unused,
75a9643ea8Slogwang struct rte_mbuf **pkts, uint16_t nb_pkts, void *_ __rte_unused)
76a9643ea8Slogwang {
77a9643ea8Slogwang uint64_t cycles = 0;
784418919fSjohnjiang uint64_t queue_ticks = 0;
79a9643ea8Slogwang uint64_t now = rte_rdtsc();
804418919fSjohnjiang uint64_t ticks;
81a9643ea8Slogwang unsigned i;
82a9643ea8Slogwang
834418919fSjohnjiang if (hw_timestamping)
844418919fSjohnjiang rte_eth_read_clock(port, &ticks);
854418919fSjohnjiang
864418919fSjohnjiang for (i = 0; i < nb_pkts; i++) {
87*2d9fd380Sjfb8856606 cycles += now - *tsc_field(pkts[i]);
884418919fSjohnjiang if (hw_timestamping)
89*2d9fd380Sjfb8856606 queue_ticks += ticks - *hwts_field(pkts[i]);
904418919fSjohnjiang }
914418919fSjohnjiang
92a9643ea8Slogwang latency_numbers.total_cycles += cycles;
934418919fSjohnjiang if (hw_timestamping)
944418919fSjohnjiang latency_numbers.total_queue_cycles += (queue_ticks
954418919fSjohnjiang * ticks_per_cycle_mult) >> TICKS_PER_CYCLE_SHIFT;
964418919fSjohnjiang
97a9643ea8Slogwang latency_numbers.total_pkts += nb_pkts;
98a9643ea8Slogwang
99a9643ea8Slogwang if (latency_numbers.total_pkts > (100 * 1000 * 1000ULL)) {
100a9643ea8Slogwang printf("Latency = %"PRIu64" cycles\n",
101a9643ea8Slogwang latency_numbers.total_cycles / latency_numbers.total_pkts);
1024418919fSjohnjiang if (hw_timestamping) {
1034418919fSjohnjiang printf("Latency from HW = %"PRIu64" cycles\n",
1044418919fSjohnjiang latency_numbers.total_queue_cycles
1054418919fSjohnjiang / latency_numbers.total_pkts);
1064418919fSjohnjiang }
1074418919fSjohnjiang latency_numbers.total_cycles = 0;
1084418919fSjohnjiang latency_numbers.total_queue_cycles = 0;
1094418919fSjohnjiang latency_numbers.total_pkts = 0;
110a9643ea8Slogwang }
111a9643ea8Slogwang return nb_pkts;
112a9643ea8Slogwang }
113a9643ea8Slogwang
114a9643ea8Slogwang /*
115a9643ea8Slogwang * Initialises a given port using global settings and with the rx buffers
116a9643ea8Slogwang * coming from the mbuf_pool passed as parameter
117a9643ea8Slogwang */
118a9643ea8Slogwang static inline int
port_init(uint16_t port,struct rte_mempool * mbuf_pool)1192bfe3f2eSlogwang port_init(uint16_t port, struct rte_mempool *mbuf_pool)
120a9643ea8Slogwang {
121a9643ea8Slogwang struct rte_eth_conf port_conf = port_conf_default;
122a9643ea8Slogwang const uint16_t rx_rings = 1, tx_rings = 1;
1232bfe3f2eSlogwang uint16_t nb_rxd = RX_RING_SIZE;
1242bfe3f2eSlogwang uint16_t nb_txd = TX_RING_SIZE;
125a9643ea8Slogwang int retval;
126a9643ea8Slogwang uint16_t q;
127d30ea906Sjfb8856606 struct rte_eth_dev_info dev_info;
1284418919fSjohnjiang struct rte_eth_rxconf rxconf;
129d30ea906Sjfb8856606 struct rte_eth_txconf txconf;
130a9643ea8Slogwang
131d30ea906Sjfb8856606 if (!rte_eth_dev_is_valid_port(port))
132a9643ea8Slogwang return -1;
133a9643ea8Slogwang
1344418919fSjohnjiang retval = rte_eth_dev_info_get(port, &dev_info);
1354418919fSjohnjiang if (retval != 0) {
1364418919fSjohnjiang printf("Error during getting device (port %u) info: %s\n",
1374418919fSjohnjiang port, strerror(-retval));
1384418919fSjohnjiang
1394418919fSjohnjiang return retval;
1404418919fSjohnjiang }
1414418919fSjohnjiang
142d30ea906Sjfb8856606 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
143d30ea906Sjfb8856606 port_conf.txmode.offloads |=
144d30ea906Sjfb8856606 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
145d30ea906Sjfb8856606
1464418919fSjohnjiang if (hw_timestamping) {
1474418919fSjohnjiang if (!(dev_info.rx_offload_capa & DEV_RX_OFFLOAD_TIMESTAMP)) {
1484418919fSjohnjiang printf("\nERROR: Port %u does not support hardware timestamping\n"
1494418919fSjohnjiang , port);
1504418919fSjohnjiang return -1;
1514418919fSjohnjiang }
1524418919fSjohnjiang port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_TIMESTAMP;
153*2d9fd380Sjfb8856606 rte_mbuf_dyn_rx_timestamp_register(&hwts_dynfield_offset, NULL);
154*2d9fd380Sjfb8856606 if (hwts_dynfield_offset < 0) {
155*2d9fd380Sjfb8856606 printf("ERROR: Failed to register timestamp field\n");
156*2d9fd380Sjfb8856606 return -rte_errno;
157*2d9fd380Sjfb8856606 }
1584418919fSjohnjiang }
1594418919fSjohnjiang
160a9643ea8Slogwang retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
161a9643ea8Slogwang if (retval != 0)
162a9643ea8Slogwang return retval;
163a9643ea8Slogwang
1642bfe3f2eSlogwang retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
1652bfe3f2eSlogwang if (retval != 0)
1662bfe3f2eSlogwang return retval;
1672bfe3f2eSlogwang
1684418919fSjohnjiang rxconf = dev_info.default_rxconf;
1694418919fSjohnjiang
170a9643ea8Slogwang for (q = 0; q < rx_rings; q++) {
1712bfe3f2eSlogwang retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
1724418919fSjohnjiang rte_eth_dev_socket_id(port), &rxconf, mbuf_pool);
173a9643ea8Slogwang if (retval < 0)
174a9643ea8Slogwang return retval;
175a9643ea8Slogwang }
176a9643ea8Slogwang
177d30ea906Sjfb8856606 txconf = dev_info.default_txconf;
178d30ea906Sjfb8856606 txconf.offloads = port_conf.txmode.offloads;
179a9643ea8Slogwang for (q = 0; q < tx_rings; q++) {
1802bfe3f2eSlogwang retval = rte_eth_tx_queue_setup(port, q, nb_txd,
181d30ea906Sjfb8856606 rte_eth_dev_socket_id(port), &txconf);
182a9643ea8Slogwang if (retval < 0)
183a9643ea8Slogwang return retval;
184a9643ea8Slogwang }
185a9643ea8Slogwang
186a9643ea8Slogwang retval = rte_eth_dev_start(port);
187a9643ea8Slogwang if (retval < 0)
188a9643ea8Slogwang return retval;
189a9643ea8Slogwang
1904418919fSjohnjiang if (hw_timestamping && ticks_per_cycle_mult == 0) {
1914418919fSjohnjiang uint64_t cycles_base = rte_rdtsc();
1924418919fSjohnjiang uint64_t ticks_base;
1934418919fSjohnjiang retval = rte_eth_read_clock(port, &ticks_base);
1944418919fSjohnjiang if (retval != 0)
1954418919fSjohnjiang return retval;
1964418919fSjohnjiang rte_delay_ms(100);
1974418919fSjohnjiang uint64_t cycles = rte_rdtsc();
1984418919fSjohnjiang uint64_t ticks;
1994418919fSjohnjiang rte_eth_read_clock(port, &ticks);
2004418919fSjohnjiang uint64_t c_freq = cycles - cycles_base;
2014418919fSjohnjiang uint64_t t_freq = ticks - ticks_base;
2024418919fSjohnjiang double freq_mult = (double)c_freq / t_freq;
2034418919fSjohnjiang printf("TSC Freq ~= %" PRIu64
2044418919fSjohnjiang "\nHW Freq ~= %" PRIu64
2054418919fSjohnjiang "\nRatio : %f\n",
2064418919fSjohnjiang c_freq * 10, t_freq * 10, freq_mult);
2074418919fSjohnjiang /* TSC will be faster than internal ticks so freq_mult is > 0
2084418919fSjohnjiang * We convert the multiplication to an integer shift & mult
2094418919fSjohnjiang */
2104418919fSjohnjiang ticks_per_cycle_mult = (1 << TICKS_PER_CYCLE_SHIFT) / freq_mult;
2114418919fSjohnjiang }
212a9643ea8Slogwang
2134418919fSjohnjiang struct rte_ether_addr addr;
2144418919fSjohnjiang
2154418919fSjohnjiang retval = rte_eth_macaddr_get(port, &addr);
2164418919fSjohnjiang if (retval < 0) {
2174418919fSjohnjiang printf("Failed to get MAC address on port %u: %s\n",
2184418919fSjohnjiang port, rte_strerror(-retval));
2194418919fSjohnjiang return retval;
2204418919fSjohnjiang }
221a9643ea8Slogwang printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
222a9643ea8Slogwang " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
223a9643ea8Slogwang (unsigned)port,
224a9643ea8Slogwang addr.addr_bytes[0], addr.addr_bytes[1],
225a9643ea8Slogwang addr.addr_bytes[2], addr.addr_bytes[3],
226a9643ea8Slogwang addr.addr_bytes[4], addr.addr_bytes[5]);
227a9643ea8Slogwang
2284418919fSjohnjiang retval = rte_eth_promiscuous_enable(port);
2294418919fSjohnjiang if (retval != 0)
2304418919fSjohnjiang return retval;
2314418919fSjohnjiang
232a9643ea8Slogwang rte_eth_add_rx_callback(port, 0, add_timestamps, NULL);
233a9643ea8Slogwang rte_eth_add_tx_callback(port, 0, calc_latency, NULL);
234a9643ea8Slogwang
235a9643ea8Slogwang return 0;
236a9643ea8Slogwang }
237a9643ea8Slogwang
238a9643ea8Slogwang /*
239a9643ea8Slogwang * Main thread that does the work, reading from INPUT_PORT
240a9643ea8Slogwang * and writing to OUTPUT_PORT
241a9643ea8Slogwang */
242*2d9fd380Sjfb8856606 static __rte_noreturn void
lcore_main(void)243a9643ea8Slogwang lcore_main(void)
244a9643ea8Slogwang {
2452bfe3f2eSlogwang uint16_t port;
246a9643ea8Slogwang
247d30ea906Sjfb8856606 RTE_ETH_FOREACH_DEV(port)
248a9643ea8Slogwang if (rte_eth_dev_socket_id(port) > 0 &&
249a9643ea8Slogwang rte_eth_dev_socket_id(port) !=
250a9643ea8Slogwang (int)rte_socket_id())
251a9643ea8Slogwang printf("WARNING, port %u is on remote NUMA node to "
252a9643ea8Slogwang "polling thread.\n\tPerformance will "
253a9643ea8Slogwang "not be optimal.\n", port);
254a9643ea8Slogwang
255a9643ea8Slogwang printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
256a9643ea8Slogwang rte_lcore_id());
257a9643ea8Slogwang for (;;) {
258d30ea906Sjfb8856606 RTE_ETH_FOREACH_DEV(port) {
259a9643ea8Slogwang struct rte_mbuf *bufs[BURST_SIZE];
260a9643ea8Slogwang const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
261a9643ea8Slogwang bufs, BURST_SIZE);
262a9643ea8Slogwang if (unlikely(nb_rx == 0))
263a9643ea8Slogwang continue;
264a9643ea8Slogwang const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
265a9643ea8Slogwang bufs, nb_rx);
266a9643ea8Slogwang if (unlikely(nb_tx < nb_rx)) {
267a9643ea8Slogwang uint16_t buf;
268a9643ea8Slogwang
269a9643ea8Slogwang for (buf = nb_tx; buf < nb_rx; buf++)
270a9643ea8Slogwang rte_pktmbuf_free(bufs[buf]);
271a9643ea8Slogwang }
272a9643ea8Slogwang }
273a9643ea8Slogwang }
274a9643ea8Slogwang }
275a9643ea8Slogwang
276a9643ea8Slogwang /* Main function, does initialisation and calls the per-lcore functions */
277a9643ea8Slogwang int
main(int argc,char * argv[])278a9643ea8Slogwang main(int argc, char *argv[])
279a9643ea8Slogwang {
280a9643ea8Slogwang struct rte_mempool *mbuf_pool;
281d30ea906Sjfb8856606 uint16_t nb_ports;
2822bfe3f2eSlogwang uint16_t portid;
2834418919fSjohnjiang struct option lgopts[] = {
2844418919fSjohnjiang { NULL, 0, 0, 0 }
2854418919fSjohnjiang };
2864418919fSjohnjiang int opt, option_index;
2874418919fSjohnjiang
288*2d9fd380Sjfb8856606 static const struct rte_mbuf_dynfield tsc_dynfield_desc = {
289*2d9fd380Sjfb8856606 .name = "example_bbdev_dynfield_tsc",
290*2d9fd380Sjfb8856606 .size = sizeof(tsc_t),
291*2d9fd380Sjfb8856606 .align = __alignof__(tsc_t),
292*2d9fd380Sjfb8856606 };
293a9643ea8Slogwang
294a9643ea8Slogwang /* init EAL */
295a9643ea8Slogwang int ret = rte_eal_init(argc, argv);
296a9643ea8Slogwang
297a9643ea8Slogwang if (ret < 0)
298a9643ea8Slogwang rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
299a9643ea8Slogwang argc -= ret;
300a9643ea8Slogwang argv += ret;
301a9643ea8Slogwang
3024418919fSjohnjiang while ((opt = getopt_long(argc, argv, "t", lgopts, &option_index))
3034418919fSjohnjiang != EOF)
3044418919fSjohnjiang switch (opt) {
3054418919fSjohnjiang case 't':
3064418919fSjohnjiang hw_timestamping = 1;
3074418919fSjohnjiang break;
3084418919fSjohnjiang default:
3094418919fSjohnjiang printf(usage, argv[0]);
3104418919fSjohnjiang return -1;
3114418919fSjohnjiang }
3124418919fSjohnjiang optind = 1; /* reset getopt lib */
3134418919fSjohnjiang
314d30ea906Sjfb8856606 nb_ports = rte_eth_dev_count_avail();
315a9643ea8Slogwang if (nb_ports < 2 || (nb_ports & 1))
316a9643ea8Slogwang rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
317a9643ea8Slogwang
318a9643ea8Slogwang mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL",
319a9643ea8Slogwang NUM_MBUFS * nb_ports, MBUF_CACHE_SIZE, 0,
320a9643ea8Slogwang RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
321a9643ea8Slogwang if (mbuf_pool == NULL)
322a9643ea8Slogwang rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
323a9643ea8Slogwang
324*2d9fd380Sjfb8856606 tsc_dynfield_offset =
325*2d9fd380Sjfb8856606 rte_mbuf_dynfield_register(&tsc_dynfield_desc);
326*2d9fd380Sjfb8856606 if (tsc_dynfield_offset < 0)
327*2d9fd380Sjfb8856606 rte_exit(EXIT_FAILURE, "Cannot register mbuf field\n");
328*2d9fd380Sjfb8856606
329a9643ea8Slogwang /* initialize all ports */
330d30ea906Sjfb8856606 RTE_ETH_FOREACH_DEV(portid)
331a9643ea8Slogwang if (port_init(portid, mbuf_pool) != 0)
332a9643ea8Slogwang rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8"\n",
333a9643ea8Slogwang portid);
334a9643ea8Slogwang
335a9643ea8Slogwang if (rte_lcore_count() > 1)
336a9643ea8Slogwang printf("\nWARNING: Too much enabled lcores - "
337a9643ea8Slogwang "App uses only 1 lcore\n");
338a9643ea8Slogwang
339*2d9fd380Sjfb8856606 /* call lcore_main on main core only */
340a9643ea8Slogwang lcore_main();
341a9643ea8Slogwang return 0;
342a9643ea8Slogwang }
343