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