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