examples/vm_power: add core list parameter
[dpdk.git] / examples / vm_power_manager / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdint.h>
8 #include <sys/epoll.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <signal.h>
13 #include <errno.h>
14
15 #include <sys/queue.h>
16
17 #include <rte_common.h>
18 #include <rte_eal.h>
19 #include <rte_launch.h>
20 #include <rte_log.h>
21 #include <rte_per_lcore.h>
22 #include <rte_lcore.h>
23 #include <rte_ethdev.h>
24 #include <getopt.h>
25 #include <rte_cycles.h>
26 #include <rte_debug.h>
27
28 #include "channel_manager.h"
29 #include "channel_monitor.h"
30 #include "power_manager.h"
31 #include "vm_power_cli.h"
32 #include "parse.h"
33 #include <rte_pmd_ixgbe.h>
34 #include <rte_pmd_i40e.h>
35 #include <rte_pmd_bnxt.h>
36
37 #define RX_RING_SIZE 1024
38 #define TX_RING_SIZE 1024
39
40 #define NUM_MBUFS 8191
41 #define MBUF_CACHE_SIZE 250
42 #define BURST_SIZE 32
43
44 static uint32_t enabled_port_mask;
45 static volatile bool force_quit;
46
47 /****************/
48 static const struct rte_eth_conf port_conf_default = {
49         .rxmode = {
50                 .max_rx_pkt_len = ETHER_MAX_LEN,
51         },
52 };
53
54 static inline int
55 port_init(uint16_t port, struct rte_mempool *mbuf_pool)
56 {
57         struct rte_eth_conf port_conf = port_conf_default;
58         const uint16_t rx_rings = 1, tx_rings = 1;
59         int retval;
60         uint16_t q;
61         struct rte_eth_dev_info dev_info;
62         struct rte_eth_txconf txq_conf;
63
64         if (!rte_eth_dev_is_valid_port(port))
65                 return -1;
66
67         rte_eth_dev_info_get(port, &dev_info);
68         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
69                 port_conf.txmode.offloads |=
70                         DEV_TX_OFFLOAD_MBUF_FAST_FREE;
71
72         /* Configure the Ethernet device. */
73         retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
74         if (retval != 0)
75                 return retval;
76
77         /* Allocate and set up 1 RX queue per Ethernet port. */
78         for (q = 0; q < rx_rings; q++) {
79                 retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE,
80                                 rte_eth_dev_socket_id(port), NULL, mbuf_pool);
81                 if (retval < 0)
82                         return retval;
83         }
84
85         txq_conf = dev_info.default_txconf;
86         txq_conf.offloads = port_conf.txmode.offloads;
87         /* Allocate and set up 1 TX queue per Ethernet port. */
88         for (q = 0; q < tx_rings; q++) {
89                 retval = rte_eth_tx_queue_setup(port, q, TX_RING_SIZE,
90                                 rte_eth_dev_socket_id(port), &txq_conf);
91                 if (retval < 0)
92                         return retval;
93         }
94
95         /* Start the Ethernet port. */
96         retval = rte_eth_dev_start(port);
97         if (retval < 0)
98                 return retval;
99
100         /* Display the port MAC address. */
101         struct ether_addr addr;
102         rte_eth_macaddr_get(port, &addr);
103         printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
104                            " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
105                         (unsigned int)port,
106                         addr.addr_bytes[0], addr.addr_bytes[1],
107                         addr.addr_bytes[2], addr.addr_bytes[3],
108                         addr.addr_bytes[4], addr.addr_bytes[5]);
109
110         /* Enable RX in promiscuous mode for the Ethernet device. */
111         rte_eth_promiscuous_enable(port);
112
113
114         return 0;
115 }
116
117 static int
118 parse_portmask(const char *portmask)
119 {
120         char *end = NULL;
121         unsigned long pm;
122
123         /* parse hexadecimal string */
124         pm = strtoul(portmask, &end, 16);
125         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
126                 return -1;
127
128         if (pm == 0)
129                 return -1;
130
131         return pm;
132 }
133 /* Parse the argument given in the command line of the application */
134 static int
135 parse_args(int argc, char **argv)
136 {
137         int opt, ret, cnt, i;
138         char **argvopt;
139         uint16_t *oob_enable;
140         int option_index;
141         char *prgname = argv[0];
142         struct core_info *ci;
143         static struct option lgopts[] = {
144                 { "mac-updating", no_argument, 0, 1},
145                 { "no-mac-updating", no_argument, 0, 0},
146                 { "core-list", optional_argument, 0, 'l'},
147                 {NULL, 0, 0, 0}
148         };
149         argvopt = argv;
150         ci = get_core_info();
151
152         while ((opt = getopt_long(argc, argvopt, "l:p:q:T:",
153                                   lgopts, &option_index)) != EOF) {
154
155                 switch (opt) {
156                 /* portmask */
157                 case 'p':
158                         enabled_port_mask = parse_portmask(optarg);
159                         if (enabled_port_mask == 0) {
160                                 printf("invalid portmask\n");
161                                 return -1;
162                         }
163                         break;
164                 case 'l':
165                         oob_enable = malloc(ci->core_count * sizeof(uint16_t));
166                         if (oob_enable == NULL) {
167                                 printf("Error - Unable to allocate memory\n");
168                                 return -1;
169                         }
170                         cnt = parse_set(optarg, oob_enable, ci->core_count);
171                         if (cnt < 0) {
172                                 printf("Invalid core-list - [%s]\n",
173                                                 optarg);
174                                 break;
175                         }
176                         for (i = 0; i < ci->core_count; i++) {
177                                 if (oob_enable[i]) {
178                                         printf("***Using core %d\n", i);
179                                         ci->cd[i].oob_enabled = 1;
180                                         ci->cd[i].global_enabled_cpus = 1;
181                                 }
182                         }
183                         free(oob_enable);
184                         break;
185                 /* long options */
186                 case 0:
187                         break;
188
189                 default:
190                         return -1;
191                 }
192         }
193
194         if (optind >= 0)
195                 argv[optind-1] = prgname;
196
197         ret = optind-1;
198         optind = 0; /* reset getopt lib */
199         return ret;
200 }
201
202 static void
203 check_all_ports_link_status(uint32_t port_mask)
204 {
205 #define CHECK_INTERVAL 100 /* 100ms */
206 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
207         uint16_t portid, count, all_ports_up, print_flag = 0;
208         struct rte_eth_link link;
209
210         printf("\nChecking link status");
211         fflush(stdout);
212         for (count = 0; count <= MAX_CHECK_TIME; count++) {
213                 if (force_quit)
214                         return;
215                 all_ports_up = 1;
216                 RTE_ETH_FOREACH_DEV(portid) {
217                         if (force_quit)
218                                 return;
219                         if ((port_mask & (1 << portid)) == 0)
220                                 continue;
221                         memset(&link, 0, sizeof(link));
222                         rte_eth_link_get_nowait(portid, &link);
223                         /* print link status if flag set */
224                         if (print_flag == 1) {
225                                 if (link.link_status)
226                                         printf("Port %d Link Up - speed %u "
227                                                 "Mbps - %s\n", (uint16_t)portid,
228                                                 (unsigned int)link.link_speed,
229                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
230                                         ("full-duplex") : ("half-duplex\n"));
231                                 else
232                                         printf("Port %d Link Down\n",
233                                                 (uint16_t)portid);
234                                 continue;
235                         }
236                        /* clear all_ports_up flag if any link down */
237                         if (link.link_status == ETH_LINK_DOWN) {
238                                 all_ports_up = 0;
239                                 break;
240                         }
241                 }
242                 /* after finally printing all link status, get out */
243                 if (print_flag == 1)
244                         break;
245
246                 if (all_ports_up == 0) {
247                         printf(".");
248                         fflush(stdout);
249                         rte_delay_ms(CHECK_INTERVAL);
250                 }
251
252                 /* set the print_flag if all ports up or timeout */
253                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
254                         print_flag = 1;
255                         printf("done\n");
256                 }
257         }
258 }
259 static int
260 run_monitor(__attribute__((unused)) void *arg)
261 {
262         if (channel_monitor_init() < 0) {
263                 printf("Unable to initialize channel monitor\n");
264                 return -1;
265         }
266         run_channel_monitor();
267         return 0;
268 }
269
270 static void
271 sig_handler(int signo)
272 {
273         printf("Received signal %d, exiting...\n", signo);
274         channel_monitor_exit();
275         channel_manager_exit();
276         power_manager_exit();
277
278 }
279
280 int
281 main(int argc, char **argv)
282 {
283         int ret;
284         unsigned lcore_id;
285         unsigned int nb_ports;
286         struct rte_mempool *mbuf_pool;
287         uint16_t portid;
288
289
290         ret = core_info_init();
291         if (ret < 0)
292                 rte_panic("Cannot allocate core info\n");
293
294         ret = rte_eal_init(argc, argv);
295         if (ret < 0)
296                 rte_panic("Cannot init EAL\n");
297
298         signal(SIGINT, sig_handler);
299         signal(SIGTERM, sig_handler);
300
301         argc -= ret;
302         argv += ret;
303
304         /* parse application arguments (after the EAL ones) */
305         ret = parse_args(argc, argv);
306         if (ret < 0)
307                 rte_exit(EXIT_FAILURE, "Invalid arguments\n");
308
309         nb_ports = rte_eth_dev_count_avail();
310
311         if (nb_ports > 0) {
312                 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL",
313                                 NUM_MBUFS * nb_ports, MBUF_CACHE_SIZE, 0,
314                                 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
315
316                 if (mbuf_pool == NULL)
317                         rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
318
319                 /* Initialize ports. */
320                 RTE_ETH_FOREACH_DEV(portid) {
321                         struct ether_addr eth;
322                         int w, j;
323                         int ret;
324
325                         if ((enabled_port_mask & (1 << portid)) == 0)
326                                 continue;
327
328                         eth.addr_bytes[0] = 0xe0;
329                         eth.addr_bytes[1] = 0xe0;
330                         eth.addr_bytes[2] = 0xe0;
331                         eth.addr_bytes[3] = 0xe0;
332                         eth.addr_bytes[4] = portid + 0xf0;
333
334                         if (port_init(portid, mbuf_pool) != 0)
335                                 rte_exit(EXIT_FAILURE,
336                                         "Cannot init port %"PRIu8 "\n",
337                                         portid);
338
339                         for (w = 0; w < MAX_VFS; w++) {
340                                 eth.addr_bytes[5] = w + 0xf0;
341
342                                 ret = rte_pmd_ixgbe_set_vf_mac_addr(portid,
343                                                         w, &eth);
344                                 if (ret == -ENOTSUP)
345                                         ret = rte_pmd_i40e_set_vf_mac_addr(
346                                                         portid, w, &eth);
347                                 if (ret == -ENOTSUP)
348                                         ret = rte_pmd_bnxt_set_vf_mac_addr(
349                                                         portid, w, &eth);
350
351                                 switch (ret) {
352                                 case 0:
353                                         printf("Port %d VF %d MAC: ",
354                                                         portid, w);
355                                         for (j = 0; j < 5; j++) {
356                                                 printf("%02x:",
357                                                         eth.addr_bytes[j]);
358                                         }
359                                         printf("%02x\n", eth.addr_bytes[5]);
360                                         break;
361                                 }
362                                 printf("\n");
363                                 break;
364                         }
365                 }
366         }
367
368         lcore_id = rte_get_next_lcore(-1, 1, 0);
369         if (lcore_id == RTE_MAX_LCORE) {
370                 RTE_LOG(ERR, EAL, "A minimum of two cores are required to run "
371                                 "application\n");
372                 return 0;
373         }
374
375         check_all_ports_link_status(enabled_port_mask);
376         rte_eal_remote_launch(run_monitor, NULL, lcore_id);
377
378         if (power_manager_init() < 0) {
379                 printf("Unable to initialize power manager\n");
380                 return -1;
381         }
382         if (channel_manager_init(CHANNEL_MGR_DEFAULT_HV_PATH) < 0) {
383                 printf("Unable to initialize channel manager\n");
384                 return -1;
385         }
386         run_cli(NULL);
387
388         rte_eal_mp_wait_lcore();
389         return 0;
390 }