examples/ioat: add option to control DMA batch size
[dpdk.git] / examples / ioat / ioatfwd.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <getopt.h>
7 #include <signal.h>
8 #include <stdbool.h>
9 #include <unistd.h>
10
11 #include <rte_malloc.h>
12 #include <rte_ethdev.h>
13 #include <rte_rawdev.h>
14 #include <rte_ioat_rawdev.h>
15
16 /* size of ring used for software copying between rx and tx. */
17 #define RTE_LOGTYPE_IOAT RTE_LOGTYPE_USER1
18 #define MAX_PKT_BURST 32
19 #define MEMPOOL_CACHE_SIZE 512
20 #define MIN_POOL_SIZE 65536U
21 #define CMD_LINE_OPT_MAC_UPDATING "mac-updating"
22 #define CMD_LINE_OPT_NO_MAC_UPDATING "no-mac-updating"
23 #define CMD_LINE_OPT_PORTMASK "portmask"
24 #define CMD_LINE_OPT_NB_QUEUE "nb-queue"
25 #define CMD_LINE_OPT_COPY_TYPE "copy-type"
26 #define CMD_LINE_OPT_RING_SIZE "ring-size"
27 #define CMD_LINE_OPT_BATCH_SIZE "dma-batch-size"
28
29 /* configurable number of RX/TX ring descriptors */
30 #define RX_DEFAULT_RINGSIZE 1024
31 #define TX_DEFAULT_RINGSIZE 1024
32
33 /* max number of RX queues per port */
34 #define MAX_RX_QUEUES_COUNT 8
35
36 struct rxtx_port_config {
37         /* common config */
38         uint16_t rxtx_port;
39         uint16_t nb_queues;
40         /* for software copy mode */
41         struct rte_ring *rx_to_tx_ring;
42         /* for IOAT rawdev copy mode */
43         uint16_t ioat_ids[MAX_RX_QUEUES_COUNT];
44 };
45
46 /* Configuring ports and number of assigned lcores in struct. 8< */
47 struct rxtx_transmission_config {
48         struct rxtx_port_config ports[RTE_MAX_ETHPORTS];
49         uint16_t nb_ports;
50         uint16_t nb_lcores;
51 };
52 /* >8 End of configuration of ports and number of assigned lcores. */
53
54 /* per-port statistics struct */
55 struct ioat_port_statistics {
56         uint64_t rx[RTE_MAX_ETHPORTS];
57         uint64_t tx[RTE_MAX_ETHPORTS];
58         uint64_t tx_dropped[RTE_MAX_ETHPORTS];
59         uint64_t copy_dropped[RTE_MAX_ETHPORTS];
60 };
61 struct ioat_port_statistics port_statistics;
62
63 struct total_statistics {
64         uint64_t total_packets_dropped;
65         uint64_t total_packets_tx;
66         uint64_t total_packets_rx;
67         uint64_t total_successful_enqueues;
68         uint64_t total_failed_enqueues;
69 };
70
71 typedef enum copy_mode_t {
72 #define COPY_MODE_SW "sw"
73         COPY_MODE_SW_NUM,
74 #define COPY_MODE_IOAT "hw"
75         COPY_MODE_IOAT_NUM,
76         COPY_MODE_INVALID_NUM,
77         COPY_MODE_SIZE_NUM = COPY_MODE_INVALID_NUM
78 } copy_mode_t;
79
80 /* mask of enabled ports */
81 static uint32_t ioat_enabled_port_mask;
82
83 /* number of RX queues per port */
84 static uint16_t nb_queues = 1;
85
86 /* MAC updating enabled by default. */
87 static int mac_updating = 1;
88
89 /* hardare copy mode enabled by default. */
90 static copy_mode_t copy_mode = COPY_MODE_IOAT_NUM;
91
92 /* size of IOAT rawdev ring for hardware copy mode or
93  * rte_ring for software copy mode
94  */
95 static unsigned short ring_size = 2048;
96
97 /* global transmission config */
98 struct rxtx_transmission_config cfg;
99
100 /* configurable number of RX/TX ring descriptors */
101 static uint16_t nb_rxd = RX_DEFAULT_RINGSIZE;
102 static uint16_t nb_txd = TX_DEFAULT_RINGSIZE;
103
104 static volatile bool force_quit;
105
106 static uint32_t ioat_batch_sz = MAX_PKT_BURST;
107
108 /* ethernet addresses of ports */
109 static struct rte_ether_addr ioat_ports_eth_addr[RTE_MAX_ETHPORTS];
110
111 static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
112 struct rte_mempool *ioat_pktmbuf_pool;
113
114 /* Print out statistics for one port. */
115 static void
116 print_port_stats(uint16_t port_id)
117 {
118         printf("\nStatistics for port %u ------------------------------"
119                 "\nPackets sent: %34"PRIu64
120                 "\nPackets received: %30"PRIu64
121                 "\nPackets dropped on tx: %25"PRIu64
122                 "\nPackets dropped on copy: %23"PRIu64,
123                 port_id,
124                 port_statistics.tx[port_id],
125                 port_statistics.rx[port_id],
126                 port_statistics.tx_dropped[port_id],
127                 port_statistics.copy_dropped[port_id]);
128 }
129
130 /* Print out statistics for one IOAT rawdev device. */
131 static void
132 print_rawdev_stats(uint32_t dev_id, uint64_t *xstats,
133         unsigned int *ids_xstats, uint16_t nb_xstats,
134         struct rte_rawdev_xstats_name *names_xstats)
135 {
136         uint16_t i;
137
138         printf("\nIOAT channel %u", dev_id);
139         for (i = 0; i < nb_xstats; i++)
140                 printf("\n\t %s: %*"PRIu64,
141                         names_xstats[ids_xstats[i]].name,
142                         (int)(37 - strlen(names_xstats[ids_xstats[i]].name)),
143                         xstats[i]);
144 }
145
146 static void
147 print_total_stats(struct total_statistics *ts)
148 {
149         printf("\nAggregate statistics ==============================="
150                 "\nTotal packets Tx: %24"PRIu64" [pps]"
151                 "\nTotal packets Rx: %24"PRIu64" [pps]"
152                 "\nTotal packets dropped: %19"PRIu64" [pps]",
153                 ts->total_packets_tx,
154                 ts->total_packets_rx,
155                 ts->total_packets_dropped);
156
157         if (copy_mode == COPY_MODE_IOAT_NUM) {
158                 printf("\nTotal IOAT successful enqueues: %8"PRIu64" [enq/s]"
159                         "\nTotal IOAT failed enqueues: %12"PRIu64" [enq/s]",
160                         ts->total_successful_enqueues,
161                         ts->total_failed_enqueues);
162         }
163
164         printf("\n====================================================\n");
165 }
166
167 /* Print out statistics on packets dropped. */
168 static void
169 print_stats(char *prgname)
170 {
171         struct total_statistics ts, delta_ts;
172         uint32_t i, port_id, dev_id;
173         struct rte_rawdev_xstats_name *names_xstats;
174         uint64_t *xstats;
175         unsigned int *ids_xstats, nb_xstats;
176         char status_string[255]; /* to print at the top of the output */
177         int status_strlen;
178         int ret;
179
180         const char clr[] = { 27, '[', '2', 'J', '\0' };
181         const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
182
183         status_strlen = snprintf(status_string, sizeof(status_string),
184                 "%s, ", prgname);
185         status_strlen += snprintf(status_string + status_strlen,
186                 sizeof(status_string) - status_strlen,
187                 "Worker Threads = %d, ",
188                 rte_lcore_count() > 2 ? 2 : 1);
189         status_strlen += snprintf(status_string + status_strlen,
190                 sizeof(status_string) - status_strlen,
191                 "Copy Mode = %s,\n", copy_mode == COPY_MODE_SW_NUM ?
192                 COPY_MODE_SW : COPY_MODE_IOAT);
193         status_strlen += snprintf(status_string + status_strlen,
194                 sizeof(status_string) - status_strlen,
195                 "Updating MAC = %s, ", mac_updating ?
196                 "enabled" : "disabled");
197         status_strlen += snprintf(status_string + status_strlen,
198                 sizeof(status_string) - status_strlen,
199                 "Rx Queues = %d, ", nb_queues);
200         status_strlen += snprintf(status_string + status_strlen,
201                 sizeof(status_string) - status_strlen,
202                 "Ring Size = %d", ring_size);
203
204         /* Allocate memory for xstats names and values */
205         ret = rte_rawdev_xstats_names_get(
206                 cfg.ports[0].ioat_ids[0], NULL, 0);
207         if (ret < 0)
208                 return;
209         nb_xstats = (unsigned int)ret;
210
211         names_xstats = malloc(sizeof(*names_xstats) * nb_xstats);
212         if (names_xstats == NULL) {
213                 rte_exit(EXIT_FAILURE,
214                         "Error allocating xstat names memory\n");
215         }
216         rte_rawdev_xstats_names_get(cfg.ports[0].ioat_ids[0],
217                 names_xstats, nb_xstats);
218
219         ids_xstats = malloc(sizeof(*ids_xstats) * 2);
220         if (ids_xstats == NULL) {
221                 rte_exit(EXIT_FAILURE,
222                         "Error allocating xstat ids_xstats memory\n");
223         }
224
225         xstats = malloc(sizeof(*xstats) * 2);
226         if (xstats == NULL) {
227                 rte_exit(EXIT_FAILURE,
228                         "Error allocating xstat memory\n");
229         }
230
231         /* Get failed/successful enqueues stats index */
232         ids_xstats[0] = ids_xstats[1] = nb_xstats;
233         for (i = 0; i < nb_xstats; i++) {
234                 if (!strcmp(names_xstats[i].name, "failed_enqueues"))
235                         ids_xstats[0] = i;
236                 else if (!strcmp(names_xstats[i].name, "successful_enqueues"))
237                         ids_xstats[1] = i;
238                 if (ids_xstats[0] < nb_xstats && ids_xstats[1] < nb_xstats)
239                         break;
240         }
241         if (ids_xstats[0] == nb_xstats || ids_xstats[1] == nb_xstats) {
242                 rte_exit(EXIT_FAILURE,
243                         "Error getting failed/successful enqueues stats index\n");
244         }
245
246         memset(&ts, 0, sizeof(struct total_statistics));
247
248         while (!force_quit) {
249                 /* Sleep for 1 second each round - init sleep allows reading
250                  * messages from app startup.
251                  */
252                 sleep(1);
253
254                 /* Clear screen and move to top left */
255                 printf("%s%s", clr, topLeft);
256
257                 memset(&delta_ts, 0, sizeof(struct total_statistics));
258
259                 printf("%s\n", status_string);
260
261                 for (i = 0; i < cfg.nb_ports; i++) {
262                         port_id = cfg.ports[i].rxtx_port;
263                         print_port_stats(port_id);
264
265                         delta_ts.total_packets_dropped +=
266                                 port_statistics.tx_dropped[port_id]
267                                 + port_statistics.copy_dropped[port_id];
268                         delta_ts.total_packets_tx +=
269                                 port_statistics.tx[port_id];
270                         delta_ts.total_packets_rx +=
271                                 port_statistics.rx[port_id];
272
273                         if (copy_mode == COPY_MODE_IOAT_NUM) {
274                                 uint32_t j;
275
276                                 for (j = 0; j < cfg.ports[i].nb_queues; j++) {
277                                         dev_id = cfg.ports[i].ioat_ids[j];
278                                         rte_rawdev_xstats_get(dev_id,
279                                                 ids_xstats, xstats, 2);
280
281                                         print_rawdev_stats(dev_id, xstats,
282                                                 ids_xstats, 2, names_xstats);
283
284                                         delta_ts.total_failed_enqueues +=
285                                                 xstats[ids_xstats[0]];
286                                         delta_ts.total_successful_enqueues +=
287                                                 xstats[ids_xstats[1]];
288                                 }
289                         }
290                 }
291
292                 delta_ts.total_packets_tx -= ts.total_packets_tx;
293                 delta_ts.total_packets_rx -= ts.total_packets_rx;
294                 delta_ts.total_packets_dropped -= ts.total_packets_dropped;
295                 delta_ts.total_failed_enqueues -= ts.total_failed_enqueues;
296                 delta_ts.total_successful_enqueues -=
297                         ts.total_successful_enqueues;
298
299                 printf("\n");
300                 print_total_stats(&delta_ts);
301
302                 fflush(stdout);
303
304                 ts.total_packets_tx += delta_ts.total_packets_tx;
305                 ts.total_packets_rx += delta_ts.total_packets_rx;
306                 ts.total_packets_dropped += delta_ts.total_packets_dropped;
307                 ts.total_failed_enqueues += delta_ts.total_failed_enqueues;
308                 ts.total_successful_enqueues +=
309                         delta_ts.total_successful_enqueues;
310         }
311
312         free(names_xstats);
313         free(xstats);
314         free(ids_xstats);
315 }
316
317 static void
318 update_mac_addrs(struct rte_mbuf *m, uint32_t dest_portid)
319 {
320         struct rte_ether_hdr *eth;
321         void *tmp;
322
323         eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
324
325         /* 02:00:00:00:00:xx - overwriting 2 bytes of source address but
326          * it's acceptable cause it gets overwritten by rte_ether_addr_copy
327          */
328         tmp = &eth->dst_addr.addr_bytes[0];
329         *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
330
331         /* src addr */
332         rte_ether_addr_copy(&ioat_ports_eth_addr[dest_portid], &eth->src_addr);
333 }
334
335 /* Perform packet copy there is a user-defined function. 8< */
336 static inline void
337 pktmbuf_metadata_copy(const struct rte_mbuf *src, struct rte_mbuf *dst)
338 {
339         dst->data_off = src->data_off;
340         memcpy(&dst->rx_descriptor_fields1, &src->rx_descriptor_fields1,
341                 offsetof(struct rte_mbuf, buf_len) -
342                 offsetof(struct rte_mbuf, rx_descriptor_fields1));
343 }
344
345 /* Copy packet data */
346 static inline void
347 pktmbuf_sw_copy(struct rte_mbuf *src, struct rte_mbuf *dst)
348 {
349         rte_memcpy(rte_pktmbuf_mtod(dst, char *),
350                 rte_pktmbuf_mtod(src, char *), src->data_len);
351 }
352 /* >8 End of perform packet copy there is a user-defined function. */
353
354 static uint32_t
355 ioat_enqueue_packets(struct rte_mbuf *pkts[], struct rte_mbuf *pkts_copy[],
356         uint32_t nb_rx, uint16_t dev_id)
357 {
358         int ret;
359         uint32_t i;
360
361         for (i = 0; i < nb_rx; i++) {
362                 /* Perform data copy */
363                 ret = rte_ioat_enqueue_copy(dev_id,
364                         rte_pktmbuf_iova(pkts[i]),
365                         rte_pktmbuf_iova(pkts_copy[i]),
366                         rte_pktmbuf_data_len(pkts[i]),
367                         (uintptr_t)pkts[i],
368                         (uintptr_t)pkts_copy[i]);
369
370                 if (ret != 1)
371                         break;
372         }
373
374         ret = i;
375         return ret;
376 }
377
378 static inline uint32_t
379 ioat_enqueue(struct rte_mbuf *pkts[], struct rte_mbuf *pkts_copy[],
380                 uint32_t num, uint32_t step, uint16_t dev_id)
381 {
382         uint32_t i, k, m, n;
383
384         k = 0;
385         for (i = 0; i < num; i += m) {
386
387                 m = RTE_MIN(step, num - i);
388                 n = ioat_enqueue_packets(pkts + i, pkts_copy + i, m, dev_id);
389                 k += n;
390                 if (n > 0)
391                         rte_ioat_perform_ops(dev_id);
392
393                 /* don't try to enqueue more if HW queue is full */
394                 if (n != m)
395                         break;
396         }
397
398         return k;
399 }
400
401 static inline uint32_t
402 ioat_dequeue(struct rte_mbuf *src[], struct rte_mbuf *dst[], uint32_t num,
403         uint16_t dev_id)
404 {
405         int32_t rc;
406         /* Dequeue the mbufs from IOAT device. Since all memory
407          * is DPDK pinned memory and therefore all addresses should
408          * be valid, we don't check for copy errors
409          */
410         rc = rte_ioat_completed_ops(dev_id, num, NULL, NULL,
411                 (void *)src, (void *)dst);
412         if (rc < 0) {
413                 RTE_LOG(CRIT, IOAT,
414                         "rte_ioat_completed_ops(%hu) failedi, error: %d\n",
415                         dev_id, rte_errno);
416                 rc = 0;
417         }
418         return rc;
419 }
420
421 /* Receive packets on one port and enqueue to IOAT rawdev or rte_ring. 8< */
422 static void
423 ioat_rx_port(struct rxtx_port_config *rx_config)
424 {
425         int32_t ret;
426         uint32_t nb_rx, nb_enq, i, j;
427         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
428         struct rte_mbuf *pkts_burst_copy[MAX_PKT_BURST];
429
430         for (i = 0; i < rx_config->nb_queues; i++) {
431
432                 nb_rx = rte_eth_rx_burst(rx_config->rxtx_port, i,
433                         pkts_burst, MAX_PKT_BURST);
434
435                 if (nb_rx == 0)
436                         continue;
437
438                 port_statistics.rx[rx_config->rxtx_port] += nb_rx;
439
440                 ret = rte_mempool_get_bulk(ioat_pktmbuf_pool,
441                         (void *)pkts_burst_copy, nb_rx);
442
443                 if (unlikely(ret < 0))
444                         rte_exit(EXIT_FAILURE,
445                                 "Unable to allocate memory.\n");
446
447                 for (j = 0; j < nb_rx; j++)
448                         pktmbuf_metadata_copy(pkts_burst[j],
449                                 pkts_burst_copy[j]);
450
451                 if (copy_mode == COPY_MODE_IOAT_NUM) {
452
453                         /* enqueue packets for  hardware copy */
454                         nb_enq = ioat_enqueue(pkts_burst, pkts_burst_copy,
455                                 nb_rx, ioat_batch_sz, rx_config->ioat_ids[i]);
456
457                         /* free any not enqueued packets. */
458                         rte_mempool_put_bulk(ioat_pktmbuf_pool,
459                                 (void *)&pkts_burst[nb_enq],
460                                 nb_rx - nb_enq);
461                         rte_mempool_put_bulk(ioat_pktmbuf_pool,
462                                 (void *)&pkts_burst_copy[nb_enq],
463                                 nb_rx - nb_enq);
464
465                         port_statistics.copy_dropped[rx_config->rxtx_port] +=
466                                 (nb_rx - nb_enq);
467
468                         /* get completed copies */
469                         nb_rx = ioat_dequeue(pkts_burst, pkts_burst_copy,
470                                 MAX_PKT_BURST, rx_config->ioat_ids[i]);
471                 } else {
472                         /* Perform packet software copy, free source packets */
473                         for (j = 0; j < nb_rx; j++)
474                                 pktmbuf_sw_copy(pkts_burst[j],
475                                         pkts_burst_copy[j]);
476                 }
477
478                 rte_mempool_put_bulk(ioat_pktmbuf_pool,
479                         (void *)pkts_burst, nb_rx);
480
481                 nb_enq = rte_ring_enqueue_burst(rx_config->rx_to_tx_ring,
482                         (void *)pkts_burst_copy, nb_rx, NULL);
483
484                 /* Free any not enqueued packets. */
485                 rte_mempool_put_bulk(ioat_pktmbuf_pool,
486                         (void *)&pkts_burst_copy[nb_enq],
487                         nb_rx - nb_enq);
488
489                 port_statistics.copy_dropped[rx_config->rxtx_port] +=
490                         (nb_rx - nb_enq);
491         }
492 }
493 /* >8 End of receive packets on one port and enqueue to IOAT rawdev or rte_ring. */
494
495 /* Transmit packets from IOAT rawdev/rte_ring for one port. 8< */
496 static void
497 ioat_tx_port(struct rxtx_port_config *tx_config)
498 {
499         uint32_t i, j, nb_dq, nb_tx;
500         struct rte_mbuf *mbufs[MAX_PKT_BURST];
501
502         for (i = 0; i < tx_config->nb_queues; i++) {
503
504                 /* Dequeue the mbufs from rx_to_tx_ring. */
505                 nb_dq = rte_ring_dequeue_burst(tx_config->rx_to_tx_ring,
506                                 (void *)mbufs, MAX_PKT_BURST, NULL);
507                 if (nb_dq == 0)
508                         continue;
509
510                 /* Update macs if enabled */
511                 if (mac_updating) {
512                         for (j = 0; j < nb_dq; j++)
513                                 update_mac_addrs(mbufs[j],
514                                         tx_config->rxtx_port);
515                 }
516
517                 nb_tx = rte_eth_tx_burst(tx_config->rxtx_port, 0,
518                                 (void *)mbufs, nb_dq);
519
520                 port_statistics.tx[tx_config->rxtx_port] += nb_tx;
521
522                 /* Free any unsent packets. */
523                 if (unlikely(nb_tx < nb_dq))
524                         rte_mempool_put_bulk(ioat_pktmbuf_pool,
525                         (void *)&mbufs[nb_tx], nb_dq - nb_tx);
526         }
527 }
528 /* >8 End of transmitting packets from IOAT. */
529
530 /* Main rx processing loop for IOAT rawdev. */
531 static void
532 rx_main_loop(void)
533 {
534         uint16_t i;
535         uint16_t nb_ports = cfg.nb_ports;
536
537         RTE_LOG(INFO, IOAT, "Entering main rx loop for copy on lcore %u\n",
538                 rte_lcore_id());
539
540         while (!force_quit)
541                 for (i = 0; i < nb_ports; i++)
542                         ioat_rx_port(&cfg.ports[i]);
543 }
544
545 /* Main tx processing loop for hardware copy. */
546 static void
547 tx_main_loop(void)
548 {
549         uint16_t i;
550         uint16_t nb_ports = cfg.nb_ports;
551
552         RTE_LOG(INFO, IOAT, "Entering main tx loop for copy on lcore %u\n",
553                 rte_lcore_id());
554
555         while (!force_quit)
556                 for (i = 0; i < nb_ports; i++)
557                         ioat_tx_port(&cfg.ports[i]);
558 }
559
560 /* Main rx and tx loop if only one worker lcore available */
561 static void
562 rxtx_main_loop(void)
563 {
564         uint16_t i;
565         uint16_t nb_ports = cfg.nb_ports;
566
567         RTE_LOG(INFO, IOAT, "Entering main rx and tx loop for copy on"
568                 " lcore %u\n", rte_lcore_id());
569
570         while (!force_quit)
571                 for (i = 0; i < nb_ports; i++) {
572                         ioat_rx_port(&cfg.ports[i]);
573                         ioat_tx_port(&cfg.ports[i]);
574                 }
575 }
576
577 /* Start processing for each lcore. 8< */
578 static void start_forwarding_cores(void)
579 {
580         uint32_t lcore_id = rte_lcore_id();
581
582         RTE_LOG(INFO, IOAT, "Entering %s on lcore %u\n",
583                 __func__, rte_lcore_id());
584
585         if (cfg.nb_lcores == 1) {
586                 lcore_id = rte_get_next_lcore(lcore_id, true, true);
587                 rte_eal_remote_launch((lcore_function_t *)rxtx_main_loop,
588                         NULL, lcore_id);
589         } else if (cfg.nb_lcores > 1) {
590                 lcore_id = rte_get_next_lcore(lcore_id, true, true);
591                 rte_eal_remote_launch((lcore_function_t *)rx_main_loop,
592                         NULL, lcore_id);
593
594                 lcore_id = rte_get_next_lcore(lcore_id, true, true);
595                 rte_eal_remote_launch((lcore_function_t *)tx_main_loop, NULL,
596                         lcore_id);
597         }
598 }
599 /* >8 End of starting to processfor each lcore. */
600
601 /* Display usage */
602 static void
603 ioat_usage(const char *prgname)
604 {
605         printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
606                 "  -b --dma-batch-size: number of requests per DMA batch\n"
607                 "  -p --portmask: hexadecimal bitmask of ports to configure\n"
608                 "  -q NQ: number of RX queues per port (default is 1)\n"
609                 "  --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
610                 "      When enabled:\n"
611                 "       - The source MAC address is replaced by the TX port MAC address\n"
612                 "       - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n"
613                 "  -c --copy-type CT: type of copy: sw|hw\n"
614                 "  -s --ring-size RS: size of IOAT rawdev ring for hardware copy mode or rte_ring for software copy mode\n",
615                         prgname);
616 }
617
618 static int
619 ioat_parse_portmask(const char *portmask)
620 {
621         char *end = NULL;
622         unsigned long pm;
623
624         /* Parse hexadecimal string */
625         pm = strtoul(portmask, &end, 16);
626         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
627                 return 0;
628
629         return pm;
630 }
631
632 static copy_mode_t
633 ioat_parse_copy_mode(const char *copy_mode)
634 {
635         if (strcmp(copy_mode, COPY_MODE_SW) == 0)
636                 return COPY_MODE_SW_NUM;
637         else if (strcmp(copy_mode, COPY_MODE_IOAT) == 0)
638                 return COPY_MODE_IOAT_NUM;
639
640         return COPY_MODE_INVALID_NUM;
641 }
642
643 /* Parse the argument given in the command line of the application */
644 static int
645 ioat_parse_args(int argc, char **argv, unsigned int nb_ports)
646 {
647         static const char short_options[] =
648                 "b:"  /* dma batch size */
649                 "c:"  /* copy type (sw|hw) */
650                 "p:"  /* portmask */
651                 "q:"  /* number of RX queues per port */
652                 "s:"  /* ring size */
653                 ;
654
655         static const struct option lgopts[] = {
656                 {CMD_LINE_OPT_MAC_UPDATING, no_argument, &mac_updating, 1},
657                 {CMD_LINE_OPT_NO_MAC_UPDATING, no_argument, &mac_updating, 0},
658                 {CMD_LINE_OPT_PORTMASK, required_argument, NULL, 'p'},
659                 {CMD_LINE_OPT_NB_QUEUE, required_argument, NULL, 'q'},
660                 {CMD_LINE_OPT_COPY_TYPE, required_argument, NULL, 'c'},
661                 {CMD_LINE_OPT_RING_SIZE, required_argument, NULL, 's'},
662                 {CMD_LINE_OPT_BATCH_SIZE, required_argument, NULL, 'b'},
663                 {NULL, 0, 0, 0}
664         };
665
666         const unsigned int default_port_mask = (1 << nb_ports) - 1;
667         int opt, ret;
668         char **argvopt;
669         int option_index;
670         char *prgname = argv[0];
671
672         ioat_enabled_port_mask = default_port_mask;
673         argvopt = argv;
674
675         while ((opt = getopt_long(argc, argvopt, short_options,
676                         lgopts, &option_index)) != EOF) {
677
678                 switch (opt) {
679                 case 'b':
680                         ioat_batch_sz = atoi(optarg);
681                         if (ioat_batch_sz > MAX_PKT_BURST) {
682                                 printf("Invalid dma batch size, %s.\n", optarg);
683                                 ioat_usage(prgname);
684                                 return -1;
685                         }
686                         break;
687                 /* portmask */
688                 case 'p':
689                         ioat_enabled_port_mask = ioat_parse_portmask(optarg);
690                         if (ioat_enabled_port_mask & ~default_port_mask ||
691                                         ioat_enabled_port_mask <= 0) {
692                                 printf("Invalid portmask, %s, suggest 0x%x\n",
693                                                 optarg, default_port_mask);
694                                 ioat_usage(prgname);
695                                 return -1;
696                         }
697                         break;
698
699                 case 'q':
700                         nb_queues = atoi(optarg);
701                         if (nb_queues == 0 || nb_queues > MAX_RX_QUEUES_COUNT) {
702                                 printf("Invalid RX queues number %s. Max %u\n",
703                                         optarg, MAX_RX_QUEUES_COUNT);
704                                 ioat_usage(prgname);
705                                 return -1;
706                         }
707                         break;
708
709                 case 'c':
710                         copy_mode = ioat_parse_copy_mode(optarg);
711                         if (copy_mode == COPY_MODE_INVALID_NUM) {
712                                 printf("Invalid copy type. Use: sw, hw\n");
713                                 ioat_usage(prgname);
714                                 return -1;
715                         }
716                         break;
717
718                 case 's':
719                         ring_size = atoi(optarg);
720                         if (ring_size == 0) {
721                                 printf("Invalid ring size, %s.\n", optarg);
722                                 ioat_usage(prgname);
723                                 return -1;
724                         }
725                         break;
726
727                 /* long options */
728                 case 0:
729                         break;
730
731                 default:
732                         ioat_usage(prgname);
733                         return -1;
734                 }
735         }
736
737         printf("MAC updating %s\n", mac_updating ? "enabled" : "disabled");
738         if (optind >= 0)
739                 argv[optind - 1] = prgname;
740
741         ret = optind - 1;
742         optind = 1; /* reset getopt lib */
743         return ret;
744 }
745
746 /* check link status, return true if at least one port is up */
747 static int
748 check_link_status(uint32_t port_mask)
749 {
750         uint16_t portid;
751         struct rte_eth_link link;
752         int ret, link_status = 0;
753         char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
754
755         printf("\nChecking link status\n");
756         RTE_ETH_FOREACH_DEV(portid) {
757                 if ((port_mask & (1 << portid)) == 0)
758                         continue;
759
760                 memset(&link, 0, sizeof(link));
761                 ret = rte_eth_link_get(portid, &link);
762                 if (ret < 0) {
763                         printf("Port %u link get failed: err=%d\n",
764                                         portid, ret);
765                         continue;
766                 }
767
768                 /* Print link status */
769                 rte_eth_link_to_str(link_status_text,
770                         sizeof(link_status_text), &link);
771                 printf("Port %d %s\n", portid, link_status_text);
772
773                 if (link.link_status)
774                         link_status = 1;
775         }
776         return link_status;
777 }
778
779 /* Configuration of device. 8< */
780 static void
781 configure_rawdev_queue(uint32_t dev_id)
782 {
783         struct rte_ioat_rawdev_config dev_config = {
784                         .ring_size = ring_size,
785                         .no_prefetch_completions = (cfg.nb_lcores > 1),
786         };
787         struct rte_rawdev_info info = { .dev_private = &dev_config };
788
789         if (rte_rawdev_configure(dev_id, &info, sizeof(dev_config)) != 0) {
790                 rte_exit(EXIT_FAILURE,
791                         "Error with rte_rawdev_configure()\n");
792         }
793         if (rte_rawdev_start(dev_id) != 0) {
794                 rte_exit(EXIT_FAILURE,
795                         "Error with rte_rawdev_start()\n");
796         }
797 }
798 /* >8 End of configuration of device. */
799
800 /* Using IOAT rawdev API functions. 8< */
801 static void
802 assign_rawdevs(void)
803 {
804         uint16_t nb_rawdev = 0, rdev_id = 0;
805         uint32_t i, j;
806
807         for (i = 0; i < cfg.nb_ports; i++) {
808                 for (j = 0; j < cfg.ports[i].nb_queues; j++) {
809                         struct rte_rawdev_info rdev_info = { 0 };
810
811                         do {
812                                 if (rdev_id == rte_rawdev_count())
813                                         goto end;
814                                 rte_rawdev_info_get(rdev_id++, &rdev_info, 0);
815                         } while (rdev_info.driver_name == NULL ||
816                                         strcmp(rdev_info.driver_name,
817                                                 IOAT_PMD_RAWDEV_NAME_STR) != 0);
818
819                         cfg.ports[i].ioat_ids[j] = rdev_id - 1;
820                         configure_rawdev_queue(cfg.ports[i].ioat_ids[j]);
821                         ++nb_rawdev;
822                 }
823         }
824 end:
825         if (nb_rawdev < cfg.nb_ports * cfg.ports[0].nb_queues)
826                 rte_exit(EXIT_FAILURE,
827                         "Not enough IOAT rawdevs (%u) for all queues (%u).\n",
828                         nb_rawdev, cfg.nb_ports * cfg.ports[0].nb_queues);
829         RTE_LOG(INFO, IOAT, "Number of used rawdevs: %u.\n", nb_rawdev);
830 }
831 /* >8 End of using IOAT rawdev API functions. */
832
833 /* Assign ring structures for packet exchanging. 8< */
834 static void
835 assign_rings(void)
836 {
837         uint32_t i;
838
839         for (i = 0; i < cfg.nb_ports; i++) {
840                 char ring_name[RTE_RING_NAMESIZE];
841
842                 snprintf(ring_name, sizeof(ring_name), "rx_to_tx_ring_%u", i);
843                 /* Create ring for inter core communication */
844                 cfg.ports[i].rx_to_tx_ring = rte_ring_create(
845                         ring_name, ring_size,
846                         rte_socket_id(), RING_F_SP_ENQ | RING_F_SC_DEQ);
847
848                 if (cfg.ports[i].rx_to_tx_ring == NULL)
849                         rte_exit(EXIT_FAILURE, "Ring create failed: %s\n",
850                                 rte_strerror(rte_errno));
851         }
852 }
853 /* >8 End of assigning ring structures for packet exchanging. */
854
855 /*
856  * Initializes a given port using global settings and with the RX buffers
857  * coming from the mbuf_pool passed as a parameter.
858  */
859 static inline void
860 port_init(uint16_t portid, struct rte_mempool *mbuf_pool, uint16_t nb_queues)
861 {
862         /* Configuring port to use RSS for multiple RX queues. 8< */
863         static const struct rte_eth_conf port_conf = {
864                 .rxmode = {
865                         .mq_mode = RTE_ETH_MQ_RX_RSS,
866                 },
867                 .rx_adv_conf = {
868                         .rss_conf = {
869                                 .rss_key = NULL,
870                                 .rss_hf = RTE_ETH_RSS_PROTO_MASK,
871                         }
872                 }
873         };
874         /* >8 End of configuring port to use RSS for multiple RX queues. */
875
876         struct rte_eth_rxconf rxq_conf;
877         struct rte_eth_txconf txq_conf;
878         struct rte_eth_conf local_port_conf = port_conf;
879         struct rte_eth_dev_info dev_info;
880         int ret, i;
881
882         /* Skip ports that are not enabled */
883         if ((ioat_enabled_port_mask & (1 << portid)) == 0) {
884                 printf("Skipping disabled port %u\n", portid);
885                 return;
886         }
887
888         /* Init port */
889         printf("Initializing port %u... ", portid);
890         fflush(stdout);
891         ret = rte_eth_dev_info_get(portid, &dev_info);
892         if (ret < 0)
893                 rte_exit(EXIT_FAILURE, "Cannot get device info: %s, port=%u\n",
894                         rte_strerror(-ret), portid);
895
896         local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
897                 dev_info.flow_type_rss_offloads;
898         ret = rte_eth_dev_configure(portid, nb_queues, 1, &local_port_conf);
899         if (ret < 0)
900                 rte_exit(EXIT_FAILURE, "Cannot configure device:"
901                         " err=%d, port=%u\n", ret, portid);
902
903         ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
904                         &nb_txd);
905         if (ret < 0)
906                 rte_exit(EXIT_FAILURE,
907                         "Cannot adjust number of descriptors: err=%d, port=%u\n",
908                         ret, portid);
909
910         rte_eth_macaddr_get(portid, &ioat_ports_eth_addr[portid]);
911
912         /* Init RX queues */
913         rxq_conf = dev_info.default_rxconf;
914         rxq_conf.offloads = local_port_conf.rxmode.offloads;
915         for (i = 0; i < nb_queues; i++) {
916                 ret = rte_eth_rx_queue_setup(portid, i, nb_rxd,
917                         rte_eth_dev_socket_id(portid), &rxq_conf,
918                         mbuf_pool);
919                 if (ret < 0)
920                         rte_exit(EXIT_FAILURE,
921                                 "rte_eth_rx_queue_setup:err=%d,port=%u, queue_id=%u\n",
922                                 ret, portid, i);
923         }
924
925         /* Init one TX queue on each port */
926         txq_conf = dev_info.default_txconf;
927         txq_conf.offloads = local_port_conf.txmode.offloads;
928         ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
929                         rte_eth_dev_socket_id(portid),
930                         &txq_conf);
931         if (ret < 0)
932                 rte_exit(EXIT_FAILURE,
933                         "rte_eth_tx_queue_setup:err=%d,port=%u\n",
934                         ret, portid);
935
936         /* Initialize TX buffers */
937         tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
938                         RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
939                         rte_eth_dev_socket_id(portid));
940         if (tx_buffer[portid] == NULL)
941                 rte_exit(EXIT_FAILURE,
942                         "Cannot allocate buffer for tx on port %u\n",
943                         portid);
944
945         rte_eth_tx_buffer_init(tx_buffer[portid], MAX_PKT_BURST);
946
947         ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[portid],
948                 rte_eth_tx_buffer_count_callback,
949                 &port_statistics.tx_dropped[portid]);
950         if (ret < 0)
951                 rte_exit(EXIT_FAILURE,
952                         "Cannot set error callback for tx buffer on port %u\n",
953                         portid);
954
955         /* Start device. 8< */
956         ret = rte_eth_dev_start(portid);
957         if (ret < 0)
958                 rte_exit(EXIT_FAILURE,
959                         "rte_eth_dev_start:err=%d, port=%u\n",
960                         ret, portid);
961         /* >8 End of starting device. */
962
963         /* RX port is set in promiscuous mode. 8< */
964         rte_eth_promiscuous_enable(portid);
965         /* >8 End of RX port is set in promiscuous mode. */
966
967         printf("Port %u, MAC address: " RTE_ETHER_ADDR_PRT_FMT "\n\n",
968                         portid,
969                         RTE_ETHER_ADDR_BYTES(&ioat_ports_eth_addr[portid]));
970
971         cfg.ports[cfg.nb_ports].rxtx_port = portid;
972         cfg.ports[cfg.nb_ports++].nb_queues = nb_queues;
973 }
974
975 static void
976 signal_handler(int signum)
977 {
978         if (signum == SIGINT || signum == SIGTERM) {
979                 printf("\n\nSignal %d received, preparing to exit...\n",
980                         signum);
981                 force_quit = true;
982         }
983 }
984
985 int
986 main(int argc, char **argv)
987 {
988         int ret;
989         uint16_t nb_ports, portid;
990         uint32_t i;
991         unsigned int nb_mbufs;
992
993         /* Init EAL. 8< */
994         ret = rte_eal_init(argc, argv);
995         if (ret < 0)
996                 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
997         /* >8 End of init EAL. */
998         argc -= ret;
999         argv += ret;
1000
1001         force_quit = false;
1002         signal(SIGINT, signal_handler);
1003         signal(SIGTERM, signal_handler);
1004
1005         nb_ports = rte_eth_dev_count_avail();
1006         if (nb_ports == 0)
1007                 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
1008
1009         /* Parse application arguments (after the EAL ones) */
1010         ret = ioat_parse_args(argc, argv, nb_ports);
1011         if (ret < 0)
1012                 rte_exit(EXIT_FAILURE, "Invalid IOAT arguments\n");
1013
1014         /* Allocates mempool to hold the mbufs. 8< */
1015         nb_mbufs = RTE_MAX(nb_ports * (nb_queues * (nb_rxd + nb_txd +
1016                 4 * MAX_PKT_BURST + ring_size) + ring_size +
1017                 rte_lcore_count() * MEMPOOL_CACHE_SIZE),
1018                 MIN_POOL_SIZE);
1019
1020         /* Create the mbuf pool */
1021         ioat_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", nb_mbufs,
1022                 MEMPOOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
1023                 rte_socket_id());
1024         if (ioat_pktmbuf_pool == NULL)
1025                 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
1026         /* >8 End of allocates mempool to hold the mbufs. */
1027
1028         /* Initialize each port. 8< */
1029         cfg.nb_ports = 0;
1030         RTE_ETH_FOREACH_DEV(portid)
1031                 port_init(portid, ioat_pktmbuf_pool, nb_queues);
1032         /* >8 End of initializing each port. */
1033
1034         /* Initialize port xstats */
1035         memset(&port_statistics, 0, sizeof(port_statistics));
1036
1037         /* Assigning each port resources. 8< */
1038         while (!check_link_status(ioat_enabled_port_mask) && !force_quit)
1039                 sleep(1);
1040
1041         /* Check if there is enough lcores for all ports. */
1042         cfg.nb_lcores = rte_lcore_count() - 1;
1043         if (cfg.nb_lcores < 1)
1044                 rte_exit(EXIT_FAILURE,
1045                         "There should be at least one worker lcore.\n");
1046
1047         if (copy_mode == COPY_MODE_IOAT_NUM)
1048                 assign_rawdevs();
1049
1050         assign_rings();
1051         /* >8 End of assigning each port resources. */
1052
1053         start_forwarding_cores();
1054         /* main core prints stats while other cores forward */
1055         print_stats(argv[0]);
1056
1057         /* force_quit is true when we get here */
1058         rte_eal_mp_wait_lcore();
1059
1060         uint32_t j;
1061         for (i = 0; i < cfg.nb_ports; i++) {
1062                 printf("Closing port %d\n", cfg.ports[i].rxtx_port);
1063                 ret = rte_eth_dev_stop(cfg.ports[i].rxtx_port);
1064                 if (ret != 0)
1065                         RTE_LOG(ERR, IOAT, "rte_eth_dev_stop: err=%s, port=%u\n",
1066                                 rte_strerror(-ret), cfg.ports[i].rxtx_port);
1067
1068                 rte_eth_dev_close(cfg.ports[i].rxtx_port);
1069                 if (copy_mode == COPY_MODE_IOAT_NUM) {
1070                         for (j = 0; j < cfg.ports[i].nb_queues; j++) {
1071                                 printf("Stopping rawdev %d\n",
1072                                         cfg.ports[i].ioat_ids[j]);
1073                                 rte_rawdev_stop(cfg.ports[i].ioat_ids[j]);
1074                         }
1075                 } else /* copy_mode == COPY_MODE_SW_NUM */
1076                         rte_ring_free(cfg.ports[i].rx_to_tx_ring);
1077         }
1078
1079         /* clean up the EAL */
1080         rte_eal_cleanup();
1081
1082         printf("Bye...\n");
1083         return 0;
1084 }