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