examples/ioat: add software copy mode
[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 static void
93 update_mac_addrs(struct rte_mbuf *m, uint32_t dest_portid)
94 {
95         struct rte_ether_hdr *eth;
96         void *tmp;
97
98         eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
99
100         /* 02:00:00:00:00:xx - overwriting 2 bytes of source address but
101          * it's acceptable cause it gets overwritten by rte_ether_addr_copy
102          */
103         tmp = &eth->d_addr.addr_bytes[0];
104         *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
105
106         /* src addr */
107         rte_ether_addr_copy(&ioat_ports_eth_addr[dest_portid], &eth->s_addr);
108 }
109
110 static inline void
111 pktmbuf_sw_copy(struct rte_mbuf *src, struct rte_mbuf *dst)
112 {
113         /* Copy packet metadata */
114         rte_memcpy(&dst->rearm_data,
115                 &src->rearm_data,
116                 offsetof(struct rte_mbuf, cacheline1)
117                 - offsetof(struct rte_mbuf, rearm_data));
118
119         /* Copy packet data */
120         rte_memcpy(rte_pktmbuf_mtod(dst, char *),
121                 rte_pktmbuf_mtod(src, char *), src->data_len);
122 }
123
124 /* Receive packets on one port and enqueue to IOAT rawdev or rte_ring. */
125 static void
126 ioat_rx_port(struct rxtx_port_config *rx_config)
127 {
128         uint32_t nb_rx, nb_enq, i, j;
129         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
130
131         for (i = 0; i < rx_config->nb_queues; i++) {
132
133                 nb_rx = rte_eth_rx_burst(rx_config->rxtx_port, i,
134                         pkts_burst, MAX_PKT_BURST);
135
136                 if (nb_rx == 0)
137                         continue;
138
139                 if (copy_mode == COPY_MODE_IOAT_NUM) {
140                         /* Perform packet hardware copy */
141                 } else {
142                         /* Perform packet software copy, free source packets */
143                         int ret;
144                         struct rte_mbuf *pkts_burst_copy[MAX_PKT_BURST];
145
146                         ret = rte_mempool_get_bulk(ioat_pktmbuf_pool,
147                                 (void *)pkts_burst_copy, nb_rx);
148
149                         if (unlikely(ret < 0))
150                                 rte_exit(EXIT_FAILURE,
151                                         "Unable to allocate memory.\n");
152
153                         for (j = 0; j < nb_rx; j++)
154                                 pktmbuf_sw_copy(pkts_burst[j],
155                                         pkts_burst_copy[j]);
156
157                         rte_mempool_put_bulk(ioat_pktmbuf_pool,
158                                 (void *)pkts_burst, nb_rx);
159
160                         nb_enq = rte_ring_enqueue_burst(
161                                 rx_config->rx_to_tx_ring,
162                                 (void *)pkts_burst_copy, nb_rx, NULL);
163
164                         /* Free any not enqueued packets. */
165                         rte_mempool_put_bulk(ioat_pktmbuf_pool,
166                                 (void *)&pkts_burst_copy[nb_enq],
167                                 nb_rx - nb_enq);
168                 }
169         }
170 }
171
172 /* Transmit packets from IOAT rawdev/rte_ring for one port. */
173 static void
174 ioat_tx_port(struct rxtx_port_config *tx_config)
175 {
176         uint32_t i, j, nb_dq = 0;
177         struct rte_mbuf *mbufs_dst[MAX_PKT_BURST];
178
179         for (i = 0; i < tx_config->nb_queues; i++) {
180                 if (copy_mode == COPY_MODE_IOAT_NUM) {
181                         /* Deque the mbufs from IOAT device. */
182                 } else {
183                         /* Deque the mbufs from rx_to_tx_ring. */
184                         nb_dq = rte_ring_dequeue_burst(
185                                 tx_config->rx_to_tx_ring, (void *)mbufs_dst,
186                                 MAX_PKT_BURST, NULL);
187                 }
188
189                 if (nb_dq == 0)
190                         return;
191
192                 /* Update macs if enabled */
193                 if (mac_updating) {
194                         for (j = 0; j < nb_dq; j++)
195                                 update_mac_addrs(mbufs_dst[j],
196                                         tx_config->rxtx_port);
197                 }
198
199                 const uint16_t nb_tx = rte_eth_tx_burst(
200                         tx_config->rxtx_port, 0,
201                         (void *)mbufs_dst, nb_dq);
202
203                 /* Free any unsent packets. */
204                 if (unlikely(nb_tx < nb_dq))
205                         rte_mempool_put_bulk(ioat_pktmbuf_pool,
206                         (void *)&mbufs_dst[nb_tx],
207                                 nb_dq - nb_tx);
208         }
209 }
210
211 /* Main rx and tx loop if only one slave lcore available */
212 static void
213 rxtx_main_loop(void)
214 {
215         uint16_t i;
216         uint16_t nb_ports = cfg.nb_ports;
217
218         RTE_LOG(INFO, IOAT, "Entering main rx and tx loop for copy on"
219                 " lcore %u\n", rte_lcore_id());
220
221         while (!force_quit)
222                 for (i = 0; i < nb_ports; i++) {
223                         ioat_rx_port(&cfg.ports[i]);
224                         ioat_tx_port(&cfg.ports[i]);
225                 }
226 }
227
228 static void start_forwarding_cores(void)
229 {
230         uint32_t lcore_id = rte_lcore_id();
231
232         RTE_LOG(INFO, IOAT, "Entering %s on lcore %u\n",
233                 __func__, rte_lcore_id());
234
235         lcore_id = rte_get_next_lcore(lcore_id, true, true);
236         rte_eal_remote_launch((lcore_function_t *)rxtx_main_loop,
237                 NULL, lcore_id);
238 }
239
240 /* Display usage */
241 static void
242 ioat_usage(const char *prgname)
243 {
244         printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
245                 "  -p --portmask: hexadecimal bitmask of ports to configure\n"
246                 "  -q NQ: number of RX queues per port (default is 1)\n"
247                 "  --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
248                 "      When enabled:\n"
249                 "       - The source MAC address is replaced by the TX port MAC address\n"
250                 "       - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n"
251                 "  -c --copy-type CT: type of copy: sw|hw\n"
252                 "  -s --ring-size RS: size of IOAT rawdev ring for hardware copy mode or rte_ring for software copy mode\n",
253                         prgname);
254 }
255
256 static int
257 ioat_parse_portmask(const char *portmask)
258 {
259         char *end = NULL;
260         unsigned long pm;
261
262         /* Parse hexadecimal string */
263         pm = strtoul(portmask, &end, 16);
264         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
265                 return -1;
266
267         return pm;
268 }
269
270 static copy_mode_t
271 ioat_parse_copy_mode(const char *copy_mode)
272 {
273         if (strcmp(copy_mode, COPY_MODE_SW) == 0)
274                 return COPY_MODE_SW_NUM;
275         else if (strcmp(copy_mode, COPY_MODE_IOAT) == 0)
276                 return COPY_MODE_IOAT_NUM;
277
278         return COPY_MODE_INVALID_NUM;
279 }
280
281 /* Parse the argument given in the command line of the application */
282 static int
283 ioat_parse_args(int argc, char **argv, unsigned int nb_ports)
284 {
285         static const char short_options[] =
286                 "p:"  /* portmask */
287                 "q:"  /* number of RX queues per port */
288                 "c:"  /* copy type (sw|hw) */
289                 "s:"  /* ring size */
290                 ;
291
292         static const struct option lgopts[] = {
293                 {CMD_LINE_OPT_MAC_UPDATING, no_argument, &mac_updating, 1},
294                 {CMD_LINE_OPT_NO_MAC_UPDATING, no_argument, &mac_updating, 0},
295                 {CMD_LINE_OPT_PORTMASK, required_argument, NULL, 'p'},
296                 {CMD_LINE_OPT_NB_QUEUE, required_argument, NULL, 'q'},
297                 {CMD_LINE_OPT_COPY_TYPE, required_argument, NULL, 'c'},
298                 {CMD_LINE_OPT_RING_SIZE, required_argument, NULL, 's'},
299                 {NULL, 0, 0, 0}
300         };
301
302         const unsigned int default_port_mask = (1 << nb_ports) - 1;
303         int opt, ret;
304         char **argvopt;
305         int option_index;
306         char *prgname = argv[0];
307
308         ioat_enabled_port_mask = default_port_mask;
309         argvopt = argv;
310
311         while ((opt = getopt_long(argc, argvopt, short_options,
312                         lgopts, &option_index)) != EOF) {
313
314                 switch (opt) {
315                 /* portmask */
316                 case 'p':
317                         ioat_enabled_port_mask = ioat_parse_portmask(optarg);
318                         if (ioat_enabled_port_mask & ~default_port_mask ||
319                                         ioat_enabled_port_mask <= 0) {
320                                 printf("Invalid portmask, %s, suggest 0x%x\n",
321                                                 optarg, default_port_mask);
322                                 ioat_usage(prgname);
323                                 return -1;
324                         }
325                         break;
326
327                 case 'q':
328                         nb_queues = atoi(optarg);
329                         if (nb_queues == 0 || nb_queues > MAX_RX_QUEUES_COUNT) {
330                                 printf("Invalid RX queues number %s. Max %u\n",
331                                         optarg, MAX_RX_QUEUES_COUNT);
332                                 ioat_usage(prgname);
333                                 return -1;
334                         }
335                         break;
336
337                 case 'c':
338                         copy_mode = ioat_parse_copy_mode(optarg);
339                         if (copy_mode == COPY_MODE_INVALID_NUM) {
340                                 printf("Invalid copy type. Use: sw, hw\n");
341                                 ioat_usage(prgname);
342                                 return -1;
343                         }
344                         break;
345
346                 case 's':
347                         ring_size = atoi(optarg);
348                         if (ring_size == 0) {
349                                 printf("Invalid ring size, %s.\n", optarg);
350                                 ioat_usage(prgname);
351                                 return -1;
352                         }
353                         break;
354
355                 /* long options */
356                 case 0:
357                         break;
358
359                 default:
360                         ioat_usage(prgname);
361                         return -1;
362                 }
363         }
364
365         printf("MAC updating %s\n", mac_updating ? "enabled" : "disabled");
366         if (optind >= 0)
367                 argv[optind - 1] = prgname;
368
369         ret = optind - 1;
370         optind = 1; /* reset getopt lib */
371         return ret;
372 }
373
374 /* check link status, return true if at least one port is up */
375 static int
376 check_link_status(uint32_t port_mask)
377 {
378         uint16_t portid;
379         struct rte_eth_link link;
380         int retval = 0;
381
382         printf("\nChecking link status\n");
383         RTE_ETH_FOREACH_DEV(portid) {
384                 if ((port_mask & (1 << portid)) == 0)
385                         continue;
386
387                 memset(&link, 0, sizeof(link));
388                 rte_eth_link_get(portid, &link);
389
390                 /* Print link status */
391                 if (link.link_status) {
392                         printf(
393                                 "Port %d Link Up. Speed %u Mbps - %s\n",
394                                 portid, link.link_speed,
395                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
396                                 ("full-duplex") : ("half-duplex\n"));
397                         retval = 1;
398                 } else
399                         printf("Port %d Link Down\n", portid);
400         }
401         return retval;
402 }
403
404 static void
405 assign_rings(void)
406 {
407         uint32_t i;
408
409         for (i = 0; i < cfg.nb_ports; i++) {
410                 char ring_name[RTE_RING_NAMESIZE];
411
412                 snprintf(ring_name, sizeof(ring_name), "rx_to_tx_ring_%u", i);
413                 /* Create ring for inter core communication */
414                 cfg.ports[i].rx_to_tx_ring = rte_ring_create(
415                         ring_name, ring_size,
416                         rte_socket_id(), RING_F_SP_ENQ | RING_F_SC_DEQ);
417
418                 if (cfg.ports[i].rx_to_tx_ring == NULL)
419                         rte_exit(EXIT_FAILURE, "Ring create failed: %s\n",
420                                 rte_strerror(rte_errno));
421         }
422 }
423
424 /*
425  * Initializes a given port using global settings and with the RX buffers
426  * coming from the mbuf_pool passed as a parameter.
427  */
428 static inline void
429 port_init(uint16_t portid, struct rte_mempool *mbuf_pool, uint16_t nb_queues)
430 {
431         /* configuring port to use RSS for multiple RX queues */
432         static const struct rte_eth_conf port_conf = {
433                 .rxmode = {
434                         .mq_mode = ETH_MQ_RX_RSS,
435                         .max_rx_pkt_len = RTE_ETHER_MAX_LEN
436                 },
437                 .rx_adv_conf = {
438                         .rss_conf = {
439                                 .rss_key = NULL,
440                                 .rss_hf = ETH_RSS_PROTO_MASK,
441                         }
442                 }
443         };
444
445         struct rte_eth_rxconf rxq_conf;
446         struct rte_eth_txconf txq_conf;
447         struct rte_eth_conf local_port_conf = port_conf;
448         struct rte_eth_dev_info dev_info;
449         int ret, i;
450
451         /* Skip ports that are not enabled */
452         if ((ioat_enabled_port_mask & (1 << portid)) == 0) {
453                 printf("Skipping disabled port %u\n", portid);
454                 return;
455         }
456
457         /* Init port */
458         printf("Initializing port %u... ", portid);
459         fflush(stdout);
460         rte_eth_dev_info_get(portid, &dev_info);
461         local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
462                 dev_info.flow_type_rss_offloads;
463         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
464                 local_port_conf.txmode.offloads |=
465                         DEV_TX_OFFLOAD_MBUF_FAST_FREE;
466         ret = rte_eth_dev_configure(portid, nb_queues, 1, &local_port_conf);
467         if (ret < 0)
468                 rte_exit(EXIT_FAILURE, "Cannot configure device:"
469                         " err=%d, port=%u\n", ret, portid);
470
471         ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
472                         &nb_txd);
473         if (ret < 0)
474                 rte_exit(EXIT_FAILURE,
475                         "Cannot adjust number of descriptors: err=%d, port=%u\n",
476                         ret, portid);
477
478         rte_eth_macaddr_get(portid, &ioat_ports_eth_addr[portid]);
479
480         /* Init RX queues */
481         rxq_conf = dev_info.default_rxconf;
482         rxq_conf.offloads = local_port_conf.rxmode.offloads;
483         for (i = 0; i < nb_queues; i++) {
484                 ret = rte_eth_rx_queue_setup(portid, i, nb_rxd,
485                         rte_eth_dev_socket_id(portid), &rxq_conf,
486                         mbuf_pool);
487                 if (ret < 0)
488                         rte_exit(EXIT_FAILURE,
489                                 "rte_eth_rx_queue_setup:err=%d,port=%u, queue_id=%u\n",
490                                 ret, portid, i);
491         }
492
493         /* Init one TX queue on each port */
494         txq_conf = dev_info.default_txconf;
495         txq_conf.offloads = local_port_conf.txmode.offloads;
496         ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
497                         rte_eth_dev_socket_id(portid),
498                         &txq_conf);
499         if (ret < 0)
500                 rte_exit(EXIT_FAILURE,
501                         "rte_eth_tx_queue_setup:err=%d,port=%u\n",
502                         ret, portid);
503
504         /* Initialize TX buffers */
505         tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
506                         RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
507                         rte_eth_dev_socket_id(portid));
508         if (tx_buffer[portid] == NULL)
509                 rte_exit(EXIT_FAILURE,
510                         "Cannot allocate buffer for tx on port %u\n",
511                         portid);
512
513         rte_eth_tx_buffer_init(tx_buffer[portid], MAX_PKT_BURST);
514
515         /* Start device */
516         ret = rte_eth_dev_start(portid);
517         if (ret < 0)
518                 rte_exit(EXIT_FAILURE,
519                         "rte_eth_dev_start:err=%d, port=%u\n",
520                         ret, portid);
521
522         rte_eth_promiscuous_enable(portid);
523
524         printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
525                         portid,
526                         ioat_ports_eth_addr[portid].addr_bytes[0],
527                         ioat_ports_eth_addr[portid].addr_bytes[1],
528                         ioat_ports_eth_addr[portid].addr_bytes[2],
529                         ioat_ports_eth_addr[portid].addr_bytes[3],
530                         ioat_ports_eth_addr[portid].addr_bytes[4],
531                         ioat_ports_eth_addr[portid].addr_bytes[5]);
532
533         cfg.ports[cfg.nb_ports].rxtx_port = portid;
534         cfg.ports[cfg.nb_ports++].nb_queues = nb_queues;
535 }
536
537 static void
538 signal_handler(int signum)
539 {
540         if (signum == SIGINT || signum == SIGTERM) {
541                 printf("\n\nSignal %d received, preparing to exit...\n",
542                         signum);
543                 force_quit = true;
544         }
545 }
546
547 int
548 main(int argc, char **argv)
549 {
550         int ret;
551         uint16_t nb_ports, portid;
552         uint32_t i;
553         unsigned int nb_mbufs;
554
555         /* Init EAL */
556         ret = rte_eal_init(argc, argv);
557         if (ret < 0)
558                 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
559         argc -= ret;
560         argv += ret;
561
562         force_quit = false;
563         signal(SIGINT, signal_handler);
564         signal(SIGTERM, signal_handler);
565
566         nb_ports = rte_eth_dev_count_avail();
567         if (nb_ports == 0)
568                 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
569
570         /* Parse application arguments (after the EAL ones) */
571         ret = ioat_parse_args(argc, argv, nb_ports);
572         if (ret < 0)
573                 rte_exit(EXIT_FAILURE, "Invalid IOAT arguments\n");
574
575         nb_mbufs = RTE_MAX(nb_ports * (nb_queues * (nb_rxd + nb_txd +
576                 4 * MAX_PKT_BURST) + rte_lcore_count() * MEMPOOL_CACHE_SIZE),
577                 MIN_POOL_SIZE);
578
579         /* Create the mbuf pool */
580         ioat_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", nb_mbufs,
581                 MEMPOOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
582                 rte_socket_id());
583         if (ioat_pktmbuf_pool == NULL)
584                 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
585
586         /* Initialise each port */
587         cfg.nb_ports = 0;
588         RTE_ETH_FOREACH_DEV(portid)
589                 port_init(portid, ioat_pktmbuf_pool, nb_queues);
590
591         while (!check_link_status(ioat_enabled_port_mask) && !force_quit)
592                 sleep(1);
593
594         /* Check if there is enough lcores for all ports. */
595         cfg.nb_lcores = rte_lcore_count() - 1;
596         if (cfg.nb_lcores < 1)
597                 rte_exit(EXIT_FAILURE,
598                         "There should be at least one slave lcore.\n");
599
600         assign_rings();
601
602         start_forwarding_cores();
603
604         /* force_quit is true when we get here */
605         rte_eal_mp_wait_lcore();
606
607         for (i = 0; i < cfg.nb_ports; i++) {
608                 printf("Closing port %d\n", cfg.ports[i].rxtx_port);
609                 rte_eth_dev_stop(cfg.ports[i].rxtx_port);
610                 rte_eth_dev_close(cfg.ports[i].rxtx_port);
611                 rte_ring_free(cfg.ports[i].rx_to_tx_ring);
612         }
613
614         printf("Bye...\n");
615         return 0;
616 }