1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Intel Corporation
11 #include <rte_malloc.h>
12 #include <rte_ethdev.h>
13 #include <rte_rawdev.h>
14 #include <rte_ioat_rawdev.h>
16 /* size of ring used for software copying between rx and tx. */
17 #define RTE_LOGTYPE_IOAT RTE_LOGTYPE_USER1
18 #define MAX_PKT_BURST 32
19 #define MEMPOOL_CACHE_SIZE 512
20 #define MIN_POOL_SIZE 65536U
21 #define CMD_LINE_OPT_MAC_UPDATING "mac-updating"
22 #define CMD_LINE_OPT_NO_MAC_UPDATING "no-mac-updating"
23 #define CMD_LINE_OPT_PORTMASK "portmask"
24 #define CMD_LINE_OPT_NB_QUEUE "nb-queue"
25 #define CMD_LINE_OPT_COPY_TYPE "copy-type"
26 #define CMD_LINE_OPT_RING_SIZE "ring-size"
27 #define CMD_LINE_OPT_BATCH_SIZE "dma-batch-size"
28 #define CMD_LINE_OPT_FRAME_SIZE "max-frame-size"
29 #define CMD_LINE_OPT_STATS_INTERVAL "stats-interval"
31 /* configurable number of RX/TX ring descriptors */
32 #define RX_DEFAULT_RINGSIZE 1024
33 #define TX_DEFAULT_RINGSIZE 1024
35 /* max number of RX queues per port */
36 #define MAX_RX_QUEUES_COUNT 8
38 struct rxtx_port_config {
42 /* for software copy mode */
43 struct rte_ring *rx_to_tx_ring;
44 /* for IOAT rawdev copy mode */
45 uint16_t ioat_ids[MAX_RX_QUEUES_COUNT];
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];
54 /* >8 End of configuration of ports and number of assigned lcores. */
56 /* per-port statistics struct */
57 struct ioat_port_statistics {
58 uint64_t rx[RTE_MAX_ETHPORTS];
59 uint64_t tx[RTE_MAX_ETHPORTS];
60 uint64_t tx_dropped[RTE_MAX_ETHPORTS];
61 uint64_t copy_dropped[RTE_MAX_ETHPORTS];
63 struct ioat_port_statistics port_statistics;
65 struct total_statistics {
66 uint64_t total_packets_dropped;
67 uint64_t total_packets_tx;
68 uint64_t total_packets_rx;
69 uint64_t total_successful_enqueues;
70 uint64_t total_failed_enqueues;
73 typedef enum copy_mode_t {
74 #define COPY_MODE_SW "sw"
76 #define COPY_MODE_IOAT "hw"
78 COPY_MODE_INVALID_NUM,
79 COPY_MODE_SIZE_NUM = COPY_MODE_INVALID_NUM
82 /* mask of enabled ports */
83 static uint32_t ioat_enabled_port_mask;
85 /* number of RX queues per port */
86 static uint16_t nb_queues = 1;
88 /* MAC updating enabled by default. */
89 static int mac_updating = 1;
91 /* hardare copy mode enabled by default. */
92 static copy_mode_t copy_mode = COPY_MODE_IOAT_NUM;
94 /* size of IOAT rawdev ring for hardware copy mode or
95 * rte_ring for software copy mode
97 static unsigned short ring_size = 2048;
99 /* interval, in seconds, between stats prints */
100 static unsigned short stats_interval = 1;
102 /* global transmission config */
103 struct rxtx_transmission_config cfg;
105 /* configurable number of RX/TX ring descriptors */
106 static uint16_t nb_rxd = RX_DEFAULT_RINGSIZE;
107 static uint16_t nb_txd = TX_DEFAULT_RINGSIZE;
109 static volatile bool force_quit;
111 static uint32_t ioat_batch_sz = MAX_PKT_BURST;
112 static uint32_t max_frame_size = RTE_ETHER_MAX_LEN;
114 /* ethernet addresses of ports */
115 static struct rte_ether_addr ioat_ports_eth_addr[RTE_MAX_ETHPORTS];
117 static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
118 struct rte_mempool *ioat_pktmbuf_pool;
120 /* Print out statistics for one port. */
122 print_port_stats(uint16_t port_id)
124 printf("\nStatistics for port %u ------------------------------"
125 "\nPackets sent: %34"PRIu64
126 "\nPackets received: %30"PRIu64
127 "\nPackets dropped on tx: %25"PRIu64
128 "\nPackets dropped on copy: %23"PRIu64,
130 port_statistics.tx[port_id],
131 port_statistics.rx[port_id],
132 port_statistics.tx_dropped[port_id],
133 port_statistics.copy_dropped[port_id]);
136 /* Print out statistics for one IOAT rawdev device. */
138 print_rawdev_stats(uint32_t dev_id, uint64_t *xstats,
139 unsigned int *ids_xstats, uint16_t nb_xstats,
140 struct rte_rawdev_xstats_name *names_xstats)
144 printf("\nIOAT channel %u", dev_id);
145 for (i = 0; i < nb_xstats; i++)
146 printf("\n\t %s: %*"PRIu64,
147 names_xstats[ids_xstats[i]].name,
148 (int)(37 - strlen(names_xstats[ids_xstats[i]].name)),
153 print_total_stats(struct total_statistics *ts)
155 printf("\nAggregate statistics ==============================="
156 "\nTotal packets Tx: %24"PRIu64" [pps]"
157 "\nTotal packets Rx: %24"PRIu64" [pps]"
158 "\nTotal packets dropped: %19"PRIu64" [pps]",
159 ts->total_packets_tx / stats_interval,
160 ts->total_packets_rx / stats_interval,
161 ts->total_packets_dropped / stats_interval);
163 if (copy_mode == COPY_MODE_IOAT_NUM) {
164 printf("\nTotal IOAT successful enqueues: %8"PRIu64" [enq/s]"
165 "\nTotal IOAT failed enqueues: %12"PRIu64" [enq/s]",
166 ts->total_successful_enqueues / stats_interval,
167 ts->total_failed_enqueues / stats_interval);
170 printf("\n====================================================\n");
173 /* Print out statistics on packets dropped. */
175 print_stats(char *prgname)
177 struct total_statistics ts, delta_ts;
178 uint32_t i, port_id, dev_id;
179 struct rte_rawdev_xstats_name *names_xstats;
181 unsigned int *ids_xstats, nb_xstats;
182 char status_string[255]; /* to print at the top of the output */
186 const char clr[] = { 27, '[', '2', 'J', '\0' };
187 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
189 status_strlen = snprintf(status_string, sizeof(status_string),
191 status_strlen += snprintf(status_string + status_strlen,
192 sizeof(status_string) - status_strlen,
193 "Worker Threads = %d, ",
194 rte_lcore_count() > 2 ? 2 : 1);
195 status_strlen += snprintf(status_string + status_strlen,
196 sizeof(status_string) - status_strlen,
197 "Copy Mode = %s,\n", copy_mode == COPY_MODE_SW_NUM ?
198 COPY_MODE_SW : COPY_MODE_IOAT);
199 status_strlen += snprintf(status_string + status_strlen,
200 sizeof(status_string) - status_strlen,
201 "Updating MAC = %s, ", mac_updating ?
202 "enabled" : "disabled");
203 status_strlen += snprintf(status_string + status_strlen,
204 sizeof(status_string) - status_strlen,
205 "Rx Queues = %d, ", nb_queues);
206 status_strlen += snprintf(status_string + status_strlen,
207 sizeof(status_string) - status_strlen,
208 "Ring Size = %d", ring_size);
210 /* Allocate memory for xstats names and values */
211 ret = rte_rawdev_xstats_names_get(
212 cfg.ports[0].ioat_ids[0], NULL, 0);
215 nb_xstats = (unsigned int)ret;
217 names_xstats = malloc(sizeof(*names_xstats) * nb_xstats);
218 if (names_xstats == NULL) {
219 rte_exit(EXIT_FAILURE,
220 "Error allocating xstat names memory\n");
222 rte_rawdev_xstats_names_get(cfg.ports[0].ioat_ids[0],
223 names_xstats, nb_xstats);
225 ids_xstats = malloc(sizeof(*ids_xstats) * 2);
226 if (ids_xstats == NULL) {
227 rte_exit(EXIT_FAILURE,
228 "Error allocating xstat ids_xstats memory\n");
231 xstats = malloc(sizeof(*xstats) * 2);
232 if (xstats == NULL) {
233 rte_exit(EXIT_FAILURE,
234 "Error allocating xstat memory\n");
237 /* Get failed/successful enqueues stats index */
238 ids_xstats[0] = ids_xstats[1] = nb_xstats;
239 for (i = 0; i < nb_xstats; i++) {
240 if (!strcmp(names_xstats[i].name, "failed_enqueues"))
242 else if (!strcmp(names_xstats[i].name, "successful_enqueues"))
244 if (ids_xstats[0] < nb_xstats && ids_xstats[1] < nb_xstats)
247 if (ids_xstats[0] == nb_xstats || ids_xstats[1] == nb_xstats) {
248 rte_exit(EXIT_FAILURE,
249 "Error getting failed/successful enqueues stats index\n");
252 memset(&ts, 0, sizeof(struct total_statistics));
254 while (!force_quit) {
255 /* Sleep for "stats_interval" seconds each round - init sleep allows reading
256 * messages from app startup.
258 sleep(stats_interval);
260 /* Clear screen and move to top left */
261 printf("%s%s", clr, topLeft);
263 memset(&delta_ts, 0, sizeof(struct total_statistics));
265 printf("%s\n", status_string);
267 for (i = 0; i < cfg.nb_ports; i++) {
268 port_id = cfg.ports[i].rxtx_port;
269 print_port_stats(port_id);
271 delta_ts.total_packets_dropped +=
272 port_statistics.tx_dropped[port_id]
273 + port_statistics.copy_dropped[port_id];
274 delta_ts.total_packets_tx +=
275 port_statistics.tx[port_id];
276 delta_ts.total_packets_rx +=
277 port_statistics.rx[port_id];
279 if (copy_mode == COPY_MODE_IOAT_NUM) {
282 for (j = 0; j < cfg.ports[i].nb_queues; j++) {
283 dev_id = cfg.ports[i].ioat_ids[j];
284 rte_rawdev_xstats_get(dev_id,
285 ids_xstats, xstats, 2);
287 print_rawdev_stats(dev_id, xstats,
288 ids_xstats, 2, names_xstats);
290 delta_ts.total_failed_enqueues +=
291 xstats[ids_xstats[0]];
292 delta_ts.total_successful_enqueues +=
293 xstats[ids_xstats[1]];
298 delta_ts.total_packets_tx -= ts.total_packets_tx;
299 delta_ts.total_packets_rx -= ts.total_packets_rx;
300 delta_ts.total_packets_dropped -= ts.total_packets_dropped;
301 delta_ts.total_failed_enqueues -= ts.total_failed_enqueues;
302 delta_ts.total_successful_enqueues -=
303 ts.total_successful_enqueues;
306 print_total_stats(&delta_ts);
310 ts.total_packets_tx += delta_ts.total_packets_tx;
311 ts.total_packets_rx += delta_ts.total_packets_rx;
312 ts.total_packets_dropped += delta_ts.total_packets_dropped;
313 ts.total_failed_enqueues += delta_ts.total_failed_enqueues;
314 ts.total_successful_enqueues +=
315 delta_ts.total_successful_enqueues;
324 update_mac_addrs(struct rte_mbuf *m, uint32_t dest_portid)
326 struct rte_ether_hdr *eth;
329 eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
331 /* 02:00:00:00:00:xx - overwriting 2 bytes of source address but
332 * it's acceptable cause it gets overwritten by rte_ether_addr_copy
334 tmp = ð->dst_addr.addr_bytes[0];
335 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
338 rte_ether_addr_copy(&ioat_ports_eth_addr[dest_portid], ð->src_addr);
341 /* Perform packet copy there is a user-defined function. 8< */
343 pktmbuf_metadata_copy(const struct rte_mbuf *src, struct rte_mbuf *dst)
345 dst->data_off = src->data_off;
346 memcpy(&dst->rx_descriptor_fields1, &src->rx_descriptor_fields1,
347 offsetof(struct rte_mbuf, buf_len) -
348 offsetof(struct rte_mbuf, rx_descriptor_fields1));
351 /* Copy packet data */
353 pktmbuf_sw_copy(struct rte_mbuf *src, struct rte_mbuf *dst)
355 rte_memcpy(rte_pktmbuf_mtod(dst, char *),
356 rte_pktmbuf_mtod(src, char *), src->data_len);
358 /* >8 End of perform packet copy there is a user-defined function. */
361 ioat_enqueue_packets(struct rte_mbuf *pkts[], struct rte_mbuf *pkts_copy[],
362 uint32_t nb_rx, uint16_t dev_id)
367 for (i = 0; i < nb_rx; i++) {
368 /* Perform data copy */
369 ret = rte_ioat_enqueue_copy(dev_id,
370 rte_pktmbuf_iova(pkts[i]),
371 rte_pktmbuf_iova(pkts_copy[i]),
372 rte_pktmbuf_data_len(pkts[i]),
374 (uintptr_t)pkts_copy[i]);
384 static inline uint32_t
385 ioat_enqueue(struct rte_mbuf *pkts[], struct rte_mbuf *pkts_copy[],
386 uint32_t num, uint32_t step, uint16_t dev_id)
391 for (i = 0; i < num; i += m) {
393 m = RTE_MIN(step, num - i);
394 n = ioat_enqueue_packets(pkts + i, pkts_copy + i, m, dev_id);
397 rte_ioat_perform_ops(dev_id);
399 /* don't try to enqueue more if HW queue is full */
407 static inline uint32_t
408 ioat_dequeue(struct rte_mbuf *src[], struct rte_mbuf *dst[], uint32_t num,
412 /* Dequeue the mbufs from IOAT device. Since all memory
413 * is DPDK pinned memory and therefore all addresses should
414 * be valid, we don't check for copy errors
416 rc = rte_ioat_completed_ops(dev_id, num, NULL, NULL,
417 (void *)src, (void *)dst);
420 "rte_ioat_completed_ops(%hu) failedi, error: %d\n",
427 /* Receive packets on one port and enqueue to IOAT rawdev or rte_ring. 8< */
429 ioat_rx_port(struct rxtx_port_config *rx_config)
432 uint32_t nb_rx, nb_enq, i, j;
433 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
434 struct rte_mbuf *pkts_burst_copy[MAX_PKT_BURST];
436 for (i = 0; i < rx_config->nb_queues; i++) {
438 nb_rx = rte_eth_rx_burst(rx_config->rxtx_port, i,
439 pkts_burst, MAX_PKT_BURST);
444 port_statistics.rx[rx_config->rxtx_port] += nb_rx;
446 ret = rte_mempool_get_bulk(ioat_pktmbuf_pool,
447 (void *)pkts_burst_copy, nb_rx);
449 if (unlikely(ret < 0))
450 rte_exit(EXIT_FAILURE,
451 "Unable to allocate memory.\n");
453 for (j = 0; j < nb_rx; j++)
454 pktmbuf_metadata_copy(pkts_burst[j],
457 if (copy_mode == COPY_MODE_IOAT_NUM) {
459 /* enqueue packets for hardware copy */
460 nb_enq = ioat_enqueue(pkts_burst, pkts_burst_copy,
461 nb_rx, ioat_batch_sz, rx_config->ioat_ids[i]);
463 /* free any not enqueued packets. */
464 rte_mempool_put_bulk(ioat_pktmbuf_pool,
465 (void *)&pkts_burst[nb_enq],
467 rte_mempool_put_bulk(ioat_pktmbuf_pool,
468 (void *)&pkts_burst_copy[nb_enq],
471 port_statistics.copy_dropped[rx_config->rxtx_port] +=
474 /* get completed copies */
475 nb_rx = ioat_dequeue(pkts_burst, pkts_burst_copy,
476 MAX_PKT_BURST, rx_config->ioat_ids[i]);
478 /* Perform packet software copy, free source packets */
479 for (j = 0; j < nb_rx; j++)
480 pktmbuf_sw_copy(pkts_burst[j],
484 rte_mempool_put_bulk(ioat_pktmbuf_pool,
485 (void *)pkts_burst, nb_rx);
487 nb_enq = rte_ring_enqueue_burst(rx_config->rx_to_tx_ring,
488 (void *)pkts_burst_copy, nb_rx, NULL);
490 /* Free any not enqueued packets. */
491 rte_mempool_put_bulk(ioat_pktmbuf_pool,
492 (void *)&pkts_burst_copy[nb_enq],
495 port_statistics.copy_dropped[rx_config->rxtx_port] +=
499 /* >8 End of receive packets on one port and enqueue to IOAT rawdev or rte_ring. */
501 /* Transmit packets from IOAT rawdev/rte_ring for one port. 8< */
503 ioat_tx_port(struct rxtx_port_config *tx_config)
505 uint32_t i, j, nb_dq, nb_tx;
506 struct rte_mbuf *mbufs[MAX_PKT_BURST];
508 for (i = 0; i < tx_config->nb_queues; i++) {
510 /* Dequeue the mbufs from rx_to_tx_ring. */
511 nb_dq = rte_ring_dequeue_burst(tx_config->rx_to_tx_ring,
512 (void *)mbufs, MAX_PKT_BURST, NULL);
516 /* Update macs if enabled */
518 for (j = 0; j < nb_dq; j++)
519 update_mac_addrs(mbufs[j],
520 tx_config->rxtx_port);
523 nb_tx = rte_eth_tx_burst(tx_config->rxtx_port, 0,
524 (void *)mbufs, nb_dq);
526 port_statistics.tx[tx_config->rxtx_port] += nb_tx;
528 /* Free any unsent packets. */
529 if (unlikely(nb_tx < nb_dq))
530 rte_mempool_put_bulk(ioat_pktmbuf_pool,
531 (void *)&mbufs[nb_tx], nb_dq - nb_tx);
534 /* >8 End of transmitting packets from IOAT. */
536 /* Main rx processing loop for IOAT rawdev. */
541 uint16_t nb_ports = cfg.nb_ports;
543 RTE_LOG(INFO, IOAT, "Entering main rx loop for copy on lcore %u\n",
547 for (i = 0; i < nb_ports; i++)
548 ioat_rx_port(&cfg.ports[i]);
551 /* Main tx processing loop for hardware copy. */
556 uint16_t nb_ports = cfg.nb_ports;
558 RTE_LOG(INFO, IOAT, "Entering main tx loop for copy on lcore %u\n",
562 for (i = 0; i < nb_ports; i++)
563 ioat_tx_port(&cfg.ports[i]);
566 /* Main rx and tx loop if only one worker lcore available */
571 uint16_t nb_ports = cfg.nb_ports;
573 RTE_LOG(INFO, IOAT, "Entering main rx and tx loop for copy on"
574 " lcore %u\n", rte_lcore_id());
577 for (i = 0; i < nb_ports; i++) {
578 ioat_rx_port(&cfg.ports[i]);
579 ioat_tx_port(&cfg.ports[i]);
583 /* Start processing for each lcore. 8< */
584 static void start_forwarding_cores(void)
586 uint32_t lcore_id = rte_lcore_id();
588 RTE_LOG(INFO, IOAT, "Entering %s on lcore %u\n",
589 __func__, rte_lcore_id());
591 if (cfg.nb_lcores == 1) {
592 lcore_id = rte_get_next_lcore(lcore_id, true, true);
593 rte_eal_remote_launch((lcore_function_t *)rxtx_main_loop,
595 } else if (cfg.nb_lcores > 1) {
596 lcore_id = rte_get_next_lcore(lcore_id, true, true);
597 rte_eal_remote_launch((lcore_function_t *)rx_main_loop,
600 lcore_id = rte_get_next_lcore(lcore_id, true, true);
601 rte_eal_remote_launch((lcore_function_t *)tx_main_loop, NULL,
605 /* >8 End of starting to processfor each lcore. */
609 ioat_usage(const char *prgname)
611 printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
612 " -b --dma-batch-size: number of requests per DMA batch\n"
613 " -f --max-frame-size: max frame size\n"
614 " -p --portmask: hexadecimal bitmask of ports to configure\n"
615 " -q NQ: number of RX queues per port (default is 1)\n"
616 " --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
618 " - The source MAC address is replaced by the TX port MAC address\n"
619 " - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n"
620 " -c --copy-type CT: type of copy: sw|hw\n"
621 " -s --ring-size RS: size of IOAT rawdev ring for hardware copy mode or rte_ring for software copy mode\n"
622 " -i --stats-interval SI: interval, in seconds, between stats prints (default is 1)\n",
627 ioat_parse_portmask(const char *portmask)
632 /* Parse hexadecimal string */
633 pm = strtoul(portmask, &end, 16);
634 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
641 ioat_parse_copy_mode(const char *copy_mode)
643 if (strcmp(copy_mode, COPY_MODE_SW) == 0)
644 return COPY_MODE_SW_NUM;
645 else if (strcmp(copy_mode, COPY_MODE_IOAT) == 0)
646 return COPY_MODE_IOAT_NUM;
648 return COPY_MODE_INVALID_NUM;
651 /* Parse the argument given in the command line of the application */
653 ioat_parse_args(int argc, char **argv, unsigned int nb_ports)
655 static const char short_options[] =
656 "b:" /* dma batch size */
657 "c:" /* copy type (sw|hw) */
658 "f:" /* max frame size */
660 "q:" /* number of RX queues per port */
662 "i:" /* interval, in seconds, between stats prints */
665 static const struct option lgopts[] = {
666 {CMD_LINE_OPT_MAC_UPDATING, no_argument, &mac_updating, 1},
667 {CMD_LINE_OPT_NO_MAC_UPDATING, no_argument, &mac_updating, 0},
668 {CMD_LINE_OPT_PORTMASK, required_argument, NULL, 'p'},
669 {CMD_LINE_OPT_NB_QUEUE, required_argument, NULL, 'q'},
670 {CMD_LINE_OPT_COPY_TYPE, required_argument, NULL, 'c'},
671 {CMD_LINE_OPT_RING_SIZE, required_argument, NULL, 's'},
672 {CMD_LINE_OPT_BATCH_SIZE, required_argument, NULL, 'b'},
673 {CMD_LINE_OPT_FRAME_SIZE, required_argument, NULL, 'f'},
674 {CMD_LINE_OPT_STATS_INTERVAL, required_argument, NULL, 'i'},
678 const unsigned int default_port_mask = (1 << nb_ports) - 1;
682 char *prgname = argv[0];
684 ioat_enabled_port_mask = default_port_mask;
687 while ((opt = getopt_long(argc, argvopt, short_options,
688 lgopts, &option_index)) != EOF) {
692 ioat_batch_sz = atoi(optarg);
693 if (ioat_batch_sz > MAX_PKT_BURST) {
694 printf("Invalid dma batch size, %s.\n", optarg);
700 max_frame_size = atoi(optarg);
701 if (max_frame_size > RTE_ETHER_MAX_JUMBO_FRAME_LEN) {
702 printf("Invalid max frame size, %s.\n", optarg);
710 ioat_enabled_port_mask = ioat_parse_portmask(optarg);
711 if (ioat_enabled_port_mask & ~default_port_mask ||
712 ioat_enabled_port_mask <= 0) {
713 printf("Invalid portmask, %s, suggest 0x%x\n",
714 optarg, default_port_mask);
721 nb_queues = atoi(optarg);
722 if (nb_queues == 0 || nb_queues > MAX_RX_QUEUES_COUNT) {
723 printf("Invalid RX queues number %s. Max %u\n",
724 optarg, MAX_RX_QUEUES_COUNT);
731 copy_mode = ioat_parse_copy_mode(optarg);
732 if (copy_mode == COPY_MODE_INVALID_NUM) {
733 printf("Invalid copy type. Use: sw, hw\n");
740 ring_size = atoi(optarg);
741 if (ring_size == 0) {
742 printf("Invalid ring size, %s.\n", optarg);
749 stats_interval = atoi(optarg);
750 if (stats_interval == 0) {
751 printf("Invalid stats interval, setting to 1\n");
752 stats_interval = 1; /* set to default */
766 printf("MAC updating %s\n", mac_updating ? "enabled" : "disabled");
768 argv[optind - 1] = prgname;
771 optind = 1; /* reset getopt lib */
775 /* check link status, return true if at least one port is up */
777 check_link_status(uint32_t port_mask)
780 struct rte_eth_link link;
781 int ret, link_status = 0;
782 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
784 printf("\nChecking link status\n");
785 RTE_ETH_FOREACH_DEV(portid) {
786 if ((port_mask & (1 << portid)) == 0)
789 memset(&link, 0, sizeof(link));
790 ret = rte_eth_link_get(portid, &link);
792 printf("Port %u link get failed: err=%d\n",
797 /* Print link status */
798 rte_eth_link_to_str(link_status_text,
799 sizeof(link_status_text), &link);
800 printf("Port %d %s\n", portid, link_status_text);
802 if (link.link_status)
808 /* Configuration of device. 8< */
810 configure_rawdev_queue(uint32_t dev_id)
812 struct rte_ioat_rawdev_config dev_config = {
813 .ring_size = ring_size,
814 .no_prefetch_completions = (cfg.nb_lcores > 1),
816 struct rte_rawdev_info info = { .dev_private = &dev_config };
818 if (rte_rawdev_configure(dev_id, &info, sizeof(dev_config)) != 0) {
819 rte_exit(EXIT_FAILURE,
820 "Error with rte_rawdev_configure()\n");
822 if (rte_rawdev_start(dev_id) != 0) {
823 rte_exit(EXIT_FAILURE,
824 "Error with rte_rawdev_start()\n");
827 /* >8 End of configuration of device. */
829 /* Using IOAT rawdev API functions. 8< */
833 uint16_t nb_rawdev = 0, rdev_id = 0;
836 for (i = 0; i < cfg.nb_ports; i++) {
837 for (j = 0; j < cfg.ports[i].nb_queues; j++) {
838 struct rte_rawdev_info rdev_info = { 0 };
841 if (rdev_id == rte_rawdev_count())
843 rte_rawdev_info_get(rdev_id++, &rdev_info, 0);
844 } while (rdev_info.driver_name == NULL ||
845 strcmp(rdev_info.driver_name,
846 IOAT_PMD_RAWDEV_NAME_STR) != 0);
848 cfg.ports[i].ioat_ids[j] = rdev_id - 1;
849 configure_rawdev_queue(cfg.ports[i].ioat_ids[j]);
854 if (nb_rawdev < cfg.nb_ports * cfg.ports[0].nb_queues)
855 rte_exit(EXIT_FAILURE,
856 "Not enough IOAT rawdevs (%u) for all queues (%u).\n",
857 nb_rawdev, cfg.nb_ports * cfg.ports[0].nb_queues);
858 RTE_LOG(INFO, IOAT, "Number of used rawdevs: %u.\n", nb_rawdev);
860 /* >8 End of using IOAT rawdev API functions. */
862 /* Assign ring structures for packet exchanging. 8< */
868 for (i = 0; i < cfg.nb_ports; i++) {
869 char ring_name[RTE_RING_NAMESIZE];
871 snprintf(ring_name, sizeof(ring_name), "rx_to_tx_ring_%u", i);
872 /* Create ring for inter core communication */
873 cfg.ports[i].rx_to_tx_ring = rte_ring_create(
874 ring_name, ring_size,
875 rte_socket_id(), RING_F_SP_ENQ | RING_F_SC_DEQ);
877 if (cfg.ports[i].rx_to_tx_ring == NULL)
878 rte_exit(EXIT_FAILURE, "Ring create failed: %s\n",
879 rte_strerror(rte_errno));
882 /* >8 End of assigning ring structures for packet exchanging. */
885 * Initializes a given port using global settings and with the RX buffers
886 * coming from the mbuf_pool passed as a parameter.
889 port_init(uint16_t portid, struct rte_mempool *mbuf_pool, uint16_t nb_queues)
891 /* Configuring port to use RSS for multiple RX queues. 8< */
892 static const struct rte_eth_conf port_conf = {
894 .mq_mode = RTE_ETH_MQ_RX_RSS,
899 .rss_hf = RTE_ETH_RSS_PROTO_MASK,
903 /* >8 End of configuring port to use RSS for multiple RX queues. */
905 struct rte_eth_rxconf rxq_conf;
906 struct rte_eth_txconf txq_conf;
907 struct rte_eth_conf local_port_conf = port_conf;
908 struct rte_eth_dev_info dev_info;
911 if (max_frame_size > local_port_conf.rxmode.mtu)
912 local_port_conf.rxmode.mtu = max_frame_size;
914 /* Skip ports that are not enabled */
915 if ((ioat_enabled_port_mask & (1 << portid)) == 0) {
916 printf("Skipping disabled port %u\n", portid);
921 printf("Initializing port %u... ", portid);
923 ret = rte_eth_dev_info_get(portid, &dev_info);
925 rte_exit(EXIT_FAILURE, "Cannot get device info: %s, port=%u\n",
926 rte_strerror(-ret), portid);
928 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
929 dev_info.flow_type_rss_offloads;
930 ret = rte_eth_dev_configure(portid, nb_queues, 1, &local_port_conf);
932 rte_exit(EXIT_FAILURE, "Cannot configure device:"
933 " err=%d, port=%u\n", ret, portid);
935 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
938 rte_exit(EXIT_FAILURE,
939 "Cannot adjust number of descriptors: err=%d, port=%u\n",
942 rte_eth_macaddr_get(portid, &ioat_ports_eth_addr[portid]);
945 rxq_conf = dev_info.default_rxconf;
946 rxq_conf.offloads = local_port_conf.rxmode.offloads;
947 for (i = 0; i < nb_queues; i++) {
948 ret = rte_eth_rx_queue_setup(portid, i, nb_rxd,
949 rte_eth_dev_socket_id(portid), &rxq_conf,
952 rte_exit(EXIT_FAILURE,
953 "rte_eth_rx_queue_setup:err=%d,port=%u, queue_id=%u\n",
957 /* Init one TX queue on each port */
958 txq_conf = dev_info.default_txconf;
959 txq_conf.offloads = local_port_conf.txmode.offloads;
960 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
961 rte_eth_dev_socket_id(portid),
964 rte_exit(EXIT_FAILURE,
965 "rte_eth_tx_queue_setup:err=%d,port=%u\n",
968 /* Initialize TX buffers */
969 tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
970 RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
971 rte_eth_dev_socket_id(portid));
972 if (tx_buffer[portid] == NULL)
973 rte_exit(EXIT_FAILURE,
974 "Cannot allocate buffer for tx on port %u\n",
977 rte_eth_tx_buffer_init(tx_buffer[portid], MAX_PKT_BURST);
979 ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[portid],
980 rte_eth_tx_buffer_count_callback,
981 &port_statistics.tx_dropped[portid]);
983 rte_exit(EXIT_FAILURE,
984 "Cannot set error callback for tx buffer on port %u\n",
987 /* Start device. 8< */
988 ret = rte_eth_dev_start(portid);
990 rte_exit(EXIT_FAILURE,
991 "rte_eth_dev_start:err=%d, port=%u\n",
993 /* >8 End of starting device. */
995 /* RX port is set in promiscuous mode. 8< */
996 rte_eth_promiscuous_enable(portid);
997 /* >8 End of RX port is set in promiscuous mode. */
999 printf("Port %u, MAC address: " RTE_ETHER_ADDR_PRT_FMT "\n\n",
1001 RTE_ETHER_ADDR_BYTES(&ioat_ports_eth_addr[portid]));
1003 cfg.ports[cfg.nb_ports].rxtx_port = portid;
1004 cfg.ports[cfg.nb_ports++].nb_queues = nb_queues;
1008 signal_handler(int signum)
1010 if (signum == SIGINT || signum == SIGTERM) {
1011 printf("\n\nSignal %d received, preparing to exit...\n",
1018 main(int argc, char **argv)
1021 uint16_t nb_ports, portid;
1023 unsigned int nb_mbufs;
1027 ret = rte_eal_init(argc, argv);
1029 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
1030 /* >8 End of init EAL. */
1035 signal(SIGINT, signal_handler);
1036 signal(SIGTERM, signal_handler);
1038 nb_ports = rte_eth_dev_count_avail();
1040 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
1042 /* Parse application arguments (after the EAL ones) */
1043 ret = ioat_parse_args(argc, argv, nb_ports);
1045 rte_exit(EXIT_FAILURE, "Invalid IOAT arguments\n");
1047 /* Allocates mempool to hold the mbufs. 8< */
1048 nb_mbufs = RTE_MAX(nb_ports * (nb_queues * (nb_rxd + nb_txd +
1049 4 * MAX_PKT_BURST + ring_size) + ring_size +
1050 rte_lcore_count() * MEMPOOL_CACHE_SIZE),
1053 /* Create the mbuf pool */
1054 sz = max_frame_size + RTE_PKTMBUF_HEADROOM;
1055 sz = RTE_MAX(sz, (size_t)RTE_MBUF_DEFAULT_BUF_SIZE);
1056 ioat_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", nb_mbufs,
1057 MEMPOOL_CACHE_SIZE, 0, sz, rte_socket_id());
1058 if (ioat_pktmbuf_pool == NULL)
1059 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
1060 /* >8 End of allocates mempool to hold the mbufs. */
1062 /* Initialize each port. 8< */
1064 RTE_ETH_FOREACH_DEV(portid)
1065 port_init(portid, ioat_pktmbuf_pool, nb_queues);
1066 /* >8 End of initializing each port. */
1068 /* Initialize port xstats */
1069 memset(&port_statistics, 0, sizeof(port_statistics));
1071 /* Assigning each port resources. 8< */
1072 while (!check_link_status(ioat_enabled_port_mask) && !force_quit)
1075 /* Check if there is enough lcores for all ports. */
1076 cfg.nb_lcores = rte_lcore_count() - 1;
1077 if (cfg.nb_lcores < 1)
1078 rte_exit(EXIT_FAILURE,
1079 "There should be at least one worker lcore.\n");
1081 if (copy_mode == COPY_MODE_IOAT_NUM)
1085 /* >8 End of assigning each port resources. */
1087 start_forwarding_cores();
1088 /* main core prints stats while other cores forward */
1089 print_stats(argv[0]);
1091 /* force_quit is true when we get here */
1092 rte_eal_mp_wait_lcore();
1095 for (i = 0; i < cfg.nb_ports; i++) {
1096 printf("Closing port %d\n", cfg.ports[i].rxtx_port);
1097 ret = rte_eth_dev_stop(cfg.ports[i].rxtx_port);
1099 RTE_LOG(ERR, IOAT, "rte_eth_dev_stop: err=%s, port=%u\n",
1100 rte_strerror(-ret), cfg.ports[i].rxtx_port);
1102 rte_eth_dev_close(cfg.ports[i].rxtx_port);
1103 if (copy_mode == COPY_MODE_IOAT_NUM) {
1104 for (j = 0; j < cfg.ports[i].nb_queues; j++) {
1105 printf("Stopping rawdev %d\n",
1106 cfg.ports[i].ioat_ids[j]);
1107 rte_rawdev_stop(cfg.ports[i].ioat_ids[j]);
1109 } else /* copy_mode == COPY_MODE_SW_NUM */
1110 rte_ring_free(cfg.ports[i].rx_to_tx_ring);
1113 /* clean up the EAL */