net: add macro to extract MAC address bytes
[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                         RTE_ETHER_ADDR_BYTES(&addr));
231
232         retval = rte_eth_promiscuous_enable(port);
233         if (retval != 0)
234                 return retval;
235
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. */
240
241         return 0;
242 }
243 /* >8 End of port initialization. */
244
245 /*
246  * Main thread that does the work, reading from INPUT_PORT
247  * and writing to OUTPUT_PORT
248  */
249 static  __rte_noreturn void
250 lcore_main(void)
251 {
252         uint16_t port;
253
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);
261
262         printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
263                         rte_lcore_id());
264         for (;;) {
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,
268                                         bufs, BURST_SIZE);
269                         if (unlikely(nb_rx == 0))
270                                 continue;
271                         const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
272                                         bufs, nb_rx);
273                         if (unlikely(nb_tx < nb_rx)) {
274                                 uint16_t buf;
275
276                                 for (buf = nb_tx; buf < nb_rx; buf++)
277                                         rte_pktmbuf_free(bufs[buf]);
278                         }
279                 }
280         }
281 }
282
283 /* Main function, does initialisation and calls the per-lcore functions */
284 int
285 main(int argc, char *argv[])
286 {
287         struct rte_mempool *mbuf_pool;
288         uint16_t nb_ports;
289         uint16_t portid;
290         struct option lgopts[] = {
291                 { NULL,  0, 0, 0 }
292         };
293         int opt, option_index;
294
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),
299         };
300
301         /* init EAL */
302         int ret = rte_eal_init(argc, argv);
303
304         if (ret < 0)
305                 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
306         argc -= ret;
307         argv += ret;
308
309         while ((opt = getopt_long(argc, argv, "t", lgopts, &option_index))
310                         != EOF)
311                 switch (opt) {
312                 case 't':
313                         hw_timestamping = 1;
314                         break;
315                 default:
316                         printf(usage, argv[0]);
317                         return -1;
318                 }
319         optind = 1; /* reset getopt lib */
320
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");
324
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");
330
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");
335
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",
340                                         portid);
341
342         if (rte_lcore_count() > 1)
343                 printf("\nWARNING: Too much enabled lcores - "
344                         "App uses only 1 lcore\n");
345
346         /* call lcore_main on main core only */
347         lcore_main();
348
349         /* clean up the EAL */
350         rte_eal_cleanup();
351
352         return 0;
353 }