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