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 struct rxtx_transmission_config {
46 struct rxtx_port_config ports[RTE_MAX_ETHPORTS];
51 typedef enum copy_mode_t {
52 #define COPY_MODE_SW "sw"
54 #define COPY_MODE_IOAT "hw"
56 COPY_MODE_INVALID_NUM,
57 COPY_MODE_SIZE_NUM = COPY_MODE_INVALID_NUM
60 /* mask of enabled ports */
61 static uint32_t ioat_enabled_port_mask;
63 /* number of RX queues per port */
64 static uint16_t nb_queues = 1;
66 /* MAC updating enabled by default. */
67 static int mac_updating = 1;
69 /* hardare copy mode enabled by default. */
70 static copy_mode_t copy_mode = COPY_MODE_IOAT_NUM;
72 /* size of IOAT rawdev ring for hardware copy mode or
73 * rte_ring for software copy mode
75 static unsigned short ring_size = 2048;
77 /* global transmission config */
78 struct rxtx_transmission_config cfg;
80 /* configurable number of RX/TX ring descriptors */
81 static uint16_t nb_rxd = RX_DEFAULT_RINGSIZE;
82 static uint16_t nb_txd = TX_DEFAULT_RINGSIZE;
84 static volatile bool force_quit;
86 /* ethernet addresses of ports */
87 static struct rte_ether_addr ioat_ports_eth_addr[RTE_MAX_ETHPORTS];
89 static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
90 struct rte_mempool *ioat_pktmbuf_pool;
94 ioat_usage(const char *prgname)
96 printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
97 " -p --portmask: hexadecimal bitmask of ports to configure\n"
98 " -q NQ: number of RX queues per port (default is 1)\n"
99 " --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
101 " - The source MAC address is replaced by the TX port MAC address\n"
102 " - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n"
103 " -c --copy-type CT: type of copy: sw|hw\n"
104 " -s --ring-size RS: size of IOAT rawdev ring for hardware copy mode or rte_ring for software copy mode\n",
109 ioat_parse_portmask(const char *portmask)
114 /* Parse hexadecimal string */
115 pm = strtoul(portmask, &end, 16);
116 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
123 ioat_parse_copy_mode(const char *copy_mode)
125 if (strcmp(copy_mode, COPY_MODE_SW) == 0)
126 return COPY_MODE_SW_NUM;
127 else if (strcmp(copy_mode, COPY_MODE_IOAT) == 0)
128 return COPY_MODE_IOAT_NUM;
130 return COPY_MODE_INVALID_NUM;
133 /* Parse the argument given in the command line of the application */
135 ioat_parse_args(int argc, char **argv, unsigned int nb_ports)
137 static const char short_options[] =
139 "q:" /* number of RX queues per port */
140 "c:" /* copy type (sw|hw) */
144 static const struct option lgopts[] = {
145 {CMD_LINE_OPT_MAC_UPDATING, no_argument, &mac_updating, 1},
146 {CMD_LINE_OPT_NO_MAC_UPDATING, no_argument, &mac_updating, 0},
147 {CMD_LINE_OPT_PORTMASK, required_argument, NULL, 'p'},
148 {CMD_LINE_OPT_NB_QUEUE, required_argument, NULL, 'q'},
149 {CMD_LINE_OPT_COPY_TYPE, required_argument, NULL, 'c'},
150 {CMD_LINE_OPT_RING_SIZE, required_argument, NULL, 's'},
154 const unsigned int default_port_mask = (1 << nb_ports) - 1;
158 char *prgname = argv[0];
160 ioat_enabled_port_mask = default_port_mask;
163 while ((opt = getopt_long(argc, argvopt, short_options,
164 lgopts, &option_index)) != EOF) {
169 ioat_enabled_port_mask = ioat_parse_portmask(optarg);
170 if (ioat_enabled_port_mask & ~default_port_mask ||
171 ioat_enabled_port_mask <= 0) {
172 printf("Invalid portmask, %s, suggest 0x%x\n",
173 optarg, default_port_mask);
180 nb_queues = atoi(optarg);
181 if (nb_queues == 0 || nb_queues > MAX_RX_QUEUES_COUNT) {
182 printf("Invalid RX queues number %s. Max %u\n",
183 optarg, MAX_RX_QUEUES_COUNT);
190 copy_mode = ioat_parse_copy_mode(optarg);
191 if (copy_mode == COPY_MODE_INVALID_NUM) {
192 printf("Invalid copy type. Use: sw, hw\n");
199 ring_size = atoi(optarg);
200 if (ring_size == 0) {
201 printf("Invalid ring size, %s.\n", optarg);
217 printf("MAC updating %s\n", mac_updating ? "enabled" : "disabled");
219 argv[optind - 1] = prgname;
222 optind = 1; /* reset getopt lib */
226 /* check link status, return true if at least one port is up */
228 check_link_status(uint32_t port_mask)
231 struct rte_eth_link link;
234 printf("\nChecking link status\n");
235 RTE_ETH_FOREACH_DEV(portid) {
236 if ((port_mask & (1 << portid)) == 0)
239 memset(&link, 0, sizeof(link));
240 rte_eth_link_get(portid, &link);
242 /* Print link status */
243 if (link.link_status) {
245 "Port %d Link Up. Speed %u Mbps - %s\n",
246 portid, link.link_speed,
247 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
248 ("full-duplex") : ("half-duplex\n"));
251 printf("Port %d Link Down\n", portid);
257 * Initializes a given port using global settings and with the RX buffers
258 * coming from the mbuf_pool passed as a parameter.
261 port_init(uint16_t portid, struct rte_mempool *mbuf_pool, uint16_t nb_queues)
263 /* configuring port to use RSS for multiple RX queues */
264 static const struct rte_eth_conf port_conf = {
266 .mq_mode = ETH_MQ_RX_RSS,
267 .max_rx_pkt_len = RTE_ETHER_MAX_LEN
272 .rss_hf = ETH_RSS_PROTO_MASK,
277 struct rte_eth_rxconf rxq_conf;
278 struct rte_eth_txconf txq_conf;
279 struct rte_eth_conf local_port_conf = port_conf;
280 struct rte_eth_dev_info dev_info;
283 /* Skip ports that are not enabled */
284 if ((ioat_enabled_port_mask & (1 << portid)) == 0) {
285 printf("Skipping disabled port %u\n", portid);
290 printf("Initializing port %u... ", portid);
292 rte_eth_dev_info_get(portid, &dev_info);
293 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
294 dev_info.flow_type_rss_offloads;
295 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
296 local_port_conf.txmode.offloads |=
297 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
298 ret = rte_eth_dev_configure(portid, nb_queues, 1, &local_port_conf);
300 rte_exit(EXIT_FAILURE, "Cannot configure device:"
301 " err=%d, port=%u\n", ret, portid);
303 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
306 rte_exit(EXIT_FAILURE,
307 "Cannot adjust number of descriptors: err=%d, port=%u\n",
310 rte_eth_macaddr_get(portid, &ioat_ports_eth_addr[portid]);
313 rxq_conf = dev_info.default_rxconf;
314 rxq_conf.offloads = local_port_conf.rxmode.offloads;
315 for (i = 0; i < nb_queues; i++) {
316 ret = rte_eth_rx_queue_setup(portid, i, nb_rxd,
317 rte_eth_dev_socket_id(portid), &rxq_conf,
320 rte_exit(EXIT_FAILURE,
321 "rte_eth_rx_queue_setup:err=%d,port=%u, queue_id=%u\n",
325 /* Init one TX queue on each port */
326 txq_conf = dev_info.default_txconf;
327 txq_conf.offloads = local_port_conf.txmode.offloads;
328 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
329 rte_eth_dev_socket_id(portid),
332 rte_exit(EXIT_FAILURE,
333 "rte_eth_tx_queue_setup:err=%d,port=%u\n",
336 /* Initialize TX buffers */
337 tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
338 RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
339 rte_eth_dev_socket_id(portid));
340 if (tx_buffer[portid] == NULL)
341 rte_exit(EXIT_FAILURE,
342 "Cannot allocate buffer for tx on port %u\n",
345 rte_eth_tx_buffer_init(tx_buffer[portid], MAX_PKT_BURST);
348 ret = rte_eth_dev_start(portid);
350 rte_exit(EXIT_FAILURE,
351 "rte_eth_dev_start:err=%d, port=%u\n",
354 rte_eth_promiscuous_enable(portid);
356 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
358 ioat_ports_eth_addr[portid].addr_bytes[0],
359 ioat_ports_eth_addr[portid].addr_bytes[1],
360 ioat_ports_eth_addr[portid].addr_bytes[2],
361 ioat_ports_eth_addr[portid].addr_bytes[3],
362 ioat_ports_eth_addr[portid].addr_bytes[4],
363 ioat_ports_eth_addr[portid].addr_bytes[5]);
365 cfg.ports[cfg.nb_ports].rxtx_port = portid;
366 cfg.ports[cfg.nb_ports++].nb_queues = nb_queues;
370 signal_handler(int signum)
372 if (signum == SIGINT || signum == SIGTERM) {
373 printf("\n\nSignal %d received, preparing to exit...\n",
380 main(int argc, char **argv)
383 uint16_t nb_ports, portid;
385 unsigned int nb_mbufs;
388 ret = rte_eal_init(argc, argv);
390 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
395 signal(SIGINT, signal_handler);
396 signal(SIGTERM, signal_handler);
398 nb_ports = rte_eth_dev_count_avail();
400 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
402 /* Parse application arguments (after the EAL ones) */
403 ret = ioat_parse_args(argc, argv, nb_ports);
405 rte_exit(EXIT_FAILURE, "Invalid IOAT arguments\n");
407 nb_mbufs = RTE_MAX(nb_ports * (nb_queues * (nb_rxd + nb_txd +
408 4 * MAX_PKT_BURST) + rte_lcore_count() * MEMPOOL_CACHE_SIZE),
411 /* Create the mbuf pool */
412 ioat_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", nb_mbufs,
413 MEMPOOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
415 if (ioat_pktmbuf_pool == NULL)
416 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
418 /* Initialise each port */
420 RTE_ETH_FOREACH_DEV(portid)
421 port_init(portid, ioat_pktmbuf_pool, nb_queues);
423 while (!check_link_status(ioat_enabled_port_mask) && !force_quit)
426 /* Check if there is enough lcores for all ports. */
427 cfg.nb_lcores = rte_lcore_count() - 1;
428 if (cfg.nb_lcores < 1)
429 rte_exit(EXIT_FAILURE,
430 "There should be at least one slave lcore.\n");
431 for (i = 0; i < cfg.nb_ports; i++) {
432 printf("Closing port %d\n", cfg.ports[i].rxtx_port);
433 rte_eth_dev_stop(cfg.ports[i].rxtx_port);
434 rte_eth_dev_close(cfg.ports[i].rxtx_port);