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