app/flow-perf: add flow performance skeleton
[dpdk.git] / app / test-flow-perf / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2020 Mellanox Technologies, Ltd
3  *
4  * This file contain the application main file
5  * This application provides the user the ability to test the
6  * insertion rate for specific rte_flow rule under stress state ~4M rule/
7  *
8  * Then it will also provide packet per second measurement after installing
9  * all rules, the user may send traffic to test the PPS that match the rules
10  * after all rules are installed, to check performance or functionality after
11  * the stress.
12  *
13  * The flows insertion will go for all ports first, then it will print the
14  * results, after that the application will go into forwarding packets mode
15  * it will start receiving traffic if any and then forwarding it back and
16  * gives packet per second measurement.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdint.h>
23 #include <inttypes.h>
24 #include <stdarg.h>
25 #include <errno.h>
26 #include <getopt.h>
27 #include <stdbool.h>
28 #include <sys/time.h>
29
30 #include <rte_malloc.h>
31 #include <rte_mempool.h>
32 #include <rte_mbuf.h>
33 #include <rte_ethdev.h>
34 #include <rte_flow.h>
35
36 #include "config.h"
37
38 static struct rte_mempool *mbuf_mp;
39 static uint32_t nb_lcores;
40
41 static void
42 usage(char *progname)
43 {
44         printf("\nusage: %s\n", progname);
45 }
46
47 static void
48 args_parse(int argc, char **argv)
49 {
50         char **argvopt;
51         int opt;
52         int opt_idx;
53         static struct option lgopts[] = {
54                 /* Control */
55                 { "help",                       0, 0, 0 },
56         };
57
58         argvopt = argv;
59
60         while ((opt = getopt_long(argc, argvopt, "",
61                                 lgopts, &opt_idx)) != EOF) {
62                 switch (opt) {
63                 case 0:
64                         if (strcmp(lgopts[opt_idx].name, "help") == 0) {
65                                 usage(argv[0]);
66                                 rte_exit(EXIT_SUCCESS, "Displayed help\n");
67                         }
68                         break;
69                 default:
70                         fprintf(stderr, "Invalid option: %s\n", argv[optind]);
71                         usage(argv[0]);
72                         rte_exit(EXIT_SUCCESS, "Invalid option\n");
73                         break;
74                 }
75         }
76 }
77
78 static void
79 init_port(void)
80 {
81         int ret;
82         uint16_t std_queue;
83         uint16_t port_id;
84         uint16_t nr_ports;
85         struct rte_eth_conf port_conf = {
86                 .rx_adv_conf = {
87                         .rss_conf.rss_hf =
88                                 GET_RSS_HF(),
89                 }
90         };
91         struct rte_eth_txconf txq_conf;
92         struct rte_eth_rxconf rxq_conf;
93         struct rte_eth_dev_info dev_info;
94
95         nr_ports = rte_eth_dev_count_avail();
96         if (nr_ports == 0)
97                 rte_exit(EXIT_FAILURE, "Error: no port detected\n");
98
99         mbuf_mp = rte_pktmbuf_pool_create("mbuf_pool",
100                                         TOTAL_MBUF_NUM, MBUF_CACHE_SIZE,
101                                         0, MBUF_SIZE,
102                                         rte_socket_id());
103         if (mbuf_mp == NULL)
104                 rte_exit(EXIT_FAILURE, "Error: can't init mbuf pool\n");
105
106         for (port_id = 0; port_id < nr_ports; port_id++) {
107                 ret = rte_eth_dev_info_get(port_id, &dev_info);
108                 if (ret != 0)
109                         rte_exit(EXIT_FAILURE,
110                                 "Error during getting device"
111                                 " (port %u) info: %s\n",
112                                 port_id, strerror(-ret));
113
114                 port_conf.txmode.offloads &= dev_info.tx_offload_capa;
115                 port_conf.rxmode.offloads &= dev_info.rx_offload_capa;
116
117                 printf(":: initializing port: %d\n", port_id);
118
119                 ret = rte_eth_dev_configure(port_id, RXQ_NUM,
120                                 TXQ_NUM, &port_conf);
121                 if (ret < 0)
122                         rte_exit(EXIT_FAILURE,
123                                 ":: cannot configure device: err=%d, port=%u\n",
124                                 ret, port_id);
125
126                 rxq_conf = dev_info.default_rxconf;
127                 for (std_queue = 0; std_queue < RXQ_NUM; std_queue++) {
128                         ret = rte_eth_rx_queue_setup(port_id, std_queue, NR_RXD,
129                                         rte_eth_dev_socket_id(port_id),
130                                         &rxq_conf,
131                                         mbuf_mp);
132                         if (ret < 0)
133                                 rte_exit(EXIT_FAILURE,
134                                         ":: Rx queue setup failed: err=%d, port=%u\n",
135                                         ret, port_id);
136                 }
137
138                 txq_conf = dev_info.default_txconf;
139                 for (std_queue = 0; std_queue < TXQ_NUM; std_queue++) {
140                         ret = rte_eth_tx_queue_setup(port_id, std_queue, NR_TXD,
141                                         rte_eth_dev_socket_id(port_id),
142                                         &txq_conf);
143                         if (ret < 0)
144                                 rte_exit(EXIT_FAILURE,
145                                         ":: Tx queue setup failed: err=%d, port=%u\n",
146                                         ret, port_id);
147                 }
148
149                 /* Catch all packets from traffic generator. */
150                 ret = rte_eth_promiscuous_enable(port_id);
151                 if (ret != 0)
152                         rte_exit(EXIT_FAILURE,
153                                 ":: promiscuous mode enable failed: err=%s, port=%u\n",
154                                 rte_strerror(-ret), port_id);
155
156                 ret = rte_eth_dev_start(port_id);
157                 if (ret < 0)
158                         rte_exit(EXIT_FAILURE,
159                                 "rte_eth_dev_start:err=%d, port=%u\n",
160                                 ret, port_id);
161
162                 printf(":: initializing port: %d done\n", port_id);
163         }
164 }
165
166 int
167 main(int argc, char **argv)
168 {
169         int ret;
170         uint16_t port;
171         struct rte_flow_error error;
172
173         ret = rte_eal_init(argc, argv);
174         if (ret < 0)
175                 rte_exit(EXIT_FAILURE, "EAL init failed\n");
176
177         argc -= ret;
178         argv += ret;
179         if (argc > 1)
180                 args_parse(argc, argv);
181
182         init_port();
183
184         nb_lcores = rte_lcore_count();
185         if (nb_lcores <= 1)
186                 rte_exit(EXIT_FAILURE, "This app needs at least two cores\n");
187
188         RTE_ETH_FOREACH_DEV(port) {
189                 rte_flow_flush(port, &error);
190                 rte_eth_dev_stop(port);
191                 rte_eth_dev_close(port);
192         }
193         return 0;
194 }