app/testpmd: record Rx and dropped stats in flowgen
[dpdk.git] / examples / rxtx_callbacks / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2015 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <inttypes.h>
7 #include <getopt.h>
8 #include <rte_eal.h>
9 #include <rte_ethdev.h>
10 #include <rte_cycles.h>
11 #include <rte_lcore.h>
12 #include <rte_mbuf.h>
13 #include <rte_mbuf_dyn.h>
14
15 #define RX_RING_SIZE 1024
16 #define TX_RING_SIZE 1024
17
18 #define NUM_MBUFS 8191
19 #define MBUF_CACHE_SIZE 250
20 #define BURST_SIZE 32
21
22 static int hwts_dynfield_offset = -1;
23
24 static inline rte_mbuf_timestamp_t *
25 hwts_field(struct rte_mbuf *mbuf)
26 {
27         return RTE_MBUF_DYNFIELD(mbuf,
28                         hwts_dynfield_offset, rte_mbuf_timestamp_t *);
29 }
30
31 typedef uint64_t tsc_t;
32 static int tsc_dynfield_offset = -1;
33
34 static inline tsc_t *
35 tsc_field(struct rte_mbuf *mbuf)
36 {
37         return RTE_MBUF_DYNFIELD(mbuf, tsc_dynfield_offset, tsc_t *);
38 }
39
40 static const char usage[] =
41         "%s EAL_ARGS -- [-t]\n";
42
43 static const struct rte_eth_conf port_conf_default = {
44         .rxmode = {
45                 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
46         },
47 };
48
49 static struct {
50         uint64_t total_cycles;
51         uint64_t total_queue_cycles;
52         uint64_t total_pkts;
53 } latency_numbers;
54
55 int hw_timestamping;
56
57 #define TICKS_PER_CYCLE_SHIFT 16
58 static uint64_t ticks_per_cycle_mult;
59
60 /* Callback added to the RX port and applied to packets. 8< */
61 static uint16_t
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)
65 {
66         unsigned i;
67         uint64_t now = rte_rdtsc();
68
69         for (i = 0; i < nb_pkts; i++)
70                 *tsc_field(pkts[i]) = now;
71         return nb_pkts;
72 }
73 /* >8 End of callback addition and application. */
74
75 /* Callback is added to the TX port. 8< */
76 static uint16_t
77 calc_latency(uint16_t port, uint16_t qidx __rte_unused,
78                 struct rte_mbuf **pkts, uint16_t nb_pkts, void *_ __rte_unused)
79 {
80         uint64_t cycles = 0;
81         uint64_t queue_ticks = 0;
82         uint64_t now = rte_rdtsc();
83         uint64_t ticks;
84         unsigned i;
85
86         if (hw_timestamping)
87                 rte_eth_read_clock(port, &ticks);
88
89         for (i = 0; i < nb_pkts; i++) {
90                 cycles += now - *tsc_field(pkts[i]);
91                 if (hw_timestamping)
92                         queue_ticks += ticks - *hwts_field(pkts[i]);
93         }
94
95         latency_numbers.total_cycles += cycles;
96         if (hw_timestamping)
97                 latency_numbers.total_queue_cycles += (queue_ticks
98                         * ticks_per_cycle_mult) >> TICKS_PER_CYCLE_SHIFT;
99
100         latency_numbers.total_pkts += nb_pkts;
101
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);
109                 }
110                 latency_numbers.total_cycles = 0;
111                 latency_numbers.total_queue_cycles = 0;
112                 latency_numbers.total_pkts = 0;
113         }
114         return nb_pkts;
115 }
116 /* >8 End of callback addition. */
117
118 /*
119  * Initialises a given port using global settings and with the rx buffers
120  * coming from the mbuf_pool passed as parameter
121  */
122
123  /* Port initialization. 8< */
124 static inline int
125 port_init(uint16_t port, struct rte_mempool *mbuf_pool)
126 {
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;
131         int retval;
132         uint16_t q;
133         struct rte_eth_dev_info dev_info;
134         struct rte_eth_rxconf rxconf;
135         struct rte_eth_txconf txconf;
136
137         if (!rte_eth_dev_is_valid_port(port))
138                 return -1;
139
140         retval = rte_eth_dev_info_get(port, &dev_info);
141         if (retval != 0) {
142                 printf("Error during getting device (port %u) info: %s\n",
143                                 port, strerror(-retval));
144
145                 return retval;
146         }
147
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;
151
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"
155                                         , port);
156                         return -1;
157                 }
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");
162                         return -rte_errno;
163                 }
164         }
165
166         retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
167         if (retval != 0)
168                 return retval;
169
170         retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
171         if (retval != 0)
172                 return retval;
173
174         rxconf = dev_info.default_rxconf;
175
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);
179                 if (retval < 0)
180                         return retval;
181         }
182
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);
188                 if (retval < 0)
189                         return retval;
190         }
191
192         retval  = rte_eth_dev_start(port);
193         if (retval < 0)
194                 return retval;
195
196         if (hw_timestamping && ticks_per_cycle_mult  == 0) {
197                 uint64_t cycles_base = rte_rdtsc();
198                 uint64_t ticks_base;
199                 retval = rte_eth_read_clock(port, &ticks_base);
200                 if (retval != 0)
201                         return retval;
202                 rte_delay_ms(100);
203                 uint64_t cycles = rte_rdtsc();
204                 uint64_t ticks;
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
211                                 "\nRatio : %f\n",
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
215                  */
216                 ticks_per_cycle_mult = (1 << TICKS_PER_CYCLE_SHIFT) / freq_mult;
217         }
218
219         struct rte_ether_addr addr;
220
221         retval = rte_eth_macaddr_get(port, &addr);
222         if (retval < 0) {
223                 printf("Failed to get MAC address on port %u: %s\n",
224                         port, rte_strerror(-retval));
225                 return retval;
226         }
227         printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
228                         " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
229                         (unsigned)port,
230                         addr.addr_bytes[0], addr.addr_bytes[1],
231                         addr.addr_bytes[2], addr.addr_bytes[3],
232                         addr.addr_bytes[4], addr.addr_bytes[5]);
233
234         retval = rte_eth_promiscuous_enable(port);
235         if (retval != 0)
236                 return retval;
237
238         /* RX and TX callbacks are added to the ports. 8< */
239         rte_eth_add_rx_callback(port, 0, add_timestamps, NULL);
240         rte_eth_add_tx_callback(port, 0, calc_latency, NULL);
241         /* >8 End of RX and TX callbacks. */
242
243         return 0;
244 }
245 /* >8 End of port initialization. */
246
247 /*
248  * Main thread that does the work, reading from INPUT_PORT
249  * and writing to OUTPUT_PORT
250  */
251 static  __rte_noreturn void
252 lcore_main(void)
253 {
254         uint16_t port;
255
256         RTE_ETH_FOREACH_DEV(port)
257                 if (rte_eth_dev_socket_id(port) > 0 &&
258                                 rte_eth_dev_socket_id(port) !=
259                                                 (int)rte_socket_id())
260                         printf("WARNING, port %u is on remote NUMA node to "
261                                         "polling thread.\n\tPerformance will "
262                                         "not be optimal.\n", port);
263
264         printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
265                         rte_lcore_id());
266         for (;;) {
267                 RTE_ETH_FOREACH_DEV(port) {
268                         struct rte_mbuf *bufs[BURST_SIZE];
269                         const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
270                                         bufs, BURST_SIZE);
271                         if (unlikely(nb_rx == 0))
272                                 continue;
273                         const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
274                                         bufs, nb_rx);
275                         if (unlikely(nb_tx < nb_rx)) {
276                                 uint16_t buf;
277
278                                 for (buf = nb_tx; buf < nb_rx; buf++)
279                                         rte_pktmbuf_free(bufs[buf]);
280                         }
281                 }
282         }
283 }
284
285 /* Main function, does initialisation and calls the per-lcore functions */
286 int
287 main(int argc, char *argv[])
288 {
289         struct rte_mempool *mbuf_pool;
290         uint16_t nb_ports;
291         uint16_t portid;
292         struct option lgopts[] = {
293                 { NULL,  0, 0, 0 }
294         };
295         int opt, option_index;
296
297         static const struct rte_mbuf_dynfield tsc_dynfield_desc = {
298                 .name = "example_bbdev_dynfield_tsc",
299                 .size = sizeof(tsc_t),
300                 .align = __alignof__(tsc_t),
301         };
302
303         /* init EAL */
304         int ret = rte_eal_init(argc, argv);
305
306         if (ret < 0)
307                 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
308         argc -= ret;
309         argv += ret;
310
311         while ((opt = getopt_long(argc, argv, "t", lgopts, &option_index))
312                         != EOF)
313                 switch (opt) {
314                 case 't':
315                         hw_timestamping = 1;
316                         break;
317                 default:
318                         printf(usage, argv[0]);
319                         return -1;
320                 }
321         optind = 1; /* reset getopt lib */
322
323         nb_ports = rte_eth_dev_count_avail();
324         if (nb_ports < 2 || (nb_ports & 1))
325                 rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
326
327         mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL",
328                 NUM_MBUFS * nb_ports, MBUF_CACHE_SIZE, 0,
329                 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
330         if (mbuf_pool == NULL)
331                 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
332
333         tsc_dynfield_offset =
334                 rte_mbuf_dynfield_register(&tsc_dynfield_desc);
335         if (tsc_dynfield_offset < 0)
336                 rte_exit(EXIT_FAILURE, "Cannot register mbuf field\n");
337
338         /* initialize all ports */
339         RTE_ETH_FOREACH_DEV(portid)
340                 if (port_init(portid, mbuf_pool) != 0)
341                         rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu16"\n",
342                                         portid);
343
344         if (rte_lcore_count() > 1)
345                 printf("\nWARNING: Too much enabled lcores - "
346                         "App uses only 1 lcore\n");
347
348         /* call lcore_main on main core only */
349         lcore_main();
350
351         /* clean up the EAL */
352         rte_eal_cleanup();
353
354         return 0;
355 }