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"
28 /* configurable number of RX/TX ring descriptors */
29 #define RX_DEFAULT_RINGSIZE 1024
30 #define TX_DEFAULT_RINGSIZE 1024
32 /* max number of RX queues per port */
33 #define MAX_RX_QUEUES_COUNT 8
35 struct rxtx_port_config {
39 /* for software copy mode */
40 struct rte_ring *rx_to_tx_ring;
41 /* for IOAT rawdev copy mode */
42 uint16_t ioat_ids[MAX_RX_QUEUES_COUNT];
45 /* Configuring ports and number of assigned lcores in struct. 8< */
46 struct rxtx_transmission_config {
47 struct rxtx_port_config ports[RTE_MAX_ETHPORTS];
51 /* >8 End of configuration of ports and number of assigned lcores. */
53 /* per-port statistics struct */
54 struct ioat_port_statistics {
55 uint64_t rx[RTE_MAX_ETHPORTS];
56 uint64_t tx[RTE_MAX_ETHPORTS];
57 uint64_t tx_dropped[RTE_MAX_ETHPORTS];
58 uint64_t copy_dropped[RTE_MAX_ETHPORTS];
60 struct ioat_port_statistics port_statistics;
62 struct total_statistics {
63 uint64_t total_packets_dropped;
64 uint64_t total_packets_tx;
65 uint64_t total_packets_rx;
66 uint64_t total_successful_enqueues;
67 uint64_t total_failed_enqueues;
70 typedef enum copy_mode_t {
71 #define COPY_MODE_SW "sw"
73 #define COPY_MODE_IOAT "hw"
75 COPY_MODE_INVALID_NUM,
76 COPY_MODE_SIZE_NUM = COPY_MODE_INVALID_NUM
79 /* mask of enabled ports */
80 static uint32_t ioat_enabled_port_mask;
82 /* number of RX queues per port */
83 static uint16_t nb_queues = 1;
85 /* MAC updating enabled by default. */
86 static int mac_updating = 1;
88 /* hardare copy mode enabled by default. */
89 static copy_mode_t copy_mode = COPY_MODE_IOAT_NUM;
91 /* size of IOAT rawdev ring for hardware copy mode or
92 * rte_ring for software copy mode
94 static unsigned short ring_size = 2048;
96 /* global transmission config */
97 struct rxtx_transmission_config cfg;
99 /* configurable number of RX/TX ring descriptors */
100 static uint16_t nb_rxd = RX_DEFAULT_RINGSIZE;
101 static uint16_t nb_txd = TX_DEFAULT_RINGSIZE;
103 static volatile bool force_quit;
105 /* ethernet addresses of ports */
106 static struct rte_ether_addr ioat_ports_eth_addr[RTE_MAX_ETHPORTS];
108 static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
109 struct rte_mempool *ioat_pktmbuf_pool;
111 /* Print out statistics for one port. */
113 print_port_stats(uint16_t port_id)
115 printf("\nStatistics for port %u ------------------------------"
116 "\nPackets sent: %34"PRIu64
117 "\nPackets received: %30"PRIu64
118 "\nPackets dropped on tx: %25"PRIu64
119 "\nPackets dropped on copy: %23"PRIu64,
121 port_statistics.tx[port_id],
122 port_statistics.rx[port_id],
123 port_statistics.tx_dropped[port_id],
124 port_statistics.copy_dropped[port_id]);
127 /* Print out statistics for one IOAT rawdev device. */
129 print_rawdev_stats(uint32_t dev_id, uint64_t *xstats,
130 unsigned int *ids_xstats, uint16_t nb_xstats,
131 struct rte_rawdev_xstats_name *names_xstats)
135 printf("\nIOAT channel %u", dev_id);
136 for (i = 0; i < nb_xstats; i++)
137 printf("\n\t %s: %*"PRIu64,
138 names_xstats[ids_xstats[i]].name,
139 (int)(37 - strlen(names_xstats[ids_xstats[i]].name)),
144 print_total_stats(struct total_statistics *ts)
146 printf("\nAggregate statistics ==============================="
147 "\nTotal packets Tx: %24"PRIu64" [pps]"
148 "\nTotal packets Rx: %24"PRIu64" [pps]"
149 "\nTotal packets dropped: %19"PRIu64" [pps]",
150 ts->total_packets_tx,
151 ts->total_packets_rx,
152 ts->total_packets_dropped);
154 if (copy_mode == COPY_MODE_IOAT_NUM) {
155 printf("\nTotal IOAT successful enqueues: %8"PRIu64" [enq/s]"
156 "\nTotal IOAT failed enqueues: %12"PRIu64" [enq/s]",
157 ts->total_successful_enqueues,
158 ts->total_failed_enqueues);
161 printf("\n====================================================\n");
164 /* Print out statistics on packets dropped. */
166 print_stats(char *prgname)
168 struct total_statistics ts, delta_ts;
169 uint32_t i, port_id, dev_id;
170 struct rte_rawdev_xstats_name *names_xstats;
172 unsigned int *ids_xstats, nb_xstats;
173 char status_string[255]; /* to print at the top of the output */
177 const char clr[] = { 27, '[', '2', 'J', '\0' };
178 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
180 status_strlen = snprintf(status_string, sizeof(status_string),
182 status_strlen += snprintf(status_string + status_strlen,
183 sizeof(status_string) - status_strlen,
184 "Worker Threads = %d, ",
185 rte_lcore_count() > 2 ? 2 : 1);
186 status_strlen += snprintf(status_string + status_strlen,
187 sizeof(status_string) - status_strlen,
188 "Copy Mode = %s,\n", copy_mode == COPY_MODE_SW_NUM ?
189 COPY_MODE_SW : COPY_MODE_IOAT);
190 status_strlen += snprintf(status_string + status_strlen,
191 sizeof(status_string) - status_strlen,
192 "Updating MAC = %s, ", mac_updating ?
193 "enabled" : "disabled");
194 status_strlen += snprintf(status_string + status_strlen,
195 sizeof(status_string) - status_strlen,
196 "Rx Queues = %d, ", nb_queues);
197 status_strlen += snprintf(status_string + status_strlen,
198 sizeof(status_string) - status_strlen,
199 "Ring Size = %d", ring_size);
201 /* Allocate memory for xstats names and values */
202 ret = rte_rawdev_xstats_names_get(
203 cfg.ports[0].ioat_ids[0], NULL, 0);
206 nb_xstats = (unsigned int)ret;
208 names_xstats = malloc(sizeof(*names_xstats) * nb_xstats);
209 if (names_xstats == NULL) {
210 rte_exit(EXIT_FAILURE,
211 "Error allocating xstat names memory\n");
213 rte_rawdev_xstats_names_get(cfg.ports[0].ioat_ids[0],
214 names_xstats, nb_xstats);
216 ids_xstats = malloc(sizeof(*ids_xstats) * 2);
217 if (ids_xstats == NULL) {
218 rte_exit(EXIT_FAILURE,
219 "Error allocating xstat ids_xstats memory\n");
222 xstats = malloc(sizeof(*xstats) * 2);
223 if (xstats == NULL) {
224 rte_exit(EXIT_FAILURE,
225 "Error allocating xstat memory\n");
228 /* Get failed/successful enqueues stats index */
229 ids_xstats[0] = ids_xstats[1] = nb_xstats;
230 for (i = 0; i < nb_xstats; i++) {
231 if (!strcmp(names_xstats[i].name, "failed_enqueues"))
233 else if (!strcmp(names_xstats[i].name, "successful_enqueues"))
235 if (ids_xstats[0] < nb_xstats && ids_xstats[1] < nb_xstats)
238 if (ids_xstats[0] == nb_xstats || ids_xstats[1] == nb_xstats) {
239 rte_exit(EXIT_FAILURE,
240 "Error getting failed/successful enqueues stats index\n");
243 memset(&ts, 0, sizeof(struct total_statistics));
245 while (!force_quit) {
246 /* Sleep for 1 second each round - init sleep allows reading
247 * messages from app startup.
251 /* Clear screen and move to top left */
252 printf("%s%s", clr, topLeft);
254 memset(&delta_ts, 0, sizeof(struct total_statistics));
256 printf("%s\n", status_string);
258 for (i = 0; i < cfg.nb_ports; i++) {
259 port_id = cfg.ports[i].rxtx_port;
260 print_port_stats(port_id);
262 delta_ts.total_packets_dropped +=
263 port_statistics.tx_dropped[port_id]
264 + port_statistics.copy_dropped[port_id];
265 delta_ts.total_packets_tx +=
266 port_statistics.tx[port_id];
267 delta_ts.total_packets_rx +=
268 port_statistics.rx[port_id];
270 if (copy_mode == COPY_MODE_IOAT_NUM) {
273 for (j = 0; j < cfg.ports[i].nb_queues; j++) {
274 dev_id = cfg.ports[i].ioat_ids[j];
275 rte_rawdev_xstats_get(dev_id,
276 ids_xstats, xstats, 2);
278 print_rawdev_stats(dev_id, xstats,
279 ids_xstats, 2, names_xstats);
281 delta_ts.total_failed_enqueues +=
282 xstats[ids_xstats[0]];
283 delta_ts.total_successful_enqueues +=
284 xstats[ids_xstats[1]];
289 delta_ts.total_packets_tx -= ts.total_packets_tx;
290 delta_ts.total_packets_rx -= ts.total_packets_rx;
291 delta_ts.total_packets_dropped -= ts.total_packets_dropped;
292 delta_ts.total_failed_enqueues -= ts.total_failed_enqueues;
293 delta_ts.total_successful_enqueues -=
294 ts.total_successful_enqueues;
297 print_total_stats(&delta_ts);
301 ts.total_packets_tx += delta_ts.total_packets_tx;
302 ts.total_packets_rx += delta_ts.total_packets_rx;
303 ts.total_packets_dropped += delta_ts.total_packets_dropped;
304 ts.total_failed_enqueues += delta_ts.total_failed_enqueues;
305 ts.total_successful_enqueues +=
306 delta_ts.total_successful_enqueues;
315 update_mac_addrs(struct rte_mbuf *m, uint32_t dest_portid)
317 struct rte_ether_hdr *eth;
320 eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
322 /* 02:00:00:00:00:xx - overwriting 2 bytes of source address but
323 * it's acceptable cause it gets overwritten by rte_ether_addr_copy
325 tmp = ð->dst_addr.addr_bytes[0];
326 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
329 rte_ether_addr_copy(&ioat_ports_eth_addr[dest_portid], ð->src_addr);
332 /* Perform packet copy there is a user-defined function. 8< */
334 pktmbuf_metadata_copy(const struct rte_mbuf *src, struct rte_mbuf *dst)
336 dst->data_off = src->data_off;
337 memcpy(&dst->rx_descriptor_fields1, &src->rx_descriptor_fields1,
338 offsetof(struct rte_mbuf, buf_len) -
339 offsetof(struct rte_mbuf, rx_descriptor_fields1));
342 /* Copy packet data */
344 pktmbuf_sw_copy(struct rte_mbuf *src, struct rte_mbuf *dst)
346 rte_memcpy(rte_pktmbuf_mtod(dst, char *),
347 rte_pktmbuf_mtod(src, char *), src->data_len);
349 /* >8 End of perform packet copy there is a user-defined function. */
352 ioat_enqueue_packets(struct rte_mbuf *pkts[], struct rte_mbuf *pkts_copy[],
353 uint32_t nb_rx, uint16_t dev_id)
358 for (i = 0; i < nb_rx; i++) {
359 /* Perform data copy */
360 ret = rte_ioat_enqueue_copy(dev_id,
361 rte_pktmbuf_iova(pkts[i]),
362 rte_pktmbuf_iova(pkts_copy[i]),
363 rte_pktmbuf_data_len(pkts[i]),
365 (uintptr_t)pkts_copy[i]);
375 static inline uint32_t
376 ioat_enqueue(struct rte_mbuf *pkts[], struct rte_mbuf *pkts_copy[],
377 uint32_t num, uint16_t dev_id)
381 n = ioat_enqueue_packets(pkts, pkts_copy, num, dev_id);
383 rte_ioat_perform_ops(dev_id);
388 static inline uint32_t
389 ioat_dequeue(struct rte_mbuf *src[], struct rte_mbuf *dst[], uint32_t num,
393 /* Dequeue the mbufs from IOAT device. Since all memory
394 * is DPDK pinned memory and therefore all addresses should
395 * be valid, we don't check for copy errors
397 rc = rte_ioat_completed_ops(dev_id, num, NULL, NULL,
398 (void *)src, (void *)dst);
401 "rte_ioat_completed_ops(%hu) failedi, error: %d\n",
408 /* Receive packets on one port and enqueue to IOAT rawdev or rte_ring. 8< */
410 ioat_rx_port(struct rxtx_port_config *rx_config)
413 uint32_t nb_rx, nb_enq, i, j;
414 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
415 struct rte_mbuf *pkts_burst_copy[MAX_PKT_BURST];
417 for (i = 0; i < rx_config->nb_queues; i++) {
419 nb_rx = rte_eth_rx_burst(rx_config->rxtx_port, i,
420 pkts_burst, MAX_PKT_BURST);
425 port_statistics.rx[rx_config->rxtx_port] += nb_rx;
427 ret = rte_mempool_get_bulk(ioat_pktmbuf_pool,
428 (void *)pkts_burst_copy, nb_rx);
430 if (unlikely(ret < 0))
431 rte_exit(EXIT_FAILURE,
432 "Unable to allocate memory.\n");
434 for (j = 0; j < nb_rx; j++)
435 pktmbuf_metadata_copy(pkts_burst[j],
438 if (copy_mode == COPY_MODE_IOAT_NUM) {
440 /* enqueue packets for hardware copy */
441 nb_enq = ioat_enqueue(pkts_burst, pkts_burst_copy,
442 nb_rx, rx_config->ioat_ids[i]);
444 /* free any not enqueued packets. */
445 rte_mempool_put_bulk(ioat_pktmbuf_pool,
446 (void *)&pkts_burst[nb_enq],
448 rte_mempool_put_bulk(ioat_pktmbuf_pool,
449 (void *)&pkts_burst_copy[nb_enq],
452 port_statistics.copy_dropped[rx_config->rxtx_port] +=
455 /* get completed copies */
456 nb_rx = ioat_dequeue(pkts_burst, pkts_burst_copy,
457 MAX_PKT_BURST, rx_config->ioat_ids[i]);
459 /* Perform packet software copy, free source packets */
460 for (j = 0; j < nb_rx; j++)
461 pktmbuf_sw_copy(pkts_burst[j],
465 rte_mempool_put_bulk(ioat_pktmbuf_pool,
466 (void *)pkts_burst, nb_rx);
468 nb_enq = rte_ring_enqueue_burst(rx_config->rx_to_tx_ring,
469 (void *)pkts_burst_copy, nb_rx, NULL);
471 /* Free any not enqueued packets. */
472 rte_mempool_put_bulk(ioat_pktmbuf_pool,
473 (void *)&pkts_burst_copy[nb_enq],
476 port_statistics.copy_dropped[rx_config->rxtx_port] +=
480 /* >8 End of receive packets on one port and enqueue to IOAT rawdev or rte_ring. */
482 /* Transmit packets from IOAT rawdev/rte_ring for one port. 8< */
484 ioat_tx_port(struct rxtx_port_config *tx_config)
486 uint32_t i, j, nb_dq, nb_tx;
487 struct rte_mbuf *mbufs[MAX_PKT_BURST];
489 for (i = 0; i < tx_config->nb_queues; i++) {
491 /* Dequeue the mbufs from rx_to_tx_ring. */
492 nb_dq = rte_ring_dequeue_burst(tx_config->rx_to_tx_ring,
493 (void *)mbufs, MAX_PKT_BURST, NULL);
497 /* Update macs if enabled */
499 for (j = 0; j < nb_dq; j++)
500 update_mac_addrs(mbufs[j],
501 tx_config->rxtx_port);
504 nb_tx = rte_eth_tx_burst(tx_config->rxtx_port, 0,
505 (void *)mbufs, nb_dq);
507 port_statistics.tx[tx_config->rxtx_port] += nb_tx;
509 /* Free any unsent packets. */
510 if (unlikely(nb_tx < nb_dq))
511 rte_mempool_put_bulk(ioat_pktmbuf_pool,
512 (void *)&mbufs[nb_tx], nb_dq - nb_tx);
515 /* >8 End of transmitting packets from IOAT. */
517 /* Main rx processing loop for IOAT rawdev. */
522 uint16_t nb_ports = cfg.nb_ports;
524 RTE_LOG(INFO, IOAT, "Entering main rx loop for copy on lcore %u\n",
528 for (i = 0; i < nb_ports; i++)
529 ioat_rx_port(&cfg.ports[i]);
532 /* Main tx processing loop for hardware copy. */
537 uint16_t nb_ports = cfg.nb_ports;
539 RTE_LOG(INFO, IOAT, "Entering main tx loop for copy on lcore %u\n",
543 for (i = 0; i < nb_ports; i++)
544 ioat_tx_port(&cfg.ports[i]);
547 /* Main rx and tx loop if only one worker lcore available */
552 uint16_t nb_ports = cfg.nb_ports;
554 RTE_LOG(INFO, IOAT, "Entering main rx and tx loop for copy on"
555 " lcore %u\n", rte_lcore_id());
558 for (i = 0; i < nb_ports; i++) {
559 ioat_rx_port(&cfg.ports[i]);
560 ioat_tx_port(&cfg.ports[i]);
564 /* Start processing for each lcore. 8< */
565 static void start_forwarding_cores(void)
567 uint32_t lcore_id = rte_lcore_id();
569 RTE_LOG(INFO, IOAT, "Entering %s on lcore %u\n",
570 __func__, rte_lcore_id());
572 if (cfg.nb_lcores == 1) {
573 lcore_id = rte_get_next_lcore(lcore_id, true, true);
574 rte_eal_remote_launch((lcore_function_t *)rxtx_main_loop,
576 } else if (cfg.nb_lcores > 1) {
577 lcore_id = rte_get_next_lcore(lcore_id, true, true);
578 rte_eal_remote_launch((lcore_function_t *)rx_main_loop,
581 lcore_id = rte_get_next_lcore(lcore_id, true, true);
582 rte_eal_remote_launch((lcore_function_t *)tx_main_loop, NULL,
586 /* >8 End of starting to processfor each lcore. */
590 ioat_usage(const char *prgname)
592 printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
593 " -p --portmask: hexadecimal bitmask of ports to configure\n"
594 " -q NQ: number of RX queues per port (default is 1)\n"
595 " --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
597 " - The source MAC address is replaced by the TX port MAC address\n"
598 " - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n"
599 " -c --copy-type CT: type of copy: sw|hw\n"
600 " -s --ring-size RS: size of IOAT rawdev ring for hardware copy mode or rte_ring for software copy mode\n",
605 ioat_parse_portmask(const char *portmask)
610 /* Parse hexadecimal string */
611 pm = strtoul(portmask, &end, 16);
612 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
619 ioat_parse_copy_mode(const char *copy_mode)
621 if (strcmp(copy_mode, COPY_MODE_SW) == 0)
622 return COPY_MODE_SW_NUM;
623 else if (strcmp(copy_mode, COPY_MODE_IOAT) == 0)
624 return COPY_MODE_IOAT_NUM;
626 return COPY_MODE_INVALID_NUM;
629 /* Parse the argument given in the command line of the application */
631 ioat_parse_args(int argc, char **argv, unsigned int nb_ports)
633 static const char short_options[] =
635 "q:" /* number of RX queues per port */
636 "c:" /* copy type (sw|hw) */
640 static const struct option lgopts[] = {
641 {CMD_LINE_OPT_MAC_UPDATING, no_argument, &mac_updating, 1},
642 {CMD_LINE_OPT_NO_MAC_UPDATING, no_argument, &mac_updating, 0},
643 {CMD_LINE_OPT_PORTMASK, required_argument, NULL, 'p'},
644 {CMD_LINE_OPT_NB_QUEUE, required_argument, NULL, 'q'},
645 {CMD_LINE_OPT_COPY_TYPE, required_argument, NULL, 'c'},
646 {CMD_LINE_OPT_RING_SIZE, required_argument, NULL, 's'},
650 const unsigned int default_port_mask = (1 << nb_ports) - 1;
654 char *prgname = argv[0];
656 ioat_enabled_port_mask = default_port_mask;
659 while ((opt = getopt_long(argc, argvopt, short_options,
660 lgopts, &option_index)) != EOF) {
665 ioat_enabled_port_mask = ioat_parse_portmask(optarg);
666 if (ioat_enabled_port_mask & ~default_port_mask ||
667 ioat_enabled_port_mask <= 0) {
668 printf("Invalid portmask, %s, suggest 0x%x\n",
669 optarg, default_port_mask);
676 nb_queues = atoi(optarg);
677 if (nb_queues == 0 || nb_queues > MAX_RX_QUEUES_COUNT) {
678 printf("Invalid RX queues number %s. Max %u\n",
679 optarg, MAX_RX_QUEUES_COUNT);
686 copy_mode = ioat_parse_copy_mode(optarg);
687 if (copy_mode == COPY_MODE_INVALID_NUM) {
688 printf("Invalid copy type. Use: sw, hw\n");
695 ring_size = atoi(optarg);
696 if (ring_size == 0) {
697 printf("Invalid ring size, %s.\n", optarg);
713 printf("MAC updating %s\n", mac_updating ? "enabled" : "disabled");
715 argv[optind - 1] = prgname;
718 optind = 1; /* reset getopt lib */
722 /* check link status, return true if at least one port is up */
724 check_link_status(uint32_t port_mask)
727 struct rte_eth_link link;
728 int ret, link_status = 0;
729 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
731 printf("\nChecking link status\n");
732 RTE_ETH_FOREACH_DEV(portid) {
733 if ((port_mask & (1 << portid)) == 0)
736 memset(&link, 0, sizeof(link));
737 ret = rte_eth_link_get(portid, &link);
739 printf("Port %u link get failed: err=%d\n",
744 /* Print link status */
745 rte_eth_link_to_str(link_status_text,
746 sizeof(link_status_text), &link);
747 printf("Port %d %s\n", portid, link_status_text);
749 if (link.link_status)
755 /* Configuration of device. 8< */
757 configure_rawdev_queue(uint32_t dev_id)
759 struct rte_ioat_rawdev_config dev_config = {
760 .ring_size = ring_size,
761 .no_prefetch_completions = (cfg.nb_lcores > 1),
763 struct rte_rawdev_info info = { .dev_private = &dev_config };
765 if (rte_rawdev_configure(dev_id, &info, sizeof(dev_config)) != 0) {
766 rte_exit(EXIT_FAILURE,
767 "Error with rte_rawdev_configure()\n");
769 if (rte_rawdev_start(dev_id) != 0) {
770 rte_exit(EXIT_FAILURE,
771 "Error with rte_rawdev_start()\n");
774 /* >8 End of configuration of device. */
776 /* Using IOAT rawdev API functions. 8< */
780 uint16_t nb_rawdev = 0, rdev_id = 0;
783 for (i = 0; i < cfg.nb_ports; i++) {
784 for (j = 0; j < cfg.ports[i].nb_queues; j++) {
785 struct rte_rawdev_info rdev_info = { 0 };
788 if (rdev_id == rte_rawdev_count())
790 rte_rawdev_info_get(rdev_id++, &rdev_info, 0);
791 } while (rdev_info.driver_name == NULL ||
792 strcmp(rdev_info.driver_name,
793 IOAT_PMD_RAWDEV_NAME_STR) != 0);
795 cfg.ports[i].ioat_ids[j] = rdev_id - 1;
796 configure_rawdev_queue(cfg.ports[i].ioat_ids[j]);
801 if (nb_rawdev < cfg.nb_ports * cfg.ports[0].nb_queues)
802 rte_exit(EXIT_FAILURE,
803 "Not enough IOAT rawdevs (%u) for all queues (%u).\n",
804 nb_rawdev, cfg.nb_ports * cfg.ports[0].nb_queues);
805 RTE_LOG(INFO, IOAT, "Number of used rawdevs: %u.\n", nb_rawdev);
807 /* >8 End of using IOAT rawdev API functions. */
809 /* Assign ring structures for packet exchanging. 8< */
815 for (i = 0; i < cfg.nb_ports; i++) {
816 char ring_name[RTE_RING_NAMESIZE];
818 snprintf(ring_name, sizeof(ring_name), "rx_to_tx_ring_%u", i);
819 /* Create ring for inter core communication */
820 cfg.ports[i].rx_to_tx_ring = rte_ring_create(
821 ring_name, ring_size,
822 rte_socket_id(), RING_F_SP_ENQ | RING_F_SC_DEQ);
824 if (cfg.ports[i].rx_to_tx_ring == NULL)
825 rte_exit(EXIT_FAILURE, "Ring create failed: %s\n",
826 rte_strerror(rte_errno));
829 /* >8 End of assigning ring structures for packet exchanging. */
832 * Initializes a given port using global settings and with the RX buffers
833 * coming from the mbuf_pool passed as a parameter.
836 port_init(uint16_t portid, struct rte_mempool *mbuf_pool, uint16_t nb_queues)
838 /* Configuring port to use RSS for multiple RX queues. 8< */
839 static const struct rte_eth_conf port_conf = {
841 .mq_mode = RTE_ETH_MQ_RX_RSS,
846 .rss_hf = RTE_ETH_RSS_PROTO_MASK,
850 /* >8 End of configuring port to use RSS for multiple RX queues. */
852 struct rte_eth_rxconf rxq_conf;
853 struct rte_eth_txconf txq_conf;
854 struct rte_eth_conf local_port_conf = port_conf;
855 struct rte_eth_dev_info dev_info;
858 /* Skip ports that are not enabled */
859 if ((ioat_enabled_port_mask & (1 << portid)) == 0) {
860 printf("Skipping disabled port %u\n", portid);
865 printf("Initializing port %u... ", portid);
867 ret = rte_eth_dev_info_get(portid, &dev_info);
869 rte_exit(EXIT_FAILURE, "Cannot get device info: %s, port=%u\n",
870 rte_strerror(-ret), portid);
872 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
873 dev_info.flow_type_rss_offloads;
874 ret = rte_eth_dev_configure(portid, nb_queues, 1, &local_port_conf);
876 rte_exit(EXIT_FAILURE, "Cannot configure device:"
877 " err=%d, port=%u\n", ret, portid);
879 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
882 rte_exit(EXIT_FAILURE,
883 "Cannot adjust number of descriptors: err=%d, port=%u\n",
886 rte_eth_macaddr_get(portid, &ioat_ports_eth_addr[portid]);
889 rxq_conf = dev_info.default_rxconf;
890 rxq_conf.offloads = local_port_conf.rxmode.offloads;
891 for (i = 0; i < nb_queues; i++) {
892 ret = rte_eth_rx_queue_setup(portid, i, nb_rxd,
893 rte_eth_dev_socket_id(portid), &rxq_conf,
896 rte_exit(EXIT_FAILURE,
897 "rte_eth_rx_queue_setup:err=%d,port=%u, queue_id=%u\n",
901 /* Init one TX queue on each port */
902 txq_conf = dev_info.default_txconf;
903 txq_conf.offloads = local_port_conf.txmode.offloads;
904 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
905 rte_eth_dev_socket_id(portid),
908 rte_exit(EXIT_FAILURE,
909 "rte_eth_tx_queue_setup:err=%d,port=%u\n",
912 /* Initialize TX buffers */
913 tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
914 RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
915 rte_eth_dev_socket_id(portid));
916 if (tx_buffer[portid] == NULL)
917 rte_exit(EXIT_FAILURE,
918 "Cannot allocate buffer for tx on port %u\n",
921 rte_eth_tx_buffer_init(tx_buffer[portid], MAX_PKT_BURST);
923 ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[portid],
924 rte_eth_tx_buffer_count_callback,
925 &port_statistics.tx_dropped[portid]);
927 rte_exit(EXIT_FAILURE,
928 "Cannot set error callback for tx buffer on port %u\n",
931 /* Start device. 8< */
932 ret = rte_eth_dev_start(portid);
934 rte_exit(EXIT_FAILURE,
935 "rte_eth_dev_start:err=%d, port=%u\n",
937 /* >8 End of starting device. */
939 /* RX port is set in promiscuous mode. 8< */
940 rte_eth_promiscuous_enable(portid);
941 /* >8 End of RX port is set in promiscuous mode. */
943 printf("Port %u, MAC address: " RTE_ETHER_ADDR_PRT_FMT "\n\n",
945 RTE_ETHER_ADDR_BYTES(&ioat_ports_eth_addr[portid]));
947 cfg.ports[cfg.nb_ports].rxtx_port = portid;
948 cfg.ports[cfg.nb_ports++].nb_queues = nb_queues;
952 signal_handler(int signum)
954 if (signum == SIGINT || signum == SIGTERM) {
955 printf("\n\nSignal %d received, preparing to exit...\n",
962 main(int argc, char **argv)
965 uint16_t nb_ports, portid;
967 unsigned int nb_mbufs;
970 ret = rte_eal_init(argc, argv);
972 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
973 /* >8 End of init EAL. */
978 signal(SIGINT, signal_handler);
979 signal(SIGTERM, signal_handler);
981 nb_ports = rte_eth_dev_count_avail();
983 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
985 /* Parse application arguments (after the EAL ones) */
986 ret = ioat_parse_args(argc, argv, nb_ports);
988 rte_exit(EXIT_FAILURE, "Invalid IOAT arguments\n");
990 /* Allocates mempool to hold the mbufs. 8< */
991 nb_mbufs = RTE_MAX(nb_ports * (nb_queues * (nb_rxd + nb_txd +
992 4 * MAX_PKT_BURST + ring_size) + ring_size +
993 rte_lcore_count() * MEMPOOL_CACHE_SIZE),
996 /* Create the mbuf pool */
997 ioat_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", nb_mbufs,
998 MEMPOOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
1000 if (ioat_pktmbuf_pool == NULL)
1001 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
1002 /* >8 End of allocates mempool to hold the mbufs. */
1004 /* Initialize each port. 8< */
1006 RTE_ETH_FOREACH_DEV(portid)
1007 port_init(portid, ioat_pktmbuf_pool, nb_queues);
1008 /* >8 End of initializing each port. */
1010 /* Initialize port xstats */
1011 memset(&port_statistics, 0, sizeof(port_statistics));
1013 /* Assigning each port resources. 8< */
1014 while (!check_link_status(ioat_enabled_port_mask) && !force_quit)
1017 /* Check if there is enough lcores for all ports. */
1018 cfg.nb_lcores = rte_lcore_count() - 1;
1019 if (cfg.nb_lcores < 1)
1020 rte_exit(EXIT_FAILURE,
1021 "There should be at least one worker lcore.\n");
1023 if (copy_mode == COPY_MODE_IOAT_NUM)
1027 /* >8 End of assigning each port resources. */
1029 start_forwarding_cores();
1030 /* main core prints stats while other cores forward */
1031 print_stats(argv[0]);
1033 /* force_quit is true when we get here */
1034 rte_eal_mp_wait_lcore();
1037 for (i = 0; i < cfg.nb_ports; i++) {
1038 printf("Closing port %d\n", cfg.ports[i].rxtx_port);
1039 ret = rte_eth_dev_stop(cfg.ports[i].rxtx_port);
1041 RTE_LOG(ERR, IOAT, "rte_eth_dev_stop: err=%s, port=%u\n",
1042 rte_strerror(-ret), cfg.ports[i].rxtx_port);
1044 rte_eth_dev_close(cfg.ports[i].rxtx_port);
1045 if (copy_mode == COPY_MODE_IOAT_NUM) {
1046 for (j = 0; j < cfg.ports[i].nb_queues; j++) {
1047 printf("Stopping rawdev %d\n",
1048 cfg.ports[i].ioat_ids[j]);
1049 rte_rawdev_stop(cfg.ports[i].ioat_ids[j]);
1051 } else /* copy_mode == COPY_MODE_SW_NUM */
1052 rte_ring_free(cfg.ports[i].rx_to_tx_ring);
1055 /* clean up the EAL */