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