examples/ioat: add new sample app for ioat driver
[dpdk.git] / examples / ioat / ioatfwd.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <getopt.h>
7 #include <signal.h>
8 #include <stdbool.h>
9 #include <unistd.h>
10
11 #include <rte_malloc.h>
12 #include <rte_ethdev.h>
13 #include <rte_rawdev.h>
14 #include <rte_ioat_rawdev.h>
15
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
28 /* configurable number of RX/TX ring descriptors */
29 #define RX_DEFAULT_RINGSIZE 1024
30 #define TX_DEFAULT_RINGSIZE 1024
31
32 /* max number of RX queues per port */
33 #define MAX_RX_QUEUES_COUNT 8
34
35 struct rxtx_port_config {
36         /* common config */
37         uint16_t rxtx_port;
38         uint16_t nb_queues;
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];
43 };
44
45 struct rxtx_transmission_config {
46         struct rxtx_port_config ports[RTE_MAX_ETHPORTS];
47         uint16_t nb_ports;
48         uint16_t nb_lcores;
49 };
50
51 typedef enum copy_mode_t {
52 #define COPY_MODE_SW "sw"
53         COPY_MODE_SW_NUM,
54 #define COPY_MODE_IOAT "hw"
55         COPY_MODE_IOAT_NUM,
56         COPY_MODE_INVALID_NUM,
57         COPY_MODE_SIZE_NUM = COPY_MODE_INVALID_NUM
58 } copy_mode_t;
59
60 /* mask of enabled ports */
61 static uint32_t ioat_enabled_port_mask;
62
63 /* number of RX queues per port */
64 static uint16_t nb_queues = 1;
65
66 /* MAC updating enabled by default. */
67 static int mac_updating = 1;
68
69 /* hardare copy mode enabled by default. */
70 static copy_mode_t copy_mode = COPY_MODE_IOAT_NUM;
71
72 /* size of IOAT rawdev ring for hardware copy mode or
73  * rte_ring for software copy mode
74  */
75 static unsigned short ring_size = 2048;
76
77 /* global transmission config */
78 struct rxtx_transmission_config cfg;
79
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;
83
84 static volatile bool force_quit;
85
86 /* ethernet addresses of ports */
87 static struct rte_ether_addr ioat_ports_eth_addr[RTE_MAX_ETHPORTS];
88
89 static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
90 struct rte_mempool *ioat_pktmbuf_pool;
91
92 /* Display usage */
93 static void
94 ioat_usage(const char *prgname)
95 {
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"
100                 "      When enabled:\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",
105                         prgname);
106 }
107
108 static int
109 ioat_parse_portmask(const char *portmask)
110 {
111         char *end = NULL;
112         unsigned long pm;
113
114         /* Parse hexadecimal string */
115         pm = strtoul(portmask, &end, 16);
116         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
117                 return -1;
118
119         return pm;
120 }
121
122 static copy_mode_t
123 ioat_parse_copy_mode(const char *copy_mode)
124 {
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;
129
130         return COPY_MODE_INVALID_NUM;
131 }
132
133 /* Parse the argument given in the command line of the application */
134 static int
135 ioat_parse_args(int argc, char **argv, unsigned int nb_ports)
136 {
137         static const char short_options[] =
138                 "p:"  /* portmask */
139                 "q:"  /* number of RX queues per port */
140                 "c:"  /* copy type (sw|hw) */
141                 "s:"  /* ring size */
142                 ;
143
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'},
151                 {NULL, 0, 0, 0}
152         };
153
154         const unsigned int default_port_mask = (1 << nb_ports) - 1;
155         int opt, ret;
156         char **argvopt;
157         int option_index;
158         char *prgname = argv[0];
159
160         ioat_enabled_port_mask = default_port_mask;
161         argvopt = argv;
162
163         while ((opt = getopt_long(argc, argvopt, short_options,
164                         lgopts, &option_index)) != EOF) {
165
166                 switch (opt) {
167                 /* portmask */
168                 case 'p':
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);
174                                 ioat_usage(prgname);
175                                 return -1;
176                         }
177                         break;
178
179                 case 'q':
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);
184                                 ioat_usage(prgname);
185                                 return -1;
186                         }
187                         break;
188
189                 case 'c':
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");
193                                 ioat_usage(prgname);
194                                 return -1;
195                         }
196                         break;
197
198                 case 's':
199                         ring_size = atoi(optarg);
200                         if (ring_size == 0) {
201                                 printf("Invalid ring size, %s.\n", optarg);
202                                 ioat_usage(prgname);
203                                 return -1;
204                         }
205                         break;
206
207                 /* long options */
208                 case 0:
209                         break;
210
211                 default:
212                         ioat_usage(prgname);
213                         return -1;
214                 }
215         }
216
217         printf("MAC updating %s\n", mac_updating ? "enabled" : "disabled");
218         if (optind >= 0)
219                 argv[optind - 1] = prgname;
220
221         ret = optind - 1;
222         optind = 1; /* reset getopt lib */
223         return ret;
224 }
225
226 /* check link status, return true if at least one port is up */
227 static int
228 check_link_status(uint32_t port_mask)
229 {
230         uint16_t portid;
231         struct rte_eth_link link;
232         int retval = 0;
233
234         printf("\nChecking link status\n");
235         RTE_ETH_FOREACH_DEV(portid) {
236                 if ((port_mask & (1 << portid)) == 0)
237                         continue;
238
239                 memset(&link, 0, sizeof(link));
240                 rte_eth_link_get(portid, &link);
241
242                 /* Print link status */
243                 if (link.link_status) {
244                         printf(
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"));
249                         retval = 1;
250                 } else
251                         printf("Port %d Link Down\n", portid);
252         }
253         return retval;
254 }
255
256 /*
257  * Initializes a given port using global settings and with the RX buffers
258  * coming from the mbuf_pool passed as a parameter.
259  */
260 static inline void
261 port_init(uint16_t portid, struct rte_mempool *mbuf_pool, uint16_t nb_queues)
262 {
263         /* configuring port to use RSS for multiple RX queues */
264         static const struct rte_eth_conf port_conf = {
265                 .rxmode = {
266                         .mq_mode = ETH_MQ_RX_RSS,
267                         .max_rx_pkt_len = RTE_ETHER_MAX_LEN
268                 },
269                 .rx_adv_conf = {
270                         .rss_conf = {
271                                 .rss_key = NULL,
272                                 .rss_hf = ETH_RSS_PROTO_MASK,
273                         }
274                 }
275         };
276
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;
281         int ret, i;
282
283         /* Skip ports that are not enabled */
284         if ((ioat_enabled_port_mask & (1 << portid)) == 0) {
285                 printf("Skipping disabled port %u\n", portid);
286                 return;
287         }
288
289         /* Init port */
290         printf("Initializing port %u... ", portid);
291         fflush(stdout);
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);
299         if (ret < 0)
300                 rte_exit(EXIT_FAILURE, "Cannot configure device:"
301                         " err=%d, port=%u\n", ret, portid);
302
303         ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
304                         &nb_txd);
305         if (ret < 0)
306                 rte_exit(EXIT_FAILURE,
307                         "Cannot adjust number of descriptors: err=%d, port=%u\n",
308                         ret, portid);
309
310         rte_eth_macaddr_get(portid, &ioat_ports_eth_addr[portid]);
311
312         /* Init RX queues */
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,
318                         mbuf_pool);
319                 if (ret < 0)
320                         rte_exit(EXIT_FAILURE,
321                                 "rte_eth_rx_queue_setup:err=%d,port=%u, queue_id=%u\n",
322                                 ret, portid, i);
323         }
324
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),
330                         &txq_conf);
331         if (ret < 0)
332                 rte_exit(EXIT_FAILURE,
333                         "rte_eth_tx_queue_setup:err=%d,port=%u\n",
334                         ret, portid);
335
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",
343                         portid);
344
345         rte_eth_tx_buffer_init(tx_buffer[portid], MAX_PKT_BURST);
346
347         /* Start device */
348         ret = rte_eth_dev_start(portid);
349         if (ret < 0)
350                 rte_exit(EXIT_FAILURE,
351                         "rte_eth_dev_start:err=%d, port=%u\n",
352                         ret, portid);
353
354         rte_eth_promiscuous_enable(portid);
355
356         printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
357                         portid,
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]);
364
365         cfg.ports[cfg.nb_ports].rxtx_port = portid;
366         cfg.ports[cfg.nb_ports++].nb_queues = nb_queues;
367 }
368
369 static void
370 signal_handler(int signum)
371 {
372         if (signum == SIGINT || signum == SIGTERM) {
373                 printf("\n\nSignal %d received, preparing to exit...\n",
374                         signum);
375                 force_quit = true;
376         }
377 }
378
379 int
380 main(int argc, char **argv)
381 {
382         int ret;
383         uint16_t nb_ports, portid;
384         uint32_t i;
385         unsigned int nb_mbufs;
386
387         /* Init EAL */
388         ret = rte_eal_init(argc, argv);
389         if (ret < 0)
390                 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
391         argc -= ret;
392         argv += ret;
393
394         force_quit = false;
395         signal(SIGINT, signal_handler);
396         signal(SIGTERM, signal_handler);
397
398         nb_ports = rte_eth_dev_count_avail();
399         if (nb_ports == 0)
400                 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
401
402         /* Parse application arguments (after the EAL ones) */
403         ret = ioat_parse_args(argc, argv, nb_ports);
404         if (ret < 0)
405                 rte_exit(EXIT_FAILURE, "Invalid IOAT arguments\n");
406
407         nb_mbufs = RTE_MAX(nb_ports * (nb_queues * (nb_rxd + nb_txd +
408                 4 * MAX_PKT_BURST) + rte_lcore_count() * MEMPOOL_CACHE_SIZE),
409                 MIN_POOL_SIZE);
410
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,
414                 rte_socket_id());
415         if (ioat_pktmbuf_pool == NULL)
416                 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
417
418         /* Initialise each port */
419         cfg.nb_ports = 0;
420         RTE_ETH_FOREACH_DEV(portid)
421                 port_init(portid, ioat_pktmbuf_pool, nb_queues);
422
423         while (!check_link_status(ioat_enabled_port_mask) && !force_quit)
424                 sleep(1);
425
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);
435         }
436
437         printf("Bye...\n");
438         return 0;
439 }