1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2021 Intel Corporation
11 #include <rte_malloc.h>
12 #include <rte_ethdev.h>
13 #include <rte_dmadev.h>
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"
30 /* configurable number of RX/TX ring descriptors */
31 #define RX_DEFAULT_RINGSIZE 1024
32 #define TX_DEFAULT_RINGSIZE 1024
34 /* max number of RX queues per port */
35 #define MAX_RX_QUEUES_COUNT 8
37 struct rxtx_port_config {
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];
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];
53 /* >8 End of configuration of ports and number of assigned lcores. */
55 /* per-port statistics struct */
56 struct dma_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];
62 struct dma_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;
72 typedef enum copy_mode_t {
73 #define COPY_MODE_SW "sw"
75 #define COPY_MODE_DMA "hw"
77 COPY_MODE_INVALID_NUM,
78 COPY_MODE_SIZE_NUM = COPY_MODE_INVALID_NUM
81 /* mask of enabled ports */
82 static uint32_t dma_enabled_port_mask;
84 /* number of RX queues per port */
85 static uint16_t nb_queues = 1;
87 /* MAC updating enabled by default. */
88 static int mac_updating = 1;
90 /* hardare copy mode enabled by default. */
91 static copy_mode_t copy_mode = COPY_MODE_DMA_NUM;
93 /* size of descriptor ring for hardware copy mode or
94 * rte_ring for software copy mode
96 static unsigned short ring_size = 2048;
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)
104 struct rte_mbuf *bufs[MBUF_RING_SIZE];
105 struct rte_mbuf *copies[MBUF_RING_SIZE];
108 static struct dma_bufs dma_bufs[RTE_DMADEV_DEFAULT_MAX];
110 /* global transmission config */
111 struct rxtx_transmission_config cfg;
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;
117 static volatile bool force_quit;
119 static uint32_t dma_batch_sz = MAX_PKT_BURST;
120 static uint32_t max_frame_size = RTE_ETHER_MAX_LEN;
122 /* ethernet addresses of ports */
123 static struct rte_ether_addr dma_ports_eth_addr[RTE_MAX_ETHPORTS];
125 static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
126 struct rte_mempool *dma_pktmbuf_pool;
128 /* Print out statistics for one port. */
130 print_port_stats(uint16_t port_id)
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,
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]);
144 /* Print out statistics for one dmadev device. */
146 print_dmadev_stats(uint32_t dev_id, struct rte_dma_stats stats)
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);
155 print_total_stats(struct total_statistics *ts)
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);
165 if (copy_mode == COPY_MODE_DMA_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);
174 printf("\n====================================================\n");
177 /* Print out statistics on packets dropped. */
179 print_stats(char *prgname)
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 */
187 const char clr[] = { 27, '[', '2', 'J', '\0' };
188 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
190 status_strlen = snprintf(status_string, sizeof(status_string),
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_DMA);
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);
211 memset(&ts, 0, sizeof(struct total_statistics));
213 while (!force_quit) {
214 /* Sleep for "stats_interval" seconds each round - init sleep allows reading
215 * messages from app startup.
217 sleep(stats_interval);
219 /* Clear screen and move to top left */
220 printf("%s%s", clr, topLeft);
222 memset(&delta_ts, 0, sizeof(struct total_statistics));
224 printf("%s\n", status_string);
226 for (i = 0; i < cfg.nb_ports; i++) {
227 port_id = cfg.ports[i].rxtx_port;
228 print_port_stats(port_id);
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];
238 if (copy_mode == COPY_MODE_DMA_NUM) {
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);
246 delta_ts.total_submitted += stats.submitted;
247 delta_ts.total_completed += stats.completed;
248 delta_ts.total_failed += stats.errors;
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;
261 print_total_stats(&delta_ts);
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;
275 update_mac_addrs(struct rte_mbuf *m, uint32_t dest_portid)
277 struct rte_ether_hdr *eth;
280 eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
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
285 tmp = ð->dst_addr.addr_bytes[0];
286 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
289 rte_ether_addr_copy(&dma_ports_eth_addr[dest_portid], ð->src_addr);
292 /* Perform packet copy there is a user-defined function. 8< */
294 pktmbuf_metadata_copy(const struct rte_mbuf *src, struct rte_mbuf *dst)
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));
302 /* Copy packet data */
304 pktmbuf_sw_copy(struct rte_mbuf *src, struct rte_mbuf *dst)
306 rte_memcpy(rte_pktmbuf_mtod(dst, char *),
307 rte_pktmbuf_mtod(src, char *), src->data_len);
309 /* >8 End of perform packet copy there is a user-defined function. */
312 dma_enqueue_packets(struct rte_mbuf *pkts[], struct rte_mbuf *pkts_copy[],
313 uint32_t nb_rx, uint16_t dev_id)
315 struct dma_bufs *dma = &dma_bufs[dev_id];
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);
329 dma->bufs[ret & MBUF_RING_MASK] = pkts[i];
330 dma->copies[ret & MBUF_RING_MASK] = pkts_copy[i];
337 static inline uint32_t
338 dma_enqueue(struct rte_mbuf *pkts[], struct rte_mbuf *pkts_copy[],
339 uint32_t num, uint32_t step, uint16_t dev_id)
344 for (i = 0; i < num; i += m) {
346 m = RTE_MIN(step, num - i);
347 n = dma_enqueue_packets(pkts + i, pkts_copy + i, m, dev_id);
350 rte_dma_submit(dev_id, 0);
352 /* don't try to enqueue more if HW queue is full */
360 static inline uint32_t
361 dma_dequeue(struct rte_mbuf *src[], struct rte_mbuf *dst[], uint32_t num,
364 struct dma_bufs *dma = &dma_bufs[dev_id];
365 uint16_t nb_dq, filled;
366 /* Dequeue the mbufs from DMA device. Since all memory
367 * is DPDK pinned memory and therefore all addresses should
368 * be valid, we don't check for copy errors
370 nb_dq = rte_dma_completed(dev_id, 0, num, NULL, NULL);
372 /* Return early if no work to do */
373 if (unlikely(nb_dq == 0))
376 /* Populate pkts_copy with the copies bufs from dma->copies for tx */
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];
387 /* Receive packets on one port and enqueue to dmadev or rte_ring. 8< */
389 dma_rx_port(struct rxtx_port_config *rx_config)
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];
396 for (i = 0; i < rx_config->nb_queues; i++) {
398 nb_rx = rte_eth_rx_burst(rx_config->rxtx_port, i,
399 pkts_burst, MAX_PKT_BURST);
404 port_statistics.rx[rx_config->rxtx_port] += nb_rx;
406 ret = rte_mempool_get_bulk(dma_pktmbuf_pool,
407 (void *)pkts_burst_copy, nb_rx);
409 if (unlikely(ret < 0))
410 rte_exit(EXIT_FAILURE,
411 "Unable to allocate memory.\n");
413 for (j = 0; j < nb_rx; j++)
414 pktmbuf_metadata_copy(pkts_burst[j],
417 if (copy_mode == COPY_MODE_DMA_NUM) {
418 /* enqueue packets for hardware copy */
419 nb_enq = dma_enqueue(pkts_burst, pkts_burst_copy,
420 nb_rx, dma_batch_sz, rx_config->dmadev_ids[i]);
422 /* free any not enqueued packets. */
423 rte_mempool_put_bulk(dma_pktmbuf_pool,
424 (void *)&pkts_burst[nb_enq],
426 rte_mempool_put_bulk(dma_pktmbuf_pool,
427 (void *)&pkts_burst_copy[nb_enq],
430 port_statistics.copy_dropped[rx_config->rxtx_port] +=
433 /* get completed copies */
434 nb_rx = dma_dequeue(pkts_burst, pkts_burst_copy,
435 MAX_PKT_BURST, rx_config->dmadev_ids[i]);
437 /* Perform packet software copy, free source packets */
438 for (j = 0; j < nb_rx; j++)
439 pktmbuf_sw_copy(pkts_burst[j],
443 rte_mempool_put_bulk(dma_pktmbuf_pool,
444 (void *)pkts_burst, nb_rx);
446 nb_enq = rte_ring_enqueue_burst(rx_config->rx_to_tx_ring,
447 (void *)pkts_burst_copy, nb_rx, NULL);
449 /* Free any not enqueued packets. */
450 rte_mempool_put_bulk(dma_pktmbuf_pool,
451 (void *)&pkts_burst_copy[nb_enq],
454 port_statistics.copy_dropped[rx_config->rxtx_port] +=
458 /* >8 End of receive packets on one port and enqueue to dmadev or rte_ring. */
460 /* Transmit packets from dmadev/rte_ring for one port. 8< */
462 dma_tx_port(struct rxtx_port_config *tx_config)
464 uint32_t i, j, nb_dq, nb_tx;
465 struct rte_mbuf *mbufs[MAX_PKT_BURST];
467 for (i = 0; i < tx_config->nb_queues; i++) {
469 /* Dequeue the mbufs from rx_to_tx_ring. */
470 nb_dq = rte_ring_dequeue_burst(tx_config->rx_to_tx_ring,
471 (void *)mbufs, MAX_PKT_BURST, NULL);
475 /* Update macs if enabled */
477 for (j = 0; j < nb_dq; j++)
478 update_mac_addrs(mbufs[j],
479 tx_config->rxtx_port);
482 nb_tx = rte_eth_tx_burst(tx_config->rxtx_port, 0,
483 (void *)mbufs, nb_dq);
485 port_statistics.tx[tx_config->rxtx_port] += nb_tx;
487 /* Free any unsent packets. */
488 if (unlikely(nb_tx < nb_dq))
489 rte_mempool_put_bulk(dma_pktmbuf_pool,
490 (void *)&mbufs[nb_tx], nb_dq - nb_tx);
493 /* >8 End of transmitting packets from dmadev. */
495 /* Main rx processing loop for dmadev. */
500 uint16_t nb_ports = cfg.nb_ports;
502 RTE_LOG(INFO, DMA, "Entering main rx loop for copy on lcore %u\n",
506 for (i = 0; i < nb_ports; i++)
507 dma_rx_port(&cfg.ports[i]);
510 /* Main tx processing loop for hardware copy. */
515 uint16_t nb_ports = cfg.nb_ports;
517 RTE_LOG(INFO, DMA, "Entering main tx loop for copy on lcore %u\n",
521 for (i = 0; i < nb_ports; i++)
522 dma_tx_port(&cfg.ports[i]);
525 /* Main rx and tx loop if only one worker lcore available */
530 uint16_t nb_ports = cfg.nb_ports;
532 RTE_LOG(INFO, DMA, "Entering main rx and tx loop for copy on"
533 " lcore %u\n", rte_lcore_id());
536 for (i = 0; i < nb_ports; i++) {
537 dma_rx_port(&cfg.ports[i]);
538 dma_tx_port(&cfg.ports[i]);
542 /* Start processing for each lcore. 8< */
543 static void start_forwarding_cores(void)
545 uint32_t lcore_id = rte_lcore_id();
547 RTE_LOG(INFO, DMA, "Entering %s on lcore %u\n",
548 __func__, rte_lcore_id());
550 if (cfg.nb_lcores == 1) {
551 lcore_id = rte_get_next_lcore(lcore_id, true, true);
552 rte_eal_remote_launch((lcore_function_t *)rxtx_main_loop,
554 } else if (cfg.nb_lcores > 1) {
555 lcore_id = rte_get_next_lcore(lcore_id, true, true);
556 rte_eal_remote_launch((lcore_function_t *)rx_main_loop,
559 lcore_id = rte_get_next_lcore(lcore_id, true, true);
560 rte_eal_remote_launch((lcore_function_t *)tx_main_loop, NULL,
564 /* >8 End of starting to processfor each lcore. */
568 dma_usage(const char *prgname)
570 printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
571 " -b --dma-batch-size: number of requests per DMA batch\n"
572 " -f --max-frame-size: max frame size\n"
573 " -p --portmask: hexadecimal bitmask of ports to configure\n"
574 " -q NQ: number of RX queues per port (default is 1)\n"
575 " --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
577 " - The source MAC address is replaced by the TX port MAC address\n"
578 " - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n"
579 " -c --copy-type CT: type of copy: sw|hw\n"
580 " -s --ring-size RS: size of dmadev descriptor ring for hardware copy mode or rte_ring for software copy mode\n"
581 " -i --stats-interval SI: interval, in seconds, between stats prints (default is 1)\n",
586 dma_parse_portmask(const char *portmask)
591 /* Parse hexadecimal string */
592 pm = strtoul(portmask, &end, 16);
593 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
600 dma_parse_copy_mode(const char *copy_mode)
602 if (strcmp(copy_mode, COPY_MODE_SW) == 0)
603 return COPY_MODE_SW_NUM;
604 else if (strcmp(copy_mode, COPY_MODE_DMA) == 0)
605 return COPY_MODE_DMA_NUM;
607 return COPY_MODE_INVALID_NUM;
610 /* Parse the argument given in the command line of the application */
612 dma_parse_args(int argc, char **argv, unsigned int nb_ports)
614 static const char short_options[] =
615 "b:" /* dma batch size */
616 "c:" /* copy type (sw|hw) */
617 "f:" /* max frame size */
619 "q:" /* number of RX queues per port */
621 "i:" /* interval, in seconds, between stats prints */
624 static const struct option lgopts[] = {
625 {CMD_LINE_OPT_MAC_UPDATING, no_argument, &mac_updating, 1},
626 {CMD_LINE_OPT_NO_MAC_UPDATING, no_argument, &mac_updating, 0},
627 {CMD_LINE_OPT_PORTMASK, required_argument, NULL, 'p'},
628 {CMD_LINE_OPT_NB_QUEUE, required_argument, NULL, 'q'},
629 {CMD_LINE_OPT_COPY_TYPE, required_argument, NULL, 'c'},
630 {CMD_LINE_OPT_RING_SIZE, required_argument, NULL, 's'},
631 {CMD_LINE_OPT_BATCH_SIZE, required_argument, NULL, 'b'},
632 {CMD_LINE_OPT_FRAME_SIZE, required_argument, NULL, 'f'},
633 {CMD_LINE_OPT_STATS_INTERVAL, required_argument, NULL, 'i'},
637 const unsigned int default_port_mask = (1 << nb_ports) - 1;
641 char *prgname = argv[0];
643 dma_enabled_port_mask = default_port_mask;
646 while ((opt = getopt_long(argc, argvopt, short_options,
647 lgopts, &option_index)) != EOF) {
651 dma_batch_sz = atoi(optarg);
652 if (dma_batch_sz > MAX_PKT_BURST) {
653 printf("Invalid dma batch size, %s.\n", optarg);
659 max_frame_size = atoi(optarg);
660 if (max_frame_size > RTE_ETHER_MAX_JUMBO_FRAME_LEN) {
661 printf("Invalid max frame size, %s.\n", optarg);
669 dma_enabled_port_mask = dma_parse_portmask(optarg);
670 if (dma_enabled_port_mask & ~default_port_mask ||
671 dma_enabled_port_mask <= 0) {
672 printf("Invalid portmask, %s, suggest 0x%x\n",
673 optarg, default_port_mask);
680 nb_queues = atoi(optarg);
681 if (nb_queues == 0 || nb_queues > MAX_RX_QUEUES_COUNT) {
682 printf("Invalid RX queues number %s. Max %u\n",
683 optarg, MAX_RX_QUEUES_COUNT);
690 copy_mode = dma_parse_copy_mode(optarg);
691 if (copy_mode == COPY_MODE_INVALID_NUM) {
692 printf("Invalid copy type. Use: sw, hw\n");
699 ring_size = atoi(optarg);
700 if (ring_size == 0) {
701 printf("Invalid ring size, %s.\n", optarg);
705 /* ring_size must be less-than or equal to MBUF_RING_SIZE
706 * to avoid overwriting bufs
708 if (ring_size > MBUF_RING_SIZE) {
709 printf("Max ring_size is %d, setting ring_size to max",
711 ring_size = MBUF_RING_SIZE;
716 stats_interval = atoi(optarg);
717 if (stats_interval == 0) {
718 printf("Invalid stats interval, setting to 1\n");
719 stats_interval = 1; /* set to default */
733 printf("MAC updating %s\n", mac_updating ? "enabled" : "disabled");
735 argv[optind - 1] = prgname;
738 optind = 1; /* reset getopt lib */
742 /* check link status, return true if at least one port is up */
744 check_link_status(uint32_t port_mask)
747 struct rte_eth_link link;
748 int ret, link_status = 0;
749 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
751 printf("\nChecking link status\n");
752 RTE_ETH_FOREACH_DEV(portid) {
753 if ((port_mask & (1 << portid)) == 0)
756 memset(&link, 0, sizeof(link));
757 ret = rte_eth_link_get(portid, &link);
759 printf("Port %u link get failed: err=%d\n",
764 /* Print link status */
765 rte_eth_link_to_str(link_status_text,
766 sizeof(link_status_text), &link);
767 printf("Port %d %s\n", portid, link_status_text);
769 if (link.link_status)
775 /* Configuration of device. 8< */
777 configure_dmadev_queue(uint32_t dev_id)
779 struct rte_dma_info info;
780 struct rte_dma_conf dev_config = { .nb_vchans = 1 };
781 struct rte_dma_vchan_conf qconf = {
782 .direction = RTE_DMA_DIR_MEM_TO_MEM,
787 if (rte_dma_configure(dev_id, &dev_config) != 0)
788 rte_exit(EXIT_FAILURE, "Error with rte_dma_configure()\n");
790 if (rte_dma_vchan_setup(dev_id, vchan, &qconf) != 0) {
791 printf("Error with queue configuration\n");
794 rte_dma_info_get(dev_id, &info);
795 if (info.nb_vchans != 1) {
796 printf("Error, no configured queues reported on device id %u\n", dev_id);
799 if (rte_dma_start(dev_id) != 0)
800 rte_exit(EXIT_FAILURE, "Error with rte_dma_start()\n");
802 /* >8 End of configuration of device. */
804 /* Using dmadev API functions. 8< */
808 uint16_t nb_dmadev = 0;
809 int16_t dev_id = rte_dma_next_dev(0);
812 for (i = 0; i < cfg.nb_ports; i++) {
813 for (j = 0; j < cfg.ports[i].nb_queues; j++) {
817 cfg.ports[i].dmadev_ids[j] = dev_id;
818 configure_dmadev_queue(cfg.ports[i].dmadev_ids[j]);
819 dev_id = rte_dma_next_dev(dev_id + 1);
824 if (nb_dmadev < cfg.nb_ports * cfg.ports[0].nb_queues)
825 rte_exit(EXIT_FAILURE,
826 "Not enough dmadevs (%u) for all queues (%u).\n",
827 nb_dmadev, cfg.nb_ports * cfg.ports[0].nb_queues);
828 RTE_LOG(INFO, DMA, "Number of used dmadevs: %u.\n", nb_dmadev);
830 /* >8 End of using dmadev API functions. */
832 /* Assign ring structures for packet exchanging. 8< */
838 for (i = 0; i < cfg.nb_ports; i++) {
839 char ring_name[RTE_RING_NAMESIZE];
841 snprintf(ring_name, sizeof(ring_name), "rx_to_tx_ring_%u", i);
842 /* Create ring for inter core communication */
843 cfg.ports[i].rx_to_tx_ring = rte_ring_create(
844 ring_name, ring_size,
845 rte_socket_id(), RING_F_SP_ENQ | RING_F_SC_DEQ);
847 if (cfg.ports[i].rx_to_tx_ring == NULL)
848 rte_exit(EXIT_FAILURE, "Ring create failed: %s\n",
849 rte_strerror(rte_errno));
852 /* >8 End of assigning ring structures for packet exchanging. */
855 * Initializes a given port using global settings and with the RX buffers
856 * coming from the mbuf_pool passed as a parameter.
859 port_init(uint16_t portid, struct rte_mempool *mbuf_pool, uint16_t nb_queues)
861 /* Configuring port to use RSS for multiple RX queues. 8< */
862 static const struct rte_eth_conf port_conf = {
864 .mq_mode = RTE_ETH_MQ_RX_RSS,
869 .rss_hf = RTE_ETH_RSS_PROTO_MASK,
873 /* >8 End of configuring port to use RSS for multiple RX queues. */
875 struct rte_eth_rxconf rxq_conf;
876 struct rte_eth_txconf txq_conf;
877 struct rte_eth_conf local_port_conf = port_conf;
878 struct rte_eth_dev_info dev_info;
881 if (max_frame_size > local_port_conf.rxmode.mtu)
882 local_port_conf.rxmode.mtu = max_frame_size;
884 /* Skip ports that are not enabled */
885 if ((dma_enabled_port_mask & (1 << portid)) == 0) {
886 printf("Skipping disabled port %u\n", portid);
891 printf("Initializing port %u... ", portid);
893 ret = rte_eth_dev_info_get(portid, &dev_info);
895 rte_exit(EXIT_FAILURE, "Cannot get device info: %s, port=%u\n",
896 rte_strerror(-ret), portid);
898 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
899 dev_info.flow_type_rss_offloads;
900 ret = rte_eth_dev_configure(portid, nb_queues, 1, &local_port_conf);
902 rte_exit(EXIT_FAILURE, "Cannot configure device:"
903 " err=%d, port=%u\n", ret, portid);
905 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
908 rte_exit(EXIT_FAILURE,
909 "Cannot adjust number of descriptors: err=%d, port=%u\n",
912 rte_eth_macaddr_get(portid, &dma_ports_eth_addr[portid]);
915 rxq_conf = dev_info.default_rxconf;
916 rxq_conf.offloads = local_port_conf.rxmode.offloads;
917 for (i = 0; i < nb_queues; i++) {
918 ret = rte_eth_rx_queue_setup(portid, i, nb_rxd,
919 rte_eth_dev_socket_id(portid), &rxq_conf,
922 rte_exit(EXIT_FAILURE,
923 "rte_eth_rx_queue_setup:err=%d,port=%u, queue_id=%u\n",
927 /* Init one TX queue on each port */
928 txq_conf = dev_info.default_txconf;
929 txq_conf.offloads = local_port_conf.txmode.offloads;
930 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
931 rte_eth_dev_socket_id(portid),
934 rte_exit(EXIT_FAILURE,
935 "rte_eth_tx_queue_setup:err=%d,port=%u\n",
938 /* Initialize TX buffers */
939 tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
940 RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
941 rte_eth_dev_socket_id(portid));
942 if (tx_buffer[portid] == NULL)
943 rte_exit(EXIT_FAILURE,
944 "Cannot allocate buffer for tx on port %u\n",
947 rte_eth_tx_buffer_init(tx_buffer[portid], MAX_PKT_BURST);
949 ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[portid],
950 rte_eth_tx_buffer_count_callback,
951 &port_statistics.tx_dropped[portid]);
953 rte_exit(EXIT_FAILURE,
954 "Cannot set error callback for tx buffer on port %u\n",
957 /* Start device. 8< */
958 ret = rte_eth_dev_start(portid);
960 rte_exit(EXIT_FAILURE,
961 "rte_eth_dev_start:err=%d, port=%u\n",
963 /* >8 End of starting device. */
965 /* RX port is set in promiscuous mode. 8< */
966 rte_eth_promiscuous_enable(portid);
967 /* >8 End of RX port is set in promiscuous mode. */
969 printf("Port %u, MAC address: " RTE_ETHER_ADDR_PRT_FMT "\n\n",
971 RTE_ETHER_ADDR_BYTES(&dma_ports_eth_addr[portid]));
973 cfg.ports[cfg.nb_ports].rxtx_port = portid;
974 cfg.ports[cfg.nb_ports++].nb_queues = nb_queues;
977 /* Get a device dump for each device being used by the application */
983 if (copy_mode != COPY_MODE_DMA_NUM)
986 for (i = 0; i < cfg.nb_ports; i++)
987 for (j = 0; j < cfg.ports[i].nb_queues; j++)
988 rte_dma_dump(cfg.ports[i].dmadev_ids[j], stdout);
992 signal_handler(int signum)
994 if (signum == SIGINT || signum == SIGTERM) {
995 printf("\n\nSignal %d received, preparing to exit...\n",
998 } else if (signum == SIGUSR1) {
1004 main(int argc, char **argv)
1007 uint16_t nb_ports, portid;
1009 unsigned int nb_mbufs;
1013 ret = rte_eal_init(argc, argv);
1015 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
1016 /* >8 End of init EAL. */
1021 signal(SIGINT, signal_handler);
1022 signal(SIGTERM, signal_handler);
1023 signal(SIGUSR1, signal_handler);
1025 nb_ports = rte_eth_dev_count_avail();
1027 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
1029 /* Parse application arguments (after the EAL ones) */
1030 ret = dma_parse_args(argc, argv, nb_ports);
1032 rte_exit(EXIT_FAILURE, "Invalid DMA arguments\n");
1034 /* Allocates mempool to hold the mbufs. 8< */
1035 nb_mbufs = RTE_MAX(nb_ports * (nb_queues * (nb_rxd + nb_txd +
1036 4 * MAX_PKT_BURST + ring_size) + ring_size +
1037 rte_lcore_count() * MEMPOOL_CACHE_SIZE),
1040 /* Create the mbuf pool */
1041 sz = max_frame_size + RTE_PKTMBUF_HEADROOM;
1042 sz = RTE_MAX(sz, (size_t)RTE_MBUF_DEFAULT_BUF_SIZE);
1043 dma_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", nb_mbufs,
1044 MEMPOOL_CACHE_SIZE, 0, sz, rte_socket_id());
1045 if (dma_pktmbuf_pool == NULL)
1046 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
1047 /* >8 End of allocates mempool to hold the mbufs. */
1049 /* Initialize each port. 8< */
1051 RTE_ETH_FOREACH_DEV(portid)
1052 port_init(portid, dma_pktmbuf_pool, nb_queues);
1053 /* >8 End of initializing each port. */
1055 /* Initialize port xstats */
1056 memset(&port_statistics, 0, sizeof(port_statistics));
1058 /* Assigning each port resources. 8< */
1059 while (!check_link_status(dma_enabled_port_mask) && !force_quit)
1062 /* Check if there is enough lcores for all ports. */
1063 cfg.nb_lcores = rte_lcore_count() - 1;
1064 if (cfg.nb_lcores < 1)
1065 rte_exit(EXIT_FAILURE,
1066 "There should be at least one worker lcore.\n");
1068 if (copy_mode == COPY_MODE_DMA_NUM)
1072 /* >8 End of assigning each port resources. */
1074 start_forwarding_cores();
1075 /* main core prints stats while other cores forward */
1076 print_stats(argv[0]);
1078 /* force_quit is true when we get here */
1079 rte_eal_mp_wait_lcore();
1082 for (i = 0; i < cfg.nb_ports; i++) {
1083 printf("Closing port %d\n", cfg.ports[i].rxtx_port);
1084 ret = rte_eth_dev_stop(cfg.ports[i].rxtx_port);
1086 RTE_LOG(ERR, DMA, "rte_eth_dev_stop: err=%s, port=%u\n",
1087 rte_strerror(-ret), cfg.ports[i].rxtx_port);
1089 rte_eth_dev_close(cfg.ports[i].rxtx_port);
1090 if (copy_mode == COPY_MODE_DMA_NUM) {
1091 for (j = 0; j < cfg.ports[i].nb_queues; j++) {
1092 printf("Stopping dmadev %d\n",
1093 cfg.ports[i].dmadev_ids[j]);
1094 rte_dma_stop(cfg.ports[i].dmadev_ids[j]);
1096 } else /* copy_mode == COPY_MODE_SW_NUM */
1097 rte_ring_free(cfg.ports[i].rx_to_tx_ring);
1100 /* clean up the EAL */